Skip to content

Commit

Permalink
chore: adding type alias for code hash byte slices (#5004)
Browse files Browse the repository at this point in the history
* chore: adding type alias for code hash byte slices

* chore: adjust tests and add type conversions for code hashes

* chore: initialise slice in favour of nil declaration
  • Loading branch information
damiannolan authored Nov 1, 2023
1 parent fe602e0 commit 9e15947
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
4 changes: 3 additions & 1 deletion modules/light-clients/08-wasm/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
wasmvmtypes "github.com/CosmWasm/wasmvm/types"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
Expand Down Expand Up @@ -29,7 +31,7 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) types.GenesisState {
// Grab code from wasmVM and add to genesis state.
var genesisState types.GenesisState
for _, codeHash := range codeHashes {
code, err := k.wasmVM.GetCode(codeHash)
code, err := k.wasmVM.GetCode(wasmvmtypes.Checksum(codeHash))
if err != nil {
panic(err)
}
Expand Down
1 change: 0 additions & 1 deletion modules/light-clients/08-wasm/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func (suite *KeeperTestSuite) TestInitGenesis() {
var storedHashes []string
codeHashes, err := types.GetAllCodeHashes(suite.chainA.GetContext())
suite.Require().NoError(err)
suite.Require().NotNil(codeHashes)

for _, hash := range codeHashes {
storedHashes = append(storedHashes, hex.EncodeToString(hash))
Expand Down
4 changes: 3 additions & 1 deletion modules/light-clients/08-wasm/keeper/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/hex"
"io"

wasmvmtypes "github.com/CosmWasm/wasmvm/types"

errorsmod "cosmossdk.io/errors"
snapshot "cosmossdk.io/store/snapshots/types"
storetypes "cosmossdk.io/store/types"
Expand Down Expand Up @@ -72,7 +74,7 @@ func (ws *WasmSnapshotter) SnapshotExtension(height uint64, payloadWriter snapsh
}

for _, codeHash := range codeHashes {
wasmCode, err := ws.keeper.wasmVM.GetCode(codeHash)
wasmCode, err := ws.keeper.wasmVM.GetCode(wasmvmtypes.Checksum(codeHash))
if err != nil {
return err
}
Expand Down
14 changes: 9 additions & 5 deletions modules/light-clients/08-wasm/types/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,33 @@ import (
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/internal/ibcwasm"
)

// CodeHash is a type alias used for wasm byte code checksums.
type CodeHash []byte

// GetAllCodeHashes is a helper to get all code hashes from the store.
// It returns an empty slice if no code hashes are found
func GetAllCodeHashes(ctx sdk.Context) ([][]byte, error) {
func GetAllCodeHashes(ctx sdk.Context) ([]CodeHash, error) {
iterator, err := ibcwasm.CodeHashes.Iterate(ctx, nil)
if err != nil {
return nil, err
}

codeHashes, err := iterator.Keys()
keys, err := iterator.Keys()
if err != nil {
return nil, err
}

if codeHashes == nil {
codeHashes = [][]byte{}
codeHashes := []CodeHash{}
for _, key := range keys {
codeHashes = append(codeHashes, key)
}

return codeHashes, nil
}

// HasCodeHash returns true if the given code hash exists in the store and
// false otherwise.
func HasCodeHash(ctx sdk.Context, codeHash []byte) bool {
func HasCodeHash(ctx sdk.Context, codeHash CodeHash) bool {
found, err := ibcwasm.CodeHashes.Has(ctx, codeHash)
if err != nil {
return false
Expand Down
24 changes: 12 additions & 12 deletions modules/light-clients/08-wasm/types/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ func (suite *TypesTestSuite) TestGetCodeHashes() {
testCases := []struct {
name string
malleate func()
expResult func(codeHashes [][]byte)
expResult func(codeHashes []types.CodeHash)
}{
{
"success: no contract stored.",
func() {},
func(codeHashes [][]byte) {
func(codeHashes []types.CodeHash) {
suite.Require().Len(codeHashes, 0)
},
},
Expand All @@ -26,23 +26,23 @@ func (suite *TypesTestSuite) TestGetCodeHashes() {
func() {
suite.SetupWasmWithMockVM()
},
func(codeHashes [][]byte) {
func(codeHashes []types.CodeHash) {
suite.Require().Len(codeHashes, 1)
expectedCodeHash := sha256.Sum256(wasmtesting.Code)
suite.Require().Equal(expectedCodeHash[:], codeHashes[0])
suite.Require().Equal(types.CodeHash(expectedCodeHash[:]), codeHashes[0])
},
},
{
"success: non-empty code hashes",
func() {
suite.SetupWasmWithMockVM()

err := ibcwasm.CodeHashes.Set(suite.chainA.GetContext(), []byte("codehash"))
err := ibcwasm.CodeHashes.Set(suite.chainA.GetContext(), types.CodeHash("codehash"))
suite.Require().NoError(err)
},
func(codeHashes [][]byte) {
func(codeHashes []types.CodeHash) {
suite.Require().Len(codeHashes, 2)
suite.Require().Contains(codeHashes, []byte("codehash"))
suite.Require().Contains(codeHashes, types.CodeHash("codehash"))
},
},
}
Expand All @@ -67,8 +67,8 @@ func (suite *TypesTestSuite) TestAddCodeHash() {
// default mock vm contract is stored
suite.Require().Len(codeHashes, 1)

codeHash1 := []byte("codehash1")
codeHash2 := []byte("codehash2")
codeHash1 := types.CodeHash("codehash1")
codeHash2 := types.CodeHash("codehash2")
err = ibcwasm.CodeHashes.Set(suite.chainA.GetContext(), codeHash1)
suite.Require().NoError(err)
err = ibcwasm.CodeHashes.Set(suite.chainA.GetContext(), codeHash2)
Expand All @@ -86,7 +86,7 @@ func (suite *TypesTestSuite) TestAddCodeHash() {
}

func (suite *TypesTestSuite) TestHasCodeHash() {
var codeHash []byte
var codeHash types.CodeHash

testCases := []struct {
name string
Expand All @@ -96,7 +96,7 @@ func (suite *TypesTestSuite) TestHasCodeHash() {
{
"success: code hash exists",
func() {
codeHash = []byte("codehash")
codeHash = types.CodeHash("codehash")
err := ibcwasm.CodeHashes.Set(suite.chainA.GetContext(), codeHash)
suite.Require().NoError(err)
},
Expand All @@ -105,7 +105,7 @@ func (suite *TypesTestSuite) TestHasCodeHash() {
{
"success: code hash does not exist",
func() {
codeHash = []byte("non-existent-codehash")
codeHash = types.CodeHash("non-existent-codehash")
},
false,
},
Expand Down

0 comments on commit 9e15947

Please sign in to comment.