Skip to content

Commit

Permalink
misc edits from testing with command line, adds outer cancel from get…
Browse files Browse the repository at this point in the history
…h command, fixes forkid issue
  • Loading branch information
alecps committed Aug 9, 2024
1 parent 4c028a8 commit 9374344
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 11 deletions.
30 changes: 30 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/celo-org/celo-blockchain/cmd/utils"
"github.com/celo-org/celo-blockchain/common"
"github.com/celo-org/celo-blockchain/console/prompt"
ethCore "github.com/celo-org/celo-blockchain/core"
"github.com/celo-org/celo-blockchain/core/types"
"github.com/celo-org/celo-blockchain/eth"
"github.com/celo-org/celo-blockchain/eth/downloader"
Expand Down Expand Up @@ -327,6 +328,11 @@ func geth(ctx *cli.Context) error {

prepare(ctx)
stack, backend := makeFullNode(ctx)

if backend.ChainConfig().IsL2Migration(backend.CurrentBlock().Number()) {
return fmt.Errorf("Attempted to start node with an l2 migration block that has already been reached. latestBlock: %d, hash: %s", backend.CurrentBlock().NumberU64(), backend.CurrentBlock().Hash())
}

defer stack.Close()

startNode(ctx, stack, backend)
Expand Down Expand Up @@ -421,6 +427,30 @@ func startNode(ctx *cli.Context, stack *node.Node, backend ethapi.Backend) {
}()
}

// Spawn a standalone goroutine to monitor the chain head for the l2 migration block.
// When the l2 migration block is reached, close the entire stack.
if ctx.GlobalIsSet(utils.L2MigrationBlockFlag.Name) && ctx.GlobalUint64(utils.L2MigrationBlockFlag.Name) > 0 {
go func() {
chainHeadCh := make(chan ethCore.ChainHeadEvent, 10)
chainHeadSub := backend.SubscribeChainHeadEvent(chainHeadCh)
defer chainHeadSub.Unsubscribe()
for {
select {
case chainHeadEvent := <-chainHeadCh:
block := chainHeadEvent.Block
if backend.ChainConfig().IsL2Migration(block.Number()) {
log.Info("L2 Migration Block Reached, closing entire stack from geth cmd", "block", block.NumberU64(), "hash", block.Hash())
stack.Close()
return
}
case err := <-chainHeadSub.Err():
log.Error("Error in outer subscription to blockchain's chainhead event listening for l2 migration block", "err", err)
return
}
}
}()
}

isFullNode := ctx.GlobalString(utils.SyncModeFlag.Name) == "full" || ctx.GlobalString(utils.SyncModeFlag.Name) == "fast"
// Miners and proxies only makes sense if a full node is running
if ctx.GlobalBool(utils.ProxyFlag.Name) || ctx.GlobalBool(utils.MiningEnabledFlag.Name) || ctx.GlobalBool(utils.DeveloperFlag.Name) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ var (

L2MigrationBlockFlag = cli.Uint64Flag{
Name: "l2migrationblock",
Usage: "Block number at which to halt the network for Celo L2 migration. Last block of Celo as an L1.",
Usage: "Block number at which to halt the network for Celo L2 migration. Last block of Celo as an L1. If unset or set to 0, no halt will occur.",
}

BloomFilterSizeFlag = cli.Uint64Flag{
Expand Down
6 changes: 5 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,10 @@ func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
//
// Note, this function assumes that the `mu` mutex is held!
func (bc *BlockChain) writeHeadBlock(block *types.Block) {
if bc.Config().IsL2Migration(new(big.Int).Sub(block.Number(), big.NewInt(1))) {
bc.StopInsert() // Just for good measure
log.Crit("Attempt to insert block beyond L2 migration block. This should never happen.", "block", block.NumberU64(), "hash", block.Hash())
}
// If the block is on a side chain or an unknown one, force other heads onto it too
updateHeads := rawdb.ReadCanonicalHash(bc.db, block.NumberU64()) != block.Hash()

Expand Down Expand Up @@ -1093,7 +1097,7 @@ func (bc *BlockChain) Stop() {
triedb := bc.stateCache.TrieDB()
triedb.SaveCache(bc.cacheConfig.TrieCleanJournal)
}
log.Info("Blockchain stopped")
log.Info("Blockchain stopped", "number", bc.CurrentBlock().NumberU64(), "hash", bc.CurrentBlock().Hash())
}

// StopInsert interrupts all insertion methods, causing them to return
Expand Down
4 changes: 4 additions & 0 deletions core/forkid/forkid.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ func gatherForks(config *params.ChainConfig) []uint64 {
if !strings.HasSuffix(field.Name, "Block") {
continue
}
// Do not include L2MigrationBlock in forkid as doing so will prevent syncing with nodes that have not set the L2MigrationBlock flag
if field.Name == "L2MigrationBlock" {
continue
}
if field.Type != reflect.TypeOf(new(big.Int)) {
continue
}
Expand Down
7 changes: 4 additions & 3 deletions e2e_test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"math/big"
"os"
"os/exec"
"strings"
"sync"
Expand Down Expand Up @@ -34,10 +35,10 @@ func init() {
// This statement is commented out but left here since its very useful for
// debugging problems and its non trivial to construct.
//
// log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stdout, log.TerminalFormat(true))))
log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stdout, log.TerminalFormat(true))))

// This disables all logging which in general we want, because there is a lot
log.Root().SetHandler(log.DiscardHandler())
// log.Root().SetHandler(log.DiscardHandler())
}

// This test starts a network submits a transaction and waits for the whole
Expand Down Expand Up @@ -382,7 +383,7 @@ func TestStopNetworkAtL2Block(t *testing.T) {
shutdown()
}()

ctx, cancel := context.WithTimeout(context.Background(), time.Second*4000)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*200)
defer cancel()

err = network.AwaitBlock(ctx, l2Block.Uint64())
Expand Down
11 changes: 7 additions & 4 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/celo-org/celo-blockchain/common"
"github.com/celo-org/celo-blockchain/consensus"
istanbulBackend "github.com/celo-org/celo-blockchain/consensus/istanbul/backend"
"github.com/celo-org/celo-blockchain/core"
"github.com/celo-org/celo-blockchain/core/state"
"github.com/celo-org/celo-blockchain/core/types"
Expand Down Expand Up @@ -239,10 +240,12 @@ func (w *worker) start() {
func (w *worker) stop() {
atomic.StoreInt32(&w.running, 0)

if istanbul, ok := w.engine.(consensus.Istanbul); ok {
err := istanbul.StopValidating()
if err != nil {
log.Error("Error while calling engine.StopValidating", "err", err)
if istanbul, ok := w.engine.(*istanbulBackend.Backend); ok {
if istanbul.IsValidating() {
err := istanbul.StopValidating()
if err != nil {
log.Error("Error while calling engine.StopValidating", "err", err)
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ type ChainConfig struct {
GingerbreadBlock *big.Int `json:"gingerbreadBlock,omitempty"` // Gingerbread switch block (nil = no fork, 0 = already activated)
GingerbreadP2Block *big.Int `json:"gingerbreadP2Block,omitempty"` // GingerbreadP2 switch block (nil = no fork, 0 = already activated)
HForkBlock *big.Int `json:"hforkBlock,omitempty"` // HFork switch block (nil = no fork, 0 = already activated)
L2MigrationBlock *big.Int `json:"l2MigrationBlock,omitempty"` // l2 migration block / last block of l1 (nil = no fork, 0 = already activated)
L2MigrationBlock *big.Int `json:"l2MigrationBlock,omitempty"` // l2 migration block / last block of l1 (nil = no migration, 0 = no migration)

Istanbul *IstanbulConfig `json:"istanbul,omitempty"`
// This does not belong here but passing it to every function is not possible since that breaks
Expand Down Expand Up @@ -467,7 +467,7 @@ func (c *ChainConfig) IsGingerbreadP2(num *big.Int) bool {

// IsL2 returns whether num represents a block number greater than or equal to the L2 migration block (last block before L2)
func (c *ChainConfig) IsL2Migration(num *big.Int) bool {
return isForked(c.L2MigrationBlock, num)
return isForked(c.L2MigrationBlock, num) && c.L2MigrationBlock.Cmp(big.NewInt(0)) > 0 // return false if L2MigrationBlock is nil or 0
}

func (c *ChainConfig) IsHFork(num *big.Int) bool {
Expand Down

0 comments on commit 9374344

Please sign in to comment.