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
1569ff03
Commit
1569ff03
authored
Jan 02, 2019
by
vipwzw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化runtime cache 部分结构
parent
4c05a1f1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
11 deletions
+29
-11
js.go
plugin/dapp/js/executor/js.go
+25
-10
jsvm_test.go
plugin/dapp/js/executor/jsvm_test.go
+4
-1
No files found.
plugin/dapp/js/executor/js.go
View file @
1569ff03
...
...
@@ -8,11 +8,13 @@ import (
"github.com/33cn/chain33/types"
ptypes
"github.com/33cn/plugin/plugin/dapp/js/types"
"github.com/33cn/plugin/plugin/dapp/js/types/jsproto"
lru
"github.com/hashicorp/golang-lru"
"github.com/robertkrimen/otto"
)
var
driverName
=
ptypes
.
JsX
var
basevm
*
otto
.
Otto
var
codecache
*
lru
.
Cache
func
init
()
{
ety
:=
types
.
LoadExecutorType
(
driverName
)
...
...
@@ -26,6 +28,12 @@ func init() {
//Init 插件初始化
func
Init
(
name
string
,
sub
[]
byte
)
{
//最新的64个code做cache
var
err
error
codecache
,
err
=
lru
.
New
(
512
)
if
err
!=
nil
{
panic
(
err
)
}
drivers
.
Register
(
GetName
(),
newjs
,
0
)
}
...
...
@@ -53,10 +61,6 @@ func (u *js) GetDriverName() string {
func
(
u
*
js
)
callVM
(
prefix
string
,
payload
*
jsproto
.
Call
,
tx
*
types
.
Transaction
,
index
int
,
receiptData
*
types
.
ReceiptData
)
(
*
otto
.
Object
,
error
)
{
vm
,
err
:=
u
.
createVM
(
payload
.
Name
,
tx
,
index
)
if
err
!=
nil
{
return
nil
,
err
}
if
payload
.
Args
!=
""
{
newjson
,
err
:=
rewriteJSON
([]
byte
(
payload
.
Args
))
if
err
!=
nil
{
...
...
@@ -66,17 +70,15 @@ func (u *js) callVM(prefix string, payload *jsproto.Call, tx *types.Transaction,
}
else
{
payload
.
Args
=
"{}"
}
db
:=
u
.
GetStateDB
()
code
,
err
:=
db
.
Get
(
calcCodeKey
(
payload
.
Name
))
loglist
,
err
:=
jslogs
(
receiptData
)
if
err
!=
nil
{
return
nil
,
err
}
loglist
,
err
:=
jslogs
(
receiptData
)
vm
,
err
:=
u
.
createVM
(
payload
.
Name
,
tx
,
index
)
if
err
!=
nil
{
return
nil
,
err
}
vm
.
Set
(
"loglist"
,
loglist
)
vm
.
Set
(
"code"
,
code
)
if
prefix
==
"init"
{
vm
.
Set
(
"f"
,
"init"
)
}
else
{
...
...
@@ -84,7 +86,7 @@ func (u *js) callVM(prefix string, payload *jsproto.Call, tx *types.Transaction,
}
vm
.
Set
(
"args"
,
payload
.
Args
)
callfunc
:=
"callcode(context, f, args, loglist)"
jsvalue
,
err
:=
vm
.
Run
(
string
(
code
)
+
"
\n
"
+
callfunc
)
jsvalue
,
err
:=
vm
.
Run
(
callfunc
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -201,7 +203,20 @@ func (u *js) createVM(name string, tx *types.Transaction, index int) (*otto.Otto
if
err
!=
nil
{
return
nil
,
err
}
vm
:=
basevm
.
Copy
()
var
vm
*
otto
.
Otto
if
vmitem
,
ok
:=
codecache
.
Get
(
name
);
ok
{
vm
=
vmitem
.
(
*
otto
.
Otto
)
.
Copy
()
}
else
{
code
,
err
:=
u
.
GetStateDB
()
.
Get
(
calcCodeKey
(
name
))
if
err
!=
nil
{
return
nil
,
err
}
//cache 合约代码部分,不会cache 具体执行
cachevm
:=
basevm
.
Copy
()
cachevm
.
Run
(
code
)
codecache
.
Add
(
name
,
cachevm
)
vm
=
cachevm
.
Copy
()
}
vm
.
Set
(
"context"
,
string
(
data
))
u
.
getstatedbFunc
(
vm
,
name
)
u
.
getlocaldbFunc
(
vm
,
name
)
...
...
plugin/dapp/js/executor/jsvm_test.go
View file @
1569ff03
...
...
@@ -69,12 +69,15 @@ Query.prototype.hello = function(args) {
}
`
func
init
()
{
Init
(
"js"
,
nil
)
}
func
initExec
(
ldb
db
.
DB
,
kvdb
db
.
KVDB
,
t
assert
.
TestingT
)
*
js
{
e
:=
newjs
()
.
(
*
js
)
e
.
SetEnv
(
1
,
time
.
Now
()
.
Unix
(),
1
)
e
.
SetLocalDB
(
kvdb
)
e
.
SetStateDB
(
kvdb
)
c
,
tx
:=
createCodeTx
(
"test"
,
jscode
)
receipt
,
err
:=
e
.
Exec_Create
(
c
,
tx
,
0
)
assert
.
Nil
(
t
,
err
)
...
...
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