Skip to content

Commit

Permalink
增加context
Browse files Browse the repository at this point in the history
  • Loading branch information
web3creator committed Sep 30, 2022
1 parent e4dc6df commit f4ed329
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 14 deletions.
118 changes: 118 additions & 0 deletions sdk/cases/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,124 @@
"params":"",
"expect_code": 16,
"expect_message": "test_abort USR_ILLEGAL_ARGUMENT"
},
{
"name": "abort_SYS_SENDER_STATE_INVALID ",
"binary": "abort.wasm",
"actor_balance": 0,
"send_value": 0,
"params":"",
"method_num": 2,
"expect_code": 2,
"expect_message": "test_abort SYS_SENDER_STATE_INVALID"
},
{
"name": "ipld_test ",
"binary": "ipld.wasm",
"actor_balance": 0,
"send_value": 0,
"params":"",
"method_num": 1,
"expect_code": 0,
"expect_message": ""
},
{
"name": "message_test ",
"binary": "message.wasm",
"actor_balance": 0,
"send_value": 10,
"params":"",
"method_num": 1,
"expect_code": 0,
"expect_message": ""
},
{
"name": "sself_test ",
"binary": "sself.wasm",
"actor_balance": 20,
"send_value": 0,
"params":"",
"method_num": 1,
"expect_code": 0,
"expect_message": ""
},
{
"name": "network_test",
"binary": "network.wasm",
"actor_balance": 0,
"send_value": 0,
"params":"",
"method_num": 1,
"expect_code": 0,
"expect_message": ""
},
{
"name": "gas_test",
"binary": "gas.wasm",
"actor_balance": 0,
"send_value": 0,
"params":"",
"method_num": 1,
"expect_code": 0,
"expect_message": ""
},
{
"name": "debug_log_test",
"binary": "debug.wasm",
"actor_balance": 0,
"send_value": 0,
"params":"",
"method_num": 1,
"expect_code": 0,
"expect_message": ""
},
{
"name": "send_test_SYS_ILLEGAL_INSTRUCTION(resource not found)",
"binary": "send.wasm",
"actor_balance": 10,
"send_value": 0,
"params":"",
"method_num": 1,
"expect_code": 4,
"expect_message": "wasm `unreachable` instruction executed"
},
{
"name": "send_test",
"binary": "send.wasm",
"actor_balance": 10,
"send_value": 0,
"params":"",
"method_num": 2,
"expect_code": 0,
"expect_message": ""
},
{
"name": "send_test_SYS_ILLEGAL_INSTRUCTION(insufficient funds)",
"binary": "send.wasm",
"actor_balance": 10,
"send_value": 0,
"params":"",
"method_num": 3,
"expect_code": 4,
"expect_message": "wasm `unreachable` instruction executed"
},
{
"name": "adt_test",
"binary": "adt.wasm",
"actor_balance": 0,
"send_value": 0,
"method_num": 1,
"params":"",
"expect_code": 0
},
{
"name": "rand_test",
"binary": "rand.wasm",
"actor_balance": 0,
"send_value": 0,
"method_num": 1,
"params":"",
"expect_code": 0
}
]
}
7 changes: 4 additions & 3 deletions sdk/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"github.com/ipfs-force-community/go-fvm-sdk/sdk/types"
)

// CreateSimulateEnv new context of simulated
func CreateSimulateEnv() context.Context {
s := simulated.NewSimulated()
return context.WithValue(context.Background(), types.Envkey, s)
return context.WithValue(context.Background(), types.SimulatedEnvkey, simulated.NewSimulated())
}

// CreateEntityEnv new context of entity
func CreateEntityEnv() context.Context {
return context.WithValue(context.Background(), types.Envkey, types.EnvMain)
return context.WithValue(context.Background(), types.SimulatedEnvkey, "")
}
1 change: 0 additions & 1 deletion sdk/sys/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func VMContext(ctx context.Context) (*types.InvocationContext, error) {
if env, ok := isSimulatedEnv(ctx); ok {
return env.VMContext()
}

var result types.InvocationContext
code := vmContext(uintptr(unsafe.Pointer(&result)))
if code != 0 {
Expand Down
2 changes: 1 addition & 1 deletion sdk/sys/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import (
)

func isSimulatedEnv(ctx context.Context) (*simulated.Fsm, bool) {
env, ok := ctx.Value(types.Envkey).(*simulated.Fsm)
env, ok := ctx.Value(types.SimulatedEnvkey).(*simulated.Fsm)
return env, ok
}
3 changes: 1 addition & 2 deletions sdk/sys/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
)

func Test_isSimulatedEnv(t *testing.T) {
s := simulated.NewSimulated()
ctx := context.WithValue(context.Background(), types.Envkey, s)
ctx := context.WithValue(context.Background(), types.SimulatedEnvkey, simulated.NewSimulated())
_, got := isSimulatedEnv(ctx)
if !reflect.DeepEqual(got, true) {
t.Errorf("isSimulatedEnv() got = %v, want %v", got, true)
Expand Down
10 changes: 3 additions & 7 deletions sdk/types/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ func (k StringKey) Key() string {
return string(k)
}

var (
Envkey = "envkey"
)
var (
EnvSimulated = "entity"
EnvMain = "entity"
)
type emptyKeyType struct{}

var SimulatedEnvkey emptyKeyType

0 comments on commit f4ed329

Please sign in to comment.