Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add the ability to add packages to genesis transactions #2327

Merged
merged 7 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/gno.land/p/demo/blog/blog.gno
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func (b Blog) RenderPost(res *mux.ResponseWriter, req *mux.Request) {

res.Write("</details>\n")
res.Write("</main>")

}

func (b Blog) RenderTag(res *mux.ResponseWriter, req *mux.Request) {
Expand Down
6 changes: 4 additions & 2 deletions examples/gno.land/r/gnoland/events/events.gno
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ func upcomingEvents() ui.Element {
<div class="column">

</div><!-- end column-->
</div><!-- end columns-3-->`)}
</div><!-- end columns-3-->`),
}
}

func pastEvents() ui.Element {
Expand Down Expand Up @@ -211,5 +212,6 @@ func pastEvents() ui.Element {
[Watch the talk](https://www.youtube.com/watch?v=hCLErPgnavI)

</div><!-- end column-->
</div><!-- end columns-3-->`)}
</div><!-- end columns-3-->`),
}
}
36 changes: 36 additions & 0 deletions gno.land/cmd/gnoland/genesis_txs.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package main

import (
"errors"
"flag"

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

type txsCfg struct {
commonCfg
}

var errInvalidGenesisStateType = errors.New("invalid genesis state type")

// newTxsCmd creates the genesis txs subcommand
func newTxsCmd(io commands.IO) *commands.Command {
cfg := &txsCfg{}
Expand Down Expand Up @@ -37,3 +43,33 @@ func newTxsCmd(io commands.IO) *commands.Command {
func (c *txsCfg) RegisterFlags(fs *flag.FlagSet) {
c.commonCfg.RegisterFlags(fs)
}

// appendGenesisTxs saves the given transactions to the genesis doc
func appendGenesisTxs(genesis *types.GenesisDoc, txs []std.Tx) error {
// Initialize the app state if it's not present
if genesis.AppState == nil {
genesis.AppState = gnoland.GnoGenesisState{}
}

// Make sure the app state is the Gno genesis state
state, ok := genesis.AppState.(gnoland.GnoGenesisState)
if !ok {
return errInvalidGenesisStateType
}

// Left merge the transactions
fileTxStore := txStore(txs)
genesisTxStore := txStore(state.Txs)

// The genesis transactions have preference with the order
// in the genesis.json
if err := genesisTxStore.leftMerge(fileTxStore); err != nil {
return err
}

// Save the state
state.Txs = genesisTxStore
genesis.AppState = state

return nil
}
92 changes: 9 additions & 83 deletions gno.land/cmd/gnoland/genesis_txs_add.go
Original file line number Diff line number Diff line change
@@ -1,100 +1,26 @@
package main

import (
"context"
"errors"
"fmt"
"os"

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

var (
errInvalidTxsFile = errors.New("unable to open transactions file")
errNoTxsFileSpecified = errors.New("no txs file specified")
)

// newTxsAddCmd creates the genesis txs add subcommand
func newTxsAddCmd(txsCfg *txsCfg, io commands.IO) *commands.Command {
return commands.NewCommand(
cmd := commands.NewCommand(
commands.Metadata{
Name: "add",
ShortUsage: "txs add <tx-file ...>",
ShortHelp: "imports transactions into the genesis.json",
LongHelp: "Imports the transactions from a tx-archive backup to the genesis.json",
ShortUsage: "txs add <subcommand> [flags] [<arg>...]",
ShortHelp: "adds transactions into the genesis.json",
LongHelp: "Adds initial transactions to the genesis.json",
},
commands.NewEmptyConfig(),
func(ctx context.Context, args []string) error {
return execTxsAdd(ctx, txsCfg, io, args)
},
commands.HelpExec,
)
}

func execTxsAdd(
ctx context.Context,
cfg *txsCfg,
io commands.IO,
args []string,
) error {
// Load the genesis
genesis, loadErr := types.GenesisDocFromFile(cfg.genesisPath)
if loadErr != nil {
return fmt.Errorf("unable to load genesis, %w", loadErr)
}

// Open the transactions files
if len(args) == 0 {
return errNoTxsFileSpecified
}

parsedTxs := make([]std.Tx, 0)
for _, file := range args {
file, loadErr := os.Open(file)
if loadErr != nil {
return fmt.Errorf("%w, %w", errInvalidTxsFile, loadErr)
}

txs, err := std.ParseTxs(ctx, file)
if err != nil {
return fmt.Errorf("unable to read file, %w", err)
}

parsedTxs = append(parsedTxs, txs...)
}

// Initialize the app state if it's not present
if genesis.AppState == nil {
genesis.AppState = gnoland.GnoGenesisState{}
}

state := genesis.AppState.(gnoland.GnoGenesisState)

// Left merge the transactions
fileTxStore := txStore(parsedTxs)
genesisTxStore := txStore(state.Txs)

// The genesis transactions have preference with the order
// in the genesis.json
if err := genesisTxStore.leftMerge(fileTxStore); err != nil {
return err
}

// Save the state
state.Txs = genesisTxStore
genesis.AppState = state

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

io.Printfln(
"Saved %d transactions to genesis.json",
len(parsedTxs),
cmd.AddSubCommands(
newTxsAddSheetCmd(txsCfg, io),
newTxsAddPackagesCmd(txsCfg, io),
)

return nil
return cmd
}
81 changes: 81 additions & 0 deletions gno.land/cmd/gnoland/genesis_txs_add_packages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"context"
"errors"
"fmt"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
"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 (
genesisDeployAddress = crypto.MustAddressFromString("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") // test1
genesisDeployFee = std.NewFee(50000, std.MustParseCoin("1000000ugnot"))
)

// newTxsAddPackagesCmd creates the genesis txs add packages subcommand
func newTxsAddPackagesCmd(txsCfg *txsCfg, io commands.IO) *commands.Command {
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(),
func(_ context.Context, args []string) error {
return execTxsAddPackages(txsCfg, io, args)
},
)
}

func execTxsAddPackages(
cfg *txsCfg,
io commands.IO,
args []string,
) error {
// Load the genesis
genesis, loadErr := types.GenesisDocFromFile(cfg.genesisPath)
if loadErr != nil {
return fmt.Errorf("unable to load genesis, %w", loadErr)
}

// Make sure the package dir is set
if len(args) == 0 {
return errInvalidPackageDir
}

parsedTxs := make([]std.Tx, 0)
for _, path := range args {
// Generate transactions from the packages (recursively)
txs, err := gnoland.LoadPackagesFromDir(path, genesisDeployAddress, genesisDeployFee)
if err != nil {
return fmt.Errorf("unable to load txs from directory, %w", err)
}

parsedTxs = append(parsedTxs, txs...)
}

// Save the txs to the genesis.json
if err := appendGenesisTxs(genesis, parsedTxs); err != nil {
return fmt.Errorf("unable to append genesis transactions, %w", err)
}

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

io.Printfln(
"Saved %d transactions to genesis.json",
len(parsedTxs),
)

return nil
}
Loading
Loading