Skip to content

Commit

Permalink
Added support for --genesis-block-burst to produce a bunch of block…
Browse files Browse the repository at this point in the history
… on startup
  • Loading branch information
maoueh committed Jan 26, 2024
1 parent ca1073b commit edf8778
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
29 changes: 18 additions & 11 deletions core/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@ import (
)

type Engine struct {
genesisHeight uint64
stopHeight uint64
blockRate time.Duration
blockChan chan *types.Block
prevBlock *types.Block
finalBlock *types.Block
genesisHeight uint64
genesisBlockBurst uint64
stopHeight uint64
blockRate time.Duration
blockChan chan *types.Block
prevBlock *types.Block
finalBlock *types.Block
}

func NewEngine(genesisHeight, stopHeight uint64, rate int) Engine {
func NewEngine(genesisHeight, genesisBlockBurst, stopHeight uint64, rate int) Engine {
blockRate := time.Minute / time.Duration(rate)

return Engine{
genesisHeight: genesisHeight,
stopHeight: stopHeight,
blockRate: blockRate,
blockChan: make(chan *types.Block),
genesisHeight: genesisHeight,
genesisBlockBurst: genesisBlockBurst,
stopHeight: stopHeight,
blockRate: blockRate,
blockChan: make(chan *types.Block),
}
}

Expand Down Expand Up @@ -84,6 +86,11 @@ func (e *Engine) createBlocks() (out []*types.Block) {
e.finalBlock = genesisBlock

out = append(out, genesisBlock)

for len(out)-1 < int(e.genesisBlockBurst) {
out = append(out, e.createBlocks()...)
}

return
}

Expand Down
7 changes: 5 additions & 2 deletions core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@ func NewNode(
storeDir string,
blockRate int,
genesisHeight uint64,
genesisBlockBurst uint64,
stopHeight uint64,
serverAddr string,
tracer tracer.Tracer,
) *Node {
store := NewStore(storeDir, genesisHeight)

return &Node{
engine: NewEngine(genesisHeight, stopHeight, blockRate),
engine: NewEngine(genesisHeight, genesisBlockBurst, stopHeight, blockRate),
store: store,
server: NewServer(store, serverAddr),
tracer: tracer,
}
}

func (node *Node) Initialize() error {
logrus.WithField("genesis_height", node.store.genesisHeight).Info("initializing node")
logrus.
WithField("genesis_height", node.store.genesisHeight).
Info("initializing node")

logrus.Info("initializing store")
if err := node.store.Initialize(); err != nil {
Expand Down
22 changes: 14 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
)

var cliOpts = struct {
GenesisHeight uint64
LogLevel string
StoreDir string
BlockRate int
ServerAddr string
Tracer string
StopHeight uint64
GenesisHeight uint64
GenesisBlockBurst uint64
LogLevel string
StoreDir string
BlockRate int
ServerAddr string
Tracer string
StopHeight uint64
}{}

func main() {
Expand Down Expand Up @@ -53,6 +54,7 @@ func initFlags(root *cobra.Command) error {
flags := root.PersistentFlags()

flags.Uint64Var(&cliOpts.GenesisHeight, "genesis-height", 1, "Blockchain genesis height")
flags.Uint64Var(&cliOpts.GenesisBlockBurst, "genesis-block-burst", 0, "The amount of block to produce when initially starting from genesis block")
flags.StringVar(&cliOpts.LogLevel, "log-level", "info", "Logging level")
flags.StringVar(&cliOpts.StoreDir, "store-dir", "./data", "Directory for storing blockchain state")
flags.IntVar(&cliOpts.BlockRate, "block-rate", 60, "Block production rate (per minute)")
Expand Down Expand Up @@ -82,7 +84,10 @@ func makeInitCommand() *cobra.Command {
Short: "Initialize local blockchain state",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
logrus.WithField("dir", cliOpts.StoreDir).WithField("genesis_height", cliOpts.GenesisHeight).Info("initializing chain store")
logrus.
WithField("dir", cliOpts.StoreDir).
WithField("genesis_height", cliOpts.GenesisHeight).
Info("initializing chain store")

store := core.NewStore(cliOpts.StoreDir, cliOpts.GenesisHeight)
return store.Initialize()
Expand Down Expand Up @@ -127,6 +132,7 @@ func makeStartComand() *cobra.Command {
cliOpts.StoreDir,
cliOpts.BlockRate,
cliOpts.GenesisHeight,
cliOpts.GenesisBlockBurst,
cliOpts.StopHeight,
cliOpts.ServerAddr,
blockTracer,
Expand Down

0 comments on commit edf8778

Please sign in to comment.