Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
plugin
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
plugin
Commits
c25ac1d7
Commit
c25ac1d7
authored
Sep 01, 2020
by
袁兴强
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gowasm init
parent
0be50d74
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
972 additions
and
0 deletions
+972
-0
go.mod
go.mod
+1
-0
go.sum
go.sum
+12
-0
init.go
plugin/dapp/init/init.go
+1
-0
wasm.go
plugin/dapp/wasm/commands/wasm.go
+134
-0
callback.go
plugin/dapp/wasm/executor/callback.go
+118
-0
db.go
plugin/dapp/wasm/executor/db.go
+37
-0
exec.go
plugin/dapp/wasm/executor/exec.go
+118
-0
exec_del_local.go
plugin/dapp/wasm/executor/exec_del_local.go
+33
-0
exec_local.go
plugin/dapp/wasm/executor/exec_local.go
+33
-0
query.go
plugin/dapp/wasm/executor/query.go
+63
-0
resolver.go
plugin/dapp/wasm/executor/resolver.go
+200
-0
wasm.go
plugin/dapp/wasm/executor/wasm.go
+52
-0
plugin.go
plugin/dapp/wasm/plugin.go
+19
-0
Makefile
plugin/dapp/wasm/proto/Makefile
+2
-0
create_protobuf.sh
plugin/dapp/wasm/proto/create_protobuf.sh
+4
-0
wasm.proto
plugin/dapp/wasm/proto/wasm.proto
+57
-0
rpc.go
plugin/dapp/wasm/rpc/rpc.go
+1
-0
errors.go
plugin/dapp/wasm/types/errors.go
+11
-0
wasm.go
plugin/dapp/wasm/types/wasm.go
+76
-0
wasm.pb.go
plugin/dapp/wasm/types/wasm.pb.go
+0
-0
No files found.
go.mod
View file @
c25ac1d7
...
...
@@ -28,6 +28,7 @@ require (
github.com/miguelmota/go-solidity-sha3 v0.1.0
github.com/mr-tron/base58 v1.1.3
github.com/pborman/uuid v1.2.0
github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea
github.com/phoreproject/bls v0.0.0-20200525203911-a88a5ae26844
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v0.9.2 // indirect
...
...
go.sum
View file @
c25ac1d7
...
...
@@ -125,6 +125,8 @@ github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/edsrzf/mmap-go v0.0.0-20160512033002-935e0e8a636c h1:JHHhtb9XWJrGNMcrVP6vyzO4dusgi/HnceHTgxSejUM=
github.com/edsrzf/mmap-go v0.0.0-20160512033002-935e0e8a636c/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw=
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/elastic/gosigar v0.8.1-0.20180330100440-37f05ff46ffa h1:XKAhUk/dtp+CV0VO6mhG2V7jA9vbcGcnYF/Ay9NjZrY=
github.com/elastic/gosigar v0.8.1-0.20180330100440-37f05ff46ffa/go.mod h1:cdorVVzy1fhmEqmtgqkoE3bYtCfSCkVyjTyCIo22xvs=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
...
...
@@ -147,6 +149,8 @@ github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x
github.com/go-check/check v0.0.0-20180628173108-788fd7840127 h1:0gkP6mzaMqkmpcJYCFOLkIBwI7xFExG03bbkOkCvUPI=
github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
github.com/go-critic/go-critic v0.3.5-0.20190526074819-1df300866540/go.mod h1:+sE8vrLDS2M0pZkBk0wy6+nLdKexVDrl/jBqQOTDThA=
github.com/go-interpreter/wagon v0.6.0 h1:BBxDxjiJiHgw9EdkYXAWs8NHhwnazZ5P2EWBW5hFNWw=
github.com/go-interpreter/wagon v0.6.0/go.mod h1:5+b/MBYkclRZngKF5s6qrgWxSLgE9F5dFdO1hAueZLc=
github.com/go-kit/kit v0.8.0 h1:Wz+5lgoB0kkuqLEc6NVmwRknTKP6dTGbSqvhZtBI/j0=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-lintpack/lintpack v0.5.2/go.mod h1:NwZuYi2nUHho8XEIZ6SIxihrnPoqBTDqfpXvXAN0sXM=
...
...
@@ -541,6 +545,8 @@ github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml v1.1.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea h1:okKoivlkNRRLqXraEtatHfEhW+D71QTwkaj+4n4M2Xc=
github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea/go.mod h1:3KEU5Dm8MAYWZqity880wOFJ9PhQjyKVZGwAEfc5Q4E=
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0=
github.com/phoreproject/bls v0.0.0-20200525203911-a88a5ae26844 h1:Yflyn+XFLEu7RPzxovgEVLP6Es8JLJrHqdXunpm2ak4=
github.com/phoreproject/bls v0.0.0-20200525203911-a88a5ae26844/go.mod h1:xHJKf2TLXUA39Dhv8k5QmQOxLsbrb1KeTS/3ERfLeqc=
...
...
@@ -633,6 +639,8 @@ github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d/go.mod h1:9OrXJ
github.com/timakin/bodyclose v0.0.0-20190721030226-87058b9bfcec/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk=
github.com/tjfoc/gmsm v1.3.1 h1:+k3IAlF81c31/TllJmIfuCYnjl8ziMdTWGWJcP9J1uo=
github.com/tjfoc/gmsm v1.3.1/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w=
github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc h1:RTUQlKzoZZVG3umWNzOYeFecQLIh+dbxXvJp1zPQJTI=
github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc/go.mod h1:NoCfSFWosfqMqmmD7hApkirIK9ozpHjxRnRxs1l413A=
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef h1:wHSqTBrZW24CsNJDfeh9Ex6Pm0Rcpc7qrgKBiL44vF4=
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
...
...
@@ -646,6 +654,8 @@ github.com/valyala/fasthttp v1.5.0 h1:dhq+O9pmNZFF6qAXpasMO1xSm7dL4qEz2ylfZN8BG9
github.com/valyala/fasthttp v1.5.0/go.mod h1:eriCz9OhZjKCGfJ185a/IDgNl0bg9IbzfpcslMZXU1c=
github.com/valyala/quicktemplate v1.1.1/go.mod h1:EH+4AkTd43SvgIbQHYu59/cJyxDoOVRUAfrukLPuGJ4=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc h1:BCPnHtcboadS0DvysUuJXZ4lWVv5Bh5i7+tbIyi+ck4=
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc/go.mod h1:r45hJU7yEoA81k6MWNhpMj/kms0n14dkzkxYHoB96UM=
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 h1:EKhdznlJHPMoKr0XTrX+IlJs1LH3lyx2nfr1dOlZ79k=
...
...
@@ -749,6 +759,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190228124157-a34e9553db1e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190306220234-b354f8bf4d9e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190322080309-f49334f85ddc/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
...
...
@@ -794,6 +805,7 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IV
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.6.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
...
...
plugin/dapp/init/init.go
View file @
c25ac1d7
...
...
@@ -30,5 +30,6 @@ import (
_
"github.com/33cn/plugin/plugin/dapp/trade"
//auto gen
_
"github.com/33cn/plugin/plugin/dapp/unfreeze"
//auto gen
_
"github.com/33cn/plugin/plugin/dapp/valnode"
//auto gen
_
"github.com/33cn/plugin/plugin/dapp/wasm"
//auto gen
_
"github.com/33cn/plugin/plugin/dapp/x2ethereum"
//auto gen
)
plugin/dapp/wasm/commands/wasm.go
0 → 100644
View file @
c25ac1d7
package
commands
import
(
"fmt"
"io/ioutil"
"os"
"github.com/33cn/chain33/rpc/jsonclient"
rpctypes
"github.com/33cn/chain33/rpc/types"
"github.com/33cn/chain33/types"
wasmtypes
"github.com/33cn/plugin/plugin/dapp/wasm/types"
"github.com/spf13/cobra"
)
// TokenCmd token 命令行
func
Cmd
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"wasm"
,
Short
:
"Wasm management"
,
Args
:
cobra
.
MinimumNArgs
(
1
),
}
cmd
.
AddCommand
(
CmdCheckContract
(),
CmdCreateContract
(),
CmdCallContract
(),
)
return
cmd
}
func
CmdCheckContract
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"check"
,
Short
:
"Check whether the contract with the given name exists or not."
,
Run
:
checkContract
,
}
cmd
.
Flags
()
.
StringP
(
"name"
,
"n"
,
""
,
"contract name"
)
_
=
cmd
.
MarkFlagRequired
(
"name"
)
return
cmd
}
func
CmdCreateContract
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"create"
,
Short
:
"publish a new contract on chain33"
,
Run
:
createContract
,
}
cmd
.
Flags
()
.
StringP
(
"name"
,
"n"
,
""
,
"contract name"
)
cmd
.
Flags
()
.
StringP
(
"path"
,
"p"
,
""
,
"path of the wasm file, such as ./test.wasm"
)
_
=
cmd
.
MarkFlagRequired
(
"name"
)
_
=
cmd
.
MarkFlagRequired
(
"path"
)
return
cmd
}
func
CmdCallContract
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"call"
,
Short
:
"call contract on chain33"
,
Run
:
callContract
,
}
cmd
.
Flags
()
.
StringP
(
"name"
,
"n"
,
""
,
"contract name"
)
cmd
.
Flags
()
.
StringP
(
"method"
,
"m"
,
""
,
"method name"
)
cmd
.
Flags
()
.
IntSliceP
(
"parameters"
,
"p"
,
nil
,
"parameters of the method which should be num"
)
_
=
cmd
.
MarkFlagRequired
(
"name"
)
_
=
cmd
.
MarkFlagRequired
(
"method"
)
return
cmd
}
func
checkContract
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
rpcLaddr
,
_
:=
cmd
.
Flags
()
.
GetString
(
"rpc_laddr"
)
name
,
_
:=
cmd
.
Flags
()
.
GetString
(
"name"
)
params
:=
rpctypes
.
Query4Jrpc
{
Execer
:
wasmtypes
.
WasmX
,
FuncName
:
"Check"
,
Payload
:
types
.
MustPBToJSON
(
&
wasmtypes
.
QueryCheckConract
{
Name
:
name
,
}),
}
var
resp
types
.
Reply
ctx
:=
jsonclient
.
NewRPCCtx
(
rpcLaddr
,
"Chain33.Query"
,
params
,
&
resp
)
ctx
.
Run
()
}
func
createContract
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
rpcLaddr
,
_
:=
cmd
.
Flags
()
.
GetString
(
"rpc_laddr"
)
name
,
_
:=
cmd
.
Flags
()
.
GetString
(
"name"
)
path
,
_
:=
cmd
.
Flags
()
.
GetString
(
"path"
)
// Read WebAssembly *.wasm file.
code
,
err
:=
ioutil
.
ReadFile
(
path
)
if
err
!=
nil
{
fmt
.
Fprintln
(
os
.
Stderr
,
err
)
return
}
payload
:=
wasmtypes
.
WasmCreate
{
Name
:
name
,
Code
:
code
,
}
params
:=
rpctypes
.
CreateTxIn
{
Execer
:
wasmtypes
.
WasmX
,
ActionName
:
"Create"
,
Payload
:
types
.
MustPBToJSON
(
&
payload
),
}
ctx
:=
jsonclient
.
NewRPCCtx
(
rpcLaddr
,
"Chain33.CreateTransaction"
,
params
,
nil
)
ctx
.
RunWithoutMarshal
()
}
func
callContract
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
rpcLaddr
,
_
:=
cmd
.
Flags
()
.
GetString
(
"rpc_laddr"
)
name
,
_
:=
cmd
.
Flags
()
.
GetString
(
"name"
)
method
,
_
:=
cmd
.
Flags
()
.
GetString
(
"method"
)
parameters
,
_
:=
cmd
.
Flags
()
.
GetIntSlice
(
"parameters"
)
var
parameters2
[]
int64
for
_
,
param
:=
range
parameters
{
parameters2
=
append
(
parameters2
,
int64
(
param
))
}
payload
:=
wasmtypes
.
WasmCall
{
Contract
:
name
,
Method
:
method
,
Parameters
:
parameters2
,
}
params
:=
rpctypes
.
CreateTxIn
{
Execer
:
wasmtypes
.
WasmX
,
ActionName
:
"Call"
,
Payload
:
types
.
MustPBToJSON
(
&
payload
),
}
ctx
:=
jsonclient
.
NewRPCCtx
(
rpcLaddr
,
"Chain33.CreateTransaction"
,
params
,
nil
)
ctx
.
RunWithoutMarshal
()
}
plugin/dapp/wasm/executor/callback.go
0 → 100644
View file @
c25ac1d7
package
executor
import
(
"github.com/33cn/chain33/types"
types2
"github.com/33cn/plugin/plugin/dapp/wasm/types"
)
func
setStateDB
(
key
,
value
[]
byte
)
{
wasmCB
.
kvs
=
append
(
wasmCB
.
kvs
,
&
types
.
KeyValue
{
Key
:
wasmCB
.
formatStateKey
(
key
),
Value
:
value
,
})
}
func
getStateDBSize
(
key
[]
byte
)
int
{
value
,
err
:=
getStateDB
(
key
)
if
err
!=
nil
{
return
0
}
return
len
(
value
)
}
func
getStateDB
(
key
[]
byte
)
([]
byte
,
error
)
{
return
wasmCB
.
GetStateDB
()
.
Get
(
wasmCB
.
formatStateKey
(
key
))
}
func
setLocalDB
(
key
,
value
[]
byte
)
{
preValue
,
_
:=
getLocalDB
(
key
)
wasmCB
.
receiptLogs
=
append
(
wasmCB
.
receiptLogs
,
&
types
.
ReceiptLog
{
Ty
:
types2
.
TyLogLocalData
,
Log
:
types
.
Encode
(
&
types2
.
LocalDataLog
{
Key
:
wasmCB
.
formatLocalKey
(
key
),
PreValue
:
preValue
,
CurValue
:
value
,
}),
})
}
func
getLocalDBSize
(
key
[]
byte
)
int
{
value
,
err
:=
getLocalDB
(
key
)
if
err
!=
nil
{
return
0
}
return
len
(
value
)
}
func
getLocalDB
(
key
[]
byte
)
([]
byte
,
error
)
{
return
wasmCB
.
GetLocalDB
()
.
Get
(
wasmCB
.
formatLocalKey
(
key
))
}
func
execFrozen
(
addr
string
,
amount
int64
)
error
{
receipt
,
err
:=
wasmCB
.
GetCoinsAccount
()
.
ExecFrozen
(
addr
,
wasmCB
.
execAddr
,
amount
)
if
err
!=
nil
{
return
err
}
wasmCB
.
kvs
=
append
(
wasmCB
.
kvs
,
receipt
.
KV
...
)
wasmCB
.
receiptLogs
=
append
(
wasmCB
.
receiptLogs
,
receipt
.
Logs
...
)
return
nil
}
func
execActive
(
addr
string
,
amount
int64
)
error
{
receipt
,
err
:=
wasmCB
.
GetCoinsAccount
()
.
ExecActive
(
addr
,
wasmCB
.
execAddr
,
amount
)
if
err
!=
nil
{
return
err
}
wasmCB
.
kvs
=
append
(
wasmCB
.
kvs
,
receipt
.
KV
...
)
wasmCB
.
receiptLogs
=
append
(
wasmCB
.
receiptLogs
,
receipt
.
Logs
...
)
return
nil
}
func
execTransfer
(
from
,
to
string
,
amount
int64
)
error
{
receipt
,
err
:=
wasmCB
.
GetCoinsAccount
()
.
ExecTransfer
(
from
,
to
,
wasmCB
.
execAddr
,
amount
)
if
err
!=
nil
{
return
err
}
wasmCB
.
kvs
=
append
(
wasmCB
.
kvs
,
receipt
.
KV
...
)
wasmCB
.
receiptLogs
=
append
(
wasmCB
.
receiptLogs
,
receipt
.
Logs
...
)
return
nil
}
func
execTransferFrozen
(
from
,
to
string
,
amount
int64
)
error
{
receipt
,
err
:=
wasmCB
.
GetCoinsAccount
()
.
ExecTransferFrozen
(
from
,
to
,
wasmCB
.
execAddr
,
amount
)
if
err
!=
nil
{
return
err
}
wasmCB
.
kvs
=
append
(
wasmCB
.
kvs
,
receipt
.
KV
...
)
wasmCB
.
receiptLogs
=
append
(
wasmCB
.
receiptLogs
,
receipt
.
Logs
...
)
return
nil
}
func
getFrom
()
string
{
return
wasmCB
.
tx
.
From
()
}
func
getHeight
()
int64
{
return
wasmCB
.
GetHeight
()
}
func
getRandom
()
int64
{
req
:=
&
types
.
ReqRandHash
{
ExecName
:
"ticket"
,
BlockNum
:
5
,
Hash
:
wasmCB
.
GetLastHash
(),
}
hash
,
err
:=
wasmCB
.
GetExecutorAPI
()
.
GetRandNum
(
req
)
if
err
!=
nil
{
return
-
1
}
var
rand
int64
for
_
,
c
:=
range
hash
{
rand
=
rand
*
256
+
int64
(
c
)
}
return
rand
}
func
printlog
(
s
string
)
{
wasmCB
.
logs
=
append
(
wasmCB
.
logs
,
s
)
}
plugin/dapp/wasm/executor/db.go
0 → 100644
View file @
c25ac1d7
package
executor
import
(
"fmt"
"github.com/33cn/chain33/common/address"
)
const
(
KeyPrefixContract
=
"mavl-wasm-%s:%s"
KeyPrefixLocal
=
"LODB-wasm-%s:%s"
)
func
contractKey
(
name
string
)
[]
byte
{
contractAddr
:=
address
.
ExecAddress
(
name
)
key
:=
"mavl-wasm-address:"
+
contractAddr
return
[]
byte
(
key
)
}
func
(
w
*
Wasm
)
formatStateKey
(
key
[]
byte
)
[]
byte
{
addr
:=
address
.
ExecAddress
(
w
.
contractAddr
)
return
[]
byte
(
fmt
.
Sprintf
(
KeyPrefixContract
,
addr
,
string
(
key
)))
}
func
(
w
*
Wasm
)
formatLocalKey
(
key
[]
byte
)
[]
byte
{
addr
:=
address
.
ExecAddress
(
w
.
contractAddr
)
return
[]
byte
(
fmt
.
Sprintf
(
KeyPrefixLocal
,
addr
,
string
(
key
)))
}
func
(
w
*
Wasm
)
contractExist
(
name
string
)
bool
{
_
,
err
:=
w
.
GetStateDB
()
.
Get
(
contractKey
(
name
))
return
err
==
nil
}
func
(
w
*
Wasm
)
getContract
(
name
string
)
([]
byte
,
error
)
{
return
w
.
GetStateDB
()
.
Get
(
contractKey
(
name
))
}
plugin/dapp/wasm/executor/exec.go
0 → 100644
View file @
c25ac1d7
package
executor
import
(
"encoding/hex"
"github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/types"
types2
"github.com/33cn/plugin/plugin/dapp/wasm/types"
"github.com/perlin-network/life/compiler"
"github.com/perlin-network/life/exec"
validation
"github.com/perlin-network/life/wasm-validation"
)
var
wasmCB
*
Wasm
func
(
w
*
Wasm
)
Exec_Create
(
payload
*
types2
.
WasmCreate
,
tx
*
types
.
Transaction
,
index
int
)
(
*
types
.
Receipt
,
error
)
{
if
payload
==
nil
{
return
nil
,
types
.
ErrInvalidParam
}
name
:=
payload
.
Name
if
!
validateName
(
name
)
{
return
nil
,
types2
.
ErrInvalidContractName
}
if
w
.
contractExist
(
name
)
{
return
nil
,
types2
.
ErrContractExist
}
code
:=
payload
.
Code
if
len
(
code
)
>
1
<<
20
{
//TODO: max size to define
return
nil
,
types2
.
ErrCodeOversize
}
if
err
:=
validation
.
ValidateWasm
(
code
);
err
!=
nil
{
return
nil
,
types2
.
ErrInvalidWasm
}
var
receiptLogs
[]
*
types
.
ReceiptLog
kv
:=
[]
*
types
.
KeyValue
{{
Key
:
contractKey
(
name
),
Value
:
code
,
}}
receiptLog
:=
&
types
.
ReceiptLog
{
Ty
:
types2
.
TyLogWasmCreate
,
Log
:
types
.
Encode
(
&
types2
.
CreateContractLog
{
Name
:
name
,
Code
:
hex
.
EncodeToString
(
code
),
}),
}
receiptLogs
=
append
(
receiptLogs
,
receiptLog
)
return
&
types
.
Receipt
{
Ty
:
types
.
ExecOk
,
KV
:
kv
,
Logs
:
receiptLogs
,
},
nil
}
func
(
w
*
Wasm
)
Exec_Call
(
payload
*
types2
.
WasmCall
,
tx
*
types
.
Transaction
,
index
int
)
(
*
types
.
Receipt
,
error
)
{
if
payload
==
nil
{
return
nil
,
types
.
ErrInvalidParam
}
code
,
err
:=
w
.
getContract
(
payload
.
Contract
)
if
err
!=
nil
{
return
nil
,
err
}
vm
,
err
:=
exec
.
NewVirtualMachine
(
code
,
exec
.
VMConfig
{
DefaultMemoryPages
:
128
,
DefaultTableSize
:
128
,
DisableFloatingPoint
:
true
,
GasLimit
:
uint64
(
tx
.
Fee
),
},
new
(
Resolver
),
&
compiler
.
SimpleGasPolicy
{
GasPerInstruction
:
1
})
if
err
!=
nil
{
return
nil
,
err
}
// Get the function ID of the entry function to be executed.
entryID
,
ok
:=
vm
.
GetFunctionExport
(
payload
.
Method
)
if
!
ok
{
return
nil
,
types2
.
ErrInvalidMethod
}
w
.
contractAddr
=
address
.
ExecAddress
(
payload
.
Contract
)
w
.
tx
=
tx
w
.
execAddr
=
address
.
ExecAddress
(
string
(
tx
.
Execer
))
wasmCB
=
w
defer
func
()
{
wasmCB
=
nil
}()
// Run the WebAssembly module's entry function.
ret
,
err
:=
vm
.
RunWithGasLimit
(
entryID
,
int
(
tx
.
Fee
),
payload
.
Parameters
...
)
if
err
!=
nil
{
return
nil
,
err
}
var
logs
[]
*
types
.
ReceiptLog
logs
=
append
(
logs
,
&
types
.
ReceiptLog
{
Ty
:
types2
.
TyLogWasmCall
,
Log
:
types
.
Encode
(
&
types2
.
CallContractLog
{
Contract
:
payload
.
Contract
,
Method
:
payload
.
Method
,
Result
:
ret
,
})})
logs
=
append
(
logs
,
&
types
.
ReceiptLog
{
Ty
:
types2
.
TyLogCustom
,
Log
:
types
.
Encode
(
&
types2
.
CustomLog
{
Info
:
w
.
logs
,
})})
logs
=
append
(
logs
,
w
.
receiptLogs
...
)
return
&
types
.
Receipt
{
Ty
:
types
.
ExecOk
,
KV
:
w
.
kvs
,
Logs
:
logs
,
},
nil
}
func
validateName
(
name
string
)
bool
{
if
!
types2
.
NameReg
.
MatchString
(
name
)
||
len
(
name
)
<
4
||
len
(
name
)
>
20
{
return
false
}
return
true
}
plugin/dapp/wasm/executor/exec_del_local.go
0 → 100644
View file @
c25ac1d7
package
executor
import
(
"github.com/33cn/chain33/types"
types2
"github.com/33cn/plugin/plugin/dapp/wasm/types"
)
func
(
w
*
Wasm
)
ExecDelLocal_Create
(
payload
*
types2
.
WasmCreate
,
tx
*
types
.
Transaction
,
receipt
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
return
execDelLocal
(
receipt
)
}
func
(
w
*
Wasm
)
ExecDelLocal_Call
(
payload
*
types2
.
WasmCall
,
tx
*
types
.
Transaction
,
receipt
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
return
execDelLocal
(
receipt
)
}
func
execDelLocal
(
receipt
*
types
.
ReceiptData
)
(
*
types
.
LocalDBSet
,
error
)
{
if
receipt
.
Ty
!=
types
.
ExecOk
{
return
nil
,
nil
}
set
:=
&
types
.
LocalDBSet
{}
for
_
,
item
:=
range
receipt
.
Logs
{
if
item
.
Ty
==
types2
.
TyLogLocalData
{
var
data
types2
.
LocalDataLog
err
:=
types
.
Decode
(
item
.
Log
,
&
data
)
if
err
!=
nil
{
log
.
Error
(
"execLocal"
,
"decode error"
,
err
)
continue
}
set
.
KV
=
append
(
set
.
KV
,
&
types
.
KeyValue
{
Key
:
data
.
Key
,
Value
:
data
.
PreValue
})
}
}
return
set
,
nil
}
plugin/dapp/wasm/executor/exec_local.go
0 → 100644
View file @
c25ac1d7
package
executor
import
(
"github.com/33cn/chain33/types"
types2
"github.com/33cn/plugin/plugin/dapp/wasm/types"
)
func
(
w
*
Wasm
)
ExecLocal_Create
(
payload
*
types2
.
WasmCreate
,
tx
*
types
.
Transaction
,
receipt
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
return
execLocal
(
receipt
)
}
func
(
w
*
Wasm
)
ExecLocal_Call
(
payload
*
types2
.
WasmCall
,
tx
*
types
.
Transaction
,
receipt
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
return
execLocal
(
receipt
)
}
func
execLocal
(
receipt
*
types
.
ReceiptData
)
(
*
types
.
LocalDBSet
,
error
)
{
if
receipt
.
Ty
!=
types
.
ExecOk
{
return
nil
,
nil
}
set
:=
&
types
.
LocalDBSet
{}
for
_
,
item
:=
range
receipt
.
Logs
{
if
item
.
Ty
==
types2
.
TyLogLocalData
{
var
data
types2
.
LocalDataLog
err
:=
types
.
Decode
(
item
.
Log
,
&
data
)
if
err
!=
nil
{
log
.
Error
(
"execLocal"
,
"decode error"
,
err
)
continue
}
set
.
KV
=
append
(
set
.
KV
,
&
types
.
KeyValue
{
Key
:
data
.
Key
,
Value
:
data
.
CurValue
})
}
}
return
set
,
nil
}
plugin/dapp/wasm/executor/query.go
0 → 100644
View file @
c25ac1d7
package
executor
import
(
"github.com/33cn/chain33/types"
types2
"github.com/33cn/plugin/plugin/dapp/wasm/types"
)
func
(
w
*
Wasm
)
Query_Check
(
query
*
types2
.
QueryCheckConract
)
(
types
.
Message
,
error
)
{
if
query
==
nil
{
return
nil
,
types
.
ErrInvalidParam
}
return
&
types
.
Reply
{
IsOk
:
w
.
contractExist
(
query
.
Name
)},
nil
}
//func (w *Wasm) Query_CreateTransaction(query *types2.QueryCreateTransaction) (types.Message, error) {
// if query == nil {
// return nil, types.ErrInvalidParam
// }
// if !validateName(query.Name) {
// return nil, types2.ErrInvalidContractName
// }
//
// var action *types2.WasmAction
// switch query.Ty {
// case types2.WasmActionCreate:
// action = &types2.WasmAction{
// Value: &types2.WasmAction_Create{
// Create: &types2.WasmCreate{
// Name: query.Name,
// Code: query.Code,
// },
// },
// }
// case types2.WasmActionCall:
// action = &types2.WasmAction{
// Value: &types2.WasmAction_Call{
// Call: &types2.WasmCall{
// Contract: query.Name,
// Method: query.Method,
// Parameters: query.Parameters,
// },
// },
// }
// }
// if action == nil {
// return nil, types.ErrInvalidParam
// }
// cfg := w.GetAPI().GetConfig()
// tx := &types.Transaction{
// Payload: types.Encode(action),
// }
// tx, err := types.FormatTx(w.GetAPI().GetConfig(), cfg.ExecName(types2.WasmX), tx)
// if err != nil {
// return nil, err
// }
// if tx.Fee < query.Fee {
// tx.Fee = query.Fee
// }
// resp := types.ReplyString{
// Data: common.ToHex(types.Encode(tx)),
// }
// return &resp, nil
//}
plugin/dapp/wasm/executor/resolver.go
0 → 100644
View file @
c25ac1d7
package
executor
import
(
"fmt"
"github.com/perlin-network/life/exec"
)
// Resolver defines imports for WebAssembly modules ran in Life.
type
Resolver
struct
{}
// ResolveFunc defines a set of import functions that may be called within a WebAssembly module.
func
(
r
*
Resolver
)
ResolveFunc
(
module
,
field
string
)
exec
.
FunctionImport
{
switch
module
{
case
"env"
:
switch
field
{
case
"setStateDB"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
keyPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
keyLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
1
]))
key
:=
vm
.
Memory
[
keyPtr
:
keyPtr
+
keyLen
]
valuePtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
2
]))
valueLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
3
]))
value
:=
vm
.
Memory
[
valuePtr
:
valuePtr
+
valueLen
]
setStateDB
(
key
,
value
)
return
0
}
case
"getStateDBSize"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
keyPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
keyLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
1
]))
key
:=
vm
.
Memory
[
keyPtr
:
keyPtr
+
keyLen
]
return
int64
(
getStateDBSize
(
key
))
}
case
"getStateDB"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
keyPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
keyLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
1
]))
key
:=
vm
.
Memory
[
keyPtr
:
keyPtr
+
keyLen
]
valuePtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
2
]))
valueLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
3
]))
value
,
err
:=
getStateDB
(
key
)
if
err
!=
nil
{
for
i
:=
0
;
i
<
valueLen
;
i
++
{
vm
.
Memory
[
valuePtr
+
i
]
=
0
}
return
0
}
if
valueLen
!=
len
(
value
)
{
return
0
}
for
i
,
c
:=
range
value
{
vm
.
Memory
[
valuePtr
+
i
]
=
c
}
return
int64
(
valueLen
)
}
case
"setLocalDB"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
keyPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
keyLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
1
]))
key
:=
vm
.
Memory
[
keyPtr
:
keyPtr
+
keyLen
]
valuePtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
2
]))
valueLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
3
]))
value
:=
vm
.
Memory
[
valuePtr
:
valuePtr
+
valueLen
]
setLocalDB
(
key
,
value
)
return
0
}
case
"getLocalDBSize"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
keyPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
keyLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
1
]))
key
:=
vm
.
Memory
[
keyPtr
:
keyPtr
+
keyLen
]
return
int64
(
getLocalDBSize
(
key
))
}
case
"getLocalDB"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
keyPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
keyLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
1
]))
key
:=
vm
.
Memory
[
keyPtr
:
keyPtr
+
keyLen
]
valuePtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
2
]))
valueLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
3
]))
value
,
err
:=
getLocalDB
(
key
)
if
err
!=
nil
{
for
i
:=
0
;
i
<
valueLen
;
i
++
{
vm
.
Memory
[
valuePtr
+
i
]
=
0
}
return
0
}
if
valueLen
!=
len
(
value
)
{
return
0
}
for
i
,
c
:=
range
value
{
vm
.
Memory
[
valuePtr
+
i
]
=
c
}
return
int64
(
valueLen
)
}
case
"execFrozen"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
addrPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
addrLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
1
]))
addr
:=
string
(
vm
.
Memory
[
addrPtr
:
addrPtr
+
addrLen
])
amount
:=
vm
.
GetCurrentFrame
()
.
Locals
[
2
]
err
:=
execFrozen
(
addr
,
amount
)
if
err
!=
nil
{
return
-
1
}
return
0
}
case
"execActive"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
addrPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
addrLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
1
]))
addr
:=
string
(
vm
.
Memory
[
addrPtr
:
addrPtr
+
addrLen
])
amount
:=
vm
.
GetCurrentFrame
()
.
Locals
[
2
]
err
:=
execActive
(
addr
,
amount
)
if
err
!=
nil
{
return
-
1
}
return
0
}
case
"execTransfer"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
fromPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
fromLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
1
]))
fromAddr
:=
string
(
vm
.
Memory
[
fromPtr
:
fromPtr
+
fromLen
])
toPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
2
]))
toLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
3
]))
toAddr
:=
string
(
vm
.
Memory
[
toPtr
:
toPtr
+
toLen
])
amount
:=
vm
.
GetCurrentFrame
()
.
Locals
[
4
]
err
:=
execTransfer
(
fromAddr
,
toAddr
,
amount
)
if
err
!=
nil
{
return
-
1
}
return
0
}
case
"execTransferFrozen"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
fromPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
fromLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
1
]))
fromAddr
:=
string
(
vm
.
Memory
[
fromPtr
:
fromPtr
+
fromLen
])
toPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
2
]))
toLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
3
]))
toAddr
:=
string
(
vm
.
Memory
[
toPtr
:
toPtr
+
toLen
])
amount
:=
vm
.
GetCurrentFrame
()
.
Locals
[
4
]
err
:=
execTransferFrozen
(
fromAddr
,
toAddr
,
amount
)
if
err
!=
nil
{
return
-
1
}
return
0
}
case
"getFrom"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
fromAddr
:=
[]
byte
(
getFrom
())
fromPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
fromLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
1
]))
copy
(
vm
.
Memory
[
fromPtr
:
fromPtr
+
fromLen
],
fromAddr
)
return
0
}
case
"getHeight"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
return
getHeight
()
}
case
"getRandom"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
return
getRandom
()
}
case
"printlog"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
fmt
.
Println
(
vm
.
GetCurrentFrame
()
.
Locals
)
logPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
logLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
1
]))
logInfo
:=
string
(
vm
.
Memory
[
logPtr
:
logPtr
+
logLen
])
printlog
(
logInfo
)
return
0
}
default
:
panic
(
fmt
.
Errorf
(
"unknown field: %s"
,
field
))
}
default
:
panic
(
fmt
.
Errorf
(
"unknown module: %s"
,
module
))
}
}
// ResolveGlobal defines a set of global variables for use within a WebAssembly module.
func
(
r
*
Resolver
)
ResolveGlobal
(
module
,
field
string
)
int64
{
fmt
.
Printf
(
"Resolve global: %s %s
\n
"
,
module
,
field
)
switch
module
{
case
"env"
:
switch
field
{
case
"__life_magic"
:
return
424
default
:
panic
(
fmt
.
Errorf
(
"unknown field: %s"
,
field
))
}
default
:
panic
(
fmt
.
Errorf
(
"unknown module: %s"
,
module
))
}
}
plugin/dapp/wasm/executor/wasm.go
0 → 100644
View file @
c25ac1d7
package
executor
import
(
"github.com/33cn/chain33/common/log/log15"
drivers
"github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
types2
"github.com/33cn/plugin/plugin/dapp/wasm/types"
)
var
driverName
=
types2
.
WasmX
var
log
=
log15
.
New
(
"module"
,
"execs."
+
types2
.
WasmX
)
func
Init
(
name
string
,
cfg
*
types
.
Chain33Config
,
sub
[]
byte
)
{
if
name
!=
driverName
{
panic
(
"system dapp can not be rename"
)
}
drivers
.
Register
(
cfg
,
name
,
newWasm
,
cfg
.
GetDappFork
(
name
,
"Enable"
))
initExecType
()
}
func
initExecType
()
{
ety
:=
types
.
LoadExecutorType
(
driverName
)
ety
.
InitFuncList
(
types
.
ListMethod
(
&
Wasm
{}))
}
type
Wasm
struct
{
drivers
.
DriverBase
tx
*
types
.
Transaction
kvs
[]
*
types
.
KeyValue
receiptLogs
[]
*
types
.
ReceiptLog
logs
[]
string
execAddr
string
contractAddr
string
}
func
newWasm
()
drivers
.
Driver
{
d
:=
&
Wasm
{}
d
.
SetChild
(
d
)
d
.
SetExecutorType
(
types
.
LoadExecutorType
(
driverName
))
return
d
}
// GetName 获取执行器别名
func
GetName
()
string
{
return
newWasm
()
.
GetName
()
}
func
(
w
*
Wasm
)
GetDriverName
()
string
{
return
driverName
}
plugin/dapp/wasm/plugin.go
0 → 100644
View file @
c25ac1d7
package
wasm
import
(
"github.com/33cn/chain33/pluginmgr"
"github.com/33cn/plugin/plugin/dapp/wasm/commands"
"github.com/33cn/plugin/plugin/dapp/wasm/executor"
_
"github.com/33cn/plugin/plugin/dapp/wasm/rpc"
"github.com/33cn/plugin/plugin/dapp/wasm/types"
)
func
init
()
{
pluginmgr
.
Register
(
&
pluginmgr
.
PluginBase
{
Name
:
types
.
WasmX
,
ExecName
:
executor
.
GetName
(),
Exec
:
executor
.
Init
,
Cmd
:
commands
.
Cmd
,
//RPC: rpc.Init,
})
}
plugin/dapp/wasm/proto/Makefile
0 → 100644
View file @
c25ac1d7
all
:
sh ./create_protobuf.sh
plugin/dapp/wasm/proto/create_protobuf.sh
0 → 100755
View file @
c25ac1d7
#!/bin/sh
chain33_path
=
$(
go list
-f
'{{.Dir}}'
"github.com/33cn/chain33"
)
protoc
--go_out
=
plugins
=
grpc:../types ./
*
.proto
--proto_path
=
.
--proto_path
=
"
${
chain33_path
}
/types/proto/"
plugin/dapp/wasm/proto/wasm.proto
0 → 100644
View file @
c25ac1d7
syntax
=
"proto3"
;
package
types
;
message
wasmAction
{
oneof
value
{
wasmCreate
create
=
1
;
wasmCall
call
=
2
;
}
int32
ty
=
3
;
}
message
wasmCreate
{
string
name
=
1
;
bytes
code
=
2
;
}
message
wasmCall
{
string
contract
=
1
;
string
method
=
2
;
repeated
int64
parameters
=
3
;
}
message
queryCheckConract
{
string
name
=
1
;
}
message
queryCreateTransaction
{
int32
ty
=
1
;
string
name
=
2
;
bytes
code
=
3
;
string
method
=
4
;
repeated
int64
parameters
=
5
;
int64
fee
=
6
;
}
message
customLog
{
repeated
string
info
=
1
;
}
message
createContractLog
{
string
name
=
1
;
string
code
=
2
;
}
message
callContractLog
{
string
contract
=
1
;
string
method
=
2
;
int64
result
=
3
;
}
message
localDataLog
{
bytes
key
=
1
;
bytes
preValue
=
2
;
bytes
curValue
=
3
;
}
plugin/dapp/wasm/rpc/rpc.go
0 → 100644
View file @
c25ac1d7
package
rpc
plugin/dapp/wasm/types/errors.go
0 → 100644
View file @
c25ac1d7
package
types
import
"errors"
var
(
ErrContractExist
=
errors
.
New
(
"contract exist"
)
ErrInvalidWasm
=
errors
.
New
(
"invalid wasm code"
)
ErrCodeOversize
=
errors
.
New
(
"code oversize"
)
ErrInvalidMethod
=
errors
.
New
(
"invalid method"
)
ErrInvalidContractName
=
errors
.
New
(
"invalid contract name"
)
)
plugin/dapp/wasm/types/wasm.go
0 → 100644
View file @
c25ac1d7
package
types
import
(
"reflect"
"regexp"
"github.com/33cn/chain33/types"
)
var
NameReg
*
regexp
.
Regexp
const
(
WasmX
=
"wasm"
NameRegExp
=
"^[a-z0-9]+$"
)
// action for executor
const
(
WasmActionCreate
=
iota
+
1
WasmActionCall
)
// log ty for executor
const
(
TyLogWasmCreate
=
iota
+
100
TyLogWasmCall
TyLogCustom
TyLogLocalData
)
func
init
()
{
types
.
AllowUserExec
=
append
(
types
.
AllowUserExec
,
[]
byte
(
WasmX
))
types
.
RegFork
(
WasmX
,
InitFork
)
types
.
RegExec
(
WasmX
,
InitExecutor
)
NameReg
,
_
=
regexp
.
Compile
(
NameRegExp
)
}
func
InitFork
(
cfg
*
types
.
Chain33Config
)
{
cfg
.
RegisterDappFork
(
WasmX
,
"Enable"
,
0
)
}
func
InitExecutor
(
cfg
*
types
.
Chain33Config
)
{
types
.
RegistorExecutor
(
WasmX
,
NewType
(
cfg
))
}
type
WasmType
struct
{
types
.
ExecTypeBase
}
func
NewType
(
cfg
*
types
.
Chain33Config
)
*
WasmType
{
c
:=
&
WasmType
{}
c
.
SetChild
(
c
)
c
.
SetConfig
(
cfg
)
return
c
}
func
(
t
*
WasmType
)
GetPayload
()
types
.
Message
{
return
&
WasmAction
{}
}
func
(
t
*
WasmType
)
GetTypeMap
()
map
[
string
]
int32
{
return
map
[
string
]
int32
{
"Create"
:
WasmActionCreate
,
"Call"
:
WasmActionCall
,
}
}
func
(
t
*
WasmType
)
GetLogMap
()
map
[
int64
]
*
types
.
LogInfo
{
return
map
[
int64
]
*
types
.
LogInfo
{
TyLogWasmCreate
:
{
Ty
:
reflect
.
TypeOf
(
CreateContractLog
{}),
Name
:
"LogWasmCreate"
},
TyLogWasmCall
:
{
Ty
:
reflect
.
TypeOf
(
CallContractLog
{}),
Name
:
"LogWasmCall"
},
TyLogCustom
:
{
Ty
:
reflect
.
TypeOf
(
CustomLog
{}),
Name
:
"LogWasmCustom"
},
TyLogLocalData
:
{
Ty
:
reflect
.
TypeOf
(
LocalDataLog
{}),
Name
:
"LogWasmLocalData"
},
}
}
plugin/dapp/wasm/types/wasm.pb.go
0 → 100644
View file @
c25ac1d7
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment