From 6fc2b8fa66940959f8ea1c544c5ef8fa73275e2a Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Tue, 9 Jan 2024 14:21:26 +0100 Subject: [PATCH] vochain: add StateSync* and SnapshotInterval flags --- cmd/node/main.go | 10 ++++++++++ config/config.go | 13 +++++++++++++ vochain/app.go | 10 ++++++++-- vochain/start.go | 4 ++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/cmd/node/main.go b/cmd/node/main.go index bde17c199..745d2ee1a 100644 --- a/cmd/node/main.go +++ b/cmd/node/main.go @@ -188,6 +188,16 @@ func loadConfig() *config.Config { "do not wait for Vochain to synchronize (for testing only)") flag.Int("vochainMempoolSize", 20000, "vochain mempool size") + flag.Int("vochainSnapshotInterval", 0, + "create state snapshot every N blocks (0 to disable)") + flag.Bool("vochainStateSyncEnabled", false, + "during startup, let cometBFT ask peers for available snapshots and use them to bootstrap the state") + flag.StringSlice("vochainStateSyncRPCServers", []string{}, + "list of RPC servers to fetch snapshots from (minimum 2)") + flag.String("vochainStateSyncTrustHash", "", + "hash of the trusted block") + flag.Int64("vochainStateSyncTrustHeight", 0, + "height of the trusted block") flag.Int("vochainMinerTargetBlockTimeSeconds", 10, "vochain consensus block time target (in seconds)") diff --git a/config/config.go b/config/config.go index 957e531ff..6150b5484 100644 --- a/config/config.go +++ b/config/config.go @@ -125,6 +125,19 @@ type VochainCfg struct { IsSeedNode bool // OffChainDataDownload specifies if the node is configured to download off-chain data OffChainDataDownload bool + // SnapshotInterval enables creating a state snapshot every N blocks (0 to disable) + SnapshotInterval int + // StateSyncEnabled allows cometBFT during startup, to ask peers for available snapshots + // and use them to bootstrap the state + StateSyncEnabled bool + // StateSyncRPCServers to fetch the snapshots from (minimum 2 needed) + StateSyncRPCServers []string + // StateSyncTrustHeight and TrustHash must both be provided to force the trusting of a + // particular header. + StateSyncTrustHeight int64 + // StateSyncTrustHeight and TrustHash must both be provided to force the trusting of a + // particular header. + StateSyncTrustHash string } // IndexerCfg handles the configuration options of the indexer diff --git a/vochain/app.go b/vochain/app.go index 9f1d6cc22..fa115c5b6 100644 --- a/vochain/app.go +++ b/vochain/app.go @@ -76,6 +76,9 @@ type BaseApplication struct { // blockTime is the target block time that miners use blockTime time.Duration + // snapshotInterval create state snapshot every N blocks (0 to disable) + snapshotInterval int + // endBlockTimestamp is the last block end timestamp calculated from local time. endBlockTimestamp atomic.Int64 // startBlockTimestamp is the current block timestamp from tendermint's @@ -154,6 +157,7 @@ func NewBaseApplication(vochainCfg *config.VochainCfg) (*BaseApplication, error) TransactionHandler: transactionHandler, blockCache: blockCache, dataDir: vochainCfg.DataDir, + snapshotInterval: vochainCfg.SnapshotInterval, genesisInfo: &comettypes.GenesisDoc{}, }, nil } @@ -205,8 +209,10 @@ func (app *BaseApplication) CommitState() ([]byte, error) { if err != nil { return nil, fmt.Errorf("cannot save state: %w", err) } - // perform state snapshot (DISABLED) - if false && app.Height()%50000 == 0 && !app.IsSynchronizing() { // DISABLED + // perform state snapshot + if app.snapshotInterval > 0 && + app.Height()%uint32(app.snapshotInterval) == 0 && + !app.IsSynchronizing() { startTime := time.Now() log.Infof("performing a state snapshot on block %d", app.Height()) if _, err := app.State.Snapshot(); err != nil { diff --git a/vochain/start.go b/vochain/start.go index 6d9107fb7..8f52bd489 100644 --- a/vochain/start.go +++ b/vochain/start.go @@ -184,6 +184,10 @@ func newTendermint(app *BaseApplication, tconfig.Mempool.MaxTxsBytes = int64(tconfig.Mempool.Size * tconfig.Mempool.MaxTxBytes) tconfig.Mempool.CacheSize = 100000 tconfig.Mempool.Broadcast = true + tconfig.StateSync.Enable = localConfig.StateSyncEnabled + tconfig.StateSync.RPCServers = localConfig.StateSyncRPCServers + tconfig.StateSync.TrustHeight = localConfig.StateSyncTrustHeight + tconfig.StateSync.TrustHash = localConfig.StateSyncTrustHash log.Debugf("mempool config: %+v", tconfig.Mempool) // tmdbBackend defaults to goleveldb, but switches to cleveldb if