Skip to content

Commit

Permalink
Add unit test for non-metadata tx
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Oct 12, 2024
1 parent ec30ae7 commit dcdbc02
Showing 1 changed file with 121 additions and 81 deletions.
202 changes: 121 additions & 81 deletions gno.land/pkg/gnoland/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,64 @@ func createAndSignTx(
}

func TestInitChainer_MetadataTxs(t *testing.T) {
t.Parallel()

var (
db = memdb.NewMemDB()
currentTimestamp = time.Now()
laterTimestamp = currentTimestamp.Add(10 * 24 * time.Hour) // 10 days

getMetadataState = func(tx std.Tx, balances []Balance) GnoGenesis {
return MetadataGenesisState{
// Set the package deployment as the genesis tx
Txs: []MetadataTx{
{
GenesisTx: tx,
TxMetadata: GenesisTxMetadata{
Timestamp: laterTimestamp.Unix(),
},
},
},
// Make sure the deployer account has a balance
Balances: balances,
}
}

getNonMetadataState = func(tx std.Tx, balances []Balance) GnoGenesis {
return GnoGenesisState{
Txs: []std.Tx{tx},
Balances: balances,
}
}
)

testTable := []struct {
name string
genesisTime time.Time
expectedTime time.Time
stateFn func(std.Tx, []Balance) GnoGenesis
}{
{
"non-metadata transaction",
currentTimestamp,
currentTimestamp,
getNonMetadataState,
},
{
"metadata transaction",
currentTimestamp,
laterTimestamp,
getMetadataState,
},
}

for _, testCase := range testTable {
t.Run(testCase.name, func(t *testing.T) {
var (
db = memdb.NewMemDB()

key = getDummyKey(t) // user account, and genesis deployer
timestamp = time.Now().Add(10 * 24 * time.Hour) // 10 days
chainID = "test"
key = getDummyKey(t) // user account, and genesis deployer
chainID = "test"

path = "gno.land/r/demo/metadatatx"
body = `package metadatatx
path = "gno.land/r/demo/metadatatx"
body = `package metadatatx
import "time"
Expand All @@ -273,89 +320,82 @@ func TestInitChainer_MetadataTxs(t *testing.T) {
// GetT returns the time that was saved from genesis
func GetT() int64 { return t.Unix() }
`
)

// Create a fresh app instance
app, err := NewAppWithOptions(TestAppOptions(db))
require.NoError(t, err)
)

// Prepare the deploy transaction
msg := vm.MsgAddPackage{
Creator: key.PubKey().Address(),
Package: &std.MemPackage{
Name: "metadatatx",
Path: path,
Files: []*std.MemFile{
{
Name: "file.gno",
Body: body,
// Create a fresh app instance
app, err := NewAppWithOptions(TestAppOptions(db))
require.NoError(t, err)

// Prepare the deploy transaction
msg := vm.MsgAddPackage{
Creator: key.PubKey().Address(),
Package: &std.MemPackage{
Name: "metadatatx",
Path: path,
Files: []*std.MemFile{
{
Name: "file.gno",
Body: body,
},
},
},
},
},
Deposit: nil,
}

// Create the initial genesis tx
tx := createAndSignTx(t, []std.Msg{msg}, chainID, key)
Deposit: nil,
}

// Run the top-level init chain process
app.InitChain(abci.RequestInitChain{
ChainID: chainID,
Time: time.Now(),
ConsensusParams: &abci.ConsensusParams{
Block: defaultBlockParams(),
Validator: &abci.ValidatorParams{
PubKeyTypeURLs: []string{},
},
},
AppState: MetadataGenesisState{
// Set the package deployment as the genesis tx
Txs: []MetadataTx{
{
GenesisTx: tx,
TxMetadata: GenesisTxMetadata{
Timestamp: timestamp.Unix(),
// Create the initial genesis tx
tx := createAndSignTx(t, []std.Msg{msg}, chainID, key)

// Run the top-level init chain process
app.InitChain(abci.RequestInitChain{
ChainID: chainID,
Time: testCase.genesisTime,
ConsensusParams: &abci.ConsensusParams{
Block: defaultBlockParams(),
Validator: &abci.ValidatorParams{
PubKeyTypeURLs: []string{},
},
},
},
// Make sure the deployer account has a balance
Balances: []Balance{
{
Address: key.PubKey().Address(),
Amount: std.NewCoins(std.NewCoin("ugnot", 20_000_000)),
},
},
},
})

// Prepare the call transaction
callMsg := vm.MsgCall{
Caller: key.PubKey().Address(),
PkgPath: path,
Func: "GetT",
}
// Set the package deployment as the genesis tx,
// and make sure the deployer account has a balance
AppState: testCase.stateFn(tx, []Balance{
{
// Make sure the deployer account has a balance
Address: key.PubKey().Address(),
Amount: std.NewCoins(std.NewCoin("ugnot", 20_000_000)),
},
}),
})

// Prepare the call transaction
callMsg := vm.MsgCall{
Caller: key.PubKey().Address(),
PkgPath: path,
Func: "GetT",
}

tx = createAndSignTx(t, []std.Msg{callMsg}, chainID, key)
tx = createAndSignTx(t, []std.Msg{callMsg}, chainID, key)

// Marshal the transaction to Amino binary
marshalledTx, err := amino.Marshal(tx)
require.NoError(t, err)
// Marshal the transaction to Amino binary
marshalledTx, err := amino.Marshal(tx)
require.NoError(t, err)

// Execute the call to the "GetT" method
// on the deployed Realm
resp := app.DeliverTx(abci.RequestDeliverTx{
Tx: marshalledTx,
})
// Execute the call to the "GetT" method
// on the deployed Realm
resp := app.DeliverTx(abci.RequestDeliverTx{
Tx: marshalledTx,
})

require.True(t, resp.IsOK())
require.True(t, resp.IsOK())

// Make sure the initialized Realm state is
// the injected context timestamp from the tx metadata
assert.Contains(
t,
string(resp.Data),
fmt.Sprintf("(%d int64)", timestamp.Unix()),
)
// Make sure the initialized Realm state is
// the injected context timestamp from the tx metadata
assert.Contains(
t,
string(resp.Data),
fmt.Sprintf("(%d int64)", testCase.expectedTime.Unix()),
)
})
}
}

func TestEndBlocker(t *testing.T) {
Expand Down

0 comments on commit dcdbc02

Please sign in to comment.