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: support metadata for genesis txs #2941

Merged
merged 28 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
225235e
Save baseapp boilerplate
zivkovicmilos Oct 11, 2024
28f8c33
Move out the hook modification from BaseApp
zivkovicmilos Oct 11, 2024
56dfb9b
Revert "Move out the hook modification from BaseApp"
zivkovicmilos Oct 11, 2024
0a08d81
Revert "Revert "Move out the hook modification from BaseApp""
zivkovicmilos Oct 11, 2024
ada450e
Revert "Revert "Revert "Move out the hook modification from BaseApp"""
zivkovicmilos Oct 11, 2024
801b442
Allow for ctx modification
zivkovicmilos Oct 11, 2024
7635859
Introduce a new genesis state type
zivkovicmilos Oct 12, 2024
a9ca81f
Fix faulty log line
zivkovicmilos Oct 12, 2024
6cc4fc6
Simplify
zivkovicmilos Oct 12, 2024
ec30ae7
Add unit test for metadata tx
zivkovicmilos Oct 12, 2024
dcdbc02
Add unit test for non-metadata tx
zivkovicmilos Oct 12, 2024
ad72ac2
Update tm2/pkg/sdk/helpers.go
moul Oct 24, 2024
ea38da2
Merge branch 'master' into dev/zivkovicmilos/genesis-timestamps
zivkovicmilos Oct 25, 2024
2026261
Merge branch 'master' into dev/zivkovicmilos/genesis-timestamps
zivkovicmilos Oct 26, 2024
861bee0
ctx.Mode() -> mode
zivkovicmilos Oct 26, 2024
f0ce0f0
Fix updated import
zivkovicmilos Oct 26, 2024
a715d43
GenesisTxMetadata -> GnoTxMetadata
zivkovicmilos Oct 26, 2024
1283d93
MetadataTx -> TxWithMetadata
zivkovicmilos Oct 26, 2024
9ff26fe
Merge branch 'master' into dev/zivkovicmilos/genesis-timestamps
zivkovicmilos Oct 29, 2024
ad58e86
Merge branch 'master' into dev/zivkovicmilos/genesis-timestamps
zivkovicmilos Oct 29, 2024
a49c608
Merge branch 'master' into dev/zivkovicmilos/genesis-timestamps
zivkovicmilos Nov 1, 2024
335048c
Modify GnoGenesisState
zivkovicmilos Nov 1, 2024
7b9d87b
Move tx reader fn
zivkovicmilos Nov 4, 2024
3c34099
Change load sig
zivkovicmilos Nov 4, 2024
11504df
Update gnogenesis
zivkovicmilos Nov 4, 2024
e336eaf
Fix wrong init
zivkovicmilos Nov 4, 2024
90a99c3
Fix linter
zivkovicmilos Nov 4, 2024
052e3ee
Add coverage for types helpers
zivkovicmilos Nov 4, 2024
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
10 changes: 8 additions & 2 deletions contribs/gnodev/cmd/gnodev/setup_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func setupDevNode(

if devCfg.txsFile != "" { // Load txs files
var err error
nodeConfig.InitialTxs, err = parseTxs(devCfg.txsFile)
nodeConfig.InitialTxs, err = gnoland.ReadGenesisTxs(ctx, devCfg.txsFile)
if err != nil {
return nil, fmt.Errorf("unable to load transactions: %w", err)
}
Expand All @@ -35,7 +35,13 @@ func setupDevNode(

// Override balances and txs
nodeConfig.BalancesList = state.Balances
nodeConfig.InitialTxs = state.Txs

stateTxs := state.Txs
nodeConfig.InitialTxs = make([]gnoland.TxWithMetadata, len(stateTxs))

for index, nodeTx := range stateTxs {
nodeConfig.InitialTxs[index] = nodeTx
}

logger.Info("genesis file loaded", "path", devCfg.genesisFile, "txs", len(nodeConfig.InitialTxs))
}
Expand Down
23 changes: 0 additions & 23 deletions contribs/gnodev/cmd/gnodev/txs.go

This file was deleted.

26 changes: 17 additions & 9 deletions contribs/gnodev/pkg/dev/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
BalancesList []gnoland.Balance
PackagesPathList []PackagePath
Emitter emitter.Emitter
InitialTxs []std.Tx
InitialTxs []gnoland.TxWithMetadata
TMConfig *tmcfg.Config
SkipFailingGenesisTxs bool
NoReplay bool
Expand Down Expand Up @@ -84,7 +84,7 @@
loadedPackages int

// state
initialState, state []std.Tx
initialState, state []gnoland.TxWithMetadata
currentStateIndex int
}

Expand Down Expand Up @@ -154,7 +154,7 @@

// GetBlockTransactions returns the transactions contained
// within the specified block, if any
func (n *Node) GetBlockTransactions(blockNum uint64) ([]std.Tx, error) {
func (n *Node) GetBlockTransactions(blockNum uint64) ([]gnoland.TxWithMetadata, error) {

Check warning on line 157 in contribs/gnodev/pkg/dev/node.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/pkg/dev/node.go#L157

Added line #L157 was not covered by tests
n.muNode.RLock()
defer n.muNode.RUnlock()

Expand All @@ -163,21 +163,26 @@

// GetBlockTransactions returns the transactions contained
// within the specified block, if any
func (n *Node) getBlockTransactions(blockNum uint64) ([]std.Tx, error) {
func (n *Node) getBlockTransactions(blockNum uint64) ([]gnoland.TxWithMetadata, error) {
int64BlockNum := int64(blockNum)
b, err := n.client.Block(&int64BlockNum)
if err != nil {
return []std.Tx{}, fmt.Errorf("unable to load block at height %d: %w", blockNum, err) // nothing to see here
return []gnoland.TxWithMetadata{}, fmt.Errorf("unable to load block at height %d: %w", blockNum, err) // nothing to see here

Check warning on line 170 in contribs/gnodev/pkg/dev/node.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/pkg/dev/node.go#L170

Added line #L170 was not covered by tests
}

txs := make([]std.Tx, len(b.Block.Data.Txs))
txs := make([]gnoland.TxWithMetadata, len(b.Block.Data.Txs))
for i, encodedTx := range b.Block.Data.Txs {
var tx std.Tx
if unmarshalErr := amino.Unmarshal(encodedTx, &tx); unmarshalErr != nil {
return nil, fmt.Errorf("unable to unmarshal amino tx, %w", unmarshalErr)
}

txs[i] = tx
txs[i] = gnoland.TxWithMetadata{
Tx: tx,
Metadata: &gnoland.GnoTxMetadata{
Timestamp: b.BlockMeta.Header.Time.Unix(),
},
}
}

return txs, nil
Expand Down Expand Up @@ -347,11 +352,14 @@
return nil
}

func (n *Node) getBlockStoreState(ctx context.Context) ([]std.Tx, error) {
func (n *Node) getBlockStoreState(ctx context.Context) ([]gnoland.TxWithMetadata, error) {
// get current genesis state
genesis := n.GenesisDoc().AppState.(gnoland.GnoGenesisState)

state := genesis.Txs[n.loadedPackages:] // ignore previously loaded packages
initialTxs := genesis.Txs[n.loadedPackages:] // ignore previously loaded packages

state := append([]gnoland.TxWithMetadata{}, initialTxs...)

lastBlock := n.getLatestBlockNumber()
var blocnum uint64 = 1
for ; blocnum <= lastBlock; blocnum++ {
Expand Down
5 changes: 2 additions & 3 deletions contribs/gnodev/pkg/dev/node_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/gnolang/gno/contribs/gnodev/pkg/events"
"github.com/gnolang/gno/gno.land/pkg/gnoland"
bft "github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/std"
)

var ErrEmptyState = errors.New("empty state")
Expand All @@ -29,7 +28,7 @@ func (n *Node) SaveCurrentState(ctx context.Context) error {
}

// Export the current state as list of txs
func (n *Node) ExportCurrentState(ctx context.Context) ([]std.Tx, error) {
func (n *Node) ExportCurrentState(ctx context.Context) ([]gnoland.TxWithMetadata, error) {
n.muNode.RLock()
defer n.muNode.RUnlock()

Expand All @@ -42,7 +41,7 @@ func (n *Node) ExportCurrentState(ctx context.Context) ([]std.Tx, error) {
return state[:n.currentStateIndex], nil
}

func (n *Node) getState(ctx context.Context) ([]std.Tx, error) {
func (n *Node) getState(ctx context.Context) ([]gnoland.TxWithMetadata, error) {
if n.state == nil {
var err error
n.state, err = n.getBlockStoreState(ctx)
Expand Down
24 changes: 14 additions & 10 deletions contribs/gnodev/pkg/dev/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"

"github.com/gnolang/gno/contribs/gnodev/pkg/address"
"github.com/gnolang/gno/gno.land/pkg/gnoland"
vmm "github.com/gnolang/gno/gno.land/pkg/sdk/vm"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
Expand Down Expand Up @@ -118,7 +119,7 @@ func (pm PackagesMap) toList() gnomod.PkgList {
return list
}

func (pm PackagesMap) Load(fee std.Fee) ([]std.Tx, error) {
func (pm PackagesMap) Load(fee std.Fee) ([]gnoland.TxWithMetadata, error) {
pkgs := pm.toList()

sorted, err := pkgs.Sort()
Expand All @@ -127,7 +128,8 @@ func (pm PackagesMap) Load(fee std.Fee) ([]std.Tx, error) {
}

nonDraft := sorted.GetNonDraftPkgs()
txs := []std.Tx{}
txs := make([]gnoland.TxWithMetadata, 0, len(nonDraft))

for _, modPkg := range nonDraft {
pkg := pm[modPkg.Dir]
if pkg.Creator.IsZero() {
Expand All @@ -141,18 +143,20 @@ func (pm PackagesMap) Load(fee std.Fee) ([]std.Tx, error) {
}

// Create transaction
tx := std.Tx{
Fee: fee,
Msgs: []std.Msg{
vmm.MsgAddPackage{
Creator: pkg.Creator,
Deposit: pkg.Deposit,
Package: memPkg,
tx := gnoland.TxWithMetadata{
Tx: std.Tx{
Fee: fee,
Msgs: []std.Msg{
vmm.MsgAddPackage{
Creator: pkg.Creator,
Deposit: pkg.Deposit,
Package: memPkg,
},
},
},
}

tx.Signatures = make([]std.Signature, len(tx.GetSigners()))
tx.Tx.Signatures = make([]std.Signature, len(tx.Tx.GetSigners()))
txs = append(txs, tx)
}

Expand Down
9 changes: 4 additions & 5 deletions contribs/gnogenesis/internal/txs/txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"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 {
Expand Down Expand Up @@ -47,7 +46,7 @@ func (c *txsCfg) RegisterFlags(fs *flag.FlagSet) {
}

// appendGenesisTxs saves the given transactions to the genesis doc
func appendGenesisTxs(genesis *types.GenesisDoc, txs []std.Tx) error {
func appendGenesisTxs(genesis *types.GenesisDoc, txs []gnoland.TxWithMetadata) error {
// Initialize the app state if it's not present
if genesis.AppState == nil {
genesis.AppState = gnoland.GnoGenesisState{}
Expand Down Expand Up @@ -77,7 +76,7 @@ func appendGenesisTxs(genesis *types.GenesisDoc, txs []std.Tx) error {
}

// txStore is a wrapper for TM2 transactions
type txStore []std.Tx
type txStore []gnoland.TxWithMetadata

// leftMerge merges the two tx stores, with
// preference to the left
Expand All @@ -86,7 +85,7 @@ func (i *txStore) leftMerge(b txStore) error {
txHashMap := make(map[string]struct{}, len(*i))

for _, tx := range *i {
txHash, err := getTxHash(tx)
txHash, err := getTxHash(tx.Tx)
if err != nil {
return err
}
Expand All @@ -95,7 +94,7 @@ func (i *txStore) leftMerge(b txStore) error {
}

for _, tx := range b {
txHash, err := getTxHash(tx)
txHash, err := getTxHash(tx.Tx)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion contribs/gnogenesis/internal/txs/txs_add_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func execTxsAddPackages(
return errInvalidPackageDir
}

parsedTxs := make([]std.Tx, 0)
parsedTxs := make([]gnoland.TxWithMetadata, 0)
for _, path := range args {
// Generate transactions from the packages (recursively)
txs, err := gnoland.LoadPackagesFromDir(path, genesisDeployAddress, genesisDeployFee)
Expand Down
4 changes: 2 additions & 2 deletions contribs/gnogenesis/internal/txs/txs_add_packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) {
state := updatedGenesis.AppState.(gnoland.GnoGenesisState)

require.Equal(t, 1, len(state.Txs))
require.Equal(t, 1, len(state.Txs[0].Msgs))
require.Equal(t, 1, len(state.Txs[0].Tx.Msgs))

msgAddPkg, ok := state.Txs[0].Msgs[0].(vmm.MsgAddPackage)
msgAddPkg, ok := state.Txs[0].Tx.Msgs[0].(vmm.MsgAddPackage)
require.True(t, ok)

assert.Equal(t, packagePath, msgAddPkg.Package.Path)
Expand Down
21 changes: 4 additions & 17 deletions contribs/gnogenesis/internal/txs/txs_add_sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ 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")
)
var errNoTxsFileSpecified = errors.New("no txs file specified")

// newTxsAddSheetCmd creates the genesis txs add sheet subcommand
func newTxsAddSheetCmd(txsCfg *txsCfg, io commands.IO) *commands.Command {
Expand Down Expand Up @@ -49,22 +45,13 @@ func execTxsAddSheet(
return errNoTxsFileSpecified
}

parsedTxs := make([]std.Tx, 0)
parsedTxs := make([]gnoland.TxWithMetadata, 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)
txs, err := gnoland.ReadGenesisTxs(ctx, file)
if err != nil {
return fmt.Errorf("unable to parse file, %w", err)
}

if err = file.Close(); err != nil {
return fmt.Errorf("unable to gracefully close file, %w", err)
}

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

Expand Down
33 changes: 17 additions & 16 deletions contribs/gnogenesis/internal/txs/txs_add_sheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,35 @@ import (
)

// generateDummyTxs generates dummy transactions
func generateDummyTxs(t *testing.T, count int) []std.Tx {
func generateDummyTxs(t *testing.T, count int) []gnoland.TxWithMetadata {
t.Helper()

txs := make([]std.Tx, count)
txs := make([]gnoland.TxWithMetadata, count)

for i := 0; i < count; i++ {
txs[i] = std.Tx{
Msgs: []std.Msg{
bank.MsgSend{
FromAddress: crypto.Address{byte(i)},
ToAddress: crypto.Address{byte((i + 1) % count)},
Amount: std.NewCoins(std.NewCoin(ugnot.Denom, 1)),
txs[i] = gnoland.TxWithMetadata{
Tx: std.Tx{
Msgs: []std.Msg{
bank.MsgSend{
FromAddress: crypto.Address{byte(i)},
ToAddress: crypto.Address{byte((i + 1) % count)},
Amount: std.NewCoins(std.NewCoin(ugnot.Denom, 1)),
},
},
Fee: std.Fee{
GasWanted: 1,
GasFee: std.NewCoin(ugnot.Denom, 1000000),
},
Memo: fmt.Sprintf("tx %d", i),
},
Fee: std.Fee{
GasWanted: 1,
GasFee: std.NewCoin(ugnot.Denom, 1000000),
},
Memo: fmt.Sprintf("tx %d", i),
}
}

return txs
}

// encodeDummyTxs encodes the transactions into amino JSON
func encodeDummyTxs(t *testing.T, txs []std.Tx) []string {
func encodeDummyTxs(t *testing.T, txs []gnoland.TxWithMetadata) []string {
t.Helper()

encodedTxs := make([]string, 0, len(txs))
Expand Down Expand Up @@ -104,8 +106,7 @@ func TestGenesis_Txs_Add_Sheets(t *testing.T) {
}

// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
assert.ErrorContains(t, cmdErr, errInvalidTxsFile.Error())
assert.Error(t, cmd.ParseAndRun(context.Background(), args))
})

t.Run("no txs file", func(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions contribs/gnogenesis/internal/txs/txs_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -117,9 +116,9 @@ func TestGenesis_Txs_Export(t *testing.T) {
// Validate the transactions were written down
scanner := bufio.NewScanner(outputFile)

outputTxs := make([]std.Tx, 0)
outputTxs := make([]gnoland.TxWithMetadata, 0)
for scanner.Scan() {
var tx std.Tx
var tx gnoland.TxWithMetadata

require.NoError(t, amino.UnmarshalJSON(scanner.Bytes(), &tx))

Expand Down
Loading
Loading