Skip to content

Commit

Permalink
feat(genesis): deployerAddress passed as parameter (gnolang#3253)
Browse files Browse the repository at this point in the history
closes gnolang#2573

Had to change the PR from a personal repository as the pipeline was
failing
old PR: gnolang#2986
<!-- please provide a detailed description of the changes made in this
pull request. -->

<details><summary>Contributors' checklist...</summary>

- [ ] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
</details>

---------

Co-authored-by: 6h057 <[email protected]>
Co-authored-by: Miloš Živković <[email protected]>
  • Loading branch information
3 people authored and r3v4s committed Dec 10, 2024
1 parent 55be1f1 commit ad788bd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
4 changes: 4 additions & 0 deletions contribs/gnogenesis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ To clear specific transactions, use the transaction hash:
```shell
gnogenesis txs remove "5HuU9LN8WUa2NsjiNxp8Xii9n0zlSGXc9UqzLHB+DPs="
```
To specify a deployer address (package creator) on add packages command
```shell
gnogenesis txs add packages ./examples --deployer-address=SOME_ADDRESS
```

The transaction hash is the base64 encoding of the Amino-Binary encoded `std.Tx` transaction hash.

Expand Down
58 changes: 47 additions & 11 deletions contribs/gnogenesis/internal/txs/txs_add_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,70 @@ package txs
import (
"context"
"errors"
"flag"
"fmt"

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

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

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

// Keep in sync with gno.land/cmd/start.go
var (
// Keep in sync with gno.land/cmd/start.go
genesisDeployAddress = crypto.MustAddressFromString("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") // test1
genesisDeployFee = std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000)))
defaultCreator = crypto.MustAddressFromString("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") // test1
genesisDeployFee = std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000)))
)

type addPkgCfg struct {
txsCfg *txsCfg
deployerAddress string
}

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",
)
}

// newTxsAddPackagesCmd creates the genesis txs add packages subcommand
func newTxsAddPackagesCmd(txsCfg *txsCfg, io commands.IO) *commands.Command {
cfg := &addPkgCfg{
txsCfg: txsCfg,
}

return commands.NewCommand(
commands.Metadata{
Name: "packages",
ShortUsage: "txs add packages <package-path ...>",
ShortHelp: "imports transactions from the given packages into the genesis.json",
LongHelp: "Imports the transactions from a given package directory recursively to the genesis.json",
},
commands.NewEmptyConfig(),
cfg,
func(_ context.Context, args []string) error {
return execTxsAddPackages(txsCfg, io, args)
return execTxsAddPackages(cfg, io, args)
},
)
}

func execTxsAddPackages(
cfg *txsCfg,
cfg *addPkgCfg,
io commands.IO,
args []string,
) error {
// Load the genesis
genesis, loadErr := types.GenesisDocFromFile(cfg.GenesisPath)
genesis, loadErr := types.GenesisDocFromFile(cfg.txsCfg.GenesisPath)
if loadErr != nil {
return fmt.Errorf("unable to load genesis, %w", loadErr)
}
Expand All @@ -53,10 +76,23 @@ func execTxsAddPackages(
return errInvalidPackageDir
}

var (
creator = defaultCreator
err error
)

// Check if the deployer address is set
if cfg.deployerAddress != defaultCreator.String() {
creator, err = crypto.AddressFromString(cfg.deployerAddress)
if err != nil {
return fmt.Errorf("%w, %w", errInvalidDeployerAddr, err)
}
}

parsedTxs := make([]gnoland.TxWithMetadata, 0)
for _, path := range args {
// Generate transactions from the packages (recursively)
txs, err := gnoland.LoadPackagesFromDir(path, genesisDeployAddress, genesisDeployFee)
txs, err := gnoland.LoadPackagesFromDir(path, creator, genesisDeployFee)
if err != nil {
return fmt.Errorf("unable to load txs from directory, %w", err)
}
Expand All @@ -70,7 +106,7 @@ func execTxsAddPackages(
}

// Save the updated genesis
if err := genesis.SaveAs(cfg.GenesisPath); err != nil {
if err := genesis.SaveAs(cfg.txsCfg.GenesisPath); err != nil {
return fmt.Errorf("unable to save genesis.json, %w", err)
}

Expand Down
26 changes: 26 additions & 0 deletions contribs/gnogenesis/internal/txs/txs_add_packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) {
assert.ErrorContains(t, cmdErr, errInvalidPackageDir.Error())
})

t.Run("invalid deployer address", func(t *testing.T) {
t.Parallel()

tempGenesis, cleanup := testutils.NewTestFile(t)
t.Cleanup(cleanup)

genesis := common.GetDefaultGenesis()
require.NoError(t, genesis.SaveAs(tempGenesis.Name()))

// Create the command
cmd := NewTxsCmd(commands.NewTestIO())
args := []string{
"add",
"packages",
"--genesis-path",
tempGenesis.Name(),
t.TempDir(), // package dir
"--deployer-address",
"beep-boop", // invalid address
}

// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
assert.ErrorIs(t, cmdErr, errInvalidDeployerAddr)
})

t.Run("valid package", func(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit ad788bd

Please sign in to comment.