Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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: 5 additions & 3 deletions app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
type HandlerOptions struct {
ante.HandlerOptions

IBCKeeper *keeper.Keeper
WasmConfig *compute.WasmConfig
TXCounterStoreKey sdk.StoreKey
IBCKeeper *keeper.Keeper
WasmConfig *compute.WasmConfig
TXCounterStoreKey sdk.StoreKey
LastMsgMarkerStoreKey sdk.StoreKey
}

func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
Expand All @@ -39,6 +40,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {

anteDecorators := []sdk.AnteDecorator{
compute.NewCountTXDecorator(options.TXCounterStoreKey),
compute.NewLastMsgDecorator(options.LastMsgMarkerStoreKey),
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
ante.NewRejectExtensionOptionsDecorator(),
ante.NewMempoolFeeDecorator(),
Expand Down
7 changes: 4 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,10 @@ func NewSecretNetworkApp(
SignModeHandler: encodingConfig.TxConfig.SignModeHandler(),
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
},
IBCKeeper: app.AppKeepers.IbcKeeper,
WasmConfig: computeConfig,
TXCounterStoreKey: app.AppKeepers.GetKey(compute.StoreKey),
IBCKeeper: app.AppKeepers.IbcKeeper,
WasmConfig: computeConfig,
TXCounterStoreKey: app.AppKeepers.GetKey(compute.StoreKey),
LastMsgMarkerStoreKey: app.AppKeepers.GetKey(compute.StoreKey),
})
if err != nil {
panic(fmt.Errorf("failed to create AnteHandler: %s", err))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,28 @@ pub fn wasm_msg(ty: String) -> StdResult<Response> {
#[entry_point]
pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> StdResult<Response> {
match msg {
ExecuteMsg::Special {} => {
let s = SubMsg {
id: 0,
msg: CosmosMsg::Wasm(WasmMsg::Execute {
code_hash: env.contract.code_hash,
contract_addr: env.contract.address.into_string(),
msg: Binary::from("{\"increment\":{\"addition\":1}}".as_bytes().to_vec()),
funds: vec![],
})
.into(),
reply_on: ReplyOn::Never,
gas_limit: None,
};

Ok(Response::new().add_submessages(vec![
s.clone(),
s.clone(),
s.clone(),
s.clone(),
s.clone(),
]))
}
ExecuteMsg::WasmMsg { ty } => wasm_msg(ty),
ExecuteMsg::Increment { addition } => increment(env, deps, addition),
ExecuteMsg::SendFundsWithErrorWithReply {} => Ok(Response::new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub enum InstantiateMsg {
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
Special {},
WasmMsg {
ty: String,
},
Expand Down
1 change: 1 addition & 0 deletions x/compute/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ var (
NewWasmSnapshotter = keeper.NewWasmSnapshotter
ContractFromPortID = keeper.ContractFromPortID
NewCountTXDecorator = keeper.NewCountTXDecorator
NewLastMsgDecorator = keeper.NewLastMsgDecorator

// variable aliases
ModuleCdc = types.ModuleCdc
Expand Down
24 changes: 24 additions & 0 deletions x/compute/internal/keeper/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"encoding/binary"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/scrtlabs/SecretNetwork/x/compute/internal/types"
Expand All @@ -12,11 +13,20 @@ type CountTXDecorator struct {
storeKey sdk.StoreKey
}

type LastMsgDecorator struct {
storeKey sdk.StoreKey
}

// NewCountTXDecorator constructor
func NewCountTXDecorator(storeKey sdk.StoreKey) *CountTXDecorator {
return &CountTXDecorator{storeKey: storeKey}
}

// LastMsgDecorator constructor
func NewLastMsgDecorator(storeKey sdk.StoreKey) *LastMsgDecorator {
return &LastMsgDecorator{storeKey: storeKey}
}

// AnteHandle handler stores a tx counter with current height encoded in the store to let the app handle
// global rollback behavior instead of keeping state in the handler itself.
// The ante handler passes the counter value via sdk.Context upstream. See `types.TXCounter(ctx)` to read the value.
Expand All @@ -43,6 +53,20 @@ func (a CountTXDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool,
return next(types.WithTXCounter(ctx, txCounter), tx, simulate)
}

// AnteHandle handler resets the market of the last message in the block
func (a LastMsgDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
if simulate {
return next(ctx, tx, simulate)
}

store := ctx.KVStore(a.storeKey)
// Reset last msg marker
fmt.Println("LIORRR Setting marker to off")
store.Set(types.LastMsgPrefix, nil)

return next(ctx, tx, simulate)
}

func encodeHeightCounter(height int64, counter uint32) []byte {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, counter)
Expand Down
4 changes: 4 additions & 0 deletions x/compute/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1073,3 +1073,7 @@ func (k Keeper) reply(ctx sdk.Context, contractAddress sdk.AccAddress, reply v1w
return nil, sdkerrors.Wrap(types.ErrReplyFailed, fmt.Sprintf("cannot detect response type: %+v", res))
}
}

func (k Keeper) GetStoreKey() sdk.StoreKey {
return k.storeKey
}
13 changes: 12 additions & 1 deletion x/compute/internal/keeper/msg_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Messenger interface {
// Replyer is a subset of keeper that can handle replies to submessages
type Replyer interface {
reply(ctx sdk.Context, contractAddress sdk.AccAddress, reply v1wasmTypes.Reply, ogTx []byte, ogSigInfo wasmTypes.VerificationInfo) ([]byte, error)
GetStoreKey() sdk.StoreKey
}

// MessageDispatcher coordinates message sending and submessage reply/ state commits
Expand Down Expand Up @@ -185,7 +186,17 @@ func redactError(err error) (bool, error) {
// that dispatched them, both on success as well as failure
func (d MessageDispatcher) DispatchSubmessages(ctx sdk.Context, contractAddr sdk.AccAddress, ibcPort string, msgs []v1wasmTypes.SubMsg, ogTx []byte, ogSigInfo wasmTypes.VerificationInfo, ogCosmosMessageVersion wasmTypes.CosmosMsgVersion) ([]byte, error) {
var rsp []byte
for _, msg := range msgs {
for idx, msg := range msgs {
store := ctx.KVStore(d.keeper.GetStoreKey())
if idx == 2 {
store.Set(types.LastMsgPrefix, []byte{1})
fmt.Printf("LIORRR Setting marker to on: %+v\n", store.Get(types.LastMsgPrefix))
}

if store.Get(types.LastMsgPrefix) != nil {
break
}

// Check replyOn validity
switch msg.ReplyOn {
case v1wasmTypes.ReplySuccess, v1wasmTypes.ReplyError, v1wasmTypes.ReplyAlways, v1wasmTypes.ReplyNever:
Expand Down
19 changes: 19 additions & 0 deletions x/compute/internal/keeper/secret_contracts_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,25 @@ func TestV1EndpointsSanity(t *testing.T) {
require.Equal(t, uint32(23), resp.Get.Count)
}

func TestSpecial(t *testing.T) {
ctx, keeper, codeID, _, walletA, privKeyA, _, _ := setupTest(t, TestContractPaths[v1Contract], sdk.NewCoins())

_, _, contractAddress, _, _ := initHelper(t, keeper, ctx, codeID, walletA, privKeyA, `{"counter":{"counter":10, "expires":100}}`, true, true, defaultGasForTests)

_, _, _, _, _, err := execHelper(t, keeper, ctx, contractAddress, walletA, privKeyA, `{"special":{}}`, true, true, math.MaxUint64, 0)

require.Empty(t, err)

queryRes, qErr := queryHelper(t, keeper, ctx, contractAddress, `{"get":{}}`, true, true, math.MaxUint64)
require.Empty(t, qErr)

// assert result is 32 byte sha256 hash (if hashed), or contractAddr if not
var resp v1QueryResponse
e := json.Unmarshal([]byte(queryRes), &resp)
require.NoError(t, e)
fmt.Printf("LIORRR count: %+v\n", resp.Get.Count)
}

func TestV1QueryWorksWithEnv(t *testing.T) {
ctx, keeper, codeID, _, walletA, privKeyA, _, _ := setupTest(t, TestContractPaths[v1Contract], sdk.NewCoins())

Expand Down
6 changes: 6 additions & 0 deletions x/compute/internal/keeper/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,12 @@ func TestHandler(k Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())

store := ctx.KVStore(k.GetStoreKey())
if bz := store.Get(wasmtypes.LastMsgPrefix); bz != nil {
fmt.Printf("Message of type: %+v was filtered out\n", msg)
return &sdk.Result{}, nil
}

switch msg := msg.(type) {
case *wasmtypes.MsgInstantiateContract:
return handleInstantiate(ctx, k, msg)
Expand Down
1 change: 1 addition & 0 deletions x/compute/internal/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
ContractEnclaveIdPrefix = []byte{0x06}
ContractLabelPrefix = []byte{0x07}
TXCounterPrefix = []byte{0x08}
LastMsgPrefix = []byte{0x09}
// RandomPrefix = []byte{0xFF}
KeyLastCodeID = append(SequenceKeyPrefix, []byte("lastCodeId")...)
KeyLastInstanceID = append(SequenceKeyPrefix, []byte("lastContractId")...)
Expand Down