Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pinosu committed May 18, 2023
1 parent ef25457 commit 7c98f53
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 18 deletions.
19 changes: 1 addition & 18 deletions tests/system/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (c WasmdCli) QuerySmart(contractAddr, msg string, args ...string) string {
}

// QueryBalances queries all balances for an account. Returns json response
// Example:`{"balances":[{"denom":"node0token","amount":"1000000000"},{"denom":"utgd","amount":"400000003"}],"pagination":{}}`
// Example:`{"balances":[{"denom":"node0token","amount":"1000000000"},{"denom":"ustake","amount":"400000003"}],"pagination":{}}`
func (c WasmdCli) QueryBalances(addr string) string {
return c.CustomQuery("q", "bank", "balances", addr)
}
Expand All @@ -284,23 +284,6 @@ func (c WasmdCli) QueryTotalSupply(denom string) int64 {
return gjson.Get(raw, "amount").Int()
}

// QueryValidator queries the validator for the given operator address. Returns json response
func (c WasmdCli) QueryValidator(addr string) string {
return c.CustomQuery("q", "poe", "validator", addr)
}

// QueryValidatorRewards queries the validator rewards for the given operator address
func (c WasmdCli) QueryValidatorRewards(addr string) sdk.DecCoin {
raw := c.CustomQuery("q", "poe", "validator-reward", addr)
require.NotEmpty(c.t, raw)

r := gjson.Get(raw, "reward")
amount, err := sdk.NewDecFromStr(gjson.Get(r.Raw, "amount").String())
require.NoError(c.t, err)
denom := gjson.Get(r.Raw, "denom").String()
return sdk.NewDecCoinFromDec(denom, amount)
}

func (c WasmdCli) GetTendermintValidatorSet() rpc.ResultValidatorsOutput {
args := []string{"q", "tendermint-validator-set"}
got := c.CustomQuery(args...)
Expand Down
86 changes: 86 additions & 0 deletions tests/system/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//go:build system_test

package system

import (
"fmt"
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
)

func TestVestingAccounts(t *testing.T) {
// Scenario:
// given: a genesis file
// when: add-genesis-account with vesting flags is executed
// then: the vesting account data is added to the genesis
sut.ResetChain(t)
cli := NewWasmdCLI(t, sut, verbose)
vest1Addr := cli.AddKey("vesting1")
vest2Addr := cli.AddKey("vesting2")
vest3Addr := cli.AddKey("vesting3")
myStartTimestamp := time.Now().Add(time.Minute).Unix()
myEndTimestamp := time.Now().Add(time.Hour).Unix()
sut.ModifyGenesisCLI(t,
// delayed vesting no cash
[]string{"genesis", "add-genesis-account", vest1Addr, "100000000ustake", "--vesting-amount=100000000ustake", fmt.Sprintf("--vesting-end-time=%d", myEndTimestamp)},
// continuous vesting no cash
[]string{"genesis", "add-genesis-account", vest2Addr, "100000001ustake", "--vesting-amount=100000001ustake", fmt.Sprintf("--vesting-start-time=%d", myStartTimestamp), fmt.Sprintf("--vesting-end-time=%d", myEndTimestamp)},
// continuous vesting with some cash
[]string{"genesis", "add-genesis-account", vest3Addr, "200000002ustake", "--vesting-amount=100000002ustake", fmt.Sprintf("--vesting-start-time=%d", myStartTimestamp), fmt.Sprintf("--vesting-end-time=%d", myEndTimestamp)},
)
raw := sut.ReadGenesisJSON(t)
// delayed vesting: without a start time
accounts := gjson.GetBytes([]byte(raw), `app_state.auth.accounts.#[@type=="/cosmos.vesting.v1beta1.DelayedVestingAccount"]#`).Array()
require.Len(t, accounts, 1)
gotAddr := accounts[0].Get("base_vesting_account.base_account.address").String()
assert.Equal(t, vest1Addr, gotAddr)
amounts := accounts[0].Get("base_vesting_account.original_vesting").Array()
require.Len(t, amounts, 1)
assert.Equal(t, "ustake", amounts[0].Get("denom").String())
assert.Equal(t, "100000000", amounts[0].Get("amount").String())
assert.Equal(t, myEndTimestamp, accounts[0].Get("base_vesting_account.end_time").Int())
assert.Equal(t, int64(0), accounts[0].Get("start_time").Int())

// continuous vesting: start time
accounts = gjson.GetBytes([]byte(raw), `app_state.auth.accounts.#[@type=="/cosmos.vesting.v1beta1.ContinuousVestingAccount"]#`).Array()
require.Len(t, accounts, 2)
gotAddr = accounts[0].Get("base_vesting_account.base_account.address").String()
assert.Equal(t, vest2Addr, gotAddr)
amounts = accounts[0].Get("base_vesting_account.original_vesting").Array()
require.Len(t, amounts, 1)
assert.Equal(t, "ustake", amounts[0].Get("denom").String())
assert.Equal(t, "100000001", amounts[0].Get("amount").String())
assert.Equal(t, myEndTimestamp, accounts[0].Get("base_vesting_account.end_time").Int())
assert.Equal(t, myStartTimestamp, accounts[0].Get("start_time").Int())
// with some cash
gotAddr = accounts[1].Get("base_vesting_account.base_account.address").String()
assert.Equal(t, vest3Addr, gotAddr)
amounts = accounts[1].Get("base_vesting_account.original_vesting").Array()
require.Len(t, amounts, 1)
assert.Equal(t, "ustake", amounts[0].Get("denom").String())
assert.Equal(t, "100000002", amounts[0].Get("amount").String())
assert.Equal(t, myEndTimestamp, accounts[0].Get("base_vesting_account.end_time").Int())
assert.Equal(t, myStartTimestamp, accounts[0].Get("start_time").Int())

// check accounts have some balances
assert.Equal(t, sdk.NewCoins(sdk.NewCoin("ustake", sdk.NewInt(100000000))), getGenesisBalance([]byte(raw), vest1Addr))
assert.Equal(t, sdk.NewCoins(sdk.NewCoin("ustake", sdk.NewInt(100000001))), getGenesisBalance([]byte(raw), vest2Addr))
assert.Equal(t, sdk.NewCoins(sdk.NewCoin("ustake", sdk.NewInt(200000002))), getGenesisBalance([]byte(raw), vest3Addr))
}

func getGenesisBalance(raw []byte, addr string) sdk.Coins {
var r []sdk.Coin
balances := gjson.GetBytes(raw, fmt.Sprintf(`app_state.bank.balances.#[address==%q]#.coins`, addr)).Array()
for _, coins := range balances {
for _, coin := range coins.Array() {
r = append(r, sdk.NewCoin(coin.Get("denom").String(), sdk.NewInt(coin.Get("amount").Int())))
}
}
return r
}
27 changes: 27 additions & 0 deletions tests/system/fraud_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build system_test

package system

import (
"fmt"
"math"
"testing"
)

func TestRecursiveSmartQuery(t *testing.T) {
sut.ResetDirtyChain(t)
sut.StartChain(t)
cli := NewWasmdCLI(t, sut, verbose)

initMsg := fmt.Sprintf(`{"verifier":%q, "beneficiary":%q}`, randomBech32Addr(), randomBech32Addr())
maliciousContractAddr := cli.WasmInstantiate(cli.WasmStore("./testdata/hackatom.wasm.gzip", "--from=node0", "--gas=1500000", "--fees=2stake"), initMsg)

msg := fmt.Sprintf(`{"recurse":{"depth":%d, "work":0}}`, math.MaxUint32)

// when
for _, n := range sut.AllNodes(t) {
cli.WithRunErrorMatcher(ErrInvalidQuery).WithNodeAddress(n.RPCAddr()).
QuerySmart(maliciousContractAddr, msg)
}
sut.AwaitNextBlock(t)
}

0 comments on commit 7c98f53

Please sign in to comment.