diff --git a/.gitignore b/.gitignore index 52e6b48..e113b90 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea **/*.pkr.hcl +go.work* \ No newline at end of file diff --git a/core/provider/docker/docker_provider.go b/core/provider/docker/docker_provider.go index 32c1a77..b01a33e 100644 --- a/core/provider/docker/docker_provider.go +++ b/core/provider/docker/docker_provider.go @@ -24,7 +24,7 @@ type Provider struct { name string dockerNetworkID string dockerNetworkName string - networkMu sync.RWMutex + networkMu sync.Mutex listeners map[string]Listeners builderImageName string } diff --git a/core/provider/docker/task.go b/core/provider/docker/task.go index 8437db3..7685536 100644 --- a/core/provider/docker/task.go +++ b/core/provider/docker/task.go @@ -93,8 +93,11 @@ func (p *Provider) CreateTask(ctx context.Context, logger *zap.Logger, definitio listeners.CloseAll() return "", err } - + + // network map is volatile, so we need to mutex update it + p.networkMu.Lock() p.listeners[createdContainer.ID] = listeners + p.networkMu.Unlock() return createdContainer.ID, nil } @@ -118,8 +121,8 @@ func (p *Provider) pullImage(ctx context.Context, imageName string) error { func (p *Provider) StartTask(ctx context.Context, id string) error { p.logger.Info("starting task", zap.String("id", id)) - p.networkMu.RLock() - defer p.networkMu.RUnlock() + p.networkMu.Lock() + defer p.networkMu.Unlock() if _, ok := p.listeners[id]; !ok { return fmt.Errorf("task port listeners %s not found", id) diff --git a/core/types/chain.go b/core/types/chain.go index 66f7ad1..785015b 100644 --- a/core/types/chain.go +++ b/core/types/chain.go @@ -3,6 +3,7 @@ package types import ( "context" "fmt" + "math/big" rpchttp "github.com/cometbft/cometbft/rpc/client/http" "github.com/cosmos/cosmos-sdk/client" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -62,7 +63,6 @@ type ChainConfig struct { SidecarArgs []string // SidecarArgs are the arguments to launch the chain sidecar CoinType string // CoinType is the coin type of the chain (e.g. 118) - HDPath string // HDPath is the HD path of the chain (e.g. m/44'/118'/0'/0/0) ChainId string // ChainId is the chain ID of the chain ModifyGenesis GenesisModifier // ModifyGenesis is a function that modifies the genesis bytes of the chain @@ -73,6 +73,25 @@ type ChainConfig struct { NodeCreator NodeCreator // NodeCreator is a function that creates a node NodeDefinitionModifier NodeDefinitionModifier // NodeDefinitionModifier is a function that modifies a node's definition + // number of tokens to allocate per account in the genesis state (unscaled). This value defaults to 10_000_000 if not set. + // if not set. + GenesisDelegation *big.Int + // number of tokens to allocate to the genesis account. This value defaults to 5_000_000 if not set. + GenesisBalance *big.Int +} + +func (c ChainConfig) GetGenesisBalance() *big.Int { + if c.GenesisBalance == nil { + return big.NewInt(10_000_000) + } + return c.GenesisBalance +} + +func (c ChainConfig) GetGenesisDelegation() *big.Int { + if c.GenesisDelegation == nil { + return big.NewInt(5_000_000) + } + return c.GenesisDelegation } func (c *ChainConfig) ValidateBasic() error { @@ -118,10 +137,6 @@ func (c *ChainConfig) ValidateBasic() error { return fmt.Errorf("coin type cannot be empty") } - if c.HDPath == "" { - return fmt.Errorf("HD path cannot be empty") - } - if c.ChainId == "" { return fmt.Errorf("chain ID cannot be empty") } diff --git a/cosmos/chain/chain.go b/cosmos/chain/chain.go index 262827f..05f984a 100644 --- a/cosmos/chain/chain.go +++ b/cosmos/chain/chain.go @@ -159,14 +159,16 @@ func (c *Chain) Init(ctx context.Context) error { decimalPow := int64(math.Pow10(int(c.Config.Decimals))) genesisCoin := types.Coin{ - Amount: sdkmath.NewInt(10_000_000).MulRaw(decimalPow), + Amount: sdkmath.NewIntFromBigInt(c.GetConfig().GetGenesisBalance()).MulRaw(decimalPow), Denom: c.Config.Denom, } + c.logger.Info("creating genesis accounts", zap.String("coin", genesisCoin.String())) genesisSelfDelegation := types.Coin{ - Amount: sdkmath.NewInt(5_000_000).MulRaw(decimalPow), + Amount: sdkmath.NewIntFromBigInt(c.GetConfig().GetGenesisDelegation()).MulRaw(decimalPow), Denom: c.Config.Denom, } + c.logger.Info("creating genesis self-delegations", zap.String("coin", genesisSelfDelegation.String())) genesisAmounts := []types.Coin{genesisCoin} @@ -425,26 +427,19 @@ func (c *Chain) WaitForBlocks(ctx context.Context, delta uint64) error { // WaitForHeight blocks until the chain reaches block height desiredHeight func (c *Chain) WaitForHeight(ctx context.Context, desiredHeight uint64) error { c.logger.Info("waiting for height", zap.Uint64("desired_height", desiredHeight)) - - height, err := c.Height(ctx) - - if err != nil { - return err - } - for { - c.logger.Debug("waiting for height", zap.Uint64("desired_height", desiredHeight), zap.Uint64("current_height", height)) - - if height >= desiredHeight { - break - } - - height, err = c.Height(ctx) + c.logger.Debug("waiting for height", zap.Uint64("desired_height", desiredHeight)) + + height, err := c.Height(ctx) if err != nil { time.Sleep(2 * time.Second) continue } + if height >= desiredHeight { + break + } + // We assume the chain will eventually return a non-zero height, otherwise // this may block indefinitely. if height == 0 { diff --git a/cosmos/cosmosutil/wallet.go b/cosmos/cosmosutil/wallet.go index 85822d8..b793e7b 100644 --- a/cosmos/cosmosutil/wallet.go +++ b/cosmos/cosmosutil/wallet.go @@ -3,6 +3,9 @@ package cosmosutil import ( "context" "fmt" + "strings" + "time" + "github.com/cosmos/cosmos-sdk/client" clienttx "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/codec" @@ -15,8 +18,6 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" petritypes "github.com/skip-mev/petri/core/v2/types" "github.com/skip-mev/petri/core/v2/util" - "strings" - "time" ) // EncodingConfig is a struct that packs all the necessary encoding information @@ -63,7 +64,6 @@ func (w *InteractingWallet) CreateSignedTx(ctx context.Context, gas int64, fees } acc, err := w.Account(ctx) - if err != nil { return nil, err } diff --git a/cosmos/loadtest/client_factory.go b/cosmos/loadtest/client_factory.go index 08d4ecc..8d871b2 100644 --- a/cosmos/loadtest/client_factory.go +++ b/cosmos/loadtest/client_factory.go @@ -2,6 +2,7 @@ package loadtest import ( "context" + "fmt" "github.com/cometbft/cometbft/test/loadtime/payload" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" @@ -67,6 +68,9 @@ func (f *DefaultClientFactory) NewClient(cfg loadtest.Config) (loadtest.Client, GasDenom: f.chain.GetConfig().Denom, PricePerGas: 0, // todo(Zygimantass): get gas settings }, true) + if err != nil { + return nil, fmt.Errorf("error seeding account %s from seeder %s: %v", interactingLoaderWallet.FormattedAddress(), f.seeder.FormattedAddress(), err) + } msgs, gasSettings, err := f.msgGenerator(interactingLoaderWallet.Address()) @@ -77,7 +81,7 @@ func (f *DefaultClientFactory) NewClient(cfg loadtest.Config) (loadtest.Client, acc, err := interactingLoaderWallet.Account(context.Background()) if err != nil { - return nil, err + return nil, fmt.Errorf("error in client initialization: sender account was not created %s", interactingLoaderWallet.FormattedAddress()) } return NewDefaultClient(interactingLoaderWallet, f.chainClient, msgs, acc.GetSequence(), acc.GetAccountNumber(), gasSettings, &payload.Payload{