Skip to content

Commit

Permalink
fix hash function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Nov 4, 2024
1 parent 84bcfd0 commit ed94fd3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
26 changes: 13 additions & 13 deletions rpc/l1.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ type LogMessageToL2 struct {
Fee *big.Int
}

// HashMessage calculates the message hash following the Keccak256 hash method
func (l *LogMessageToL2) HashMessage() *common.Hash {
hash := sha3.NewLegacyKeccak256()

// Padding for Ethereum address to 32 bytes
hash.Write(make([]byte, 12)) //nolint:mnd
hash.Write(l.FromAddress.Bytes())
hash.Write(l.ToAddress.Bytes())
hash.Write(l.Nonce.Bytes())
hash.Write(l.Selector.Bytes())
writeUint256 := func(value *big.Int) {
bytes := make([]byte, 32)

Check failure on line 32 in rpc/l1.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 32, in <argument> detected (mnd)
value.FillBytes(bytes)
hash.Write(bytes)
}

// Padding for payload length (u64)
hash.Write(make([]byte, 24)) //nolint:mnd
payloadLength := make([]byte, 8) //nolint:mnd
big.NewInt(int64(len(l.Payload))).FillBytes(payloadLength)
hash.Write(payloadLength)
hash.Write(make([]byte, 12)) // Pad FromAddress to 32 bytes

Check failure on line 37 in rpc/l1.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 12, in <argument> detected (mnd)
hash.Write(l.FromAddress.Bytes())
writeUint256(l.ToAddress)
writeUint256(l.Nonce)
writeUint256(l.Selector)
writeUint256(big.NewInt(int64(len(l.Payload))))

for _, elem := range l.Payload {
hash.Write(elem.Bytes())
writeUint256(elem)
}

tmp := common.BytesToHash(hash.Sum(nil))
return &tmp
}
Expand Down
29 changes: 16 additions & 13 deletions rpc/l1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ func TestGetMessageStatus(t *testing.T) {
require.NoError(t, json.Unmarshal([]byte(l1ReceiptSepolia), &l1TxnReceiptSepolia))

tests := map[string]struct {
network utils.Network
l1TxnHash common.Hash
msgs []rpc.MsgStatus
msgHashes []common.Hash
l1TxnReceipt types.Receipt
blockNum uint
network utils.Network
l1TxnHash common.Hash
msgs []rpc.MsgStatus
msgHashes []common.Hash
l1TxnReceipt types.Receipt
blockNum uint
l1HeadBlockNum uint
}{
"mainnet 0.13.2.1": {
network: utils.Mainnet,
Expand All @@ -49,9 +50,10 @@ func TestGetMessageStatus(t *testing.T) {
FinalityStatus: rpc.TxnStatusAcceptedOnL1,
FailureReason: "",
}},
msgHashes: []common.Hash{common.HexToHash("0xd8824a75a588f0726d7d83b3e9560810c763043e979fdb77b11c1a51a991235d")},
l1TxnReceipt: l1TxnReceipt,
blockNum: 763497,
msgHashes: []common.Hash{common.HexToHash("0xd8824a75a588f0726d7d83b3e9560810c763043e979fdb77b11c1a51a991235d")},
l1TxnReceipt: l1TxnReceipt,
blockNum: 763497,
l1HeadBlockNum: 763498,
},
"sepolia 0.13.2.1": {
network: utils.Sepolia,
Expand All @@ -61,9 +63,10 @@ func TestGetMessageStatus(t *testing.T) {
FinalityStatus: rpc.TxnStatusAcceptedOnL2,
FailureReason: "",
}},
msgHashes: []common.Hash{common.HexToHash("0x4bce24fddbc380266493f5e2b1f5625e606fb4286dd08a4f3a625032b3dd474b")}, // todo check against starkli
l1TxnReceipt: l1TxnReceiptSepolia,
blockNum: 284801,
msgHashes: []common.Hash{common.HexToHash("0x4bce24fddbc380266493f5e2b1f5625e606fb4286dd08a4f3a625032b3dd474b")}, // todo check against starkli
l1TxnReceipt: l1TxnReceiptSepolia,
blockNum: 284801,
l1HeadBlockNum: 284800,
},
}

Expand All @@ -87,7 +90,7 @@ func TestGetMessageStatus(t *testing.T) {
// Expects for h.TransactionStatus()
mockReader.EXPECT().TransactionByHash(msg.L1HandlerHash).Return(l1handlerTxns[i], nil)
mockReader.EXPECT().Receipt(msg.L1HandlerHash).Return(block.Receipts[0], block.Hash, block.Number, nil)
mockReader.EXPECT().L1Head().Return(&core.L1Head{BlockNumber: uint64(test.blockNum) + 1}, nil)
mockReader.EXPECT().L1Head().Return(&core.L1Head{BlockNumber: uint64(test.l1HeadBlockNum)}, nil)
}
msgStatuses, rpcErr := handler.GetMessageStatus(context.Background(), &test.l1TxnHash)
require.Nil(t, rpcErr)
Expand Down

0 comments on commit ed94fd3

Please sign in to comment.