Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof aggregation improvements #14

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions op-proposer/proposer/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"
)

const aggregateBatchSize = 1000
Expand Down Expand Up @@ -257,25 +258,29 @@ func (l *L2OutputSubmitter) generateOutputs(ctx context.Context, latestOutput bi
}
}

for i := latestOutputNumber + 1; ; i++ {
block, err := l.L2Client.BlockByNumber(ctx, new(big.Int).SetUint64(i))
// calculate `aggregateBatchSize` proofs at once, which are then aggregated in `nextOutput`
for i := uint64(0); i < aggregateBatchSize; i++ {
number := i + latestOutputNumber + 1
block, err := l.L2Client.BlockByNumber(ctx, new(big.Int).SetUint64(number))
if errors.Is(err, ethereum.NotFound) {
return nil
break
}
if err != nil {
return fmt.Errorf("failed to get block %d: %w", i, err)
return fmt.Errorf("failed to get block %d: %w", number, err)
}

proposal, err := l.prover.Generate(ctx, block)
if err != nil {
return fmt.Errorf("failed to generate proof for block %d: %w", i, err)
return fmt.Errorf("failed to generate proof for block %d: %w", number, err)
}

l.Log.Info("Generated proof for block",
"block", l2BlockRefToBlockID(proposal.To), "l1Origin", proposal.To.L1Origin,
"withdrawals", proposal.Withdrawals, "output", proposal.Output.OutputRoot.String())
l.pending = append(l.pending, proposal)
}

return nil
}

func (l *L2OutputSubmitter) nextOutput(ctx context.Context, latestOutput bindings.TypesOutputProposal) (*Proposal, bool, error) {
Expand All @@ -285,32 +290,34 @@ func (l *L2OutputSubmitter) nextOutput(ctx context.Context, latestOutput binding
return nil, false, err
}

var proposals []*Proposal
for _, proposal := range l.pending {
if proposal.To.Number > latestSafe.Number {
break
}
proposals = append(proposals, proposal)
count := 0
for count < len(l.pending) && l.pending[count].To.Number < latestSafe.Number {
count++
}
if len(proposals) == 0 {
if count <= 0 {
return nil, false, nil
}
l.pending = l.pending[len(proposals):]

for len(proposals) > 1 {
batchLength := min(len(proposals), aggregateBatchSize)
batch := proposals[:batchLength]
for count > 1 {
batchLength := min(count, aggregateBatchSize)
batch := l.pending[:batchLength]
aggregated, err := l.prover.Aggregate(ctx, latestOutput.OutputRoot, batch)
if err != nil {
return nil, false, fmt.Errorf("failed to aggregate proofs: %w", err)
var rpcError rpc.Error
if errors.As(err, &rpcError) {
// if we received an explicit error from the enclave (like "invalid signer"), clear the pending proofs
l.Log.Warn("Non-recoverable error aggregating proofs", "err", err)
l.pending = nil
}
return nil, false, err
}
l.pending = append([]*Proposal{aggregated}, l.pending[batchLength:]...)
count -= batchLength - 1
l.Log.Info("Aggregated proofs",
"output", aggregated.Output.OutputRoot.String(), "blocks", batchLength, "remaining", len(proposals),
"output", aggregated.Output.OutputRoot.String(), "blocks", batchLength, "remaining", count-1,
"withdrawals", aggregated.Withdrawals, "from", aggregated.From.Number, "to", aggregated.To.Number)
proposals = append([]*Proposal{aggregated}, proposals[batchLength:]...)
}
proposal := proposals[0]
l.pending = append([]*Proposal{proposal}, l.pending...)
proposal := l.pending[0]

if proposal.To.Hash != latestSafe.Hash {
l.Log.Warn("Aggregated output does not match the latest batched block, possible reorg",
Expand Down