Skip to content

Commit

Permalink
Implement starknet_getMessageStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Oct 3, 2024
1 parent 0c0700c commit 4f6fd6d
Show file tree
Hide file tree
Showing 15 changed files with 8,717 additions and 10 deletions.
50 changes: 49 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/encoder"
"github.com/NethermindEth/juno/utils"
"github.com/ethereum/go-ethereum/common"
)

//go:generate mockgen -destination=../mocks/mock_blockchain.go -package=mocks github.com/NethermindEth/juno/blockchain Reader
Expand All @@ -34,6 +35,7 @@ type Reader interface {
Receipt(hash *felt.Felt) (receipt *core.TransactionReceipt, blockHash *felt.Felt, blockNumber uint64, err error)
StateUpdateByNumber(number uint64) (update *core.StateUpdate, err error)
StateUpdateByHash(hash *felt.Felt) (update *core.StateUpdate, err error)
L1HandlerTxnHash(msgHash *common.Hash) (l1HandlerTxnHashes *felt.Felt, err error)

HeadState() (core.StateReader, StateCloser, error)
StateAtBlockHash(blockHash *felt.Felt) (core.StateReader, StateCloser, error)
Expand Down Expand Up @@ -226,6 +228,16 @@ func (b *Blockchain) StateUpdateByHash(hash *felt.Felt) (*core.StateUpdate, erro
})
}

func (b *Blockchain) L1HandlerTxnHash(msgHash *common.Hash) (*felt.Felt, error) {
b.listener.OnRead("L1HandlerTxnHash")
var l1HandlerTxnHash *felt.Felt
return l1HandlerTxnHash, b.database.View(func(txn db.Transaction) error {
var err error
l1HandlerTxnHash, err = l1MsgHashes(txn, msgHash)
return err
})
}

// TransactionByBlockNumberAndIndex gets the transaction for a given block number and index.
func (b *Blockchain) TransactionByBlockNumberAndIndex(blockNumber, index uint64) (core.Transaction, error) {
b.listener.OnRead("TransactionByBlockNumberAndIndex")
Expand Down Expand Up @@ -363,6 +375,10 @@ func (b *Blockchain) Store(block *core.Block, blockCommitments *core.BlockCommit
return err
}

if err := storeL1HandlerMsgHashes(txn, block); err != nil {
return err

Check warning on line 379 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L379

Added line #L379 was not covered by tests
}

if err := b.storeEmptyPending(txn, block.Header); err != nil {
return err
}
Expand Down Expand Up @@ -589,6 +605,19 @@ func blockByHash(txn db.Transaction, hash *felt.Felt) (*core.Block, error) {
})
}

func storeL1HandlerMsgHashes(dbTxn db.Transaction, block *core.Block) error {
for _, txn := range block.Transactions {
if l1Handler, ok := (txn).(*core.L1HandlerTransaction); ok {
l1HandlerTxnHashBytes := txn.Hash().Bytes()
err := dbTxn.Set(db.L1HandlerTxnHash.Key(l1Handler.MessageHash()), l1HandlerTxnHashBytes[:])
if err != nil {
return err

Check warning on line 614 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L614

Added line #L614 was not covered by tests
}
}
}
return nil
}

func storeStateUpdate(txn db.Transaction, blockNumber uint64, update *core.StateUpdate) error {
numBytes := core.MarshalBlockNumber(blockNumber)

Expand Down Expand Up @@ -622,6 +651,21 @@ func stateUpdateByHash(txn db.Transaction, hash *felt.Felt) (*core.StateUpdate,
})
}

func l1MsgHashes(txn db.Transaction, l1HandlerMsgHash *common.Hash) (*felt.Felt, error) {
var raw []byte
err := txn.Get(db.L1HandlerTxnHash.Key(l1HandlerMsgHash.Bytes()), func(val []byte) error {
if len(val) == 0 {
return db.ErrKeyNotFound

Check warning on line 658 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L658

Added line #L658 was not covered by tests
}
raw = val
return nil
})
if err != nil {
return nil, err
}
return new(felt.Felt).SetBytes(raw), nil
}

// SanityCheckNewHeight checks integrity of a block and resulting state update
func (b *Blockchain) SanityCheckNewHeight(block *core.Block, stateUpdate *core.StateUpdate,
newClasses map[felt.Felt]core.Class,
Expand Down Expand Up @@ -874,7 +918,6 @@ func (b *Blockchain) revertHead(txn db.Transaction) error {
}

// Revert chain height and pending.

if genesisBlock {
if err = txn.Delete(db.Pending.Key()); err != nil {
return err
Expand Down Expand Up @@ -917,6 +960,11 @@ func removeTxsAndReceipts(txn db.Transaction, blockNumber, numTxs uint64) error
if err = txn.Delete(db.TransactionBlockNumbersAndIndicesByHash.Key(reorgedTxn.Hash().Marshal())); err != nil {
return err
}
if l1handler, ok := reorgedTxn.(*core.L1HandlerTransaction); ok {
if err = txn.Delete(db.L1HandlerTxnHash.Key(l1handler.MessageHash())); err != nil {
return err

Check warning on line 965 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L964-L965

Added lines #L964 - L965 were not covered by tests
}
}
}

return nil
Expand Down
31 changes: 31 additions & 0 deletions blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/NethermindEth/juno/mocks"
adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder"
"github.com/NethermindEth/juno/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -238,7 +239,13 @@ func TestStore(t *testing.T) {
got0Update, err := chain.StateUpdateByHash(block0.Hash)
require.NoError(t, err)
assert.Equal(t, stateUpdate0, got0Update)

nonExistentMsgHash := common.HexToHash("0xcoffeebabe")
hash, err := chain.L1HandlerTxnHash(&nonExistentMsgHash)
require.Nil(t, hash)
require.Equal(t, db.ErrKeyNotFound, err)
})

t.Run("add block to non-empty blockchain", func(t *testing.T) {
block1, err := gw.BlockByNumber(context.Background(), 1)
require.NoError(t, err)
Expand All @@ -265,6 +272,30 @@ func TestStore(t *testing.T) {
got1Update, err := chain.StateUpdateByNumber(1)
require.NoError(t, err)
assert.Equal(t, stateUpdate1, got1Update)

nonExistentMsgHash := common.HexToHash("0xcoffeebabe")
hash, err := chain.L1HandlerTxnHash(&nonExistentMsgHash)
require.Nil(t, hash)
require.Equal(t, db.ErrKeyNotFound, err)
})

t.Run("add block with L1 Handler Txn", func(t *testing.T) {
client := feeder.NewTestClient(t, &utils.Sepolia)
gw := adaptfeeder.New(client)
chain := blockchain.New(pebble.NewMemTest(t), &utils.Sepolia)
var block *core.Block
var stateUpdate *core.StateUpdate
for i := range uint64(7) {
block, err = gw.BlockByNumber(context.Background(), i)
require.NoError(t, err)
stateUpdate, err = gw.StateUpdate(context.Background(), i)
require.NoError(t, err)
require.NoError(t, chain.Store(block, &emptyCommitments, stateUpdate, nil))
}
l1HandlerMsgHash := common.HexToHash("0x42e76df4e3d5255262929c27132bd0d295a8d3db2cfe63d2fcd061c7a7a7ab34")
l1HandlerTxnHash, err := chain.L1HandlerTxnHash(&l1HandlerMsgHash)
require.NoError(t, err)
require.Equal(t, utils.HexToFelt(t, "0x785c2ada3f53fbc66078d47715c27718f92e6e48b96372b36e5197de69b82b5"), l1HandlerTxnHash)
})
}

Expand Down
Loading

0 comments on commit 4f6fd6d

Please sign in to comment.