Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Tendermint lite client verification #5666

Merged
merged 28 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7b8afe1
start ics07 integration with lite.Verify
AdityaSripal Feb 18, 2020
0732280
Merge branch 'aditya/lite-verify' of https://github.com/cosmos/cosmos…
AdityaSripal Feb 18, 2020
8f9f329
make build passes, there are still a number of tests to update
jackzampolin Feb 19, 2020
77c14a4
WIP test refactor
jackzampolin Feb 19, 2020
f9e0f30
start fixing tendermint tests
AdityaSripal Feb 19, 2020
4ac36fa
add test cases to improve coverage
AdityaSripal Feb 19, 2020
32ac85a
fix merge conflicts
AdityaSripal Feb 19, 2020
8c5d6ab
Tests compiling
jackzampolin Feb 19, 2020
b504fed
fix tests
AdityaSripal Feb 20, 2020
d99fb3c
fix merge
AdityaSripal Feb 20, 2020
07aae46
fix missed merge conflicts and fix ics2 tests
AdityaSripal Feb 20, 2020
6a17783
add comments for connection handshake code
AdityaSripal Feb 20, 2020
bc2b74d
start refactor of test code
AdityaSripal Feb 20, 2020
73a209f
complete verify_test
AdityaSripal Feb 20, 2020
433d9a3
Merge branch 'ibc-alpha' into aditya/lite-verify
cwgoes Feb 20, 2020
195afbc
Fix spacing from merge
cwgoes Feb 20, 2020
e5dd0ff
Note blocking ICS issue
cwgoes Feb 20, 2020
d95840e
fix 03-connection tests
AdityaSripal Feb 20, 2020
b03efa8
Merge branch 'aditya/lite-verify' of https://github.com/cosmos/cosmos…
AdityaSripal Feb 20, 2020
99116c5
readd slashing defensive check
AdityaSripal Feb 21, 2020
e32aa1c
fix connection tests to have right connection structure; revert incor…
AdityaSripal Feb 21, 2020
5e2cfaf
fix keeper and handshake tests
AdityaSripal Feb 21, 2020
baffeda
fix 04-channel tests
AdityaSripal Feb 21, 2020
1e035b0
fix 20-transfer tests
AdityaSripal Feb 21, 2020
faed84d
get ante test to build
AdityaSripal Feb 21, 2020
b78c92e
fixed ante tests
AdityaSripal Feb 21, 2020
0a3b207
address fede review
AdityaSripal Feb 21, 2020
d41d155
linting
AdityaSripal Feb 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions x/ibc/02-client/keeper/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func (suite *KeeperTestSuite) TestCreateClient() {

if tc.expPanic {
suite.Require().Panics(func() {
clientState, err := ibctmtypes.Initialize(tc.clientID, tc.clientID, suite.consensusState, trustingPeriod, ubdPeriod)
clientState, err := ibctmtypes.Initialize(tc.clientID, trustingPeriod, ubdPeriod, suite.header)
suite.Require().NoError(err, "err on client state initialization")
suite.keeper.CreateClient(suite.ctx, clientState, suite.consensusState)
}, "Msg %d didn't panic: %s", i, tc.msg)
} else {
clientState, err := ibctmtypes.Initialize(tc.clientID, tc.clientID, suite.consensusState, trustingPeriod, ubdPeriod)
clientState, err := ibctmtypes.Initialize(tc.clientID, trustingPeriod, ubdPeriod, suite.header)
if tc.expPass {
suite.Require().NoError(err, "errored on initialization")
suite.Require().NotNil(clientState, "valid test case %d failed: %s", i, tc.msg)
Expand All @@ -71,7 +71,7 @@ func (suite *KeeperTestSuite) TestUpdateClient() {
expPass bool
}{
{"valid update", func() error {
clientState, err := ibctmtypes.Initialize(testClientID, testClientID, suite.consensusState, trustingPeriod, ubdPeriod)
clientState, err := ibctmtypes.Initialize(testClientID, trustingPeriod, ubdPeriod, suite.header)
if err != nil {
return err
}
Expand All @@ -90,13 +90,13 @@ func (suite *KeeperTestSuite) TestUpdateClient() {
return nil
}, false},
{"frozen client", func() error {
clientState := ibctmtypes.ClientState{FrozenHeight: 1, ID: testClientID, LatestHeight: 10}
clientState := ibctmtypes.ClientState{FrozenHeight: 1, ID: testClientID, LastHeader: suite.header}
suite.keeper.SetClientState(suite.ctx, clientState)
suite.keeper.SetClientType(suite.ctx, testClientID, exported.Tendermint)
return nil
}, false},
{"invalid header", func() error {
clientState, err := ibctmtypes.Initialize(testClientID, testClientID, suite.consensusState, trustingPeriod, ubdPeriod)
clientState, err := ibctmtypes.Initialize(testClientID, trustingPeriod, ubdPeriod, suite.header)
if err != nil {
return err
}
Expand Down Expand Up @@ -181,7 +181,7 @@ func (suite *KeeperTestSuite) TestCheckMisbehaviourAndUpdateState() {
},
func() error {
suite.consensusState.ValidatorSet = bothValSet
clientState, err := ibctmtypes.Initialize(testClientID, testClientID, suite.consensusState, trustingPeriod, ubdPeriod)
clientState, err := ibctmtypes.Initialize(testClientID, trustingPeriod, ubdPeriod, suite.header)
if err != nil {
return err
}
Expand All @@ -201,7 +201,7 @@ func (suite *KeeperTestSuite) TestCheckMisbehaviourAndUpdateState() {
},
func() error {
suite.consensusState.ValidatorSet = bothValSet
clientState, err := ibctmtypes.Initialize(testClientID, testClientID, suite.consensusState, trustingPeriod, ubdPeriod)
clientState, err := ibctmtypes.Initialize(testClientID, trustingPeriod, ubdPeriod, suite.header)
if err != nil {
return err
}
Expand All @@ -226,7 +226,7 @@ func (suite *KeeperTestSuite) TestCheckMisbehaviourAndUpdateState() {
ClientID: testClientID,
},
func() error {
clientState := ibctmtypes.ClientState{FrozenHeight: 1, ID: testClientID, LatestHeight: 10}
clientState := ibctmtypes.ClientState{FrozenHeight: 1, ID: testClientID, LastHeader: suite.header}
suite.keeper.SetClientState(suite.ctx, clientState)
return nil
},
Expand All @@ -241,7 +241,7 @@ func (suite *KeeperTestSuite) TestCheckMisbehaviourAndUpdateState() {
ClientID: testClientID,
},
func() error {
clientState := ibctmtypes.ClientState{FrozenHeight: 1, ID: testClientID, LatestHeight: 10}
clientState := ibctmtypes.ClientState{FrozenHeight: 1, ID: testClientID, LastHeader: suite.header}
suite.keeper.SetClientState(suite.ctx, clientState)
return nil
},
Expand All @@ -256,7 +256,7 @@ func (suite *KeeperTestSuite) TestCheckMisbehaviourAndUpdateState() {
ClientID: testClientID,
},
func() error {
clientState, err := ibctmtypes.Initialize(testClientID, testClientID, suite.consensusState, trustingPeriod, ubdPeriod)
clientState, err := ibctmtypes.Initialize(testClientID, trustingPeriod, ubdPeriod, suite.header)
if err != nil {
return err
}
Expand Down
11 changes: 5 additions & 6 deletions x/ibc/02-client/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestKeeperTestSuite(t *testing.T) {
}

func (suite *KeeperTestSuite) TestSetClientState() {
clientState := ibctmtypes.NewClientState(testClientID, testClientID, trustingPeriod, ubdPeriod, testClientHeight, suite.now)
clientState := ibctmtypes.NewClientState(testClientID, trustingPeriod, ubdPeriod, ibctmtypes.Header{})
suite.keeper.SetClientState(suite.ctx, clientState)

retrievedState, found := suite.keeper.GetClientState(suite.ctx, testClientID)
Expand Down Expand Up @@ -113,9 +113,9 @@ func (suite *KeeperTestSuite) TestSetClientConsensusState() {

func (suite KeeperTestSuite) TestGetAllClients() {
expClients := []exported.ClientState{
ibctmtypes.NewClientState(testClientID2, testClientID, trustingPeriod, ubdPeriod, testClientHeight, suite.now),
ibctmtypes.NewClientState(testClientID3, testClientID, trustingPeriod, ubdPeriod, testClientHeight, suite.now),
ibctmtypes.NewClientState(testClientID, testClientID, trustingPeriod, ubdPeriod, testClientHeight, suite.now),
ibctmtypes.NewClientState(testClientID2, trustingPeriod, ubdPeriod, ibctmtypes.Header{}),
ibctmtypes.NewClientState(testClientID3, trustingPeriod, ubdPeriod, ibctmtypes.Header{}),
ibctmtypes.NewClientState(testClientID, trustingPeriod, ubdPeriod, ibctmtypes.Header{}),
}

for i := range expClients {
Expand Down Expand Up @@ -155,7 +155,7 @@ func (suite KeeperTestSuite) TestGetConsensusState() {

func (suite KeeperTestSuite) TestConsensusStateHelpers() {
// initial setup
clientState, _ := ibctmtypes.Initialize(testClientID, testClientID, suite.consensusState, trustingPeriod, ubdPeriod)
clientState, _ := ibctmtypes.Initialize(testClientID, trustingPeriod, ubdPeriod, suite.header)
suite.keeper.SetClientState(suite.ctx, clientState)
suite.keeper.SetClientConsensusState(suite.ctx, testClientID, testClientHeight, suite.consensusState)

Expand All @@ -168,7 +168,6 @@ func (suite KeeperTestSuite) TestConsensusStateHelpers() {

// mock update functionality
suite.keeper.SetClientConsensusState(suite.ctx, testClientID, testClientHeight+5, nextState)
clientState.LatestHeight += 5
suite.keeper.SetClientState(suite.ctx, clientState)

latest, ok := suite.keeper.GetLatestClientConsensusState(suite.ctx, testClientID)
Expand Down
14 changes: 3 additions & 11 deletions x/ibc/03-connection/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,15 @@ func (suite *KeeperTestSuite) queryProof(key []byte) (commitment.Proof, int64) {

func (suite *KeeperTestSuite) createClient(clientID string) {
suite.app.Commit()
commitID := suite.app.LastCommitID()
suite.now = suite.now.Add(time.Minute)

suite.app.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{Height: suite.app.LastBlockHeight() + 1, Time: suite.now}})
suite.ctx = suite.ctx.WithBlockHeight(suite.ctx.BlockHeight() + 1)
testHeight++

consensusState := ibctmtypes.ConsensusState{
Height: testHeight,
Timestamp: suite.now,
Root: commitment.NewRoot(commitID.Hash),
ValidatorSet: suite.valSet,
}

clientState, err := ibctmtypes.Initialize(clientID, clientID, consensusState, trustingPeriod, ubdPeriod)
clientState, err := ibctmtypes.Initialize(clientID, trustingPeriod, ubdPeriod, suite.header)
suite.Require().NoError(err)
_, err = suite.app.IBCKeeper.ClientKeeper.CreateClient(suite.ctx, clientState, consensusState)
_, err = suite.app.IBCKeeper.ClientKeeper.CreateClient(suite.ctx, clientState, suite.header.ConsensusState())
suite.Require().NoError(err)

// _, _, err := simapp.SignCheckDeliver(
Expand Down Expand Up @@ -161,7 +153,7 @@ func (suite *KeeperTestSuite) updateClient(clientID string) {
suite.ctx, clientID, uint64(suite.app.LastBlockHeight()), consensusState,
)
suite.app.IBCKeeper.ClientKeeper.SetClientState(
suite.ctx, ibctmtypes.NewClientState(clientID, clientID, trustingPeriod, ubdPeriod, uint64(suite.app.LastBlockHeight()), suite.now),
suite.ctx, ibctmtypes.NewClientState(clientID, trustingPeriod, ubdPeriod, suite.header),
)

// _, _, err := simapp.SignCheckDeliver(
Expand Down
20 changes: 9 additions & 11 deletions x/ibc/04-channel/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const (
testChannelOrder = exported.ORDERED
testChannelVersion = "1.0"

chainID = "gaia"

testHeight = 1

trustingPeriod time.Duration = time.Hour * 24 * 7 * 2
Expand All @@ -57,13 +59,16 @@ type KeeperTestSuite struct {
cdc *codec.Codec
ctx sdk.Context
app *simapp.SimApp
header ibctmtypes.Header
valSet *tmtypes.ValidatorSet
now time.Time
}

func (suite *KeeperTestSuite) SetupTest() {
isCheckTx := false
app := simapp.Setup(isCheckTx)

suite.now = time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
now2 := suite.now.Add(time.Duration(time.Hour * 1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary conversion (from unconvert)

suite.cdc = app.Codec()
suite.ctx = app.BaseApp.NewContext(isCheckTx, abci.Header{})
suite.app = app
Expand All @@ -72,6 +77,7 @@ func (suite *KeeperTestSuite) SetupTest() {

validator := tmtypes.NewValidator(privVal.GetPubKey(), 1)
suite.valSet = tmtypes.NewValidatorSet([]*tmtypes.Validator{validator})
suite.header = ibctmtypes.CreateTestHeader(chainID, int64(testHeight), now2, suite.valSet, suite.valSet, []tmtypes.PrivValidator{privVal})
}

func (suite *KeeperTestSuite) TestSetChannel() {
Expand Down Expand Up @@ -210,16 +216,9 @@ func (suite *KeeperTestSuite) commitNBlocks(n int) {
func (suite *KeeperTestSuite) createClient(clientID string) {
suite.commitNBlocks(1)

commitID := suite.app.LastCommitID()
consensusState := ibctmtypes.ConsensusState{
Height: testHeight,
Root: commitment.NewRoot(commitID.Hash),
ValidatorSet: suite.valSet,
}

clientState, err := ibctmtypes.Initialize(clientID, clientID, consensusState, trustingPeriod, ubdPeriod)
clientState, err := ibctmtypes.Initialize(clientID, trustingPeriod, ubdPeriod, suite.header)
suite.Require().NoError(err)
_, err = suite.app.IBCKeeper.ClientKeeper.CreateClient(suite.ctx, clientState, consensusState)
_, err = suite.app.IBCKeeper.ClientKeeper.CreateClient(suite.ctx, clientState, suite.header.ConsensusState())
suite.Require().NoError(err)
}

Expand All @@ -240,7 +239,6 @@ func (suite *KeeperTestSuite) updateClient() {
suite.app.IBCKeeper.ClientKeeper.SetClientConsensusState(suite.ctx, testClientID1, uint64(height-1), state)
csi, _ := suite.app.IBCKeeper.ClientKeeper.GetClientState(suite.ctx, testClientID1)
cs, _ := csi.(ibctmtypes.ClientState)
cs.LatestHeight = uint64(height - 1)
suite.app.IBCKeeper.ClientKeeper.SetClientState(suite.ctx, cs)
}

Expand Down
24 changes: 10 additions & 14 deletions x/ibc/07-tendermint/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/evidence"
evidenceexported "github.com/cosmos/cosmos-sdk/x/evidence/exported"
ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types"
Expand All @@ -27,7 +27,7 @@ import (
// in https://github.com/cosmos/ics/tree/master/spec/ics-002-client-semantics#create
func GetCmdCreateClient(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create [client-id] [chain-id] [path/to/consensus_state.json] [trusting_period] [unbonding_period]",
Use: "create [client-id] [path/to/consensus_state.json] [trusting_period] [unbonding_period]",
Short: "create new client with a consensus state",
Long: strings.TrimSpace(fmt.Sprintf(`create new client with a specified identifier and consensus state:

Expand All @@ -42,34 +42,30 @@ $ %s tx ibc client create [client-id] [path/to/consensus_state.json] [trusting_p
cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc).WithBroadcastMode(flags.BroadcastBlock)

clientID := args[0]
chainID := args[1]

var state ibctmtypes.ConsensusState
if err := cdc.UnmarshalJSON([]byte(args[2]), &state); err != nil {
var header ibctmtypes.Header
if err := cdc.UnmarshalJSON([]byte(args[1]), &header); err != nil {
// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(args[2])
contents, err := ioutil.ReadFile(args[1])
if err != nil {
return errors.New("neither JSON input nor path to .json file were provided")
}
if err := cdc.UnmarshalJSON(contents, &state); err != nil {
return errors.Wrap(err, "error unmarshalling consensus state file")
if err := cdc.UnmarshalJSON(contents, &header); err != nil {
return errors.Wrap(err, "error unmarshalling consensus header file")
}
}

trustingPeriod, err := time.ParseDuration(args[3])
trustingPeriod, err := time.ParseDuration(args[2])
if err != nil {
return err
}

ubdPeriod, err := time.ParseDuration(args[4])
ubdPeriod, err := time.ParseDuration(args[3])
if err != nil {
return err
}

msg := ibctmtypes.NewMsgCreateClient(
clientID, chainID, state,
trustingPeriod, ubdPeriod, cliCtx.GetFromAddress(),
)
msg := ibctmtypes.NewMsgCreateClient(clientID, header, trustingPeriod, ubdPeriod, cliCtx.GetFromAddress())

if err := msg.ValidateBasic(); err != nil {
return err
Expand Down
12 changes: 6 additions & 6 deletions x/ibc/07-tendermint/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, queryRoute string)

// CreateClientReq defines the properties of a create client request's body.
type CreateClientReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
ClientID string `json:"client_id" yaml:"client_id"`
ChainID string `json:"chain_id" yaml:"chain_id"`
ConsensusState ibctmtypes.ConsensusState `json:"consensus_state" yaml:"consensus_state"`
TrustingPeriod time.Duration `json:"trusting_period" yaml:"trusting_period"`
UnbondingPeriod time.Duration `json:"unbonding_period" yaml:"unbonding_period"`
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
ClientID string `json:"client_id" yaml:"client_id"`
ChainID string `json:"chain_id" yaml:"chain_id"`
Header ibctmtypes.Header `json:"consensus_state" yaml:"consensus_state"`
TrustingPeriod time.Duration `json:"trusting_period" yaml:"trusting_period"`
UnbondingPeriod time.Duration `json:"unbonding_period" yaml:"unbonding_period"`
}

// UpdateClientReq defines the properties of a update client request's body.
Expand Down
3 changes: 1 addition & 2 deletions x/ibc/07-tendermint/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ func createClientHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
// create the message
msg := ibctmtypes.NewMsgCreateClient(
req.ClientID,
req.ChainID,
req.ConsensusState,
req.Header,
req.TrustingPeriod, req.UnbondingPeriod,
fromAddr,
)
Expand Down
20 changes: 11 additions & 9 deletions x/ibc/07-tendermint/misbehaviour.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
"github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types"
ibctypes "github.com/cosmos/cosmos-sdk/x/ibc/types"
lite "github.com/tendermint/tendermint/lite2"
)

// CheckMisbehaviourAndUpdateState determines whether or not two conflicting
Expand Down Expand Up @@ -78,19 +79,20 @@ func checkMisbehaviour(
return errors.New("unbonding period since last consensus state timestamp is over")
}

// Evidence is within the trusting period. ValidatorSet must have 2/3 similarity with trusted FromValidatorSet
// check that the validator sets on both headers are valid given the last trusted validatorset
// less than or equal to evidence height
if err := consensusState.ValidatorSet.VerifyFutureCommit(
evidence.Header1.ValidatorSet, evidence.ChainID,
evidence.Header1.Commit.BlockID, evidence.Header1.Height, evidence.Header1.Commit,
// TODO: - Evidence must be within trusting period
cwgoes marked this conversation as resolved.
Show resolved Hide resolved

// - ValidatorSet must have 2/3 similarity with trusted FromValidatorSet
// - ValidatorSets on both headers are valid given the last trusted ValidatorSet
if err := consensusState.ValidatorSet.VerifyCommitTrusting(
evidence.ChainID, evidence.Header1.Commit.BlockID, evidence.Header1.Height,
evidence.Header1.Commit, lite.DefaultTrustLevel,
); err != nil {
return fmt.Errorf("validator set in header 1 has too much change from last known validator set: %v", err)
}

if err := consensusState.ValidatorSet.VerifyFutureCommit(
evidence.Header2.ValidatorSet, evidence.ChainID,
evidence.Header2.Commit.BlockID, evidence.Header2.Height, evidence.Header2.Commit,
if err := consensusState.ValidatorSet.VerifyCommitTrusting(
evidence.ChainID, evidence.Header2.Commit.BlockID, evidence.Header2.Height,
evidence.Header2.Commit, lite.DefaultTrustLevel,
); err != nil {
return fmt.Errorf("validator set in header 2 has too much change from last known validator set: %v", err)
}
Expand Down
Loading