Skip to content

Commit

Permalink
Update aggregator for etrog (0xPolygonHermez#2943)
Browse files Browse the repository at this point in the history
* wip

* update aggregator for etrog

* remove TODO
  • Loading branch information
ToniRamirezM authored Dec 19, 2023
1 parent f341155 commit 456a62c
Show file tree
Hide file tree
Showing 10 changed files with 418 additions and 117 deletions.
57 changes: 55 additions & 2 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/encoding"
ethmanTypes "github.com/0xPolygonHermez/zkevm-node/etherman/types"
"github.com/0xPolygonHermez/zkevm-node/ethtxmanager"
"github.com/0xPolygonHermez/zkevm-node/l1infotree"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -977,6 +978,57 @@ func (a *Aggregator) buildInputProver(ctx context.Context, batchToVerify *state.
return nil, fmt.Errorf("failed to get previous batch, err: %v", err)
}

batchRawData, err := state.DecodeBatchV2(batchToVerify.BatchL2Data)
if err != nil {
return nil, err
}

l1InfoTreeData := map[uint32]*prover.L1Data{}
l1InfoRoot := common.Hash{}
tree, err := l1infotree.NewL1InfoTree(32, [][32]byte{}) // nolint:gomnd
if err != nil {
return nil, err
}

for _, l2blockRaw := range batchRawData.Blocks {
_, contained := l1InfoTreeData[l2blockRaw.IndexL1InfoTree]
if !contained {
l1InfoTreeExitRootStorageEntry, err := a.State.GetL1InfoRootLeafByIndex(ctx, l2blockRaw.IndexL1InfoTree, nil)
if err != nil {
return nil, err
}

leaves, err := a.State.GetLeafsByL1InfoRoot(ctx, l1InfoTreeExitRootStorageEntry.L1InfoTreeRoot, nil)
if err != nil {
return nil, err
}

aLeaves := make([][32]byte, len(leaves))
for i, leaf := range leaves {
aLeaves[i] = l1infotree.HashLeafData(leaf.GlobalExitRoot.GlobalExitRoot, leaf.PreviousBlockHash, uint64(leaf.Timestamp.Unix()))
}

// Calculate smt proof
smtProof, _, err := tree.ComputeMerkleProof(l2blockRaw.IndexL1InfoTree, aLeaves)
if err != nil {
return nil, err
}

protoProof := make([][]byte, len(smtProof))
for i, proof := range smtProof {
protoProof[i] = proof[:]
}

l1InfoTreeData[l2blockRaw.IndexL1InfoTree] = &prover.L1Data{
GlobalExitRoot: l1InfoTreeExitRootStorageEntry.L1InfoTreeLeaf.GlobalExitRoot.GlobalExitRoot.Bytes(),
BlockhashL1: l1InfoTreeExitRootStorageEntry.L1InfoTreeLeaf.PreviousBlockHash.Bytes(),
MinTimestamp: uint32(l1InfoTreeExitRootStorageEntry.L1InfoTreeLeaf.GlobalExitRoot.Timestamp.Unix()),
SmtProof: protoProof,
}
l1InfoRoot = l1InfoTreeExitRootStorageEntry.L1InfoTreeRoot
}
}

inputProver := &prover.InputProver{
PublicInputs: &prover.PublicInputs{
OldStateRoot: previousBatch.StateRoot.Bytes(),
Expand All @@ -985,10 +1037,11 @@ func (a *Aggregator) buildInputProver(ctx context.Context, batchToVerify *state.
ChainId: a.cfg.ChainID,
ForkId: a.cfg.ForkId,
BatchL2Data: batchToVerify.BatchL2Data,
GlobalExitRoot: batchToVerify.GlobalExitRoot.Bytes(),
EthTimestamp: uint64(batchToVerify.Timestamp.Unix()),
L1InfoRoot: l1InfoRoot.Bytes(),
TimestampLimit: uint64(batchToVerify.Timestamp.Unix()),
SequencerAddr: batchToVerify.Coinbase.String(),
AggregatorAddr: a.cfg.SenderAddress,
L1InfoTreeData: l1InfoTreeData,
},
Db: map[string]string{},
ContractsBytecode: map[string]string{},
Expand Down
2 changes: 2 additions & 0 deletions aggregator/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ type stateInterface interface {
DeleteUngeneratedProofs(ctx context.Context, dbTx pgx.Tx) error
CleanupGeneratedProofs(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) error
CleanupLockedProofs(ctx context.Context, duration string, dbTx pgx.Tx) (int64, error)
GetL1InfoRootLeafByIndex(ctx context.Context, l1InfoTreeIndex uint32, dbTx pgx.Tx) (state.L1InfoTreeExitRootStorageEntry, error)
GetLeafsByL1InfoRoot(ctx context.Context, l1InfoRoot common.Hash, dbTx pgx.Tx) ([]state.L1InfoTreeExitRootStorageEntry, error)
}
55 changes: 54 additions & 1 deletion aggregator/mocks/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 456a62c

Please sign in to comment.