Skip to content

Commit

Permalink
Fixed dummy chain blockchain block's time that wasn't populated properly
Browse files Browse the repository at this point in the history
  • Loading branch information
maoueh committed Apr 30, 2024
1 parent edf8778 commit cc82e68
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 66 deletions.
7 changes: 5 additions & 2 deletions core/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

type Engine struct {
genesisHeight uint64
genesisTime time.Time
genesisBlockBurst uint64
stopHeight uint64
blockRate time.Duration
Expand All @@ -20,11 +21,12 @@ type Engine struct {
finalBlock *types.Block
}

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

return Engine{
genesisHeight: genesisHeight,
genesisTime: genesisTime,
genesisBlockBurst: genesisBlockBurst,
stopHeight: stopHeight,
blockRate: blockRate,
Expand Down Expand Up @@ -80,7 +82,7 @@ func (e *Engine) Subscription() <-chan *types.Block {

func (e *Engine) createBlocks() (out []*types.Block) {
if e.prevBlock == nil {
genesisBlock := types.GenesisBlock(e.genesisHeight)
genesisBlock := types.GenesisBlock(e.genesisHeight, e.genesisTime)
logrus.WithField("block", blockRef{genesisBlock.Header.Hash, e.genesisHeight}).Info("starting from genesis block height")
e.prevBlock = genesisBlock
e.finalBlock = genesisBlock
Expand Down Expand Up @@ -151,6 +153,7 @@ func (e *Engine) newBlock(height uint64, nonce *uint64, parent *types.Block) *ty
PrevHash: &parent.Header.Hash,
FinalNum: e.finalBlock.Header.Height,
FinalHash: e.finalBlock.Header.Hash,
Timestamp: e.genesisTime.Add(e.blockRate * time.Duration(height)),
},
Transactions: []types.Transaction{},
}
Expand Down
10 changes: 6 additions & 4 deletions core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"context"
"fmt"
"time"

"github.com/sirupsen/logrus"
"github.com/streamingfast/dummy-blockchain/tracer"
Expand All @@ -20,15 +21,16 @@ func NewNode(
storeDir string,
blockRate int,
genesisHeight uint64,
genesisTime time.Time,
genesisBlockBurst uint64,
stopHeight uint64,
serverAddr string,
tracer tracer.Tracer,
) *Node {
store := NewStore(storeDir, genesisHeight)
store := NewStore(storeDir, genesisHeight, genesisTime)

return &Node{
engine: NewEngine(genesisHeight, genesisBlockBurst, stopHeight, blockRate),
engine: NewEngine(genesisHeight, genesisTime, genesisBlockBurst, stopHeight, blockRate),
store: store,
server: NewServer(store, serverAddr),
tracer: tracer,
Expand All @@ -37,7 +39,7 @@ func NewNode(

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

logrus.Info("initializing store")
Expand All @@ -61,7 +63,7 @@ func (node *Node) Initialize() error {
final := node.store.meta.FinalHeight
if final == 0 {
// We are uninitialized, so we need to create a genesis block
final = node.store.genesisHeight
final = node.store.meta.GenesisHeight
}

logrus.WithField("final", final).Info("loading final block")
Expand Down
44 changes: 26 additions & 18 deletions core/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/sirupsen/logrus"
"github.com/streamingfast/dummy-blockchain/types"
Expand All @@ -14,26 +15,33 @@ const (
filesPerDir = 1000
)

type StoreMeta struct {
GenesisHeight uint64 `json:"genesis_height"`
GenesisTimeNanos int64 `json:"genesis_time_nanos"`
FinalHeight uint64 `json:"final_height"`
HeadHeight uint64 `json:"head_height"`
}

type Store struct {
rootDir string
blocksDir string
metaPath string
currentGroup int
genesisHeight uint64

meta struct {
FinalHeight uint64 `json:"final_height"`
HeadHeight uint64 `json:"head_height"`
}
rootDir string
blocksDir string
metaPath string
currentGroup int

meta StoreMeta
}

func NewStore(rootDir string, genesisHeight uint64) *Store {
func NewStore(rootDir string, genesisHeight uint64, genesisTime time.Time) *Store {
return &Store{
rootDir: rootDir,
blocksDir: filepath.Join(rootDir, "blocks"),
metaPath: filepath.Join(rootDir, "meta.json"),
currentGroup: -1,
genesisHeight: genesisHeight,
rootDir: rootDir,
blocksDir: filepath.Join(rootDir, "blocks"),
metaPath: filepath.Join(rootDir, "meta.json"),
currentGroup: -1,

meta: StoreMeta{
GenesisHeight: genesisHeight,
GenesisTimeNanos: genesisTime.UnixNano(),
},
}
}

Expand Down Expand Up @@ -89,8 +97,8 @@ func (store *Store) CurrentBlock() (*types.Block, error) {
}

func (store *Store) ReadBlock(height uint64) (*types.Block, error) {
if height == store.genesisHeight {
return types.GenesisBlock(store.genesisHeight), nil
if height == store.meta.GenesisHeight {
return types.GenesisBlock(store.meta.GenesisHeight, time.Unix(0, store.meta.GenesisTimeNanos)), nil
}

block := &types.Block{}
Expand Down
37 changes: 34 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,44 @@ package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"time"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/streamingfast/dummy-blockchain/core"
"github.com/streamingfast/dummy-blockchain/tracer"
)

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

func (f *Flags) GenesisTime() (time.Time, error) {
if f.GenesisTimeRaw == "" {
return time.Now(), nil
}

genesisTime, err := time.Parse(time.RFC3339, f.GenesisTimeRaw)
if err != nil {
return time.Time{}, fmt.Errorf("parse %q: %w", f.GenesisTimeRaw, err)
}

return genesisTime, nil
}

var cliOpts Flags

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

flags.Uint64Var(&cliOpts.GenesisHeight, "genesis-height", 1, "Blockchain genesis height")
flags.StringVar(&cliOpts.GenesisTimeRaw, "genesis-time", "", "Blockchain genesis time in RFC3339 time format, leave empty for current time")
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")
Expand Down Expand Up @@ -84,12 +103,18 @@ func makeInitCommand() *cobra.Command {
Short: "Initialize local blockchain state",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
genesisTime, err := cliOpts.GenesisTime()
if err != nil {
return fmt.Errorf("get genesis time: %w", err)
}

logrus.
WithField("dir", cliOpts.StoreDir).
WithField("genesis_height", cliOpts.GenesisHeight).
WithField("genesis_time", genesisTime).
Info("initializing chain store")

store := core.NewStore(cliOpts.StoreDir, cliOpts.GenesisHeight)
store := core.NewStore(cliOpts.StoreDir, cliOpts.GenesisHeight, genesisTime)
return store.Initialize()
},
}
Expand Down Expand Up @@ -123,6 +148,11 @@ func makeStartComand() *cobra.Command {
return errors.New("block rate option must be greater than 1")
}

genesisTime, err := cliOpts.GenesisTime()
if err != nil {
return fmt.Errorf("get genesis time: %w", err)
}

var blockTracer tracer.Tracer
if cliOpts.Tracer == "firehose" {
blockTracer = &tracer.FirehoseTracer{}
Expand All @@ -132,6 +162,7 @@ func makeStartComand() *cobra.Command {
cliOpts.StoreDir,
cliOpts.BlockRate,
cliOpts.GenesisHeight,
genesisTime,
cliOpts.GenesisBlockBurst,
cliOpts.StopHeight,
cliOpts.ServerAddr,
Expand Down
4 changes: 2 additions & 2 deletions pb/last_generate.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
generate.sh - Mon Jan 8 15:38:28 EST 2024 - maoueh
firehose-acme/proto revision: 1e24bd4
generate.sh - Tue Apr 30 16:05:24 EDT 2024 - maoueh
firehose-acme/proto revision: b03c6d4
8 changes: 4 additions & 4 deletions pb/sf/acme/type/v1/type.pb.go

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

56 changes: 29 additions & 27 deletions tracer/firehose_tracer.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package tracer

import (
"encoding/base64"
"fmt"
"time"

pbacme "github.com/streamingfast/dummy-blockchain/pb/sf/acme/type/v1"
"github.com/streamingfast/dummy-blockchain/types"
"google.golang.org/protobuf/proto"
)

var _ Tracer = &FirehoseTracer{}
Expand All @@ -30,30 +29,33 @@ func (t *FirehoseTracer) OnBlockEnd(blk *types.Block, finalBlockHeader *types.Bl

header := t.activeBlock.Header

previousNum := uint64(0)
if header.PreviousNum != nil {
previousNum = *header.PreviousNum
}

previousHash := ""
if header.PreviousHash != nil {
previousHash = *header.PreviousHash
}

blockPayload, err := proto.Marshal(t.activeBlock)
if err != nil {
panic(fmt.Errorf("unable to marshal block: %w", err))
}

fmt.Printf("FIRE BLOCK %d %s %d %s %d %d %s\n",
header.Height,
header.Hash,
previousNum,
previousHash,
header.FinalNum,
header.Timestamp,
base64.StdEncoding.EncodeToString(blockPayload),
)
// previousNum := uint64(0)
// if header.PreviousNum != nil {
// previousNum = *header.PreviousNum
// }

// previousHash := ""
// if header.PreviousHash != nil {
// previousHash = *header.PreviousHash
// }

// blockPayload, err := proto.Marshal(t.activeBlock)
// if err != nil {
// panic(fmt.Errorf("unable to marshal block: %w", err))
// }

blockTime := time.Unix(0, int64(header.Timestamp))
fmt.Println(blockTime)

// fmt.Printf("FIRE BLOCK %d %s %d %s %d %d %s\n",
// header.Height,
// header.Hash,
// previousNum,
// previousHash,
// header.FinalNum,
// header.Timestamp,
// base64.StdEncoding.EncodeToString(blockPayload),
// )

t.activeBlock = nil
t.activeTrx = nil
Expand All @@ -71,7 +73,7 @@ func (t *FirehoseTracer) OnBlockStart(header *types.BlockHeader) {
Hash: header.Hash,
FinalNum: header.FinalNum,
FinalHash: header.FinalHash,
Timestamp: uint64(header.Timestamp.UnixNano()),
Timestamp: header.Timestamp.UnixNano(),
},
}

Expand Down
7 changes: 1 addition & 6 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ func MakeHashNonce(data interface{}, nonce *uint64) string {
return fmt.Sprintf("%x", shaSum)
}

func GenesisBlock(height uint64) *Block {
genesisTime, err := time.Parse(time.RFC3339, "2024-01-01T00:00:00Z")
if err != nil {
panic(err)
}

func GenesisBlock(height uint64, genesisTime time.Time) *Block {
header := &BlockHeader{
Height: height,
Hash: MakeHash(height),
Expand Down

0 comments on commit cc82e68

Please sign in to comment.