Skip to content

Commit

Permalink
refactor genesis package
Browse files Browse the repository at this point in the history
* don't pass genesisBytes as arg on SetNode, NewVochain, newTendermint, etc

* notably, now cometbft has it's own datadir separate from our state,
  allowing to wipe cometbft data without wiping our state.

* drop genesis.AutoUpdateGenesis
* instead of comparing local and hardcoded genesis Hash, compare ChainID
  to decide wether to wipe out just cometbft, whole datadir, or do nothing
* cmd/node: add flags to override genesis ChainID, InitialHeight and AppHash
* cmd/node: add flag to schedule an EndOfChain

* move SeedNodes from genesis into config package (DefaultSeedNodes)
* add config consts: DefaultCometBFTPath and DefaultGenesisPath

* testsuite: use genesis.go instead of hardcoded genesis.json
    this increases the code coverage of testsuite and helps
    developing refactors of genesis.go
* now testsuite network is `test` (instead of `dev`)

* app.genesisDoc is now populated during newTendermint, instead of
  pulling from app.NodeClient.Genesis, since it's needed during Info() handshake

* cometbft: removed redundant app.State.SetHeight(lastHeight) during Info()

* cometbft: now Info() supports doing a chain bump, if genesis InitialHeight > lastHeight
* cometbft: now ProcessProposal supports stopping the current chain at height config.Forks.EndOfChain

* genesis: remove copypasted types, use upstream comettypes directly
* genesis: deduplicate ConsensusParams literals using DefaultBlockParams, etc
* genesis: remove useless hardcoded StateSyncParams
* genesis: add a simple unit test TestSaveAsAndLoad
  • Loading branch information
altergui committed May 16, 2024
1 parent 7b18fa9 commit 2284015
Show file tree
Hide file tree
Showing 25 changed files with 611 additions and 576 deletions.
8 changes: 8 additions & 0 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ func loadConfig() *config.Config {
"external address:port to announce to other peers (automatically guessed if empty)")
flag.String("vochainGenesis", "",
"use alternative genesis file for the vochain")
flag.String("vochainGenesisChainID", "",
"override ChainID in genesis for the vochain")
flag.Int64("vochainGenesisInitialHeight", 0,
"override InitialHeight in genesis for the vochain")
flag.String("vochainGenesisAppHash", "",
"override AppHash in genesis for the vochain")
flag.Int64("vochainEndOfChain", 0,
"height at which this node will refuse adding new blocks to the chain")
flag.String("vochainLogLevel", "disabled",
"tendermint node log level (debug, info, error, disabled)")
flag.StringSlice("vochainPeers", []string{},
Expand Down
10 changes: 4 additions & 6 deletions cmd/tools/vochaininspector/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"go.vocdoni.io/dvote/statedb"
"go.vocdoni.io/dvote/util"
"go.vocdoni.io/dvote/vochain"
"go.vocdoni.io/dvote/vochain/genesis"
"go.vocdoni.io/dvote/vochain/state"
"go.vocdoni.io/dvote/vochain/vochaininfo"
"go.vocdoni.io/proto/build/go/models"
Expand Down Expand Up @@ -62,15 +61,15 @@ func main() {
if blockHeight == 0 {
log.Fatal("listProcess requires a height value")
}
path := filepath.Join(dataDir, "data", "vcstate")
path := filepath.Join(dataDir, vochain.StateDataDir)
log.Infof("opening state database path %s", path)
listStateProcesses(int64(blockHeight), path)

case "listVotes":
if blockHeight == 0 {
log.Fatal("listVotes requires a height value")
}
path := filepath.Join(dataDir, "data", "vcstate")
path := filepath.Join(dataDir, vochain.StateDataDir)
log.Infof("opening state database path %s", path)
listStateVotes(pid, int64(blockHeight), path)

Expand All @@ -84,7 +83,7 @@ func main() {
if blockHeight == 0 {
log.Fatal("stateGraph requires a height value")
}
path := filepath.Join(dataDir, "data", "vcstate")
path := filepath.Join(dataDir, vochain.StateDataDir)
graphVizMainTree(int64(blockHeight), path)

case "sync":
Expand Down Expand Up @@ -191,8 +190,7 @@ func newVochain(network, dataDir string) *vochain.BaseApplication {
}
log.Infof("external ip address %s", cfg.PublicAddr)
// Create the vochain node
genesisBytes := genesis.Genesis[network].Genesis.Marshal()
return vochain.NewVochain(cfg, genesisBytes)
return vochain.NewVochain(cfg)
}

func listBlockVotes(_ int64, _ string) {
Expand Down
8 changes: 8 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ type VochainCfg struct {
DBType string
// Genesis path where the genesis file is stored
Genesis string
// GenesisChainID overrides ChainID in hardcoded genesis
GenesisChainID string
// GenesisInitialHeight overrides InitialHeight in hardcoded genesis
GenesisInitialHeight int64
// GenesisAppHash overrides AppHash in hardcoded genesis
GenesisAppHash string
// Peers peers with which the node tries to connect
Peers []string
// Seeds seeds with which the node tries to connect
Expand All @@ -105,6 +111,8 @@ type VochainCfg struct {
PrivValidatorListenAddr string
// NoWaitSync if enabled the Vochain synchronization won't be blocking
NoWaitSync bool
// EndOfChain is the height at which this node will refuse adding new blocks to the chain
EndOfChain int64
// MempoolSize is the size of the mempool
MempoolSize int
// SkipPreviousOffchainData if enabled, the node will skip downloading the previous off-chain data to the current block
Expand Down
25 changes: 25 additions & 0 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,29 @@ package config
// These consts are defaults used in VochainCfg
const (
DefaultMinerTargetBlockTimeSeconds = 10
DefaultCometBFTPath = "cometbft"
DefaultGenesisPath = DefaultCometBFTPath + "/config/genesis.json"
)

// DefaultSeedNodes is a map indexed by network name
var DefaultSeedNodes = map[string][]string{
// testsuite test network
"test": {
"3c3765494e758ae7baccb1f5b0661755302ddc47@seed:26656",
},
// Development network
"dev": {
"[email protected]:26656",
},

// Staging network
"stage": {
"[email protected]:26656",
},

// LTS production network
"lts": {
"[email protected]:26656",
"[email protected]:26656",
},
}
50 changes: 35 additions & 15 deletions config/forks.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
package config

import (
"sync"
)

// ForksCfg allows applying softforks at specified heights
type ForksCfg struct {
VoceremonyForkBlock uint32
NullifierFromZkProof uint32
EndOfChain uint32
}

// Forks is a map of chainIDs
var Forks = map[string]*ForksCfg{
"vocdoni/DEV/29": {
VoceremonyForkBlock: 217200, // estimated 2023-12-05T11:33:31.426638381Z
},
"vocdoni/STAGE/9": {
VoceremonyForkBlock: 250000, // estimated 2023-12-11T12:09:00.917676214Z
NullifierFromZkProof: 439000, // estimated 2024-01-03T12:09:30.009477164Z
},
"vocdoni/LTS/1.2": {
VoceremonyForkBlock: 400200, // estimated 2023-12-12T09:09:31.511245938Z
NullifierFromZkProof: 575800, // estimated 2024-01-03T12:09:30.009477164Z
},
}
var (
mu sync.RWMutex
// forks is a map of chainIDs to their respective ForksCfg
forks = map[string]*ForksCfg{
"vocdoni/TEST/1.2": {
EndOfChain: 100,
},
"vocdoni/DEV/29": {
VoceremonyForkBlock: 217200, // estimated 2023-12-05T11:33:31.426638381Z
},
"vocdoni/STAGE/9": {
VoceremonyForkBlock: 250000, // estimated 2023-12-11T12:09:00.917676214Z
NullifierFromZkProof: 439000, // estimated 2024-01-03T12:09:30.009477164Z
},
"vocdoni/LTS/1.2": {
VoceremonyForkBlock: 400200, // estimated 2023-12-12T09:09:31.511245938Z
NullifierFromZkProof: 575800, // estimated 2024-01-03T12:09:30.009477164Z
},
}
)

// ForksForChainID returns the ForksCfg of chainID, if found, or an empty ForksCfg otherwise
func ForksForChainID(chainID string) *ForksCfg {
if cfg, found := Forks[chainID]; found {
mu.RLock()
defer mu.RUnlock()
if cfg, found := forks[chainID]; found {
return cfg
}
return &ForksCfg{}
}

// SetForksForChainID sets the ForksCfg of chainID
func SetForksForChainID(chainID string, forksCfg *ForksCfg) {
mu.Lock()
defer mu.Unlock()
forks[chainID] = forksCfg
}
2 changes: 0 additions & 2 deletions dockerfiles/testsuite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ the testnet is composed of:
* four [miners](https://docs.vocdoni.io/architecture/services/vochain.html#miner) (aka [validator nodes](https://docs.tendermint.com/master/nodes/#validators) in tendermint jargon)
* one [gateway](https://docs.vocdoni.io/architecture/components.html#gateway)

the `genesis.json` file lists the public keys of all the miners, since vochain is a Proof-of-Authority.

the seed node will serve to bootstrap the network: it'll just wait for incoming connections from other nodes, and provide them a list of peers which they can connect to.
the miners will first connect to the seed node, get the list of peers, and connect to each other. when there are at least 3 miners online, they can reach consensus and start producing blocks.

Expand Down
21 changes: 7 additions & 14 deletions dockerfiles/testsuite/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ services:
- blockchain
volumes:
- data-seed:/app/run/
- ${COMPOSE_HOST_PATH:-.}/genesis.json:/app/misc/genesis.json
- /tmp/.vochain-zkCircuits/:/app/run/dev/zkCircuits/
- /tmp/.vochain-zkCircuits/:/app/run/test/zkCircuits/
- gocoverage-seed:/app/run/gocoverage
environment:
- GOCOVERDIR=/app/run/gocoverage
Expand All @@ -24,8 +23,7 @@ services:
- blockchain
volumes:
- data-miner0:/app/run/
- ${COMPOSE_HOST_PATH:-.}/genesis.json:/app/misc/genesis.json
- /tmp/.vochain-zkCircuits/:/app/run/dev/zkCircuits/
- /tmp/.vochain-zkCircuits/:/app/run/test/zkCircuits/
- gocoverage-miner0:/app/run/gocoverage
environment:
- GOCOVERDIR=/app/run/gocoverage
Expand All @@ -38,8 +36,7 @@ services:
- blockchain
volumes:
- data-miner1:/app/run/
- ${COMPOSE_HOST_PATH:-.}/genesis.json:/app/misc/genesis.json
- /tmp/.vochain-zkCircuits/:/app/run/dev/zkCircuits/
- /tmp/.vochain-zkCircuits/:/app/run/test/zkCircuits/
- gocoverage-miner1:/app/run/gocoverage
environment:
- GOCOVERDIR=/app/run/gocoverage
Expand All @@ -52,8 +49,7 @@ services:
- blockchain
volumes:
- data-miner2:/app/run/
- ${COMPOSE_HOST_PATH:-.}/genesis.json:/app/misc/genesis.json
- /tmp/.vochain-zkCircuits/:/app/run/dev/zkCircuits/
- /tmp/.vochain-zkCircuits/:/app/run/test/zkCircuits/
- gocoverage-miner2:/app/run/gocoverage
environment:
- GOCOVERDIR=/app/run/gocoverage
Expand All @@ -66,8 +62,7 @@ services:
- blockchain
volumes:
- data-miner3:/app/run/
- ${COMPOSE_HOST_PATH:-.}/genesis.json:/app/misc/genesis.json
- /tmp/.vochain-zkCircuits/:/app/run/dev/zkCircuits/
- /tmp/.vochain-zkCircuits/:/app/run/test/zkCircuits/
- gocoverage-miner3:/app/run/gocoverage
environment:
- GOCOVERDIR=/app/run/gocoverage
Expand All @@ -82,8 +77,7 @@ services:
- blockchain
volumes:
- data-gateway0:/app/run/
- ${COMPOSE_HOST_PATH:-.}/genesis.json:/app/misc/genesis.json
- /tmp/.vochain-zkCircuits/:/app/run/dev/zkCircuits/
- /tmp/.vochain-zkCircuits/:/app/run/test/zkCircuits/
- gocoverage-gateway0:/app/run/gocoverage
environment:
- GOCOVERDIR=/app/run/gocoverage
Expand Down Expand Up @@ -163,8 +157,7 @@ services:
- blockchain
volumes:
- data-gatewaySync:/app/run/
- ${COMPOSE_HOST_PATH:-.}/genesis.json:/app/misc/genesis.json
- /tmp/.vochain-zkCircuits/:/app/run/dev/zkCircuits/
- /tmp/.vochain-zkCircuits/:/app/run/test/zkCircuits/
- gocoverage-gatewaySync:/app/run/gocoverage
environment:
- GOCOVERDIR=/app/run/gocoverage
Expand Down
3 changes: 1 addition & 2 deletions dockerfiles/testsuite/env.gateway0
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
VOCDONI_DATADIR=/app/run
VOCDONI_MODE=gateway
VOCDONI_CHAIN=test
VOCDONI_LOGLEVEL=debug
VOCDONI_VOCHAIN_LOGLEVEL=error
VOCDONI_DEV=True
Expand All @@ -8,11 +9,9 @@ VOCDONI_ENABLERPC=True
VOCDONI_ENABLEFAUCETWITHAMOUNT=100000
VOCDONI_VOCHAIN_PUBLICADDR=gateway0:26656
VOCDONI_VOCHAIN_SEEDS=3c3765494e758ae7baccb1f5b0661755302ddc47@seed:26656
VOCDONI_VOCHAIN_GENESIS=/app/misc/genesis.json
VOCDONI_VOCHAIN_NOWAITSYNC=True
VOCDONI_METRICS_ENABLED=True
VOCDONI_METRICS_REFRESHINTERVAL=5
VOCDONI_CHAIN=dev
VOCDONI_SIGNINGKEY=e0f1412b86d6ca9f2b318f1d243ef50be23d315a2e6c1c3035bc72d44c8b2f90 # 0x88a499cEf9D1330111b41360173967c9C1bf703f
VOCDONI_ARCHIVEURL=none
VOCDONI_VOCHAIN_STATESYNCENABLED=false
Expand Down
3 changes: 1 addition & 2 deletions dockerfiles/testsuite/env.gatewaySync
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
VOCDONI_DATADIR=/app/run
VOCDONI_MODE=gateway
VOCDONI_CHAIN=test
VOCDONI_LOGLEVEL=debug
VOCDONI_VOCHAIN_LOGLEVEL=info
VOCDONI_DEV=True
Expand All @@ -8,11 +9,9 @@ VOCDONI_ENABLERPC=True
VOCDONI_ENABLEFAUCETWITHAMOUNT=100000
VOCDONI_VOCHAIN_PUBLICADDR=gatewaySync:26656
VOCDONI_VOCHAIN_SEEDS=3c3765494e758ae7baccb1f5b0661755302ddc47@seed:26656
VOCDONI_VOCHAIN_GENESIS=/app/misc/genesis.json
VOCDONI_VOCHAIN_NOWAITSYNC=True
VOCDONI_METRICS_ENABLED=True
VOCDONI_METRICS_REFRESHINTERVAL=5
VOCDONI_CHAIN=dev
VOCDONI_SIGNINGKEY=f50be23d315a2e6c1c30e0f1412b86d6ca9f2b318f1d243e35bc72d44c8b2f90
VOCDONI_ARCHIVEURL=none
VOCDONI_VOCHAIN_SNAPSHOTINTERVAL=3
2 changes: 1 addition & 1 deletion dockerfiles/testsuite/env.miner0
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
VOCDONI_DATADIR=/app/run
VOCDONI_MODE=miner
VOCDONI_CHAIN=test
VOCDONI_LOGLEVEL=debug
VOCDONI_VOCHAIN_LOGLEVEL=error
VOCDONI_DEV=True
VOCDONI_VOCHAIN_PUBLICADDR=miner0:26656
VOCDONI_VOCHAIN_SEEDS=3c3765494e758ae7baccb1f5b0661755302ddc47@seed:26656
VOCDONI_VOCHAIN_GENESIS=/app/misc/genesis.json
VOCDONI_VOCHAIN_MINERKEY=cda909c34901c137e12bb7d0afbcb9d1c8abc66f03862a42344b1f509d1ae4ce
VOCDONI_METRICS_ENABLED=True
VOCDONI_METRICS_REFRESHINTERVAL=5
Expand Down
2 changes: 1 addition & 1 deletion dockerfiles/testsuite/env.miner1
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
VOCDONI_DATADIR=/app/run
VOCDONI_MODE=miner
VOCDONI_CHAIN=test
VOCDONI_LOGLEVEL=info
VOCDONI_DEV=True
VOCDONI_VOCHAIN_PUBLICADDR=miner1:26656
VOCDONI_VOCHAIN_SEEDS=3c3765494e758ae7baccb1f5b0661755302ddc47@seed:26656
VOCDONI_VOCHAIN_GENESIS=/app/misc/genesis.json
VOCDONI_VOCHAIN_MINERKEY=d52a488fa1511a07778cc94ed9d8130fb255537783ea7c669f38292b4f53ac4f
VOCDONI_METRICS_ENABLED=True
VOCDONI_METRICS_REFRESHINTERVAL=5
Expand Down
2 changes: 1 addition & 1 deletion dockerfiles/testsuite/env.miner2
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
VOCDONI_DATADIR=/app/run
VOCDONI_MODE=miner
VOCDONI_CHAIN=test
VOCDONI_LOGLEVEL=info
VOCDONI_DEV=True
VOCDONI_VOCHAIN_PUBLICADDR=miner2:26656
VOCDONI_VOCHAIN_SEEDS=3c3765494e758ae7baccb1f5b0661755302ddc47@seed:26656
VOCDONI_VOCHAIN_GENESIS=/app/misc/genesis.json
VOCDONI_VOCHAIN_MINERKEY=e06976e5eaf3f147e12763eb140e3d5c2ed16a6fa747d787d8b92ca961fa7dc4
VOCDONI_METRICS_ENABLED=True
VOCDONI_METRICS_REFRESHINTERVAL=5
Expand Down
2 changes: 1 addition & 1 deletion dockerfiles/testsuite/env.miner3
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
VOCDONI_DATADIR=/app/run
VOCDONI_MODE=miner
VOCDONI_CHAIN=test
VOCDONI_LOGLEVEL=info
VOCDONI_DEV=True
VOCDONI_VOCHAIN_PUBLICADDR=miner3:26656
VOCDONI_VOCHAIN_SEEDS=3c3765494e758ae7baccb1f5b0661755302ddc47@seed:26656
VOCDONI_VOCHAIN_GENESIS=/app/misc/genesis.json
VOCDONI_VOCHAIN_MINERKEY=b8d258559adee836a43e964badf541ec106cc68a01f989d3c3c9a030ae3945a6
VOCDONI_METRICS_ENABLED=True
VOCDONI_METRICS_REFRESHINTERVAL=5
Expand Down
2 changes: 1 addition & 1 deletion dockerfiles/testsuite/env.seed
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
VOCDONI_DATADIR=/app/run
VOCDONI_MODE=seed
VOCDONI_CHAIN=test
VOCDONI_LOGLEVEL=debug
VOCDONI_DEV=True
VOCDONI_VOCHAIN_PUBLICADDR=seed:26656
VOCDONI_VOCHAIN_LOGLEVEL=info
VOCDONI_VOCHAIN_GENESIS=/app/misc/genesis.json
VOCDONI_VOCHAIN_NODEKEY=0x2060e20d1f0894d6b23901bce3f20f26107baf0335451ad75ef27b14e4fc56ae050a65ae3883c379b70d811d6e12db2fe1e3a5cf0cae4d03dbbbfebc68601bdd
VOCDONI_METRICS_ENABLED=True
VOCDONI_METRICS_REFRESHINTERVAL=5
Expand Down
Loading

0 comments on commit 2284015

Please sign in to comment.