/
chain.go
115 lines (93 loc) · 3.65 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
107
108
109
110
111
112
113
114
115
package ibctesting
import (
"testing"
"github.com/stretchr/testify/require"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtypes "github.com/cometbft/cometbft/types"
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/v7/modules/core/04-channel/types"
ibcgotesting "github.com/cosmos/ibc-go/v7/testing"
"github.com/cosmos/ibc-go/v7/testing/mock"
"github.com/ethereum/go-ethereum/common"
"github.com/haqq-network/haqq/crypto/ethsecp256k1"
haqqtypes "github.com/haqq-network/haqq/types"
"github.com/haqq-network/haqq/utils"
evmtypes "github.com/haqq-network/haqq/x/evm/types"
)
// ChainIDPrefix defines the default chain ID prefix for Haqq Network test chains
var ChainIDPrefix = "haqq_"
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)
signers[pubKey.Address().String()] = privVal
// generate genesis account
senderPrivKey, err := ethsecp256k1.GenerateKey()
if err != nil {
panic(err)
}
baseAcc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
acc := &haqqtypes.EthAccount{
BaseAccount: baseAcc,
CodeHash: common.BytesToHash(evmtypes.EmptyCodeHash).Hex(),
}
amount := sdk.TokensFromConsensusPower(1, haqqtypes.PowerReduction)
balance := banktypes.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, 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(),
}
txConfig := app.GetTxConfig()
// create an account to send transactions from
chain := &ibcgotesting.TestChain{
T: t,
Coordinator: coord,
ChainID: chainID,
App: app,
CurrentHeader: header,
QueryServer: app.GetIBCKeeper(),
TxConfig: txConfig,
Codec: app.AppCodec(),
Vals: valSet,
Signers: signers,
SenderPrivKey: senderPrivKey,
SenderAccount: acc,
NextVals: valSet,
}
coord.CommitBlock(chain)
return chain
}
func NewTransferPath(chainA, chainB *ibcgotesting.TestChain) *Path {
path := 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
}