Skip to content

Commit

Permalink
add zkevm banana batches
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Aug 21, 2024
1 parent 936d464 commit 5977675
Show file tree
Hide file tree
Showing 14 changed files with 336 additions and 148 deletions.
29 changes: 20 additions & 9 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,13 @@ func NewClient(cfg Config) (*Client, error) {
return nil, err
}

batchDecoders := []SequenceBatchesDecoder{decodeEtrog, decodeElderberry}
decodeBanana, err := NewDecodeSequenceBatchesBanana()
if err != nil {
log.Errorf("error creating NewDecodeSequenceBatchesBanana client. Error: %w", err)
return nil, err
}

batchDecoders := []SequenceBatchesDecoder{decodeEtrog, decodeElderberry, decodeBanana}
if cfg.Validium.Enabled {
log.Infof("Validium is enabled")
validium, err = NewEthermanValidium(cfg, ethClient)
Expand Down Expand Up @@ -870,7 +876,7 @@ func (etherMan *Client) updateEtrogSequence(ctx context.Context, vLog types.Log,
SequencerAddr: updateEtrogSequence.Sequencer,
TxHash: vLog.TxHash,
Nonce: msg.Nonce,
PolygonRollupBaseEtrogBatchData: &polygonzkevm.PolygonRollupBaseEtrogBatchData{
EtrogSequenceData: &EtrogSequenceData{
Transactions: updateEtrogSequence.Transactions,
ForcedGlobalExitRoot: updateEtrogSequence.LastGlobalExitRoot,
ForcedTimestamp: fullBlock.Time(),
Expand Down Expand Up @@ -928,7 +934,7 @@ func (etherMan *Client) initialSequenceBatches(ctx context.Context, vLog types.L
SequencerAddr: initialSequenceBatches.Sequencer,
TxHash: vLog.TxHash,
Nonce: msg.Nonce,
PolygonRollupBaseEtrogBatchData: &polygonzkevm.PolygonRollupBaseEtrogBatchData{
EtrogSequenceData: &EtrogSequenceData{
Transactions: initialSequenceBatches.Transactions,
ForcedGlobalExitRoot: initialSequenceBatches.LastGlobalExitRoot,
ForcedTimestamp: fullBlock.Time(),
Expand Down Expand Up @@ -1519,12 +1525,17 @@ func decodeSequencedForceBatches(txData []byte, lastBatchNumber uint64, sequence
for i, force := range forceBatches {
bn := lastBatchNumber - uint64(len(forceBatches)-(i+1))
sequencedForcedBatches[i] = SequencedForceBatch{
BatchNumber: bn,
Coinbase: sequencer,
TxHash: txHash,
Timestamp: time.Unix(int64(block.Time()), 0),
Nonce: nonce,
PolygonRollupBaseEtrogBatchData: force,
BatchNumber: bn,
Coinbase: sequencer,
TxHash: txHash,
Timestamp: time.Unix(int64(block.Time()), 0),
Nonce: nonce,
EtrogSequenceData: EtrogSequenceData{
Transactions: force.Transactions,
ForcedGlobalExitRoot: force.ForcedGlobalExitRoot,
ForcedTimestamp: force.ForcedTimestamp,
ForcedBlockHashL1: force.ForcedBlockHashL1,
},
}
}
return sequencedForcedBatches, nil
Expand Down
112 changes: 66 additions & 46 deletions etherman/sequence_batches_decode_banana.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package etherman

import (
"fmt"
"encoding/json"

"github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonvalidiumetrog"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)

var (
Expand All @@ -27,67 +29,85 @@ var (
sequenceBatches((bytes,bytes32,uint64,bytes32)[],uint32,uint64,bytes32,address)
*/
methodIDSequenceBatchesBanana = []byte{0xdb, 0x5b, 0x0e, 0xd7} // 165e8a8d sequenceBatches((bytes,bytes32,uint64,bytes32)[],uint32,uint64,bytes32,address)
methodIDSequenceBatchesBanana = crypto.Keccak256Hash([]byte("sequenceBatches((bytes,bytes32,uint64,bytes32)[],uint32,uint64,bytes32,address)")).Bytes()[:4] // b910e0f9
methodIDSequenceBatchesBananaName = "sequenceBatchesBanana"
)

type DecodeSequenceBatchesDecodeBanana struct {
type DecodeSequenceBatchesBanana struct {
SequenceBatchesBase
}

func NewDecodeSequenceBatchesDecodeBanana() (*DecodeSequenceBatchesDecodeBanana, error) {
func NewDecodeSequenceBatchesBanana() (*DecodeSequenceBatchesBanana, error) {
base, err := NewSequenceBatchesBase(methodIDSequenceBatchesBanana, methodIDSequenceBatchesBananaName, polygonvalidiumetrog.PolygonvalidiumetrogABI)
if err != nil {
return nil, err
}
return &DecodeSequenceBatchesDecodeBanana{*base}, nil
return &DecodeSequenceBatchesBanana{*base}, nil
}

func (s *DecodeSequenceBatchesDecodeBanana) DecodeSequenceBatches(txData []byte, lastBatchNumber uint64, sequencer common.Address, txHash common.Hash, nonce uint64, l1InfoRoot common.Hash) ([]SequencedBatch, error) {
//decoded, err := decodeSequenceCallData(s.SmcABI(), txData)
_, err := decodeSequenceCallData(s.SmcABI(), txData)
func (s *DecodeSequenceBatchesBanana) DecodeSequenceBatches(txData []byte, lastBatchNumber uint64, sequencer common.Address, txHash common.Hash, nonce uint64, l1InfoRoot common.Hash) ([]SequencedBatch, error) {
decoded, err := decodeSequenceCallData(s.SmcABI(), txData)
if err != nil {
return nil, err
}
data := decoded.Data
bytedata := decoded.InputByteData
var sequences []polygonvalidiumetrog.PolygonRollupBaseEtrogBatchData
err = json.Unmarshal(bytedata, &sequences)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("not implemented")
/*
data := decoded.Data
bytedata := decoded.InputByteData
var sequences []polygonvalidiumetrog.PolygonRollupBaseEtrogBatchData
err = json.Unmarshal(bytedata, &sequences)
if err != nil {
return nil, err
}
maxSequenceTimestamp := data[1].(uint64)
initSequencedBatchNumber := data[2].(uint64)
coinbase := (data[3]).(common.Address)
sequencedBatches := make([]SequencedBatch, len(sequences))

SequencedBatchMetadata := &SequencedBatchMetadata{
CallFunctionName: s.NameMethodID(txData[:4]),
RollupFlavor: RollupFlavorZkEVM,
ForkName: "elderberry",
}
counterL1InfoRoot := data[1].(uint32)
maxSequenceTimestamp := data[2].(uint64)
//expectedFinalAccInputHash := data[3].(common.Hash)
expectedFinalAccInputHashraw := data[3].([common.HashLength]byte)
expectedFinalAccInputHash := common.Hash(expectedFinalAccInputHashraw)
coinbase := data[4].(common.Address)

bananaData := BananaSequenceData{
CounterL1InfoRoot: counterL1InfoRoot,
MaxSequenceTimestamp: maxSequenceTimestamp,
ExpectedFinalAccInputHash: expectedFinalAccInputHash,
DataAvailabilityMsg: []byte{},
}
SequencedBatchMetadata := &SequencedBatchMetadata{
CallFunctionName: s.NameMethodID(txData[:4]),
ForkName: "banana",
RollupFlavor: RollupFlavorZkEVM,
}

log.Debugf("Decoded %s: event(lastBatchNumber:%d, sequencer:%s, txHash:%s, nonce:%d, l1InfoRoot:%s) bananaData:%s",
methodIDSequenceBatchesBananaName,
lastBatchNumber, sequencer.String(), txHash.String(), nonce, l1InfoRoot.String(),
bananaData.String())
log.Debugf("%s batchNum: %d Data:%s", methodIDSequenceBatchesBananaName, lastBatchNumber+1, common.Bytes2Hex(txData))
for i, d := range sequences {
log.Debugf("%s BatchData[%d]: %s", methodIDSequenceBatchesBananaName, i, common.Bytes2Hex(d.Transactions))
}
sequencedBatches := make([]SequencedBatch, len(sequences))

for i, seq := range sequences {

for i, seq := range sequences {
elderberry := SequencedBatchElderberryData{
MaxSequenceTimestamp: maxSequenceTimestamp,
InitSequencedBatchNumber: initSequencedBatchNumber,
}
bn := lastBatchNumber - uint64(len(sequences)-(i+1))
s := seq
sequencedBatches[i] = SequencedBatch{
BatchNumber: bn,
L1InfoRoot: &l1InfoRoot,
SequencerAddr: sequencer,
TxHash: txHash,
Nonce: nonce,
Coinbase: coinbase,
PolygonRollupBaseEtrogBatchData: &s,
SequencedBatchElderberryData: &elderberry,
Metadata: SequencedBatchMetadata,
}
bn := lastBatchNumber - uint64(len(sequences)-(i+1))
s := EtrogSequenceData{
Transactions: seq.Transactions,
ForcedGlobalExitRoot: seq.ForcedGlobalExitRoot,
ForcedTimestamp: seq.ForcedTimestamp,
ForcedBlockHashL1: seq.ForcedBlockHashL1,
}
return sequencedBatches, nil
*/
sequencedBatches[i] = SequencedBatch{
BatchNumber: bn,
L1InfoRoot: &l1InfoRoot,
SequencerAddr: sequencer,
TxHash: txHash,
Nonce: nonce,
Coinbase: coinbase,
EtrogSequenceData: &s,
BananaData: &bananaData,
Metadata: SequencedBatchMetadata,
}
}
return sequencedBatches, nil

}
50 changes: 50 additions & 0 deletions etherman/sequence_batches_decode_banana_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package etherman

import (
"encoding/hex"
"testing"

"github.com/0xPolygonHermez/zkevm-synchronizer-l1/log"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)

const (
txDataBananaHex = "b910e0f900000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000066c4b6531317f62f023ccd827099d9070ac5eb453040af849485827b42eb6ac92fe112530000000000000000000000005b06837a43bdc3dd9f114558daf4b26ed49842ed00000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001e0000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000120b0000007a000000000b00000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000120b00000006000000000b000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001b0b00000006000000000b00000006000000000b00000006000000000000000000"
expedtedSeqBanana = `BatchNumber: 2
L1InfoRoot: 0xa958fee09bf3c4d782ab6839f81c8f40316521e19ba088b9c1a580c9a1438506
SequencerAddr: 0x5b06837A43bdC3dD9F114558DAf4B26ed49842Ed
TxHash: 0x2b3fa84d57739604e5fbbc921fca588842923c64e28e0636609ee7ba1bea8c64
Nonce: 1
Coinbase: 0x5b06837A43bdC3dD9F114558DAf4B26ed49842Ed
PolygonZkEVMBatchData: nil
___EtrogSequenceData:ForcedTimestamp: 0
___EtrogSequenceData:ForcedGlobalExitRoot: 0000000000000000000000000000000000000000000000000000000000000000
___EtrogSequenceData:ForcedBlockHashL1: 0000000000000000000000000000000000000000000000000000000000000000
___EtrogSequenceData:Transactions: 0b0000007a000000000b0000000600000000
SequencedBatchElderberryData: nil
BananaData: CounterL1InfoRoot: 1 MaxSequenceTimestamp: 1724167763 ExpectedFinalAccInputHash: 0x1317f62f023ccd827099d9070ac5eb453040af849485827b42eb6ac92fe11253 DataAvailabilityMsg(0):
Metadata: SourceBatchData: RollupFlavor: kEVM CallFunctionName: sequenceBatchesBanana ForkName: banana
`
)

func TestSequencedBatchBananaDecode(t *testing.T) {
sut, err := NewDecodeSequenceBatchesBanana()
require.NoError(t, err)
require.NotNil(t, sut)
txData, err := hex.DecodeString(txDataBananaHex)
require.NoError(t, err)
lastBatchNumber := uint64(4)
sequencer := common.HexToAddress("0x5b06837A43bdC3dD9F114558DAf4B26ed49842Ed")
txHash := common.HexToHash("0x2b3fa84d57739604e5fbbc921fca588842923c64e28e0636609ee7ba1bea8c64")
nonce := uint64(1)
l1InfoRoot := common.HexToHash("0xa958fee09bf3c4d782ab6839f81c8f40316521e19ba088b9c1a580c9a1438506")

res, err := sut.DecodeSequenceBatches(txData, lastBatchNumber, sequencer, txHash, nonce, l1InfoRoot)
require.NoError(t, err)
require.NotNil(t, res)
res0 := res[0].String()
log.Debug(res0)
require.Equal(t, expedtedSeqBanana, res0)

}
54 changes: 25 additions & 29 deletions etherman/sequence_batches_decode_banana_validium.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonvalidiumetrog"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/dataavailability"
elderberrypolygonzkevm "github.com/0xPolygonHermez/zkevm-synchronizer-l1/etherman/smartcontracts/polygonzkevm"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/log"
"github.com/ethereum/go-ethereum/common"
)
Expand Down Expand Up @@ -53,7 +52,7 @@ func NewSequenceBatchesDecoderBananaValidium(da dataavailability.BatchDataProvid
}

func (s *SequenceBatchesDecoderBananaValidium) DecodeSequenceBatches(txData []byte, lastBatchNumber uint64, sequencer common.Address, txHash common.Hash, nonce uint64, l1InfoRoot common.Hash) ([]SequencedBatch, error) {
log.Debugf("%s batchNum: %d Data:%s", methodIDSequenceBatchesBananaValidiumName, lastBatchNumber+1, common.Bytes2Hex(txData))

if s.da == nil {
return nil, fmt.Errorf("data availability backend not set")
}
Expand All @@ -76,14 +75,14 @@ func (s *SequenceBatchesDecoderBananaValidium) DecodeSequenceBatches(txData []by
expectedFinalAccInputHash := common.Hash(expectedFinalAccInputHashraw)
coinbase := data[4].(common.Address)
dataAvailabilityMsg := data[5].([]byte)
/*
bananaData := BananaSequenceData{
CounterL1InfoRoot: counterL1InfoRoot,
MaxSequenceTimestamp: maxSequenceTimestamp,
ExpectedFinalAccInputHash: expectedFinalAccInputHash,
DataAvailabilityMsg: dataAvailabilityMsg,
}
*/

bananaData := BananaSequenceData{
CounterL1InfoRoot: counterL1InfoRoot,
MaxSequenceTimestamp: maxSequenceTimestamp,
ExpectedFinalAccInputHash: expectedFinalAccInputHash,
DataAvailabilityMsg: dataAvailabilityMsg,
}

batchInfos := createBatchInfoBanana(sequencesValidium, lastBatchNumber)

batchData, err := retrieveBatchData(s.da, batchInfos, dataAvailabilityMsg)
Expand All @@ -93,13 +92,16 @@ func (s *SequenceBatchesDecoderBananaValidium) DecodeSequenceBatches(txData []by

log.Debugf("Decoded Banana sequenceBatchesValidium: counterL1InfoRoot:%d maxSequenceTimestamp:%d expectedFinalAccInputHash:%s coinbase:%s dataAvailabilityMsg:%s",
counterL1InfoRoot, maxSequenceTimestamp, expectedFinalAccInputHash, coinbase.Hex(), dataAvailabilityMsg)

log.Debugf("%s batchNum: %d Data:%s", methodIDSequenceBatchesBananaValidiumName, lastBatchNumber+1, common.Bytes2Hex(txData))
for i, d := range batchData {
log.Debugf("%s BatchData[%d]: %s", methodIDSequenceBatchesBananaValidiumName, i, common.Bytes2Hex(d.Data))
}
SequencedBatchMetadata := &SequencedBatchMetadata{
CallFunctionName: s.NameMethodID(txData[:4]),
ForkName: "banana",
RollupFlavor: RollupFlavorValidium,
}
sequencedBatches := createSequencedBatchListBanana(sequencesValidium, batchInfos, batchData, l1InfoRoot, sequencer, txHash, nonce, coinbase, maxSequenceTimestamp, SequencedBatchMetadata)
sequencedBatches := createSequencedBatchListBanana(sequencesValidium, batchInfos, batchData, l1InfoRoot, sequencer, txHash, nonce, coinbase, maxSequenceTimestamp, bananaData, SequencedBatchMetadata)

return sequencedBatches, nil

Expand All @@ -122,11 +124,12 @@ func createBatchInfoBanana(sequencesValidium []polygonvalidiumetrog.PolygonValid
func createSequencedBatchListBanana(sequencesValidium []polygonvalidiumetrog.PolygonValidiumEtrogValidiumBatchData, batchInfos []batchInfo, batchData []dataavailability.BatchL2Data,
l1InfoRoot common.Hash, sequencer common.Address, txHash common.Hash, nonce uint64, coinbase common.Address,
maxSequenceTimestamp uint64,
bananaSequenceData BananaSequenceData,
metaData *SequencedBatchMetadata) []SequencedBatch {
sequencedBatches := make([]SequencedBatch, len(sequencesValidium))
for i, info := range batchInfos {
bn := info.num
s := elderberrypolygonzkevm.PolygonRollupBaseEtrogBatchData{
s := EtrogSequenceData{
Transactions: batchData[i].Data,
ForcedGlobalExitRoot: sequencesValidium[i].ForcedGlobalExitRoot,
ForcedTimestamp: sequencesValidium[i].ForcedTimestamp,
Expand All @@ -143,23 +146,16 @@ func createSequencedBatchListBanana(sequencesValidium []polygonvalidiumetrog.Pol
}
}
batch := SequencedBatch{
BatchNumber: bn,
L1InfoRoot: &l1InfoRoot,
SequencerAddr: sequencer,
TxHash: txHash,
Nonce: nonce,
Coinbase: coinbase,
PolygonRollupBaseEtrogBatchData: &s,
Metadata: metaData,
BatchNumber: bn,
L1InfoRoot: &l1InfoRoot,
SequencerAddr: sequencer,
TxHash: txHash,
Nonce: nonce,
Coinbase: coinbase,
EtrogSequenceData: &s,
BananaData: &bananaSequenceData,
Metadata: metaData,
}

/*
elderberry := &SequencedBatchElderberryData{
MaxSequenceTimestamp: maxSequenceTimestamp,
InitSequencedBatchNumber: initSequencedBatchNumber,
}
batch.SequencedBatchElderberryData = elderberry
*/
sequencedBatches[i] = batch
}
return sequencedBatches
Expand Down
Loading

0 comments on commit 5977675

Please sign in to comment.