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
4e9550f8
Commit
4e9550f8
authored
Sep 10, 2020
by
袁兴强
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add some callback functions
parent
496555ae
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
153 additions
and
0 deletions
+153
-0
callback.go
plugin/dapp/wasm/executor/callback.go
+51
-0
resolver.go
plugin/dapp/wasm/executor/resolver.go
+102
-0
No files found.
plugin/dapp/wasm/executor/callback.go
View file @
4e9550f8
package
executor
package
executor
import
(
import
(
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/types"
types2
"github.com/33cn/plugin/plugin/dapp/wasm/types"
types2
"github.com/33cn/plugin/plugin/dapp/wasm/types"
)
)
//stateDB wrapper
func
setStateDB
(
key
,
value
[]
byte
)
{
func
setStateDB
(
key
,
value
[]
byte
)
{
wasmCB
.
kvs
=
append
(
wasmCB
.
kvs
,
&
types
.
KeyValue
{
wasmCB
.
kvs
=
append
(
wasmCB
.
kvs
,
&
types
.
KeyValue
{
Key
:
wasmCB
.
formatStateKey
(
key
),
Key
:
wasmCB
.
formatStateKey
(
key
),
...
@@ -24,6 +27,7 @@ func getStateDB(key []byte) ([]byte, error) {
...
@@ -24,6 +27,7 @@ func getStateDB(key []byte) ([]byte, error) {
return
wasmCB
.
GetStateDB
()
.
Get
(
wasmCB
.
formatStateKey
(
key
))
return
wasmCB
.
GetStateDB
()
.
Get
(
wasmCB
.
formatStateKey
(
key
))
}
}
//localDB wrapper
func
setLocalDB
(
key
,
value
[]
byte
)
{
func
setLocalDB
(
key
,
value
[]
byte
)
{
preValue
,
_
:=
getLocalDB
(
key
)
preValue
,
_
:=
getLocalDB
(
key
)
wasmCB
.
receiptLogs
=
append
(
wasmCB
.
receiptLogs
,
&
types
.
ReceiptLog
{
wasmCB
.
receiptLogs
=
append
(
wasmCB
.
receiptLogs
,
&
types
.
ReceiptLog
{
...
@@ -48,6 +52,45 @@ func getLocalDB(key []byte) ([]byte, error) {
...
@@ -48,6 +52,45 @@ func getLocalDB(key []byte) ([]byte, error) {
return
wasmCB
.
GetLocalDB
()
.
Get
(
wasmCB
.
formatLocalKey
(
key
))
return
wasmCB
.
GetLocalDB
()
.
Get
(
wasmCB
.
formatLocalKey
(
key
))
}
}
//account wrapper
func
getBalance
(
addr
,
execer
string
)
(
balance
,
frozen
int64
,
err
error
)
{
accounts
,
err
:=
wasmCB
.
GetCoinsAccount
()
.
GetBalance
(
wasmCB
.
GetAPI
(),
&
types
.
ReqBalance
{
Addresses
:
[]
string
{
addr
},
Execer
:
execer
,
})
if
err
!=
nil
{
return
-
1
,
-
1
,
err
}
return
accounts
[
0
]
.
Balance
,
accounts
[
0
]
.
Frozen
,
nil
}
func
transfer
(
from
,
to
string
,
amount
int64
)
error
{
receipt
,
err
:=
wasmCB
.
GetCoinsAccount
()
.
Transfer
(
from
,
to
,
amount
)
if
err
!=
nil
{
return
err
}
wasmCB
.
receiptLogs
=
append
(
wasmCB
.
receiptLogs
,
receipt
.
Logs
...
)
return
nil
}
func
transferToExec
(
addr
,
execaddr
string
,
amount
int64
)
error
{
receipt
,
err
:=
wasmCB
.
GetCoinsAccount
()
.
TransferToExec
(
addr
,
execaddr
,
amount
)
if
err
!=
nil
{
return
err
}
wasmCB
.
receiptLogs
=
append
(
wasmCB
.
receiptLogs
,
receipt
.
Logs
...
)
return
nil
}
func
transferWithdraw
(
addr
,
execaddr
string
,
amount
int64
)
error
{
receipt
,
err
:=
wasmCB
.
GetCoinsAccount
()
.
TransferWithdraw
(
addr
,
execaddr
,
amount
)
if
err
!=
nil
{
return
err
}
wasmCB
.
receiptLogs
=
append
(
wasmCB
.
receiptLogs
,
receipt
.
Logs
...
)
return
nil
}
func
execFrozen
(
addr
string
,
amount
int64
)
error
{
func
execFrozen
(
addr
string
,
amount
int64
)
error
{
receipt
,
err
:=
wasmCB
.
GetCoinsAccount
()
.
ExecFrozen
(
addr
,
wasmCB
.
execAddr
,
amount
)
receipt
,
err
:=
wasmCB
.
GetCoinsAccount
()
.
ExecFrozen
(
addr
,
wasmCB
.
execAddr
,
amount
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -88,6 +131,10 @@ func execTransferFrozen(from, to string, amount int64) error {
...
@@ -88,6 +131,10 @@ func execTransferFrozen(from, to string, amount int64) error {
return
nil
return
nil
}
}
func
execAddress
(
name
string
)
string
{
return
address
.
ExecAddress
(
name
)
}
func
getFrom
()
string
{
func
getFrom
()
string
{
return
wasmCB
.
tx
.
From
()
return
wasmCB
.
tx
.
From
()
}
}
...
@@ -119,3 +166,7 @@ func getRandom() int64 {
...
@@ -119,3 +166,7 @@ func getRandom() int64 {
func
printlog
(
s
string
)
{
func
printlog
(
s
string
)
{
wasmCB
.
logs
=
append
(
wasmCB
.
logs
,
s
)
wasmCB
.
logs
=
append
(
wasmCB
.
logs
,
s
)
}
}
func
sha256
(
data
[]
byte
)
[]
byte
{
return
common
.
Sha256
(
data
)
}
plugin/dapp/wasm/executor/resolver.go
View file @
4e9550f8
...
@@ -98,6 +98,96 @@ func (r *Resolver) ResolveFunc(module, field string) exec.FunctionImport {
...
@@ -98,6 +98,96 @@ func (r *Resolver) ResolveFunc(module, field string) exec.FunctionImport {
return
int64
(
valueLen
)
return
int64
(
valueLen
)
}
}
case
"getBalance"
:
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
])
execPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
2
]))
execLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
3
]))
exec
:=
string
(
vm
.
Memory
[
execPtr
:
execPtr
+
execLen
])
balance
,
_
,
err
:=
getBalance
(
addr
,
exec
)
if
err
!=
nil
{
return
-
1
}
return
balance
}
case
"getFrozen"
:
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
])
execPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
2
]))
execLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
3
]))
exec
:=
string
(
vm
.
Memory
[
execPtr
:
execPtr
+
execLen
])
_
,
frozen
,
err
:=
getBalance
(
addr
,
exec
)
if
err
!=
nil
{
return
-
1
}
return
frozen
}
case
"transfer"
:
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
:=
transfer
(
fromAddr
,
toAddr
,
amount
)
if
err
!=
nil
{
return
-
1
}
return
0
}
case
"transferToExec"
:
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
:=
transferToExec
(
fromAddr
,
toAddr
,
amount
)
if
err
!=
nil
{
return
-
1
}
return
0
}
case
"transferWithdraw"
:
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
:=
transferWithdraw
(
fromAddr
,
toAddr
,
amount
)
if
err
!=
nil
{
return
-
1
}
return
0
}
case
"execAddress"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
namePtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
nameLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
1
]))
name
:=
string
(
vm
.
Memory
[
namePtr
:
namePtr
+
nameLen
])
addr
:=
[]
byte
(
execAddress
(
name
))
addrPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
2
]))
addrLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
3
]))
copy
(
vm
.
Memory
[
addrPtr
:
addrPtr
+
addrLen
],
addr
)
return
0
}
case
"execFrozen"
:
case
"execFrozen"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
addrPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
addrPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
...
@@ -164,6 +254,7 @@ func (r *Resolver) ResolveFunc(module, field string) exec.FunctionImport {
...
@@ -164,6 +254,7 @@ func (r *Resolver) ResolveFunc(module, field string) exec.FunctionImport {
copy
(
vm
.
Memory
[
fromPtr
:
fromPtr
+
fromLen
],
fromAddr
)
copy
(
vm
.
Memory
[
fromPtr
:
fromPtr
+
fromLen
],
fromAddr
)
return
0
return
0
}
}
case
"getHeight"
:
case
"getHeight"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
return
getHeight
()
}
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
return
getHeight
()
}
...
@@ -186,6 +277,17 @@ func (r *Resolver) ResolveFunc(module, field string) exec.FunctionImport {
...
@@ -186,6 +277,17 @@ func (r *Resolver) ResolveFunc(module, field string) exec.FunctionImport {
return
0
return
0
}
}
case
"sha256"
:
return
func
(
vm
*
exec
.
VirtualMachine
)
int64
{
dataPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
0
]))
dataLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
1
]))
data
:=
vm
.
Memory
[
dataPtr
:
dataPtr
+
dataLen
]
sumPtr
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
2
]))
sumLen
:=
int
(
uint32
(
vm
.
GetCurrentFrame
()
.
Locals
[
3
]))
copy
(
vm
.
Memory
[
sumPtr
:
sumPtr
+
sumLen
],
sha256
(
data
))
return
0
}
default
:
default
:
log
.
Error
(
"ResolveFunc"
,
"unknown field"
,
field
)
log
.
Error
(
"ResolveFunc"
,
"unknown field"
,
field
)
}
}
...
...
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