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
7c71130e
Commit
7c71130e
authored
Dec 31, 2018
by
vipwzw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add more test
parent
38938245
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
63 additions
and
14 deletions
+63
-14
const.go
plugin/dapp/js/executor/const.go
+11
-6
exec.go
plugin/dapp/js/executor/exec.go
+1
-1
exec_del_local.go
plugin/dapp/js/executor/exec_del_local.go
+5
-2
exec_local.go
plugin/dapp/js/executor/exec_local.go
+2
-1
jsvm_test.go
plugin/dapp/js/executor/jsvm_test.go
+44
-4
No files found.
plugin/dapp/js/executor/const.go
View file @
7c71130e
...
@@ -25,9 +25,11 @@ function kvcreator(dbtype) {
...
@@ -25,9 +25,11 @@ function kvcreator(dbtype) {
}
}
kvcreator.prototype.add = function(k, v) {
kvcreator.prototype.add = function(k, v) {
var data = JSON.stringify(v)
if (typeof v != "string") {
this.data[k] = data
v = JSON.stringify(v)
this.kvs.push({key:k, value: data})
}
this.data[k] = v
this.kvs.push({key:k, value: v})
}
}
kvcreator.prototype.get = function(k) {
kvcreator.prototype.get = function(k) {
...
@@ -58,8 +60,11 @@ kvcreator.prototype.listvalue = function(prefix, key, count, direction) {
...
@@ -58,8 +60,11 @@ kvcreator.prototype.listvalue = function(prefix, key, count, direction) {
kvcreator.prototype.addlog = function(log) {
kvcreator.prototype.addlog = function(log) {
if (this.list) {
if (this.list) {
throw new Error("local or dellocal can't set log")
throw new Error("local or dellocal can't set log")
}
}
this.logs.push(JSON.stringify(log))
if (typeof v != "string") {
log = JSON.stringify(log)
}
this.logs.push(log)
}
}
kvcreator.prototype.receipt = function() {
kvcreator.prototype.receipt = function() {
...
@@ -68,7 +73,7 @@ kvcreator.prototype.receipt = function() {
...
@@ -68,7 +73,7 @@ kvcreator.prototype.receipt = function() {
function callcode(context, f, args, loglist) {
function callcode(context, f, args, loglist) {
if (f == "init") {
if (f == "init") {
return Init(
context
)
return Init(
JSON.parse(context)
)
}
}
var farr = f.split("_", 2)
var farr = f.split("_", 2)
if (farr.length != 2) {
if (farr.length != 2) {
...
...
plugin/dapp/js/executor/exec.go
View file @
7c71130e
...
@@ -33,7 +33,7 @@ func (c *js) Exec_Create(payload *jsproto.Create, tx *types.Transaction, index i
...
@@ -33,7 +33,7 @@ func (c *js) Exec_Create(payload *jsproto.Create, tx *types.Transaction, index i
func
(
c
*
js
)
Exec_Call
(
payload
*
jsproto
.
Call
,
tx
*
types
.
Transaction
,
index
int
)
(
*
types
.
Receipt
,
error
)
{
func
(
c
*
js
)
Exec_Call
(
payload
*
jsproto
.
Call
,
tx
*
types
.
Transaction
,
index
int
)
(
*
types
.
Receipt
,
error
)
{
execer
:=
types
.
ExecName
(
"user.js."
+
payload
.
Name
)
execer
:=
types
.
ExecName
(
"user.js."
+
payload
.
Name
)
kvc
:=
dapp
.
NewKVCreator
(
c
.
GetStateDB
(),
[]
byte
(
execer
),
nil
)
kvc
:=
dapp
.
NewKVCreator
(
c
.
GetStateDB
(),
calcStatePrefix
([]
byte
(
execer
)
),
nil
)
jsvalue
,
err
:=
c
.
callVM
(
"exec"
,
payload
,
tx
,
index
,
nil
)
jsvalue
,
err
:=
c
.
callVM
(
"exec"
,
payload
,
tx
,
index
,
nil
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
...
plugin/dapp/js/executor/exec_del_local.go
View file @
7c71130e
...
@@ -12,12 +12,15 @@ func (c *js) ExecDelLocal_Create(payload *jsproto.Create, tx *types.Transaction,
...
@@ -12,12 +12,15 @@ func (c *js) ExecDelLocal_Create(payload *jsproto.Create, tx *types.Transaction,
func
(
c
*
js
)
ExecDelLocal_Call
(
payload
*
jsproto
.
Call
,
tx
*
types
.
Transaction
,
receiptData
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
func
(
c
*
js
)
ExecDelLocal_Call
(
payload
*
jsproto
.
Call
,
tx
*
types
.
Transaction
,
receiptData
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
krollback
:=
calcRollbackKey
(
tx
.
Hash
())
krollback
:=
calcRollbackKey
(
tx
.
Hash
())
kvc
:=
dapp
.
NewKVCreator
(
c
.
GetLocalDB
(),
calcLocalPrefix
(
tx
.
Execer
),
krollback
)
execer
:=
types
.
ExecName
(
"user.js."
+
payload
.
Name
)
kvc
:=
dapp
.
NewKVCreator
(
c
.
GetLocalDB
(),
calcLocalPrefix
([]
byte
(
execer
)),
krollback
)
kvs
,
err
:=
kvc
.
GetRollbackKVList
()
kvs
,
err
:=
kvc
.
GetRollbackKVList
()
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
kvc
.
AddKVListOnly
(
kvs
)
for
_
,
kv
:=
range
kvs
{
kvc
.
AddNoPrefix
(
kv
.
Key
,
kv
.
Value
)
}
kvc
.
DelRollbackKV
()
kvc
.
DelRollbackKV
()
r
:=
&
types
.
LocalDBSet
{}
r
:=
&
types
.
LocalDBSet
{}
r
.
KV
=
kvc
.
KVList
()
r
.
KV
=
kvc
.
KVList
()
...
...
plugin/dapp/js/executor/exec_local.go
View file @
7c71130e
...
@@ -12,7 +12,8 @@ func (c *js) ExecLocal_Create(payload *jsproto.Create, tx *types.Transaction, re
...
@@ -12,7 +12,8 @@ func (c *js) ExecLocal_Create(payload *jsproto.Create, tx *types.Transaction, re
func
(
c
*
js
)
ExecLocal_Call
(
payload
*
jsproto
.
Call
,
tx
*
types
.
Transaction
,
receiptData
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
func
(
c
*
js
)
ExecLocal_Call
(
payload
*
jsproto
.
Call
,
tx
*
types
.
Transaction
,
receiptData
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
k
:=
calcRollbackKey
(
tx
.
Hash
())
k
:=
calcRollbackKey
(
tx
.
Hash
())
kvc
:=
dapp
.
NewKVCreator
(
c
.
GetLocalDB
(),
calcLocalPrefix
(
tx
.
Execer
),
k
)
execer
:=
types
.
ExecName
(
"user.js."
+
payload
.
Name
)
kvc
:=
dapp
.
NewKVCreator
(
c
.
GetLocalDB
(),
calcLocalPrefix
([]
byte
(
execer
)),
k
)
jsvalue
,
err
:=
c
.
callVM
(
"execlocal"
,
payload
,
tx
,
index
,
receiptData
)
jsvalue
,
err
:=
c
.
callVM
(
"execlocal"
,
payload
,
tx
,
index
,
receiptData
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
...
plugin/dapp/js/executor/jsvm_test.go
View file @
7c71130e
package
executor
package
executor
import
(
import
(
"encoding/json"
"strings"
"strings"
"testing"
"testing"
"time"
"time"
...
@@ -40,14 +41,14 @@ Exec.prototype.hello = function(args) {
...
@@ -40,14 +41,14 @@ Exec.prototype.hello = function(args) {
this.kvc.add("args", args)
this.kvc.add("args", args)
this.kvc.add("action", "exec")
this.kvc.add("action", "exec")
this.kvc.add("context", this.context)
this.kvc.add("context", this.context)
this.kvc.addlog(
'{"key1": "value1"}'
)
this.kvc.addlog(
{"key1": "value1"}
)
this.kvc.addlog(
'{"key2": "value2"}'
)
this.kvc.addlog(
{"key2": "value2"}
)
return this.kvc.receipt()
return this.kvc.receipt()
}
}
ExecLocal.prototype.hello = function(args) {
ExecLocal.prototype.hello = function(args) {
this.kvc.add("args", args)
this.kvc.add("args", args)
this.kvc.add("action", "exec")
this.kvc.add("action", "exec
local
")
this.kvc.add("log", this.logs)
this.kvc.add("log", this.logs)
this.kvc.add("context", this.context)
this.kvc.add("context", this.context)
return this.kvc.receipt()
return this.kvc.receipt()
...
@@ -93,7 +94,39 @@ func TestCallcode(t *testing.T) {
...
@@ -93,7 +94,39 @@ func TestCallcode(t *testing.T) {
receipt
,
err
:=
e
.
Exec_Call
(
call
,
tx
,
0
)
receipt
,
err
:=
e
.
Exec_Call
(
call
,
tx
,
0
)
assert
.
Nil
(
t
,
err
)
assert
.
Nil
(
t
,
err
)
util
.
SaveKVList
(
ldb
,
receipt
.
KV
)
util
.
SaveKVList
(
ldb
,
receipt
.
KV
)
util
.
PrintKV
(
receipt
.
KV
)
assert
.
Equal
(
t
,
string
(
receipt
.
KV
[
0
]
.
Value
),
`{"hello":"world"}`
)
assert
.
Equal
(
t
,
string
(
receipt
.
KV
[
1
]
.
Value
),
"exec"
)
var
data
blockContext
err
=
json
.
Unmarshal
(
receipt
.
KV
[
2
]
.
Value
,
&
data
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
uint64
(
1
),
data
.
Difficulty
)
assert
.
Equal
(
t
,
"js"
,
data
.
DriverName
)
assert
.
Equal
(
t
,
int64
(
1
),
data
.
Height
)
assert
.
Equal
(
t
,
int64
(
0
),
data
.
Index
)
kvset
,
err
:=
e
.
ExecLocal_Call
(
call
,
tx
,
&
types
.
ReceiptData
{
Logs
:
receipt
.
Logs
},
0
)
assert
.
Nil
(
t
,
err
)
util
.
SaveKVList
(
ldb
,
kvset
.
KV
)
assert
.
Equal
(
t
,
string
(
kvset
.
KV
[
0
]
.
Value
),
`{"hello":"world"}`
)
assert
.
Equal
(
t
,
string
(
kvset
.
KV
[
1
]
.
Value
),
"execlocal"
)
//test log is ok
assert
.
Equal
(
t
,
string
(
kvset
.
KV
[
2
]
.
Value
),
`[{"key1":"value1"},{"key2":"value2"}]`
)
//test context
err
=
json
.
Unmarshal
(
kvset
.
KV
[
3
]
.
Value
,
&
data
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
uint64
(
1
),
data
.
Difficulty
)
assert
.
Equal
(
t
,
"js"
,
data
.
DriverName
)
assert
.
Equal
(
t
,
int64
(
1
),
data
.
Height
)
assert
.
Equal
(
t
,
int64
(
0
),
data
.
Index
)
//call rollback
kvset
,
err
=
e
.
ExecDelLocal_Call
(
call
,
tx
,
&
types
.
ReceiptData
{
Logs
:
receipt
.
Logs
},
0
)
assert
.
Nil
(
t
,
err
)
util
.
SaveKVList
(
ldb
,
kvset
.
KV
)
assert
.
Equal
(
t
,
5
,
len
(
kvset
.
KV
))
for
i
:=
0
;
i
<
len
(
kvset
.
KV
);
i
++
{
assert
.
Equal
(
t
,
kvset
.
KV
[
i
]
.
Value
,
[]
byte
(
nil
))
}
}
}
func
TestCallError
(
t
*
testing
.
T
)
{
func
TestCallError
(
t
*
testing
.
T
)
{
...
@@ -119,3 +152,10 @@ func TestCallError(t *testing.T) {
...
@@ -119,3 +152,10 @@ func TestCallError(t *testing.T) {
assert
.
Equal
(
t
,
true
,
ok
)
assert
.
Equal
(
t
,
true
,
ok
)
assert
.
Equal
(
t
,
true
,
strings
.
Contains
(
err
.
Error
(),
errFuncNotFound
.
Error
()))
assert
.
Equal
(
t
,
true
,
strings
.
Contains
(
err
.
Error
(),
errFuncNotFound
.
Error
()))
}
}
func
TestcalcLocalPrefix
(
t
*
testing
.
T
)
{
assert
.
Equal
(
t
,
calcLocalPrefix
([]
byte
(
"a"
)),
[]
byte
(
"LODB-a-"
))
assert
.
Equal
(
t
,
calcStatePrefix
([]
byte
(
"a"
)),
[]
byte
(
"mavl-a-"
))
assert
.
Equal
(
t
,
calcCodeKey
(
"a"
),
[]
byte
(
"mavl-js-code-a"
))
assert
.
Equal
(
t
,
calcRollbackKey
([]
byte
(
"a"
)),
[]
byte
(
"mavl-js-rollback-a"
))
}
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