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

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 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
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 = parseTxs(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
40 changes: 37 additions & 3 deletions contribs/gnodev/cmd/gnodev/txs.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package main

import (
"bufio"
"context"
"fmt"
"os"

"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/tm2/pkg/amino"
)

func parseTxs(txFile string) ([]std.Tx, error) {
func parseTxs(ctx context.Context, txFile string) ([]gnoland.TxWithMetadata, error) {
if txFile == "" {
return nil, nil
}
Expand All @@ -19,5 +21,37 @@ func parseTxs(txFile string) ([]std.Tx, error) {
}
defer file.Close()

return std.ParseTxs(context.Background(), file)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create a similar helper in the gnoland package.

var (
txs []gnoland.TxWithMetadata

scanner = bufio.NewScanner(file)
)

for scanner.Scan() {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
// Parse the amino JSON
var tx gnoland.TxWithMetadata
if err := amino.UnmarshalJSON(scanner.Bytes(), &tx); err != nil {
return nil, fmt.Errorf(
"unable to unmarshal amino JSON, %w",
err,
)
}

txs = append(txs, tx)
}
}

// Check for scanning errors
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf(
"error encountered while reading file, %w",
err,
)
}

return txs, nil
}
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
4 changes: 2 additions & 2 deletions contribs/gnogenesis/internal/balances/balances_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,11 @@ func getBalancesFromTransactions(

// mapGenesisBalancesFromState extracts the initial account balances from the
// genesis app state
func mapGenesisBalancesFromState(state gnoland.GnoGenesisState) (gnoland.Balances, error) {
func mapGenesisBalancesFromState(state gnoland.GnoGenesis) (gnoland.Balances, error) {
// Construct the initial genesis balance sheet
genesisBalances := gnoland.NewBalances()

for _, balance := range state.Balances {
for _, balance := range state.GenesisBalances() {
genesisBalances[balance.Address] = balance
}

Expand Down
9 changes: 8 additions & 1 deletion gno.land/cmd/gnoland/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,17 @@ func generateGenesisFile(genesisFile string, pk crypto.PubKey, c *startCfg) erro

genesisTxs = append(pkgsTxs, genesisTxs...)

metadataTxs := make([]gnoland.TxWithMetadata, 0, len(genesisTxs))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid this and patch the genesisTxs file accordingly?

for _, tx := range genesisTxs {
metadataTxs = append(metadataTxs, gnoland.TxWithMetadata{
Tx: tx,
})
}

// Construct genesis AppState.
gen.AppState = gnoland.GnoGenesisState{
Balances: balances,
Txs: genesisTxs,
Txs: metadataTxs,
}

// Write genesis state
Expand Down
27 changes: 25 additions & 2 deletions gno.land/pkg/gnoland/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
"github.com/gnolang/gno/tm2/pkg/bft/config"
bft "github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/crypto"
dbm "github.com/gnolang/gno/tm2/pkg/db"
"github.com/gnolang/gno/tm2/pkg/events"
Expand Down Expand Up @@ -301,9 +302,31 @@ func (cfg InitChainerConfig) loadAppState(ctx sdk.Context, appState any) ([]abci
}

txResponses := make([]abci.ResponseDeliverTx, 0, len(state.Txs))

// Run genesis txs
for _, tx := range state.Txs {
res := cfg.baseApp.Deliver(tx)
var (
stdTx = tx.Tx
metadata = tx.Metadata

ctxFn sdk.ContextFn
)

// Check if there is metadata associated with the tx
if metadata != nil {
// Create a custom context modifier
ctxFn = func(ctx sdk.Context) sdk.Context {
moul marked this conversation as resolved.
Show resolved Hide resolved
// Create a copy of the header, in
// which only the timestamp information is modified
header := ctx.BlockHeader().(*bft.Header).Copy()
header.Time = time.Unix(metadata.Timestamp, 0)

// Save the modified header
return ctx.WithBlockHeader(header)
}
}

res := cfg.baseApp.Deliver(stdTx, ctxFn)
if res.IsErr() {
ctx.Logger().Error(
"Unable to deliver genesis tx",
Expand All @@ -319,7 +342,7 @@ func (cfg InitChainerConfig) loadAppState(ctx sdk.Context, appState any) ([]abci
GasUsed: res.GasUsed,
})

cfg.GenesisTxResultHandler(ctx, tx, res)
cfg.GenesisTxResultHandler(ctx, stdTx, res)
}
return txResponses, nil
}
Expand Down
Loading
Loading