Skip to content

Commit

Permalink
Modify GnoGenesisState
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Nov 1, 2024
1 parent a49c608 commit 335048c
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 156 deletions.
17 changes: 8 additions & 9 deletions contribs/gnodev/cmd/gnodev/setup_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/gnolang/gno/contribs/gnodev/pkg/emitter"
"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/std"
)

// setupDevNode initializes and returns a new DevNode.
Expand All @@ -24,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,13 +34,13 @@ func setupDevNode(
}

// Override balances and txs
nodeConfig.BalancesList = state.GenesisBalances()
nodeConfig.BalancesList = state.Balances

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

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

logger.Info("genesis file loaded", "path", devCfg.genesisFile, "txs", len(nodeConfig.InitialTxs))
Expand Down Expand Up @@ -76,18 +75,18 @@ func setupDevNodeConfig(
return config
}

func extractAppStateFromGenesisFile(path string) (gnoland.GnoGenesis, error) {
func extractAppStateFromGenesisFile(path string) (*gnoland.GnoGenesisState, error) {
doc, err := types.GenesisDocFromFile(path)
if err != nil {
return nil, fmt.Errorf("unable to parse doc file: %w", err)
}

state, ok := doc.AppState.(gnoland.GnoGenesis)
state, ok := doc.AppState.(gnoland.GnoGenesisState)
if !ok {
return nil, fmt.Errorf("invalid `GnoGenesisState` app state")
}

return state, nil
return &state, nil
}

func resolveUnixOrTCPAddr(in string) (out string) {
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)
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
}
34 changes: 18 additions & 16 deletions contribs/gnodev/pkg/dev/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type NodeConfig struct {
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 @@ type Node struct {
loadedPackages int

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

Expand Down Expand Up @@ -154,7 +154,7 @@ func (n *Node) GetRemoteAddress() string {

// 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 @@ func (n *Node) GetBlockTransactions(blockNum uint64) ([]std.Tx, error) {

// 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,16 +352,13 @@ func (n *Node) SendTransaction(tx *std.Tx) error {
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.GnoGenesis)
genesis := n.GenesisDoc().AppState.(gnoland.GnoGenesisState)

initialTxs := genesis.GenesisTxs()[n.loadedPackages:] // ignore previously loaded packages
initialTxs := genesis.Txs[n.loadedPackages:] // ignore previously loaded packages

state := make([]std.Tx, 0, len(initialTxs))
for _, tx := range initialTxs {
state = append(state, tx.Tx())
}
state := append([]gnoland.TxWithMetadata{}, initialTxs...)

lastBlock := n.getLatestBlockNumber()
var blocnum uint64 = 1
Expand Down Expand Up @@ -465,7 +467,7 @@ func (n *Node) handleEventTX(evt tm2events.Event) {
}
}

func (n *Node) rebuildNode(ctx context.Context, genesis gnoland.GnoGenesis) (err error) {
func (n *Node) rebuildNode(ctx context.Context, genesis gnoland.GnoGenesisState) (err error) {
noopLogger := log.NewNoopLogger()

// Stop the node if it's currently running.
Expand Down Expand Up @@ -553,7 +555,7 @@ func (n *Node) genesisTxResultHandler(ctx sdk.Context, tx std.Tx, res sdk.Result
return
}

func newNodeConfig(tmc *tmcfg.Config, chainid string, appstate gnoland.GnoGenesis) *gnoland.InMemoryNodeConfig {
func newNodeConfig(tmc *tmcfg.Config, chainid string, appstate gnoland.GnoGenesisState) *gnoland.InMemoryNodeConfig {
// Create Mocked Identity
pv := gnoland.NewMockedPrivValidator()
genesis := gnoland.NewDefaultGenesisConfig(chainid)
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
4 changes: 2 additions & 2 deletions contribs/gnodev/pkg/dev/node_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ func TestExportState(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, doc.AppState)

state, ok := doc.AppState.(gnoland.GnoGenesis)
state, ok := doc.AppState.(gnoland.GnoGenesisState)
require.True(t, ok)
assert.Equal(t, 3, len(state.GenesisTxs()))
assert.Equal(t, 3, len(state.Txs))
})
}

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: 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))
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
15 changes: 6 additions & 9 deletions gno.land/pkg/gnoland/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,13 @@ func (cfg InitChainerConfig) loadStdlibs(ctx sdk.Context) {
}

func (cfg InitChainerConfig) loadAppState(ctx sdk.Context, appState any) ([]abci.ResponseDeliverTx, error) {
state, ok := appState.(GnoGenesis)
state, ok := appState.(GnoGenesisState)
if !ok {
return nil, fmt.Errorf("invalid AppState of type %T", appState)
}

// Parse and set genesis state balances
for _, bal := range state.GenesisBalances() {
for _, bal := range state.Balances {
acc := cfg.acctKpr.NewAccountWithAddress(ctx, bal.Address)
cfg.acctKpr.SetAccount(ctx, acc)
err := cfg.bankKpr.SetCoins(ctx, bal.Address, bal.Amount)
Expand All @@ -301,16 +301,13 @@ func (cfg InitChainerConfig) loadAppState(ctx sdk.Context, appState any) ([]abci
}
}

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

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

ctxFn sdk.ContextFn
)
Expand Down
Loading

0 comments on commit 335048c

Please sign in to comment.