/
chain.go
106 lines (84 loc) · 3.44 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
103
104
105
106
package ibctesting
import (
"testing"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
ibcgotesting "github.com/cosmos/ibc-go/v6/testing"
"github.com/cosmos/ibc-go/v6/testing/mock"
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"
fxtypes "github.com/functionx/fx-core/v4/types"
)
// ChainIDPrefix defines the default chain ID prefix for Evmos test chains
var ChainIDPrefix = "fxcore"
func init() {
ibcgotesting.ChainIDPrefix = ChainIDPrefix
}
// NewTestChain initializes a new TestChain instance with a single validator set using a
// generated private key. It also creates a sender account to be used for delivering transactions.
//
// The first block height is committed to state in order to allow for client creations on
// counterparty chains. The TestChain will return with a block height starting at 2.
//
// Time management is handled by the Coordinator in order to ensure synchrony between chains.
// Each update of any chain increments the block header time for all chains by 5 seconds.
func NewTestChain(t *testing.T, coord *ibcgotesting.Coordinator, chainID string) *ibcgotesting.TestChain {
// generate validator private/public key
privVal := mock.NewPV()
pubKey, err := privVal.GetPubKey()
require.NoError(t, err)
// create validator set with single validator
validator := tmtypes.NewValidator(pubKey, 1)
valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator})
signers := make(map[string]tmtypes.PrivValidator, 1)
signers[pubKey.Address().String()] = privVal
// generate genesis account
senderPrivKey := secp256k1.GenPrivKey()
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
amount := sdk.TokensFromConsensusPower(1, sdk.DefaultPowerReduction)
balance := banktypes.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(fxtypes.DefaultDenom, amount)),
}
app := SetupWithGenesisValSet(t, valSet, []authtypes.GenesisAccount{acc}, chainID, balance)
// create current header and call begin block
header := tmproto.Header{
ChainID: chainID,
Height: 1,
Time: coord.CurrentTime.UTC(),
ProposerAddress: pubKey.Address().Bytes(),
}
txConfig := app.GetTxConfig()
// create an account to send transactions from
chain := &ibcgotesting.TestChain{
T: t,
Coordinator: coord,
App: app,
ChainID: chainID,
CurrentHeader: header,
QueryServer: app.GetIBCKeeper(),
TxConfig: txConfig,
Codec: app.AppCodec(),
Vals: valSet,
NextVals: valSet,
Signers: signers,
SenderPrivKey: senderPrivKey,
SenderAccount: acc,
}
coord.CommitBlock(chain)
return chain
}
func NewTransferPath(chainA, chainB *ibcgotesting.TestChain) *ibcgotesting.Path {
path := ibcgotesting.NewPath(chainA, chainB)
path.EndpointA.ChannelConfig.PortID = ibcgotesting.TransferPort
path.EndpointB.ChannelConfig.PortID = ibcgotesting.TransferPort
path.EndpointA.ChannelConfig.Order = channeltypes.UNORDERED
path.EndpointB.ChannelConfig.Order = channeltypes.UNORDERED
path.EndpointA.ChannelConfig.Version = "ics20-1"
path.EndpointB.ChannelConfig.Version = "ics20-1"
return path
}