diff --git a/core/compatibility.go b/core/compatibility.go index 94661a80b..408967cc7 100644 --- a/core/compatibility.go +++ b/core/compatibility.go @@ -58,7 +58,7 @@ var ( "assert.js": {"1.0.0"}, "instruction_counter.js": {"1.0.0"}, "typescriptServices.js": {"1.0.0"}, - "blockchain.js": {"1.0.0"}, + "blockchain.js": {"1.0.0", "1.0.5"}, "console.js": {"1.0.0"}, "event.js": {"1.0.0"}, "storage.js": {"1.0.0"}, diff --git a/nf/nvm/crypto.go b/nf/nvm/crypto.go index db5a64a54..808533f57 100644 --- a/nf/nvm/crypto.go +++ b/nf/nvm/crypto.go @@ -33,7 +33,7 @@ import ( //export Sha256Func func Sha256Func(data *C.char, gasCnt *C.size_t) *C.char { s := C.GoString(data) - *gasCnt = C.size_t(1000 + len(s)) + *gasCnt = C.size_t(len(s) * 100) r := hash.Sha256([]byte(s)) return C.CString(byteutils.Hex(r)) @@ -43,7 +43,7 @@ func Sha256Func(data *C.char, gasCnt *C.size_t) *C.char { //export Sha3256Func func Sha3256Func(data *C.char, gasCnt *C.size_t) *C.char { s := C.GoString(data) - *gasCnt = C.size_t(1000 + len(s)) + *gasCnt = C.size_t(len(s) * 100) r := hash.Sha3256([]byte(s)) return C.CString(byteutils.Hex(r)) @@ -53,7 +53,7 @@ func Sha3256Func(data *C.char, gasCnt *C.size_t) *C.char { //export Ripemd160Func func Ripemd160Func(data *C.char, gasCnt *C.size_t) *C.char { s := C.GoString(data) - *gasCnt = C.size_t(1000 + len(s)) + *gasCnt = C.size_t(len(s) * 100) r := hash.Ripemd160([]byte(s)) return C.CString(byteutils.Hex(r)) @@ -65,7 +65,7 @@ func RecoverAddressFunc(alg int, data, sign *C.char, gasCnt *C.size_t) *C.char { d := C.GoString(data) s := C.GoString(sign) - *gasCnt = C.size_t(0) + *gasCnt = C.size_t(1000) plain, err := byteutils.FromHex(d) if err != nil { diff --git a/nf/nvm/engine_v8_test.go b/nf/nvm/engine_v8_test.go index 96ae17e58..35786218e 100644 --- a/nf/nvm/engine_v8_test.go +++ b/nf/nvm/engine_v8_test.go @@ -1760,6 +1760,7 @@ func TestMultiLibVersion(t *testing.T) { {"test/test_uint.js", nil, "\"\""}, {"test/test_date_1.0.5.js", nil, "\"\""}, {"test/test_crypto.js", nil, "\"\""}, + {"test/test_blockchain_1.0.5.js", nil, "\"\""}, } for _, tt := range tests { diff --git a/nf/nvm/test/test_blockchain_1.0.5.js b/nf/nvm/test/test_blockchain_1.0.5.js new file mode 100644 index 000000000..a68598cd7 --- /dev/null +++ b/nf/nvm/test/test_blockchain_1.0.5.js @@ -0,0 +1,51 @@ +// Copyright (C) 2018 go-nebulas authors +// +// This file is part of the go-nebulas library. +// +// the go-nebulas library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// the go-nebulas library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with the go-nebulas library. If not, see . +// + +'use strict'; + +if (typeof _native_blockchain === "undefined") { + throw new Error("_native_blockchain is undefined."); +} + +var ok = Blockchain.transfer("n1FF1nz6tarkDVwWQkMnnwFPuPKUaQTdptE", "1"); +console.log("transfer:" + ok) + +var result = Blockchain.verifyAddress("n1FF1nz6tarkDVwWQkMnnwFPuPKUaQTdptE"); +console.log("verifyAddress:" + result) + +try { + Blockchain.transfer("n1FF1nz6tarkDVwWQkMnnwFPuPKUaQTdptE", -1); +} catch (err) { + if (err.message !== "invalid value" ) { + throw err; + } +} +try { + Blockchain.transfer("n1FF1nz6tarkDVwWQkMnnwFPuPKUaQTdptE", NaN); +} catch (err) { + if (err.message !== "invalid value" ) { + throw err; + } +} +try { + Blockchain.transfer("n1FF1nz6tarkDVwWQkMnnwFPuPKUaQTdptE", Infinity); +} catch (err) { + if (err.message !== "invalid value" ) { + throw err; + } +} \ No newline at end of file diff --git a/nf/nvm/v8/libjs/1.0.5/blockchain.js b/nf/nvm/v8/libjs/1.0.5/blockchain.js new file mode 100644 index 000000000..a00f47dc2 --- /dev/null +++ b/nf/nvm/v8/libjs/1.0.5/blockchain.js @@ -0,0 +1,82 @@ +// Copyright (C) 2017 go-nebulas authors +// +// This file is part of the go-nebulas library. +// +// the go-nebulas library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// the go-nebulas library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with the go-nebulas library. If not, see . +// + +'use strict'; + +var Blockchain = function () { + Object.defineProperty(this, "nativeBlockchain", { + configurable: false, + enumerable: false, + get: function(){ + return _native_blockchain; + } + }); +}; + +Blockchain.prototype = { + AccountAddress: 0x57, + ContractAddress: 0x58, + + blockParse: function (str) { + var block = JSON.parse(str); + if (block != null) { + var fb = Object.freeze(block); + Object.defineProperty(this, "block", { + configurable: false, + enumerable: false, + get: function(){ + return fb; + } + }); + } + }, + transactionParse: function (str) { + var tx = JSON.parse(str); + if (tx != null) { + var value = tx.value === undefined || tx.value.length === 0 ? "0" : tx.value; + tx.value = new BigNumber(value); + var gasPrice = tx.gasPrice === undefined || tx.gasPrice.length === 0 ? "0" : tx.gasPrice; + tx.gasPrice = new BigNumber(gasPrice); + var gasLimit = tx.gasLimit === undefined || tx.gasLimit.length === 0 ? "0" : tx.gasLimit; + tx.gasLimit = new BigNumber(gasLimit); + + var ft = Object.freeze(tx); + Object.defineProperty(this, "transaction", { + configurable: false, + enumerable: false, + get: function(){ + return ft; + } + }); + } + }, + transfer: function (address, value) { + if (!(value instanceof BigNumber)) { + value = new BigNumber(value); + } + if (value.isNaN() || value.isNegative() || !value.isFinite()) { + throw new Error("invalid value"); + } + var ret = this.nativeBlockchain.transfer(address, value.toString(10)); + return ret == 0; + }, + verifyAddress: function (address) { + return this.nativeBlockchain.verifyAddress(address); + } +}; +module.exports = new Blockchain();