From fff0c6cd8eaf902e4f4099a3d06bc95d6c81564e Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 21 Feb 2018 19:36:57 +0100 Subject: [PATCH] Add app_state from genesis file in InitChain message --- consensus/replay.go | 24 +++++++++++++++++++----- consensus/replay_file.go | 9 +++++++-- consensus/replay_test.go | 2 +- consensus/wal_generator.go | 2 +- node/node.go | 2 +- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/consensus/replay.go b/consensus/replay.go index 2e6b36a22..39dd592a4 100644 --- a/consensus/replay.go +++ b/consensus/replay.go @@ -2,6 +2,7 @@ package consensus import ( "bytes" + "encoding/json" "fmt" "hash/crc32" "io" @@ -190,13 +191,23 @@ type Handshaker struct { stateDB dbm.DB initialState sm.State store types.BlockStore + appState json.RawMessage logger log.Logger nBlocks int // number of blocks applied to the state } -func NewHandshaker(stateDB dbm.DB, state sm.State, store types.BlockStore) *Handshaker { - return &Handshaker{stateDB, state, store, log.NewNopLogger(), 0} +func NewHandshaker(stateDB dbm.DB, state sm.State, + store types.BlockStore, appState json.RawMessage) *Handshaker { + + return &Handshaker{ + stateDB: stateDB, + initialState: state, + store: store, + appState: appState, + logger: log.NewNopLogger(), + nBlocks: 0, + } } func (h *Handshaker) SetLogger(l log.Logger) { @@ -249,9 +260,12 @@ func (h *Handshaker) ReplayBlocks(state sm.State, appHash []byte, appBlockHeight // If appBlockHeight == 0 it means that we are at genesis and hence should send InitChain if appBlockHeight == 0 { validators := types.TM2PB.Validators(state.Validators) - // TODO: get the genesis bytes (https://github.com/tendermint/tendermint/issues/1224) - var genesisBytes []byte - if _, err := proxyApp.Consensus().InitChainSync(abci.RequestInitChain{validators, genesisBytes}); err != nil { + req := abci.RequestInitChain{ + Validators: validators, + AppStateBytes: h.appState, + } + _, err := proxyApp.Consensus().InitChainSync(req) + if err != nil { return nil, err } } diff --git a/consensus/replay_file.go b/consensus/replay_file.go index 979425f87..d47e90527 100644 --- a/consensus/replay_file.go +++ b/consensus/replay_file.go @@ -287,14 +287,19 @@ func newConsensusStateForReplay(config cfg.BaseConfig, csConfig *cfg.ConsensusCo // Get State stateDB := dbm.NewDB("state", dbType, config.DBDir()) - state, err := sm.MakeGenesisStateFromFile(config.GenesisFile()) + gdoc, err := sm.MakeGenesisDocFromFile(config.GenesisFile()) + if err != nil { + cmn.Exit(err.Error()) + } + state, err := sm.MakeGenesisState(gdoc) if err != nil { cmn.Exit(err.Error()) } // Create proxyAppConn connection (consensus, mempool, query) clientCreator := proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()) - proxyApp := proxy.NewAppConns(clientCreator, NewHandshaker(stateDB, state, blockStore)) + proxyApp := proxy.NewAppConns(clientCreator, + NewHandshaker(stateDB, state, blockStore, gdoc.AppState)) err = proxyApp.Start() if err != nil { cmn.Exit(cmn.Fmt("Error starting proxy app conns: %v", err)) diff --git a/consensus/replay_test.go b/consensus/replay_test.go index 9bcddeaee..9d7bbc8e4 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -362,7 +362,7 @@ func testHandshakeReplay(t *testing.T, nBlocks int, mode uint) { } // now start the app using the handshake - it should sync - handshaker := NewHandshaker(stateDB, state, store) + handshaker := NewHandshaker(stateDB, state, store, nil) proxyApp := proxy.NewAppConns(clientCreator2, handshaker) if err := proxyApp.Start(); err != nil { t.Fatalf("Error starting proxy app connections: %v", err) diff --git a/consensus/wal_generator.go b/consensus/wal_generator.go index dee8b5143..14f82d8a8 100644 --- a/consensus/wal_generator.go +++ b/consensus/wal_generator.go @@ -52,7 +52,7 @@ func WALWithNBlocks(numBlocks int) (data []byte, err error) { return nil, errors.Wrap(err, "failed to make genesis state") } blockStore := bc.NewBlockStore(blockStoreDB) - handshaker := NewHandshaker(stateDB, state, blockStore) + handshaker := NewHandshaker(stateDB, state, blockStore, genDoc.AppState) proxyApp := proxy.NewAppConns(proxy.NewLocalClientCreator(app), handshaker) proxyApp.SetLogger(logger.With("module", "proxy")) if err := proxyApp.Start(); err != nil { diff --git a/node/node.go b/node/node.go index 710a81dc4..9c78360af 100644 --- a/node/node.go +++ b/node/node.go @@ -162,7 +162,7 @@ func NewNode(config *cfg.Config, // and sync tendermint and the app by performing a handshake // and replaying any necessary blocks consensusLogger := logger.With("module", "consensus") - handshaker := cs.NewHandshaker(stateDB, state, blockStore) + handshaker := cs.NewHandshaker(stateDB, state, blockStore, genDoc.AppState) handshaker.SetLogger(consensusLogger) proxyApp := proxy.NewAppConns(clientCreator, handshaker) proxyApp.SetLogger(logger.With("module", "proxy"))