From aa156266235cf01391ac1d4d20b47e3bbcd7e3dd Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Thu, 5 Dec 2024 13:03:56 -0500 Subject: [PATCH 01/25] feat: add genesis packages with transaction signing --- .../internal/txs/txs_add_packages.go | 76 ++++++-- .../internal/txs/txs_add_packages_test.go | 175 ++++++++++++++++-- gno.land/pkg/gnoland/app.go | 2 +- tm2/pkg/sdk/auth/ante.go | 4 +- tm2/pkg/sdk/auth/ante_test.go | 6 +- 5 files changed, 226 insertions(+), 37 deletions(-) diff --git a/contribs/gnogenesis/internal/txs/txs_add_packages.go b/contribs/gnogenesis/internal/txs/txs_add_packages.go index cf863c72116..a8437bb7847 100644 --- a/contribs/gnogenesis/internal/txs/txs_add_packages.go +++ b/contribs/gnogenesis/internal/txs/txs_add_packages.go @@ -6,7 +6,9 @@ import ( "flag" "fmt" + "github.com/gnolang/gno/gnovm/pkg/gnoenv" "github.com/gnolang/gno/tm2/pkg/crypto" + "github.com/gnolang/gno/tm2/pkg/crypto/keys" "github.com/gnolang/gno/gno.land/pkg/gnoland" "github.com/gnolang/gno/gno.land/pkg/gnoland/ugnot" @@ -27,16 +29,32 @@ var ( ) type addPkgCfg struct { - txsCfg *txsCfg - deployerAddress string + txsCfg *txsCfg + keyName string + gnoHome string + insecurePasswordStdin bool } func (c *addPkgCfg) RegisterFlags(fs *flag.FlagSet) { fs.StringVar( - &c.deployerAddress, - "deployer-address", - defaultCreator.String(), - "the address that will be used to deploy the package", + &c.keyName, + "key-name", + "", + "The package deployer key name or address", + ) + + fs.StringVar( + &c.gnoHome, + "gno-home", + gnoenv.HomeDir(), + "the gno home directory", + ) + + fs.BoolVar( + &c.insecurePasswordStdin, + "insecure-password-stdin", + false, + "the gno home directory", ) } @@ -78,14 +96,21 @@ func execTxsAddPackages( var ( creator = defaultCreator - err error + pass string ) - - // Check if the deployer address is set - if cfg.deployerAddress != defaultCreator.String() { - creator, err = crypto.AddressFromString(cfg.deployerAddress) + kb, err := keys.NewKeyBaseFromDir(cfg.gnoHome) + if err != nil { + return err + } + if cfg.keyName != "" { + info, err := kb.GetByNameOrAddress(cfg.keyName) + if err != nil { + return err + } + creator = info.GetAddress() + pass, err = io.GetPassword("Enter password.", cfg.insecurePasswordStdin) if err != nil { - return fmt.Errorf("%w, %w", errInvalidDeployerAddr, err) + return fmt.Errorf("cannot read password: %w", err) } } @@ -96,7 +121,11 @@ func execTxsAddPackages( if err != nil { return fmt.Errorf("unable to load txs from directory, %w", err) } - + if creator != defaultCreator { + if err := signTxs(txs, cfg.keyName, genesis.ChainID, kb, pass); err != nil { + return fmt.Errorf("unable to sign txs, %w", err) + } + } parsedTxs = append(parsedTxs, txs...) } @@ -117,3 +146,24 @@ func execTxsAddPackages( return nil } + +func signTxs(txs []gnoland.TxWithMetadata, keyName string, chainID string, kb keys.Keybase, pass string) error { + for index, tx := range txs { + signBytes, err := tx.Tx.GetSignBytes(chainID, 0, 0) + if err != nil { + return fmt.Errorf("unable to load txs from directory, %w", err) + } + signature, publicKey, err := kb.Sign(keyName, pass, signBytes) + txs[index].Tx.Signatures = []std.Signature{ + { + PubKey: publicKey, + Signature: signature, + }, + } + if err != nil { + return fmt.Errorf("unable sign tx %w", err) + } + } + + return nil +} diff --git a/contribs/gnogenesis/internal/txs/txs_add_packages_test.go b/contribs/gnogenesis/internal/txs/txs_add_packages_test.go index c3405d6ff8d..f67529c449f 100644 --- a/contribs/gnogenesis/internal/txs/txs_add_packages_test.go +++ b/contribs/gnogenesis/internal/txs/txs_add_packages_test.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "testing" "github.com/gnolang/contribs/gnogenesis/internal/common" @@ -12,6 +13,8 @@ import ( vmm "github.com/gnolang/gno/gno.land/pkg/sdk/vm" "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/crypto/keys/client" "github.com/gnolang/gno/tm2/pkg/testutils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -60,8 +63,10 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { assert.ErrorContains(t, cmdErr, errInvalidPackageDir.Error()) }) - t.Run("invalid deployer address", func(t *testing.T) { + t.Run("nonExistentKeyName", func(t *testing.T) { t.Parallel() + keybaseDir := t.TempDir() + keyname := "beep-boop" tempGenesis, cleanup := testutils.NewTestFile(t) t.Cleanup(cleanup) @@ -77,16 +82,19 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { "--genesis-path", tempGenesis.Name(), t.TempDir(), // package dir - "--deployer-address", - "beep-boop", // invalid address + "--key-name", + keyname, // non-existent key name + "--gno-home", + keybaseDir, // temporaryDir for keybase } // Run the command cmdErr := cmd.ParseAndRun(context.Background(), args) - assert.ErrorIs(t, cmdErr, errInvalidDeployerAddr) + fmt.Println(cmdErr.Error()) + assert.ErrorContains(t, cmdErr, "Key "+keyname+" not found") }) - t.Run("valid package", func(t *testing.T) { + t.Run("existentKeyBadPassword", func(t *testing.T) { t.Parallel() tempGenesis, cleanup := testutils.NewTestFile(t) @@ -94,32 +102,139 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { genesis := common.GetDefaultGenesis() require.NoError(t, genesis.SaveAs(tempGenesis.Name())) - // Prepare the package var ( packagePath = "gno.land/p/demo/cuttlas" dir = t.TempDir() + keybaseDir = t.TempDir() + keyname = "beep-boop" + password = "somepass" ) + createValidFile(t, dir, packagePath) + // Create key + kb, err := keys.NewKeyBaseFromDir(keybaseDir) + require.NoError(t, err) + mnemonic, err := client.GenerateMnemonic(256) + require.NoError(t, err) + _, err = kb.CreateAccount(keyname, mnemonic, "", password+"wrong", 0, 0) + require.NoError(t, err) - createFile := func(path, data string) { - file, err := os.Create(path) - require.NoError(t, err) + io := commands.NewTestIO() + io.SetIn( + strings.NewReader( + fmt.Sprintf( + "%s\n", + password, + ), + ), + ) - _, err = file.WriteString(data) - require.NoError(t, err) + // Create the command + cmd := NewTxsCmd(io) + args := []string{ + "add", + "packages", + "--genesis-path", + tempGenesis.Name(), + "--key-name", + keyname, // non-existent key name + "--gno-home", + keybaseDir, // temporaryDir for keybase + "--insecure-password-stdin", + dir, } - // Create the gno.mod file - createFile( - filepath.Join(dir, "gno.mod"), - fmt.Sprintf("module %s\n", packagePath), + // Run the command + cmdErr := cmd.ParseAndRun(context.Background(), args) + fmt.Println(cmdErr.Error()) + assert.ErrorContains(t, cmdErr, "unable to sign txs, unable sign tx invalid account password") + }) + + t.Run("existentKeyOKPassword", func(t *testing.T) { + t.Parallel() + + tempGenesis, cleanup := testutils.NewTestFile(t) + t.Cleanup(cleanup) + + genesis := common.GetDefaultGenesis() + require.NoError(t, genesis.SaveAs(tempGenesis.Name())) + // Prepare the package + var ( + packagePath = "gno.land/p/demo/cuttlas" + dir = t.TempDir() + keybaseDir = t.TempDir() + keyname = "beep-boop" + password = "somepass" + ) + createValidFile(t, dir, packagePath) + // Create key + kb, err := keys.NewKeyBaseFromDir(keybaseDir) + require.NoError(t, err) + mnemonic, err := client.GenerateMnemonic(256) + require.NoError(t, err) + _, err = kb.CreateAccount(keyname, mnemonic, "", password, 0, 0) + require.NoError(t, err) + + io := commands.NewTestIO() + io.SetIn( + strings.NewReader( + fmt.Sprintf( + "%s\n", + password, + ), + ), ) - // Create a simple main.gno - createFile( - filepath.Join(dir, "main.gno"), - "package cuttlas\n\nfunc Example() string {\nreturn \"Manos arriba!\"\n}", + // Create the command + cmd := NewTxsCmd(io) + args := []string{ + "add", + "packages", + "--genesis-path", + tempGenesis.Name(), + "--key-name", + keyname, // non-existent key name + "--gno-home", + keybaseDir, // temporaryDir for keybase + "--insecure-password-stdin", + dir, + } + + // Run the command + cmdErr := cmd.ParseAndRun(context.Background(), args) + require.NoError(t, cmdErr) + + // Validate the transactions were written down + updatedGenesis, err := types.GenesisDocFromFile(tempGenesis.Name()) + require.NoError(t, err) + require.NotNil(t, updatedGenesis.AppState) + + // Fetch the state + state := updatedGenesis.AppState.(gnoland.GnoGenesisState) + + require.Equal(t, 1, len(state.Txs)) + require.Equal(t, 1, len(state.Txs[0].Tx.Msgs)) + + msgAddPkg, ok := state.Txs[0].Tx.Msgs[0].(vmm.MsgAddPackage) + require.True(t, ok) + + assert.Equal(t, packagePath, msgAddPkg.Package.Path) + }) + + t.Run("valid package", func(t *testing.T) { + t.Parallel() + + tempGenesis, cleanup := testutils.NewTestFile(t) + t.Cleanup(cleanup) + + genesis := common.GetDefaultGenesis() + require.NoError(t, genesis.SaveAs(tempGenesis.Name())) + // Prepare the package + var ( + packagePath = "gno.land/p/demo/cuttlas" + dir = t.TempDir() ) + createValidFile(t, dir, packagePath) // Create the command cmd := NewTxsCmd(commands.NewTestIO()) @@ -152,3 +267,25 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { assert.Equal(t, packagePath, msgAddPkg.Package.Path) }) } + +func createValidFile(t *testing.T, dir string, packagePath string) { + createFile := func(path, data string) { + file, err := os.Create(path) + require.NoError(t, err) + + _, err = file.WriteString(data) + require.NoError(t, err) + } + + // Create the gno.mod file + createFile( + filepath.Join(dir, "gno.mod"), + fmt.Sprintf("module %s\n", packagePath), + ) + + // Create a simple main.gno + createFile( + filepath.Join(dir, "main.gno"), + "package cuttlas\n\nfunc Example() string {\nreturn \"Manos arriba!\"\n}", + ) +} diff --git a/gno.land/pkg/gnoland/app.go b/gno.land/pkg/gnoland/app.go index e0c93f6194f..c526bc11c41 100644 --- a/gno.land/pkg/gnoland/app.go +++ b/gno.land/pkg/gnoland/app.go @@ -103,7 +103,7 @@ func NewAppWithOptions(cfg *AppOptions) (abci.Application, error) { // Set AnteHandler authOptions := auth.AnteOptions{ - VerifyGenesisSignatures: false, // for development + VerifyGenesisSignatures: true, } authAnteHandler := auth.NewAnteHandler( acctKpr, bankKpr, auth.DefaultSigVerificationGasConsumer, authOptions) diff --git a/tm2/pkg/sdk/auth/ante.go b/tm2/pkg/sdk/auth/ante.go index d36b376aa8d..da7ae2575d7 100644 --- a/tm2/pkg/sdk/auth/ante.go +++ b/tm2/pkg/sdk/auth/ante.go @@ -394,15 +394,17 @@ 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: acc.GetSequence(), + Sequence: accSequence, Fee: tx.Fee, Msgs: tx.Msgs, Memo: tx.Memo, diff --git a/tm2/pkg/sdk/auth/ante_test.go b/tm2/pkg/sdk/auth/ante_test.go index 86e34391770..597d4408efe 100644 --- a/tm2/pkg/sdk/auth/ante_test.go +++ b/tm2/pkg/sdk/auth/ante_test.go @@ -208,8 +208,8 @@ func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) { 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{1} + // At genesis account number is zero + seqs = []uint64{0} tx = tu.NewTestTx(t, ctx.ChainID(), msgs, privs, []uint64{0}, seqs, fee) checkValidTx(t, anteHandler, ctx, tx, false) @@ -222,7 +222,7 @@ func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) { checkInvalidTx(t, anteHandler, ctx, tx, false, std.UnauthorizedError{}) // correct account numbers - privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{0, 0}, []uint64{2, 0} + privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{0, 0}, []uint64{0, 0} tx = tu.NewTestTx(t, ctx.ChainID(), msgs, privs, accnums, seqs, fee) checkValidTx(t, anteHandler, ctx, tx, false) } From 169e2a3fa35d6b800e9169f909aba85f6fa4bd61 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Thu, 5 Dec 2024 13:29:25 -0500 Subject: [PATCH 02/25] fix linter --- contribs/gnogenesis/internal/txs/txs_add_packages.go | 3 +-- contribs/gnogenesis/internal/txs/txs_add_packages_test.go | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contribs/gnogenesis/internal/txs/txs_add_packages.go b/contribs/gnogenesis/internal/txs/txs_add_packages.go index a8437bb7847..0df579d6d5b 100644 --- a/contribs/gnogenesis/internal/txs/txs_add_packages.go +++ b/contribs/gnogenesis/internal/txs/txs_add_packages.go @@ -18,8 +18,7 @@ import ( ) var ( - errInvalidPackageDir = errors.New("invalid package directory") - errInvalidDeployerAddr = errors.New("invalid deployer address") + errInvalidPackageDir = errors.New("invalid package directory") ) // Keep in sync with gno.land/cmd/start.go diff --git a/contribs/gnogenesis/internal/txs/txs_add_packages_test.go b/contribs/gnogenesis/internal/txs/txs_add_packages_test.go index f67529c449f..d28dbdd075d 100644 --- a/contribs/gnogenesis/internal/txs/txs_add_packages_test.go +++ b/contribs/gnogenesis/internal/txs/txs_add_packages_test.go @@ -269,6 +269,7 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { } func createValidFile(t *testing.T, dir string, packagePath string) { + t.Helper() createFile := func(path, data string) { file, err := os.Create(path) require.NoError(t, err) From 4eb4f8a1d55a7f4e49126856767a9524c429f112 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Mon, 9 Dec 2024 13:24:25 -0500 Subject: [PATCH 03/25] fix tests --- .../internal/txs/txs_add_packages.go | 4 +--- .../pkg/integration/testing_integration.go | 19 +++++++++++++++++++ tm2/pkg/sdk/auth/ante.go | 4 +++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/contribs/gnogenesis/internal/txs/txs_add_packages.go b/contribs/gnogenesis/internal/txs/txs_add_packages.go index 0df579d6d5b..16da84a916d 100644 --- a/contribs/gnogenesis/internal/txs/txs_add_packages.go +++ b/contribs/gnogenesis/internal/txs/txs_add_packages.go @@ -17,9 +17,7 @@ import ( "github.com/gnolang/gno/tm2/pkg/std" ) -var ( - errInvalidPackageDir = errors.New("invalid package directory") -) +var errInvalidPackageDir = errors.New("invalid package directory") // Keep in sync with gno.land/cmd/start.go var ( diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index 2a0a4cf1106..fa0a0118566 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -664,6 +664,11 @@ func (pl *pkgsLoader) SetPatch(replace, with string) { } func (pl *pkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std.Coins) ([]gnoland.TxWithMetadata, error) { + kb := keys.NewInMemory() + _, err := kb.CreateAccount("creator", DefaultAccount_Seed, "", "", 0, 0) + if err != nil { + return nil, fmt.Errorf("createAccount: %w", err) + } pkgslist, err := pl.List().Sort() // sorts packages by their dependencies. if err != nil { return nil, fmt.Errorf("unable to sort packages: %w", err) @@ -695,6 +700,20 @@ func (pl *pkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std } } } + signbytes, err := tx.GetSignBytes("tendermint_test", 0, 0) + if err != nil { + return nil, fmt.Errorf("unable to get sign bytes %q: %w", pkg.Name, err) + } + signature, pubKey, err := kb.Sign("creator", "", signbytes) + if err != nil { + return nil, fmt.Errorf("unable to sign transaction %q: %w", pkg.Name, err) + } + tx.Signatures = []std.Signature{ + { + PubKey: pubKey, + Signature: signature, + }, + } txs[i] = gnoland.TxWithMetadata{ Tx: tx, diff --git a/tm2/pkg/sdk/auth/ante.go b/tm2/pkg/sdk/auth/ante.go index da7ae2575d7..d00a38925d3 100644 --- a/tm2/pkg/sdk/auth/ante.go +++ b/tm2/pkg/sdk/auth/ante.go @@ -146,7 +146,9 @@ func NewAnteHandler(ak AccountKeeper, bank BankKeeperI, sigGasConsumer Signature // check signature, return account with incremented nonce sacc := signerAccs[i] - if isGenesis && !opts.VerifyGenesisSignatures { + // Do not check signatures if the chainID is dev (gnodev) if we decide to verify the genesis signatures for + // gnodev the initialization would take longer + if isGenesis && (!opts.VerifyGenesisSignatures || newCtx.ChainID() == "dev") { // No signatures are needed for genesis. } else { // Check signature From 90e0c0cda551b60005f96d43478541029488a8e9 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Thu, 12 Dec 2024 10:06:24 -0500 Subject: [PATCH 04/25] testing fix --- tm2/pkg/sdk/auth/ante.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tm2/pkg/sdk/auth/ante.go b/tm2/pkg/sdk/auth/ante.go index d00a38925d3..53cfe4b598f 100644 --- a/tm2/pkg/sdk/auth/ante.go +++ b/tm2/pkg/sdk/auth/ante.go @@ -148,7 +148,7 @@ func NewAnteHandler(ak AccountKeeper, bank BankKeeperI, sigGasConsumer Signature sacc := signerAccs[i] // Do not check signatures if the chainID is dev (gnodev) if we decide to verify the genesis signatures for // gnodev the initialization would take longer - if isGenesis && (!opts.VerifyGenesisSignatures || newCtx.ChainID() == "dev") { + if isGenesis && (!opts.VerifyGenesisSignatures || newCtx.ChainID() == "dev" || newCtx.ChainID() == "tendermint_test") { // No signatures are needed for genesis. } else { // Check signature From af8615275d48487fe0e78eee949519fd9d0eec44 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Thu, 12 Dec 2024 11:13:12 -0500 Subject: [PATCH 05/25] sign packages by default with test1 account --- contribs/gnogenesis/go.mod | 4 + contribs/gnogenesis/go.sum | 8 ++ .../internal/txs/txs_add_packages.go | 81 ++++++++++++------- 3 files changed, 62 insertions(+), 31 deletions(-) diff --git a/contribs/gnogenesis/go.mod b/contribs/gnogenesis/go.mod index f1b316c2bee..a9b5d6afd63 100644 --- a/contribs/gnogenesis/go.mod +++ b/contribs/gnogenesis/go.mod @@ -30,6 +30,7 @@ require ( github.com/peterbourgon/ff/v3 v3.4.0 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/rs/cors v1.11.1 // indirect github.com/rs/xid v1.6.0 // indirect github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect @@ -45,6 +46,8 @@ require ( go.opentelemetry.io/otel/trace v1.29.0 // indirect go.opentelemetry.io/proto/otlp v1.3.1 // indirect go.uber.org/multierr v1.11.0 // indirect + go.uber.org/zap v1.27.0 // indirect + go.uber.org/zap/exp v0.2.0 // indirect golang.org/x/crypto v0.26.0 // indirect golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect golang.org/x/mod v0.20.0 // indirect @@ -52,6 +55,7 @@ require ( golang.org/x/sys v0.24.0 // indirect golang.org/x/term v0.23.0 // indirect golang.org/x/text v0.17.0 // indirect + golang.org/x/tools v0.24.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20240822170219-fc7c04adadcd // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd // indirect google.golang.org/grpc v1.65.0 // indirect diff --git a/contribs/gnogenesis/go.sum b/contribs/gnogenesis/go.sum index 7ba3aede534..d1b75fe6eb1 100644 --- a/contribs/gnogenesis/go.sum +++ b/contribs/gnogenesis/go.sum @@ -153,8 +153,14 @@ go.opentelemetry.io/otel/trace v1.29.0 h1:J/8ZNK4XgR7a21DZUAsbF8pZ5Jcw1VhACmnYt3 go.opentelemetry.io/otel/trace v1.29.0/go.mod h1:eHl3w0sp3paPkYstJOmAimxhiFXPg+MMTlEh3nsQgWQ= go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0= go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +go.uber.org/zap/exp v0.2.0 h1:FtGenNNeCATRB3CmB/yEUnjEFeJWpB/pMcy7e2bKPYs= +go.uber.org/zap/exp v0.2.0/go.mod h1:t0gqAIdh1MfKv9EwN/dLwfZnJxe9ITAZN78HEWPFWDQ= golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= @@ -193,6 +199,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24= +golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/contribs/gnogenesis/internal/txs/txs_add_packages.go b/contribs/gnogenesis/internal/txs/txs_add_packages.go index 16da84a916d..2446f898a65 100644 --- a/contribs/gnogenesis/internal/txs/txs_add_packages.go +++ b/contribs/gnogenesis/internal/txs/txs_add_packages.go @@ -5,13 +5,14 @@ import ( "errors" "flag" "fmt" + "os" - "github.com/gnolang/gno/gnovm/pkg/gnoenv" - "github.com/gnolang/gno/tm2/pkg/crypto" "github.com/gnolang/gno/tm2/pkg/crypto/keys" + "github.com/gnolang/gno/gno.land/pkg/gnoclient" "github.com/gnolang/gno/gno.land/pkg/gnoland" "github.com/gnolang/gno/gno.land/pkg/gnoland/ugnot" + "github.com/gnolang/gno/gno.land/pkg/integration" "github.com/gnolang/gno/tm2/pkg/bft/types" "github.com/gnolang/gno/tm2/pkg/commands" "github.com/gnolang/gno/tm2/pkg/std" @@ -20,10 +21,7 @@ import ( var errInvalidPackageDir = errors.New("invalid package directory") // Keep in sync with gno.land/cmd/start.go -var ( - defaultCreator = crypto.MustAddressFromString("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") // test1 - genesisDeployFee = std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000))) -) +var genesisDeployFee = std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000))) type addPkgCfg struct { txsCfg *txsCfg @@ -43,7 +41,7 @@ func (c *addPkgCfg) RegisterFlags(fs *flag.FlagSet) { fs.StringVar( &c.gnoHome, "gno-home", - gnoenv.HomeDir(), + os.Getenv("GNOHOME"), "the gno home directory", ) @@ -91,38 +89,28 @@ func execTxsAddPackages( return errInvalidPackageDir } - var ( - creator = defaultCreator - pass string - ) - kb, err := keys.NewKeyBaseFromDir(cfg.gnoHome) + signer, err := signerWithConfig(cfg, io, genesis.ChainID) if err != nil { - return err + return fmt.Errorf("unable to load signer, %w", err) } - if cfg.keyName != "" { - info, err := kb.GetByNameOrAddress(cfg.keyName) - if err != nil { - return err - } - creator = info.GetAddress() - pass, err = io.GetPassword("Enter password.", cfg.insecurePasswordStdin) - if err != nil { - return fmt.Errorf("cannot read password: %w", err) - } + + info, err := signer.Info() + if err != nil { + return fmt.Errorf("unable to get signer info, %w", err) } parsedTxs := make([]gnoland.TxWithMetadata, 0) for _, path := range args { // Generate transactions from the packages (recursively) - txs, err := gnoland.LoadPackagesFromDir(path, creator, genesisDeployFee) + txs, err := gnoland.LoadPackagesFromDir(path, info.GetAddress(), genesisDeployFee) if err != nil { return fmt.Errorf("unable to load txs from directory, %w", err) } - if creator != defaultCreator { - if err := signTxs(txs, cfg.keyName, genesis.ChainID, kb, pass); err != nil { - return fmt.Errorf("unable to sign txs, %w", err) - } + + if err := signTxs(txs, signer); err != nil { + return fmt.Errorf("unable to sign txs, %w", err) } + parsedTxs = append(parsedTxs, txs...) } @@ -144,13 +132,13 @@ func execTxsAddPackages( return nil } -func signTxs(txs []gnoland.TxWithMetadata, keyName string, chainID string, kb keys.Keybase, pass string) error { +func signTxs(txs []gnoland.TxWithMetadata, signer *gnoclient.SignerFromKeybase) error { for index, tx := range txs { - signBytes, err := tx.Tx.GetSignBytes(chainID, 0, 0) + signBytes, err := tx.Tx.GetSignBytes(signer.ChainID, 0, 0) if err != nil { return fmt.Errorf("unable to load txs from directory, %w", err) } - signature, publicKey, err := kb.Sign(keyName, pass, signBytes) + signature, publicKey, err := signer.Keybase.Sign(signer.Account, signer.Password, signBytes) txs[index].Tx.Signatures = []std.Signature{ { PubKey: publicKey, @@ -164,3 +152,34 @@ func signTxs(txs []gnoland.TxWithMetadata, keyName string, chainID string, kb ke return nil } + +func signerWithConfig(cfg *addPkgCfg, io commands.IO, chainID string) (*gnoclient.SignerFromKeybase, error) { + var ( + keyname = integration.DefaultAccount_Name + pass string + kb keys.Keybase + err error + ) + + if cfg.keyName != "" { + keyname = cfg.keyName + kb, err = keys.NewKeyBaseFromDir(cfg.gnoHome) + if err != nil { + return nil, fmt.Errorf("unable to load keybase: %w", err) + } + pass, err = io.GetPassword("Enter password.", cfg.insecurePasswordStdin) + if err != nil { + return nil, fmt.Errorf("cannot read password: %w", err) + } + } else { + kb = keys.NewInMemory() + kb.CreateAccount(integration.DefaultAccount_Name, integration.DefaultAccount_Seed, "", "", 0, 0) + } + + return &gnoclient.SignerFromKeybase{ + Account: keyname, + ChainID: chainID, + Keybase: kb, + Password: pass, + }, nil +} From bf39fe7167d8c5f24249116632e327809f5bd582 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Thu, 12 Dec 2024 11:27:44 -0500 Subject: [PATCH 06/25] Add test --- .../internal/txs/txs_add_packages_test.go | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/contribs/gnogenesis/internal/txs/txs_add_packages_test.go b/contribs/gnogenesis/internal/txs/txs_add_packages_test.go index d28dbdd075d..5dbe1b7dc9d 100644 --- a/contribs/gnogenesis/internal/txs/txs_add_packages_test.go +++ b/contribs/gnogenesis/internal/txs/txs_add_packages_test.go @@ -74,8 +74,17 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { genesis := common.GetDefaultGenesis() require.NoError(t, genesis.SaveAs(tempGenesis.Name())) + io := commands.NewTestIO() + io.SetIn( + strings.NewReader( + fmt.Sprintf( + "%s\n", + "password", + ), + ), + ) // Create the command - cmd := NewTxsCmd(commands.NewTestIO()) + cmd := NewTxsCmd(io) args := []string{ "add", "packages", @@ -86,6 +95,7 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { keyname, // non-existent key name "--gno-home", keybaseDir, // temporaryDir for keybase + "--insecure-password-stdin", } // Run the command @@ -220,6 +230,54 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { assert.Equal(t, packagePath, msgAddPkg.Package.Path) }) + t.Run("okDefaultAccount", func(t *testing.T) { + t.Parallel() + + tempGenesis, cleanup := testutils.NewTestFile(t) + t.Cleanup(cleanup) + + genesis := common.GetDefaultGenesis() + require.NoError(t, genesis.SaveAs(tempGenesis.Name())) + // Prepare the package + var ( + packagePath = "gno.land/p/demo/cuttlas" + dir = t.TempDir() + keybaseDir = t.TempDir() + ) + createValidFile(t, dir, packagePath) + + // Create the command + cmd := NewTxsCmd(commands.NewTestIO()) + args := []string{ + "add", + "packages", + "--genesis-path", + tempGenesis.Name(), + "--gno-home", + keybaseDir, // temporaryDir for keybase + dir, + } + + // Run the command + cmdErr := cmd.ParseAndRun(context.Background(), args) + require.NoError(t, cmdErr) + + // Validate the transactions were written down + updatedGenesis, err := types.GenesisDocFromFile(tempGenesis.Name()) + require.NoError(t, err) + require.NotNil(t, updatedGenesis.AppState) + + // Fetch the state + state := updatedGenesis.AppState.(gnoland.GnoGenesisState) + + require.Equal(t, 1, len(state.Txs)) + require.Equal(t, 1, len(state.Txs[0].Tx.Msgs)) + + msgAddPkg, ok := state.Txs[0].Tx.Msgs[0].(vmm.MsgAddPackage) + require.True(t, ok) + + assert.Equal(t, packagePath, msgAddPkg.Package.Path) + }) t.Run("valid package", func(t *testing.T) { t.Parallel() From 66b1725164d12b98baaf540b4cb31f7df935b81e Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Thu, 12 Dec 2024 12:06:19 -0500 Subject: [PATCH 07/25] remove temporary fix on gnostart --- gno.land/pkg/integration/testing_integration.go | 14 +++++++------- tm2/pkg/sdk/auth/ante.go | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index fa0a0118566..11e372b7da0 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -187,7 +187,9 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { pkgs := ts.Value(envKeyPkgsLoader).(*pkgsLoader) // grab logger creator := crypto.MustAddressFromString(DefaultAccount_Address) // test1 defaultFee := std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000))) - pkgsTxs, err := pkgs.LoadPackages(creator, defaultFee, nil) + // Generate config and node + cfg := TestingMinimalNodeConfig(t, gnoRootDir) + pkgsTxs, err := pkgs.LoadPackages(creator, defaultFee, nil, cfg.TMConfig.ChainID()) if err != nil { ts.Fatalf("unable to load packages txs: %s", err) } @@ -195,8 +197,6 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { // Warp up `ts` so we can pass it to other testing method t := TSTestingT(ts) - // Generate config and node - cfg := TestingMinimalNodeConfig(t, gnoRootDir) genesis := ts.Value(envKeyGenesis).(*gnoland.GnoGenesisState) genesis.Txs = append(pkgsTxs, genesis.Txs...) @@ -663,9 +663,9 @@ func (pl *pkgsLoader) SetPatch(replace, with string) { pl.patchs[replace] = with } -func (pl *pkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std.Coins) ([]gnoland.TxWithMetadata, error) { +func (pl *pkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std.Coins, chainID string) ([]gnoland.TxWithMetadata, error) { kb := keys.NewInMemory() - _, err := kb.CreateAccount("creator", DefaultAccount_Seed, "", "", 0, 0) + _, err := kb.CreateAccount(DefaultAccount_Name, DefaultAccount_Seed, "", "", 0, 0) if err != nil { return nil, fmt.Errorf("createAccount: %w", err) } @@ -700,11 +700,11 @@ func (pl *pkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std } } } - signbytes, err := tx.GetSignBytes("tendermint_test", 0, 0) + signbytes, err := tx.GetSignBytes(chainID, 0, 0) if err != nil { return nil, fmt.Errorf("unable to get sign bytes %q: %w", pkg.Name, err) } - signature, pubKey, err := kb.Sign("creator", "", signbytes) + signature, pubKey, err := kb.Sign(DefaultAccount_Name, "", signbytes) if err != nil { return nil, fmt.Errorf("unable to sign transaction %q: %w", pkg.Name, err) } diff --git a/tm2/pkg/sdk/auth/ante.go b/tm2/pkg/sdk/auth/ante.go index 53cfe4b598f..d00a38925d3 100644 --- a/tm2/pkg/sdk/auth/ante.go +++ b/tm2/pkg/sdk/auth/ante.go @@ -148,7 +148,7 @@ func NewAnteHandler(ak AccountKeeper, bank BankKeeperI, sigGasConsumer Signature sacc := signerAccs[i] // Do not check signatures if the chainID is dev (gnodev) if we decide to verify the genesis signatures for // gnodev the initialization would take longer - if isGenesis && (!opts.VerifyGenesisSignatures || newCtx.ChainID() == "dev" || newCtx.ChainID() == "tendermint_test") { + if isGenesis && (!opts.VerifyGenesisSignatures || newCtx.ChainID() == "dev") { // No signatures are needed for genesis. } else { // Check signature From 5b4aadff2b7953bfcbfd8793ab6f25c7c5509488 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Thu, 12 Dec 2024 12:42:18 -0500 Subject: [PATCH 08/25] change default name to dev --- tm2/pkg/bft/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tm2/pkg/bft/config/config.go b/tm2/pkg/bft/config/config.go index f9e9a0cd899..86ce3e958a3 100644 --- a/tm2/pkg/bft/config/config.go +++ b/tm2/pkg/bft/config/config.go @@ -344,7 +344,7 @@ func DefaultBaseConfig() BaseConfig { // testBaseConfig returns a base configuration for testing a Tendermint node func testBaseConfig() BaseConfig { cfg := DefaultBaseConfig() - cfg.chainID = "tendermint_test" + cfg.chainID = "dev" cfg.ProxyApp = "mock://kvstore" cfg.FastSyncMode = false cfg.DBBackend = "memdb" From 98f9ae0539e78cf68f0739c149b3b002f0fd7499 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Thu, 12 Dec 2024 13:57:00 -0500 Subject: [PATCH 09/25] change back to tendermit_test --- tm2/pkg/bft/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tm2/pkg/bft/config/config.go b/tm2/pkg/bft/config/config.go index 86ce3e958a3..f9e9a0cd899 100644 --- a/tm2/pkg/bft/config/config.go +++ b/tm2/pkg/bft/config/config.go @@ -344,7 +344,7 @@ func DefaultBaseConfig() BaseConfig { // testBaseConfig returns a base configuration for testing a Tendermint node func testBaseConfig() BaseConfig { cfg := DefaultBaseConfig() - cfg.chainID = "dev" + cfg.chainID = "tendermint_test" cfg.ProxyApp = "mock://kvstore" cfg.FastSyncMode = false cfg.DBBackend = "memdb" From d70fa9b318f6cb9427c50e1a657ca9ac19189d29 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Thu, 12 Dec 2024 14:51:04 -0500 Subject: [PATCH 10/25] do not check verification on gnodev tests --- tm2/pkg/sdk/auth/ante.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tm2/pkg/sdk/auth/ante.go b/tm2/pkg/sdk/auth/ante.go index d00a38925d3..b228388dfad 100644 --- a/tm2/pkg/sdk/auth/ante.go +++ b/tm2/pkg/sdk/auth/ante.go @@ -148,7 +148,8 @@ func NewAnteHandler(ak AccountKeeper, bank BankKeeperI, sigGasConsumer Signature sacc := signerAccs[i] // Do not check signatures if the chainID is dev (gnodev) if we decide to verify the genesis signatures for // gnodev the initialization would take longer - if isGenesis && (!opts.VerifyGenesisSignatures || newCtx.ChainID() == "dev") { + // tendermint_test to skip verification on tests + if isGenesis && (!opts.VerifyGenesisSignatures || newCtx.ChainID() == "dev" || newCtx.ChainID() == "tendermint_test") { // No signatures are needed for genesis. } else { // Check signature From ed1c3c6c749c0d1033fe6c18717e891ed47bee7b Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Tue, 17 Dec 2024 12:29:28 -0500 Subject: [PATCH 11/25] configure genesis transaction verification depending on service --- contribs/gnodev/pkg/dev/node.go | 2 ++ gno.land/pkg/gnoland/app.go | 13 +++++++------ gno.land/pkg/gnoland/node_inmemory.go | 22 ++++++++++++---------- tm2/pkg/sdk/auth/ante.go | 2 +- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/contribs/gnodev/pkg/dev/node.go b/contribs/gnodev/pkg/dev/node.go index fa9e2d11e29..12a88490515 100644 --- a/contribs/gnodev/pkg/dev/node.go +++ b/contribs/gnodev/pkg/dev/node.go @@ -489,6 +489,8 @@ func (n *Node) rebuildNode(ctx context.Context, genesis gnoland.GnoGenesisState) // Speed up stdlib loading after first start (saves about 2-3 seconds on each reload). nodeConfig.CacheStdlibLoad = true nodeConfig.Genesis.ConsensusParams.Block.MaxGas = n.config.MaxGasPerBlock + // Genesis verification is always false with Gnodev + nodeConfig.SkipGenesisVerification = true // recoverFromError handles panics and converts them to errors. recoverFromError := func() { diff --git a/gno.land/pkg/gnoland/app.go b/gno.land/pkg/gnoland/app.go index a5510533d8e..25ae420984e 100644 --- a/gno.land/pkg/gnoland/app.go +++ b/gno.land/pkg/gnoland/app.go @@ -34,11 +34,12 @@ import ( // AppOptions contains the options to create the gno.land ABCI application. type AppOptions struct { - DB dbm.DB // required - Logger *slog.Logger // required - EventSwitch events.EventSwitch // required - VMOutput io.Writer // optional - InitChainerConfig // options related to InitChainer + DB dbm.DB // required + Logger *slog.Logger // required + EventSwitch events.EventSwitch // required + VMOutput io.Writer // optional + SkipGenesisVerification bool // default to verify genesis transactions + InitChainerConfig // options related to InitChainer } // TestAppOptions provides a "ready" default [AppOptions] for use with @@ -105,7 +106,7 @@ func NewAppWithOptions(cfg *AppOptions) (abci.Application, error) { // Set AnteHandler authOptions := auth.AnteOptions{ - VerifyGenesisSignatures: true, + VerifyGenesisSignatures: !cfg.SkipGenesisVerification, } authAnteHandler := auth.NewAnteHandler( acctKpr, bankKpr, auth.DefaultSigVerificationGasConsumer, authOptions) diff --git a/gno.land/pkg/gnoland/node_inmemory.go b/gno.land/pkg/gnoland/node_inmemory.go index f42166411c8..5f8ff6a283e 100644 --- a/gno.land/pkg/gnoland/node_inmemory.go +++ b/gno.land/pkg/gnoland/node_inmemory.go @@ -20,11 +20,12 @@ import ( ) type InMemoryNodeConfig struct { - PrivValidator bft.PrivValidator // identity of the validator - Genesis *bft.GenesisDoc - TMConfig *tmcfg.Config - DB *memdb.MemDB // will be initialized if nil - VMOutput io.Writer // optional + PrivValidator bft.PrivValidator // identity of the validator + Genesis *bft.GenesisDoc + TMConfig *tmcfg.Config + DB *memdb.MemDB // will be initialized if nil + VMOutput io.Writer // optional + SkipGenesisVerification bool // If StdlibDir not set, then it's filepath.Join(TMConfig.RootDir, "gnovm", "stdlibs") InitChainerConfig @@ -112,11 +113,12 @@ func NewInMemoryNode(logger *slog.Logger, cfg *InMemoryNodeConfig) (*node.Node, // Initialize the application with the provided options gnoApp, err := NewAppWithOptions(&AppOptions{ - Logger: logger, - DB: cfg.DB, - EventSwitch: evsw, - InitChainerConfig: cfg.InitChainerConfig, - VMOutput: cfg.VMOutput, + Logger: logger, + DB: cfg.DB, + EventSwitch: evsw, + InitChainerConfig: cfg.InitChainerConfig, + VMOutput: cfg.VMOutput, + SkipGenesisVerification: cfg.SkipGenesisVerification, }) if err != nil { return nil, fmt.Errorf("error initializing new app: %w", err) diff --git a/tm2/pkg/sdk/auth/ante.go b/tm2/pkg/sdk/auth/ante.go index 6e5adc0c7cf..0321658f349 100644 --- a/tm2/pkg/sdk/auth/ante.go +++ b/tm2/pkg/sdk/auth/ante.go @@ -149,7 +149,7 @@ func NewAnteHandler(ak AccountKeeper, bank BankKeeperI, sigGasConsumer Signature // Do not check signatures if the chainID is dev (gnodev) if we decide to verify the genesis signatures for // gnodev the initialization would take longer // tendermint_test to skip verification on tests - if isGenesis && (!opts.VerifyGenesisSignatures || newCtx.ChainID() == "dev" || newCtx.ChainID() == "tendermint_test") { + if isGenesis && !opts.VerifyGenesisSignatures { // No signatures are needed for genesis. } else { // Check signature From 8dfb9bfa7401b74057fdea0f9f056137f2a034af Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Tue, 17 Dec 2024 16:45:53 -0500 Subject: [PATCH 12/25] Fix tests --- .../pkg/integration/testing_integration.go | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index ce1413134e3..9122afd9ba4 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -188,8 +188,10 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { pkgs := ts.Value(envKeyPkgsLoader).(*pkgsLoader) // grab logger creator := crypto.MustAddressFromString(DefaultAccount_Address) // test1 defaultFee := std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000))) + // Generate config and node + cfg := TestingMinimalNodeConfig(t, gnoRootDir) // we need to define a new err1 otherwise the out err would be shadowed in the case "start": - pkgsTxs, loadErr := pkgs.LoadPackages(creator, defaultFee, nil) + pkgsTxs, loadErr := pkgs.LoadPackages(creator, defaultFee, nil, cfg.TMConfig.ChainID()) if loadErr != nil { ts.Fatalf("unable to load packages txs: %s", err) @@ -198,8 +200,6 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { // Warp up `ts` so we can pass it to other testing method t := TSTestingT(ts) - // Generate config and node - cfg := TestingMinimalNodeConfig(t, gnoRootDir) genesis := ts.Value(envKeyGenesis).(*gnoland.GnoGenesisState) genesis.Txs = append(pkgsTxs, genesis.Txs...) @@ -666,7 +666,12 @@ func (pl *pkgsLoader) SetPatch(replace, with string) { pl.patchs[replace] = with } -func (pl *pkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std.Coins) ([]gnoland.TxWithMetadata, error) { +func (pl *pkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std.Coins, chainID string) ([]gnoland.TxWithMetadata, error) { + kb := keys.NewInMemory() + _, err := kb.CreateAccount(DefaultAccount_Name, DefaultAccount_Seed, "", "", 0, 0) + if err != nil { + return nil, fmt.Errorf("createAccount: %w", err) + } pkgslist, err := pl.List().Sort() // sorts packages by their dependencies. if err != nil { return nil, fmt.Errorf("unable to sort packages: %w", err) @@ -698,7 +703,20 @@ func (pl *pkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std } } } - + signbytes, err := tx.GetSignBytes(chainID, 0, 0) + if err != nil { + return nil, fmt.Errorf("unable to get sign bytes %q: %w", pkg.Name, err) + } + signature, pubKey, err := kb.Sign(DefaultAccount_Name, "", signbytes) + if err != nil { + return nil, fmt.Errorf("unable to sign transaction %q: %w", pkg.Name, err) + } + tx.Signatures = []std.Signature{ + { + PubKey: pubKey, + Signature: signature, + }, + } txs[i] = gnoland.TxWithMetadata{ Tx: tx, } From c72fe3cca319beae66c24de732e985edffb62a9d Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Wed, 18 Dec 2024 16:05:13 -0500 Subject: [PATCH 13/25] fix txtar tests --- .../gnoland/testdata/event_multi_msg.txtar | 9 ++++++--- .../gnoland/testdata/gnokey_simulate.txtar | 20 +++++++++---------- .../gnoland/testdata/gnoweb_airgapped.txtar | 9 ++++++--- .../testdata/restart_missing_type.txtar | 6 +++--- .../cmd/gnoland/testdata/simulate_gas.txtar | 4 ++-- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/gno.land/cmd/gnoland/testdata/event_multi_msg.txtar b/gno.land/cmd/gnoland/testdata/event_multi_msg.txtar index 84afe3cc6a4..13a448e7f8c 100644 --- a/gno.land/cmd/gnoland/testdata/event_multi_msg.txtar +++ b/gno.land/cmd/gnoland/testdata/event_multi_msg.txtar @@ -11,16 +11,19 @@ stdout 'data: {' stdout ' "BaseAccount": {' stdout ' "address": "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",' stdout ' "coins": "[0-9]*ugnot",' # dynamic -stdout ' "public_key": null,' +stdout ' "public_key": {' +stdout ' "@type": "/tm.PubKeySecp256k1",' +stdout ' "value": "A\+FhNtsXHjLfSJk1lB8FbiL4mGPjc50Kt81J7EKDnJ2y"' +stdout ' },' stdout ' "account_number": "0",' -stdout ' "sequence": "0"' +stdout ' "sequence": "1"' stdout ' }' stdout '}' ! stderr '.+' # empty ## sign -gnokey sign -tx-path $WORK/multi/multi_msg.tx -chainid=tendermint_test -account-number 0 -account-sequence 0 test1 +gnokey sign -tx-path $WORK/multi/multi_msg.tx -chainid=tendermint_test -account-number 0 -account-sequence 1 test1 stdout 'Tx successfully signed and saved to ' ## broadcast diff --git a/gno.land/cmd/gnoland/testdata/gnokey_simulate.txtar b/gno.land/cmd/gnoland/testdata/gnokey_simulate.txtar index 8db2c7302fc..db3cd527eb3 100644 --- a/gno.land/cmd/gnoland/testdata/gnokey_simulate.txtar +++ b/gno.land/cmd/gnoland/testdata/gnokey_simulate.txtar @@ -7,41 +7,41 @@ gnoland start # Initial state: assert that sequence == 0. gnokey query auth/accounts/$USER_ADDR_test1 -stdout '"sequence": "0"' +stdout '"sequence": "1"' # attempt adding the "test" package. # the package has a syntax error; simulation should catch this ahead of time and prevent the tx. # -simulate test ! gnokey maketx addpkg -pkgdir $WORK/test -pkgpath gno.land/r/test -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test -simulate test test1 gnokey query auth/accounts/$USER_ADDR_test1 -stdout '"sequence": "0"' +stdout '"sequence": "1"' # -simulate only ! gnokey maketx addpkg -pkgdir $WORK/test -pkgpath gno.land/r/test -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test -simulate only test1 gnokey query auth/accounts/$USER_ADDR_test1 -stdout '"sequence": "0"' +stdout '"sequence": "1"' # -simulate skip ! gnokey maketx addpkg -pkgdir $WORK/test -pkgpath gno.land/r/test -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test -simulate skip test1 gnokey query auth/accounts/$USER_ADDR_test1 -stdout '"sequence": "1"' +stdout '"sequence": "2"' # attempt calling hello.SetName correctly. # -simulate test and skip should do it successfully, -simulate only should not. # -simulate test gnokey maketx call -pkgpath gno.land/r/hello -func SetName -args John -gas-wanted 2000000 -gas-fee 1000000ugnot -broadcast -chainid tendermint_test -simulate test test1 gnokey query auth/accounts/$USER_ADDR_test1 -stdout '"sequence": "2"' +stdout '"sequence": "3"' gnokey query vm/qeval --data "gno.land/r/hello.Hello()" stdout 'Hello, John!' # -simulate only gnokey maketx call -pkgpath gno.land/r/hello -func SetName -args Paul -gas-wanted 2000000 -gas-fee 1000000ugnot -broadcast -chainid tendermint_test -simulate only test1 gnokey query auth/accounts/$USER_ADDR_test1 -stdout '"sequence": "2"' +stdout '"sequence": "3"' gnokey query vm/qeval --data "gno.land/r/hello.Hello()" stdout 'Hello, John!' # -simulate skip gnokey maketx call -pkgpath gno.land/r/hello -func SetName -args George -gas-wanted 2000000 -gas-fee 1000000ugnot -broadcast -chainid tendermint_test -simulate skip test1 gnokey query auth/accounts/$USER_ADDR_test1 -stdout '"sequence": "3"' +stdout '"sequence": "4"' gnokey query vm/qeval --data "gno.land/r/hello.Hello()" stdout 'Hello, George!' @@ -51,19 +51,19 @@ stdout 'Hello, George!' # -simulate test ! gnokey maketx call -pkgpath gno.land/r/hello -func Grumpy -gas-wanted 2000000 -gas-fee 1000000ugnot -broadcast -chainid tendermint_test -simulate test test1 gnokey query auth/accounts/$USER_ADDR_test1 -stdout '"sequence": "3"' +stdout '"sequence": "4"' gnokey query vm/qeval --data "gno.land/r/hello.Hello()" stdout 'Hello, George!' # -simulate only ! gnokey maketx call -pkgpath gno.land/r/hello -func Grumpy -gas-wanted 2000000 -gas-fee 1000000ugnot -broadcast -chainid tendermint_test -simulate only test1 gnokey query auth/accounts/$USER_ADDR_test1 -stdout '"sequence": "3"' +stdout '"sequence": "4"' gnokey query vm/qeval --data "gno.land/r/hello.Hello()" stdout 'Hello, George!' # -simulate skip ! gnokey maketx call -pkgpath gno.land/r/hello -func Grumpy -gas-wanted 2000000 -gas-fee 1000000ugnot -broadcast -chainid tendermint_test -simulate skip test1 gnokey query auth/accounts/$USER_ADDR_test1 -stdout '"sequence": "4"' +stdout '"sequence": "5"' gnokey query vm/qeval --data "gno.land/r/hello.Hello()" stdout 'Hello, George!' diff --git a/gno.land/cmd/gnoland/testdata/gnoweb_airgapped.txtar b/gno.land/cmd/gnoland/testdata/gnoweb_airgapped.txtar index 3ed35a1b1d3..02bd8058214 100644 --- a/gno.land/cmd/gnoland/testdata/gnoweb_airgapped.txtar +++ b/gno.land/cmd/gnoland/testdata/gnoweb_airgapped.txtar @@ -14,9 +14,12 @@ stdout 'data: {' stdout ' "BaseAccount": {' stdout ' "address": "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",' stdout ' "coins": "[0-9]*ugnot",' # dynamic -stdout ' "public_key": null,' +stdout ' "public_key": {' +stdout ' "@type": "/tm.PubKeySecp256k1",' +stdout ' "value": "A\+FhNtsXHjLfSJk1lB8FbiL4mGPjc50Kt81J7EKDnJ2y"' +stdout ' },' stdout ' "account_number": "0",' -stdout ' "sequence": "0"' +stdout ' "sequence": "4"' stdout ' }' stdout '}' ! stderr '.+' # empty @@ -26,7 +29,7 @@ gnokey maketx call -pkgpath "gno.land/r/demo/echo" -func "Render" -gas-fee 10000 cp stdout call.tx # Sign -gnokey sign -tx-path $WORK/call.tx -chainid "tendermint_test" -account-number 0 -account-sequence 0 test1 +gnokey sign -tx-path $WORK/call.tx -chainid "tendermint_test" -account-number 0 -account-sequence 4 test1 cmpenv stdout sign.stdout.golden gnokey broadcast $WORK/call.tx diff --git a/gno.land/cmd/gnoland/testdata/restart_missing_type.txtar b/gno.land/cmd/gnoland/testdata/restart_missing_type.txtar index b02acc16d96..09e1a27d6f4 100644 --- a/gno.land/cmd/gnoland/testdata/restart_missing_type.txtar +++ b/gno.land/cmd/gnoland/testdata/restart_missing_type.txtar @@ -5,15 +5,15 @@ loadpkg gno.land/p/demo/avl gnoland start -gnokey sign -tx-path $WORK/tx1.tx -chainid tendermint_test -account-sequence 0 test1 +gnokey sign -tx-path $WORK/tx1.tx -chainid tendermint_test -account-sequence 1 test1 ! gnokey broadcast $WORK/tx1.tx stderr 'out of gas' -gnokey sign -tx-path $WORK/tx2.tx -chainid tendermint_test -account-sequence 1 test1 +gnokey sign -tx-path $WORK/tx2.tx -chainid tendermint_test -account-sequence 2 test1 gnokey broadcast $WORK/tx2.tx stdout 'OK!' -gnokey sign -tx-path $WORK/tx3.tx -chainid tendermint_test -account-sequence 2 test1 +gnokey sign -tx-path $WORK/tx3.tx -chainid tendermint_test -account-sequence 3 test1 gnokey broadcast $WORK/tx3.tx stdout 'OK!' diff --git a/gno.land/cmd/gnoland/testdata/simulate_gas.txtar b/gno.land/cmd/gnoland/testdata/simulate_gas.txtar index 8550419f205..4c5213da345 100644 --- a/gno.land/cmd/gnoland/testdata/simulate_gas.txtar +++ b/gno.land/cmd/gnoland/testdata/simulate_gas.txtar @@ -6,11 +6,11 @@ gnoland start # simulate only gnokey maketx call -pkgpath gno.land/r/simulate -func Hello -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test -simulate only test1 -stdout 'GAS USED: 96411' +stdout 'GAS USED: 99015' # simulate skip gnokey maketx call -pkgpath gno.land/r/simulate -func Hello -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test -simulate skip test1 -stdout 'GAS USED: 96411' # same as simulate only +stdout 'GAS USED: 99015' # same as simulate only -- package/package.gno -- From d0715e9f75c7560b020d183aceae70c058f4eef1 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Wed, 18 Dec 2024 16:12:52 -0500 Subject: [PATCH 14/25] fix TestCallSingle_Integration test --- gno.land/pkg/integration/testing_node.go | 1 + 1 file changed, 1 insertion(+) diff --git a/gno.land/pkg/integration/testing_node.go b/gno.land/pkg/integration/testing_node.go index 7eaf3457b03..46d2956186b 100644 --- a/gno.land/pkg/integration/testing_node.go +++ b/gno.land/pkg/integration/testing_node.go @@ -93,6 +93,7 @@ func TestingMinimalNodeConfig(t TestingTS, gnoroot string) *gnoland.InMemoryNode GenesisTxResultHandler: gnoland.PanicOnFailingTxResultHandler, CacheStdlibLoad: true, }, + SkipGenesisVerification: true, } } From 2bddc48c4d920db5301cfc3b22a3a2e9890caab1 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Thu, 19 Dec 2024 09:57:26 -0500 Subject: [PATCH 15/25] Fix tests --- gno.land/pkg/gnoland/app.go | 1 + gno.land/pkg/integration/testing_node.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/gno.land/pkg/gnoland/app.go b/gno.land/pkg/gnoland/app.go index 25ae420984e..9319933efcb 100644 --- a/gno.land/pkg/gnoland/app.go +++ b/gno.land/pkg/gnoland/app.go @@ -54,6 +54,7 @@ func TestAppOptions(db dbm.DB) *AppOptions { StdlibDir: filepath.Join(gnoenv.RootDir(), "gnovm", "stdlibs"), CacheStdlibLoad: true, }, + SkipGenesisVerification: true, } } diff --git a/gno.land/pkg/integration/testing_node.go b/gno.land/pkg/integration/testing_node.go index 46d2956186b..e06277f1ad2 100644 --- a/gno.land/pkg/integration/testing_node.go +++ b/gno.land/pkg/integration/testing_node.go @@ -56,6 +56,7 @@ func TestingInMemoryNode(t TestingTS, logger *slog.Logger, config *gnoland.InMem // It will return the default creator address of the loaded packages. func TestingNodeConfig(t TestingTS, gnoroot string, additionalTxs ...gnoland.TxWithMetadata) (*gnoland.InMemoryNodeConfig, bft.Address) { cfg := TestingMinimalNodeConfig(t, gnoroot) + cfg.SkipGenesisVerification = true creator := crypto.MustAddressFromString(DefaultAccount_Address) // test1 @@ -93,7 +94,6 @@ func TestingMinimalNodeConfig(t TestingTS, gnoroot string) *gnoland.InMemoryNode GenesisTxResultHandler: gnoland.PanicOnFailingTxResultHandler, CacheStdlibLoad: true, }, - SkipGenesisVerification: true, } } From 8b27abe41c412cc402125e3e4921f523fec98d9d Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Thu, 19 Dec 2024 10:02:11 -0500 Subject: [PATCH 16/25] add comment on gnoHome config --- contribs/gnogenesis/internal/txs/txs_add_packages.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contribs/gnogenesis/internal/txs/txs_add_packages.go b/contribs/gnogenesis/internal/txs/txs_add_packages.go index 2446f898a65..7c4901b7abf 100644 --- a/contribs/gnogenesis/internal/txs/txs_add_packages.go +++ b/contribs/gnogenesis/internal/txs/txs_add_packages.go @@ -26,7 +26,7 @@ var genesisDeployFee = std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(100 type addPkgCfg struct { txsCfg *txsCfg keyName string - gnoHome string + gnoHome string // default GNOHOME env var, just here to ease testing with parallel tests insecurePasswordStdin bool } From 3c887a9d536b25c67fac1e8d9acb88e9ecf76bb4 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Fri, 20 Dec 2024 11:00:48 -0500 Subject: [PATCH 17/25] make tidy --- contribs/gnogenesis/go.mod | 2 +- contribs/gnogenesis/go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/contribs/gnogenesis/go.mod b/contribs/gnogenesis/go.mod index a9b5d6afd63..aa3cb14c19b 100644 --- a/contribs/gnogenesis/go.mod +++ b/contribs/gnogenesis/go.mod @@ -47,7 +47,7 @@ require ( go.opentelemetry.io/proto/otlp v1.3.1 // indirect go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.27.0 // indirect - go.uber.org/zap/exp v0.2.0 // indirect + go.uber.org/zap/exp v0.3.0 // indirect golang.org/x/crypto v0.26.0 // indirect golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect golang.org/x/mod v0.20.0 // indirect diff --git a/contribs/gnogenesis/go.sum b/contribs/gnogenesis/go.sum index d1b75fe6eb1..24a4186ce48 100644 --- a/contribs/gnogenesis/go.sum +++ b/contribs/gnogenesis/go.sum @@ -159,8 +159,8 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= -go.uber.org/zap/exp v0.2.0 h1:FtGenNNeCATRB3CmB/yEUnjEFeJWpB/pMcy7e2bKPYs= -go.uber.org/zap/exp v0.2.0/go.mod h1:t0gqAIdh1MfKv9EwN/dLwfZnJxe9ITAZN78HEWPFWDQ= +go.uber.org/zap/exp v0.3.0 h1:6JYzdifzYkGmTdRR59oYH+Ng7k49H9qVpWwNSsGJj3U= +go.uber.org/zap/exp v0.3.0/go.mod h1:5I384qq7XGxYyByIhHm6jg5CHkGY0nsTfbDLgDDlgJQ= golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= From c967c2f43104f565d44d7dcf789cfb7cdf1798e5 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Mon, 6 Jan 2025 20:43:53 +0100 Subject: [PATCH 18/25] Solve comments --- contribs/gnogenesis/go.mod | 2 -- contribs/gnogenesis/go.sum | 6 ------ contribs/gnogenesis/internal/txs/txs_add_packages.go | 11 ++++++++--- .../gnogenesis/internal/txs/txs_add_packages_test.go | 2 -- misc/autocounterd/go.sum | 4 ---- 5 files changed, 8 insertions(+), 17 deletions(-) diff --git a/contribs/gnogenesis/go.mod b/contribs/gnogenesis/go.mod index aa3cb14c19b..cc4429f44e3 100644 --- a/contribs/gnogenesis/go.mod +++ b/contribs/gnogenesis/go.mod @@ -46,8 +46,6 @@ require ( go.opentelemetry.io/otel/trace v1.29.0 // indirect go.opentelemetry.io/proto/otlp v1.3.1 // indirect go.uber.org/multierr v1.11.0 // indirect - go.uber.org/zap v1.27.0 // indirect - go.uber.org/zap/exp v0.3.0 // indirect golang.org/x/crypto v0.26.0 // indirect golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect golang.org/x/mod v0.20.0 // indirect diff --git a/contribs/gnogenesis/go.sum b/contribs/gnogenesis/go.sum index 24a4186ce48..cc190bef54c 100644 --- a/contribs/gnogenesis/go.sum +++ b/contribs/gnogenesis/go.sum @@ -153,14 +153,8 @@ go.opentelemetry.io/otel/trace v1.29.0 h1:J/8ZNK4XgR7a21DZUAsbF8pZ5Jcw1VhACmnYt3 go.opentelemetry.io/otel/trace v1.29.0/go.mod h1:eHl3w0sp3paPkYstJOmAimxhiFXPg+MMTlEh3nsQgWQ= go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0= go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8= -go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= -go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= -go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= -go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= -go.uber.org/zap/exp v0.3.0 h1:6JYzdifzYkGmTdRR59oYH+Ng7k49H9qVpWwNSsGJj3U= -go.uber.org/zap/exp v0.3.0/go.mod h1:5I384qq7XGxYyByIhHm6jg5CHkGY0nsTfbDLgDDlgJQ= golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= diff --git a/contribs/gnogenesis/internal/txs/txs_add_packages.go b/contribs/gnogenesis/internal/txs/txs_add_packages.go index 7c4901b7abf..14e18d49b1c 100644 --- a/contribs/gnogenesis/internal/txs/txs_add_packages.go +++ b/contribs/gnogenesis/internal/txs/txs_add_packages.go @@ -35,7 +35,7 @@ func (c *addPkgCfg) RegisterFlags(fs *flag.FlagSet) { &c.keyName, "key-name", "", - "The package deployer key name or address", + "The package deployer key name or address contained on gnokey", ) fs.StringVar( @@ -99,10 +99,11 @@ func execTxsAddPackages( return fmt.Errorf("unable to get signer info, %w", err) } + creator := info.GetAddress() parsedTxs := make([]gnoland.TxWithMetadata, 0) for _, path := range args { // Generate transactions from the packages (recursively) - txs, err := gnoland.LoadPackagesFromDir(path, info.GetAddress(), genesisDeployFee) + txs, err := gnoland.LoadPackagesFromDir(path, creator, genesisDeployFee) if err != nil { return fmt.Errorf("unable to load txs from directory, %w", err) } @@ -134,6 +135,7 @@ func execTxsAddPackages( func signTxs(txs []gnoland.TxWithMetadata, signer *gnoclient.SignerFromKeybase) error { for index, tx := range txs { + // Here accountNumber and sequenceNumber are set to 0 because they are considered as 0 on genesis transactions. signBytes, err := tx.Tx.GetSignBytes(signer.ChainID, 0, 0) if err != nil { return fmt.Errorf("unable to load txs from directory, %w", err) @@ -173,7 +175,10 @@ func signerWithConfig(cfg *addPkgCfg, io commands.IO, chainID string) (*gnoclien } } else { kb = keys.NewInMemory() - kb.CreateAccount(integration.DefaultAccount_Name, integration.DefaultAccount_Seed, "", "", 0, 0) + _, err = kb.CreateAccount(integration.DefaultAccount_Name, integration.DefaultAccount_Seed, "", "", 0, 0) + if err != nil { + return nil, fmt.Errorf("unable to create account: %w", err) + } } return &gnoclient.SignerFromKeybase{ diff --git a/contribs/gnogenesis/internal/txs/txs_add_packages_test.go b/contribs/gnogenesis/internal/txs/txs_add_packages_test.go index 5dbe1b7dc9d..30e8364f28e 100644 --- a/contribs/gnogenesis/internal/txs/txs_add_packages_test.go +++ b/contribs/gnogenesis/internal/txs/txs_add_packages_test.go @@ -100,7 +100,6 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { // Run the command cmdErr := cmd.ParseAndRun(context.Background(), args) - fmt.Println(cmdErr.Error()) assert.ErrorContains(t, cmdErr, "Key "+keyname+" not found") }) @@ -156,7 +155,6 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { // Run the command cmdErr := cmd.ParseAndRun(context.Background(), args) - fmt.Println(cmdErr.Error()) assert.ErrorContains(t, cmdErr, "unable to sign txs, unable sign tx invalid account password") }) diff --git a/misc/autocounterd/go.sum b/misc/autocounterd/go.sum index 28959bf214e..9fdeec5ce70 100644 --- a/misc/autocounterd/go.sum +++ b/misc/autocounterd/go.sum @@ -155,10 +155,6 @@ go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeX go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= -go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= -go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= -go.uber.org/zap/exp v0.3.0 h1:6JYzdifzYkGmTdRR59oYH+Ng7k49H9qVpWwNSsGJj3U= -go.uber.org/zap/exp v0.3.0/go.mod h1:5I384qq7XGxYyByIhHm6jg5CHkGY0nsTfbDLgDDlgJQ= golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= From 09dce4322f68e8f7aa366c3c33da287d5761d8ae Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Tue, 7 Jan 2025 21:24:58 +0100 Subject: [PATCH 19/25] fix more comments :) --- .../internal/txs/txs_add_packages.go | 90 ++++++++----------- .../internal/txs/txs_add_packages_test.go | 21 +++-- tm2/pkg/sdk/auth/ante.go | 9 +- 3 files changed, 56 insertions(+), 64 deletions(-) diff --git a/contribs/gnogenesis/internal/txs/txs_add_packages.go b/contribs/gnogenesis/internal/txs/txs_add_packages.go index 14e18d49b1c..d2e7fd887b3 100644 --- a/contribs/gnogenesis/internal/txs/txs_add_packages.go +++ b/contribs/gnogenesis/internal/txs/txs_add_packages.go @@ -9,15 +9,19 @@ import ( "github.com/gnolang/gno/tm2/pkg/crypto/keys" - "github.com/gnolang/gno/gno.land/pkg/gnoclient" "github.com/gnolang/gno/gno.land/pkg/gnoland" "github.com/gnolang/gno/gno.land/pkg/gnoland/ugnot" - "github.com/gnolang/gno/gno.land/pkg/integration" "github.com/gnolang/gno/tm2/pkg/bft/types" "github.com/gnolang/gno/tm2/pkg/commands" "github.com/gnolang/gno/tm2/pkg/std" ) +const ( + DefaultAccount_Name = "test1" + DefaultAccount_Address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" + DefaultAccount_Seed = "source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast" +) + var errInvalidPackageDir = errors.New("invalid package directory") // Keep in sync with gno.land/cmd/start.go @@ -78,10 +82,15 @@ func execTxsAddPackages( io commands.IO, args []string, ) error { + var ( + keyname = DefaultAccount_Name + keybase keys.Keybase + pass string + ) // Load the genesis - genesis, loadErr := types.GenesisDocFromFile(cfg.txsCfg.GenesisPath) - if loadErr != nil { - return fmt.Errorf("unable to load genesis, %w", loadErr) + genesis, err := types.GenesisDocFromFile(cfg.txsCfg.GenesisPath) + if err != nil { + return fmt.Errorf("unable to load genesis, %w", err) } // Make sure the package dir is set @@ -89,14 +98,27 @@ func execTxsAddPackages( return errInvalidPackageDir } - signer, err := signerWithConfig(cfg, io, genesis.ChainID) - if err != nil { - return fmt.Errorf("unable to load signer, %w", err) + if cfg.keyName != "" { + keyname = cfg.keyName + keybase, err = keys.NewKeyBaseFromDir(cfg.gnoHome) + if err != nil { + return fmt.Errorf("unable to load keybase: %w", err) + } + pass, err = io.GetPassword("Enter password.", cfg.insecurePasswordStdin) + if err != nil { + return fmt.Errorf("cannot read password: %w", err) + } + } else { + keybase = keys.NewInMemory() + _, err := keybase.CreateAccount(DefaultAccount_Name, DefaultAccount_Seed, "", "", 0, 0) + if err != nil { + return fmt.Errorf("unable to create account: %w", err) + } } - info, err := signer.Info() + info, err := keybase.GetByNameOrAddress(keyname) if err != nil { - return fmt.Errorf("unable to get signer info, %w", err) + return err } creator := info.GetAddress() @@ -108,7 +130,7 @@ func execTxsAddPackages( return fmt.Errorf("unable to load txs from directory, %w", err) } - if err := signTxs(txs, signer); err != nil { + if err := signTxs(txs, keybase, genesis.ChainID, keyname, pass); err != nil { return fmt.Errorf("unable to sign txs, %w", err) } @@ -133,58 +155,24 @@ func execTxsAddPackages( return nil } -func signTxs(txs []gnoland.TxWithMetadata, signer *gnoclient.SignerFromKeybase) error { +func signTxs(txs []gnoland.TxWithMetadata, keybase keys.Keybase, chainID, keyname string, password string) error { for index, tx := range txs { // Here accountNumber and sequenceNumber are set to 0 because they are considered as 0 on genesis transactions. - signBytes, err := tx.Tx.GetSignBytes(signer.ChainID, 0, 0) + signBytes, err := tx.Tx.GetSignBytes(chainID, 0, 0) if err != nil { return fmt.Errorf("unable to load txs from directory, %w", err) } - signature, publicKey, err := signer.Keybase.Sign(signer.Account, signer.Password, signBytes) + signature, publicKey, err := keybase.Sign(keyname, password, signBytes) + if err != nil { + return fmt.Errorf("unable sign tx %w", err) + } txs[index].Tx.Signatures = []std.Signature{ { PubKey: publicKey, Signature: signature, }, } - if err != nil { - return fmt.Errorf("unable sign tx %w", err) - } } return nil } - -func signerWithConfig(cfg *addPkgCfg, io commands.IO, chainID string) (*gnoclient.SignerFromKeybase, error) { - var ( - keyname = integration.DefaultAccount_Name - pass string - kb keys.Keybase - err error - ) - - if cfg.keyName != "" { - keyname = cfg.keyName - kb, err = keys.NewKeyBaseFromDir(cfg.gnoHome) - if err != nil { - return nil, fmt.Errorf("unable to load keybase: %w", err) - } - pass, err = io.GetPassword("Enter password.", cfg.insecurePasswordStdin) - if err != nil { - return nil, fmt.Errorf("cannot read password: %w", err) - } - } else { - kb = keys.NewInMemory() - _, err = kb.CreateAccount(integration.DefaultAccount_Name, integration.DefaultAccount_Seed, "", "", 0, 0) - if err != nil { - return nil, fmt.Errorf("unable to create account: %w", err) - } - } - - return &gnoclient.SignerFromKeybase{ - Account: keyname, - ChainID: chainID, - Keybase: kb, - Password: pass, - }, nil -} diff --git a/contribs/gnogenesis/internal/txs/txs_add_packages_test.go b/contribs/gnogenesis/internal/txs/txs_add_packages_test.go index 30e8364f28e..d13ae9d236b 100644 --- a/contribs/gnogenesis/internal/txs/txs_add_packages_test.go +++ b/contribs/gnogenesis/internal/txs/txs_add_packages_test.go @@ -2,6 +2,7 @@ package txs import ( "context" + "encoding/hex" "fmt" "os" "path/filepath" @@ -63,7 +64,7 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { assert.ErrorContains(t, cmdErr, errInvalidPackageDir.Error()) }) - t.Run("nonExistentKeyName", func(t *testing.T) { + t.Run("non existent key", func(t *testing.T) { t.Parallel() keybaseDir := t.TempDir() keyname := "beep-boop" @@ -103,7 +104,7 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { assert.ErrorContains(t, cmdErr, "Key "+keyname+" not found") }) - t.Run("existentKeyBadPassword", func(t *testing.T) { + t.Run("existent key wrong password", func(t *testing.T) { t.Parallel() tempGenesis, cleanup := testutils.NewTestFile(t) @@ -155,10 +156,10 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { // Run the command cmdErr := cmd.ParseAndRun(context.Background(), args) - assert.ErrorContains(t, cmdErr, "unable to sign txs, unable sign tx invalid account password") + assert.Equal(t, cmdErr.Error(), "unable to sign txs, unable sign tx invalid account password") }) - t.Run("existentKeyOKPassword", func(t *testing.T) { + t.Run("existent key correct password", func(t *testing.T) { t.Parallel() tempGenesis, cleanup := testutils.NewTestFile(t) @@ -178,9 +179,7 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { // Create key kb, err := keys.NewKeyBaseFromDir(keybaseDir) require.NoError(t, err) - mnemonic, err := client.GenerateMnemonic(256) - require.NoError(t, err) - _, err = kb.CreateAccount(keyname, mnemonic, "", password, 0, 0) + _, err = kb.CreateAccount(keyname, DefaultAccount_Seed, "", password, 0, 0) require.NoError(t, err) io := commands.NewTestIO() @@ -225,10 +224,12 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { msgAddPkg, ok := state.Txs[0].Tx.Msgs[0].(vmm.MsgAddPackage) require.True(t, ok) + require.Equal(t, "gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pq0skzdkmzu0r9h6gny6eg8c9dc303xrrudee6z4he4y7cs5rnjwmyf40yaj", state.Txs[0].Tx.Signatures[0].PubKey.String()) + require.Equal(t, "cfe5a15d8def04cbdaf9d08e2511db7928152b26419c4577cbfa282c83118852411f3de5d045ce934555572c21bda8042ce5c64b793a01748e49cf2cff7c2983", hex.EncodeToString(state.Txs[0].Tx.Signatures[0].Signature)) assert.Equal(t, packagePath, msgAddPkg.Package.Path) }) - t.Run("okDefaultAccount", func(t *testing.T) { + t.Run("ok default key", func(t *testing.T) { t.Parallel() tempGenesis, cleanup := testutils.NewTestFile(t) @@ -273,6 +274,8 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { msgAddPkg, ok := state.Txs[0].Tx.Msgs[0].(vmm.MsgAddPackage) require.True(t, ok) + require.Equal(t, "gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pq0skzdkmzu0r9h6gny6eg8c9dc303xrrudee6z4he4y7cs5rnjwmyf40yaj", state.Txs[0].Tx.Signatures[0].PubKey.String()) + require.Equal(t, "cfe5a15d8def04cbdaf9d08e2511db7928152b26419c4577cbfa282c83118852411f3de5d045ce934555572c21bda8042ce5c64b793a01748e49cf2cff7c2983", hex.EncodeToString(state.Txs[0].Tx.Signatures[0].Signature)) assert.Equal(t, packagePath, msgAddPkg.Package.Path) }) @@ -319,6 +322,8 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { msgAddPkg, ok := state.Txs[0].Tx.Msgs[0].(vmm.MsgAddPackage) require.True(t, ok) + require.Equal(t, "gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pq0skzdkmzu0r9h6gny6eg8c9dc303xrrudee6z4he4y7cs5rnjwmyf40yaj", state.Txs[0].Tx.Signatures[0].PubKey.String()) + require.Equal(t, "cfe5a15d8def04cbdaf9d08e2511db7928152b26419c4577cbfa282c83118852411f3de5d045ce934555572c21bda8042ce5c64b793a01748e49cf2cff7c2983", hex.EncodeToString(state.Txs[0].Tx.Signatures[0].Signature)) assert.Equal(t, packagePath, msgAddPkg.Package.Path) }) diff --git a/tm2/pkg/sdk/auth/ante.go b/tm2/pkg/sdk/auth/ante.go index 66f25900dc1..fee290f7235 100644 --- a/tm2/pkg/sdk/auth/ante.go +++ b/tm2/pkg/sdk/auth/ante.go @@ -146,9 +146,6 @@ func NewAnteHandler(ak AccountKeeper, bank BankKeeperI, sigGasConsumer Signature // check signature, return account with incremented nonce sacc := signerAccs[i] - // Do not check signatures if the chainID is dev (gnodev) if we decide to verify the genesis signatures for - // gnodev the initialization would take longer - // tendermint_test to skip verification on tests if isGenesis && !opts.VerifyGenesisSignatures { // No signatures are needed for genesis. } else { @@ -421,8 +418,10 @@ func SetGasMeter(simulate bool, ctx sdk.Context, gasLimit int64) sdk.Context { // GetSignBytes returns a slice of bytes to sign over for a given transaction // and an account. func GetSignBytes(chainID string, tx std.Tx, acc std.Account, genesis bool) ([]byte, error) { - var accNum uint64 - var accSequence uint64 + var ( + accNum uint64 + accSequence uint64 + ) if !genesis { accNum = acc.GetAccountNumber() accSequence = acc.GetSequence() From 9f59ccb33fba5a34143be063e7948468e82df4ef Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Tue, 7 Jan 2025 21:28:36 +0100 Subject: [PATCH 20/25] make tidy --- contribs/gnogenesis/go.mod | 2 -- contribs/gnogenesis/go.sum | 2 -- 2 files changed, 4 deletions(-) diff --git a/contribs/gnogenesis/go.mod b/contribs/gnogenesis/go.mod index 38031f9ca14..3056af1d4cc 100644 --- a/contribs/gnogenesis/go.mod +++ b/contribs/gnogenesis/go.mod @@ -30,7 +30,6 @@ require ( github.com/peterbourgon/ff/v3 v3.4.0 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/rs/cors v1.11.1 // indirect github.com/rs/xid v1.6.0 // indirect github.com/sig-0/insertion-queue v0.0.0-20241004125609-6b3ca841346b // indirect @@ -55,7 +54,6 @@ require ( golang.org/x/sys v0.24.0 // indirect golang.org/x/term v0.23.0 // indirect golang.org/x/text v0.17.0 // indirect - golang.org/x/tools v0.24.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20240822170219-fc7c04adadcd // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd // indirect google.golang.org/grpc v1.65.0 // indirect diff --git a/contribs/gnogenesis/go.sum b/contribs/gnogenesis/go.sum index bd88dd5d08c..7e4a683cad1 100644 --- a/contribs/gnogenesis/go.sum +++ b/contribs/gnogenesis/go.sum @@ -195,8 +195,6 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24= -golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= From e33600fea9340c0595c1087876d47ad5c38eb239 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Tue, 7 Jan 2025 22:08:14 +0100 Subject: [PATCH 21/25] sign load packages on integration tests --- gno.land/pkg/gnoclient/integration_test.go | 1 - gno.land/pkg/integration/defaultsigner.go | 32 ++++++++++++++++++++++ gno.land/pkg/integration/pkgloader.go | 5 ++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 gno.land/pkg/integration/defaultsigner.go diff --git a/gno.land/pkg/gnoclient/integration_test.go b/gno.land/pkg/gnoclient/integration_test.go index d3d4e0d2c52..6487544b78b 100644 --- a/gno.land/pkg/gnoclient/integration_test.go +++ b/gno.land/pkg/gnoclient/integration_test.go @@ -719,6 +719,5 @@ func loadpkgs(t *testing.T, rootdir string, paths ...string) []gnoland.TxWithMet meta, err := loader.LoadPackages(creator, defaultFee, nil) require.NoError(t, err) - return meta } diff --git a/gno.land/pkg/integration/defaultsigner.go b/gno.land/pkg/integration/defaultsigner.go new file mode 100644 index 00000000000..d4dae5b0f61 --- /dev/null +++ b/gno.land/pkg/integration/defaultsigner.go @@ -0,0 +1,32 @@ +package integration + +import ( + "github.com/gnolang/gno/gno.land/pkg/gnoland" + "github.com/gnolang/gno/tm2/pkg/crypto/keys" + "github.com/gnolang/gno/tm2/pkg/std" +) + +func SignTxs(txs []gnoland.TxWithMetadata, chainID string) error { + kb := keys.NewInMemory() + _, err := kb.CreateAccount(DefaultAccount_Name, DefaultAccount_Seed, "", "", 0, 0) + if err != nil { + return err + } + for index, tx := range txs { + bytes, err := tx.Tx.GetSignBytes(chainID, 0, 0) + if err != nil { + return err + } + signature, publicKey, err := kb.Sign(DefaultAccount_Name, "", bytes) + if err != nil { + return err + } + txs[index].Tx.Signatures = []std.Signature{ + { + PubKey: publicKey, + Signature: signature, + }, + } + } + return nil +} diff --git a/gno.land/pkg/integration/pkgloader.go b/gno.land/pkg/integration/pkgloader.go index 541e24b96eb..dffd4a540bd 100644 --- a/gno.land/pkg/integration/pkgloader.go +++ b/gno.land/pkg/integration/pkgloader.go @@ -77,6 +77,11 @@ func (pl *PkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std } } + err = SignTxs(txs, "tendermint_test") + if err != nil { + return nil, fmt.Errorf("unable to sign txs: %w", err) + } + return txs, nil } From 2a67769b4f165ffd4d9ed62cf27891387fa8b949 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Wed, 8 Jan 2025 20:23:17 +0100 Subject: [PATCH 22/25] improve SignTx function --- .../internal/txs/txs_add_packages.go | 13 ++++---- .../internal/txs/txs_add_packages_test.go | 16 +++++---- gno.land/pkg/integration/defaultsigner.go | 32 ------------------ gno.land/pkg/integration/pkgloader.go | 5 +-- gno.land/pkg/integration/signer.go | 33 +++++++++++++++++++ .../pkg/integration/testscript_gnoland.go | 5 ++- 6 files changed, 56 insertions(+), 48 deletions(-) delete mode 100644 gno.land/pkg/integration/defaultsigner.go create mode 100644 gno.land/pkg/integration/signer.go diff --git a/contribs/gnogenesis/internal/txs/txs_add_packages.go b/contribs/gnogenesis/internal/txs/txs_add_packages.go index d2e7fd887b3..0ab5724154e 100644 --- a/contribs/gnogenesis/internal/txs/txs_add_packages.go +++ b/contribs/gnogenesis/internal/txs/txs_add_packages.go @@ -17,9 +17,10 @@ import ( ) const ( - DefaultAccount_Name = "test1" - DefaultAccount_Address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" - DefaultAccount_Seed = "source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast" + defaultAccount_Name = "test1" + defaultAccount_Address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" + defaultAccount_Seed = "source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast" + defaultAccount_publicKey = "gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pq0skzdkmzu0r9h6gny6eg8c9dc303xrrudee6z4he4y7cs5rnjwmyf40yaj" ) var errInvalidPackageDir = errors.New("invalid package directory") @@ -83,7 +84,7 @@ func execTxsAddPackages( args []string, ) error { var ( - keyname = DefaultAccount_Name + keyname = defaultAccount_Name keybase keys.Keybase pass string ) @@ -110,7 +111,7 @@ func execTxsAddPackages( } } else { keybase = keys.NewInMemory() - _, err := keybase.CreateAccount(DefaultAccount_Name, DefaultAccount_Seed, "", "", 0, 0) + _, err := keybase.CreateAccount(defaultAccount_Name, defaultAccount_Seed, "", "", 0, 0) if err != nil { return fmt.Errorf("unable to create account: %w", err) } @@ -118,7 +119,7 @@ func execTxsAddPackages( info, err := keybase.GetByNameOrAddress(keyname) if err != nil { - return err + return fmt.Errorf("unable to find key in keybase: %w", err) } creator := info.GetAddress() diff --git a/contribs/gnogenesis/internal/txs/txs_add_packages_test.go b/contribs/gnogenesis/internal/txs/txs_add_packages_test.go index d13ae9d236b..d4dda87cf65 100644 --- a/contribs/gnogenesis/internal/txs/txs_add_packages_test.go +++ b/contribs/gnogenesis/internal/txs/txs_add_packages_test.go @@ -23,6 +23,7 @@ import ( func TestGenesis_Txs_Add_Packages(t *testing.T) { t.Parallel() + const addPkgExpectedSignature = "cfe5a15d8def04cbdaf9d08e2511db7928152b26419c4577cbfa282c83118852411f3de5d045ce934555572c21bda8042ce5c64b793a01748e49cf2cff7c2983" t.Run("invalid genesis file", func(t *testing.T) { t.Parallel() @@ -179,7 +180,7 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { // Create key kb, err := keys.NewKeyBaseFromDir(keybaseDir) require.NoError(t, err) - _, err = kb.CreateAccount(keyname, DefaultAccount_Seed, "", password, 0, 0) + info, err := kb.CreateAccount(keyname, defaultAccount_Seed, "", password, 0, 0) require.NoError(t, err) io := commands.NewTestIO() @@ -224,11 +225,12 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { msgAddPkg, ok := state.Txs[0].Tx.Msgs[0].(vmm.MsgAddPackage) require.True(t, ok) - require.Equal(t, "gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pq0skzdkmzu0r9h6gny6eg8c9dc303xrrudee6z4he4y7cs5rnjwmyf40yaj", state.Txs[0].Tx.Signatures[0].PubKey.String()) - require.Equal(t, "cfe5a15d8def04cbdaf9d08e2511db7928152b26419c4577cbfa282c83118852411f3de5d045ce934555572c21bda8042ce5c64b793a01748e49cf2cff7c2983", hex.EncodeToString(state.Txs[0].Tx.Signatures[0].Signature)) + require.Equal(t, info.GetPubKey(), state.Txs[0].Tx.Signatures[0].PubKey) + require.Equal(t, addPkgExpectedSignature, hex.EncodeToString(state.Txs[0].Tx.Signatures[0].Signature)) assert.Equal(t, packagePath, msgAddPkg.Package.Path) }) + t.Run("ok default key", func(t *testing.T) { t.Parallel() @@ -274,8 +276,8 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { msgAddPkg, ok := state.Txs[0].Tx.Msgs[0].(vmm.MsgAddPackage) require.True(t, ok) - require.Equal(t, "gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pq0skzdkmzu0r9h6gny6eg8c9dc303xrrudee6z4he4y7cs5rnjwmyf40yaj", state.Txs[0].Tx.Signatures[0].PubKey.String()) - require.Equal(t, "cfe5a15d8def04cbdaf9d08e2511db7928152b26419c4577cbfa282c83118852411f3de5d045ce934555572c21bda8042ce5c64b793a01748e49cf2cff7c2983", hex.EncodeToString(state.Txs[0].Tx.Signatures[0].Signature)) + require.Equal(t, defaultAccount_publicKey, state.Txs[0].Tx.Signatures[0].PubKey.String()) + require.Equal(t, addPkgExpectedSignature, hex.EncodeToString(state.Txs[0].Tx.Signatures[0].Signature)) assert.Equal(t, packagePath, msgAddPkg.Package.Path) }) @@ -322,8 +324,8 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { msgAddPkg, ok := state.Txs[0].Tx.Msgs[0].(vmm.MsgAddPackage) require.True(t, ok) - require.Equal(t, "gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pq0skzdkmzu0r9h6gny6eg8c9dc303xrrudee6z4he4y7cs5rnjwmyf40yaj", state.Txs[0].Tx.Signatures[0].PubKey.String()) - require.Equal(t, "cfe5a15d8def04cbdaf9d08e2511db7928152b26419c4577cbfa282c83118852411f3de5d045ce934555572c21bda8042ce5c64b793a01748e49cf2cff7c2983", hex.EncodeToString(state.Txs[0].Tx.Signatures[0].Signature)) + require.Equal(t, defaultAccount_publicKey, state.Txs[0].Tx.Signatures[0].PubKey.String()) + require.Equal(t, addPkgExpectedSignature, hex.EncodeToString(state.Txs[0].Tx.Signatures[0].Signature)) assert.Equal(t, packagePath, msgAddPkg.Package.Path) }) diff --git a/gno.land/pkg/integration/defaultsigner.go b/gno.land/pkg/integration/defaultsigner.go deleted file mode 100644 index d4dae5b0f61..00000000000 --- a/gno.land/pkg/integration/defaultsigner.go +++ /dev/null @@ -1,32 +0,0 @@ -package integration - -import ( - "github.com/gnolang/gno/gno.land/pkg/gnoland" - "github.com/gnolang/gno/tm2/pkg/crypto/keys" - "github.com/gnolang/gno/tm2/pkg/std" -) - -func SignTxs(txs []gnoland.TxWithMetadata, chainID string) error { - kb := keys.NewInMemory() - _, err := kb.CreateAccount(DefaultAccount_Name, DefaultAccount_Seed, "", "", 0, 0) - if err != nil { - return err - } - for index, tx := range txs { - bytes, err := tx.Tx.GetSignBytes(chainID, 0, 0) - if err != nil { - return err - } - signature, publicKey, err := kb.Sign(DefaultAccount_Name, "", bytes) - if err != nil { - return err - } - txs[index].Tx.Signatures = []std.Signature{ - { - PubKey: publicKey, - Signature: signature, - }, - } - } - return nil -} diff --git a/gno.land/pkg/integration/pkgloader.go b/gno.land/pkg/integration/pkgloader.go index dffd4a540bd..767af5a4ce9 100644 --- a/gno.land/pkg/integration/pkgloader.go +++ b/gno.land/pkg/integration/pkgloader.go @@ -11,6 +11,7 @@ import ( "github.com/gnolang/gno/gnovm/pkg/gnomod" "github.com/gnolang/gno/gnovm/pkg/packages" bft "github.com/gnolang/gno/tm2/pkg/bft/types" + "github.com/gnolang/gno/tm2/pkg/crypto" "github.com/gnolang/gno/tm2/pkg/std" ) @@ -39,7 +40,7 @@ func (pl *PkgsLoader) SetPatch(replace, with string) { pl.patchs[replace] = with } -func (pl *PkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std.Coins) ([]gnoland.TxWithMetadata, error) { +func (pl *PkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std.Coins, creatorKey crypto.PrivKey) ([]gnoland.TxWithMetadata, error) { pkgslist, err := pl.List().Sort() // sorts packages by their dependencies. if err != nil { return nil, fmt.Errorf("unable to sort packages: %w", err) @@ -77,7 +78,7 @@ func (pl *PkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std } } - err = SignTxs(txs, "tendermint_test") + err = SignTxs(txs, creatorKey, "tendermint_test") if err != nil { return nil, fmt.Errorf("unable to sign txs: %w", err) } diff --git a/gno.land/pkg/integration/signer.go b/gno.land/pkg/integration/signer.go new file mode 100644 index 00000000000..b32cd9c59bc --- /dev/null +++ b/gno.land/pkg/integration/signer.go @@ -0,0 +1,33 @@ +package integration + +import ( + "fmt" + + "github.com/gnolang/gno/gno.land/pkg/gnoland" + + "github.com/gnolang/gno/tm2/pkg/crypto" + "github.com/gnolang/gno/tm2/pkg/std" +) + +// SignTxs will sign all txs passed as argument using the private key +// this signature is only valid for genesis transactions as accountNumber and sequence are 0 +func SignTxs(txs []gnoland.TxWithMetadata, privKey crypto.PrivKey, chainID string) error { + for index, tx := range txs { + bytes, err := tx.Tx.GetSignBytes(chainID, 0, 0) + if err != nil { + return fmt.Errorf("unable to get sign bytes for transaction, %w", err) + } + signature, err := privKey.Sign(bytes) + if err != nil { + return fmt.Errorf("unable to sign transaction, %w", err) + } + + txs[index].Tx.Signatures = []std.Signature{ + { + PubKey: privKey.PubKey(), + Signature: signature, + }, + } + } + return nil +} diff --git a/gno.land/pkg/integration/testscript_gnoland.go b/gno.land/pkg/integration/testscript_gnoland.go index ae484a07669..15d582f3df6 100644 --- a/gno.land/pkg/integration/testscript_gnoland.go +++ b/gno.land/pkg/integration/testscript_gnoland.go @@ -237,6 +237,9 @@ func SetupGnolandTestscript(t *testing.T, p *testscript.Params) error { func gnolandCmd(t *testing.T, nodesManager *NodesManager, gnoRootDir string) func(ts *testscript.TestScript, neg bool, args []string) { t.Helper() + defaultPK, err := generatePrivKeyFromMnemonic(DefaultAccount_Seed, "", 0, 0) + require.NoError(t, err) + return func(ts *testscript.TestScript, neg bool, args []string) { sid := getNodeSID(ts) @@ -267,7 +270,7 @@ func gnolandCmd(t *testing.T, nodesManager *NodesManager, gnoRootDir string) fun pkgs := ts.Value(envKeyPkgsLoader).(*PkgsLoader) creator := crypto.MustAddressFromString(DefaultAccount_Address) defaultFee := std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000))) - pkgsTxs, err := pkgs.LoadPackages(creator, defaultFee, nil) + pkgsTxs, err := pkgs.LoadPackages(creator, defaultFee, nil, defaultPK) if err != nil { ts.Fatalf("unable to load packages txs: %s", err) } From 7a134abaeb1125117d032f21911137264acc02ff Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Wed, 8 Jan 2025 20:45:38 +0100 Subject: [PATCH 23/25] fix build --- gno.land/pkg/gnoclient/integration_test.go | 5 +++-- gno.land/pkg/integration/testscript_gnoland.go | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/gno.land/pkg/gnoclient/integration_test.go b/gno.land/pkg/gnoclient/integration_test.go index 6487544b78b..8b70987ea9a 100644 --- a/gno.land/pkg/gnoclient/integration_test.go +++ b/gno.land/pkg/gnoclient/integration_test.go @@ -713,11 +713,12 @@ func loadpkgs(t *testing.T, rootdir string, paths ...string) []gnoland.TxWithMet err := loader.LoadPackage(examplesDir, path, "") require.NoErrorf(t, err, "`loadpkg` unable to load package(s) from %q: %s", path, err) } - + privKey, err := integration.GeneratePrivKeyFromMnemonic(integration.DefaultAccount_Seed, "", 0, 0) + require.NoError(t, err) creator := crypto.MustAddressFromString(integration.DefaultAccount_Address) defaultFee := std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000))) - meta, err := loader.LoadPackages(creator, defaultFee, nil) + meta, err := loader.LoadPackages(creator, defaultFee, nil, privKey) require.NoError(t, err) return meta } diff --git a/gno.land/pkg/integration/testscript_gnoland.go b/gno.land/pkg/integration/testscript_gnoland.go index 15d582f3df6..1e9dc3b0883 100644 --- a/gno.land/pkg/integration/testscript_gnoland.go +++ b/gno.land/pkg/integration/testscript_gnoland.go @@ -117,7 +117,7 @@ func SetupGnolandTestscript(t *testing.T, p *testscript.Params) error { nodesManager := NewNodesManager() - defaultPK, err := generatePrivKeyFromMnemonic(DefaultAccount_Seed, "", 0, 0) + defaultPK, err := GeneratePrivKeyFromMnemonic(DefaultAccount_Seed, "", 0, 0) require.NoError(t, err) var buildOnce sync.Once @@ -237,7 +237,7 @@ func SetupGnolandTestscript(t *testing.T, p *testscript.Params) error { func gnolandCmd(t *testing.T, nodesManager *NodesManager, gnoRootDir string) func(ts *testscript.TestScript, neg bool, args []string) { t.Helper() - defaultPK, err := generatePrivKeyFromMnemonic(DefaultAccount_Seed, "", 0, 0) + defaultPK, err := GeneratePrivKeyFromMnemonic(DefaultAccount_Seed, "", 0, 0) require.NoError(t, err) return func(ts *testscript.TestScript, neg bool, args []string) { @@ -768,7 +768,7 @@ func buildGnoland(t *testing.T, rootdir string) string { } // GeneratePrivKeyFromMnemonic generates a crypto.PrivKey from a mnemonic. -func generatePrivKeyFromMnemonic(mnemonic, bip39Passphrase string, account, index uint32) (crypto.PrivKey, error) { +func GeneratePrivKeyFromMnemonic(mnemonic, bip39Passphrase string, account, index uint32) (crypto.PrivKey, error) { // Generate Seed from Mnemonic seed, err := bip39.NewSeedWithErrorChecking(mnemonic, bip39Passphrase) if err != nil { From cfcc35d1e88d4afed936fa4b69a13e325908ea8f Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Wed, 8 Jan 2025 21:06:03 +0100 Subject: [PATCH 24/25] improve check --- contribs/gnogenesis/internal/txs/txs_add_packages_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contribs/gnogenesis/internal/txs/txs_add_packages_test.go b/contribs/gnogenesis/internal/txs/txs_add_packages_test.go index d4dda87cf65..38d930401e8 100644 --- a/contribs/gnogenesis/internal/txs/txs_add_packages_test.go +++ b/contribs/gnogenesis/internal/txs/txs_add_packages_test.go @@ -157,7 +157,7 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) { // Run the command cmdErr := cmd.ParseAndRun(context.Background(), args) - assert.Equal(t, cmdErr.Error(), "unable to sign txs, unable sign tx invalid account password") + assert.ErrorContains(t, cmdErr, "unable to sign txs") }) t.Run("existent key correct password", func(t *testing.T) { From e0bb164e1a305aa2f130131fc98b692ac8644312 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Thu, 9 Jan 2025 13:48:26 +0100 Subject: [PATCH 25/25] remove creator from LoadPackages func --- gno.land/pkg/gnoclient/integration_test.go | 4 ++-- gno.land/pkg/integration/pkgloader.go | 5 ++--- gno.land/pkg/integration/testscript_gnoland.go | 3 +-- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/gno.land/pkg/gnoclient/integration_test.go b/gno.land/pkg/gnoclient/integration_test.go index 8b70987ea9a..bfcaaec999e 100644 --- a/gno.land/pkg/gnoclient/integration_test.go +++ b/gno.land/pkg/gnoclient/integration_test.go @@ -715,10 +715,10 @@ func loadpkgs(t *testing.T, rootdir string, paths ...string) []gnoland.TxWithMet } privKey, err := integration.GeneratePrivKeyFromMnemonic(integration.DefaultAccount_Seed, "", 0, 0) require.NoError(t, err) - creator := crypto.MustAddressFromString(integration.DefaultAccount_Address) + defaultFee := std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000))) - meta, err := loader.LoadPackages(creator, defaultFee, nil, privKey) + meta, err := loader.LoadPackages(privKey, defaultFee, nil) require.NoError(t, err) return meta } diff --git a/gno.land/pkg/integration/pkgloader.go b/gno.land/pkg/integration/pkgloader.go index 767af5a4ce9..7e7e817dd92 100644 --- a/gno.land/pkg/integration/pkgloader.go +++ b/gno.land/pkg/integration/pkgloader.go @@ -10,7 +10,6 @@ import ( "github.com/gnolang/gno/gnovm/pkg/gnolang" "github.com/gnolang/gno/gnovm/pkg/gnomod" "github.com/gnolang/gno/gnovm/pkg/packages" - bft "github.com/gnolang/gno/tm2/pkg/bft/types" "github.com/gnolang/gno/tm2/pkg/crypto" "github.com/gnolang/gno/tm2/pkg/std" ) @@ -40,7 +39,7 @@ func (pl *PkgsLoader) SetPatch(replace, with string) { pl.patchs[replace] = with } -func (pl *PkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std.Coins, creatorKey crypto.PrivKey) ([]gnoland.TxWithMetadata, error) { +func (pl *PkgsLoader) LoadPackages(creatorKey crypto.PrivKey, fee std.Fee, deposit std.Coins) ([]gnoland.TxWithMetadata, error) { pkgslist, err := pl.List().Sort() // sorts packages by their dependencies. if err != nil { return nil, fmt.Errorf("unable to sort packages: %w", err) @@ -48,7 +47,7 @@ func (pl *PkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std txs := make([]gnoland.TxWithMetadata, len(pkgslist)) for i, pkg := range pkgslist { - tx, err := gnoland.LoadPackage(pkg, creator, fee, deposit) + tx, err := gnoland.LoadPackage(pkg, creatorKey.PubKey().Address(), fee, deposit) if err != nil { return nil, fmt.Errorf("unable to load pkg %q: %w", pkg.Name, err) } diff --git a/gno.land/pkg/integration/testscript_gnoland.go b/gno.land/pkg/integration/testscript_gnoland.go index 1e9dc3b0883..9781799ea7d 100644 --- a/gno.land/pkg/integration/testscript_gnoland.go +++ b/gno.land/pkg/integration/testscript_gnoland.go @@ -268,9 +268,8 @@ func gnolandCmd(t *testing.T, nodesManager *NodesManager, gnoRootDir string) fun } pkgs := ts.Value(envKeyPkgsLoader).(*PkgsLoader) - creator := crypto.MustAddressFromString(DefaultAccount_Address) defaultFee := std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000))) - pkgsTxs, err := pkgs.LoadPackages(creator, defaultFee, nil, defaultPK) + pkgsTxs, err := pkgs.LoadPackages(defaultPK, defaultFee, nil) if err != nil { ts.Fatalf("unable to load packages txs: %s", err) }