Skip to content

Commit

Permalink
Refactor blockchain.Pending to return a reference
Browse files Browse the repository at this point in the history
  • Loading branch information
IronGauntlets committed Dec 12, 2024
1 parent 7634b9f commit 1b41439
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 32 deletions.
26 changes: 10 additions & 16 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Reader interface {

EventFilter(from *felt.Felt, keys [][]felt.Felt) (EventFilterer, error)

Pending() (Pending, error)
Pending() (*Pending, error)

Network() *utils.Network
}
Expand Down Expand Up @@ -269,7 +269,7 @@ func (b *Blockchain) TransactionByHash(hash *felt.Felt) (core.Transaction, error

// not found in the canonical blocks, try pending
if errors.Is(err, db.ErrKeyNotFound) {
var pending Pending
var pending *Pending
pending, err = pendingBlock(txn)
if err != nil {
return err
Expand Down Expand Up @@ -302,7 +302,7 @@ func (b *Blockchain) Receipt(hash *felt.Felt) (*core.TransactionReceipt, *felt.F

// not found in the canonical blocks, try pending
if errors.Is(err, db.ErrKeyNotFound) {
var pending Pending
var pending *Pending
pending, err = pendingBlock(txn)
if err != nil {
if !errors.Is(err, ErrPendingBlockNotFound) {
Expand Down Expand Up @@ -1009,32 +1009,30 @@ func (b *Blockchain) StorePending(pending *Pending) error {
})
}

// Todo:
// 2. Consider returning reference to Pending instead of Pending
func pendingBlock(txn db.Transaction) (Pending, error) {
func pendingBlock(txn db.Transaction) (*Pending, error) {
pending := cachedPending.Load()
if pending == nil {
return Pending{}, ErrPendingBlockNotFound
return nil, ErrPendingBlockNotFound
}

expectedParentHash := &felt.Zero
if head, err := headsHeader(txn); err == nil {
expectedParentHash = head.Hash
}
if pending.Block.ParentHash.Equal(expectedParentHash) {
return *pending, nil
return pending, nil
}

// Since the pending block in the cache is outdated remove it
cachedPending.Store(nil)

return Pending{}, ErrPendingBlockNotFound
return nil, ErrPendingBlockNotFound
}

// Pending returns the pending block from the database
func (b *Blockchain) Pending() (Pending, error) {
func (b *Blockchain) Pending() (*Pending, error) {
b.listener.OnRead("Pending")
var pending Pending
var pending *Pending
return pending, b.database.View(func(txn db.Transaction) error {
var err error
pending, err = pendingBlock(txn)
Expand All @@ -1055,9 +1053,5 @@ func (b *Blockchain) PendingState() (core.StateReader, StateCloser, error) {
return nil, nil, utils.RunAndWrapOnError(txn.Discard, err)
}

return NewPendingState(
pending.StateUpdate.StateDiff,
pending.NewClasses,
core.NewState(txn),
), txn.Discard, nil
return NewPendingState(pending.StateUpdate.StateDiff, pending.NewClasses, core.NewState(txn)), txn.Discard, nil
}
8 changes: 4 additions & 4 deletions blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,11 +706,11 @@ func TestPending(t *testing.T) {
})

t.Run("store genesis as pending", func(t *testing.T) {
pendingGenesis := blockchain.Pending{
pendingGenesis := &blockchain.Pending{
Block: b,
StateUpdate: su,
}
require.NoError(t, chain.StorePending(&pendingGenesis))
require.NoError(t, chain.StorePending(pendingGenesis))

gotPending, pErr := chain.Pending()
require.NoError(t, pErr)
Expand Down Expand Up @@ -739,11 +739,11 @@ func TestPending(t *testing.T) {
su, err = gw.StateUpdate(context.Background(), 1)
require.NoError(t, err)

expectedPending := blockchain.Pending{
expectedPending := &blockchain.Pending{
Block: b,
StateUpdate: su,
}
require.NoError(t, chain.StorePending(&expectedPending))
require.NoError(t, chain.StorePending(expectedPending))

gotPending, pErr := chain.Pending()
require.NoError(t, pErr)
Expand Down
4 changes: 2 additions & 2 deletions mocks/mock_blockchain.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions rpc/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func TestBlockTransactionCount(t *testing.T) {
t.Run("blockID - pending", func(t *testing.T) {
latestBlock.Hash = nil
latestBlock.GlobalStateRoot = nil
mockReader.EXPECT().Pending().Return(blockchain.Pending{
mockReader.EXPECT().Pending().Return(&blockchain.Pending{
Block: latestBlock,
}, nil)

Expand Down Expand Up @@ -333,7 +333,7 @@ func TestBlockWithTxHashes(t *testing.T) {
t.Run("blockID - pending", func(t *testing.T) {
latestBlock.Hash = nil
latestBlock.GlobalStateRoot = nil
mockReader.EXPECT().Pending().Return(blockchain.Pending{
mockReader.EXPECT().Pending().Return(&blockchain.Pending{
Block: latestBlock,
}, nil)
mockReader.EXPECT().L1Head().Return(nil, db.ErrKeyNotFound)
Expand Down Expand Up @@ -470,7 +470,7 @@ func TestBlockWithTxs(t *testing.T) {
t.Run("blockID - pending", func(t *testing.T) {
latestBlock.Hash = nil
latestBlock.GlobalStateRoot = nil
mockReader.EXPECT().Pending().Return(blockchain.Pending{
mockReader.EXPECT().Pending().Return(&blockchain.Pending{
Block: latestBlock,
}, nil).Times(2)
mockReader.EXPECT().L1Head().Return(nil, db.ErrKeyNotFound).Times(2)
Expand Down
10 changes: 6 additions & 4 deletions rpc/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ func (h *Handler) blockByID(id *BlockID) (*core.Block, *jsonrpc.Error) {
case id.Hash != nil:
block, err = h.bcReader.BlockByHash(id.Hash)
case id.Pending:
var pending blockchain.Pending
var pending *blockchain.Pending
pending, err = h.bcReader.Pending()
block = pending.Block
if err == nil {
block = pending.Block
}
default:
block, err = h.bcReader.BlockByNumber(id.Number)
}
Expand All @@ -66,9 +68,9 @@ func (h *Handler) blockHeaderByID(id *BlockID) (*core.Header, *jsonrpc.Error) {
case id.Hash != nil:
header, err = h.bcReader.BlockHeaderByHash(id.Hash)
case id.Pending:
var pending blockchain.Pending
var pending *blockchain.Pending
pending, err = h.bcReader.Pending()
if pending.Block != nil {
if err == nil {
header = pending.Block.Header
}
default:
Expand Down
2 changes: 1 addition & 1 deletion rpc/state_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (h *Handler) StateUpdate(id BlockID) (*StateUpdate, *jsonrpc.Error) {
update, err = h.bcReader.StateUpdateByNumber(height)
}
} else if id.Pending {
var pending blockchain.Pending
var pending *blockchain.Pending
pending, err = h.bcReader.Pending()
if err == nil {
update = pending.StateUpdate
Expand Down
2 changes: 1 addition & 1 deletion rpc/state_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestStateUpdate(t *testing.T) {
t.Run("pending", func(t *testing.T) {
update21656.BlockHash = nil
update21656.NewRoot = nil
mockReader.EXPECT().Pending().Return(blockchain.Pending{
mockReader.EXPECT().Pending().Return(&blockchain.Pending{
StateUpdate: update21656,
}, nil)

Expand Down
2 changes: 1 addition & 1 deletion rpc/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func TestTransactionByBlockIdAndIndex(t *testing.T) {

latestBlock.Hash = nil
latestBlock.GlobalStateRoot = nil
mockReader.EXPECT().Pending().Return(blockchain.Pending{
mockReader.EXPECT().Pending().Return(&blockchain.Pending{
Block: latestBlock,
}, nil)
mockReader.EXPECT().TransactionByHash(latestBlock.Transactions[index].Hash()).DoAndReturn(
Expand Down

0 comments on commit 1b41439

Please sign in to comment.