Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel 2023-12-05 14:01:13 +01:00
parent 94830df5fc
commit 7c9b8b4682
No known key found for this signature in database
10 changed files with 148 additions and 20 deletions

View File

@ -380,6 +380,7 @@ func initAppConfig(clientCtx client.Context) (string, interface{}) {
plmntCfg := planetmintconfig.GetConfig()
plmntCfg.SetRoot(clientCtx.HomeDir)
plmntCfg.SetWalletDir(clientCtx.HomeDir)
customAppConfig := CustomAppConfig{
Config: *srvCfg,

View File

@ -55,6 +55,8 @@ type Config struct {
DistributionAddrPop string `json:"distribution-addr-pop" mapstructure:"distribution-addr-pop"`
DistributionEpochs int `json:"distribution-epochs" mapstructure:"distribution-epochs"`
ReIssuanceEpochs int `json:"re-issuance-epochs" mapstructure:"re-issuance-epochs"`
WalletDir string `json:"wallet-dir" mapstructure:"wallet-dir"`
SelfRpcAddress string `json:"self-rpc-address" mapstructure:"self-rpc-address"`
}
// cosmos-sdk wide global singleton
@ -73,7 +75,8 @@ func DefaultConfig() *Config {
StagedDenom: "stagedcrddl",
ClaimDenom: "crddl",
ConfigRootDir: "",
PopEpochs: 24, // 24 CometBFT epochs of 5s equate 120s
WalletDir: "",
PopEpochs: 3, // 24 CometBFT epochs of 5s equate 120s
RPCHost: "localhost",
RPCPort: 18884,
RPCUser: "user",
@ -85,7 +88,8 @@ func DefaultConfig() *Config {
DistributionAddrDAO: "vjU8eMzU3JbUWZEpVANt2ePJuPWSPixgjiSj2jDMvkVVQQi2DDnZuBRVX4Ygt5YGBf5zvTWCr1ntdqYH",
DistributionAddrPop: "vjTvXCFSReRsZ7grdsAreRR12KuKpDw8idueQJK9Yh1BYS7ggAqgvCxCgwh13KGK6M52y37HUmvr4GdD",
DistributionEpochs: 17640, // CometBFT epochs of 5s equate 1 day (12*60*24) + 15 min (15*24) to wait for confirmations on the re-issuance
ReIssuanceEpochs: 17280, // CometBFT epochs of 5s equate 1 day (12*60*24)
ReIssuanceEpochs: 20, // CometBFT epochs of 5s equate 1 day (12*60*24)
SelfRpcAddress: "",
}
}
@ -102,6 +106,16 @@ func (config *Config) SetRoot(root string) *Config {
return config
}
func (config *Config) SetWalletDir(walletDir string) *Config {
config.WalletDir = walletDir
return config
}
func (config *Config) SetOwnRPCAddress(rpcAddress string) *Config {
config.SelfRpcAddress = rpcAddress
return config
}
// SetWatchmenConfig sets Planetmint's configuration
func (config *Config) SetPlanetmintConfig(planetmintconfig interface{}) {
jsonConfig, err := json.Marshal(planetmintconfig)

View File

@ -38,7 +38,6 @@ func GetConfig() *Config {
libConfig = DefaultConfig()
sdkConfig = sdk.GetConfig()
libConfig.SetBech32PrefixForAccount("plmnt")
encodingConfig := MakeEncodingConfig()
libConfig.SetEncodingConfig(encodingConfig)
})

View File

@ -106,6 +106,7 @@ func getClientContext(address sdk.AccAddress) (clientCtx client.Context, err err
NodeURI: remote,
Offline: true,
Output: &output,
OutputFormat: "json",
SkipConfirm: true,
TxConfig: encodingConfig.TxConfig,
}

View File

@ -41,7 +41,7 @@ func NewE2ETestSuite(cfg network.Config) *E2ETestSuite {
func (s *E2ETestSuite) SetupSuite() {
s.T().Log("setting up e2e test suite")
s.network = network.New(s.T())
s.network = network.New(s.T(), s.T().TempDir())
val := s.network.Validators[0]
kb := val.ClientCtx.Keyring

View File

@ -6,6 +6,7 @@ import (
"fmt"
"os"
"strconv"
"strings"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client/flags"
@ -16,6 +17,7 @@ import (
clitestutil "github.com/planetmint/planetmint-go/testutil/cli"
"github.com/planetmint/planetmint-go/testutil/network"
"github.com/planetmint/planetmint-go/testutil/sample"
"github.com/planetmint/planetmint-go/util"
daocli "github.com/planetmint/planetmint-go/x/dao/client/cli"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
@ -53,6 +55,19 @@ func (s *E2ETestSuite) SetupSuite() {
s.T().Log("setting up e2e test suite")
s.cfg.Mnemonics = []string{sample.Mnemonic}
validatorTmpDir := s.T().TempDir()
conf.SetWalletDir(validatorTmpDir + "/node0/simcli")
conf.SetRoot(validatorTmpDir + "/node0/simd")
//keyringAddress := network.CreateTestingKeyring(conf.ConfigRootDir, conf.WalletDir, sample.Mnemonic)
// set the proper root dir for the test environment so that the abci.go logic works
//conf.ValidatorAddress = keyringAddress
//basicKey, err := sdk.AccAddressFromBech32(keyringAddress)
//_ = authtypes.NewBaseAccount(basicKey, nil, 0, 0)
// set accounts for alice and bob in genesis state
var authGenState authtypes.GenesisState
s.cfg.Codec.MustUnmarshalJSON(s.cfg.GenesisState[authtypes.ModuleName], &authGenState)
@ -83,6 +98,11 @@ func (s *E2ETestSuite) SetupSuite() {
sdk.NewCoin(conf.StakeDenom, math.NewInt(5000)),
)
// vbalances := sdk.NewCoins(
// sdk.NewCoin(conf.TokenDenom, math.NewInt(10000)),
// sdk.NewCoin(conf.StakeDenom, math.NewInt(5000)),
// )
accountBalances := []banktypes.Balance{
{Address: bobAddr.String(), Coins: bbalances.Sort()},
{Address: aliceAddr.String(), Coins: abalances.Sort()},
@ -104,7 +124,13 @@ func (s *E2ETestSuite) SetupSuite() {
s.cfg.GenesisState[daotypes.ModuleName] = s.cfg.Codec.MustMarshalJSON(&daoGenState)
s.cfg.MinGasPrices = fmt.Sprintf("0.000006%s", conf.FeeDenom)
s.network = network.New(s.T(), s.cfg)
s.network = network.New(s.T(), validatorTmpDir, s.cfg)
validatorAddress := util.GetValidatorAddress(conf.ConfigRootDir, conf.WalletDir)
localhostString := strings.Replace(s.network.Validators[0].RPCAddress, "0.0.0.0", "localhost", -1)
conf.SetOwnRPCAddress(localhostString)
conf.ValidatorAddress = validatorAddress
}
// TearDownSuite clean up after testing
@ -262,14 +288,21 @@ func (s *E2ETestSuite) TestReissuance() {
val := s.network.Validators[0]
var err error
for i := 0; i < conf.PopEpochs+10; i++ {
for i := 0; i < conf.PopEpochs+40; i++ {
err = s.network.WaitForNextBlock()
s.Require().NoError(err)
}
var height int64
height, _ = s.network.LatestHeight()
intValue := strconv.FormatInt(height, 10)
_, _ = clitestutil.ExecTestCLICmd(val.ClientCtx, daocli.CmdGetReissuance(), []string{intValue})
for j := 0; int64(j) <= height; j++ {
reissuanceHeight := j
intValueString := strconv.FormatInt(int64(reissuanceHeight), 10)
buffer, err := clitestutil.ExecTestCLICmd(val.ClientCtx, daocli.CmdGetReissuance(), []string{intValueString})
if err != nil {
fmt.Println("error : " + err.Error())
}
fmt.Println(buffer)
}
}
func (s *E2ETestSuite) TestPoPResult() {

View File

@ -35,7 +35,7 @@ func NewE2ETestSuite(cfg network.Config) *E2ETestSuite {
func (s *E2ETestSuite) SetupSuite() {
s.T().Log("setting up e2e test suite")
s.network = network.New(s.T())
s.network = network.New(s.T(), s.T().TempDir())
val := s.network.Validators[0]
kb := val.ClientCtx.Keyring

View File

@ -18,7 +18,6 @@ import (
"github.com/stretchr/testify/require"
"github.com/planetmint/planetmint-go/app"
"github.com/planetmint/planetmint-go/config"
)
type (
@ -28,7 +27,7 @@ type (
// New creates instance with fully configured cosmos network.
// Accepts optional config, that will be used in place of the DefaultConfig() if provided.
func New(t *testing.T, configs ...Config) *Network {
func New(t *testing.T, baseDir string, configs ...Config) *Network {
if len(configs) > 1 {
panic("at most one config should be provided")
}
@ -38,15 +37,10 @@ func New(t *testing.T, configs ...Config) *Network {
} else {
cfg = configs[0]
}
validatorTmpDir := t.TempDir()
// set the proper root dir for the test environment so that the abci.go logic works
appConfig := config.GetConfig()
appConfig.SetRoot(validatorTmpDir + "/node0/simd")
net, err := network.New(t, validatorTmpDir, cfg)
appConfig.ValidatorAddress = net.Validators[0].Address.String()
// keyringAddress := CreateTestingKeyring(baseDir,
// "helmet hedgehog lab actor weekend elbow pelican valid obtain hungry rocket decade tower gallery fit practice cart cherry giggle hair snack glance bulb farm")
// fmt.Println("address : " + keyringAddress)
net, err := network.New(t, baseDir, cfg)
require.NoError(t, err)
_, err = net.WaitForHeight(1)
@ -62,6 +56,7 @@ func DefaultConfig() network.Config {
encoding = app.MakeEncodingConfig()
chainID = "chain-foobarbaz"
)
return network.Config{
Codec: encoding.Marshaler,
TxConfig: encoding.TxConfig,

View File

@ -3,8 +3,11 @@ package util
import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
@ -24,6 +27,85 @@ type KeyFile struct {
PrivKey Key `json:"priv-key"`
}
type KeyringEntry struct {
Name string `json:"name"`
Type string `json:"type"`
Address string `json:"address"`
Pubkey string `json:"pubkey"`
}
func getAddressFromKeyringEntry(jsonString string) (bech32Address string) {
// Slice to hold the parsed data
var keyringEntrys []KeyringEntry
// Unmarshal the JSON string into the slice
err := json.Unmarshal([]byte(jsonString), &keyringEntrys)
if err != nil {
log.Fatalf("Error parsing JSON: %v", err)
}
// Iterate over the parsed data and print the addresses
for _, v := range keyringEntrys {
fmt.Printf("Address: %s\n", v.Address)
bech32Address = v.Address
}
return
}
func GetValidatorAddress(rootDir string, keyringDir string) (bech32Address string) {
// Sanity check
listCmd := "planetmint-god keys list --home " + rootDir + " --keyring-backend test --keyring-dir " + keyringDir + " --output json"
output, err := runCommandWithOutput(listCmd)
if err != nil {
fmt.Println("Error listing keys:", err)
return
}
bech32Address = getAddressFromKeyringEntry(output)
return
}
func CreateTestingKeyring(rootDir string, keyringDir string, mnemonic string) (bech32Address string) {
// Define the variables (replace with actual values)
filePath := rootDir + "/keyring-test/validator.info"
inventoryHostname := "validator"
// Check if file exists
if _, err := os.Stat(filePath); os.IsNotExist(err) {
// File does not exist, recover the key
recoverCmd := fmt.Sprintf("echo '%s' | planetmint-god keys --home %s add %s --recover=true --keyring-dir %s --keyring-backend test", mnemonic, rootDir, inventoryHostname, keyringDir)
output, err := runCommandWithOutput(recoverCmd)
if err != nil {
fmt.Println("Error recovering key:", err)
fmt.Println(" output : " + output)
return
}
fmt.Println(" output : " + output)
}
// Sanity check
listCmd := "planetmint-god keys list --home " + rootDir + " --keyring-backend test --keyring-dir " + keyringDir + " --output json"
output, err := runCommandWithOutput(listCmd)
if err != nil {
fmt.Println("Error listing keys:", err)
return
}
bech32Address = getAddressFromKeyringEntry(output)
return
}
func runCommand(cmdString string) error {
cmd := exec.Command("bash", "-c", cmdString)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func runCommandWithOutput(cmdString string) (string, error) {
cmd := exec.Command("bash", "-c", cmdString)
outputBytes, err := cmd.Output()
return string(outputBytes), err
}
func GetValidatorCometBFTIdentity(ctx sdk.Context) (string, bool) {
conf := config.GetConfig()

View File

@ -12,9 +12,12 @@ import (
)
func setRPCConfig(goCtx context.Context) {
appConfig := config.GetConfig()
rpcConf := lib.GetConfig()
rpcConf.RootDir = appConfig.WalletDir
ctx := sdk.UnwrapSDKContext(goCtx)
rpcConf.SetChainID(ctx.ChainID())
rpcConf.SetRPCEndpoint(appConfig.SelfRpcAddress)
}
func buildSignBroadcastTx(goCtx context.Context, loggingContext string, sendingValidatorAddress string, msg sdk.Msg) {
go func() {