Skip to content

Commit

Permalink
v1.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
assafmo committed Sep 14, 2021
1 parent 5405c63 commit 594160b
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/enigmampc/SecretNetwork
go 1.14

require (
github.com/enigmampc/cosmos-sdk v0.9.0-scrt
github.com/enigmampc/cosmos-sdk v0.9.1-scrt
github.com/google/gofuzz v1.0.0
github.com/gorilla/mux v1.7.4
github.com/miscreant/miscreant.go v0.0.0-20200214223636-26d376326b75
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ github.com/99designs/keyring v1.1.3/go.mod h1:657DQuMrBZRtuL/voxVyiyb6zpMehlm5vL
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/ChainSafe/go-schnorrkel v0.0.0-20200102211924-4bcbc698314f h1:4O1om+UVU+Hfcihr1timk8YNXHxzZWgCo7ofnrZRApw=
github.com/ChainSafe/go-schnorrkel v0.0.0-20200102211924-4bcbc698314f/go.mod h1:URdX5+vg25ts3aCh8H5IFZybJYKWhJHYMTnf+ULtoC4=
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d h1:nalkkPQcITbvhmL4+C4cKA87NW0tfm3Kl9VXRoPywFg=
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d/go.mod h1:URdX5+vg25ts3aCh8H5IFZybJYKWhJHYMTnf+ULtoC4=
Expand Down Expand Up @@ -113,8 +112,8 @@ github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5m
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/enigmampc/cosmos-sdk v0.9.0-scrt h1:xWGqWmBXx+v/RbQG2+wFUxw1YrTzGtb0wXSM1pWxNYc=
github.com/enigmampc/cosmos-sdk v0.9.0-scrt/go.mod h1:3bJQ86UAzUlL3G7/HdTdFn4cfRtu7WBbE02sv85bqZY=
github.com/enigmampc/cosmos-sdk v0.9.1-scrt h1:PXOVcjo/ZWUvyR04CYI3jhWCgCMiJzEBQNbsdvbvJuY=
github.com/enigmampc/cosmos-sdk v0.9.1-scrt/go.mod h1:3bJQ86UAzUlL3G7/HdTdFn4cfRtu7WBbE02sv85bqZY=
github.com/envoyproxy/go-control-plane v0.6.9/go.mod h1:SBwIajubJHhxtWwsL9s8ss4safvEdbitLhGGK48rN6g=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down
12 changes: 7 additions & 5 deletions x/compute/internal/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) error

var maxContractID int
for i, contract := range data.Contracts {
err := keeper.importContract(ctx, contract.ContractAddress, &contract.ContractInfo, contract.ContractState)
err := keeper.importContract(ctx, contract.ContractAddress, &contract.ContractCustomInfo, &contract.ContractInfo, contract.ContractState)
if err != nil {
return sdkerrors.Wrapf(err, "contract number %d", i)
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
return false
})

keeper.IterateContractInfo(ctx, func(addr sdk.AccAddress, contract types.ContractInfo) bool {
keeper.IterateContractInfo(ctx, func(addr sdk.AccAddress, contract types.ContractInfo, contractCustomInfo types.ContractCustomInfo) bool {
contractStateIterator := keeper.GetContractState(ctx, addr)
var state []types.Model
for ; contractStateIterator.Valid(); contractStateIterator.Next() {
Expand All @@ -80,13 +80,15 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
}
state = append(state, m)
}

// redact contract info
contract.Created = nil

genState.Contracts = append(genState.Contracts, types.Contract{
ContractAddress: addr,
ContractInfo: contract,
ContractState: state,
ContractAddress: addr,
ContractInfo: contract,
ContractState: state,
ContractCustomInfo: contractCustomInfo,
})

return false
Expand Down
52 changes: 49 additions & 3 deletions x/compute/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,14 +671,33 @@ func (k Keeper) setContractInfo(ctx sdk.Context, contractAddress sdk.AccAddress,
store.Set(types.GetContractAddressKey(contractAddress), k.cdc.MustMarshalBinaryBare(contract))
}

func (k Keeper) IterateContractInfo(ctx sdk.Context, cb func(sdk.AccAddress, types.ContractInfo) bool) {
func (k Keeper) setContractCustomInfo(ctx sdk.Context, contractAddress sdk.AccAddress, contract *types.ContractCustomInfo) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetContractEnclaveKey(contractAddress), contract.EnclaveKey)
fmt.Printf("Setting enclave key: %x: %x\n", types.GetContractEnclaveKey(contractAddress), contract.EnclaveKey)
store.Set(types.GetContractLabelPrefix(contract.Label), contractAddress)
fmt.Printf("Setting label: %x: %x\n", types.GetContractLabelPrefix(contract.Label), contractAddress)
}

func (k Keeper) IterateContractInfo(ctx sdk.Context, cb func(sdk.AccAddress, types.ContractInfo, types.ContractCustomInfo) bool) {

prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.ContractKeyPrefix)
iter := prefixStore.Iterator(nil, nil)
for ; iter.Valid(); iter.Next() {
var contract types.ContractInfo
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &contract)

enclaveId := ctx.KVStore(k.storeKey).Get(types.GetContractEnclaveKey(iter.Key()))
fmt.Printf("Setting enclave key: %x: %x\n", types.GetContractEnclaveKey(iter.Key()), enclaveId)
fmt.Printf("Setting label: %x: %x\n", types.GetContractLabelPrefix(contract.Label), contractAddress)

contractCustomInfo := types.ContractCustomInfo{
EnclaveKey: enclaveId,
Label: contract.Label,
}

// cb returns true to stop early
if cb(iter.Key(), contract) {
if cb(iter.Key(), contract, contractCustomInfo) {
break
}
}
Expand All @@ -697,10 +716,36 @@ func (k Keeper) importContractState(ctx sdk.Context, contractAddress sdk.AccAddr
if model.Value == nil {
model.Value = []byte{}
}

if prefixStore.Has(model.Key) {
return sdkerrors.Wrapf(types.ErrDuplicate, "duplicate key: %x", model.Key)
}
prefixStore.Set(model.Key, model.Value)

}
return nil
}

func (k Keeper) fixContractState(ctx sdk.Context, contractAddress sdk.AccAddress, models []types.Model) error {
prefixStoreKey := types.GetContractStorePrefixKey(contractAddress)
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)
for _, model := range models {
if model.Value == nil {
model.Value = []byte{}
}

if !prefixStore.Has(model.Key) {
prefixStore.Set(model.Key, model.Value)
continue
}

existingValue := prefixStore.Get(model.Key)
if bytes.Equal(existingValue, model.Value) {
continue
} else {
prefixStore.Set(model.Key, model.Value)
}

}
return nil
}
Expand Down Expand Up @@ -829,7 +874,7 @@ func (k Keeper) importAutoIncrementID(ctx sdk.Context, lastIDKey []byte, val uin
return nil
}

func (k Keeper) importContract(ctx sdk.Context, contractAddr sdk.AccAddress, c *types.ContractInfo, state []types.Model) error {
func (k Keeper) importContract(ctx sdk.Context, contractAddr sdk.AccAddress, customInfo *types.ContractCustomInfo, c *types.ContractInfo, state []types.Model) error {
if !k.containsCodeInfo(ctx, c.CodeID) {
return errors.Wrapf(types.ErrNotFound, "code id: %d", c.CodeID)
}
Expand All @@ -839,6 +884,7 @@ func (k Keeper) importContract(ctx sdk.Context, contractAddr sdk.AccAddress, c *

// historyEntry := c.ResetFromGenesis(ctx)
// k.appendToContractHistory(ctx, contractAddr, historyEntry)
k.setContractCustomInfo(ctx, contractAddr, customInfo)
k.setContractInfo(ctx, contractAddr, c)
return k.importContractState(ctx, contractAddr, state)
}
Expand Down
2 changes: 1 addition & 1 deletion x/compute/internal/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func queryContractListByCode(ctx sdk.Context, codeIDstr string, keeper Keeper) (
}

var contracts []ContractInfoWithAddress
keeper.IterateContractInfo(ctx, func(addr sdk.AccAddress, info types.ContractInfo) bool {
keeper.IterateContractInfo(ctx, func(addr sdk.AccAddress, info types.ContractInfo, _ types.ContractCustomInfo) bool {
if info.CodeID == codeID {
// and add the address
infoWithAddress := ContractInfoWithAddress{
Expand Down
8 changes: 5 additions & 3 deletions x/compute/internal/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ func (c Code) ValidateBasic() error {

// Contract struct encompasses ContractAddress, ContractInfo, and ContractState
type Contract struct {
ContractAddress sdk.AccAddress `json:"contract_address"`
ContractInfo ContractInfo `json:"contract_info"`
ContractState []Model `json:"contract_state"`
ContractAddress sdk.AccAddress `json:"contract_address"`
ContractInfo ContractInfo `json:"contract_info"`
ContractState []Model `json:"contract_state"`
ContractCustomInfo ContractCustomInfo `json:"contract_custom_info"`
}

func (c Contract) ValidateBasic() error {
Expand All @@ -92,6 +93,7 @@ func (c Contract) ValidateBasic() error {
return sdkerrors.Wrapf(err, "contract state %d", i)
}
}

return nil
}

Expand Down
8 changes: 8 additions & 0 deletions x/compute/internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ type ContractCodeHistoryEntry struct {
}
*/

// ContractInfo stores a WASM contract instance
type ContractCustomInfo struct {
EnclaveKey []byte `json:"enclave_key"`
//Address sdk.AccAddress `json:"address"`
// Admin sdk.AccAddress `json:"admin,omitempty"`
Label string `json:"label"`
}

// ContractInfo stores a WASM contract instance
type ContractInfo struct {
CodeID uint64 `json:"code_id"`
Expand Down

0 comments on commit 594160b

Please sign in to comment.