@@ -734,18 +734,51 @@ func testnetify(ctx *Context, home string, testnetAppCreator types.AppCreator, d
734734 return nil , err
735735 }
736736
737- // There are times when a user stops their node between commits, resulting in a mismatch between the
738- // blockStore and state. For convenience, we just discard the uncommitted blockStore block and operate on
739- // the lastBlockHeight in state.
740- if blockStore .Height () != state .LastBlockHeight {
737+ ctx .Viper .Set (KeyNewValAddr , validatorAddress )
738+ ctx .Viper .Set (KeyUserPubKey , userPubKey )
739+ testnetApp := testnetAppCreator (ctx .Logger , db , traceWriter , ctx .Viper )
740+
741+ // We need to create a temporary proxyApp to get the initial state of the application.
742+ // Depending on how the node was stopped, the application height can differ from the blockStore height.
743+ // This height difference changes how we go about modifying the state.
744+ clientCreator := proxy .NewLocalClientCreator (testnetApp )
745+ metrics := node .DefaultMetricsProvider (config .Instrumentation )
746+ _ , _ , _ , _ , proxyMetrics := metrics (genDoc .ChainID ) //nolint:dogsled
747+ proxyApp := proxy .NewAppConns (clientCreator , proxyMetrics )
748+ if err := proxyApp .Start (); err != nil {
749+ return nil , fmt .Errorf ("error starting proxy app connections: %v" , err )
750+ }
751+ res , err := proxyApp .Query ().InfoSync (proxy .RequestInfo )
752+ if err != nil {
753+ return nil , fmt .Errorf ("error calling Info: %v" , err )
754+ }
755+ err = proxyApp .Stop ()
756+ if err != nil {
757+ return nil , err
758+ }
759+ appHash := res .LastBlockAppHash
760+ appHeight := res .LastBlockHeight
761+
762+ var block * cmttypes.Block
763+ switch {
764+ case appHeight == blockStore .Height ():
765+ // This state occurs when we stop the node with the halt height flag, and need to handle differently
766+ state .LastBlockHeight ++
767+ block = blockStore .LoadBlock (blockStore .Height ())
768+ block .AppHash = appHash
769+ state .AppHash = appHash
770+ case blockStore .Height () > state .LastBlockHeight :
771+ // This state occurs when we kill the node
741772 err = blockStore .DeleteLatestBlock ()
742773 if err != nil {
743774 return nil , err
744775 }
776+ block = blockStore .LoadBlock (blockStore .Height ())
777+ default :
778+ // If there is any other state, we just load the block
779+ block = blockStore .LoadBlock (blockStore .Height ())
745780 }
746781
747- block := blockStore .LoadBlock (blockStore .Height ())
748-
749782 block .ChainID = newChainID
750783 state .ChainID = newChainID
751784
@@ -853,12 +886,6 @@ func testnetify(ctx *Context, home string, testnetAppCreator types.AppCreator, d
853886 return nil , err
854887 }
855888
856- // testnetAppCreator makes any application side changes that must be made due to the above modifications.
857- // Also, it makes any optional application side changes to make running the testnet easier (voting times, fund accounts, etc).
858- ctx .Viper .Set (KeyNewValAddr , validatorAddress )
859- ctx .Viper .Set (KeyUserPubKey , userPubKey )
860- testnetApp := testnetAppCreator (ctx .Logger , db , traceWriter , ctx .Viper )
861-
862889 return testnetApp , err
863890}
864891
0 commit comments