Skip to content

Commit

Permalink
add key-name param
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed Nov 29, 2024
1 parent a9d7766 commit 4fed6a4
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 56 deletions.
38 changes: 30 additions & 8 deletions contribs/gnogenesis/internal/txs/txs_add_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,35 @@ import (
"flag"
"fmt"

"github.com/gnolang/gno/tm2/pkg/crypto"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/gno.land/pkg/gnoland/ugnot"
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
"github.com/gnolang/gno/tm2/pkg/std"
)

var errInvalidPackageDir = errors.New("invalid package directory")

var genesisDeployFee = std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000)))
var (
genesisDeployFee = std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000)))
genesisDeployAddress = crypto.MustAddressFromString("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") // test1
)

type addPkgCfg struct {
txsCfg *txsCfg
deployerMnemonic string
txsCfg *txsCfg
keyName string
}

func (c *addPkgCfg) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(
&c.deployerMnemonic,
"deployer-mnemonic",
"source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast", // test1
"The mnemonic of the wallet that will create packages on the transaction genesis",
&c.keyName,
"key-name",
"",
"The package deployer key name or address",
)
}

Expand Down Expand Up @@ -66,11 +73,26 @@ func execTxsAddPackages(
if len(args) == 0 {
return errInvalidPackageDir
}
var creator crypto.Address
if cfg.keyName != "" {

Check failure on line 77 in contribs/gnogenesis/internal/txs/txs_add_packages.go

View workflow job for this annotation

GitHub Actions / Run Main (gnogenesis) / Go Linter / lint

unnecessary leading newline (whitespace)

kb, err := keys.NewKeyBaseFromDir(gnoenv.HomeDir())
if err != nil {
return err
}
info, err := kb.GetByNameOrAddress(cfg.keyName)
if err != nil {
return err
}
creator = info.GetAddress()
} else {
creator = genesisDeployAddress
}

parsedTxs := make([]gnoland.TxWithMetadata, 0)
for _, path := range args {
// Generate transactions from the packages (recursively)
txs, err := gnoland.LoadPackagesFromDir(path, cfg.deployerMnemonic, genesis.ChainID, genesisDeployFee)
txs, err := gnoland.LoadPackagesFromDir(path, creator, genesis.ChainID, genesisDeployFee)
if err != nil {
return fmt.Errorf("unable to load txs from directory, %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions gno.land/cmd/gnoland/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ var startGraphic = strings.ReplaceAll(`

var (
// Keep in sync with contribs/gnogenesis/internal/txs/txs_add_packages.go
deployerMnemonic = "source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast" // test1
genesisDeployFee = std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000)))
genesisDeployAddress = crypto.MustAddressFromString("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") // test1
genesisDeployFee = std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000)))
)

type startCfg struct {
Expand Down Expand Up @@ -396,7 +396,7 @@ func generateGenesisFile(genesisFile string, pk crypto.PubKey, c *startCfg) erro

// Load examples folder
examplesDir := filepath.Join(c.gnoRootDir, "examples")
pkgsTxs, err := gnoland.LoadPackagesFromDir(examplesDir, deployerMnemonic, c.chainID, genesisDeployFee)
pkgsTxs, err := gnoland.LoadPackagesFromDir(examplesDir, genesisDeployAddress, c.chainID, genesisDeployFee)
if err != nil {
return fmt.Errorf("unable to load examples folder: %w", err)
}
Expand Down
5 changes: 2 additions & 3 deletions gno.land/cmd/gnoland/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ func TestStart_Lazy(t *testing.T) {
io.SetErr(commands.WriteNopCloser(mockErr))

// Create and run the command
// Now lazy init takes longer as we're signing each transaction
ctx, cancelFn := context.WithTimeout(context.Background(), 250*time.Second)
ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFn()

// Set up the command ctx
Expand All @@ -129,7 +128,7 @@ func TestStart_Lazy(t *testing.T) {
})

// Set up the retry ctx
retryCtx, retryCtxCancelFn := context.WithTimeout(ctx, 250*time.Second)
retryCtx, retryCtxCancelFn := context.WithTimeout(ctx, 10*time.Second)
defer retryCtxCancelFn()

// This is a very janky way to verify the node has started.
Expand Down
33 changes: 2 additions & 31 deletions gno.land/pkg/gnoland/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/gnolang/gno/tm2/pkg/amino"
bft "github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
osm "github.com/gnolang/gno/tm2/pkg/os"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/pelletier/go-toml"
Expand Down Expand Up @@ -134,7 +133,7 @@ func LoadGenesisTxsFile(path string, chainID string, genesisRemote string) ([]Tx

// LoadPackagesFromDir loads gno packages from a directory.
// It creates and returns a list of transactions based on these packages.
func LoadPackagesFromDir(dir string, creatorMnemonic string, chainID string, fee std.Fee) ([]TxWithMetadata, error) {
func LoadPackagesFromDir(dir string, creator bft.Address, chainID string, fee std.Fee) ([]TxWithMetadata, error) {
// list all packages from target path
pkgs, err := gnomod.ListPkgs(dir)
if err != nil {
Expand All @@ -147,43 +146,15 @@ func LoadPackagesFromDir(dir string, creatorMnemonic string, chainID string, fee
return nil, fmt.Errorf("sorting packages: %w", err)
}

kb := keys.NewInMemory()
// Save the account
info, err := kb.CreateAccount(
"deployer",
creatorMnemonic,
"",
"",
0,
0,
)
if err != nil {
return nil, fmt.Errorf("unable to create account, %w", err)
}

// Filter out draft packages.
nonDraftPkgs := sortedPkgs.GetNonDraftPkgs()
txs := make([]TxWithMetadata, 0, len(nonDraftPkgs))

for _, pkg := range nonDraftPkgs {
tx, err := LoadPackage(pkg, info.GetAddress(), fee, nil)
tx, err := LoadPackage(pkg, creator, fee, nil)
if err != nil {
return nil, fmt.Errorf("unable to load package %q: %w", pkg.Dir, err)
}

// Both account number and account sequence are 0 on genesis transactions
txData, err := tx.GetSignBytes(chainID, 0, 0)
if err != nil {
return nil, fmt.Errorf("unable to generate mnemonic, %w", err)
}
sig, pub, err := kb.Sign("deployer", "", txData)
if err != nil {
return nil, err
}
tx.Signatures = []std.Signature{{
PubKey: pub,
Signature: sig,
}}
txs = append(txs, TxWithMetadata{
Tx: tx,
})
Expand Down
6 changes: 3 additions & 3 deletions gno.land/pkg/integration/testing_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestingNodeConfig(t TestingTS, gnoroot string, additionalTxs ...gnoland.TxW
params := LoadDefaultGenesisParamFile(t, gnoroot)
balances := LoadDefaultGenesisBalanceFile(t, gnoroot)
txs := make([]gnoland.TxWithMetadata, 0)
txs = append(txs, LoadDefaultPackages(t, DefaultAccount_Seed, gnoroot)...)
txs = append(txs, LoadDefaultPackages(t, creator, gnoroot)...)
txs = append(txs, additionalTxs...)

cfg.Genesis.AppState = gnoland.GnoGenesisState{
Expand Down Expand Up @@ -130,11 +130,11 @@ func DefaultTestingGenesisConfig(t TestingTS, gnoroot string, self crypto.PubKey
}

// LoadDefaultPackages loads the default packages for testing using a given creator address and gnoroot directory.
func LoadDefaultPackages(t TestingTS, creatorMnemonic string, gnoroot string) []gnoland.TxWithMetadata {
func LoadDefaultPackages(t TestingTS, creator bft.Address, gnoroot string) []gnoland.TxWithMetadata {
examplesDir := filepath.Join(gnoroot, "examples")

defaultFee := std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000)))
txs, err := gnoland.LoadPackagesFromDir(examplesDir, creatorMnemonic, "dev", defaultFee)
txs, err := gnoland.LoadPackagesFromDir(examplesDir, creator, "dev", defaultFee)
require.NoError(t, err)

return txs
Expand Down
4 changes: 1 addition & 3 deletions tm2/pkg/sdk/auth/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,17 +435,15 @@ func SetGasMeter(simulate bool, ctx sdk.Context, gasLimit int64) sdk.Context {
// and an account.
func GetSignBytes(chainID string, tx std.Tx, acc std.Account, genesis bool) ([]byte, error) {
var accNum uint64
var accSequence uint64
if !genesis {
accNum = acc.GetAccountNumber()
accSequence = acc.GetSequence()
}

return std.GetSignaturePayload(
std.SignDoc{
ChainID: chainID,
AccountNumber: accNum,
Sequence: accSequence,
Sequence: acc.GetSequence(),
Fee: tx.Fee,
Msgs: tx.Msgs,
Memo: tx.Memo,
Expand Down
10 changes: 5 additions & 5 deletions tm2/pkg/sdk/auth/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) {
env.acck.SetAccount(ctx, acc1)
acc2 := env.acck.NewAccountWithAddress(ctx, addr2)
acc2.SetCoins(tu.NewTestCoins())
require.NoError(t, acc2.SetAccountNumber(0))
require.NoError(t, acc2.SetAccountNumber(1))
env.acck.SetAccount(ctx, acc2)

// msg and signatures
Expand All @@ -204,25 +204,25 @@ func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) {
checkValidTx(t, anteHandler, ctx, tx, false)

// new tx from wrong account number
seqs = []uint64{0}
seqs = []uint64{1}
tx = tu.NewTestTx(t, ctx.ChainID(), msgs, privs, []uint64{1}, seqs, fee)
checkInvalidTx(t, anteHandler, ctx, tx, false, std.UnauthorizedError{})

// from correct account number
seqs = []uint64{0}
seqs = []uint64{1}
tx = tu.NewTestTx(t, ctx.ChainID(), msgs, privs, []uint64{0}, seqs, fee)
checkValidTx(t, anteHandler, ctx, tx, false)

// new tx with another signer and incorrect account numbers
msg1 := tu.NewTestMsg(addr1, addr2)
msg2 := tu.NewTestMsg(addr2, addr1)
msgs = []std.Msg{msg1, msg2}
privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{1, 0}, []uint64{0, 0}
privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{2, 0}, []uint64{0, 0}
tx = tu.NewTestTx(t, ctx.ChainID(), msgs, privs, accnums, seqs, fee)
checkInvalidTx(t, anteHandler, ctx, tx, false, std.UnauthorizedError{})

// correct account numbers
privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{0, 0}, []uint64{0, 0}
privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{0, 0}, []uint64{2, 0}
tx = tu.NewTestTx(t, ctx.ChainID(), msgs, privs, accnums, seqs, fee)
checkValidTx(t, anteHandler, ctx, tx, false)
}
Expand Down

0 comments on commit 4fed6a4

Please sign in to comment.