Skip to content

Commit

Permalink
Merge pull request #99 from 0xPolygonHermez/feature/98-banana_fork-part1
Browse files Browse the repository at this point in the history
feat: add banana (forkid12) batches
  • Loading branch information
joanestebanr authored Aug 21, 2024
2 parents 7ca6a32 + 4c008cd commit 9995ab1
Show file tree
Hide file tree
Showing 25 changed files with 650 additions and 77 deletions.
4 changes: 3 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func TestLoadConfigCommentedUnknownFieldOk(t *testing.T) {
}
func TestLoadConfigValdiumTranslatorOk(t *testing.T) {
fileExtension := "toml"
_, err := config.LoadFileFromString(string(ConfigFileValidiumTranslatorTest), fileExtension)
cfg, err := config.LoadFileFromString(string(ConfigFileValidiumTranslatorTest), fileExtension)
require.NoError(t, err)
require.NotNil(t, cfg)
require.Equal(t, 3, len(cfg.Etherman.Validium.Translator.FullMatchRules))
}
2 changes: 1 addition & 1 deletion config/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestDefault(t *testing.T) {
Enabled: false,
TrustedSequencerURL: "",
DataSourcePriority: []dataavailability.DataSourcePriority{dataavailability.Trusted, dataavailability.External},
Translator: translator.Config{},
Translator: translator.Config{FullMatchRules: []translator.ConfigRuleFullMatch{}},
RetryOnDACErrorInterval: types.Duration{Duration: time.Minute},
RateLimit: utils.NewRateLimitConfig(900, time.Second),
},
Expand Down
39 changes: 28 additions & 11 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 All @@ -262,7 +268,13 @@ func NewClient(cfg Config) (*Client, error) {
log.Errorf("error creating NewDecodeSequenceBatchesElderberryValidium client. Error: %w", err)
return nil, err
}
batchDecoders = append(batchDecoders, decodeEtrogValidium, decodeElderberryValidium)

decodeBananaValidium, err := NewSequenceBatchesDecoderBananaValidium(validium.DataAvailabilityClient)
if err != nil {
log.Errorf("error creating NewSequenceBatchesDecoderBananaValidium client. Error: %w", err)
return nil, err
}
batchDecoders = append(batchDecoders, decodeEtrogValidium, decodeElderberryValidium, decodeBananaValidium)
}
client := &Client{
EthClient: ethClient,
Expand Down Expand Up @@ -780,7 +792,7 @@ func (etherMan *Client) processEvent(ctx context.Context, vLog types.Log, blocks
log.Debug("InitL1InfoRootMap event detected. Ignoring...")
return nil
}
log.Warnf("Event not registered: %+v", vLog)
log.Infof("Event not registered: %+v", vLog)
return nil
}

Expand Down Expand Up @@ -864,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 @@ -922,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 @@ -1513,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
113 changes: 113 additions & 0 deletions etherman/sequence_batches_decode_banana.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package etherman

import (
"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 (
/*
struct BatchData {
bytes transactions;
bytes32 forcedGlobalExitRoot;
uint64 forcedTimestamp;
bytes32 forcedBlockHashL1;
}
function sequenceBatches(
BatchData[] calldata batches,
uint32 indexL1InfoRoot,
uint64 maxSequenceTimestamp,
bytes32 expectedFinalAccInputHash,
address l2Coinbase
)
b910e0f97e3128707f02262ec5c90481c982744e80bd892a4449261f745e95d0
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 DecodeSequenceBatchesBanana struct {
SequenceBatchesBase
}

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

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
}

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 {

bn := lastBatchNumber - uint64(len(sequences)-(i+1))
s := EtrogSequenceData{
Transactions: seq.Transactions,
ForcedGlobalExitRoot: seq.ForcedGlobalExitRoot,
ForcedTimestamp: seq.ForcedTimestamp,
ForcedBlockHashL1: seq.ForcedBlockHashL1,
}
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)

}
Loading

0 comments on commit 9995ab1

Please sign in to comment.