Skip to content

Commit 420b8b8

Browse files
committed
add key-name param
1 parent a9d7766 commit 420b8b8

File tree

7 files changed

+46
-55
lines changed

7 files changed

+46
-55
lines changed

contribs/gnogenesis/internal/txs/txs_add_packages.go

+31-8
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,36 @@ import (
66
"flag"
77
"fmt"
88

9+
"github.com/gnolang/gno/tm2/pkg/crypto"
10+
911
"github.com/gnolang/gno/gno.land/pkg/gnoland"
1012
"github.com/gnolang/gno/gno.land/pkg/gnoland/ugnot"
13+
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
1114
"github.com/gnolang/gno/tm2/pkg/bft/types"
1215
"github.com/gnolang/gno/tm2/pkg/commands"
16+
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
1317
"github.com/gnolang/gno/tm2/pkg/std"
1418
)
1519

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

18-
var genesisDeployFee = std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000)))
22+
var (
23+
// Keep in sync with gno.land/cmd/start.go
24+
genesisDeployAddress = crypto.MustAddressFromString("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") // test1
25+
genesisDeployFee = std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000)))
26+
)
1927

2028
type addPkgCfg struct {
21-
txsCfg *txsCfg
22-
deployerMnemonic string
29+
txsCfg *txsCfg
30+
keyName string
2331
}
2432

2533
func (c *addPkgCfg) RegisterFlags(fs *flag.FlagSet) {
2634
fs.StringVar(
27-
&c.deployerMnemonic,
28-
"deployer-mnemonic",
29-
"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
30-
"The mnemonic of the wallet that will create packages on the transaction genesis",
35+
&c.keyName,
36+
"key-name",
37+
"",
38+
"The package deployer key name or address",
3139
)
3240
}
3341

@@ -66,11 +74,26 @@ func execTxsAddPackages(
6674
if len(args) == 0 {
6775
return errInvalidPackageDir
6876
}
77+
var creator crypto.Address
78+
if cfg.keyName != "" {
79+
80+
kb, err := keys.NewKeyBaseFromDir(gnoenv.HomeDir())
81+
if err != nil {
82+
return err
83+
}
84+
info, err := kb.GetByNameOrAddress(cfg.keyName)
85+
if err != nil {
86+
return err
87+
}
88+
creator = info.GetAddress()
89+
} else {
90+
creator = genesisDeployAddress
91+
}
6992

7093
parsedTxs := make([]gnoland.TxWithMetadata, 0)
7194
for _, path := range args {
7295
// Generate transactions from the packages (recursively)
73-
txs, err := gnoland.LoadPackagesFromDir(path, cfg.deployerMnemonic, genesis.ChainID, genesisDeployFee)
96+
txs, err := gnoland.LoadPackagesFromDir(path, creator, genesisDeployFee)
7497
if err != nil {
7598
return fmt.Errorf("unable to load txs from directory, %w", err)
7699
}

gno.land/cmd/gnoland/start.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ var startGraphic = strings.ReplaceAll(`
4646

4747
var (
4848
// Keep in sync with contribs/gnogenesis/internal/txs/txs_add_packages.go
49-
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
50-
genesisDeployFee = std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000)))
49+
genesisDeployAddress = crypto.MustAddressFromString("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") // test1
50+
genesisDeployFee = std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000)))
5151
)
5252

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

397397
// Load examples folder
398398
examplesDir := filepath.Join(c.gnoRootDir, "examples")
399-
pkgsTxs, err := gnoland.LoadPackagesFromDir(examplesDir, deployerMnemonic, c.chainID, genesisDeployFee)
399+
pkgsTxs, err := gnoland.LoadPackagesFromDir(examplesDir, genesisDeployAddress, genesisDeployFee)
400400
if err != nil {
401401
return fmt.Errorf("unable to load examples folder: %w", err)
402402
}

gno.land/cmd/gnoland/start_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ func TestStart_Lazy(t *testing.T) {
116116
io.SetErr(commands.WriteNopCloser(mockErr))
117117

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

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

131130
// Set up the retry ctx
132-
retryCtx, retryCtxCancelFn := context.WithTimeout(ctx, 250*time.Second)
131+
retryCtx, retryCtxCancelFn := context.WithTimeout(ctx, 5*time.Second)
133132
defer retryCtxCancelFn()
134133

135134
// This is a very janky way to verify the node has started.

gno.land/pkg/gnoland/genesis.go

+2-31
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/gnolang/gno/tm2/pkg/amino"
1212
bft "github.com/gnolang/gno/tm2/pkg/bft/types"
1313
"github.com/gnolang/gno/tm2/pkg/crypto"
14-
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
1514
osm "github.com/gnolang/gno/tm2/pkg/os"
1615
"github.com/gnolang/gno/tm2/pkg/std"
1716
"github.com/pelletier/go-toml"
@@ -134,7 +133,7 @@ func LoadGenesisTxsFile(path string, chainID string, genesisRemote string) ([]Tx
134133

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

150-
kb := keys.NewInMemory()
151-
// Save the account
152-
info, err := kb.CreateAccount(
153-
"deployer",
154-
creatorMnemonic,
155-
"",
156-
"",
157-
0,
158-
0,
159-
)
160-
if err != nil {
161-
return nil, fmt.Errorf("unable to create account, %w", err)
162-
}
163-
164149
// Filter out draft packages.
165150
nonDraftPkgs := sortedPkgs.GetNonDraftPkgs()
166151
txs := make([]TxWithMetadata, 0, len(nonDraftPkgs))
167-
168152
for _, pkg := range nonDraftPkgs {
169-
tx, err := LoadPackage(pkg, info.GetAddress(), fee, nil)
153+
tx, err := LoadPackage(pkg, creator, fee, nil)
170154
if err != nil {
171155
return nil, fmt.Errorf("unable to load package %q: %w", pkg.Dir, err)
172156
}
173157

174-
// Both account number and account sequence are 0 on genesis transactions
175-
txData, err := tx.GetSignBytes(chainID, 0, 0)
176-
if err != nil {
177-
return nil, fmt.Errorf("unable to generate mnemonic, %w", err)
178-
}
179-
sig, pub, err := kb.Sign("deployer", "", txData)
180-
if err != nil {
181-
return nil, err
182-
}
183-
tx.Signatures = []std.Signature{{
184-
PubKey: pub,
185-
Signature: sig,
186-
}}
187158
txs = append(txs, TxWithMetadata{
188159
Tx: tx,
189160
})

gno.land/pkg/integration/testing_node.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestingNodeConfig(t TestingTS, gnoroot string, additionalTxs ...gnoland.TxW
6262
params := LoadDefaultGenesisParamFile(t, gnoroot)
6363
balances := LoadDefaultGenesisBalanceFile(t, gnoroot)
6464
txs := make([]gnoland.TxWithMetadata, 0)
65-
txs = append(txs, LoadDefaultPackages(t, DefaultAccount_Seed, gnoroot)...)
65+
txs = append(txs, LoadDefaultPackages(t, creator, gnoroot)...)
6666
txs = append(txs, additionalTxs...)
6767

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

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

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

140140
return txs

tm2/pkg/sdk/auth/ante.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -435,17 +435,15 @@ func SetGasMeter(simulate bool, ctx sdk.Context, gasLimit int64) sdk.Context {
435435
// and an account.
436436
func GetSignBytes(chainID string, tx std.Tx, acc std.Account, genesis bool) ([]byte, error) {
437437
var accNum uint64
438-
var accSequence uint64
439438
if !genesis {
440439
accNum = acc.GetAccountNumber()
441-
accSequence = acc.GetSequence()
442440
}
443441

444442
return std.GetSignaturePayload(
445443
std.SignDoc{
446444
ChainID: chainID,
447445
AccountNumber: accNum,
448-
Sequence: accSequence,
446+
Sequence: acc.GetSequence(),
449447
Fee: tx.Fee,
450448
Msgs: tx.Msgs,
451449
Memo: tx.Memo,

tm2/pkg/sdk/auth/ante_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) {
188188
env.acck.SetAccount(ctx, acc1)
189189
acc2 := env.acck.NewAccountWithAddress(ctx, addr2)
190190
acc2.SetCoins(tu.NewTestCoins())
191-
require.NoError(t, acc2.SetAccountNumber(0))
191+
require.NoError(t, acc2.SetAccountNumber(1))
192192
env.acck.SetAccount(ctx, acc2)
193193

194194
// msg and signatures
@@ -204,12 +204,12 @@ func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) {
204204
checkValidTx(t, anteHandler, ctx, tx, false)
205205

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

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

@@ -222,7 +222,7 @@ func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) {
222222
checkInvalidTx(t, anteHandler, ctx, tx, false, std.UnauthorizedError{})
223223

224224
// correct account numbers
225-
privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{0, 0}, []uint64{0, 0}
225+
privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{0, 0}, []uint64{2, 0}
226226
tx = tu.NewTestTx(t, ctx.ChainID(), msgs, privs, accnums, seqs, fee)
227227
checkValidTx(t, anteHandler, ctx, tx, false)
228228
}

0 commit comments

Comments
 (0)