Skip to content
This repository was archived by the owner on May 13, 2022. It is now read-only.

Commit 8b0bc7e

Browse files
authored
Merge pull request #1390 from seanyoung/ewasm
Improve ewasm support
2 parents 4b095b9 + cf68b27 commit 8b0bc7e

File tree

5 files changed

+133
-32
lines changed

5 files changed

+133
-32
lines changed

execution/contexts/call_context.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,18 @@ func (ctx *CallContext) Deliver(inAcc, outAcc *acm.Account, value uint64) error
185185
var err error
186186
txHash := ctx.txe.Envelope.Tx.Hash()
187187
gas := ctx.tx.GasLimit
188+
189+
params := engine.CallParams{
190+
Origin: caller,
191+
Caller: caller,
192+
Callee: callee,
193+
Input: ctx.tx.Data,
194+
Value: value,
195+
Gas: &gas,
196+
}
197+
188198
if len(wcode) != 0 {
189-
ret, err = wasm.RunWASM(txCache, callee, createContract, wcode, ctx.tx.Data)
199+
ret, err = wasm.RunWASM(txCache, params, wcode)
190200
if err != nil {
191201
// Failure. Charge the gas fee. The 'value' was otherwise not transferred.
192202
ctx.Logger.InfoMsg("Error on WASM execution",
@@ -211,15 +221,6 @@ func (ctx *CallContext) Deliver(inAcc, outAcc *acm.Account, value uint64) error
211221
ctx.EVM.SetNonce(txHash)
212222
ctx.EVM.SetLogger(ctx.Logger.With(structure.TxHashKey, txHash))
213223

214-
params := engine.CallParams{
215-
Origin: caller,
216-
Caller: caller,
217-
Callee: callee,
218-
Input: ctx.tx.Data,
219-
Value: value,
220-
Gas: &gas,
221-
}
222-
223224
ret, err = ctx.EVM.Execute(txCache, ctx.Blockchain, ctx.txe, params, code)
224225

225226
if err != nil {

execution/evm/abi/spec.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ type specJSON struct {
2727
Type string
2828
Inputs []argumentJSON
2929
Outputs []argumentJSON
30-
Constant bool
31-
Payable bool
3230
StateMutability string
3331
Anonymous bool
3432
}

execution/wasm/storage_test.solang.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package wasm
22

33
import hex "github.com/tmthrgd/go-hex"
44

5-
var Bytecode_storage_test = hex.MustDecodeString("0061736D01000000013A0B60037F7E7F0060037F7F7F0060027F7F0060000060017F0060017F017F60027F7F017F60047F7F7F7F0060047F7E7E7F006000017E6000017F029B010708657468657265756D06726576657274000208657468657265756D0B73746F726167654C6F6164000208657468657265756D0C73746F7261676553746F7265000208657468657265756D0F67657443616C6C4461746153697A65000A08657468657265756D0C63616C6C44617461436F7079000108657468657265756D0666696E697368000208657468657265756D066D656D6F7279020102020316150001020203040506010101010708080409030303030608017F01418080040B070801046D61696E001A0ACA0E151C00034020002001370300200041086A21002002417F6A22020D000B0B2600034020002001290300370300200041086A2100200141086A21012002417F6A22020D000B0B1C00034020004200370300200041086A21002001417F6A22010D000B0B1C0003402000427F370300200041086A21002001417F6A22010D000B0B2B00410041003602808004410041003602848004410041003A008C800441003F0041F0FF7B6A36028880040BAD0101037F024002402000450D00410021012000417C6A41003A00000240200041706A22022802002203450D00024020032D000C450D00200321010C010B20022003280200220136020002402001450D00200120023602040B200041786A2202200328020820022802006A41106A3602000B200041746A2802002203450D0020032D000C450D010B0F0B20012003360204200320013602002003200041786A28020020032802086A41106A3602080BA90101047F41808004210102400340024020012D000C0D002001280208220220004F0D020B200128020022010D000B41002101410028020821020B02402002200041076A41787122036B22024118490D00200120036A41106A22002001280200220436020002402004450D00200420003602040B2000200241706A360208200041003A000C2000200136020420012000360200200141086A20033602000B200141013A000C200141106A0BF50101057F024002400240200041706A22022802002203450D0020032D000C0D00200041786A220428020020032802086A41106A220520014F0D010B41002103410020014103766B21022001100C21010340200120036A200020036A290300370300200341086A2103200241016A22020D000B2000100B200121000C010B20042005360200200328020022032002360204200220033602002005200141076A41787122066B22054118490D00200020066A2201200336020002402003450D00200341046A20013602000B2001200541706A360208200141003A000C20012002360204200220013602002004200636020020000F0B20000B2D002000411F6A21000340200120002D00003A0000200141016A21012000417F6A21002002417F6A22020D000B0B29002000417F6A210003402001200020026A2D00003A0000200141016A21012002417F6A22020D000B0B2D002001411F6A21010340200120002D00003A00002001417F6A2101200041016A21002002417F6A22020D000B0B29002001417F6A21010340200120026A20002D00003A0000200041016A21002002417F6A22020D000B0BE80204077F017E037F027E200020034102746A417C6A21042003210502400340200522064101480D012006417F6A2105200428020021072004417C6A21042007450D000B0B200120034102746A417C6A21042003210502400340200522084101480D012008417F6A2105200428020021072004417C6A21042007450D000B0B024020034101480D002001417C6A21094100210A4200210B4100210C41002105410021010340200A20084E210D024002402005200520084822076A220E2001200A20064E6A22014D0D002000200C200D6A4102746A21042009200520076A4102746A21054200210F200E21070340200F4280808080107C200F200B200535020020043502007E7C2210200B541B210F200441046A21042005417C6A21052010210B2007417F6A220720014A0D000C020B0B4200210F200B21100B200C200D6A210C2002200A4102746A20103E02002010422088200F84210B200E2105200A41016A220A2003470D000B0B0B5201017E02402003450D000240200341C000710D00200141C00020036BAD8820022003AD220486842102200120048621010C010B20012003413F71AD862102420021010B20002002370308200020013703000B5201017E02402003450D000240200341C000710D00200241C00020036BAD8620012003AD220488842101200220048821020C010B20022003413F71AD882101420021020B20002002370308200020013703000B150002402000410171450D000F0B410041001000000B5102017F017E230041D0006B22002400200041386A4200370300200042003703302000420037032820004200370320200041206A20001001200020002903002201370340200041D0006A2400200142027C0B9D0102017F017E23004190016B22002400200041E0006A41186A4200370300200042003703702000420037036820004200370360200041E0006A200041C0006A1001200041206A41186A4200370300200042003703302000420037032820004200370320200020002903402201370380012000200142017C370388012000410410082000200029038801370300200041206A2000100220004190016A24000B5D01017F230041D0006B22002400200041C0006A4200370300200042003703382000420037033020004200370328200042E600370348200041086A4104100820002000290348370308200041286A200041086A1002200041D0006A24000B02000B2501017F100A10032200100C4100200010041019024020000D001018410041BA1110050B000B0BC111010041000BBA110061736D01000000013A0B60037F7E7F0060037F7F7F0060027F7F0060000060017F0060017F017F60027F7F017F60047F7F7F7F0060047F7E7E7F006000017E6000017F029B010708657468657265756D06726576657274000208657468657265756D0B73746F726167654C6F6164000208657468657265756D0C73746F7261676553746F7265000208657468657265756D0F67657443616C6C4461746153697A65000A08657468657265756D0C63616C6C44617461436F7079000108657468657265756D0666696E697368000208657468657265756D066D656D6F72790201020203151400010202030405060101010107080804090303030608017F01418080040B070801046D61696E00190AAA0F141C00034020002001370300200041086A21002002417F6A22020D000B0B2600034020002001290300370300200041086A2100200141086A21012002417F6A22020D000B0B1C00034020004200370300200041086A21002001417F6A22010D000B0B1C0003402000427F370300200041086A21002001417F6A22010D000B0B2B00410041003602808004410041003602848004410041003A008C800441003F0041F0FF7B6A36028880040BAD0101037F024002402000450D00410021012000417C6A41003A00000240200041706A22022802002203450D00024020032D000C450D00200321010C010B20022003280200220136020002402001450D00200120023602040B200041786A2202200328020820022802006A41106A3602000B200041746A2802002203450D0020032D000C450D010B0F0B20012003360204200320013602002003200041786A28020020032802086A41106A3602080BA90101047F41808004210102400340024020012D000C0D002001280208220220004F0D020B200128020022010D000B41002101410028020821020B02402002200041076A41787122036B22024118490D00200120036A41106A22002001280200220436020002402004450D00200420003602040B2000200241706A360208200041003A000C2000200136020420012000360200200141086A20033602000B200141013A000C200141106A0BF50101057F024002400240200041706A22022802002203450D0020032D000C0D00200041786A220428020020032802086A41106A220520014F0D010B41002103410020014103766B21022001100C21010340200120036A200020036A290300370300200341086A2103200241016A22020D000B2000100B200121000C010B20042005360200200328020022032002360204200220033602002005200141076A41787122066B22054118490D00200020066A2201200336020002402003450D00200341046A20013602000B2001200541706A360208200141003A000C20012002360204200220013602002004200636020020000F0B20000B2D002000411F6A21000340200120002D00003A0000200141016A21012000417F6A21002002417F6A22020D000B0B29002000417F6A210003402001200020026A2D00003A0000200141016A21012002417F6A22020D000B0B2D002001411F6A21010340200120002D00003A00002001417F6A2101200041016A21002002417F6A22020D000B0B29002001417F6A21010340200120026A20002D00003A0000200041016A21002002417F6A22020D000B0BE80204077F017E037F027E200020034102746A417C6A21042003210502400340200522064101480D012006417F6A2105200428020021072004417C6A21042007450D000B0B200120034102746A417C6A21042003210502400340200522084101480D012008417F6A2105200428020021072004417C6A21042007450D000B0B024020034101480D002001417C6A21094100210A4200210B4100210C41002105410021010340200A20084E210D024002402005200520084822076A220E2001200A20064E6A22014D0D002000200C200D6A4102746A21042009200520076A4102746A21054200210F200E21070340200F4280808080107C200F200B200535020020043502007E7C2210200B541B210F200441046A21042005417C6A21052010210B2007417F6A220720014A0D000C020B0B4200210F200B21100B200C200D6A210C2002200A4102746A20103E02002010422088200F84210B200E2105200A41016A220A2003470D000B0B0B5201017E02402003450D000240200341C000710D00200141C00020036BAD8820022003AD220486842102200120048621010C010B20012003413F71AD862102420021010B20002002370308200020013703000B5201017E02402003450D000240200341C000710D00200241C00020036BAD8620012003AD220488842101200220048821020C010B20022003413F71AD882101420021020B20002002370308200020013703000B150002402000410171450D000F0B410041001000000B5102017F017E230041D0006B22002400200041386A4200370300200042003703302000420037032820004200370320200041206A20001001200020002903002201370340200041D0006A2400200142027C0B9D0102017F017E23004190016B22002400200041E0006A41186A4200370300200042003703702000420037036820004200370360200041E0006A200041C0006A1001200041206A41186A4200370300200042003703302000420037032820004200370320200020002903402201370380012000200142017C370388012000410410082000200029038801370300200041206A2000100220004190016A24000B5D01017F230041D0006B22002400200041C0006A4200370300200042003703382000420037033020004200370328200042E600370348200041086A4104100820002000290348370308200041286A200041086A1002200041D0006A24000B870102047F017E230022002101100A10032202100C22034100200210040240200241034D0D002002417C6A21020240200328020041DE9AB88F79470D0020020D011017410041001005200124000F0B20020D00101621044120100C2202420041041006200041706A22032400200320043703002003200241081010200241201005200124000F0B000B")
6-
var Abi_storage_test = []byte(`[{"name":"","type":"constructor","inputs":[],"outputs":[],"constant":false,"payable":false,"stateMutability":"nonpayable"},{"name":"getFooPlus2","type":"function","inputs":[],"outputs":[{"name":"","type":"uint64","internalType":"uint64"}],"constant":true,"payable":false,"stateMutability":"view"},{"name":"incFoo","type":"function","inputs":[],"outputs":[],"constant":false,"payable":false,"stateMutability":"nonpayable"}]`)
5+
var Bytecode_storage_test = hex.MustDecodeString("0061736D0100000001230760027F7F0060000060017F017F60047F7E7E7F006000017F60017F0060037F7F7F000294010708657468657265756D0C73746F7261676553746F7265000008657468657265756D0C67657443616C6C56616C7565000508657468657265756D06726576657274000008657468657265756D0B676574436F646553697A65000408657468657265756D08636F6465436F7079000608657468657265756D0666696E697368000008657468657265756D066D656D6F727902010202030807000102030304010608017F01418080040B070801046D61696E000C0AB704071C00034020004200370300200041086A21002001417F6A22010D000B0B2B00410041003602808004410041003602848004410041003A008C800441003F0041F0FF7B6A36028880040BA90101047F41808004210102400340024020012D000C0D002001280208220220004F0D020B200128020022010D000B41002101410028020821020B02402002200041076A41787122036B22024118490D00200120036A41106A22002001280200220436020002402004450D00200420003602040B2000200241706A360208200041003A000C2000200136020420012000360200200141086A20033602000B200141013A000C200141106A0B5201017E02402003450D000240200341C000710D00200141C00020036BAD8820022003AD220486842102200120048621010C010B20012003413F71AD862102420021010B20002002370308200020013703000B5201017E02402003450D000240200341C000710D00200241C00020036BAD8620012003AD220488842101200220048821020C010B20022003413F71AD882101420021020B20002002370308200020013703000B4E01017F230041C0006B22002400200041386A4200370300200042003703302000420037032820004200370320200041041006200042E600370300200041206A20001000200041C0006A240041000B4C01017F230041106B220024002000100102402000290300200041086A290300844200510D00410041001002000B10071003419E716A2200100841E20E20001004100B1A410041BC081005000B0BC308010041000BBC080061736D0100000001230760027F7F0060000060017F017F60037F7F7F0060047F7E7E7F0060017F006000017F02B3010808657468657265756D0C67657443616C6C56616C7565000508657468657265756D06726576657274000008657468657265756D0F67657443616C6C4461746153697A65000608657468657265756D0C63616C6C44617461436F7079000308657468657265756D0B73746F726167654C6F6164000008657468657265756D0666696E697368000008657468657265756D0C73746F7261676553746F7265000008657468657265756D066D656D6F727902010202030807000102030404010608017F01418080040B070801046D61696E000D0AB806071C00034020004200370300200041086A21002001417F6A22010D000B0B2B00410041003602808004410041003602848004410041003A008C800441003F0041F0FF7B6A36028880040BA90101047F41808004210102400340024020012D000C0D002001280208220220004F0D020B200128020022010D000B41002101410028020821020B02402002200041076A41787122036B22024118490D00200120036A41106A22002001280200220436020002402004450D00200420003602040B2000200241706A360208200041003A000C2000200136020420012000360200200141086A20033602000B200141013A000C200141106A0B2D002001411F6A21010340200120002D00003A00002001417F6A2101200041016A21002002417F6A22020D000B0B5201017E02402003450D000240200341C000710D00200141C00020036BAD8820022003AD220486842102200120048621010C010B20012003413F71AD862102420021010B20002002370308200020013703000B5201017E02402003450D000240200341C000710D00200241C00020036BAD8620012003AD220488842101200220048821020C010B20022003413F71AD882101420021020B20002002370308200020013703000BED0202047F017E23004190016B2200240020002201100002402001290300200141086A290300844200510D00410041001001000B100810022202100922034100200210030240024002400240200241034D0D002003280200220241DE9AB88F79460D01200241A298B6BE01470D00200041706A2200240020014188016A420037030020014200370380012001420037037820014200370370200141F0006A200141D0006A10042000200129035042027C37030041000D0241201009220141041007200020014108100A200141201005000B410041001001000B200141F0006A41186A420037030020014200370380012001420037037820014200370370200141F0006A200141D0006A1004200141306A41186A420037030020014200370340200142003703382001420037033020012903502104200141106A410410072001200442017C370310200141306A200141106A100641000D01410041001005000B410041001001000B410041001001000B")
6+
var Abi_storage_test = []byte(`[{"type":"constructor","inputs":[],"outputs":[],"stateMutability":"nonpayable"},{"name":"getFooPlus2","type":"function","inputs":[],"outputs":[{"name":"","type":"uint64","internalType":"uint64"}],"stateMutability":"view"},{"name":"incFoo","type":"function","inputs":[],"outputs":[],"stateMutability":"nonpayable"}]`)

execution/wasm/wasm.go

Lines changed: 98 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
package wasm
22

33
import (
4+
"encoding/binary"
45
"fmt"
56

67
"github.com/hyperledger/burrow/acm/acmstate"
78
burrow_binary "github.com/hyperledger/burrow/binary"
89
"github.com/hyperledger/burrow/crypto"
10+
"github.com/hyperledger/burrow/execution/engine"
911
"github.com/hyperledger/burrow/execution/errors"
1012
"github.com/perlin-network/life/exec"
1113
)
1214

1315
type execContext struct {
1416
errors.Maybe
15-
address crypto.Address
16-
input []byte
17-
output []byte
18-
state acmstate.ReaderWriter
17+
code []byte
18+
output []byte
19+
returnData []byte
20+
params engine.CallParams
21+
state acmstate.ReaderWriter
1922
}
2023

2124
// Implements ewasm, see https://github.com/ewasm/design
2225

2326
// RunWASM creates a WASM VM, and executes the given WASM contract code
24-
func RunWASM(state acmstate.ReaderWriter, address crypto.Address, createContract bool, wasm, input []byte) (output []byte, cerr error) {
27+
func RunWASM(state acmstate.ReaderWriter, params engine.CallParams, wasm []byte) (output []byte, cerr error) {
2528
const errHeader = "ewasm"
2629
defer func() {
2730
if r := recover(); r != nil {
@@ -32,14 +35,14 @@ func RunWASM(state acmstate.ReaderWriter, address crypto.Address, createContract
3235
// WASM
3336
config := exec.VMConfig{
3437
DisableFloatingPoint: true,
35-
MaxMemoryPages: 2,
36-
DefaultMemoryPages: 2,
38+
MaxMemoryPages: 16,
39+
DefaultMemoryPages: 16,
3740
}
3841

3942
execContext := execContext{
40-
address: address,
41-
state: state,
42-
input: input,
43+
params: params,
44+
code: wasm,
45+
state: state,
4346
}
4447

4548
// panics in ResolveFunc() will be recovered for us, no need for our own
@@ -72,7 +75,7 @@ func (e *execContext) ResolveFunc(module, field string) exec.FunctionImport {
7275
switch field {
7376
case "getCallDataSize":
7477
return func(vm *exec.VirtualMachine) int64 {
75-
return int64(len(e.input))
78+
return int64(len(e.params.Input))
7679
}
7780

7881
case "callDataCopy":
@@ -82,7 +85,43 @@ func (e *execContext) ResolveFunc(module, field string) exec.FunctionImport {
8285
dataLen := int(uint32(vm.GetCurrentFrame().Locals[2]))
8386

8487
if dataLen > 0 {
85-
copy(vm.Memory[destPtr:], e.input[dataOffset:dataOffset+dataLen])
88+
copy(vm.Memory[destPtr:], e.params.Input[dataOffset:dataOffset+dataLen])
89+
}
90+
91+
return 0
92+
}
93+
94+
case "getReturnDataSize":
95+
return func(vm *exec.VirtualMachine) int64 {
96+
return int64(len(e.returnData))
97+
}
98+
99+
case "returnDataCopy":
100+
return func(vm *exec.VirtualMachine) int64 {
101+
destPtr := int(uint32(vm.GetCurrentFrame().Locals[0]))
102+
dataOffset := int(uint32(vm.GetCurrentFrame().Locals[1]))
103+
dataLen := int(uint32(vm.GetCurrentFrame().Locals[2]))
104+
105+
if dataLen > 0 {
106+
copy(vm.Memory[destPtr:], e.returnData[dataOffset:dataOffset+dataLen])
107+
}
108+
109+
return 0
110+
}
111+
112+
case "getCodeSize":
113+
return func(vm *exec.VirtualMachine) int64 {
114+
return int64(len(e.code))
115+
}
116+
117+
case "codeCopy":
118+
return func(vm *exec.VirtualMachine) int64 {
119+
destPtr := int(uint32(vm.GetCurrentFrame().Locals[0]))
120+
dataOffset := int(uint32(vm.GetCurrentFrame().Locals[1]))
121+
dataLen := int(uint32(vm.GetCurrentFrame().Locals[2]))
122+
123+
if dataLen > 0 {
124+
copy(vm.Memory[destPtr:], e.code[dataOffset:dataOffset+dataLen])
86125
}
87126

88127
return 0
@@ -97,7 +136,7 @@ func (e *execContext) ResolveFunc(module, field string) exec.FunctionImport {
97136

98137
copy(key[:], vm.Memory[keyPtr:keyPtr+32])
99138

100-
e.Void(e.state.SetStorage(e.address, key, vm.Memory[dataPtr:dataPtr+32]))
139+
e.Void(e.state.SetStorage(e.params.Callee, key, vm.Memory[dataPtr:dataPtr+32]))
101140
return 0
102141
}
103142

@@ -111,7 +150,7 @@ func (e *execContext) ResolveFunc(module, field string) exec.FunctionImport {
111150

112151
copy(key[:], vm.Memory[keyPtr:keyPtr+32])
113152

114-
val := e.Bytes(e.state.GetStorage(e.address, key))
153+
val := e.Bytes(e.state.GetStorage(e.params.Callee, key))
115154
copy(vm.Memory[dataPtr:], val)
116155

117156
return 0
@@ -138,6 +177,51 @@ func (e *execContext) ResolveFunc(module, field string) exec.FunctionImport {
138177
panic(errors.Codes.ExecutionReverted)
139178
}
140179

180+
case "getAddress":
181+
return func(vm *exec.VirtualMachine) int64 {
182+
addressPtr := int(uint32(vm.GetCurrentFrame().Locals[0]))
183+
184+
copy(vm.Memory[addressPtr:], e.params.Callee.Bytes())
185+
186+
return 0
187+
}
188+
189+
case "getCallValue":
190+
return func(vm *exec.VirtualMachine) int64 {
191+
192+
valuePtr := int(uint32(vm.GetCurrentFrame().Locals[0]))
193+
194+
// ewasm value is little endian 128 bit value
195+
bs := make([]byte, 16)
196+
binary.LittleEndian.PutUint64(bs, e.params.Value)
197+
198+
copy(vm.Memory[valuePtr:], bs)
199+
200+
return 0
201+
}
202+
203+
case "getExternalBalance":
204+
return func(vm *exec.VirtualMachine) int64 {
205+
addressPtr := int(uint32(vm.GetCurrentFrame().Locals[0]))
206+
balancePtr := int(uint32(vm.GetCurrentFrame().Locals[1]))
207+
208+
address := crypto.Address{}
209+
210+
copy(address[:], vm.Memory[addressPtr:addressPtr+crypto.AddressLength])
211+
acc, err := e.state.GetAccount(address)
212+
if err != nil {
213+
panic(errors.Codes.InvalidAddress)
214+
}
215+
216+
// ewasm value is little endian 128 bit value
217+
bs := make([]byte, 16)
218+
binary.LittleEndian.PutUint64(bs, acc.Balance)
219+
220+
copy(vm.Memory[balancePtr:], bs)
221+
222+
return 0
223+
}
224+
141225
default:
142226
panic(fmt.Sprintf("unknown function %s", field))
143227
}

0 commit comments

Comments
 (0)