Commit 7c71130e authored by vipwzw's avatar vipwzw

add more test

parent 38938245
...@@ -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) {
......
...@@ -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
......
...@@ -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()
......
...@@ -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
......
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", "execlocal")
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"))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment