Skip to content

Commit

Permalink
feat: implement CallCallblePoint to VM
Browse files Browse the repository at this point in the history
  • Loading branch information
loloicci committed Mar 29, 2023
1 parent f2018e8 commit c65074b
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 1 deletion.
49 changes: 49 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,55 @@ func (vm *VM) IBCPacketTimeout(
return resp.Ok, gasUsed, nil
}

func (vm *VM) CallCallablePoint(
name []byte,
checksum Checksum,
isReadonly bool,
callstack []byte,
env types.Env,
args []byte,
store KVStore,
goapi GoAPI,
querier Querier,
gasMeter GasMeter,
gasLimit uint64,
deserCost types.UFraction,
) ([]byte, types.Events, types.EventAttributes, uint64, error) {
envBin, err := json.Marshal(env)
if err != nil {
return nil, nil, nil, 0, err
}

data, eventsData, attributesData, gasUsed, err := api.CallCallablePoint(name, vm.cache, checksum, isReadonly, callstack, envBin, args, &gasMeter, store, &goapi, &querier, gasLimit, vm.printDebug)
if err != nil {
return nil, nil, nil, gasUsed, err
}

gasForDeserializingEvents := deserCost.Mul(uint64(len(eventsData))).Floor()
if gasLimit < gasForDeserializingEvents+gasUsed {
return nil, nil, nil, gasUsed, fmt.Errorf("Insufficient gas left to deserialize events (%d bytes)", len(eventsData))
}
gasUsed += gasForDeserializingEvents
var events types.Events
err = events.UnmarshalJSON(eventsData)
if err != nil {
return nil, nil, nil, gasUsed, err
}
gasForDeserializingAttrs := deserCost.Mul(uint64(len(attributesData))).Floor()

if gasLimit < gasForDeserializingAttrs+gasUsed {
return nil, nil, nil, gasUsed, fmt.Errorf("Insufficient gas left to deserialize attributes (%d bytes)", len(attributesData))
}
gasUsed += gasForDeserializingAttrs
var attributes types.EventAttributes
err = attributes.UnmarshalJSON(attributesData)
if err != nil {
return nil, nil, nil, gasUsed, err
}

return data, events, attributes, gasUsed, nil
}

func (vm *VM) GetCache() *Cache {
return &vm.cache
}
Expand Down
104 changes: 103 additions & 1 deletion lib_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cosmwasm

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -218,7 +219,7 @@ func TestGetMetrics(t *testing.T) {
require.InEpsilon(t, 5665691, metrics.SizeMemoryCache, 0.18)
}

func TestEventManage(t *testing.T) {
func TestEventManager(t *testing.T) {
vm := withVM(t)

// Create contract
Expand Down Expand Up @@ -277,3 +278,104 @@ func TestEventManage(t *testing.T) {
require.Equal(t, 0, len(eres2.Events))
require.Equal(t, []types.EventAttribute(expectedAttributes), eres2.Attributes)
}

func TestCallCallablePoint(t *testing.T) {
vm := withVM(t)

// Create contract
checksum := createTestContract(t, vm, EVENTS_TEST_CONTRACT)

deserCost := types.UFraction{1, 1}

// Instantiate
gasMeter1 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
// instantiate it with this store
store := api.NewLookup(gasMeter1)
goapi := api.NewMockAPI()
balance := types.Coins{}
querier := api.DefaultQuerier(api.MOCK_CONTRACT_ADDR, balance)

env := api.MockEnv()
info := api.MockInfo("creator", nil)
msg1 := []byte(`{}`)
ires, _, err := vm.Instantiate(checksum, env, info, msg1, store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, 0, len(ires.Messages))

// Issue Events
gasMeter2 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter2)
name := "add_events_dyn"
nameBin, err := json.Marshal(name)
require.NoError(t, err)
eventsIn := types.Events{
types.Event{
Type: "ty1",
Attributes: types.EventAttributes{
types.EventAttribute{
Key: "alice",
Value: "101010",
},
types.EventAttribute{
Key: "bob",
Value: "42",
},
},
},
types.Event{
Type: "ty2",
Attributes: types.EventAttributes{
types.EventAttribute{
Key: "ALICE",
Value: "42",
},
types.EventAttribute{
Key: "BOB",
Value: "101010",
},
},
},
}
eventsInBin, err := eventsIn.MarshalJSON()
require.NoError(t, err)
argsEv := [][]byte{eventsInBin}
argsEvBin, err := json.Marshal(argsEv)
require.NoError(t, err)
empty := []types.HumanAddress{}
emptyBin, err := json.Marshal(empty)
require.NoError(t, err)

cres1, events, attributes, _, err := vm.CallCallablePoint(nameBin, checksum, false, emptyBin, env, argsEvBin, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, []byte(`null`), cres1)
require.Equal(t, eventsIn, events)
require.Equal(t, 0, len(attributes))

// Issue Attributes
gasMeter3 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter2)
name = "add_attributes_dyn"
nameBin, err = json.Marshal(name)
require.NoError(t, err)
attrsIn := types.EventAttributes{
types.EventAttribute{
Key: "alice",
Value: "42",
},
types.EventAttribute{
Key: "bob",
Value: "101010",
},
}
attrsInBin, err := attrsIn.MarshalJSON()
require.NoError(t, err)
argsAt := [][]byte{attrsInBin}
argsAtBin, err := json.Marshal(argsAt)
require.NoError(t, err)

cres2, events, attributes, _, err := vm.CallCallablePoint(nameBin, checksum, false, emptyBin, env, argsAtBin, store, *goapi, querier, gasMeter3, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, []byte(`null`), cres2)
require.Equal(t, 0, len(events))
require.Equal(t, attrsIn, attributes)
}

0 comments on commit c65074b

Please sign in to comment.