-
Notifications
You must be signed in to change notification settings - Fork 277
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
feat(permissionless batches): recovery mode after permissionless batches #1073
base: syncUpstream/active
Are you sure you want to change the base?
Changes from 7 commits
66bddc5
605da8b
20caa19
eb6df8a
b23eb93
afdc961
86aa203
6f14c33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2021,15 +2021,15 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) | |
return it.index, err | ||
} | ||
|
||
func (bc *BlockChain) BuildAndWriteBlock(parentBlock *types.Block, header *types.Header, txs types.Transactions) (WriteStatus, error) { | ||
func (bc *BlockChain) BuildAndWriteBlock(parentBlock *types.Block, header *types.Header, txs types.Transactions, sign bool) (*types.Block, WriteStatus, error) { | ||
if !bc.chainmu.TryLock() { | ||
return NonStatTy, errInsertionInterrupted | ||
return nil, NonStatTy, errInsertionInterrupted | ||
} | ||
defer bc.chainmu.Unlock() | ||
|
||
statedb, err := state.New(parentBlock.Root(), bc.stateCache, bc.snaps) | ||
if err != nil { | ||
return NonStatTy, err | ||
return nil, NonStatTy, err | ||
} | ||
|
||
statedb.StartPrefetcher("l1sync") | ||
|
@@ -2040,18 +2040,51 @@ func (bc *BlockChain) BuildAndWriteBlock(parentBlock *types.Block, header *types | |
tempBlock := types.NewBlockWithHeader(header).WithBody(txs, nil) | ||
receipts, logs, gasUsed, err := bc.processor.Process(tempBlock, statedb, bc.vmConfig) | ||
if err != nil { | ||
return NonStatTy, fmt.Errorf("error processing block: %w", err) | ||
return nil, NonStatTy, fmt.Errorf("error processing block %d: %w", header.Number.Uint64(), err) | ||
} | ||
|
||
// TODO: once we have the extra and difficulty we need to verify the signature of the block with Clique | ||
// This should be done with https://github.com/scroll-tech/go-ethereum/pull/913. | ||
|
||
// finalize and assemble block as fullBlock | ||
if sign { | ||
// remember the time as Clique will override it | ||
originalTime := header.Time | ||
|
||
err = bc.engine.Prepare(bc, header) | ||
if err != nil { | ||
return nil, NonStatTy, fmt.Errorf("error preparing block %d: %w", tempBlock.Number().Uint64(), err) | ||
} | ||
|
||
// we want to re-sign the block: set time to original value again. | ||
header.Time = originalTime | ||
} | ||
|
||
// finalize and assemble block as fullBlock: replicates consensus.FinalizeAndAssemble() | ||
header.GasUsed = gasUsed | ||
header.Root = statedb.IntermediateRoot(bc.chainConfig.IsEIP158(header.Number)) | ||
|
||
fullBlock := types.NewBlock(header, txs, nil, receipts, trie.NewStackTrie(nil)) | ||
|
||
// Sign the block if requested | ||
if sign { | ||
resultCh, stopCh := make(chan *types.Block), make(chan struct{}) | ||
if err = bc.engine.Seal(bc, fullBlock, resultCh, stopCh); err != nil { | ||
return nil, NonStatTy, fmt.Errorf("error sealing block %d: %w", fullBlock.Number().Uint64(), err) | ||
} | ||
// Clique.Seal() will only wait for a second before giving up on us. So make sure there is nothing computational heavy | ||
// or a call that blocks between the call to Seal and the line below. Seal might introduce some delay, so we keep track of | ||
// that artificially added delay and subtract it from overall runtime of commit(). | ||
fullBlock = <-resultCh | ||
if fullBlock == nil { | ||
return nil, NonStatTy, fmt.Errorf("sealing block failed %d: block is nil", header.Number.Uint64()) | ||
} | ||
|
||
// verify the generated block with local consensus engine to make sure everything is as expected | ||
if err = bc.engine.VerifyHeader(bc, fullBlock.Header()); err != nil { | ||
return nil, NonStatTy, fmt.Errorf("error verifying signed block %d: %w", fullBlock.Number().Uint64(), err) | ||
} | ||
} | ||
|
||
blockHash := fullBlock.Hash() | ||
// manually replace the block hash in the receipts | ||
for i, receipt := range receipts { | ||
|
@@ -2068,7 +2101,14 @@ func (bc *BlockChain) BuildAndWriteBlock(parentBlock *types.Block, header *types | |
l.BlockHash = blockHash | ||
} | ||
|
||
return bc.writeBlockAndSetHead(fullBlock, receipts, logs, statedb, false) | ||
// Double check: even though we just built the block, make sure it is valid. | ||
if err = bc.validator.ValidateState(fullBlock, statedb, receipts, gasUsed); err != nil { | ||
bc.reportBlock(fullBlock, receipts, err) | ||
return nil, NonStatTy, fmt.Errorf("error validating block %d: %w", fullBlock.Number().Uint64(), err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not that expensive to run but I can't see a reason that this would ever fail. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well we're creating/assembling the block just before, but during development and testing it actually happened that this process was faulty and this method caught it. That's why I left it in. |
||
} | ||
|
||
writeStatus, err := bc.writeBlockAndSetHead(fullBlock, receipts, logs, statedb, false) | ||
colinlyguo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return fullBlock, writeStatus, err | ||
} | ||
|
||
// insertSideChain is called when an import batch hits upon a pruned ancestor | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't change this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reverted. seems my IDE reordered automatically