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

refactor: extract zkevm api fulltx function #1246

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Changes from all commits
Commits
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
122 changes: 64 additions & 58 deletions cmd/rpcdaemon/commands/zkevm_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,65 +489,10 @@ func (api *ZkEvmAPIImpl) GetBatchByNumber(ctx context.Context, batchNumber rpc.B
}

if fullTx != nil && *fullTx {
batchBlocksJson := make([]interface{}, 0, len(batchBlocks))
batchTransactionsJson := make([]interface{}, 0, len(batchTxs))
for _, blk := range batchBlocks {
bbj, err := api.populateBlockDetail(tx, ctx, blk, true)
if err != nil {
return nil, err
}

bir, err := hermezDb.GetBlockInfoRoot(blk.NumberU64())
if err != nil {
return nil, err
}

ger, err := hermezDb.GetBlockGlobalExitRoot(blk.NumberU64())
if err != nil {
return nil, err
}

batchBlockExtra := &types.BlockWithInfoRootAndGer{
Block: &bbj,
BlockInfoRoot: bir,
GlobalExitRoot: ger,
}

// txs
hashes := make([]types.TransactionOrHash, len(bbj.Transactions))
for i, txn := range bbj.Transactions {
blkTx := blk.Transactions()[i]
l2TxHash, err := zktx.ComputeL2TxHash(
blkTx.GetChainID().ToBig(),
blkTx.GetValue(),
blkTx.GetPrice(),
blkTx.GetNonce(),
blkTx.GetGas(),
blkTx.GetTo(),
&txn.Tx.From,
blkTx.GetData(),
)
if err != nil {
return nil, err
}

txn.Tx.L2Hash = l2TxHash
txn.Tx.Receipt.TransactionL2Hash = l2TxHash

batchTransactionsJson = append(batchTransactionsJson, txn)
txn.Hash = &txn.Tx.Hash
txn.Tx = nil
hashes[i] = txn
}

// after collecting transactions, reduce them to them hash only on the block
bbj.Transactions = hashes

batchBlocksJson = append(batchBlocksJson, batchBlockExtra)
batch.Blocks, batch.Transactions, err = api.fullTxBlockData(ctx, tx, hermezDb, batchBlocks, batchTxs)
if err != nil {
return nil, err
}

batch.Blocks = batchBlocksJson
batch.Transactions = batchTransactionsJson
}

// for consistency with legacy node, return nil if no transactions
Expand Down Expand Up @@ -663,6 +608,67 @@ func (api *ZkEvmAPIImpl) GetBatchByNumber(ctx context.Context, batchNumber rpc.B
return populateBatchDetails(batch)
}

func (api *ZkEvmAPIImpl) fullTxBlockData(ctx context.Context, tx kv.Tx, hermezDb *hermez_db.HermezDbReader, batchBlocks []*eritypes.Block, batchTxs []eritypes.Transaction) ([]interface{}, []interface{}, error) {
batchBlocksJson := make([]interface{}, 0, len(batchBlocks))
batchTransactionsJson := make([]interface{}, 0, len(batchTxs))
for _, blk := range batchBlocks {
bbj, err := api.populateBlockDetail(tx, ctx, blk, true)
if err != nil {
return nil, nil, err
}

bir, err := hermezDb.GetBlockInfoRoot(blk.NumberU64())
if err != nil {
return nil, nil, err
}

ger, err := hermezDb.GetBlockGlobalExitRoot(blk.NumberU64())
if err != nil {
return nil, nil, err
}

batchBlockExtra := &types.BlockWithInfoRootAndGer{
Block: &bbj,
BlockInfoRoot: bir,
GlobalExitRoot: ger,
}

// txs
hashes := make([]types.TransactionOrHash, len(bbj.Transactions))
for i, txn := range bbj.Transactions {
blkTx := blk.Transactions()[i]
l2TxHash, err := zktx.ComputeL2TxHash(
blkTx.GetChainID().ToBig(),
blkTx.GetValue(),
blkTx.GetPrice(),
blkTx.GetNonce(),
blkTx.GetGas(),
blkTx.GetTo(),
&txn.Tx.From,
blkTx.GetData(),
)
if err != nil {
return nil, nil, err
}

txn.Tx.L2Hash = l2TxHash
txn.Tx.Receipt.TransactionL2Hash = l2TxHash

batchTransactionsJson = append(batchTransactionsJson, txn)
txn.Hash = &txn.Tx.Hash
txn.Tx = nil
hashes[i] = txn
}

// after collecting transactions, reduce them to them hash only on the block
bbj.Transactions = hashes

batchBlocksJson = append(batchBlocksJson, batchBlockExtra)
}

return batchBlocksJson, batchTransactionsJson, nil
}

type SequenceReader interface {
GetRangeSequencesByBatch(batchNo uint64) (*zktypes.L1BatchInfo, *zktypes.L1BatchInfo, error)
GetForkId(batchNo uint64) (uint64, error)
Expand Down
Loading