Skip to content

Commit

Permalink
remove l2 tx hash computation and use data stored in the state return…
Browse files Browse the repository at this point in the history
…ed by the executor (0xPolygonHermez#3139)
  • Loading branch information
tclemos authored Jan 25, 2024
1 parent 8e1e5e9 commit 19d493e
Show file tree
Hide file tree
Showing 16 changed files with 248 additions and 307 deletions.
45 changes: 36 additions & 9 deletions jsonrpc/endpoints_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (e *EthEndpoints) GetBlockByHash(hash types.ArgHash, fullTx bool, includeEx
receipts = append(receipts, *receipt)
}

rpcBlock, err := types.NewBlock(state.Ptr(l2Block.Hash()), l2Block, receipts, fullTx, false, includeExtraInfo)
rpcBlock, err := types.NewBlock(ctx, e.state, state.Ptr(l2Block.Hash()), l2Block, receipts, fullTx, false, includeExtraInfo, dbTx)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, fmt.Sprintf("couldn't build block response for block by hash %v", hash.Hash()), err, true)
}
Expand All @@ -329,7 +329,7 @@ func (e *EthEndpoints) GetBlockByNumber(number types.BlockNumber, fullTx bool, i
UncleHash: ethTypes.EmptyUncleHash,
})
l2Block := state.NewL2BlockWithHeader(l2Header)
rpcBlock, err := types.NewBlock(nil, l2Block, nil, fullTx, false, includeExtraInfo)
rpcBlock, err := types.NewBlock(ctx, e.state, nil, l2Block, nil, fullTx, false, includeExtraInfo, dbTx)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "couldn't build the pending block response", err, true)
}
Expand Down Expand Up @@ -359,7 +359,7 @@ func (e *EthEndpoints) GetBlockByNumber(number types.BlockNumber, fullTx bool, i
receipts = append(receipts, *receipt)
}

rpcBlock, err := types.NewBlock(state.Ptr(l2Block.Hash()), l2Block, receipts, fullTx, false, includeExtraInfo)
rpcBlock, err := types.NewBlock(ctx, e.state, state.Ptr(l2Block.Hash()), l2Block, receipts, fullTx, false, includeExtraInfo, dbTx)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, fmt.Sprintf("couldn't build block response for block by number %v", blockNumber), err, true)
}
Expand Down Expand Up @@ -563,7 +563,16 @@ func (e *EthEndpoints) GetTransactionByBlockHashAndIndex(hash types.ArgHash, ind
return RPCErrorResponse(types.DefaultErrorCode, "failed to get transaction receipt", err, true)
}

res, err := types.NewTransaction(*tx, receipt, false, includeExtraInfo)
var l2Hash *common.Hash
if includeExtraInfo != nil && *includeExtraInfo {
l2h, err := e.state.GetL2TxHashByTxHash(ctx, tx.Hash(), dbTx)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to get l2 transaction hash", err, true)
}
l2Hash = &l2h
}

res, err := types.NewTransaction(*tx, receipt, false, l2Hash)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to build transaction response", err, true)
}
Expand Down Expand Up @@ -596,7 +605,16 @@ func (e *EthEndpoints) GetTransactionByBlockNumberAndIndex(number *types.BlockNu
return RPCErrorResponse(types.DefaultErrorCode, "failed to get transaction receipt", err, true)
}

res, err := types.NewTransaction(*tx, receipt, false, includeExtraInfo)
var l2Hash *common.Hash
if includeExtraInfo != nil && *includeExtraInfo {
l2h, err := e.state.GetL2TxHashByTxHash(ctx, tx.Hash(), dbTx)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to get l2 transaction hash", err, true)
}
l2Hash = &l2h
}

res, err := types.NewTransaction(*tx, receipt, false, l2Hash)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to build transaction response", err, true)
}
Expand All @@ -621,7 +639,16 @@ func (e *EthEndpoints) GetTransactionByHash(hash types.ArgHash, includeExtraInfo
return RPCErrorResponse(types.DefaultErrorCode, "failed to load transaction receipt from state", err, true)
}

res, err := types.NewTransaction(*tx, receipt, false, includeExtraInfo)
var l2Hash *common.Hash
if includeExtraInfo != nil && *includeExtraInfo {
l2h, err := e.state.GetL2TxHashByTxHash(ctx, hash.Hash(), dbTx)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to get l2 transaction hash", err, true)
}
l2Hash = &l2h
}

res, err := types.NewTransaction(*tx, receipt, false, l2Hash)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to build transaction response", err, true)
}
Expand All @@ -641,7 +668,7 @@ func (e *EthEndpoints) GetTransactionByHash(hash types.ArgHash, includeExtraInfo
}
if poolTx.Status == pool.TxStatusPending {
tx = &poolTx.Transaction
res, err := types.NewTransaction(*tx, nil, false, includeExtraInfo)
res, err := types.NewTransaction(*tx, nil, false, nil)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to build transaction response", err, true)
}
Expand Down Expand Up @@ -812,7 +839,7 @@ func (e *EthEndpoints) GetTransactionReceipt(hash types.ArgHash) (interface{}, t
return RPCErrorResponse(types.DefaultErrorCode, "failed to get tx receipt from state", err, true)
}

receipt, err := types.NewReceipt(*tx, r, state.Ptr(false))
receipt, err := types.NewReceipt(*tx, r, nil)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to build the receipt response", err, true)
}
Expand Down Expand Up @@ -1089,7 +1116,7 @@ func (e *EthEndpoints) notifyNewHeads(wg *sync.WaitGroup, event state.NewL2Block
defer wg.Done()
start := time.Now()

b, err := types.NewBlock(state.Ptr(event.Block.Hash()), &event.Block, nil, false, false, state.Ptr(false))
b, err := types.NewBlock(context.Background(), e.state, state.Ptr(event.Block.Hash()), &event.Block, nil, false, false, state.Ptr(false), nil)
if err != nil {
log.Errorf("failed to build block response to subscription: %v", err)
return
Expand Down
15 changes: 13 additions & 2 deletions jsonrpc/endpoints_eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,12 @@ func TestGetL2BlockByNumber(t *testing.T) {
Return(receipt, nil).
Once()
}
for _, signedTx := range signedTransactions {
m.State.
On("GetL2TxHashByTxHash", context.Background(), signedTx.Hash(), m.DbTx).
Return(signedTx.Hash(), nil).
Once()
}
},
},
{
Expand Down Expand Up @@ -1339,6 +1345,12 @@ func TestGetL2BlockByNumber(t *testing.T) {
Return(receipt, nil).
Once()
}
for _, signedTx := range signedTransactions {
m.State.
On("GetL2TxHashByTxHash", context.Background(), signedTx.Hash(), m.DbTx).
Return(signedTx.Hash(), nil).
Once()
}
},
},
{
Expand Down Expand Up @@ -3260,8 +3272,7 @@ func TestGetTransactionReceipt(t *testing.T) {
signedTx, err := auth.Signer(auth.From, tx)
require.NoError(t, err)

l2Hash, err := state.GetL2Hash(*signedTx)
require.NoError(t, err)
l2Hash := common.HexToHash("0x987654321")

log := &ethTypes.Log{Topics: []common.Hash{common.HexToHash("0x1")}, Data: []byte{}}
logs := []*ethTypes.Log{log}
Expand Down
24 changes: 17 additions & 7 deletions jsonrpc/endpoints_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (z *ZKEVMEndpoints) GetBatchByNumber(batchNumber types.BatchNumber, fullTx
}

batch.Transactions = txs
rpcBatch, err := types.NewBatch(batch, virtualBatch, verifiedBatch, blocks, receipts, fullTx, true, ger)
rpcBatch, err := types.NewBatch(ctx, z.state, batch, virtualBatch, verifiedBatch, blocks, receipts, fullTx, true, ger, dbTx)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, fmt.Sprintf("couldn't build the batch %v response", batchNumber), err, true)
}
Expand All @@ -218,7 +218,7 @@ func (z *ZKEVMEndpoints) GetFullBlockByNumber(number types.BlockNumber, fullTx b
UncleHash: ethTypes.EmptyUncleHash,
})
l2Block := state.NewL2BlockWithHeader(l2Header)
rpcBlock, err := types.NewBlock(nil, l2Block, nil, fullTx, false, state.Ptr(true))
rpcBlock, err := types.NewBlock(ctx, z.state, nil, l2Block, nil, fullTx, false, state.Ptr(true), dbTx)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "couldn't build the pending block response", err, true)
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func (z *ZKEVMEndpoints) GetFullBlockByNumber(number types.BlockNumber, fullTx b
receipts = append(receipts, *receipt)
}

rpcBlock, err := types.NewBlock(state.Ptr(l2Block.Hash()), l2Block, receipts, fullTx, true, state.Ptr(true))
rpcBlock, err := types.NewBlock(ctx, z.state, state.Ptr(l2Block.Hash()), l2Block, receipts, fullTx, true, state.Ptr(true), dbTx)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, fmt.Sprintf("couldn't build block response for block by number %v", blockNumber), err, true)
}
Expand Down Expand Up @@ -277,7 +277,7 @@ func (z *ZKEVMEndpoints) GetFullBlockByHash(hash types.ArgHash, fullTx bool) (in
receipts = append(receipts, *receipt)
}

rpcBlock, err := types.NewBlock(state.Ptr(l2Block.Hash()), l2Block, receipts, fullTx, true, state.Ptr(true))
rpcBlock, err := types.NewBlock(ctx, z.state, state.Ptr(l2Block.Hash()), l2Block, receipts, fullTx, true, state.Ptr(true), dbTx)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, fmt.Sprintf("couldn't build block response for block by hash %v", hash.Hash()), err, true)
}
Expand Down Expand Up @@ -324,7 +324,12 @@ func (z *ZKEVMEndpoints) GetTransactionByL2Hash(hash types.ArgHash) (interface{}
return RPCErrorResponse(types.DefaultErrorCode, "failed to load transaction receipt from state", err, true)
}

res, err := types.NewTransaction(*tx, receipt, false, state.Ptr(true))
l2Hash, err := z.state.GetL2TxHashByTxHash(ctx, tx.Hash(), dbTx)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to get l2 transaction hash", err, true)
}

res, err := types.NewTransaction(*tx, receipt, false, &l2Hash)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to build transaction response", err, true)
}
Expand All @@ -344,7 +349,7 @@ func (z *ZKEVMEndpoints) GetTransactionByL2Hash(hash types.ArgHash) (interface{}
}
if poolTx.Status == pool.TxStatusPending {
tx = &poolTx.Transaction
res, err := types.NewTransaction(*tx, nil, false, state.Ptr(true))
res, err := types.NewTransaction(*tx, nil, false, nil)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to build transaction response", err, true)
}
Expand All @@ -371,7 +376,12 @@ func (z *ZKEVMEndpoints) GetTransactionReceiptByL2Hash(hash types.ArgHash) (inte
return RPCErrorResponse(types.DefaultErrorCode, "failed to get tx receipt from state", err, true)
}

receipt, err := types.NewReceipt(*tx, r, state.Ptr(true))
l2Hash, err := z.state.GetL2TxHashByTxHash(ctx, tx.Hash(), dbTx)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to get l2 transaction hash", err, true)
}

receipt, err := types.NewReceipt(*tx, r, &l2Hash)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to build the receipt response", err, true)
}
Expand Down
36 changes: 31 additions & 5 deletions jsonrpc/endpoints_zkevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,10 @@ func TestGetBatchByNumber(t *testing.T) {
On("GetTransactionReceipt", context.Background(), tx.Hash(), m.DbTx).
Return(receipts[i], nil).
Once()
m.State.
On("GetL2TxHashByTxHash", context.Background(), tx.Hash(), m.DbTx).
Return(tx.Hash(), nil).
Once()
}
m.State.
On("GetTransactionsByBatchNumber", context.Background(), hex.DecodeBig(tc.Number).Uint64(), m.DbTx).
Expand Down Expand Up @@ -966,8 +970,9 @@ func TestGetBatchByNumber(t *testing.T) {
receipts = append(receipts, receipt)
from, _ := state.GetSender(*tx)
V, R, S := tx.RawSignatureValues()
l2Hash := common.HexToHash("0x987654321")

rpcReceipt, err := types.NewReceipt(*tx, receipt, state.Ptr(true))
rpcReceipt, err := types.NewReceipt(*tx, receipt, &l2Hash)
require.NoError(t, err)

tc.ExpectedResult.Transactions = append(tc.ExpectedResult.Transactions,
Expand All @@ -990,6 +995,7 @@ func TestGetBatchByNumber(t *testing.T) {
R: types.ArgBig(*R),
S: types.ArgBig(*S),
Receipt: &rpcReceipt,
L2Hash: &l2Hash,
},
},
)
Expand Down Expand Up @@ -1054,7 +1060,13 @@ func TestGetBatchByNumber(t *testing.T) {
On("GetTransactionReceipt", context.Background(), tx.Hash(), m.DbTx).
Return(receipts[i], nil).
Once()

m.State.
On("GetL2TxHashByTxHash", context.Background(), tx.Hash(), m.DbTx).
Return(tx.Hash(), nil).
Once()
}

m.State.
On("GetTransactionsByBatchNumber", context.Background(), uint64(tc.ExpectedResult.Number), m.DbTx).
Return(batchTxs, effectivePercentages, nil).
Expand Down Expand Up @@ -1911,8 +1923,7 @@ func TestGetTransactionByL2Hash(t *testing.T) {

txV, txR, txS := signedTx.RawSignatureValues()

l2Hash, err := state.GetL2Hash(*signedTx)
require.NoError(t, err)
l2Hash := common.HexToHash("0x987654321")

rpcTransaction := types.Transaction{
Nonce: types.ArgUint64(signedTx.Nonce()),
Expand Down Expand Up @@ -1962,6 +1973,11 @@ func TestGetTransactionByL2Hash(t *testing.T) {
On("GetTransactionReceipt", context.Background(), tc.Hash, m.DbTx).
Return(receipt, nil).
Once()

m.State.
On("GetL2TxHashByTxHash", context.Background(), signedTx.Hash(), m.DbTx).
Return(l2Hash, nil).
Once()
},
},
{
Expand All @@ -1974,6 +1990,7 @@ func TestGetTransactionByL2Hash(t *testing.T) {
tc.ExpectedResult.BlockHash = nil
tc.ExpectedResult.BlockNumber = nil
tc.ExpectedResult.TxIndex = nil
tc.ExpectedResult.L2Hash = nil

m.DbTx.
On("Commit", context.Background()).
Expand Down Expand Up @@ -2201,8 +2218,7 @@ func TestGetTransactionReceiptByL2Hash(t *testing.T) {
signedTx, err := auth.Signer(auth.From, tx)
require.NoError(t, err)

l2Hash, err := state.GetL2Hash(*signedTx)
require.NoError(t, err)
l2Hash := common.HexToHash("0x987654321")

log := &ethTypes.Log{Topics: []common.Hash{common.HexToHash("0x1")}, Data: []byte{}}
logs := []*ethTypes.Log{log}
Expand Down Expand Up @@ -2273,6 +2289,11 @@ func TestGetTransactionReceiptByL2Hash(t *testing.T) {
On("GetTransactionReceipt", context.Background(), tc.Hash, m.DbTx).
Return(receipt, nil).
Once()

m.State.
On("GetL2TxHashByTxHash", context.Background(), signedTx.Hash(), m.DbTx).
Return(l2Hash, nil).
Once()
},
},
{
Expand Down Expand Up @@ -2398,6 +2419,11 @@ func TestGetTransactionReceiptByL2Hash(t *testing.T) {
On("GetTransactionReceipt", context.Background(), tc.Hash, m.DbTx).
Return(ethTypes.NewReceipt([]byte{}, false, 0), nil).
Once()

m.State.
On("GetL2TxHashByTxHash", context.Background(), tx.Hash(), m.DbTx).
Return(l2Hash, nil).
Once()
},
},
}
Expand Down
30 changes: 30 additions & 0 deletions jsonrpc/mocks/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions jsonrpc/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type StateInterface interface {
GetLastVerifiedBatchNumberUntilL1Block(ctx context.Context, l1BlockNumber uint64, dbTx pgx.Tx) (uint64, error)
GetBatchTimestamp(ctx context.Context, batchNumber uint64, forcedForkId *uint64, dbTx pgx.Tx) (*time.Time, error)
GetLatestBatchGlobalExitRoot(ctx context.Context, dbTx pgx.Tx) (common.Hash, error)
GetL2TxHashByTxHash(ctx context.Context, hash common.Hash, dbTx pgx.Tx) (common.Hash, error)
}

// EthermanInterface provides integration with L1
Expand Down
Loading

0 comments on commit 19d493e

Please sign in to comment.