diff --git a/rpc/l1.go b/rpc/l1.go index c2adc0018f..c3c7f80f3f 100644 --- a/rpc/l1.go +++ b/rpc/l1.go @@ -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) + 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 + 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 } diff --git a/rpc/l1_test.go b/rpc/l1_test.go index 6a762b7bd2..a7fd9e924c 100644 --- a/rpc/l1_test.go +++ b/rpc/l1_test.go @@ -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, @@ -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, @@ -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, }, } @@ -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)