Skip to content

Commit

Permalink
Implement starknet_getMessageStatus - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Sep 26, 2024
1 parent b240aba commit d1215c9
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
84 changes: 84 additions & 0 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"math/big"
"sync/atomic"
"time"

Expand All @@ -14,6 +15,9 @@ import (
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/encoder"
"github.com/NethermindEth/juno/utils"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)

//go:generate mockgen -destination=../mocks/mock_blockchain.go -package=mocks github.com/NethermindEth/juno/blockchain Reader
Expand Down Expand Up @@ -418,6 +422,86 @@ func StoreBlockCommitments(txn db.Transaction, blockNumber uint64, commitments *
return txn.Set(db.BlockCommitments.Key(numBytes), commitmentBytes)
}

// L1Hash computes the L1 transaction hash that created the l1 handler transaction
func L1Hash(tx *core.L1HandlerTransaction) (*felt.Felt, error) {
// Define ABI types
uint256Ty, _ := abi.NewType("uint256", "", nil)
bytes32Ty, _ := abi.NewType("bytes32", "", nil)
addressTy, _ := abi.NewType("address", "", nil)

fromAddress := tx.CallData[0].Bytes()
toAddress := tx.ContractAddress.Bytes()
nonce := tx.Nonce.Bytes()
selector := tx.EntryPointSelector.Bytes()

// Pack each argument according to its type
arguments := abi.Arguments{
{
Type: addressTy, // fromAddress (tx.calldata[0])
},
{
Type: addressTy, // toAddress (tx.ContractAddress)
},
{
Type: bytes32Ty, // nonce
},
{
Type: bytes32Ty, // selector (tx.EntryPointSelector)
},
{
Type: uint256Ty, // payload length (len(tx.CallData))
},
}

// https://docs.starknet.io/architecture-and-concepts/network-architecture/messaging-mechanism/#hashing_l1-l2
packedBytes, err := arguments.Pack(
common.BytesToAddress(fromAddress[:]),
common.BytesToAddress(toAddress[:]),
common.BytesToHash(nonce[:]),
common.BytesToHash(selector[:]),
big.NewInt(int64(len(tx.CallData))),
)
if err != nil {
return nil, fmt.Errorf("Failed to pack arguments: %v", err)

Check failure on line 465 in blockchain/blockchain.go

View workflow job for this annotation

GitHub Actions / lint

ST1005: error strings should not be capitalized (stylecheck)
}

// Append the rest of the calldata as bytes
for i := 1; i < len(tx.CallData); i++ {
txCallDataBytes := tx.CallData[i].Bytes()
packedBytes = append(packedBytes, txCallDataBytes[:]...)
}

return new(felt.Felt).SetBytes(crypto.Keccak256(packedBytes)), nil
}

func StoreL1HandlerTxns(txn db.Transaction, block *core.Block) error {
keysL1TxnHashes := []*felt.Felt{}
values := map[*felt.Felt][]*felt.Felt{}
for _, txn := range block.Transactions {
if l1handler, ok := (txn).(*core.L1HandlerTransaction); ok {
l1hash, err := L1Hash(l1handler)
if err != nil {
return err
}
keysL1TxnHashes = append(keysL1TxnHashes, l1hash)
values[l1hash] = append(values[l1hash], l1handler.Hash())
}
}

for _, l1hash := range keysL1TxnHashes {
keyBytes := l1hash.Bytes()
l1HandlerHashesBytes, err := encoder.Marshal(values[l1hash])
if err != nil {
return err
}
err = txn.Set(db.L1Hashes.Key(keyBytes[:]), l1HandlerHashesBytes)
if err != nil {
return err
}
}
return nil
}

func (b *Blockchain) BlockCommitmentsByNumber(blockNumber uint64) (*core.BlockCommitments, error) {
b.listener.OnRead("BlockCommitmentsByNumber")
var commitments *core.BlockCommitments
Expand Down
22 changes: 22 additions & 0 deletions blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,3 +863,25 @@ func TestMakeStateDiffForEmptyBlock(t *testing.T) {
assert.Equal(t, blockHash, sd.StorageDiffs[*storageContractAddr][felt.Zero])
})
}

func TestL1Hash(t *testing.T) {
l1HandlerTxn := &core.L1HandlerTransaction{
TransactionHash: utils.HexToFelt(t, "0x46995a28ce018efb26e67af1481cd0e96340606e4ff3826d5e23f7835b9be9"),
ContractAddress: utils.HexToFelt(t, "0x4c5772d1914fe6ce891b64eb35bf3522aeae1315647314aac58b01137607f3f"),
EntryPointSelector: utils.HexToFelt(t, "0x1b64b1b3b690b43b9b514fb81377518f4039cd3e4f4914d8a6bdf01d679fb19"),
Nonce: utils.HexToFelt(t, "0x239a"),
CallData: []*felt.Felt{
utils.HexToFelt(t, "0x8453fc6cd1bcfe8d4dfc069c400b433054d47bdc"),
utils.HexToFelt(t, "0x455448"),
utils.HexToFelt(t, "0x7877e6ee1a4f79fb6086565ab5da4ced6605310a"),
utils.HexToFelt(t, "0x47f5fc53bc5c71c218c92981b34403bea56b4e5cec877b77232ff71d3586245"),
utils.HexToFelt(t, "0x89ff260"),
utils.HexToFelt(t, "0x0"),
},
Version: new(core.TransactionVersion).SetUint64(0),
}
computedHash, err := blockchain.L1Hash(l1HandlerTxn)
require.NoError(t, err)
expectedHash := utils.HexToFelt(t, "0x00f0ca9df935c08a05603806140d1140a24aa6334d295d98eb81a345bb2475b2")
require.Equal(t, expectedHash.String(), computedHash.String())
}
1 change: 1 addition & 0 deletions db/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
BlockCommitments
Temporary // used temporarily for migrations
SchemaIntermediateState
L1Hashes
)

// Key flattens a prefix and series of byte arrays into a single []byte.
Expand Down

0 comments on commit d1215c9

Please sign in to comment.