|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "log/slog" |
| 5 | + "path/filepath" |
| 6 | + "slices" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/gnolang/gno/gno.land/pkg/gnoland" |
| 10 | + "github.com/gnolang/gno/gno.land/pkg/gnoland/ugnot" |
| 11 | + abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types" |
| 12 | + tmcfg "github.com/gnolang/gno/tm2/pkg/bft/config" |
| 13 | + "github.com/gnolang/gno/tm2/pkg/bft/node" |
| 14 | + bft "github.com/gnolang/gno/tm2/pkg/bft/types" |
| 15 | + "github.com/gnolang/gno/tm2/pkg/crypto" |
| 16 | + "github.com/gnolang/gno/tm2/pkg/db/memdb" |
| 17 | + "github.com/gnolang/gno/tm2/pkg/std" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + DefaultAccount_Name = "test1" |
| 23 | + DefaultAccount_Address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" |
| 24 | + DefaultAccount_Seed = "source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast" |
| 25 | +) |
| 26 | + |
| 27 | +// TestingInMemoryNode initializes and starts an in-memory node for testing. |
| 28 | +// It returns the node instance and its RPC remote address. |
| 29 | +func TestingInMemoryNode(t TestingTS, logger *slog.Logger, config *gnoland.InMemoryNodeConfig) (*node.Node, string) { |
| 30 | + node, err := gnoland.NewInMemoryNode(logger, config) |
| 31 | + require.NoError(t, err) |
| 32 | + |
| 33 | + err = node.Start() |
| 34 | + require.NoError(t, err) |
| 35 | + |
| 36 | + ourAddress := config.PrivValidator.GetPubKey().Address() |
| 37 | + isValidator := slices.ContainsFunc(config.Genesis.Validators, func(val bft.GenesisValidator) bool { |
| 38 | + return val.Address == ourAddress |
| 39 | + }) |
| 40 | + |
| 41 | + // Wait for first block if we are a validator. |
| 42 | + // If we are not a validator, we don't produce blocks, so node.Ready() hangs. |
| 43 | + if isValidator { |
| 44 | + select { |
| 45 | + case <-node.Ready(): |
| 46 | + case <-time.After(time.Second * 10): |
| 47 | + require.FailNow(t, "timeout while waiting for the node to start") |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return node, node.Config().RPC.ListenAddress |
| 52 | +} |
| 53 | + |
| 54 | +// TestingNodeConfig constructs an in-memory node configuration |
| 55 | +// with default packages and genesis transactions already loaded. |
| 56 | +// It will return the default creator address of the loaded packages. |
| 57 | +func TestingNodeConfig(t TestingTS, gnoroot string, additionalTxs ...gnoland.TxWithMetadata) (*gnoland.InMemoryNodeConfig, bft.Address) { |
| 58 | + cfg := TestingMinimalNodeConfig(gnoroot) |
| 59 | + cfg.SkipGenesisVerification = true |
| 60 | + |
| 61 | + creator := crypto.MustAddressFromString(DefaultAccount_Address) // test1 |
| 62 | + |
| 63 | + params := LoadDefaultGenesisParamFile(t, gnoroot) |
| 64 | + balances := LoadDefaultGenesisBalanceFile(t, gnoroot) |
| 65 | + txs := make([]gnoland.TxWithMetadata, 0) |
| 66 | + txs = append(txs, LoadDefaultPackages(t, creator, gnoroot)...) |
| 67 | + txs = append(txs, additionalTxs...) |
| 68 | + |
| 69 | + cfg.Genesis.AppState = gnoland.GnoGenesisState{ |
| 70 | + Balances: balances, |
| 71 | + Txs: txs, |
| 72 | + Params: params, |
| 73 | + } |
| 74 | + |
| 75 | + return cfg, creator |
| 76 | +} |
| 77 | + |
| 78 | +// TestingMinimalNodeConfig constructs the default minimal in-memory node configuration for testing. |
| 79 | +func TestingMinimalNodeConfig(gnoroot string) *gnoland.InMemoryNodeConfig { |
| 80 | + tmconfig := DefaultTestingTMConfig(gnoroot) |
| 81 | + |
| 82 | + // Create Mocked Identity |
| 83 | + pv := gnoland.NewMockedPrivValidator() |
| 84 | + |
| 85 | + // Generate genesis config |
| 86 | + genesis := DefaultTestingGenesisConfig(gnoroot, pv.GetPubKey(), tmconfig) |
| 87 | + |
| 88 | + return &gnoland.InMemoryNodeConfig{ |
| 89 | + PrivValidator: pv, |
| 90 | + Genesis: genesis, |
| 91 | + TMConfig: tmconfig, |
| 92 | + DB: memdb.NewMemDB(), |
| 93 | + InitChainerConfig: gnoland.InitChainerConfig{ |
| 94 | + GenesisTxResultHandler: gnoland.PanicOnFailingTxResultHandler, |
| 95 | + CacheStdlibLoad: true, |
| 96 | + }, |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func DefaultTestingGenesisConfig(gnoroot string, self crypto.PubKey, tmconfig *tmcfg.Config) *bft.GenesisDoc { |
| 101 | + return &bft.GenesisDoc{ |
| 102 | + GenesisTime: time.Now(), |
| 103 | + ChainID: tmconfig.ChainID(), |
| 104 | + ConsensusParams: abci.ConsensusParams{ |
| 105 | + Block: &abci.BlockParams{ |
| 106 | + MaxTxBytes: 1_000_000, // 1MB, |
| 107 | + MaxDataBytes: 2_000_000, // 2MB, |
| 108 | + MaxGas: 3_000_000_000, // 3B gas |
| 109 | + TimeIotaMS: 100, // 100ms |
| 110 | + }, |
| 111 | + }, |
| 112 | + Validators: []bft.GenesisValidator{ |
| 113 | + { |
| 114 | + Address: self.Address(), |
| 115 | + PubKey: self, |
| 116 | + Power: 10, |
| 117 | + Name: "self", |
| 118 | + }, |
| 119 | + }, |
| 120 | + AppState: gnoland.GnoGenesisState{ |
| 121 | + Balances: []gnoland.Balance{ |
| 122 | + { |
| 123 | + Address: crypto.MustAddressFromString(DefaultAccount_Address), |
| 124 | + Amount: std.MustParseCoins(ugnot.ValueString(10_000_000_000_000)), |
| 125 | + }, |
| 126 | + }, |
| 127 | + Txs: []gnoland.TxWithMetadata{}, |
| 128 | + Params: []gnoland.Param{}, |
| 129 | + }, |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// LoadDefaultPackages loads the default packages for testing using a given creator address and gnoroot directory. |
| 134 | +func LoadDefaultPackages(t TestingTS, creator bft.Address, gnoroot string) []gnoland.TxWithMetadata { |
| 135 | + examplesDir := filepath.Join(gnoroot, "examples") |
| 136 | + |
| 137 | + defaultFee := std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000))) |
| 138 | + txs, err := gnoland.LoadPackagesFromDir(examplesDir, creator, defaultFee) |
| 139 | + require.NoError(t, err) |
| 140 | + |
| 141 | + return txs |
| 142 | +} |
| 143 | + |
| 144 | +// LoadDefaultGenesisBalanceFile loads the default genesis balance file for testing. |
| 145 | +func LoadDefaultGenesisBalanceFile(t TestingTS, gnoroot string) []gnoland.Balance { |
| 146 | + balanceFile := filepath.Join(gnoroot, "gno.land", "genesis", "genesis_balances.txt") |
| 147 | + |
| 148 | + genesisBalances, err := gnoland.LoadGenesisBalancesFile(balanceFile) |
| 149 | + require.NoError(t, err) |
| 150 | + |
| 151 | + return genesisBalances.List() |
| 152 | +} |
| 153 | + |
| 154 | +// LoadDefaultGenesisParamFile loads the default genesis balance file for testing. |
| 155 | +func LoadDefaultGenesisParamFile(t TestingTS, gnoroot string) []gnoland.Param { |
| 156 | + paramFile := filepath.Join(gnoroot, "gno.land", "genesis", "genesis_params.toml") |
| 157 | + |
| 158 | + genesisParams, err := gnoland.LoadGenesisParamsFile(paramFile) |
| 159 | + require.NoError(t, err) |
| 160 | + |
| 161 | + return genesisParams |
| 162 | +} |
| 163 | + |
| 164 | +// LoadDefaultGenesisTXsFile loads the default genesis transactions file for testing. |
| 165 | +func LoadDefaultGenesisTXsFile(t TestingTS, chainid string, gnoroot string) []gnoland.TxWithMetadata { |
| 166 | + txsFile := filepath.Join(gnoroot, "gno.land", "genesis", "genesis_txs.jsonl") |
| 167 | + |
| 168 | + // NOTE: We dont care about giving a correct address here, as it's only for display |
| 169 | + // XXX: Do we care loading this TXs for testing ? |
| 170 | + genesisTXs, err := gnoland.LoadGenesisTxsFile(txsFile, chainid, "https://127.0.0.1:26657") |
| 171 | + require.NoError(t, err) |
| 172 | + |
| 173 | + return genesisTXs |
| 174 | +} |
| 175 | + |
| 176 | +// DefaultTestingTMConfig constructs the default Tendermint configuration for testing. |
| 177 | +func DefaultTestingTMConfig(gnoroot string) *tmcfg.Config { |
| 178 | + const defaultListner = "tcp://127.0.0.1:0" |
| 179 | + |
| 180 | + tmconfig := tmcfg.TestConfig().SetRootDir(gnoroot) |
| 181 | + tmconfig.Consensus.WALDisabled = true |
| 182 | + tmconfig.Consensus.SkipTimeoutCommit = true |
| 183 | + tmconfig.Consensus.CreateEmptyBlocks = true |
| 184 | + tmconfig.Consensus.CreateEmptyBlocksInterval = time.Millisecond * 100 |
| 185 | + tmconfig.RPC.ListenAddress = defaultListner |
| 186 | + tmconfig.P2P.ListenAddress = defaultListner |
| 187 | + return tmconfig |
| 188 | +} |
0 commit comments