Skip to content

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Nov 23, 2020
1 parent 7d2c136 commit 6027ded
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 45 deletions.
4 changes: 2 additions & 2 deletions doc/proto.md
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ QueryContractHistoryRequest is the request type for the Query/ContractHistory RP

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| address | [string](#string) | | address is the address of the contract to query´ |
| address | [string](#string) | | address is the address of the contract to query |
| pagination | [cosmos.base.query.v1beta1.PageRequest](#cosmos.base.query.v1beta1.PageRequest) | | pagination defines an optional pagination for the request. |


Expand Down Expand Up @@ -714,7 +714,7 @@ Query provides defines the gRPC querier service
<a name="wasmd.x.wasmd.v1beta1.AbsoluteTxPosition"></a>

### AbsoluteTxPosition
AbsoluteTxPosition is a block position that can be used to sort contracts
AbsoluteTxPosition is a unique transaction position that allows for global ordering of transactions.


| Field | Type | Label | Description |
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var (
ConvertToProposals = types.ConvertToProposals
GetCodeKey = types.GetCodeKey
GetContractAddressKey = types.GetContractAddressKey
GetContractStorePrefixKey = types.GetContractStorePrefixKey
GetContractStorePrefixKey = types.GetContractStorePrefix
NewCodeInfo = types.NewCodeInfo
NewAbsoluteTxPosition = types.NewAbsoluteTxPosition
NewContractInfo = types.NewContractInfo
Expand Down
12 changes: 6 additions & 6 deletions x/wasm/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (k Keeper) instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.A

// create prefixed data store
// 0x03 | contractAddress (sdk.AccAddress)
prefixStoreKey := types.GetContractStorePrefixKey(contractAddress)
prefixStoreKey := types.GetContractStorePrefix(contractAddress)
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)

// prepare querier
Expand Down Expand Up @@ -351,7 +351,7 @@ func (k Keeper) migrate(ctx sdk.Context, contractAddress sdk.AccAddress, caller
Plugins: k.queryPlugins,
}

prefixStoreKey := types.GetContractStorePrefixKey(contractAddress)
prefixStoreKey := types.GetContractStorePrefix(contractAddress)
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)
gas := gasForContract(ctx)
res, gasUsed, err := k.wasmer.Migrate(newCodeInfo.CodeHash, env, info, msg, &prefixStore, cosmwasmAPI, &querier, gasMeter(ctx), gas)
Expand Down Expand Up @@ -464,7 +464,7 @@ func (k Keeper) QueryRaw(ctx sdk.Context, contractAddress sdk.AccAddress, key []
if key == nil {
return nil
}
prefixStoreKey := types.GetContractStorePrefixKey(contractAddress)
prefixStoreKey := types.GetContractStorePrefix(contractAddress)
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)
return prefixStore.Get(key)
}
Expand All @@ -485,7 +485,7 @@ func (k Keeper) contractInstance(ctx sdk.Context, contractAddress sdk.AccAddress
}
var codeInfo types.CodeInfo
k.cdc.MustUnmarshalBinaryBare(contractInfoBz, &codeInfo)
prefixStoreKey := types.GetContractStorePrefixKey(contractAddress)
prefixStoreKey := types.GetContractStorePrefix(contractAddress)
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)
return codeInfo, prefixStore, nil
}
Expand Down Expand Up @@ -526,13 +526,13 @@ func (k Keeper) IterateContractInfo(ctx sdk.Context, cb func(sdk.AccAddress, typ
}

func (k Keeper) GetContractState(ctx sdk.Context, contractAddress sdk.AccAddress) sdk.Iterator {
prefixStoreKey := types.GetContractStorePrefixKey(contractAddress)
prefixStoreKey := types.GetContractStorePrefix(contractAddress)
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)
return prefixStore.Iterator(nil, nil)
}

func (k Keeper) importContractState(ctx sdk.Context, contractAddress sdk.AccAddress, models []types.Model) error {
prefixStoreKey := types.GetContractStorePrefixKey(contractAddress)
prefixStoreKey := types.GetContractStorePrefix(contractAddress)
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)
for _, model := range models {
if model.Value == nil {
Expand Down
32 changes: 32 additions & 0 deletions x/wasm/internal/keeper/legacy_querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package keeper
import (
"encoding/json"
"reflect"
"sort"
"strconv"
"strings"

"github.com/CosmWasm/wasmd/x/wasm/internal/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -142,3 +144,33 @@ func queryContractHistory(ctx sdk.Context, contractAddr sdk.AccAddress, keeper K
}
return history, nil
}

func queryContractListByCode(ctx sdk.Context, codeID uint64, keeper Keeper) ([]types.ContractInfoWithAddress, error) {
var contracts []types.ContractInfoWithAddress
keeper.IterateContractInfo(ctx, func(addr sdk.AccAddress, info types.ContractInfo) bool {
if info.CodeID == codeID {
// and add the address
infoWithAddress := types.ContractInfoWithAddress{
Address: addr.String(),
ContractInfo: &info,
}
contracts = append(contracts, infoWithAddress)
}
return false
})

// now we sort them by AbsoluteTxPosition
sort.Slice(contracts, func(i, j int) bool {
this := contracts[i].ContractInfo.Created
other := contracts[j].ContractInfo.Created
if this.Equal(other) {
return strings.Compare(contracts[i].Address, contracts[j].Address) < 0
}
return this.LessThan(other)
})

for i := range contracts {
contracts[i].Created = nil
}
return contracts, nil
}
28 changes: 1 addition & 27 deletions x/wasm/internal/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper
import (
"context"
"encoding/binary"
"sort"

"github.com/CosmWasm/wasmd/x/wasm/internal/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
Expand Down Expand Up @@ -113,7 +112,7 @@ func (q grpcQuerier) AllContractState(c context.Context, req *types.QueryAllCont
}

r := make([]types.Model, 0)
prefixStore := prefix.NewStore(ctx.KVStore(q.keeper.storeKey), types.GetContractStorePrefixKey(contractAddr))
prefixStore := prefix.NewStore(ctx.KVStore(q.keeper.storeKey), types.GetContractStorePrefix(contractAddr))
pageRes, err := query.FilteredPaginate(prefixStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
if accumulate {
r = append(r, types.Model{
Expand Down Expand Up @@ -221,31 +220,6 @@ func queryContractInfo(ctx sdk.Context, addr sdk.AccAddress, keeper Keeper) (*ty
}, nil
}

func queryContractListByCode(ctx sdk.Context, codeID uint64, keeper Keeper) ([]types.ContractInfoWithAddress, error) {
var contracts []types.ContractInfoWithAddress
keeper.IterateContractInfo(ctx, func(addr sdk.AccAddress, info types.ContractInfo) bool {
if info.CodeID == codeID {
// and add the address
infoWithAddress := types.ContractInfoWithAddress{
Address: addr.String(),
ContractInfo: &info,
}
contracts = append(contracts, infoWithAddress)
}
return false
})

// now we sort them by AbsoluteTxPosition
sort.Slice(contracts, func(i, j int) bool {
return contracts[i].ContractInfo.Created.LessThan(contracts[j].ContractInfo.Created)
})

for i := range contracts {
contracts[i].Created = nil
}
return contracts, nil
}

func queryCode(ctx sdk.Context, codeID uint64, keeper *Keeper) (*types.QueryCodeResponse, error) {
if codeID == 0 {
return nil, nil
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/internal/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func GetContractAddressKey(addr sdk.AccAddress) []byte {
return append(ContractKeyPrefix, addr...)
}

// GetContractStorePrefixKey returns the store prefix for the WASM contract instance
func GetContractStorePrefixKey(addr sdk.AccAddress) []byte {
// GetContractStorePrefix returns the store prefix for the WASM contract instance
func GetContractStorePrefix(addr sdk.AccAddress) []byte {
return append(ContractStorePrefix, addr...)
}

Expand Down
2 changes: 1 addition & 1 deletion x/wasm/internal/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions x/wasm/internal/types/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ message QueryContractInfoResponse {

// QueryContractHistoryRequest is the request type for the Query/ContractHistory RPC method
message QueryContractHistoryRequest {
// address is the address of the contract to query´
// address is the address of the contract to query
string address = 1;
// pagination defines an optional pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 2;
Expand Down Expand Up @@ -116,8 +116,8 @@ message QueryRawContractStateRequest {
// address is the address of the contract
string address = 1;
bytes query_data = 2;
;
}

// QueryRawContractStateResponse is the response type for the Query/RawContractState RPC method
message QueryRawContractStateResponse {
// Data contains the raw store data
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (c *ContractInfo) AdminAddr() sdk.AccAddress {

// NewAbsoluteTxPosition gets a block position from the context
func NewAbsoluteTxPosition(ctx sdk.Context) *AbsoluteTxPosition {
// we muxst safely handle nil gas meters
// we must safely handle nil gas meters
var index uint64
meter := ctx.BlockGasMeter()
if meter != nil {
Expand Down Expand Up @@ -160,7 +160,7 @@ func (a *AbsoluteTxPosition) LessThan(b *AbsoluteTxPosition) bool {
// AbsoluteTxPositionLen number of elements in byte representation
const AbsoluteTxPositionLen = 16

// Bytes encodes the object into a 16 byte representation with big endian block height adn tx index.
// Bytes encodes the object into a 16 byte representation with big endian block height and tx index.
func (a *AbsoluteTxPosition) Bytes() []byte {
if a == nil {
panic("object must not be nil")
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/internal/types/types.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion x/wasm/internal/types/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ message ContractCodeHistoryEntry {

}

// AbsoluteTxPosition is a block position that can be used to sort contracts
// AbsoluteTxPosition is a unique transaction position that allows for global ordering of transactions.
message AbsoluteTxPosition {
// BlockHeight is the block the contract was created at
uint64 block_height = 1;
Expand Down

0 comments on commit 6027ded

Please sign in to comment.