Skip to content

Commit

Permalink
Cherry pick gnolang#1972
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Apr 24, 2024
1 parent 9cb751d commit f59f4fa
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 197 deletions.
4 changes: 2 additions & 2 deletions gno.land/cmd/gnoland/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func execStart(c *startCfg, io commands.IO) error {
// Write genesis file if missing.
// NOTE: this will be dropped in a PR that resolves issue #1883:
// https://github.com/gnolang/gno/issues/1883
genesisFilePath := filepath.Join(nodeDir, "../", cfg.Genesis)
genesisFilePath := filepath.Join(nodeDir, "../", "genesis.json")

if !osm.FileExists(genesisFilePath) {
// Create priv validator first.
Expand Down Expand Up @@ -277,7 +277,7 @@ func execStart(c *startCfg, io commands.IO) error {
io.Println(startGraphic)
}

gnoNode, err := node.DefaultNewNode(cfg, logger)
gnoNode, err := node.DefaultNewNode(cfg, genesisFilePath, logger)
if err != nil {
return fmt.Errorf("error in creating node: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions tm2/pkg/bft/blockchain/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func newBlockchainReactor(logger *slog.Logger, genDoc *types.GenesisDoc, privVal
func TestNoBlockResponse(t *testing.T) {
t.Parallel()

config = cfg.ResetTestRoot("blockchain_reactor_test")
config, _ = cfg.ResetTestRoot("blockchain_reactor_test")
defer os.RemoveAll(config.RootDir)
genDoc, privVals := randGenesisDoc(1, false, 30)

Expand Down Expand Up @@ -182,7 +182,7 @@ func TestFlappyBadBlockStopsPeer(t *testing.T) {

testutils.FilterStability(t, testutils.Flappy)

config = cfg.ResetTestRoot("blockchain_reactor_test")
config, _ = cfg.ResetTestRoot("blockchain_reactor_test")
defer os.RemoveAll(config.RootDir)
genDoc, privVals := randGenesisDoc(1, false, 30)

Expand Down
5 changes: 0 additions & 5 deletions tm2/pkg/bft/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,6 @@ func (cfg BaseConfig) ChainID() string {
return cfg.chainID
}

// GenesisFile returns the full path to the genesis.json file
func (cfg BaseConfig) GenesisFile() string {
return filepath.Join(cfg.RootDir, "../", defaultGenesisJSONName)
}

// PrivValidatorKeyFile returns the full path to the priv_validator_key.json file
func (cfg BaseConfig) PrivValidatorKeyFile() string {
return filepath.Join(cfg.RootDir, cfg.PrivValidatorKey)
Expand Down
7 changes: 4 additions & 3 deletions tm2/pkg/bft/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func WriteConfigFile(configFilePath string, config *Config) error {

/****** these are for test settings ***********/

func ResetTestRoot(testName string) *Config {
func ResetTestRoot(testName string) (*Config, string) {
chainID := "test-chain"

// create a unique, concurrency-safe test directory under os.TempDir()
Expand All @@ -81,7 +81,7 @@ func ResetTestRoot(testName string) *Config {

baseConfig := DefaultBaseConfig()
configFilePath := filepath.Join(rootDir, defaultConfigPath)
genesisFilePath := filepath.Join(rootDir, "../", baseConfig.Genesis)
genesisFilePath := filepath.Join(rootDir, defaultGenesisJSONName)
privKeyFilePath := filepath.Join(rootDir, baseConfig.PrivValidatorKey)
privStateFilePath := filepath.Join(rootDir, baseConfig.PrivValidatorState)

Expand All @@ -101,7 +101,8 @@ func ResetTestRoot(testName string) *Config {
osm.MustWriteFile(privStateFilePath, []byte(testPrivValidatorState), 0o644)

config := TestConfig().SetRootDir(rootDir)
return config

return config, genesisFilePath
}

var testGenesisFmt = `{
Expand Down
9 changes: 2 additions & 7 deletions tm2/pkg/bft/config/toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestEnsureTestRoot(t *testing.T) {
testName := "ensureTestRoot"

// create root dir
cfg := ResetTestRoot(testName)
cfg, _ := ResetTestRoot(testName)
defer os.RemoveAll(cfg.RootDir)
rootDir := cfg.RootDir

Expand All @@ -69,15 +69,10 @@ func TestEnsureTestRoot(t *testing.T) {
t,
rootDir,
DefaultDBDir,
baseConfig.Genesis,
baseConfig.PrivValidatorKey,
baseConfig.PrivValidatorState,
)

ensureFiles(
t,
filepath.Join(rootDir, ".."),
baseConfig.Genesis,
)
}

func checkConfig(configFile string) bool {
Expand Down
8 changes: 4 additions & 4 deletions tm2/pkg/bft/consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func ensureDir(dir string, mode os.FileMode) {
}
}

func ResetConfig(name string) *cfg.Config {
func ResetConfig(name string) (*cfg.Config, string) {
return cfg.ResetTestRoot(name)
}

Expand Down Expand Up @@ -261,7 +261,7 @@ func subscribeToVoter(cs *ConsensusState, addr crypto.Address) <-chan events.Eve
// consensus states

func newConsensusState(state sm.State, pv types.PrivValidator, app abci.Application) *ConsensusState {
config := cfg.ResetTestRoot("consensus_state_test")
config, _ := cfg.ResetTestRoot("consensus_state_test")
return newConsensusStateWithConfig(config, state, pv, app)
}

Expand Down Expand Up @@ -578,7 +578,7 @@ func randConsensusNet(nValidators int, testName string, tickerFunc func() Timeou
for i := 0; i < nValidators; i++ {
stateDB := memdb.NewMemDB() // each state needs its own db
state, _ := sm.LoadStateFromDBOrGenesisDoc(stateDB, genDoc)
thisConfig := ResetConfig(fmt.Sprintf("%s_%d", testName, i))
thisConfig, _ := ResetConfig(fmt.Sprintf("%s_%d", testName, i))
configRootDirs = append(configRootDirs, thisConfig.RootDir)
for _, opt := range configOpts {
opt(thisConfig)
Expand Down Expand Up @@ -618,7 +618,7 @@ func randConsensusNetWithPeers(nValidators, nPeers int, testName string, tickerF
for i := 0; i < nPeers; i++ {
stateDB := memdb.NewMemDB() // each state needs its own db
state, _ := sm.LoadStateFromDBOrGenesisDoc(stateDB, genDoc)
thisConfig := ResetConfig(fmt.Sprintf("%s_%d", testName, i))
thisConfig, _ := ResetConfig(fmt.Sprintf("%s_%d", testName, i))
configRootDirs = append(configRootDirs, thisConfig.RootDir)
ensureDir(filepath.Dir(thisConfig.Consensus.WalFile()), 0o700) // dir for wal
if i == 0 {
Expand Down
6 changes: 3 additions & 3 deletions tm2/pkg/bft/consensus/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func assertMempool(txn txNotifier) mempl.Mempool {
func TestMempoolNoProgressUntilTxsAvailable(t *testing.T) {
t.Parallel()

config := ResetConfig("consensus_mempool_no_progress_until_txs_available")
config, _ := ResetConfig("consensus_mempool_no_progress_until_txs_available")
defer os.RemoveAll(config.RootDir)
config.Consensus.CreateEmptyBlocks = false
state, privVals := randGenesisState(1, false, 10)
Expand All @@ -51,7 +51,7 @@ func TestMempoolNoProgressUntilTxsAvailable(t *testing.T) {
}

func TestMempoolProgressAfterCreateEmptyBlocksInterval(t *testing.T) {
config := ResetConfig("consensus_mempool_progress_after_create_empty_blocks_interval")
config, _ := ResetConfig("consensus_mempool_progress_after_create_empty_blocks_interval")
defer os.RemoveAll(config.RootDir)
config.Consensus.CreateEmptyBlocksInterval = ensureTimeout
state, privVals := randGenesisState(1, false, 10)
Expand All @@ -75,7 +75,7 @@ func TestMempoolProgressAfterCreateEmptyBlocksInterval(t *testing.T) {
func TestMempoolProgressInHigherRound(t *testing.T) {
t.Parallel()

config := ResetConfig("consensus_mempool_progress_in_higher_round")
config, _ := ResetConfig("consensus_mempool_progress_in_higher_round")
defer os.RemoveAll(config.RootDir)
config.Consensus.CreateEmptyBlocks = false
state, privVals := randGenesisState(1, false, 10)
Expand Down
78 changes: 0 additions & 78 deletions tm2/pkg/bft/consensus/replay_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,11 @@ import (
"strconv"
"strings"

"github.com/gnolang/gno/tm2/pkg/bft/appconn"
cfg "github.com/gnolang/gno/tm2/pkg/bft/config"
cnscfg "github.com/gnolang/gno/tm2/pkg/bft/consensus/config"
cstypes "github.com/gnolang/gno/tm2/pkg/bft/consensus/types"
"github.com/gnolang/gno/tm2/pkg/bft/mempool/mock"
"github.com/gnolang/gno/tm2/pkg/bft/proxy"
sm "github.com/gnolang/gno/tm2/pkg/bft/state"
"github.com/gnolang/gno/tm2/pkg/bft/store"
walm "github.com/gnolang/gno/tm2/pkg/bft/wal"
dbm "github.com/gnolang/gno/tm2/pkg/db"
"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/events"
"github.com/gnolang/gno/tm2/pkg/log"
osm "github.com/gnolang/gno/tm2/pkg/os"
)

Expand All @@ -33,15 +25,6 @@ const (
// --------------------------------------------------------
// replay messages interactively or all at once

// replay the wal file
func RunReplayFile(config cfg.BaseConfig, csConfig *cnscfg.ConsensusConfig, console bool) {
consensusState := newConsensusStateForReplay(config, csConfig)

if err := consensusState.ReplayFile(csConfig.WalFile(), console); err != nil {
osm.Exit(fmt.Sprintf("Error during consensus replay: %v", err))
}
}

// Replay msgs in file or start the console
func (cs *ConsensusState) ReplayFile(file string, console bool) error {
if cs.IsRunning() {
Expand Down Expand Up @@ -268,64 +251,3 @@ func (pb *playback) replayConsoleLoop() int {
}

// --------------------------------------------------------------------------------

// convenience for replay mode
func newConsensusStateForReplay(config cfg.BaseConfig, csConfig *cnscfg.ConsensusConfig) *ConsensusState {
dbType := dbm.BackendType(config.DBBackend)
// Get BlockStore
blockStoreDB, err := dbm.NewDB("blockstore", dbType, config.DBDir())
if err != nil {
osm.Exit(err.Error())
}

blockStore := store.NewBlockStore(blockStoreDB)

// Get State
stateDB, err := dbm.NewDB("state", dbType, config.DBDir())
if err != nil {
osm.Exit(err.Error())
}

gdoc, err := sm.MakeGenesisDocFromFile(config.GenesisFile())
if err != nil {
osm.Exit(err.Error())
}
state, err := sm.MakeGenesisState(gdoc)
if err != nil {
osm.Exit(err.Error())
}

// Create proxyAppConn connection (consensus, mempool, query)
clientCreator := proxy.DefaultClientCreator(
config.LocalApp,
config.ProxyApp,
config.ABCI,
config.DBDir(),
)
proxyApp := appconn.NewAppConns(clientCreator)
err = proxyApp.Start()
if err != nil {
osm.Exit(fmt.Sprintf("Error starting proxy app conns: %v", err))
}

evsw := events.NewEventSwitch()
if err := evsw.Start(); err != nil {
osm.Exit(fmt.Sprintf("Failed to start event bus: %v", err))
}

handshaker := NewHandshaker(stateDB, state, blockStore, gdoc)
handshaker.SetEventSwitch(evsw)
err = handshaker.Handshake(proxyApp)
if err != nil {
osm.Exit(fmt.Sprintf("Error on handshake: %v", err))
}

mempool := mock.Mempool{}
blockExec := sm.NewBlockExecutor(stateDB, log.NewNoopLogger(), proxyApp.Consensus(), mempool)

consensusState := NewConsensusState(csConfig, state.Copy(), blockExec,
blockStore, mempool)

consensusState.SetEventSwitch(evsw)
return consensusState
}
Loading

0 comments on commit f59f4fa

Please sign in to comment.