Commit 7c71130e authored by vipwzw's avatar vipwzw

add more test

parent 38938245
......@@ -25,9 +25,11 @@ function kvcreator(dbtype) {
}
kvcreator.prototype.add = function(k, v) {
var data = JSON.stringify(v)
this.data[k] = data
this.kvs.push({key:k, value: data})
if (typeof v != "string") {
v = JSON.stringify(v)
}
this.data[k] = v
this.kvs.push({key:k, value: v})
}
kvcreator.prototype.get = function(k) {
......@@ -58,8 +60,11 @@ kvcreator.prototype.listvalue = function(prefix, key, count, direction) {
kvcreator.prototype.addlog = function(log) {
if (this.list) {
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() {
......@@ -68,7 +73,7 @@ kvcreator.prototype.receipt = function() {
function callcode(context, f, args, loglist) {
if (f == "init") {
return Init(context)
return Init(JSON.parse(context))
}
var farr = f.split("_", 2)
if (farr.length != 2) {
......
......@@ -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) {
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)
if err != nil {
return nil, err
......
......@@ -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) {
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()
if err != nil {
return nil, err
}
kvc.AddKVListOnly(kvs)
for _, kv := range kvs {
kvc.AddNoPrefix(kv.Key, kv.Value)
}
kvc.DelRollbackKV()
r := &types.LocalDBSet{}
r.KV = kvc.KVList()
......
......@@ -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) {
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)
if err != nil {
return nil, err
......
package executor
import (
"encoding/json"
"strings"
"testing"
"time"
......@@ -40,14 +41,14 @@ Exec.prototype.hello = function(args) {
this.kvc.add("args", args)
this.kvc.add("action", "exec")
this.kvc.add("context", this.context)
this.kvc.addlog('{"key1": "value1"}')
this.kvc.addlog('{"key2": "value2"}')
this.kvc.addlog({"key1": "value1"})
this.kvc.addlog({"key2": "value2"})
return this.kvc.receipt()
}
ExecLocal.prototype.hello = function(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("context", this.context)
return this.kvc.receipt()
......@@ -93,7 +94,39 @@ func TestCallcode(t *testing.T) {
receipt, err := e.Exec_Call(call, tx, 0)
assert.Nil(t, err)
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) {
......@@ -119,3 +152,10 @@ func TestCallError(t *testing.T) {
assert.Equal(t, true, ok)
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