Skip to content

Commit

Permalink
Merge pull request #784 from multiversx/return-err-if-no-upgrade-init…
Browse files Browse the repository at this point in the history
…-func

Return err if no upgrade init func
  • Loading branch information
sasurobert authored Oct 5, 2023
2 parents 41c60e3 + 9fedfca commit bb383e6
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 46 deletions.
8 changes: 5 additions & 3 deletions test/contracts/answer-locals/answer-locals.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
(type (;0;) (func (param i64)))
(type (;1;) (func))
(import "env" "int64finish" (func (;0;) (type 0)))
(func (;1;) (type 1)
(local i64 i64 i64 i64 i64)
(func (;1;) (type 1))
(func (;2;) (type 1)
(local i64 i64 i64 i64)
i64.const 42
call 0)
(table (;0;) 1 1 funcref)
(memory (;0;) 2)
(global (;0;) (mut i32) (i32.const 66560))
(export "memory" (memory 0))
(export "answer" (func 1)))
(export "init" (func 1))
(export "answer" (func 2)))
Binary file modified test/contracts/answer-locals/output/answer-locals.wasm
Binary file not shown.
4 changes: 4 additions & 0 deletions test/contracts/counter/counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ void init() {
int64storageStore(COUNTER_KEY, COUNTER_KEY_LEN, 1);
}

void upgrade() {
int64storageStore(COUNTER_KEY, COUNTER_KEY_LEN, 1);
}

void increment() {
i64 counter = int64storageLoad(COUNTER_KEY, COUNTER_KEY_LEN);
counter++;
Expand Down
1 change: 1 addition & 0 deletions test/contracts/counter/counter.export
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
init
upgrade
increment
decrement
get
Binary file modified test/contracts/counter/output/counter.wasm
100755 → 100644
Binary file not shown.
215 changes: 178 additions & 37 deletions test/contracts/counter/output/counter.wat
Original file line number Diff line number Diff line change
@@ -1,56 +1,197 @@
(module
(type $t0 (func (param i32 i32 i64) (result i32)))
(type $t1 (func (param i32 i32) (result i64)))
(type $t2 (func (param i64)))
(type $t3 (func))
(import "env" "int64storageStore" (func $env.int64storageStore (type $t0)))
(import "env" "int64storageLoad" (func $env.int64storageLoad (type $t1)))
(import "env" "int64finish" (func $env.int64finish (type $t2)))
(func $init (type $t3)
(type (;0;) (func (param i32 i32 i64) (result i32)))
(type (;1;) (func (param i32 i32) (result i64)))
(type (;2;) (func (param i64)))
(type (;3;) (func))
(import "env" "int64storageStore" (func (;0;) (type 0)))
(import "env" "int64storageLoad" (func (;1;) (type 1)))
(import "env" "int64finish" (func (;2;) (type 2)))
(func (;3;) (type 3)
(local i32 i32 i64)
i32.const 1024
local.set 0
i32.const 7
local.set 1
i64.const 1
call $env.int64storageStore
drop)
(func $increment (type $t3)
(local $l0 i64)
local.set 2
local.get 0
local.get 1
local.get 2
call 0
drop
return)
(func (;4;) (type 3)
(local i32 i32 i64)
i32.const 1024
local.set 0
i32.const 7
local.set 1
i64.const 1
local.set 2
local.get 0
local.get 1
local.get 2
call 0
drop
return)
(func (;5;) (type 3)
(local i32 i32 i32 i32 i32 i64 i64 i64 i64 i64 i64 i32 i32)
global.get 0
local.set 0
i32.const 16
local.set 1
local.get 0
local.get 1
i32.sub
local.set 2
local.get 2
global.set 0
i32.const 1024
local.set 3
i32.const 7
call $env.int64storageLoad
local.set 4
local.get 3
local.get 4
call 1
local.set 5
local.get 2
local.get 5
i64.store offset=8
local.get 2
i64.load offset=8
local.set 6
i64.const 1
local.set 7
local.get 6
local.get 7
i64.add
local.tee $l0
call $env.int64storageStore
local.set 8
local.get 2
local.get 8
i64.store offset=8
local.get 2
i64.load offset=8
local.set 9
local.get 3
local.get 4
local.get 9
call 0
drop
local.get $l0
call $env.int64finish)
(func $decrement (type $t3)
(local $l0 i64)
i32.const 1024
i32.const 7
local.get 2
i64.load offset=8
local.set 10
local.get 10
call 2
i32.const 16
local.set 11
local.get 2
local.get 11
i32.add
local.set 12
local.get 12
global.set 0
return)
(func (;6;) (type 3)
(local i32 i32 i32 i32 i32 i64 i64 i64 i64 i64 i64 i32 i32)
global.get 0
local.set 0
i32.const 16
local.set 1
local.get 0
local.get 1
i32.sub
local.set 2
local.get 2
global.set 0
i32.const 1024
local.set 3
i32.const 7
call $env.int64storageLoad
local.set 4
local.get 3
local.get 4
call 1
local.set 5
local.get 2
local.get 5
i64.store offset=8
local.get 2
i64.load offset=8
local.set 6
i64.const -1
local.set 7
local.get 6
local.get 7
i64.add
local.tee $l0
call $env.int64storageStore
local.set 8
local.get 2
local.get 8
i64.store offset=8
local.get 2
i64.load offset=8
local.set 9
local.get 3
local.get 4
local.get 9
call 0
drop
local.get $l0
call $env.int64finish)
(func $get (type $t3)
local.get 2
i64.load offset=8
local.set 10
local.get 10
call 2
i32.const 16
local.set 11
local.get 2
local.get 11
i32.add
local.set 12
local.get 12
global.set 0
return)
(func (;7;) (type 3)
(local i32 i32 i32 i32 i32 i64 i64 i32 i32)
global.get 0
local.set 0
i32.const 16
local.set 1
local.get 0
local.get 1
i32.sub
local.set 2
local.get 2
global.set 0
i32.const 1024
local.set 3
i32.const 7
call $env.int64storageLoad
call $env.int64finish)
(table $T0 1 1 funcref)
(memory $memory 2)
(global $g0 (mut i32) (i32.const 66576))
local.set 4
local.get 3
local.get 4
call 1
local.set 5
local.get 2
local.get 5
i64.store offset=8
local.get 2
i64.load offset=8
local.set 6
local.get 6
call 2
i32.const 16
local.set 7
local.get 2
local.get 7
i32.add
local.set 8
local.get 8
global.set 0
return)
(table (;0;) 1 1 funcref)
(memory (;0;) 2)
(global (;0;) (mut i32) (i32.const 66576))
(export "memory" (memory 0))
(export "init" (func $init))
(export "increment" (func $increment))
(export "decrement" (func $decrement))
(export "get" (func $get))
(data $d0 (i32.const 1024) "COUNTER\00"))
(export "init" (func 3))
(export "upgrade" (func 4))
(export "increment" (func 5))
(export "decrement" (func 6))
(export "get" (func 7))
(data (;0;) (i32.const 1024) "COUNTER\00"))
2 changes: 1 addition & 1 deletion vmhost/hostCore/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ func (host *vmHost) callUpgradeFunction() error {
func (host *vmHost) callSCFunction(functionName string) error {
runtime := host.Runtime()
if !runtime.HasFunction(functionName) {
return nil
return executor.ErrFuncNotFound
}

err := runtime.CallSCFunction(functionName)
Expand Down
11 changes: 6 additions & 5 deletions vmhost/hosttest/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func TestExecution_DeployWASM_Popcnt(t *testing.T) {
func TestExecution_DeployWASM_AtMaximumLocals(t *testing.T) {
test.BuildInstanceCreatorTest(t).
WithInput(test.CreateTestContractCreateInputBuilder().
WithGasProvided(1000).
WithGasProvided(100000).
WithCallValue(88).
WithContractCode(makeBytecodeWithLocals(WASMLocalsLimit)).
Build()).
Expand Down Expand Up @@ -3687,8 +3687,9 @@ func TestExecution_Mocked_OnSameFollowedByOnDest(t *testing.T) {
// number of i64 locals it instantiates
func makeBytecodeWithLocals(numLocals uint64) []byte {
originalCode := test.GetTestSCCode("answer-locals", "../../")
firstSlice := originalCode[:0x5B]
secondSlice := originalCode[0x5C:]

firstSlice := originalCode[:0x66]
secondSlice := originalCode[0x67:]

encodedNumLocals := vmhost.U64ToLEB128(numLocals)
extraBytes := len(encodedNumLocals) - 1
Expand All @@ -3698,8 +3699,8 @@ func makeBytecodeWithLocals(numLocals uint64) []byte {
result = append(result, encodedNumLocals...)
result = append(result, secondSlice...)

result[0x57] = byte(int(result[0x57]) + extraBytes)
result[0x59] = byte(int(result[0x59]) + extraBytes)
result[0x5F] = byte(int(result[0x5F]) + extraBytes)
result[0x64] = byte(int(result[0x64]) + extraBytes)

return result
}
Expand Down

0 comments on commit bb383e6

Please sign in to comment.