Skip to content

Commit

Permalink
feat: add banana validium
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Aug 20, 2024
1 parent 7ca6a32 commit 005e04c
Show file tree
Hide file tree
Showing 15 changed files with 381 additions and 6 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.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const DefaultValues = `
RetryOnDACErrorInterval = "1m"
DataSourcePriority = ["trusted", "external"]
[Etherman.Validium.Translator]
FullMatchRules = []
FullMatchRules = [{Old="http://zkevm-dac-001:8484", New="http://127.0.0.1:32794"}]
[Etherman.Validium.RateLimit]
NumRequests = 900
Interval = "1s"
Expand Down
8 changes: 7 additions & 1 deletion etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,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
93 changes: 93 additions & 0 deletions etherman/sequence_batches_decode_banana.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package etherman

import (
"fmt"

"github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonvalidiumetrog"
"github.com/ethereum/go-ethereum/common"
)

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 = []byte{0xdb, 0x5b, 0x0e, 0xd7} // 165e8a8d sequenceBatches((bytes,bytes32,uint64,bytes32)[],uint32,uint64,bytes32,address)
methodIDSequenceBatchesBananaName = "sequenceBatchesBanana"
)

type DecodeSequenceBatchesDecodeBanana struct {
SequenceBatchesBase
}

func NewDecodeSequenceBatchesDecodeBanana() (*DecodeSequenceBatchesDecodeBanana, error) {
base, err := NewSequenceBatchesBase(methodIDSequenceBatchesBanana, methodIDSequenceBatchesBananaName, polygonvalidiumetrog.PolygonvalidiumetrogABI)
if err != nil {
return nil, err
}
return &DecodeSequenceBatchesDecodeBanana{*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)
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",
}
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,
}
}
return sequencedBatches, nil
*/
}
164 changes: 164 additions & 0 deletions etherman/sequence_batches_decode_banana_validium.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package etherman

import (
"encoding/json"
"fmt"

"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"
)

var (
/*
// contract: v8.0.0-rc.1-fork.12
https://github.com/0xPolygonHermez/zkevm-contracts/blob/a5eacc6e51d7456c12efcabdfc1c37457f2219b2/contracts/v2/consensus/validium/PolygonValidiumEtrog.sol#L29
struct ValidiumBatchData {
bytes32 transactionsHash;
bytes32 forcedGlobalExitRoot;
uint64 forcedTimestamp;
bytes32 forcedBlockHashL1;
}
//https://github.com/0xPolygonHermez/zkevm-contracts/blob/a9b4f742f66bd4f3bcd98a3a188422480ffe0d4e/contracts/v2/consensus/validium/PolygonValidiumEtrog.sol#L91
function sequenceBatchesValidium(
ValidiumBatchData[] calldata batches,
uint32 indexL1InfoRoot,
uint64 maxSequenceTimestamp,
bytes32 expectedFinalAccInputHash,
address l2Coinbase,
bytes calldata dataAvailabilityMessage
)
165e8a8d50cd47dabdf9bde8bf707c673d2379d465bd458693e23b75ab3a4424
sequenceBatchesValidium((bytes32,bytes32,uint64,bytes32)[],uint32,uint64,bytes32,address,bytes)
*/
methodIDSequenceBatchesBananaValidium = []byte{0x16, 0x5e, 0x8a, 0x8d} // 165e8a8d sequenceBatchesValidium((bytes32,bytes32,uint64,bytes32)[],uint32,uint64,bytes32,address,bytes)
methodIDSequenceBatchesBananaValidiumName = "sequenceBatchesBananaValidium"
)

type SequenceBatchesDecoderBananaValidium struct {
SequenceBatchesBase
da dataavailability.BatchDataProvider
}

func NewSequenceBatchesDecoderBananaValidium(da dataavailability.BatchDataProvider) (*SequenceBatchesDecoderBananaValidium, error) {
base, err := NewSequenceBatchesBase(methodIDSequenceBatchesBananaValidium,
methodIDSequenceBatchesBananaValidiumName, polygonvalidiumetrog.PolygonvalidiumetrogABI)
if err != nil {
return nil, err
}
return &SequenceBatchesDecoderBananaValidium{*base, da}, nil
}

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")
}
decoded, err := decodeSequenceCallData(s.SmcABI(), txData)
if err != nil {
return nil, err
}
data := decoded.Data
bytedata := decoded.InputByteData
var sequencesValidium []polygonvalidiumetrog.PolygonValidiumEtrogValidiumBatchData
err = json.Unmarshal(bytedata, &sequencesValidium)
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)
dataAvailabilityMsg := data[5].([]byte)
bananaData := BananaSequenceData{

Check failure on line 79 in etherman/sequence_batches_decode_banana_validium.go

View workflow job for this annotation

GitHub Actions / unittest

bananaData declared and not used

Check failure on line 79 in etherman/sequence_batches_decode_banana_validium.go

View workflow job for this annotation

GitHub Actions / check_version

bananaData declared and not used

Check failure on line 79 in etherman/sequence_batches_decode_banana_validium.go

View workflow job for this annotation

GitHub Actions / test_db

bananaData declared and not used

Check failure on line 79 in etherman/sequence_batches_decode_banana_validium.go

View workflow job for this annotation

GitHub Actions / test_db

bananaData declared and not used

Check failure on line 79 in etherman/sequence_batches_decode_banana_validium.go

View workflow job for this annotation

GitHub Actions / unittest

bananaData declared and not used

Check failure on line 79 in etherman/sequence_batches_decode_banana_validium.go

View workflow job for this annotation

GitHub Actions / lint

bananaData declared and not used) (typecheck)

Check failure on line 79 in etherman/sequence_batches_decode_banana_validium.go

View workflow job for this annotation

GitHub Actions / lint

bananaData declared and not used) (typecheck)

Check failure on line 79 in etherman/sequence_batches_decode_banana_validium.go

View workflow job for this annotation

GitHub Actions / lint

bananaData declared and not used) (typecheck)

Check failure on line 79 in etherman/sequence_batches_decode_banana_validium.go

View workflow job for this annotation

GitHub Actions / lint

bananaData declared and not used) (typecheck)

Check failure on line 79 in etherman/sequence_batches_decode_banana_validium.go

View workflow job for this annotation

GitHub Actions / lint

bananaData declared and not used) (typecheck)

Check failure on line 79 in etherman/sequence_batches_decode_banana_validium.go

View workflow job for this annotation

GitHub Actions / lint

bananaData declared and not used) (typecheck)

Check failure on line 79 in etherman/sequence_batches_decode_banana_validium.go

View workflow job for this annotation

GitHub Actions / check_version

bananaData declared and not used
CounterL1InfoRoot: counterL1InfoRoot,
MaxSequenceTimestamp: maxSequenceTimestamp,
ExpectedFinalAccInputHash: expectedFinalAccInputHash,
DataAvailabilityMsg: dataAvailabilityMsg,
}
batchInfos := createBatchInfoBanana(sequencesValidium, lastBatchNumber)

batchData, err := retrieveBatchData(s.da, batchInfos, dataAvailabilityMsg)
if err != nil {
return nil, err
}

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

SequencedBatchMetadata := &SequencedBatchMetadata{
CallFunctionName: s.NameMethodID(txData[:4]),
ForkName: "banana",
RollupFlavor: RollupFlavorValidium,
}
sequencedBatches := createSequencedBatchListBanana(sequencesValidium, batchInfos, batchData, l1InfoRoot, sequencer, txHash, nonce, coinbase, maxSequenceTimestamp, SequencedBatchMetadata)

return sequencedBatches, nil

}

func createBatchInfoBanana(sequencesValidium []polygonvalidiumetrog.PolygonValidiumEtrogValidiumBatchData, lastBatchNumber uint64) []batchInfo {
// Pair the batch number, hash, and if it is forced. This will allow
// retrieval from different sources, and keep them in original order.

var batchInfos []batchInfo
for i, d := range sequencesValidium {
bn := lastBatchNumber - uint64(len(sequencesValidium)-(i+1))
forced := d.ForcedTimestamp > 0
h := d.TransactionsHash
batchInfos = append(batchInfos, batchInfo{num: bn, hash: h, isForced: forced})
}
return batchInfos
}

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,
metaData *SequencedBatchMetadata) []SequencedBatch {
sequencedBatches := make([]SequencedBatch, len(sequencesValidium))
for i, info := range batchInfos {
bn := info.num
s := elderberrypolygonzkevm.PolygonRollupBaseEtrogBatchData{
Transactions: batchData[i].Data,
ForcedGlobalExitRoot: sequencesValidium[i].ForcedGlobalExitRoot,
ForcedTimestamp: sequencesValidium[i].ForcedTimestamp,
ForcedBlockHashL1: sequencesValidium[i].ForcedBlockHashL1,
}
if metaData != nil {
switch batchData[i].Source {
case dataavailability.External:
metaData.SourceBatchData = SourceBatchDataValidiumDAExternal
case dataavailability.Trusted:
metaData.SourceBatchData = SourceBatchDataValidiumDATrusted

metaData.SourceBatchData = string(batchData[i].Source)
}
}
batch := SequencedBatch{
BatchNumber: bn,
L1InfoRoot: &l1InfoRoot,
SequencerAddr: sequencer,
TxHash: txHash,
Nonce: nonce,
Coinbase: coinbase,
PolygonRollupBaseEtrogBatchData: &s,
Metadata: metaData,
}

/*
elderberry := &SequencedBatchElderberryData{
MaxSequenceTimestamp: maxSequenceTimestamp,
InitSequencedBatchNumber: initSequencedBatchNumber,
}
batch.SequencedBatchElderberryData = elderberry
*/
sequencedBatches[i] = batch
}
return sequencedBatches
}
5 changes: 5 additions & 0 deletions etherman/sequence_batches_decode_banana_validium_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package etherman

const (
txDataBananaValidiumHex = "165e8a8d00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000066c43b8e443448cabd1b7808f64eb3f2737180b7ef27a0a28e19afebb2981714a6492c9b0000000000000000000000005b06837a43bdc3dd9f114558daf4b26ed49842ed00000000000000000000000000000000000000000000000000000000000002600000000000000000000000000000000000000000000000000000000000000003d09d1238668f9e4dd74d503f15a90c8fc230069b5ace4a3327896b50bf809db5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000df729b193d68d2b455c59f7040687d4dc2d147557442aeef4c10cc5769d47ade00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000014df0077a11ccf22bd14d1ec3e209ba5d8e065ce73556fdc8fb9c19b83af4ec30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000055f10249e353edebbaa84755e6216d6bf44a1be5e5b140ed6cd94f56dcd7611ad2202708675673a014fcea40c1519ae920ed7652e23e43bf837e11fc5c403539fd1b5951f5b2604c9b42e478d5e2b2437f44073ef9a60000000000000000000000"
)
10 changes: 9 additions & 1 deletion etherman/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ func (s *SequencedBatchMetadata) String() string {
return fmt.Sprintf("SourceBatchData: %s RollupFlavor: %s CallFunctionName: %s ForkName: %s", s.SourceBatchData, s.RollupFlavor, s.CallFunctionName, s.ForkName)
}

type BananaSequenceData struct {
CounterL1InfoRoot uint32
MaxSequenceTimestamp uint64
ExpectedFinalAccInputHash common.Hash
DataAvailabilityMsg []byte
}

// SequencedBatch represents virtual batch
type SequencedBatch struct {
BatchNumber uint64
Expand All @@ -89,7 +96,8 @@ type SequencedBatch struct {
*polygonzkevm.PolygonRollupBaseEtrogBatchData
// Struct used in Elderberry
*SequencedBatchElderberryData
Metadata *SequencedBatchMetadata
BananaData BananaSequenceData
Metadata *SequencedBatchMetadata
}

func (s *SequencedBatch) String() string {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
)

require (
github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240819092536-5a65d4761b2f // indirect
github.com/DataDog/zstd v1.5.2 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240819092536-5a65d4761b2f h1:i9oCNDG4N7ha3fNkEKbito/HF3o4gjnW6//cpTwnp8E=
github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240819092536-5a65d4761b2f/go.mod h1:mFlcEjsm2YBBsu8atHJ3zyVnwM+Z/fMXpVmIJge+WVU=
github.com/0xPolygon/cdk-data-availability v0.0.7 h1:i5v2I8uEgHSZ5BjnJs3Dsy1XpKdSvfZbON9D16gSCUg=
github.com/0xPolygon/cdk-data-availability v0.0.7/go.mod h1:qF+xt2gYwBab8XPh3fr6pnNUMEJq/32rmJN0D630hmY=
github.com/0xPolygon/cdk-data-availability v0.0.8-0.20240712072318-72ae67613cbf h1:VWxVYeDhDURGp8pHR4kq3jwoXRazVHsQLMMIqYsO+Fw=
Expand Down
Loading

0 comments on commit 005e04c

Please sign in to comment.