-
Notifications
You must be signed in to change notification settings - Fork 610
/
tendermint_test.go
115 lines (93 loc) · 4.1 KB
/
tendermint_test.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 tendermint_test
import (
"testing"
"time"
testifysuite "github.com/stretchr/testify/suite"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
cmtbytes "github.com/cometbft/cometbft/libs/bytes"
cmttypes "github.com/cometbft/cometbft/types"
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
ibctm "github.com/cosmos/ibc-go/v9/modules/light-clients/07-tendermint"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
"github.com/cosmos/ibc-go/v9/testing/simapp"
)
const (
chainID = "gaia"
chainIDRevision0 = "gaia-revision-0"
chainIDRevision1 = "gaia-revision-1"
clientID = "gaiamainnet"
trustingPeriod time.Duration = time.Hour * 24 * 7 * 2
ubdPeriod time.Duration = time.Hour * 24 * 7 * 3
maxClockDrift time.Duration = time.Second * 10
)
var (
height = clienttypes.NewHeight(0, 4)
newClientHeight = clienttypes.NewHeight(1, 1)
upgradePath = []string{"upgrade", "upgradedIBCState"}
invalidUpgradePath = []string{"upgrade", ""}
)
type TendermintTestSuite struct {
testifysuite.Suite
coordinator *ibctesting.Coordinator
// testing chains used for convenience and readability
chainA *ibctesting.TestChain
chainB *ibctesting.TestChain
// TODO: deprecate usage in favor of testing package
ctx sdk.Context
cdc codec.Codec
privVal cmttypes.PrivValidator
valSet *cmttypes.ValidatorSet
signers map[string]cmttypes.PrivValidator
valsHash cmtbytes.HexBytes
header *ibctm.Header
now time.Time
headerTime time.Time
clientTime time.Time
}
func (suite *TendermintTestSuite) SetupTest() {
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2)
suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1))
suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(2))
// commit some blocks so that QueryProof returns valid proof (cannot return valid query if height <= 1)
suite.coordinator.CommitNBlocks(suite.chainA, 2)
suite.coordinator.CommitNBlocks(suite.chainB, 2)
// TODO: deprecate usage in favor of testing package
checkTx := false
app := simapp.Setup(suite.T(), checkTx)
suite.cdc = app.AppCodec()
// now is the time of the current chain, must be after the updating header
// mocks ctx.BlockTime()
suite.now = time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
suite.clientTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
// Header time is intended to be time for any new header used for updates
suite.headerTime = time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
suite.privVal = cmttypes.NewMockPV()
pubKey, err := suite.privVal.GetPubKey()
suite.Require().NoError(err)
heightMinus1 := clienttypes.NewHeight(0, height.RevisionHeight-1)
val := cmttypes.NewValidator(pubKey, 10)
suite.signers = make(map[string]cmttypes.PrivValidator)
suite.signers[val.Address.String()] = suite.privVal
suite.valSet = cmttypes.NewValidatorSet([]*cmttypes.Validator{val})
suite.valsHash = suite.valSet.Hash()
suite.header = suite.chainA.CreateTMClientHeader(chainID, int64(height.RevisionHeight), heightMinus1, suite.now, suite.valSet, suite.valSet, suite.valSet, suite.signers)
suite.ctx = app.BaseApp.NewContext(checkTx)
}
func getAltSigners(altVal *cmttypes.Validator, altPrivVal cmttypes.PrivValidator) map[string]cmttypes.PrivValidator {
return map[string]cmttypes.PrivValidator{altVal.Address.String(): altPrivVal}
}
func getBothSigners(suite *TendermintTestSuite, altVal *cmttypes.Validator, altPrivVal cmttypes.PrivValidator) (*cmttypes.ValidatorSet, map[string]cmttypes.PrivValidator) {
// Create bothValSet with both suite validator and altVal. Would be valid update
bothValSet := cmttypes.NewValidatorSet(append(suite.valSet.Validators, altVal))
// Create signer array and ensure it is in same order as bothValSet
_, suiteVal := suite.valSet.GetByIndex(0)
bothSigners := map[string]cmttypes.PrivValidator{
suiteVal.Address.String(): suite.privVal,
altVal.Address.String(): altPrivVal,
}
return bothValSet, bothSigners
}
func TestTendermintTestSuite(t *testing.T) {
testifysuite.Run(t, new(TendermintTestSuite))
}