Commit 04454590 authored by vipwzw's avatar vipwzw

support big number

parent 8a87b8ec
...@@ -51,6 +51,15 @@ func (u *js) callVM(prefix string, payload *jsproto.Call, tx *types.Transaction, ...@@ -51,6 +51,15 @@ func (u *js) callVM(prefix string, payload *jsproto.Call, tx *types.Transaction,
if err != nil { if err != nil {
return nil, err return nil, err
} }
if payload.Args != "" {
newjson, err := rewriteJSON([]byte(payload.Args))
if err != nil {
return nil, err
}
payload.Args = string(newjson)
} else {
payload.Args = "{}"
}
db := u.GetStateDB() db := u.GetStateDB()
code, err := db.Get(calcCodeKey(payload.Name)) code, err := db.Get(calcCodeKey(payload.Name))
if err != nil { if err != nil {
......
package executor package executor
import ( import (
"bytes"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"strings"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
ptypes "github.com/33cn/plugin/plugin/dapp/js/types" ptypes "github.com/33cn/plugin/plugin/dapp/js/types"
...@@ -107,3 +110,50 @@ func parseKV(data *otto.Object) (kv *types.KeyValue, err error) { ...@@ -107,3 +110,50 @@ func parseKV(data *otto.Object) (kv *types.KeyValue, err error) {
} }
return &types.KeyValue{Key: []byte(key), Value: []byte(value)}, nil return &types.KeyValue{Key: []byte(key), Value: []byte(value)}, nil
} }
func rewriteJSON(data []byte) ([]byte, error) {
dat := make(map[string]interface{})
d := json.NewDecoder(bytes.NewBuffer(data))
d.UseNumber()
if err := d.Decode(&dat); err != nil {
return nil, err
}
dat = rewriteString(dat)
return json.Marshal(dat)
}
func rewriteString(dat map[string]interface{}) map[string]interface{} {
for k, v := range dat {
if n, ok := v.(json.Number); ok {
dat[k] = jssafe(n)
} else if arr, ok := v.([]interface{}); ok {
for i := 0; i < len(arr); i++ {
v := arr[i]
if n, ok := v.(json.Number); ok {
arr[i] = jssafe(n)
}
}
dat[k] = arr
} else if d, ok := v.(map[string]interface{}); ok {
dat[k] = rewriteString(d)
} else {
dat[k] = v
}
}
return dat
}
func jssafe(n json.Number) interface{} {
if strings.Contains(string(n), ".") { //float
return n
}
i, err := n.Int64()
if err != nil {
return n
}
//javascript can not parse
if i >= 9007199254740991 || i <= -9007199254740991 {
return string(n)
}
return n
}
...@@ -2,6 +2,8 @@ package executor ...@@ -2,6 +2,8 @@ package executor
import ( import (
"encoding/json" "encoding/json"
"fmt"
"math"
"strings" "strings"
"testing" "testing"
"time" "time"
...@@ -158,8 +160,8 @@ func TestCallError(t *testing.T) { ...@@ -158,8 +160,8 @@ func TestCallError(t *testing.T) {
call, tx := callCodeTx("test", "hello", `{hello":"world"}`) call, tx := callCodeTx("test", "hello", `{hello":"world"}`)
_, err := e.callVM("exec", call, tx, 0, nil) _, err := e.callVM("exec", call, tx, 0, nil)
_, ok := err.(*otto.Error) _, ok := err.(*otto.Error)
assert.Equal(t, true, ok) assert.Equal(t, false, ok)
assert.Equal(t, true, strings.Contains(err.Error(), "SyntaxError")) assert.Equal(t, true, strings.Contains(err.Error(), "invalid character 'h'"))
call, tx = callCodeTx("test", "hello", `{"hello":"world"}`) call, tx = callCodeTx("test", "hello", `{"hello":"world"}`)
_, err = e.callVM("hello", call, tx, 0, nil) _, err = e.callVM("hello", call, tx, 0, nil)
...@@ -174,6 +176,37 @@ func TestCallError(t *testing.T) { ...@@ -174,6 +176,37 @@ func TestCallError(t *testing.T) {
assert.Equal(t, true, strings.Contains(err.Error(), ptypes.ErrFuncNotFound.Error())) assert.Equal(t, true, strings.Contains(err.Error(), ptypes.ErrFuncNotFound.Error()))
} }
//数字非常大的数字的处理
func TestBigInt(t *testing.T) {
dir, ldb, kvdb := util.CreateTestDB()
defer util.CloseTestDB(dir, ldb)
e := initExec(ldb, kvdb, t)
//test call error(invalid json input)
s := fmt.Sprintf(`{"balance":%d,"balance1":%d,"balance2":%d,"balance3":%d}`, math.MaxInt64, math.MinInt64, 9007199254740990, -9007199254740990)
call, tx := callCodeTx("test", "hello", s)
data, err := e.callVM("exec", call, tx, 0, nil)
kvs, _, err := parseJsReturn(data)
assert.Nil(t, err)
assert.Equal(t, `{"balance":"9223372036854775807","balance1":"-9223372036854775808","balance2":9007199254740990,"balance3":-9007199254740990}`, string(kvs[0].Value))
}
func TestRewriteJSON(t *testing.T) {
s := fmt.Sprintf(`{"balance":%d,"balance1":%d,"balance2":%d,"balance3":%d}`, math.MaxInt64, math.MinInt64, 9007199254740990, -9007199254740990)
quota := fmt.Sprintf(`{"balance":"%d","balance1":"%d","balance2":%d,"balance3":%d}`, math.MaxInt64, math.MinInt64, 9007199254740990, -9007199254740990)
data, err := rewriteJSON([]byte(s))
assert.Nil(t, err)
assert.Equal(t, quota, string(data))
data2 := make(map[string]interface{})
data2["ints"] = []int64{math.MaxInt64, math.MinInt64, 9007199254740990, -9007199254740990, 1, 0}
data2["float"] = []float64{1.1, 1000000000000000000000000000, 10000000000000000}
json1, err := json.Marshal(data2)
assert.Nil(t, err)
//assert.Equal(t, `{"float":[1.1,1100000000000000000000,-1100000000000000000000],"ints":[9223372036854775807,-9223372036854775808,9007199254740990,-9007199254740990,1,0]}`, string(json1))
json2, err := rewriteJSON(json1)
assert.Nil(t, err)
assert.Equal(t, string(json2), `{"float":[1.1,1e+27,"10000000000000000"],"ints":["9223372036854775807","-9223372036854775808",9007199254740990,-9007199254740990,1,0]}`)
}
func TestCalcLocalPrefix(t *testing.T) { func TestCalcLocalPrefix(t *testing.T) {
assert.Equal(t, calcLocalPrefix([]byte("a")), []byte("LODB-a-")) assert.Equal(t, calcLocalPrefix([]byte("a")), []byte("LODB-a-"))
assert.Equal(t, calcStatePrefix([]byte("a")), []byte("mavl-a-")) assert.Equal(t, calcStatePrefix([]byte("a")), []byte("mavl-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