Skip to content

Commit

Permalink
feat: add types and util
Browse files Browse the repository at this point in the history
  • Loading branch information
Zygimantass committed Dec 21, 2023
1 parent 1659753 commit faf9030
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 0 deletions.
43 changes: 43 additions & 0 deletions types/chain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package types

import (
"github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/skip-mev/petri/provider"
)

type ChainI interface {
GetConfig() ChainConfig
}

type ChainConfig struct {
Denom string
Decimals uint64
NumValidators int
NumNodes int

BinaryName string

Image provider.ImageDefinition
SidecarImage provider.ImageDefinition

GasPrices string
GasAdjustment float64

Bech32Prefix string

EncodingConfig testutil.TestEncodingConfig

HomeDir string
SidecarHomeDir string
SidecarPorts []string
SidecarArgs []string

CoinType string
ChainId string

ModifyGenesis GenesisModifier

NodeCreator NodeCreator
}

type GenesisModifier func([]byte) ([]byte, error)
6 changes: 6 additions & 0 deletions types/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package types

const (
ValidatorKeyName = "validator"
FaucetAccountKeyName = "faucet"
)
12 changes: 12 additions & 0 deletions types/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/skip-mev/petri/types

go 1.21.3

replace (
github.com/skip-mev/petri/provider => ../provider
github.com/skip-mev/petri/util => ../util
)

require github.com/skip-mev/petri/provider v0.0.0-00010101000000-000000000000

require github.com/skip-mev/petri/util v0.0.0-00010101000000-000000000000 // indirect
48 changes: 48 additions & 0 deletions types/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package types

import (
"context"
rpcclient "github.com/cometbft/cometbft/rpc/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/skip-mev/petri/provider"
"google.golang.org/grpc"
)

type NodeConfig struct {
Name string

IsValidator bool

Chain ChainI
Provider provider.Provider
}

type NodeCreator func(context.Context, NodeConfig) (NodeI, error)

type NodeI interface {
GetTMClient(context.Context) (rpcclient.Client, error)
GetGRPCClient(context.Context) (*grpc.ClientConn, error)
Height(context.Context) (uint64, error)

InitHome(context.Context) error

AddGenesisAccount(context.Context, string, []sdk.Coin) error
GenerateGenTx(context.Context, sdk.Coin) error
CopyGenTx(context.Context, NodeI) error
CollectGenTxs(context.Context) error

GenesisFileContent(context.Context) ([]byte, error)
OverwriteGenesisFile(context.Context, []byte) error

CreateWallet(context.Context, string) (WalletI, error)
RecoverKey(context.Context, string, string) error
KeyBech32(context.Context, string, string) (string, error)

SetDefaultConfigs(context.Context) error
SetPersistentPeers(context.Context, string) error

NodeId(context.Context) (string, error)

GetTask() *provider.Task
GetIP(context.Context) (string, error)
}
13 changes: 13 additions & 0 deletions types/wallet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package types

import cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"

type WalletI interface {
FormattedAddress() string
KeyName() string
Mnemonic() string
Address() []byte
FormattedAddressWithPrefix(prefix string) string
PublicKey() (cryptotypes.PubKey, error)
PrivateKey() (cryptotypes.PrivKey, error)
}
15 changes: 15 additions & 0 deletions util/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package util

import "bytes"

// CleanDockerOutput removes trailing bytes from Docker's stdout pipe
func CleanDockerOutput(stdout string) string {
cleanedLine := bytes.TrimFunc([]byte(stdout), func(r rune) bool {
return !(r >= 0x20 && r <= 0x7E)
})

cleanedLine = bytes.TrimSpace(cleanedLine)
cleanedLine = bytes.Trim(cleanedLine, "\n.")

return string(cleanedLine)
}
3 changes: 3 additions & 0 deletions util/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/skip-mev/petri/util

go 1.21.3
28 changes: 28 additions & 0 deletions util/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package util

import (
"context"
"fmt"
"time"
)

func WaitForCondition(timeoutAfter, pollingInterval time.Duration, fn func() (bool, error)) error {
ctx, cancel := context.WithTimeout(context.Background(), timeoutAfter)
defer cancel()

for {
select {
case <-ctx.Done():
return fmt.Errorf("failed waiting for condition after %f seconds", timeoutAfter.Seconds())
case <-time.After(pollingInterval):
reachedCondition, err := fn()
if err != nil {
return fmt.Errorf("error occurred while waiting for condition: %s", err)
}

if reachedCondition {
return nil
}
}
}
}

0 comments on commit faf9030

Please sign in to comment.