Skip to content

Commit

Permalink
new: add new simulation pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkanani committed Apr 10, 2020
1 parent 6092d97 commit 749b91f
Show file tree
Hide file tree
Showing 23 changed files with 777 additions and 75 deletions.
9 changes: 9 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,12 @@ func (app *HeimdallApp) GetSubspace(moduleName string) subspace.Subspace {
func (app *HeimdallApp) GetModuleManager() *module.Manager {
return app.mm
}

// GetMaccPerms returns a copy of the module account permissions
func GetMaccPerms() map[string][]string {
dupMaccPerms := make(map[string][]string)
for k, v := range maccPerms {
dupMaccPerms[k] = v
}
return dupMaccPerms
}
36 changes: 18 additions & 18 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@ import (
db "github.com/tendermint/tm-db"
)

func TestHeimdalldExport(t *testing.T) {
func TestHeimdallAppExport(t *testing.T) {
db := db.NewMemDB()
happ := NewHeimdallApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db)
genesisState := NewDefaultGenesisState()
err := setGenesis(happ, genesisState)

// Get state bytes
stateBytes, err := codec.MarshalJSONIndent(happ.Codec(), genesisState)
require.NoError(t, err)

// Initialize the chain
happ.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
AppStateBytes: stateBytes,
},
)

// Set commit
happ.Commit()

// Making a new app object with the db, so that initchain hasn't been called
newHapp := NewHeimdallApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db)
_, _, err = newHapp.ExportAppStateAndValidators()
Expand All @@ -29,20 +42,7 @@ func TestMakePulp(t *testing.T) {
require.NotNil(t, pulp, "Pulp should be nil")
}

func setGenesis(app *HeimdallApp, genesisState GenesisState) error {
stateBytes, err := codec.MarshalJSONIndent(app.Codec(), genesisState)
if err != nil {
return err
}

// Initialize the chain
app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
AppStateBytes: stateBytes,
},
)

app.Commit()
return nil
func TestGetMaccPerms(t *testing.T) {
dup := GetMaccPerms()
require.Equal(t, maccPerms, dup, "duplicated module account permissions differed from actual module account permissions")
}
8 changes: 4 additions & 4 deletions auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
authRest "github.com/maticnetwork/heimdall/auth/client/rest"
"github.com/maticnetwork/heimdall/auth/types"
"github.com/maticnetwork/heimdall/helper"
hmTypes "github.com/maticnetwork/heimdall/types"
hmModule "github.com/maticnetwork/heimdall/types/module"
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ hmTypes.HeimdallModuleBasic = AppModule{}
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ hmModule.HeimdallModuleBasic = AppModule{}
// _ module.AppModuleSimulation = AppModule{}
)

Expand Down
162 changes: 162 additions & 0 deletions auth/types/stdtx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package types

import (
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/libs/log"
yaml "gopkg.in/yaml.v2"
)

var (
priv = secp256k1.GenPrivKey()
addr = sdk.AccAddress(priv.PubKey().Address())
)

func TestStdTx(t *testing.T) {
msgs := []sdk.Msg{sdk.NewTestMsg(addr)}
sig := StdSignature{}

tx := NewStdTx(msgs, sig, "")
require.Equal(t, msgs, tx.GetMsgs())
require.Equal(t, sigs, tx.GetSignatures())

feePayer := tx.GetSigners()[0]
require.Equal(t, addr, feePayer)
}

func TestStdSignBytes(t *testing.T) {
type args struct {
chainID string
accnum uint64
sequence uint64
fee StdFee
msgs []sdk.Msg
memo string
}
defaultFee := sdk.NewTestStdFee()
tests := []struct {
args args
want string
}{
{
args{"1234", 3, 6, defaultFee, []sdk.Msg{sdk.NewTestMsg(addr)}, "memo"},
fmt.Sprintf("{\"account_number\":\"3\",\"chain_id\":\"1234\",\"fee\":{\"amount\":[{\"amount\":\"150\",\"denom\":\"atom\"}],\"gas\":\"50000\"},\"memo\":\"memo\",\"msgs\":[[\"%s\"]],\"sequence\":\"6\"}", addr),
},
}
for i, tc := range tests {
got := string(StdSignBytes(tc.args.chainID, tc.args.accnum, tc.args.sequence, tc.args.fee, tc.args.msgs, tc.args.memo))
require.Equal(t, tc.want, got, "Got unexpected result on test case i: %d", i)
}
}

func TestTxValidateBasic(t *testing.T) {
ctx := sdk.NewContext(nil, abci.Header{ChainID: "mychainid"}, false, log.NewNopLogger())

// keys and addresses
priv1, _, addr1 := KeyTestPubAddr()
priv2, _, addr2 := KeyTestPubAddr()

// msg and signatures
msg1 := NewTestMsg(addr1, addr2)
fee := NewTestStdFee()

msgs := []sdk.Msg{msg1}

// require to fail validation upon invalid fee
badFee := NewTestStdFee()
badFee.Amount[0].Amount = sdk.NewInt(-5)
tx := NewTestTx(ctx, nil, nil, nil, nil, badFee)

err := tx.ValidateBasic()
require.Error(t, err)
require.Equal(t, sdk.CodeInsufficientFee, err.Result().Code)

// require to fail validation when no signatures exist
privs, accNums, seqs := []crypto.PrivKey{}, []uint64{}, []uint64{}
tx = NewTestTx(ctx, msgs, privs, accNums, seqs, fee)

err = tx.ValidateBasic()
require.Error(t, err)
require.Equal(t, sdk.CodeNoSignatures, err.Result().Code)

// require to fail validation when signatures do not match expected signers
privs, accNums, seqs = []crypto.PrivKey{priv1}, []uint64{0, 1}, []uint64{0, 0}
tx = NewTestTx(ctx, msgs, privs, accNums, seqs, fee)

err = tx.ValidateBasic()
require.Error(t, err)
require.Equal(t, sdk.CodeUnauthorized, err.Result().Code)

// require to fail with invalid gas supplied
badFee = NewTestStdFee()
badFee.Gas = 9223372036854775808
tx = NewTestTx(ctx, nil, nil, nil, nil, badFee)

err = tx.ValidateBasic()
require.Error(t, err)
require.Equal(t, sdk.CodeGasOverflow, err.Result().Code)

// require to pass when above criteria are matched
privs, accNums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{0, 1}, []uint64{0, 0}
tx = NewTestTx(ctx, msgs, privs, accNums, seqs, fee)

err = tx.ValidateBasic()
require.NoError(t, err)
}

func TestDefaultTxEncoder(t *testing.T) {
cdc := codec.New()
sdk.RegisterCodec(cdc)
RegisterCodec(cdc)
cdc.RegisterConcrete(sdk.TestMsg{}, "cosmos-sdk/Test", nil)
encoder := DefaultTxEncoder(cdc)

msgs := []sdk.Msg{sdk.NewTestMsg(addr)}
fee := NewTestStdFee()
sigs := []StdSignature{}

tx := NewStdTx(msgs, fee, sigs, "")

cdcBytes, err := cdc.MarshalBinaryLengthPrefixed(tx)

require.NoError(t, err)
encoderBytes, err := encoder(tx)

require.NoError(t, err)
require.Equal(t, cdcBytes, encoderBytes)
}

func TestStdSignatureMarshalYAML(t *testing.T) {
_, pubKey, _ := KeyTestPubAddr()

testCases := []struct {
sig StdSignature
output string
}{
{
StdSignature{},
"|\n pubkey: \"\"\n signature: \"\"\n",
},
{
StdSignature{PubKey: pubKey, Signature: []byte("dummySig")},
fmt.Sprintf("|\n pubkey: %s\n signature: dummySig\n", sdk.MustBech32ifyAccPub(pubKey)),
},
{
StdSignature{PubKey: pubKey, Signature: nil},
fmt.Sprintf("|\n pubkey: %s\n signature: \"\"\n", sdk.MustBech32ifyAccPub(pubKey)),
},
}

for i, tc := range testCases {
bz, err := yaml.Marshal(tc.sig)
require.NoError(t, err)
require.Equal(t, tc.output, string(bz), "test case #%d", i)
}
}
10 changes: 5 additions & 5 deletions bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
bankRest "github.com/maticnetwork/heimdall/bank/client/rest"
"github.com/maticnetwork/heimdall/bank/types"
"github.com/maticnetwork/heimdall/helper"
hmTypes "github.com/maticnetwork/heimdall/types"
hmModule "github.com/maticnetwork/heimdall/types/module"
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ hmTypes.HeimdallModuleBasic = AppModule{}
// _ module.AppModuleSimulation = AppModule{}
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ hmModule.HeimdallModuleBasic = AppModule{}
// _ hmModule.AppModuleSimulation = AppModule{}
)

// AppModuleBasic defines the basic application module used by the auth module.
Expand Down
8 changes: 4 additions & 4 deletions bor/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
borRest "github.com/maticnetwork/heimdall/bor/client/rest"
"github.com/maticnetwork/heimdall/bor/types"
"github.com/maticnetwork/heimdall/helper"
hmTypes "github.com/maticnetwork/heimdall/types"
hmModule "github.com/maticnetwork/heimdall/types/module"
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ hmTypes.HeimdallModuleBasic = AppModule{}
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ hmModule.HeimdallModuleBasic = AppModule{}
// _ module.AppModuleSimulation = AppModule{}
)

Expand Down
8 changes: 4 additions & 4 deletions chainmanager/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
chainmanagerRest "github.com/maticnetwork/heimdall/chainmanager/client/rest"
"github.com/maticnetwork/heimdall/chainmanager/types"
"github.com/maticnetwork/heimdall/helper"
hmTypes "github.com/maticnetwork/heimdall/types"
hmModule "github.com/maticnetwork/heimdall/types/module"
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ hmTypes.HeimdallModuleBasic = AppModule{}
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ hmModule.HeimdallModuleBasic = AppModule{}
// _ module.AppModuleSimulation = AppModule{}
)

Expand Down
8 changes: 4 additions & 4 deletions checkpoint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import (
"github.com/maticnetwork/heimdall/checkpoint/types"
"github.com/maticnetwork/heimdall/helper"
"github.com/maticnetwork/heimdall/staking"
hmTypes "github.com/maticnetwork/heimdall/types"
hmModule "github.com/maticnetwork/heimdall/types/module"
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ hmTypes.HeimdallModuleBasic = AppModule{}
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ hmModule.HeimdallModuleBasic = AppModule{}
// _ module.AppModuleSimulation = AppModule{}
)

Expand Down
8 changes: 4 additions & 4 deletions clerk/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
clerkRest "github.com/maticnetwork/heimdall/clerk/client/rest"
"github.com/maticnetwork/heimdall/clerk/types"
"github.com/maticnetwork/heimdall/helper"
hmTypes "github.com/maticnetwork/heimdall/types"
hmModule "github.com/maticnetwork/heimdall/types/module"
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ hmTypes.HeimdallModuleBasic = AppModule{}
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ hmModule.HeimdallModuleBasic = AppModule{}
// _ module.AppModuleSimulation = AppModule{}
)

Expand Down
3 changes: 2 additions & 1 deletion cmd/heimdalld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/maticnetwork/heimdall/helper"
hmserver "github.com/maticnetwork/heimdall/server"
hmTypes "github.com/maticnetwork/heimdall/types"
hmModule "github.com/maticnetwork/heimdall/types/module"
)

var (
Expand Down Expand Up @@ -206,7 +207,7 @@ func VerifyGenesis(ctx *server.Context, cdc *codec.Codec) *cobra.Command {

// verify genesis
for _, b := range app.ModuleBasics {
m := b.(hmTypes.HeimdallModuleBasic)
m := b.(hmModule.HeimdallModuleBasic)
if err := m.VerifyGenesis(genesisState); err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (
"github.com/maticnetwork/heimdall/gov/client/cli"
"github.com/maticnetwork/heimdall/gov/client/rest"
"github.com/maticnetwork/heimdall/gov/types"
hmTypes "github.com/maticnetwork/heimdall/types"
hmModule "github.com/maticnetwork/heimdall/types/module"
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ hmTypes.HeimdallModuleBasic = AppModule{}
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ hmModule.HeimdallModuleBasic = AppModule{}
)

// app module basics object
Expand Down
Loading

0 comments on commit 749b91f

Please sign in to comment.