/
chain.go
102 lines (86 loc) · 2.81 KB
/
chain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package furyaibctesting
import (
"encoding/json"
"time"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
ibctesting "github.com/cosmos/ibc-go/v4/testing"
"github.com/cosmos/ibc-go/v4/testing/simapp/helpers"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/fury-labs/furya/v20/app"
)
type TestChain struct {
*ibctesting.TestChain
}
func SetupTestingApp() (ibctesting.TestingApp, map[string]json.RawMessage) {
furyaApp := app.Setup(false)
return furyaApp, app.NewDefaultGenesisState()
}
// SendMsgsNoCheck is an alternative to ibctesting.TestChain.SendMsgs so that it doesn't check for errors. That should be handled by the caller
func (chain *TestChain) SendMsgsNoCheck(msgs ...sdk.Msg) (*sdk.Result, error) {
// ensure the chain has the latest time
chain.Coordinator.UpdateTimeForChain(chain.TestChain)
_, r, err := SignAndDeliver(
chain.TxConfig,
chain.App.GetBaseApp(),
chain.GetContext().BlockHeader(),
msgs,
chain.ChainID,
[]uint64{chain.SenderAccount.GetAccountNumber()},
[]uint64{chain.SenderAccount.GetSequence()},
chain.SenderPrivKey,
)
if err != nil {
return nil, err
}
// SignAndDeliver calls app.Commit()
chain.NextBlock()
// increment sequence for successful transaction execution
err = chain.SenderAccount.SetSequence(chain.SenderAccount.GetSequence() + 1)
if err != nil {
return nil, err
}
chain.Coordinator.IncrementTime()
return r, nil
}
// SignAndDeliver signs and delivers a transaction without asserting the results. This overrides the function
// from ibctesting
func SignAndDeliver(
txCfg client.TxConfig, app *baseapp.BaseApp, header tmproto.Header, msgs []sdk.Msg,
chainID string, accNums, accSeqs []uint64, priv ...cryptotypes.PrivKey,
) (sdk.GasInfo, *sdk.Result, error) {
tx, _ := helpers.GenTx(
txCfg,
msgs,
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 2500)},
helpers.DefaultGenTxGas,
chainID,
accNums,
accSeqs,
priv...,
)
// Simulate a sending a transaction and committing a block
gInfo, res, err := app.Deliver(txCfg.TxEncoder(), tx)
return gInfo, res, err
}
// Move epochs to the future to avoid issues with minting
func (chain *TestChain) MoveEpochsToTheFuture() error {
epochsKeeper := chain.GetFuryaApp().EpochsKeeper
ctx := chain.GetContext()
for _, epoch := range epochsKeeper.AllEpochInfos(ctx) {
epoch.StartTime = ctx.BlockTime().Add(time.Hour * 24 * 30)
epochsKeeper.DeleteEpochInfo(chain.GetContext(), epoch.Identifier)
err := epochsKeeper.AddEpochInfo(ctx, epoch)
if err != nil {
return err
}
}
return nil
}
// GetFuryaApp returns the current chain's app as an FuryaApp
func (chain *TestChain) GetFuryaApp() *app.FuryaApp {
v, _ := chain.App.(*app.FuryaApp)
return v
}