From 2fbf810cd832922d113a888f0c9ecbfbdafe8916 Mon Sep 17 00:00:00 2001 From: Zarko Milosevic Date: Tue, 18 Sep 2018 11:16:50 +0200 Subject: [PATCH] Delay starting node until Genesis time (#2389) --- CHANGELOG_PENDING.md | 1 + node/node.go | 9 +++++++++ node/node_test.go | 16 ++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index bf4f3348b..c4b776246 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -24,3 +24,4 @@ IMPROVEMENTS: - [metrics] `consensus.block_interval_metrics` is now gauge, not histogram (you will be able to see spikes, if any) BUG FIXES: +- [node] \#2294 Delay starting node until Genesis time diff --git a/node/node.go b/node/node.go index 7f288fe4c..c623e620f 100644 --- a/node/node.go +++ b/node/node.go @@ -7,6 +7,7 @@ import ( "fmt" "net" "net/http" + "time" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -37,6 +38,7 @@ import ( "github.com/tendermint/tendermint/state/txindex/kv" "github.com/tendermint/tendermint/state/txindex/null" "github.com/tendermint/tendermint/types" + tmtime "github.com/tendermint/tendermint/types/time" "github.com/tendermint/tendermint/version" _ "net/http/pprof" @@ -427,6 +429,13 @@ func NewNode(config *cfg.Config, // OnStart starts the Node. It implements cmn.Service. func (n *Node) OnStart() error { + now := tmtime.Now() + genTime := n.genesisDoc.GenesisTime + if genTime.After(now) { + n.Logger.Info("Genesis time is in the future. Sleeping until then...", "genTime", genTime) + time.Sleep(genTime.Sub(now)) + } + err := n.eventBus.Start() if err != nil { return err diff --git a/node/node_test.go b/node/node_test.go index d4e35f735..f4c1f6a16 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -14,6 +14,8 @@ import ( cfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/types" + + tmtime "github.com/tendermint/tendermint/types/time" ) func TestNodeStartStop(t *testing.T) { @@ -75,3 +77,17 @@ func TestSplitAndTrimEmpty(t *testing.T) { assert.Equal(t, tc.expected, splitAndTrimEmpty(tc.s, tc.sep, tc.cutset), "%s", tc.s) } } + +func TestNodeDelayedStop(t *testing.T) { + config := cfg.ResetTestRoot("node_delayed_node_test") + now := tmtime.Now() + + // create & start node + n, err := DefaultNewNode(config, log.TestingLogger()) + n.GenesisDoc().GenesisTime = now.Add(5 * time.Second) + assert.NoError(t, err) + + n.Start() + startTime := tmtime.Now() + assert.Equal(t, true, startTime.After(n.GenesisDoc().GenesisTime)) +}