Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 21491a8

Browse files
tclemosagnusmorToniRamirezM
authored
Elderberry blockhash adjustments (#3424)
* remove intermediate state root from receipts after Etrog * use receipt state returned by executor instead of checking the RomError * use receipt status from executor only after Etrog * fix gasLimit and cumulativeGasUsed for Elderberry txs (#3428) * keep im state root in db (#3427) * generate receipt refactor (#3436) * generate receipt refactor * update prover image * downgrade prover image --------- Co-authored-by: agnusmor <[email protected]> Co-authored-by: Toni Ramírez <[email protected]>
1 parent 2261ccc commit 21491a8

File tree

22 files changed

+156
-89
lines changed

22 files changed

+156
-89
lines changed

db/migrations/state/0017.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- +migrate Up
2+
ALTER TABLE state.receipt
3+
ADD COLUMN IF NOT EXISTS im_state_root BYTEA;
4+
5+
UPDATE state.receipt SET im_state_root = post_state WHERE block_num >= (SELECT MIN(block_num) FROM state.l2block WHERE batch_num >= (SELECT from_batch_num FROM state.fork_id WHERE fork_id = 7));
6+
7+
-- +migrate Down
8+
ALTER TABLE state.receipt
9+
DROP COLUMN IF EXISTS im_state_root;

jsonrpc/endpoints_eth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3311,7 +3311,7 @@ func TestGetTransactionReceipt(t *testing.T) {
33113311
receipt.Bloom = ethTypes.CreateBloom(ethTypes.Receipts{receipt})
33123312

33133313
rpcReceipt := types.Receipt{
3314-
Root: stateRoot,
3314+
Root: &stateRoot,
33153315
CumulativeGasUsed: types.ArgUint64(receipt.CumulativeGasUsed),
33163316
LogsBloom: receipt.Bloom,
33173317
Logs: receipt.Logs,

jsonrpc/endpoints_zkevm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2260,7 +2260,7 @@ func TestGetTransactionReceiptByL2Hash(t *testing.T) {
22602260
receipt.Bloom = ethTypes.CreateBloom(ethTypes.Receipts{receipt})
22612261

22622262
rpcReceipt := types.Receipt{
2263-
Root: stateRoot,
2263+
Root: &stateRoot,
22642264
CumulativeGasUsed: types.ArgUint64(receipt.CumulativeGasUsed),
22652265
LogsBloom: receipt.Bloom,
22662266
Logs: receipt.Logs,

jsonrpc/types/types.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ func NewTransaction(
601601

602602
// Receipt structure
603603
type Receipt struct {
604-
Root common.Hash `json:"root"`
604+
Root *common.Hash `json:"root,omitempty"`
605605
CumulativeGasUsed ArgUint64 `json:"cumulativeGasUsed"`
606606
LogsBloom types.Bloom `json:"logsBloom"`
607607
Logs []*types.Log `json:"logs"`
@@ -643,7 +643,6 @@ func NewReceipt(tx types.Transaction, r *types.Receipt, l2Hash *common.Hash) (Re
643643
return Receipt{}, err
644644
}
645645
receipt := Receipt{
646-
Root: common.BytesToHash(r.PostState),
647646
CumulativeGasUsed: ArgUint64(r.CumulativeGasUsed),
648647
LogsBloom: r.Bloom,
649648
Logs: logs,
@@ -659,6 +658,11 @@ func NewReceipt(tx types.Transaction, r *types.Receipt, l2Hash *common.Hash) (Re
659658
Type: ArgUint64(r.Type),
660659
TxL2Hash: l2Hash,
661660
}
661+
if common.BytesToHash(r.PostState).String() != state.ZeroHash.String() {
662+
root := common.BytesToHash(r.PostState)
663+
receipt.Root = &root
664+
}
665+
662666
if r.EffectiveGasPrice != nil {
663667
egp := ArgBig(*r.EffectiveGasPrice)
664668
receipt.EffectiveGasPrice = &egp

proto/src/proto/executor/v1/executor.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,4 +872,6 @@ enum ExecutorError {
872872
EXECUTOR_ERROR_INVALID_DATA_STREAM = 115;
873873
// EXECUTOR_ERROR_INVALID_UPDATE_MERKLE_TREE indicates that the provided update merkle tree is invalid, e.g. because the executor is configured not to write to database
874874
EXECUTOR_ERROR_INVALID_UPDATE_MERKLE_TREE = 116;
875+
// EXECUTOR_ERROR_SM_MAIN_INVALID_TX_STATUS_ERROR indicates that a TX has an invalid status-error combination
876+
EXECUTOR_ERROR_SM_MAIN_INVALID_TX_STATUS_ERROR = 117;
875877
}

state/convertersV2.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func (s *State) convertToProcessTransactionResponseV2(responses []*executor.Proc
127127
result.ReturnValue = response.ReturnValue
128128
result.GasLeft = response.GasLeft
129129
result.GasUsed = response.GasUsed
130+
result.CumulativeGasUsed = response.CumulativeGasUsed
130131
result.GasRefunded = response.GasRefunded
131132
result.RomError = executor.RomErr(response.Error)
132133
result.CreateAddress = common.HexToAddress(response.CreateAddress)

state/datastream.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ func (b DSL2BlockStart) Decode(data []byte) DSL2BlockStart {
118118
// DSL2Transaction represents a data stream L2 transaction
119119
type DSL2Transaction struct {
120120
L2BlockNumber uint64 // Not included in the encoded data
121+
ImStateRoot common.Hash // Not included in the encoded data
121122
EffectiveGasPricePercentage uint8 // 1 byte
122123
IsValid uint8 // 1 byte
123124
StateRoot common.Hash // 32 bytes
@@ -553,6 +554,9 @@ func GenerateDataStreamerFile(ctx context.Context, streamServer *datastreamer.St
553554
}
554555

555556
for _, tx := range l2Block.Txs {
557+
// < ETROG => IM State root is retrieved from the system SC (using cache is available)
558+
// = ETROG => IM State root is retrieved from the receipt.post_state => Do nothing
559+
// > ETROG => IM State root is retrieved from the receipt.im_state_root
556560
if l2Block.ForkID < FORKID_ETROG {
557561
// Populate intermediate state root with information from the system SC (or cache if available)
558562
if imStateRoots == nil || (*imStateRoots)[blockStart.L2BlockNumber] == nil {
@@ -565,6 +569,8 @@ func GenerateDataStreamerFile(ctx context.Context, streamServer *datastreamer.St
565569
} else {
566570
tx.StateRoot = common.BytesToHash((*imStateRoots)[blockStart.L2BlockNumber])
567571
}
572+
} else if l2Block.ForkID > FORKID_ETROG {
573+
tx.StateRoot = tx.ImStateRoot
568574
}
569575

570576
_, err = streamServer.AddStreamEntry(EntryTypeL2Tx, tx.Encode())

state/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func (s *State) SetGenesis(ctx context.Context, block Block, genesis Genesis, m
204204
storeTxsEGPData := []StoreTxEGPData{}
205205
txsL2Hash := []common.Hash{}
206206

207-
err = s.AddL2Block(ctx, batch.BatchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, dbTx)
207+
err = s.AddL2Block(ctx, batch.BatchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, []common.Hash{}, dbTx)
208208
if err != nil {
209209
return common.Hash{}, err
210210
}

state/helper.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -279,19 +279,23 @@ func DecodeTx(encodedTx string) (*types.Transaction, error) {
279279
}
280280

281281
// GenerateReceipt generates a receipt from a processed transaction
282-
func GenerateReceipt(blockNumber *big.Int, processedTx *ProcessTransactionResponse, txIndex uint) *types.Receipt {
282+
func GenerateReceipt(blockNumber *big.Int, processedTx *ProcessTransactionResponse, txIndex uint, forkID uint64) *types.Receipt {
283283
receipt := &types.Receipt{
284-
Type: uint8(processedTx.Type),
285-
PostState: processedTx.StateRoot.Bytes(),
286-
CumulativeGasUsed: processedTx.GasUsed,
287-
BlockNumber: blockNumber,
288-
GasUsed: processedTx.GasUsed,
289-
TxHash: processedTx.Tx.Hash(),
290-
TransactionIndex: txIndex,
291-
ContractAddress: processedTx.CreateAddress,
292-
Logs: processedTx.Logs,
284+
Type: uint8(processedTx.Type),
285+
BlockNumber: blockNumber,
286+
GasUsed: processedTx.GasUsed,
287+
TxHash: processedTx.Tx.Hash(),
288+
TransactionIndex: txIndex,
289+
ContractAddress: processedTx.CreateAddress,
290+
Logs: processedTx.Logs,
291+
}
292+
if forkID <= FORKID_ETROG {
293+
receipt.PostState = processedTx.StateRoot.Bytes()
294+
receipt.CumulativeGasUsed = processedTx.GasUsed
295+
} else {
296+
receipt.PostState = ZeroHash.Bytes()
297+
receipt.CumulativeGasUsed = processedTx.CumulativeGasUsed
293298
}
294-
295299
if processedTx.EffectiveGasPrice != "" {
296300
effectiveGasPrice, ok := big.NewInt(0).SetString(processedTx.EffectiveGasPrice, 0)
297301
if !ok {
@@ -309,10 +313,14 @@ func GenerateReceipt(blockNumber *big.Int, processedTx *ProcessTransactionRespon
309313
for i := 0; i < len(receipt.Logs); i++ {
310314
receipt.Logs[i].TxHash = processedTx.Tx.Hash()
311315
}
312-
if processedTx.RomError == nil {
313-
receipt.Status = types.ReceiptStatusSuccessful
316+
if forkID <= FORKID_ETROG {
317+
if processedTx.RomError == nil {
318+
receipt.Status = types.ReceiptStatusSuccessful
319+
} else {
320+
receipt.Status = types.ReceiptStatusFailed
321+
}
314322
} else {
315-
receipt.Status = types.ReceiptStatusFailed
323+
receipt.Status = uint64(processedTx.Status)
316324
}
317325

318326
return receipt

state/interfaces.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type storage interface {
7272
GetL2BlockTransactionCountByHash(ctx context.Context, blockHash common.Hash, dbTx pgx.Tx) (uint64, error)
7373
GetL2BlockTransactionCountByNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (uint64, error)
7474
GetTransactionEGPLogByHash(ctx context.Context, transactionHash common.Hash, dbTx pgx.Tx) (*EffectiveGasPriceLog, error)
75-
AddL2Block(ctx context.Context, batchNumber uint64, l2Block *L2Block, receipts []*types.Receipt, txsL2Hash []common.Hash, txsEGPData []StoreTxEGPData, dbTx pgx.Tx) error
75+
AddL2Block(ctx context.Context, batchNumber uint64, l2Block *L2Block, receipts []*types.Receipt, txsL2Hash []common.Hash, txsEGPData []StoreTxEGPData, imStateRoots []common.Hash, dbTx pgx.Tx) error
7676
GetLastVirtualizedL2BlockNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
7777
GetLastConsolidatedL2BlockNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
7878
GetLastVerifiedL2BlockNumberUntilL1Block(ctx context.Context, l1FinalizedBlockNumber uint64, dbTx pgx.Tx) (uint64, error)
@@ -93,7 +93,7 @@ type storage interface {
9393
IsL2BlockConsolidated(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (bool, error)
9494
IsL2BlockVirtualized(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (bool, error)
9595
GetLogs(ctx context.Context, fromBlock uint64, toBlock uint64, addresses []common.Address, topics [][]common.Hash, blockHash *common.Hash, since *time.Time, dbTx pgx.Tx) ([]*types.Log, error)
96-
AddReceipt(ctx context.Context, receipt *types.Receipt, dbTx pgx.Tx) error
96+
AddReceipt(ctx context.Context, receipt *types.Receipt, imStateRoot common.Hash, dbTx pgx.Tx) error
9797
AddLog(ctx context.Context, l *types.Log, dbTx pgx.Tx) error
9898
GetExitRootByGlobalExitRoot(ctx context.Context, ger common.Hash, dbTx pgx.Tx) (*GlobalExitRoot, error)
9999
AddSequence(ctx context.Context, sequence Sequence, dbTx pgx.Tx) error

0 commit comments

Comments
 (0)