Skip to content

Commit

Permalink
chore(doc): add godocs everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zygimantass committed Jan 30, 2024
1 parent 26d4533 commit 4426cbc
Show file tree
Hide file tree
Showing 35 changed files with 1,071 additions and 108 deletions.
39 changes: 39 additions & 0 deletions cosmos/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"time"
)

// Chain is a logical representation of a Cosmos-based blockchain

type Chain struct {
Config petritypes.ChainConfig

Expand All @@ -33,6 +35,8 @@ type Chain struct {

var _ petritypes.ChainI = &Chain{}

// CreateChain creates the Chain object and initializes the node tasks, their backing compute and the validator wallets

func CreateChain(ctx context.Context, logger *zap.Logger, infraProvider provider.Provider, config petritypes.ChainConfig) (*Chain, error) {
var chain Chain

Expand Down Expand Up @@ -96,10 +100,14 @@ func CreateChain(ctx context.Context, logger *zap.Logger, infraProvider provider
return &chain, nil
}

// GetConfig returns the Chain's configuration

func (c *Chain) GetConfig() petritypes.ChainConfig {
return c.Config
}

// Height returns the chain's height from the first available full node in the network

func (c *Chain) Height(ctx context.Context) (uint64, error) {
node := c.GetFullNode()

Expand All @@ -120,6 +128,9 @@ func (c *Chain) Height(ctx context.Context) (uint64, error) {
return uint64(status.SyncInfo.LatestBlockHeight), nil
}

// Init initializes the chain. That consists of generating the genesis transactions, genesis file, wallets,
// the distribution of configuration files and starting the network nodes up

func (c *Chain) Init(ctx context.Context) error {
decimalPow := int64(math.Pow10(int(c.Config.Decimals)))

Expand Down Expand Up @@ -288,6 +299,8 @@ func (c *Chain) Init(ctx context.Context) error {
return nil
}

// Teardown destroys all resources related to a chain and its' nodes

func (c *Chain) Teardown(ctx context.Context) error {
c.logger.Info("tearing down chain", zap.String("name", c.Config.ChainId))

Expand All @@ -306,6 +319,9 @@ func (c *Chain) Teardown(ctx context.Context) error {
return nil
}

// PeerStrings returns a comma-delimited string with the addresses of chain nodes in
// the format of nodeid@host:port

func (c *Chain) PeerStrings(ctx context.Context) (string, error) {
peerStrings := []string{}

Expand All @@ -328,14 +344,20 @@ func (c *Chain) PeerStrings(ctx context.Context) (string, error) {
return strings.Join(peerStrings, ","), nil
}

// GetGRPCClient returns a gRPC client of the first available node

func (c *Chain) GetGRPCClient(ctx context.Context) (*grpc.ClientConn, error) {
return c.GetFullNode().GetGRPCClient(ctx)
}

// GetTMClient returns a CometBFT client of the first available node

func (c *Chain) GetTMClient(ctx context.Context) (*rpchttp.HTTP, error) {
return c.GetFullNode().GetTMClient(ctx)
}

// GetFullNode returns the first available full node in the chain

func (c *Chain) GetFullNode() petritypes.NodeI {
if len(c.Nodes) > 0 {
// use first full node
Expand All @@ -345,6 +367,8 @@ func (c *Chain) GetFullNode() petritypes.NodeI {
return c.Validators[0]
}

// WaitForBlocks blocks until the chain increases in block height by delta

func (c *Chain) WaitForBlocks(ctx context.Context, delta uint64) error {
c.logger.Info("waiting for blocks", zap.Uint64("delta", delta))

Expand Down Expand Up @@ -380,6 +404,8 @@ func (c *Chain) WaitForBlocks(ctx context.Context, delta uint64) error {
return nil
}

// 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))

Expand Down Expand Up @@ -415,26 +441,39 @@ func (c *Chain) WaitForHeight(ctx context.Context, desiredHeight uint64) error {
return nil
}

// GetValidators returns all of the validating nodes in the chain

func (c *Chain) GetValidators() []petritypes.NodeI {
return c.Validators
}

// GetNodes returns all of the non-validating nodes in the chain

func (c *Chain) GetNodes() []petritypes.NodeI {
return c.Nodes
}

// GetValidatorWallets returns the wallets that were used to create the Validators on-chain.
// The ordering of the slice should correspond to the ordering of GetValidators

func (c *Chain) GetValidatorWallets() []petritypes.WalletI {
return c.ValidatorWallets
}

// GetFaucetWallet retunrs a wallet that was funded and can be used to fund other wallets

func (c *Chain) GetFaucetWallet() petritypes.WalletI {
return c.FaucetWallet
}

// GetTxConfig returns a Cosmos SDK TxConfig

func (c *Chain) GetTxConfig() sdkclient.TxConfig {
return c.Config.EncodingConfig.TxConfig
}

// GetInterfaceRegistry returns a Cosmos SDK InterfaceRegistry

func (c *Chain) GetInterfaceRegistry() codectypes.InterfaceRegistry {
return c.Config.EncodingConfig.InterfaceRegistry
}
9 changes: 9 additions & 0 deletions cosmos/chain/chain_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"github.com/skip-mev/petri/general/v2/types"
)

// BuildWallet creates a wallet in the first available full node's keystore. If a mnemonic is not specificied,
// a random one will be generated

func (c *Chain) BuildWallet(ctx context.Context, keyName, mnemonic string, walletConfig types.WalletConfig) (types.WalletI, error) {
// if mnemonic is empty, we just generate a wallet
if mnemonic == "" {
Expand All @@ -21,14 +24,20 @@ func (c *Chain) BuildWallet(ctx context.Context, keyName, mnemonic string, walle
return wallet.NewWallet(keyName, mnemonic, walletConfig)
}

// RecoverKey recovers a wallet in the first available full node's keystore using a provided mnemonic

func (c *Chain) RecoverKey(ctx context.Context, keyName, mnemonic string) error {
return c.GetFullNode().RecoverKey(ctx, keyName, mnemonic)
}

// CreateWallet creates a wallet in the first available full node's keystore using a randomly generated mnemonic

func (c *Chain) CreateWallet(ctx context.Context, keyName string) (types.WalletI, error) {
return c.GetFullNode().CreateWallet(ctx, keyName, c.Config.WalletConfig)
}

// GetAddress returns a Bech32 formatted address for a given key in the first available full node's keystore

func (c *Chain) GetAddress(ctx context.Context, keyName string) ([]byte, error) {
b32Addr, err := c.GetFullNode().KeyBech32(ctx, keyName, "acc")
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions cosmos/chain/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,33 @@ import (
"strings"
)

// GenesisKV is used in ModifyGenesis to specify which keys have to be modified
// in the resulting genesis

type GenesisKV struct {
Key string `json:"key"`
Value interface{} `json:"value"`
}

// GenesisModifier represents a type of function that takes in genesis formatted in bytes
// and returns a modified genesis file in the same format

type GenesisModifier func([]byte) ([]byte, error)

var _ GenesisModifier = ModifyGenesis(nil)

// NewGenesisKV is a function for creating a GenesisKV object

func NewGenesisKV(key string, value interface{}) GenesisKV {
return GenesisKV{
Key: key,
Value: value,
}
}

// ModifyGenesis is a function that is a GenesisModifier and takes in GenesisKV
// to specify which fields of the genesis file should be modified

func ModifyGenesis(genesisKV []GenesisKV) func([]byte) ([]byte, error) {
return func(genbz []byte) ([]byte, error) {
g := make(map[string]interface{})
Expand Down
1 change: 1 addition & 0 deletions cosmos/cosmosutil/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

// Account fetches the Cosmos SDK account from a provided full node
func (c *ChainClient) Account(ctx context.Context, address string) (authtypes.AccountI, error) {
authClient, err := c.getAuthClient(ctx)

Expand Down
7 changes: 7 additions & 0 deletions cosmos/cosmosutil/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

// bank queries

// Balances returns the bank module token balances for a given address
func (c *ChainClient) Balances(ctx context.Context, address string) (sdk.Coins, error) {
bankClient, err := c.getBankClient(ctx)

Expand Down Expand Up @@ -49,6 +50,7 @@ func (c *ChainClient) Balances(ctx context.Context, address string) (sdk.Coins,
return balances, nil
}

// Balances returns the bank module token balance for a given address and denom
func (c *ChainClient) Balance(ctx context.Context, address, denom string) (sdk.Coin, error) {
bankClient, err := c.getBankClient(ctx)

Expand All @@ -72,6 +74,7 @@ func (c *ChainClient) Balance(ctx context.Context, address, denom string) (sdk.C
return *res.Balance, nil
}

// DenomMetadata returns the bank module token metadata for a given denom
func (c *ChainClient) DenomMetadata(ctx context.Context, denom string) (banktypes.Metadata, error) {
bankClient, err := c.getBankClient(ctx)

Expand All @@ -90,6 +93,7 @@ func (c *ChainClient) DenomMetadata(ctx context.Context, denom string) (banktype
return res.Metadata, nil
}

// DenomsMetadata returns the bank module token metadata for all denoms
func (c *ChainClient) DenomsMetadata(ctx context.Context) ([]banktypes.Metadata, error) {
bankClient, err := c.getBankClient(ctx)

Expand Down Expand Up @@ -119,6 +123,7 @@ func (c *ChainClient) DenomsMetadata(ctx context.Context) ([]banktypes.Metadata,
return metadatas, nil
}

// TotalSupplyAll returns the total supply of all tokens in the bank module
func (c *ChainClient) TotalSupplyAll(ctx context.Context) (sdk.Coins, error) {
bankClient, err := c.getBankClient(ctx)

Expand Down Expand Up @@ -148,6 +153,7 @@ func (c *ChainClient) TotalSupplyAll(ctx context.Context) (sdk.Coins, error) {
return supplies, nil
}

// TotalSupplySingle returns the total supply of a single token in the bank module
func (c *ChainClient) BankTotalSupplySingle(ctx context.Context, denom string) (sdk.Coin, error) {
bankClient, err := c.getBankClient(ctx)

Expand All @@ -168,6 +174,7 @@ func (c *ChainClient) BankTotalSupplySingle(ctx context.Context, denom string) (

// bank transactions

// BankSend sends tokens from the given user to another address
func (c *ChainClient) BankSend(ctx context.Context, user InteractingWallet, toAddress []byte, amount sdk.Coins, gasSettings types.GasSettings, blocking bool) (*sdk.TxResponse, error) {
fromAccAddress, err := sdk.AccAddressFromHexUnsafe(hex.EncodeToString(user.Address()))
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cosmos/cosmosutil/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
rpctypes "github.com/cometbft/cometbft/rpc/core/types"
)

// Block fetches the Cosmos SDK block from a provided full node
func (c *ChainClient) Block(ctx context.Context, height *int64) (*rpctypes.ResultBlock, error) {
cc, err := c.Chain.GetTMClient(ctx)
defer cc.Stop()
Expand Down
10 changes: 10 additions & 0 deletions cosmos/cosmosutil/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/skip-mev/petri/general/v2/types"
)

// GovProposal fetches a proposal from the governance module
func (c *ChainClient) GovProposal(ctx context.Context, proposalID uint64) (*govtypes.Proposal, error) {
govClient, err := c.getGovClient(ctx)

Expand All @@ -26,6 +27,7 @@ func (c *ChainClient) GovProposal(ctx context.Context, proposalID uint64) (*govt
return res.GetProposal(), nil
}

// GovProposals fetches all proposals from the governance module
func (c *ChainClient) GovProposals(ctx context.Context) ([]*govtypes.Proposal, error) {
govClient, err := c.getGovClient(ctx)

Expand Down Expand Up @@ -58,6 +60,7 @@ func (c *ChainClient) GovProposals(ctx context.Context) ([]*govtypes.Proposal, e
return proposals, nil
}

// GovProposalVotes fetches all votes for a given proposal from the governance module
func (c *ChainClient) GovProposalVotes(ctx context.Context, proposalID uint64) ([]*govtypes.Vote, error) {
govClient, err := c.getGovClient(ctx)

Expand Down Expand Up @@ -91,6 +94,7 @@ func (c *ChainClient) GovProposalVotes(ctx context.Context, proposalID uint64) (
return votes, nil
}

// GovProposalVote fetches a vote for a given proposal from the governance module
func (c *ChainClient) GovProposalVote(ctx context.Context, proposalID uint64, voter string) (*govtypes.Vote, error) {
govClient, err := c.getGovClient(ctx)

Expand All @@ -110,6 +114,7 @@ func (c *ChainClient) GovProposalVote(ctx context.Context, proposalID uint64, vo
return res.GetVote(), nil
}

// GovProposalDeposits fetches all deposits for a given proposal from the governance module
func (c *ChainClient) GovProposalDeposits(ctx context.Context, proposalID uint64) ([]*govtypes.Deposit, error) {
govClient, err := c.getGovClient(ctx)

Expand Down Expand Up @@ -143,6 +148,7 @@ func (c *ChainClient) GovProposalDeposits(ctx context.Context, proposalID uint64
return deposits, nil
}

// GovProposalDeposit fetches a deposit for a given proposal and depositor from the governance module
func (c *ChainClient) GovProposalDeposit(ctx context.Context, proposalID uint64, depositor string) (*govtypes.Deposit, error) {
govClient, err := c.getGovClient(ctx)

Expand All @@ -162,6 +168,7 @@ func (c *ChainClient) GovProposalDeposit(ctx context.Context, proposalID uint64,
return res.GetDeposit(), nil
}

// GovTallyResult fetches the tally result for a given proposal from the governance module
func (c *ChainClient) GovTallyResult(ctx context.Context, proposalID uint64) (*govtypes.TallyResult, error) {
govClient, err := c.getGovClient(ctx)

Expand All @@ -180,6 +187,7 @@ func (c *ChainClient) GovTallyResult(ctx context.Context, proposalID uint64) (*g
return res.GetTally(), nil
}

// GovVoteOnProposal casts a vote on a given proposal using the address of the wallet voter
func (c *ChainClient) GovVoteOnProposal(ctx context.Context, proposalID uint64, voter InteractingWallet, option govtypes.VoteOption, gasSettings types.GasSettings) (*sdk.TxResponse, error) {
msg := govtypes.NewMsgVote(sdk.AccAddress(voter.FormattedAddress()), proposalID, option, "")

Expand All @@ -192,6 +200,7 @@ func (c *ChainClient) GovVoteOnProposal(ctx context.Context, proposalID uint64,
return txResp, err
}

// GovDepositOnProposal deposits tokens on a given proposal using the address of the wallet depositor
func (c *ChainClient) GovDepositOnProposal(ctx context.Context, proposalID uint64, depositor InteractingWallet, amount sdk.Coins, gasSettings types.GasSettings) (*sdk.TxResponse, error) {
msg := govtypes.NewMsgDeposit(sdk.AccAddress(depositor.FormattedAddress()), proposalID, amount)

Expand All @@ -204,6 +213,7 @@ func (c *ChainClient) GovDepositOnProposal(ctx context.Context, proposalID uint6
return txResp, err
}

// GovSubmitProposal submits a proposal using the address of the wallet proposer
func (c *ChainClient) GovSubmitProposal(ctx context.Context, proposer *InteractingWallet,
messages []sdk.Msg, initialDeposit sdk.Coins, gasSettings types.GasSettings, metadata,
title, summary string, expedited bool) (*sdk.TxResponse, error) {
Expand Down
1 change: 1 addition & 0 deletions cosmos/cosmosutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/skip-mev/petri/general/v2/types"
)

// GetFeeAmountsFromGasSettings returns the fee amounts from the gas settings
func GetFeeAmountsFromGasSettings(gasSettings types.GasSettings) sdk.Coins {
return sdk.NewCoins(sdk.NewCoin(gasSettings.GasDenom, math.NewInt(gasSettings.Gas).Mul(math.NewInt(gasSettings.PricePerGas))))
}
Loading

0 comments on commit 4426cbc

Please sign in to comment.