Skip to content

Commit

Permalink
Fix/aggregator etrog inforoot (0xPolygonHermez#3048)
Browse files Browse the repository at this point in the history
* fix inforoot

* fixes

---------

Co-authored-by: Toni Ramírez <[email protected]>
  • Loading branch information
ARR552 and ToniRamirezM authored Jan 10, 2024
1 parent 10fdcf9 commit ac5a221
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 73 deletions.
85 changes: 50 additions & 35 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,52 +1004,50 @@ func (a *Aggregator) buildInputProver(ctx context.Context, batchToVerify *state.
for _, l2blockRaw := range batchRawData.Blocks {
_, contained := l1InfoTreeData[l2blockRaw.IndexL1InfoTree]
if !contained && l2blockRaw.IndexL1InfoTree != 0 {
if l2blockRaw.IndexL1InfoTree != 0 {
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,
}
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,
}
}
}
} else {
// Initial batch must be handled differently
if batchToVerify.BatchNumber == 1 {
l1InfoRoot = batchToVerify.GlobalExitRoot
forcedBlockhashL1, err = a.State.GetVirtualBatchParentHash(ctx, batchToVerify.BatchNumber, nil)
if err != nil {
return nil, err
}
} else {
l1InfoRoot = batchToVerify.GlobalExitRoot
forcedBlockhashL1, err = a.State.GetForcedBatchParentHash(ctx, *batchToVerify.ForcedBatchNum, nil)
forcedBlockhashL1, l1InfoRoot, err = a.State.GetForcedBatchParentHashAndGER(ctx, *batchToVerify.ForcedBatchNum, nil)
if err != nil {
return nil, err
}
Expand All @@ -1075,9 +1073,26 @@ func (a *Aggregator) buildInputProver(ctx context.Context, batchToVerify *state.
ContractsBytecode: map[string]string{},
}

printInputProver(inputProver)

return inputProver, nil
}

func printInputProver(inputProver *prover.InputProver) {
log.Debugf("OldStateRoot: %v", common.BytesToHash(inputProver.PublicInputs.OldStateRoot))
log.Debugf("OldAccInputHash: %v", common.BytesToHash(inputProver.PublicInputs.OldAccInputHash))
log.Debugf("OldBatchNum: %v", inputProver.PublicInputs.OldBatchNum)
log.Debugf("ChainId: %v", inputProver.PublicInputs.ChainId)
log.Debugf("ForkId: %v", inputProver.PublicInputs.ForkId)
log.Debugf("BatchL2Data: %v", common.Bytes2Hex(inputProver.PublicInputs.BatchL2Data))
log.Debugf("L1InfoRoot: %v", common.BytesToHash(inputProver.PublicInputs.L1InfoRoot))
log.Debugf("TimestampLimit: %v", inputProver.PublicInputs.TimestampLimit)
log.Debugf("SequencerAddr: %v", inputProver.PublicInputs.SequencerAddr)
log.Debugf("AggregatorAddr: %v", inputProver.PublicInputs.AggregatorAddr)
log.Debugf("L1InfoTreeData: %+v", inputProver.PublicInputs.L1InfoTreeData)
log.Debugf("ForcedBlockhashL1: %v", common.Bytes2Hex(inputProver.PublicInputs.ForcedBlockhashL1))
}

// healthChecker will provide an implementation of the HealthCheck interface.
type healthChecker struct{}

Expand Down
2 changes: 1 addition & 1 deletion aggregator/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ type stateInterface interface {
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)
GetVirtualBatchParentHash(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetForcedBatchParentHash(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetForcedBatchParentHashAndGER(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, common.Hash, error)
}
25 changes: 17 additions & 8 deletions aggregator/mocks/mock_state.go

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

2 changes: 1 addition & 1 deletion sequencer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type stateInterface interface {
GetL1InfoTreeDataFromBatchL2Data(ctx context.Context, batchL2Data []byte, dbTx pgx.Tx) (map[uint32]state.L1DataV2, common.Hash, error)
GetBlockByNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (*state.Block, error)
GetVirtualBatchParentHash(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetForcedBatchParentHash(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetForcedBatchParentHashAndGER(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, common.Hash, error)
GetL1InfoRootLeafByIndex(ctx context.Context, l1InfoTreeIndex uint32, dbTx pgx.Tx) (state.L1InfoTreeExitRootStorageEntry, error)
}

Expand Down
25 changes: 17 additions & 8 deletions sequencer/mock_state.go

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

15 changes: 10 additions & 5 deletions state/batchV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,16 @@ func (s *State) ProcessAndStoreClosedBatchV2(ctx context.Context, processingCtx
}
}
}
var ger common.Hash
if processingCtx.BatchNumber == 1 {
ger = processingCtx.L1InfoRoot
}
return common.BytesToHash(processed.NewStateRoot), processed.FlushId, processed.ProverId, s.CloseBatchInStorage(ctx, ProcessingReceipt{
BatchNumber: processingCtx.BatchNumber,
StateRoot: processedBatch.NewStateRoot,
LocalExitRoot: processedBatch.NewLocalExitRoot,
AccInputHash: processedBatch.NewAccInputHash,
BatchL2Data: *BatchL2Data,
BatchNumber: processingCtx.BatchNumber,
StateRoot: processedBatch.NewStateRoot,
LocalExitRoot: processedBatch.NewLocalExitRoot,
AccInputHash: processedBatch.NewAccInputHash,
BatchL2Data: *BatchL2Data,
GlobalExitRoot: ger,
}, dbTx)
}
6 changes: 3 additions & 3 deletions state/datastream.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ type DSState interface {
GetStorageAt(ctx context.Context, address common.Address, position *big.Int, root common.Hash) (*big.Int, error)
GetLastL2BlockHeader(ctx context.Context, dbTx pgx.Tx) (*L2Header, error)
GetVirtualBatchParentHash(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetForcedBatchParentHash(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetForcedBatchParentHashAndGER(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, common.Hash, error)
GetL1InfoRootLeafByIndex(ctx context.Context, l1InfoTreeIndex uint32, dbTx pgx.Tx) (L1InfoTreeExitRootStorageEntry, error)
}

Expand Down Expand Up @@ -448,13 +448,13 @@ func GenerateDataStreamerFile(ctx context.Context, streamServer *datastreamer.St
} else {
// Initial batch must be handled differently
if batch.BatchNumber == 1 {
l1InfoRoot = batch.GlobalExitRoot
l1BlockHash, err = stateDB.GetVirtualBatchParentHash(ctx, batch.BatchNumber, nil)
if err != nil {
return err
}
} else {
l1InfoRoot = batch.GlobalExitRoot
l1BlockHash, err = stateDB.GetForcedBatchParentHash(ctx, *batch.ForcedBatchNum, nil)
l1BlockHash, l1InfoRoot, err = stateDB.GetForcedBatchParentHashAndGER(ctx, *batch.ForcedBatchNum, nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,5 @@ type storage interface {
GetLeafsByL1InfoRoot(ctx context.Context, l1InfoRoot common.Hash, dbTx pgx.Tx) ([]L1InfoTreeExitRootStorageEntry, error)
GetBlockByNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (*Block, error)
GetVirtualBatchParentHash(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetForcedBatchParentHash(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetForcedBatchParentHashAndGER(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, common.Hash, error)
}
25 changes: 14 additions & 11 deletions state/pgstatestorage/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,16 +614,16 @@ func (p *PostgresStorage) OpenWIPBatchInStorage(ctx context.Context, batch state
// CloseBatchInStorage closes a batch in the state storage
func (p *PostgresStorage) CloseBatchInStorage(ctx context.Context, receipt state.ProcessingReceipt, dbTx pgx.Tx) error {
const closeBatchSQL = `UPDATE state.batch
SET state_root = $1, local_exit_root = $2, acc_input_hash = $3, raw_txs_data = $4, batch_resources = $5, closing_reason = $6, wip = FALSE
WHERE batch_num = $7`
SET state_root = $1, local_exit_root = $2, acc_input_hash = $3, raw_txs_data = $4, batch_resources = $5, closing_reason = $6, wip = FALSE, global_exit_root = $7
WHERE batch_num = $8`

e := p.getExecQuerier(dbTx)
batchResourcesJsonBytes, err := json.Marshal(receipt.BatchResources)
if err != nil {
return err
}
_, err = e.Exec(ctx, closeBatchSQL, receipt.StateRoot.String(), receipt.LocalExitRoot.String(),
receipt.AccInputHash.String(), receipt.BatchL2Data, string(batchResourcesJsonBytes), receipt.ClosingReason, receipt.BatchNumber)
receipt.AccInputHash.String(), receipt.BatchL2Data, string(batchResourcesJsonBytes), receipt.ClosingReason, receipt.GlobalExitRoot.String(), receipt.BatchNumber)

return err
}
Expand Down Expand Up @@ -973,19 +973,22 @@ func (p *PostgresStorage) GetVirtualBatchParentHash(ctx context.Context, batchNu
return common.HexToHash(parentHash), nil
}

// GetForcedBatchParentHash returns the parent hash of the forced batch with the given number.
func (p *PostgresStorage) GetForcedBatchParentHash(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, error) {
var parentHash string
// GetForcedBatchParentHashAndGER returns the parent hash of the forced batch with the given number and the globalExitRoot.
func (p *PostgresStorage) GetForcedBatchParentHashAndGER(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, common.Hash, error) {
var (
parentHash string
ger string
)

const sql = `SELECT b.parent_hash FROM state.forced_batch f, state.block b
const sql = `SELECT b.parent_hash, f.global_exit_root FROM state.forced_batch f, state.block b
WHERE f.forced_batch_num = $1 and b.block_num = f.block_num`

e := p.getExecQuerier(dbTx)
err := e.QueryRow(ctx, sql, forcedBatchNumber).Scan(&parentHash)
err := e.QueryRow(ctx, sql, forcedBatchNumber).Scan(&parentHash, &ger)
if errors.Is(err, pgx.ErrNoRows) {
return common.Hash{}, state.ErrNotFound
return common.Hash{}, common.Hash{}, state.ErrNotFound
} else if err != nil {
return common.Hash{}, err
return common.Hash{}, common.Hash{}, err
}
return common.HexToHash(parentHash), nil
return common.HexToHash(parentHash), common.HexToHash(ger), nil
}

0 comments on commit ac5a221

Please sign in to comment.