Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Sep 19, 2024
1 parent db81a26 commit c511b80
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
16 changes: 16 additions & 0 deletions sequencesender/txbuilder/banana_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,26 @@ func (t *TxBuilderBananaBase) NewSequence(

sequence.OldAccInputHash = oldAccInputHash
sequence.AccInputHash = accInputHash

err = SequenceSanityCheck(sequence)
if err != nil {
return nil, fmt.Errorf("sequenceSanityCheck fails. Err: %w", err)
}
res := NewBananaSequence(*sequence)
return res, nil
}

func SequenceSanityCheck(seq *etherman.SequenceBanana) error {
maxL1InfoIndex, err := CalculateMaxL1InfoTreeIndexInsideSequence(seq)
if err != nil {
return err
}
if seq.CounterL1InfoRoot < maxL1InfoIndex+1 {
return fmt.Errorf("wrong CounterL1InfoRoot(%d): BatchL2Data (max=%d) ", seq.CounterL1InfoRoot, maxL1InfoIndex)
}
return nil
}

func (t *TxBuilderBananaBase) getL1InfoRoot(counterL1InfoRoot uint32) (common.Hash, error) {
return t.globalExitRootContract.L1InfoRootMap(&bind.CallOpts{Pending: false}, counterL1InfoRoot)
}
Expand Down
32 changes: 32 additions & 0 deletions sequencesender/txbuilder/banana_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"math/big"
"testing"

"github.com/0xPolygon/cdk/etherman"
"github.com/0xPolygon/cdk/l1infotreesync"
"github.com/0xPolygon/cdk/log"
"github.com/0xPolygon/cdk/sequencesender/seqsendertypes"
"github.com/0xPolygon/cdk/sequencesender/txbuilder"
"github.com/0xPolygon/cdk/sequencesender/txbuilder/mocks_txbuilder"
"github.com/0xPolygon/cdk/state"
"github.com/0xPolygon/cdk/state/datastream"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -79,6 +81,36 @@ func TestBananaBaseNewSequenceBatch(t *testing.T) {
// TODO: check that the seq have the right values
}

func TestBananaSanityCheck(t *testing.T) {
batch := state.BatchRawV2{
Blocks: []state.L2BlockRaw{
{
BlockNumber: 1,
ChangeL2BlockHeader: state.ChangeL2BlockHeader{
DeltaTimestamp: 1,
IndexL1InfoTree: 1,
},
},
},
}
data, err := state.EncodeBatchV2(&batch)
require.NoError(t, err)
require.NotNil(t, data)
seq := etherman.SequenceBanana{
CounterL1InfoRoot: 2,
Batches: []etherman.Batch{
{
L2Data: data,
},
},
}
err = txbuilder.SequenceSanityCheck(&seq)
require.NoError(t, err, "inside batchl2data max is 1 and counter is 2 (2>=1+1)")
seq.CounterL1InfoRoot = 1
err = txbuilder.SequenceSanityCheck(&seq)
require.Error(t, err, "inside batchl2data max is 1 and counter is 1. The batchl2data is not included in counter")
}

type testDataBananaBase struct {
rollupContract *mocks_txbuilder.RollupBananaBaseContractor
getContract *mocks_txbuilder.GlobalExitRootBananaContractor
Expand Down
32 changes: 32 additions & 0 deletions sequencesender/txbuilder/banana_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/0xPolygon/cdk/etherman"
"github.com/0xPolygon/cdk/sequencesender/seqsendertypes"
"github.com/0xPolygon/cdk/state"
"github.com/ethereum/go-ethereum/common"
)

Expand Down Expand Up @@ -147,3 +148,34 @@ func (b *BananaSequence) LastVirtualBatchNumber() uint64 {
func (b *BananaSequence) SetLastVirtualBatchNumber(batchNumber uint64) {
b.SequenceBanana.LastVirtualBatchNumber = batchNumber
}

func CalculateMaxL1InfoTreeIndexInsideL2Data(l2data []byte) (uint32, error) {
batchRawV2, err := state.DecodeBatchV2(l2data)
if err != nil {
return 0, fmt.Errorf("CalculateMaxL1InfoTreeIndexInsideL2Data: error decoding batchL2Data, err:%w", err)
}
if batchRawV2 == nil {
return 0, fmt.Errorf("CalculateMaxL1InfoTreeIndexInsideL2Data: batchRawV2 is nil")
}
maxIndex := uint32(0)
for i := range batchRawV2.Blocks {
if batchRawV2.Blocks[i].IndexL1InfoTree > maxIndex {
maxIndex = batchRawV2.Blocks[i].IndexL1InfoTree
}
}
return maxIndex, nil
}

func CalculateMaxL1InfoTreeIndexInsideSequence(seq *etherman.SequenceBanana) (uint32, error) {
maxIndex := uint32(0)
for _, batch := range seq.Batches {
index, err := CalculateMaxL1InfoTreeIndexInsideL2Data(batch.L2Data)
if err != nil {
return 0, fmt.Errorf("CalculateMaxL1InfoTreeIndexInsideBatches: error getting batch L1InfoTree , err:%w", err)
}
if index > maxIndex {
maxIndex = index
}
}
return maxIndex, nil
}

0 comments on commit c511b80

Please sign in to comment.