Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement starknet_getMessageStatus #2184

Merged
merged 38 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
75408b2
Implement starknet_getMessageStatus
rianhughes Sep 27, 2024
066a1b4
run make generate
rianhughes Oct 8, 2024
3e5d823
txn status update
rianhughes Oct 8, 2024
ccfda3f
confirmed that the reert_error also covers rejected errors
rianhughes Oct 8, 2024
46432d2
update bucket name
rianhughes Oct 22, 2024
0c3e833
migrate to store historical l1 handler msg hashes
rianhughes Oct 9, 2024
db22940
bug fix
rianhughes Oct 9, 2024
13f72a9
address comments
rianhughes Oct 11, 2024
d07fbf9
lint
rianhughes Oct 11, 2024
12459bd
update calculateBlockCommitments
rianhughes Oct 11, 2024
760f220
lint
rianhughes Oct 11, 2024
e998e37
add migration test TestL1HandlerTxns
rianhughes Oct 11, 2024
c88ee9b
update TestL1HandlerTxns
rianhughes Oct 11, 2024
39befd0
fail to write error - wip..
rianhughes Oct 11, 2024
3a779b6
fix test
rianhughes Oct 11, 2024
ebff33e
register starknet_getMessagesStatus
rianhughes Oct 23, 2024
9ff5c66
lint
rianhughes Oct 23, 2024
b21dd29
address comments
rianhughes Oct 31, 2024
03385d5
revert grpc/gen changes
rianhughes Oct 31, 2024
200503c
hard copy
rianhughes Nov 1, 2024
94c9569
txn.Hash().Marshal()
rianhughes Nov 1, 2024
615807f
fmt
rianhughes Nov 1, 2024
e16cab2
add Sepolia test - theres a bug in mshHash
rianhughes Nov 4, 2024
b9d107c
Update rpc/l1.go
rianhughes Nov 7, 2024
226c576
update tests and make Hash func private
rianhughes Nov 7, 2024
148187c
reduce repitition in node.go
rianhughes Nov 7, 2024
f3a09f6
to squash
rianhughes Nov 7, 2024
fb08240
remove EthClient
rianhughes Nov 7, 2024
60d072e
Merge branch 'main' into rianhughes/rpc8-getMessageStatus2
rianhughes Nov 7, 2024
8b717e2
update the l1HandlerTxnHashByMsgHash
rianhughes Nov 7, 2024
bc2d101
Refactor l1HandlerTxnHash test and get fn
IronGauntlets Nov 7, 2024
86c6384
Remove misleading todo
IronGauntlets Nov 7, 2024
fba5243
Add L1() to l1Client to return the l1 subscriber
IronGauntlets Nov 7, 2024
9612e7a
Add l1Client interface in rpc package
IronGauntlets Nov 7, 2024
ffd8b33
Remove unnecessary warning
IronGauntlets Nov 7, 2024
cb8e1cb
Make logMessageToL2 private
IronGauntlets Nov 7, 2024
810a6bd
Remove ErrL1ClientNotFound variable
IronGauntlets Nov 7, 2024
c7dfcb9
Merge branch 'main' into rianhughes/rpc8-getMessageStatus2
kirugan Nov 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 58 additions & 10 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"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 @@
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) (l1HandlerTxnHash *felt.Felt, err error)

HeadState() (core.StateReader, StateCloser, error)
StateAtBlockHash(blockHash *felt.Felt) (core.StateReader, StateCloser, error)
Expand Down Expand Up @@ -115,12 +117,12 @@
var height uint64
return height, b.database.View(func(txn db.Transaction) error {
var err error
height, err = chainHeight(txn)
height, err = ChainHeight(txn)
return err
})
}

func chainHeight(txn db.Transaction) (uint64, error) {
func ChainHeight(txn db.Transaction) (uint64, error) {
IronGauntlets marked this conversation as resolved.
Show resolved Hide resolved
var height uint64
return height, txn.Get(db.ChainHeight.Key(), func(val []byte) error {
height = binary.BigEndian.Uint64(val)
Expand Down Expand Up @@ -150,15 +152,15 @@
}

func head(txn db.Transaction) (*core.Block, error) {
height, err := chainHeight(txn)
height, err := ChainHeight(txn)
if err != nil {
return nil, err
}
return BlockByNumber(txn, height)
}

func headsHeader(txn db.Transaction) (*core.Header, error) {
height, err := chainHeight(txn)
height, err := ChainHeight(txn)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -226,6 +228,16 @@
})
}

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 = l1HandlerTxnHashByMsgHash(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 @@
return err
}

if err := StoreL1HandlerMsgHashes(txn, block.Transactions); err != nil {
return err
}

Check warning on line 380 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L379-L380

Added lines #L379 - L380 were not covered by tests

if err := b.storeEmptyPending(txn, block.Header); err != nil {
return err
}
Expand Down Expand Up @@ -497,7 +513,7 @@

block := new(core.Block)
block.Header = header
block.Transactions, err = transactionsByBlockNumber(txn, number)
block.Transactions, err = TransactionsByBlockNumber(txn, number)
if err != nil {
return nil, err
}
Expand All @@ -509,7 +525,7 @@
return block, nil
}

func transactionsByBlockNumber(txn db.Transaction, number uint64) ([]core.Transaction, error) {
func TransactionsByBlockNumber(txn db.Transaction, number uint64) ([]core.Transaction, error) {
iterator, err := txn.NewIterator()
if err != nil {
return nil, err
Expand Down Expand Up @@ -589,6 +605,18 @@
})
}

func StoreL1HandlerMsgHashes(dbTxn db.Transaction, blockTxns []core.Transaction) error {
for _, txn := range blockTxns {
if l1Handler, ok := (txn).(*core.L1HandlerTransaction); ok {
err := dbTxn.Set(db.L1HandlerTxnHashByMsgHash.Key(l1Handler.MessageHash()), txn.Hash().Marshal())
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#L613-L614

Added lines #L613 - L614 were 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 +650,21 @@
})
}

func l1HandlerTxnHashByMsgHash(txn db.Transaction, l1HandlerMsgHash *common.Hash) (*felt.Felt, error) {
l1HandlerTxnHash := new(felt.Felt)
err := txn.Get(db.L1HandlerTxnHashByMsgHash.Key(l1HandlerMsgHash.Bytes()), func(val []byte) error {
if len(val) == 0 {
rianhughes marked this conversation as resolved.
Show resolved Hide resolved
return db.ErrKeyNotFound
}

Check warning on line 658 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L657-L658

Added lines #L657 - L658 were not covered by tests
l1HandlerTxnHash.Unmarshal(val)
return nil
})
if err != nil {
return nil, err
}
return l1HandlerTxnHash, 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 @@ -761,7 +804,7 @@
return nil, nil, err
}

_, err = chainHeight(txn)
_, err = ChainHeight(txn)
if err != nil {
return nil, nil, utils.RunAndWrapOnError(txn.Discard, err)
}
Expand Down Expand Up @@ -815,7 +858,7 @@
return nil, err
}

latest, err := chainHeight(txn)
latest, err := ChainHeight(txn)
if err != nil {
return nil, err
}
Expand All @@ -831,7 +874,7 @@
func (b *Blockchain) GetReverseStateDiff() (*core.StateDiff, error) {
var reverseStateDiff *core.StateDiff
return reverseStateDiff, b.database.View(func(txn db.Transaction) error {
blockNumber, err := chainHeight(txn)
blockNumber, err := ChainHeight(txn)
if err != nil {
return err
}
Expand All @@ -846,7 +889,7 @@
}

func (b *Blockchain) revertHead(txn db.Transaction) error {
blockNumber, err := chainHeight(txn)
blockNumber, err := ChainHeight(txn)
if err != nil {
return err
}
Expand Down Expand Up @@ -933,6 +976,11 @@
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.L1HandlerTxnHashByMsgHash.Key(l1handler.MessageHash())); err != nil {
return err
}

Check warning on line 982 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L980-L982

Added lines #L980 - L982 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")
rianhughes marked this conversation as resolved.
Show resolved Hide resolved
hash, err := chain.L1HandlerTxnHash(&nonExistentMsgHash)
require.Nil(t, hash)
require.Equal(t, db.ErrKeyNotFound, err)
rianhughes marked this conversation as resolved.
Show resolved Hide resolved
})

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)
rianhughes marked this conversation as resolved.
Show resolved Hide resolved
})

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)
rianhughes marked this conversation as resolved.
Show resolved Hide resolved
})
}

Expand Down
2 changes: 1 addition & 1 deletion blockchain/event_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type FilteredEvent struct {
//nolint:gocyclo
func (e *EventFilter) Events(cToken *ContinuationToken, chunkSize uint64) ([]*FilteredEvent, *ContinuationToken, error) {
var matchedEvents []*FilteredEvent
latest, err := chainHeight(e.txn)
latest, err := ChainHeight(e.txn)
if err != nil {
return nil, nil, err
}
Expand Down
Loading