Skip to content

Commit

Permalink
fix(gnodev): rebase on master
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <[email protected]>
  • Loading branch information
gfanton committed Nov 13, 2024
1 parent 7446231 commit 49cb027
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 83 deletions.
15 changes: 8 additions & 7 deletions contribs/gnodev/pkg/dev/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ func NewDevNode(ctx context.Context, cfg *NodeConfig) (*Node, error) {
}

// generate genesis state
genesis := NodeGenesisState{
genesis := gnoland.GnoGenesisState{
Balances: cfg.BalancesList,
Txs: append(pkgsTxs, cfg.InitialTxs...),
}

if err := devnode.rebuildNode(ctx, &genesis); err != nil {
if err := devnode.rebuildNode(ctx, genesis); err != nil {
return nil, fmt.Errorf("unable to initialize the node: %w", err)
}

Expand Down Expand Up @@ -284,13 +284,13 @@ func (n *Node) Reset(ctx context.Context) error {

// Append initialTxs
txs := append(pkgsTxs, n.initialState...)
genesis := NodeGenesisState{
genesis := gnoland.GnoGenesisState{
Balances: n.config.BalancesList,
Txs: txs,
}

// Reset the node with the new genesis state.
err = n.rebuildNode(ctx, &genesis)
err = n.rebuildNode(ctx, genesis)
if err != nil {
return fmt.Errorf("unable to initialize a new node: %w", err)
}
Expand Down Expand Up @@ -410,7 +410,7 @@ func (n *Node) rebuildNodeFromState(ctx context.Context) error {
return fmt.Errorf("unable to load pkgs: %w", err)
}

return n.rebuildNode(ctx, &NodeGenesisState{
return n.rebuildNode(ctx, gnoland.GnoGenesisState{
Balances: n.config.BalancesList, Txs: txs,
})
}
Expand All @@ -426,14 +426,15 @@ func (n *Node) rebuildNodeFromState(ctx context.Context) error {
return fmt.Errorf("unable to load pkgs: %w", err)
}

time.Now().UnixNano()
// Create genesis with loaded pkgs + previous state
genesis := NodeGenesisState{
genesis := gnoland.GnoGenesisState{
Balances: n.config.BalancesList,
Txs: append(pkgsTxs, state...),
}

// Reset the node with the new genesis state.
err = n.rebuildNode(ctx, &genesis)
err = n.rebuildNode(ctx, genesis)
n.logger.Info("reload done", "pkgs", len(pkgsTxs), "state applied", len(state))

// Update node infos
Expand Down
80 changes: 4 additions & 76 deletions contribs/gnodev/pkg/dev/node_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,8 @@ 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"
)

type NodeGenesisState struct {
Balances []gnoland.Balance
Txs []gnoland.GenesisTx
}

func (s *NodeGenesisState) Len() int { return len(s.Txs) }

func (s *NodeGenesisState) GenesisTxs() []gnoland.GenesisTx { return s.Txs }

func (s *NodeGenesisState) GenesisBalances() []gnoland.Balance {
return s.Balances
}

func (s *NodeGenesisState) MetadataGenesisState() *gnoland.MetadataGenesisState {
metaState := gnoland.MetadataGenesisState{
Balances: s.GenesisBalances(),
}

// Export metadata as well
txs := s.GenesisTxs()
metaState.Txs = make([]gnoland.MetadataTx, len(txs))
for i, tx := range txs {
metaState.Txs[i] = gnoland.MetadataTx{
GenesisTx: tx.Tx(),
}

if meta := tx.Metadata(); meta != nil {
metaState.Txs[i].TxMetadata = *meta
}
}

return &metaState
}

func (s *NodeGenesisState) AppendTxs(txs ...std.Tx) {
genesisTxs := make([]gnoland.GenesisTx, len(txs))
for i, tx := range txs {
genesisTxs[i] = &GnoGenesisTx{tx}
}
s.Txs = append(s.Txs, genesisTxs...)
}

func (s *NodeGenesisState) AppendMetadataTxs(txs ...gnoland.MetadataTx) {
genesisTxs := make([]gnoland.GenesisTx, len(txs))
for i, tx := range txs {
genesisTxs[i] = tx
}

s.Txs = append(s.Txs, genesisTxs...)
}

type GnoGenesisTx struct {
StdTx std.Tx
}

func (g GnoGenesisTx) Tx() std.Tx { return g.StdTx }
func (g GnoGenesisTx) Metadata() *gnoland.GenesisTxMetadata { return nil }

var ErrEmptyState = errors.New("empty state")

// Save the current state as initialState
Expand Down Expand Up @@ -151,13 +92,13 @@ func (n *Node) MoveBy(ctx context.Context, x int) error {
newState := n.state[:newIndex]

// Create genesis with loaded pkgs + previous state
genesis := NodeGenesisState{
genesis := gnoland.GnoGenesisState{
Balances: n.config.BalancesList,
Txs: append(pkgsTxs, newState...),
}

// Reset the node with the new genesis state.
if err = n.rebuildNode(ctx, &genesis); err != nil {
if err = n.rebuildNode(ctx, genesis); err != nil {
return fmt.Errorf("uanble to rebuild node: %w", err)
}

Expand Down Expand Up @@ -191,23 +132,10 @@ func (n *Node) ExportStateAsGenesis(ctx context.Context) (*bft.GenesisDoc, error

// Get current blockstore state
doc := *n.Node.GenesisDoc() // copy doc

metaState := gnoland.MetadataGenesisState{
doc.AppState = gnoland.GnoGenesisState{
Balances: n.config.BalancesList,
Txs: state,
}

// Export metadata as well
metaState.Txs = make([]gnoland.MetadataTx, len(state))
for i, tx := range state {
metaState.Txs[i] = gnoland.MetadataTx{
GenesisTx: tx.Tx(),
}

if meta := tx.Metadata(); meta != nil {
metaState.Txs[i].TxMetadata = *meta
}
}

doc.AppState = metaState
return &doc, nil
}
114 changes: 114 additions & 0 deletions contribs/gnodev/pkg/dev/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package dev

import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"

mock "github.com/gnolang/gno/contribs/gnodev/internal/mock"

Expand Down Expand Up @@ -221,6 +223,118 @@ func Render(_ string) string { return str }
assert.Equal(t, mock.EvtNull, emitter.NextEvent().Type())
}

func TestTxTimestampRecover(t *testing.T) {
const (
// foo package
foobarGnoMod = "module gno.land/r/dev/foo\n"
fooFile = `package foo
import (
"strconv"
"strings"
"time"
)
// First time element should be init at genesis
var times = []time.Time{time.Now()}
func SpanTime() {
times = append(times, time.Now())
}
func Render(_ string) string {
var strs strings.Builder
strs.WriteRune('[')
for i, t := range times {
if i > 0 {
strs.WriteRune(',')
}
strs.WriteString(strconv.Itoa(int(t.UnixNano())))
}
strs.WriteRune(']')
return strs.String()
}
`
)

parseJSONTimesList := func(t *testing.T, render string) []time.Time {
t.Helper()

var times []time.Time
var nanos []int64

err := json.Unmarshal([]byte(render), &nanos)
require.NoError(t, err)

for _, nano := range nanos {
sec, nsec := nano/int64(time.Second), nano%int64(time.Second)
times = append(times, time.Unix(sec, nsec))
}

return times
}

// Generate package foo
foopkg := generateTestingPackage(t, "gno.mod", foobarGnoMod, "foo.gno", fooFile)

// Call NewDevNode with no package should work
node, emitter := newTestingDevNode(t, foopkg)
assert.Len(t, node.ListPkgs(), 1)

// Span multiple time
for i := 0; i < 2; i++ {
// Wait a little for time shift
time.Sleep(time.Second)

msg := vm.MsgCall{
PkgPath: "gno.land/r/dev/foo",
Func: "SpanTime",
}
res, err := testingCallRealm(t, node, msg)
require.NoError(t, err)
require.NoError(t, res.CheckTx.Error)
require.NoError(t, res.DeliverTx.Error)
assert.Equal(t, emitter.NextEvent().Type(), events.EvtTxResult)
}

// Render JSON times list
render, err := testingRenderRealm(t, node, "gno.land/r/dev/foo")
require.NoError(t, err)

// Parse times list
times1 := parseJSONTimesList(t, render)

// Ensure times are correctly expending
for i, t2 := range times {
if i == 0 {
continue
}

t1 := times1[i-1]
require.Greater(t, t2.UnixNano(), t1.UnixNano())
}

// Reload the node
err = node.Reload(context.Background())
require.NoError(t, err)
assert.Equal(t, emitter.NextEvent().Type(), events.EvtReload)

// Fetch time list again from render
render, err = testingRenderRealm(t, node, "gno.land/r/dev/foo")
require.NoError(t, err)

times2 := parseJSONTimesList(t, render)

// Times list should be identical
require.Len(t, times2, len(times1))
for i := 0; i < len(times1); i++ {
t1nsec, t2nsec := times1[i].UnixNano(), times2[i].UnixNano()
assert.Equal(t, t1nsec, t2nsec,
"comparing times1[%d](%d) == times2[%d](%d)", i, t1nsec, i, t2nsec)
}
}

func testingRenderRealm(t *testing.T, node *Node, rlmpath string) (string, error) {
t.Helper()

Expand Down

0 comments on commit 49cb027

Please sign in to comment.