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

refactor!: add v2 MerklePath with bytes in favour of strings #6644

Merged
merged 21 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a0d2789
refactor: adapt merkle path to use repeated bytes in favour of strings
damiannolan Jun 18, 2024
a82bdbb
Merge branch 'main' into damian/6496-merklepath-bz
damiannolan Jun 18, 2024
9bb5066
refactor: update NewMerklePath func sig to take bytes
damiannolan Jun 19, 2024
5cbded9
nit: rm unnecessary variable
damiannolan Jun 19, 2024
f722998
Merge branch 'main' into damian/6496-merkle-path-constructor-breakage
damiannolan Jun 19, 2024
0ddbc9c
nit: rename var
damiannolan Jun 19, 2024
4435588
chore: add changelog
damiannolan Jun 20, 2024
eab33ad
Merge branch 'main' into damian/6496-merkle-path-constructor-breakage
damiannolan Jun 20, 2024
733539c
fix: introduce legacy merkle path type for encoding compatibility wit…
damiannolan Jun 20, 2024
4c66e27
Merge branch 'main' into damian/6496-merkle-path-constructor-breakage
damiannolan Jun 24, 2024
e09a2af
refactor: adapt code to handle v2 proto MerklePath
damiannolan Jun 25, 2024
dc567e9
chore: add deprecation notices for v1 merkle path
damiannolan Jun 25, 2024
9aca51c
chore: update changelog
damiannolan Jun 25, 2024
5b35718
fix: use pointer to leverage omitempty correctly in contract api
damiannolan Jun 25, 2024
47460fa
test: add tests for 08-wasm contract api encoding
damiannolan Jun 26, 2024
adc9718
Merge branch 'main' into damian/6496-merkle-path-constructor-breakage
damiannolan Jun 26, 2024
ba58f37
refactor: adapt IsValidUTF func per review suggestion and add more tests
damiannolan Jun 26, 2024
7667e2e
Merge branch 'main' into damian/6496-merkle-path-constructor-breakage
crodriguezvega Jul 2, 2024
dc34a09
refactor: use v2.MerklePath in contract api and update 08-wasm changelog
damiannolan Jul 3, 2024
6016180
Update CHANGELOG.md
damiannolan Jul 3, 2024
25733bf
Merge branch 'main' into damian/6496-merkle-path-constructor-breakage
damiannolan Jul 3, 2024
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
8 changes: 4 additions & 4 deletions modules/core/02-client/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ func (suite *KeeperTestSuite) TestQueryVerifyMembershipProof() {

channelProof, proofHeight := path.EndpointB.QueryProof(host.ChannelKey(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID))

merklePath := commitmenttypes.NewMerklePath(host.ChannelPath(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID))
merklePath := commitmenttypes.NewMerklePath(host.ChannelKey(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID))
merklePath, err = commitmenttypes.ApplyPrefix(suite.chainB.GetPrefix(), merklePath)
suite.Require().NoError(err)

Expand Down Expand Up @@ -890,7 +890,7 @@ func (suite *KeeperTestSuite) TestQueryVerifyMembershipProof() {
ClientId: ibctesting.FirstClientID,
Proof: []byte{0x01},
ProofHeight: types.NewHeight(1, 100),
MerklePath: commitmenttypes.NewMerklePath("/ibc", host.ChannelPath(mock.PortID, ibctesting.FirstChannelID)),
MerklePath: commitmenttypes.NewMerklePath([]byte("/ibc"), host.ChannelKey(mock.PortID, ibctesting.FirstChannelID)),
}
},
errors.New("empty value"),
Expand All @@ -902,7 +902,7 @@ func (suite *KeeperTestSuite) TestQueryVerifyMembershipProof() {
ClientId: wasmClientID, // use a client type that is not registered
Proof: []byte{0x01},
ProofHeight: types.NewHeight(1, 100),
MerklePath: commitmenttypes.NewMerklePath("/ibc", host.ChannelPath(mock.PortID, ibctesting.FirstChannelID)),
MerklePath: commitmenttypes.NewMerklePath([]byte("/ibc"), host.ChannelKey(mock.PortID, ibctesting.FirstChannelID)),
Value: []byte{0x01},
}
},
Expand All @@ -918,7 +918,7 @@ func (suite *KeeperTestSuite) TestQueryVerifyMembershipProof() {
ClientId: path.EndpointA.ClientID,
Proof: []byte{0x01},
ProofHeight: types.NewHeight(1, 100),
MerklePath: commitmenttypes.NewMerklePath("/ibc", host.ChannelPath(mock.PortID, ibctesting.FirstChannelID)),
MerklePath: commitmenttypes.NewMerklePath([]byte("/ibc"), host.ChannelKey(mock.PortID, ibctesting.FirstChannelID)),
Value: []byte{0x01},
}
},
Expand Down
20 changes: 10 additions & 10 deletions modules/core/03-connection/keeper/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (k *Keeper) VerifyClientState(
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientID)
}

merklePath := commitmenttypes.NewMerklePath(host.FullClientStatePath(connection.Counterparty.ClientId))
merklePath := commitmenttypes.NewMerklePath(host.FullClientStateKey(connection.Counterparty.ClientId))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
return err
Expand Down Expand Up @@ -76,7 +76,7 @@ func (k *Keeper) VerifyClientConsensusState(
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientID)
}

merklePath := commitmenttypes.NewMerklePath(host.FullConsensusStatePath(connection.Counterparty.ClientId, consensusHeight))
merklePath := commitmenttypes.NewMerklePath(host.FullConsensusStateKey(connection.Counterparty.ClientId, consensusHeight))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
return err
Expand Down Expand Up @@ -118,7 +118,7 @@ func (k *Keeper) VerifyConnectionState(
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientID)
}

merklePath := commitmenttypes.NewMerklePath(host.ConnectionPath(connectionID))
merklePath := commitmenttypes.NewMerklePath(host.ConnectionKey(connectionID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
return err
Expand Down Expand Up @@ -161,7 +161,7 @@ func (k *Keeper) VerifyChannelState(
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientID)
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelPath(portID, channelID))
merklePath := commitmenttypes.NewMerklePath(host.ChannelKey(portID, channelID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
return err
Expand Down Expand Up @@ -209,7 +209,7 @@ func (k *Keeper) VerifyPacketCommitment(
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)

merklePath := commitmenttypes.NewMerklePath(host.PacketCommitmentPath(portID, channelID, sequence))
merklePath := commitmenttypes.NewMerklePath(host.PacketCommitmentKey(portID, channelID, sequence))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
return err
Expand Down Expand Up @@ -250,7 +250,7 @@ func (k *Keeper) VerifyPacketAcknowledgement(
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)

merklePath := commitmenttypes.NewMerklePath(host.PacketAcknowledgementPath(portID, channelID, sequence))
merklePath := commitmenttypes.NewMerklePath(host.PacketAcknowledgementKey(portID, channelID, sequence))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
return err
Expand Down Expand Up @@ -297,7 +297,7 @@ func (k *Keeper) VerifyPacketReceiptAbsence(
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)

merklePath := commitmenttypes.NewMerklePath(host.PacketReceiptPath(portID, channelID, sequence))
merklePath := commitmenttypes.NewMerklePath(host.PacketReceiptKey(portID, channelID, sequence))
merklePath, err = commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
return err
Expand Down Expand Up @@ -337,7 +337,7 @@ func (k *Keeper) VerifyNextSequenceRecv(
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)

merklePath := commitmenttypes.NewMerklePath(host.NextSequenceRecvPath(portID, channelID))
merklePath := commitmenttypes.NewMerklePath(host.NextSequenceRecvKey(portID, channelID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
return err
Expand Down Expand Up @@ -374,7 +374,7 @@ func (k *Keeper) VerifyChannelUpgradeError(
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientID)
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradeErrorPath(portID, channelID))
merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradeErrorKey(portID, channelID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
return err
Expand Down Expand Up @@ -416,7 +416,7 @@ func (k *Keeper) VerifyChannelUpgrade(
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientID)
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradePath(portID, channelID))
merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradeKey(portID, channelID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
return err
Expand Down
13 changes: 3 additions & 10 deletions modules/core/23-commitment/types/merkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,9 @@ var _ exported.Path = (*MerklePath)(nil)

// NewMerklePath creates a new MerklePath instance
// The keys must be passed in from root-to-leaf order
func NewMerklePath(keyPath ...string) MerklePath {
var path [][]byte
for _, key := range keyPath {
path = append(path, []byte(key))
}

func NewMerklePath(keyPath ...[]byte) MerklePath {
return MerklePath{
KeyPath: path,
KeyPath: keyPath,
}
}

Expand All @@ -101,9 +96,7 @@ func ApplyPrefix(prefix exported.Prefix, path MerklePath) (MerklePath, error) {
return MerklePath{}, errorsmod.Wrap(ErrInvalidPrefix, "prefix can't be empty")
}

return MerklePath{
KeyPath: append([][]byte{prefix.Bytes()}, path.KeyPath...),
}, nil
return NewMerklePath(append([][]byte{prefix.Bytes()}, path.KeyPath...)...), nil
damiannolan marked this conversation as resolved.
Show resolved Hide resolved
}

// VerifyMembership verifies the membership of a merkle proof against the given root, path, and value.
Expand Down
52 changes: 26 additions & 26 deletions modules/core/23-commitment/types/merkle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ func (suite *MerkleTestSuite) TestVerifyMembership() {
cases := []struct {
name string
root []byte
pathArr []string
pathArr [][]byte
value []byte
malleate func()
shouldPass bool
}{
{"valid proof", cid.Hash, []string{suite.storeKey.Name(), "MYKEY"}, []byte("MYVALUE"), func() {}, true}, // valid proof
{"wrong value", cid.Hash, []string{suite.storeKey.Name(), "MYKEY"}, []byte("WRONGVALUE"), func() {}, false}, // invalid proof with wrong value
{"nil value", cid.Hash, []string{suite.storeKey.Name(), "MYKEY"}, []byte(nil), func() {}, false}, // invalid proof with nil value
{"wrong key", cid.Hash, []string{suite.storeKey.Name(), "NOTMYKEY"}, []byte("MYVALUE"), func() {}, false}, // invalid proof with wrong key
{"wrong path 1", cid.Hash, []string{suite.storeKey.Name(), "MYKEY", "MYKEY"}, []byte("MYVALUE"), func() {}, false}, // invalid proof with wrong path
{"wrong path 2", cid.Hash, []string{suite.storeKey.Name()}, []byte("MYVALUE"), func() {}, false}, // invalid proof with wrong path
{"wrong path 3", cid.Hash, []string{"MYKEY"}, []byte("MYVALUE"), func() {}, false}, // invalid proof with wrong path
{"wrong storekey", cid.Hash, []string{"otherStoreKey", "MYKEY"}, []byte("MYVALUE"), func() {}, false}, // invalid proof with wrong store prefix
{"wrong root", []byte("WRONGROOT"), []string{suite.storeKey.Name(), "MYKEY"}, []byte("MYVALUE"), func() {}, false}, // invalid proof with wrong root
{"nil root", []byte(nil), []string{suite.storeKey.Name(), "MYKEY"}, []byte("MYVALUE"), func() {}, false}, // invalid proof with nil root
{"proof is wrong length", cid.Hash, []string{suite.storeKey.Name(), "MYKEY"}, []byte("MYVALUE"), func() {
{"valid proof", cid.Hash, [][]byte{[]byte(suite.storeKey.Name()), []byte("MYKEY")}, []byte("MYVALUE"), func() {}, true}, // valid proof
{"wrong value", cid.Hash, [][]byte{[]byte(suite.storeKey.Name()), []byte("MYKEY")}, []byte("WRONGVALUE"), func() {}, false}, // invalid proof with wrong value
{"nil value", cid.Hash, [][]byte{[]byte(suite.storeKey.Name()), []byte("MYKEY")}, []byte(nil), func() {}, false}, // invalid proof with nil value
{"wrong key", cid.Hash, [][]byte{[]byte(suite.storeKey.Name()), []byte("NOTMYKEY")}, []byte("MYVALUE"), func() {}, false}, // invalid proof with wrong key
{"wrong path 1", cid.Hash, [][]byte{[]byte(suite.storeKey.Name()), []byte("MYKEY"), []byte("MYKEY")}, []byte("MYVALUE"), func() {}, false}, // invalid proof with wrong path
{"wrong path 2", cid.Hash, [][]byte{[]byte(suite.storeKey.Name())}, []byte("MYVALUE"), func() {}, false}, // invalid proof with wrong path
{"wrong path 3", cid.Hash, [][]byte{[]byte("MYKEY")}, []byte("MYVALUE"), func() {}, false}, // invalid proof with wrong path
{"wrong storekey", cid.Hash, [][]byte{[]byte("otherStoreKey"), []byte("MYKEY")}, []byte("MYVALUE"), func() {}, false}, // invalid proof with wrong store prefix
{"wrong root", []byte("WRONGROOT"), [][]byte{[]byte(suite.storeKey.Name()), []byte("MYKEY")}, []byte("MYVALUE"), func() {}, false}, // invalid proof with wrong root
{"nil root", []byte(nil), [][]byte{[]byte(suite.storeKey.Name()), []byte("MYKEY")}, []byte("MYVALUE"), func() {}, false}, // invalid proof with nil root
{"proof is wrong length", cid.Hash, [][]byte{[]byte(suite.storeKey.Name()), []byte("MYKEY")}, []byte("MYVALUE"), func() {
proof = types.MerkleProof{
Proofs: proof.Proofs[1:],
}
Expand Down Expand Up @@ -97,20 +97,20 @@ func (suite *MerkleTestSuite) TestVerifyNonMembership() {
cases := []struct {
name string
root []byte
pathArr []string
pathArr [][]byte
malleate func()
shouldPass bool
}{
{"valid proof", cid.Hash, []string{suite.storeKey.Name(), "MYABSENTKEY"}, func() {}, true}, // valid proof
{"wrong key", cid.Hash, []string{suite.storeKey.Name(), "MYKEY"}, func() {}, false}, // invalid proof with existent key
{"wrong path 1", cid.Hash, []string{suite.storeKey.Name(), "MYKEY", "MYABSENTKEY"}, func() {}, false}, // invalid proof with wrong path
{"wrong path 2", cid.Hash, []string{suite.storeKey.Name(), "MYABSENTKEY", "MYKEY"}, func() {}, false}, // invalid proof with wrong path
{"wrong path 3", cid.Hash, []string{suite.storeKey.Name()}, func() {}, false}, // invalid proof with wrong path
{"wrong path 4", cid.Hash, []string{"MYABSENTKEY"}, func() {}, false}, // invalid proof with wrong path
{"wrong storeKey", cid.Hash, []string{"otherStoreKey", "MYABSENTKEY"}, func() {}, false}, // invalid proof with wrong store prefix
{"wrong root", []byte("WRONGROOT"), []string{suite.storeKey.Name(), "MYABSENTKEY"}, func() {}, false}, // invalid proof with wrong root
{"nil root", []byte(nil), []string{suite.storeKey.Name(), "MYABSENTKEY"}, func() {}, false}, // invalid proof with nil root
{"proof is wrong length", cid.Hash, []string{suite.storeKey.Name(), "MYKEY"}, func() {
{"valid proof", cid.Hash, [][]byte{[]byte(suite.storeKey.Name()), []byte("MYABSENTKEY")}, func() {}, true}, // valid proof
{"wrong key", cid.Hash, [][]byte{[]byte(suite.storeKey.Name()), []byte("MYKEY")}, func() {}, false}, // invalid proof with existent key
{"wrong path 1", cid.Hash, [][]byte{[]byte(suite.storeKey.Name()), []byte("MYKEY"), []byte("MYABSENTKEY")}, func() {}, false}, // invalid proof with wrong path
{"wrong path 2", cid.Hash, [][]byte{[]byte(suite.storeKey.Name()), []byte("MYABSENTKEY"), []byte("MYKEY")}, func() {}, false}, // invalid proof with wrong path
{"wrong path 3", cid.Hash, [][]byte{[]byte(suite.storeKey.Name())}, func() {}, false}, // invalid proof with wrong path
{"wrong path 4", cid.Hash, [][]byte{[]byte("MYABSENTKEY")}, func() {}, false}, // invalid proof with wrong path
{"wrong storeKey", cid.Hash, [][]byte{[]byte("otherStoreKey"), []byte("MYABSENTKEY")}, func() {}, false}, // invalid proof with wrong store prefix
{"wrong root", []byte("WRONGROOT"), [][]byte{[]byte(suite.storeKey.Name()), []byte("MYABSENTKEY")}, func() {}, false}, // invalid proof with wrong root
{"nil root", []byte(nil), [][]byte{[]byte(suite.storeKey.Name()), []byte("MYABSENTKEY")}, func() {}, false}, // invalid proof with nil root
{"proof is wrong length", cid.Hash, [][]byte{[]byte(suite.storeKey.Name()), []byte("MYKEY")}, func() {
proof = types.MerkleProof{
Proofs: proof.Proofs[1:],
}
Expand Down Expand Up @@ -143,9 +143,9 @@ func (suite *MerkleTestSuite) TestVerifyNonMembership() {
func TestApplyPrefix(t *testing.T) {
prefix := types.NewMerklePrefix([]byte("storePrefixKey"))

pathStr := "pathone/pathtwo/paththree/key"
pathBz := []byte("pathone/pathtwo/paththree/key")
path := types.MerklePath{
KeyPath: [][]byte{[]byte(pathStr)},
KeyPath: [][]byte{pathBz},
}

prefixedPath, err := types.ApplyPrefix(prefix, path)
Expand All @@ -158,5 +158,5 @@ func TestApplyPrefix(t *testing.T) {

key1, err := prefixedPath.GetKey(1)
require.NoError(t, err, "get key 1 returns error")
require.Equal(t, []byte(pathStr), key1, "key 1 does not match expected value")
require.Equal(t, pathBz, key1, "key 1 does not match expected value")
}
4 changes: 2 additions & 2 deletions modules/core/23-commitment/types/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func (suite *MerkleTestSuite) TestConvertProofs() {
cid := suite.store.Commit()

root := types.NewMerkleRoot(cid.Hash)
existsPath := types.NewMerklePath(suite.storeKey.Name(), "MYKEY")
nonexistPath := types.NewMerklePath(suite.storeKey.Name(), "NOTMYKEY")
existsPath := types.NewMerklePath([]byte(suite.storeKey.Name()), []byte("MYKEY"))
nonexistPath := types.NewMerklePath([]byte(suite.storeKey.Name()), []byte("NOTMYKEY"))
value := []byte("MYVALUE")

var proofOps *crypto.ProofOps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ func (suite *SoloMachineTestSuite) TestVerifyMembership() {
clientID = sm.ClientID
clientState = sm.ClientState()

path = commitmenttypes.NewMerklePath("ibc", "solomachine")
path = commitmenttypes.NewMerklePath([]byte("ibc"), []byte("solomachine"))
merklePath, ok := path.(commitmenttypes.MerklePath)
suite.Require().True(ok)
key, err := merklePath.GetKey(1) // in a multistore context: index 0 is the key for the IBC store in the multistore, index 1 is the key in the IBC store
Expand Down Expand Up @@ -858,7 +858,7 @@ func (suite *SoloMachineTestSuite) TestVerifyNonMembership() {
clientState = sm.ClientState()
clientID = sm.ClientID

path = commitmenttypes.NewMerklePath("ibc", "solomachine")
path = commitmenttypes.NewMerklePath([]byte("ibc"), []byte("solomachine"))
merklePath, ok := path.(commitmenttypes.MerklePath)
suite.Require().True(ok)
key, err := merklePath.GetKey(1) // in a multistore context: index 0 is the key for the IBC store in the multistore, index 1 is the key in the IBC store
Expand Down
Loading
Loading