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

Use forkID while encoding/decoding txs #2219

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion ci/e2e-group10/forced_batches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
const (
toAddressHex = "0x4d5Cf5032B2a844602278b01199ED191A86c93ff"
gerFinalityBlocks = uint64(250)
forkID5 = 5
)

var (
Expand Down Expand Up @@ -60,7 +61,7 @@ func TestForcedBatches(t *testing.T) {
unsignedTx := types.NewTransaction(nonce, toAddress, amount, gasLimit, gasPrice, nil)
signedTx, err := auth.Signer(auth.From, unsignedTx)
require.NoError(t, err)
encodedTxs, err := state.EncodeTransactions([]types.Transaction{*signedTx}, constants.EffectivePercentage)
encodedTxs, err := state.EncodeTransactions([]types.Transaction{*signedTx}, constants.EffectivePercentage, forkID5)
require.NoError(t, err)
forcedBatch, err := sendForcedBatch(t, encodedTxs, opsman)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion ci/e2e-group10/forced_batches_vector_group2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestForcedBatchesVectorFiles(t *testing.T) {
require.Equal(t, testCase.ExpectedOldStateRoot, actualOldStateRoot.Hex())
decodedData, err := hex.DecodeHex(testCase.BatchL2Data)
require.NoError(t, err)
_, txBytes, _, err := state.DecodeTxs(decodedData)
_, txBytes, _, err := state.DecodeTxs(decodedData, forkID5)
forcedBatch, err := sendForcedBatchForVector(t, txBytes, opsman)
require.NoError(t, err)
actualNewStateRoot := forcedBatch.StateRoot
Expand Down
6 changes: 5 additions & 1 deletion ci/e2e-group11/forced_batches_vector_group3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (
"github.com/stretchr/testify/require"
)

const (
forkID5 = 5
)

func TestForcedBatchesVectorFiles(t *testing.T) {

if testing.Short() {
Expand Down Expand Up @@ -71,7 +75,7 @@ func TestForcedBatchesVectorFiles(t *testing.T) {
require.Equal(t, testCase.ExpectedOldStateRoot, actualOldStateRoot.Hex())
decodedData, err := hex.DecodeHex(testCase.BatchL2Data)
require.NoError(t, err)
_, txBytes, _, err := state.DecodeTxs(decodedData)
_, txBytes, _, err := state.DecodeTxs(decodedData, forkID5)
forcedBatch, err := sendForcedBatchForVector(t, txBytes, opsman)
require.NoError(t, err)
actualNewStateRoot := forcedBatch.StateRoot
Expand Down
6 changes: 5 additions & 1 deletion ci/e2e-group9/forced_batches_vector_group1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (
"github.com/stretchr/testify/require"
)

const (
forkID5 = 5
)

func TestForcedBatchesVectorFiles(t *testing.T) {

if testing.Short() {
Expand Down Expand Up @@ -71,7 +75,7 @@ func TestForcedBatchesVectorFiles(t *testing.T) {
require.Equal(t, testCase.ExpectedOldStateRoot, actualOldStateRoot.Hex())
decodedData, err := hex.DecodeHex(testCase.BatchL2Data)
require.NoError(t, err)
_, txBytes, _, err := state.DecodeTxs(decodedData)
_, txBytes, _, err := state.DecodeTxs(decodedData, forkID5)
require.NoError(t, err)
forcedBatch, err := sendForcedBatchForVector(t, txBytes, opsman)
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ func start(cliCtx *cli.Context) error {
log.Infof("Fork ID read from POE SC = %v", currentForkID)
c.Aggregator.ChainID = l2ChainID
c.Aggregator.ForkId = currentForkID
c.Sequencer.DBManager.ForkID = currentForkID
c.Sequencer.Finalizer.ForkID = currentForkID
log.Infof("Chain ID read from POE SC = %v", l2ChainID)

ctx := context.Background()
Expand Down
6 changes: 5 additions & 1 deletion etherman/etherman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (
"github.com/stretchr/testify/require"
)

const (
forkID5 = 5
)

func init() {
log.Init(log.Config{
Level: "debug",
Expand Down Expand Up @@ -315,7 +319,7 @@ func TestSendSequences(t *testing.T) {
require.NoError(t, err)

tx1 := types.NewTransaction(uint64(0), common.Address{}, big.NewInt(10), uint64(1), big.NewInt(10), []byte{})
batchL2Data, err := state.EncodeTransactions([]types.Transaction{*tx1}, constants.EffectivePercentage)
batchL2Data, err := state.EncodeTransactions([]types.Transaction{*tx1}, constants.EffectivePercentage, forkID5)
require.NoError(t, err)
sequence := ethmanTypes.Sequence{
GlobalExitRoot: ger,
Expand Down
10 changes: 7 additions & 3 deletions jsonrpc/endpoints_zkevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"github.com/stretchr/testify/require"
)

const (
forkID5 = 5
)

func TestConsolidatedBlockNumber(t *testing.T) {
s, m, _ := newSequencerMockedServer(t)
defer s.Stop()
Expand Down Expand Up @@ -708,7 +712,7 @@ func TestGetBatchByNumber(t *testing.T) {
batchTxs = append(batchTxs, *tx)
effectivePercentages = append(effectivePercentages, state.MaxEffectivePercentage)
}
batchL2Data, err := state.EncodeTransactions(batchTxs, effectivePercentages)
batchL2Data, err := state.EncodeTransactions(batchTxs, effectivePercentages, forkID5)
require.NoError(t, err)
tc.ExpectedResult.BatchL2Data = batchL2Data
batch := &state.Batch{
Expand Down Expand Up @@ -821,7 +825,7 @@ func TestGetBatchByNumber(t *testing.T) {
batchTxs = append(batchTxs, *tx)
effectivePercentages = append(effectivePercentages, state.MaxEffectivePercentage)
}
batchL2Data, err := state.EncodeTransactions(batchTxs, effectivePercentages)
batchL2Data, err := state.EncodeTransactions(batchTxs, effectivePercentages, forkID5)
require.NoError(t, err)

batch := &state.Batch{
Expand Down Expand Up @@ -959,7 +963,7 @@ func TestGetBatchByNumber(t *testing.T) {
batchTxs = append(batchTxs, *tx)
effectivePercentages = append(effectivePercentages, state.MaxEffectivePercentage)
}
batchL2Data, err := state.EncodeTransactions(batchTxs, effectivePercentages)
batchL2Data, err := state.EncodeTransactions(batchTxs, effectivePercentages, forkID5)
require.NoError(t, err)
batch := &state.Batch{
BatchNumber: 1,
Expand Down
3 changes: 2 additions & 1 deletion pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
)

const (
forkID5 = 5
senderPrivateKey = "0x28b2b0318721be8c8339199172cd7cc8f5e273800a35616ec893083a4b32c02e"
senderAddress = "0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D"
)
Expand Down Expand Up @@ -271,7 +272,7 @@ func Test_AddPreEIP155Tx(t *testing.T) {
batchL2Data := "0xe580843b9aca00830186a0941275fbb540c8efc58b812ba83b0d0b8b9917ae98808464fbb77c6b39bdc5f8e458aba689f2a1ff8c543a94e4817bda40f3fe34080c4ab26c1e3c2fc2cda93bc32f0a79940501fd505dcf48d94abfde932ebf1417f502cb0d9de81bff"
b, err := hex.DecodeHex(batchL2Data)
require.NoError(t, err)
txs, _, _, err := state.DecodeTxs(b)
txs, _, _, err := state.DecodeTxs(b, forkID5)
require.NoError(t, err)

tx := txs[0]
Expand Down
4 changes: 4 additions & 0 deletions sequencer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ type FinalizerCfg struct {

// TimestampResolution is the resolution of the timestamp used to close a batch
TimestampResolution types.Duration `mapstructure:"TimestampResolution"`

// ForkID is the fork id of the chain
ForkID uint64 `mapstructure:"ForkID"`
}

// WorkerCfg contains the Worker's configuration properties
Expand All @@ -136,6 +139,7 @@ type WorkerCfg struct {
type DBManagerCfg struct {
PoolRetrievalInterval types.Duration `mapstructure:"PoolRetrievalInterval"`
L2ReorgRetrievalInterval types.Duration `mapstructure:"L2ReorgRetrievalInterval"`
ForkID uint64 `mapstructure:"ForkID"`
}

// EffectiveGasPriceCfg contains the configuration properties for the effective gas price
Expand Down
8 changes: 4 additions & 4 deletions sequencer/dbmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (d *dbManager) StoreProcessedTxAndDeleteFromPool(ctx context.Context, tx tr
return err
}

txData, err := state.EncodeTransaction(tx.response.Tx, uint8(tx.response.EffectivePercentage))
txData, err := state.EncodeTransaction(tx.response.Tx, uint8(tx.response.EffectivePercentage), d.cfg.ForkID)
if err != nil {
return err
}
Expand Down Expand Up @@ -236,15 +236,15 @@ func (d *dbManager) GetWIPBatch(ctx context.Context) (*WipBatch, error) {
previousLastBatch = lastBatches[1]
}

lastBatchTxs, _, _, err := state.DecodeTxs(lastBatch.BatchL2Data)
lastBatchTxs, _, _, err := state.DecodeTxs(lastBatch.BatchL2Data, d.cfg.ForkID)
if err != nil {
return nil, err
}
lastBatch.Transactions = lastBatchTxs

var prevLastBatchTxs []types.Transaction
if previousLastBatch != nil {
prevLastBatchTxs, _, _, err = state.DecodeTxs(previousLastBatch.BatchL2Data)
prevLastBatchTxs, _, _, err = state.DecodeTxs(previousLastBatch.BatchL2Data, d.cfg.ForkID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -395,7 +395,7 @@ func (d *dbManager) CloseBatch(ctx context.Context, params ClosingBatchParameter
ClosingReason: params.ClosingReason,
}

batchL2Data, err := state.EncodeTransactions(params.Txs, params.EffectivePercentages)
batchL2Data, err := state.EncodeTransactions(params.Txs, params.EffectivePercentages, d.cfg.ForkID)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ func (f *finalizer) reprocessFullBatch(ctx context.Context, batchNum uint64, exp
Caller: stateMetrics.SequencerCallerLabel,
}
log.Infof("reprocessFullBatch: BatchNumber: %d, OldStateRoot: %s, Ger: %s", batch.BatchNumber, f.batch.initialStateRoot.String(), batch.GlobalExitRoot.String())
txs, _, _, err := state.DecodeTxs(batch.BatchL2Data)
txs, _, _, err := state.DecodeTxs(batch.BatchL2Data, f.cfg.ForkID)

if err != nil {
log.Errorf("reprocessFullBatch: error decoding BatchL2Data before reprocessing full batch: %d. Error: %v", batch.BatchNumber, err)
Expand Down
8 changes: 5 additions & 3 deletions state/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ func (s *State) ProcessSequencerBatch(ctx context.Context, batchNumber uint64, b
}

txs := []types.Transaction{}
forkID := GetForkIDByBatchNumber(s.cfg.ForkIDIntervals, batchNumber)

if processBatchResponse.Responses != nil && len(processBatchResponse.Responses) > 0 {
txs, _, _, err = DecodeTxs(batchL2Data)
txs, _, _, err = DecodeTxs(batchL2Data, forkID)
if err != nil && !errors.Is(err, ErrInvalidData) {
return nil, err
}
Expand Down Expand Up @@ -195,7 +196,7 @@ func (s *State) ProcessBatch(ctx context.Context, request ProcessRequest, update
return nil, err
}

txs, _, effP, err := DecodeTxs(request.Transactions)
txs, _, effP, err := DecodeTxs(request.Transactions, forkID)
if err != nil && !errors.Is(err, ErrInvalidData) {
return nil, err
}
Expand Down Expand Up @@ -407,7 +408,8 @@ func (s *State) CloseBatch(ctx context.Context, receipt ProcessingReceipt, dbTx
// ProcessAndStoreClosedBatch is used by the Synchronizer to add a closed batch into the data base
func (s *State) ProcessAndStoreClosedBatch(ctx context.Context, processingCtx ProcessingContext, encodedTxs []byte, dbTx pgx.Tx, caller metrics.CallerLabel) (common.Hash, error) {
// Decode transactions
decodedTransactions, _, _, err := DecodeTxs(encodedTxs)
forkID := GetForkIDByBatchNumber(s.cfg.ForkIDIntervals, processingCtx.BatchNumber)
decodedTransactions, _, _, err := DecodeTxs(encodedTxs, forkID)
if err != nil && !errors.Is(err, ErrInvalidData) {
log.Debugf("error decoding transactions: %v", err)
return common.Hash{}, err
Expand Down
32 changes: 21 additions & 11 deletions state/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

const (
forkID5 = 5
double = 2
ether155V = 27
etherPre155V = 35
Expand All @@ -30,7 +31,7 @@ const (
)

// EncodeTransactions RLP encodes the given transactions
func EncodeTransactions(txs []types.Transaction, effectivePercentages []uint8) ([]byte, error) {
func EncodeTransactions(txs []types.Transaction, effectivePercentages []uint8, forkID uint64) ([]byte, error) {
var batchL2Data []byte

for i, tx := range txs {
Expand All @@ -40,11 +41,13 @@ func EncodeTransactions(txs []types.Transaction, effectivePercentages []uint8) (
}
batchL2Data = append(batchL2Data, txData...)

effectivePercentageAsHex, err := hex.DecodeHex(fmt.Sprintf("%x", effectivePercentages[i]))
if err != nil {
return nil, err
if forkID >= forkID5 {
effectivePercentageAsHex, err := hex.DecodeHex(fmt.Sprintf("%x", effectivePercentages[i]))
if err != nil {
return nil, err
}
batchL2Data = append(batchL2Data, effectivePercentageAsHex...)
}
batchL2Data = append(batchL2Data, effectivePercentageAsHex...)
}

return batchL2Data, nil
Expand Down Expand Up @@ -109,8 +112,8 @@ func EncodeTransactionWithoutEffectivePercentage(tx types.Transaction) ([]byte,
}

// EncodeTransaction RLP encodes the given transaction
func EncodeTransaction(tx types.Transaction, effectivePercentage uint8) ([]byte, error) {
return EncodeTransactions([]types.Transaction{tx}, []uint8{effectivePercentage})
func EncodeTransaction(tx types.Transaction, effectivePercentage uint8, forkID uint64) ([]byte, error) {
return EncodeTransactions([]types.Transaction{tx}, []uint8{effectivePercentage}, forkID)
}

// EncodeUnsignedTransaction RLP encodes the given unsigned transaction
Expand Down Expand Up @@ -157,7 +160,7 @@ func EncodeUnsignedTransaction(tx types.Transaction, chainID uint64, forcedNonce
}

// DecodeTxs extracts Transactions for its encoded form
func DecodeTxs(txsData []byte) ([]types.Transaction, []byte, []uint8, error) {
func DecodeTxs(txsData []byte, forkID uint64) ([]types.Transaction, []byte, []uint8, error) {
// Process coded txs
var pos uint64
var txs []types.Transaction
Expand Down Expand Up @@ -196,7 +199,11 @@ func DecodeTxs(txsData []byte) ([]types.Transaction, []byte, []uint8, error) {
length = n + num - f7 // num - f7 is the header. For example 0xf7
}

endPos := pos + length + rLength + sLength + vLength + headerByteLength + efficiencyPercentageByteLength
endPos := pos + length + rLength + sLength + vLength + headerByteLength

if forkID >= forkID5 {
endPos += efficiencyPercentageByteLength
}

if endPos > txDataLength {
err := fmt.Errorf("endPos %d is bigger than txDataLength %d", endPos, txDataLength)
Expand All @@ -222,8 +229,11 @@ func DecodeTxs(txsData []byte) ([]types.Transaction, []byte, []uint8, error) {
rData := txsData[dataStart : dataStart+rLength]
sData := txsData[dataStart+rLength : dataStart+rLength+sLength]
vData := txsData[dataStart+rLength+sLength : dataStart+rLength+sLength+vLength]
efficiencyPercentage := txsData[dataStart+rLength+sLength+vLength : endPos]
efficiencyPercentages = append(efficiencyPercentages, uint8(efficiencyPercentage[0]))

if forkID >= forkID5 {
efficiencyPercentage := txsData[dataStart+rLength+sLength+vLength : endPos]
efficiencyPercentages = append(efficiencyPercentages, uint8(efficiencyPercentage[0]))
}

pos = endPos

Expand Down
Loading