Skip to content

Commit

Permalink
v8 js lib upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
bibibong committed Jun 2, 2018
1 parent 6b0988f commit b403bb4
Show file tree
Hide file tree
Showing 41 changed files with 537 additions and 57 deletions.
5 changes: 3 additions & 2 deletions cmd/v8/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"time"

"github.com/nebulasio/go-nebulas/core"
"github.com/nebulasio/go-nebulas/core/state"
"github.com/nebulasio/go-nebulas/nf/nvm"
"github.com/nebulasio/go-nebulas/storage"
Expand All @@ -34,9 +35,9 @@ func main() {

mem, _ := storage.NewMemoryStorage()
context, _ := state.NewWorldState(nil, mem)
contract, _ := context.CreateContractAccount([]byte("account2"), nil)
contract, _ := context.CreateContractAccount([]byte("account2"), nil, nil)

ctx, err := nvm.NewContext(nil, nil, contract, context)
ctx, err := nvm.NewContext(core.MockBlock(nil, 1), nil, contract, context)
if err == nil {
engine := nvm.NewV8Engine(ctx)
result, err := engine.RunScriptSource(string(data), 0)
Expand Down
8 changes: 8 additions & 0 deletions core/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -1290,3 +1290,11 @@ func LoadBlockFromStorage(hash byteutils.Hash, chain *BlockChain) (*Block, error
block.storage = chain.storage
return block, nil
}

// MockBlock nf/nvm/engine.CheckV8Run() & cmd/v8/main.go
func MockBlock(header *BlockHeader, height uint64) *Block {
return &Block{
header: header,
height: height,
}
}
26 changes: 16 additions & 10 deletions core/compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,22 @@ var (
// NOTE: versions should be arranged in ascending order
// map[libname][versions]
V8JSLibs = map[string][]string{
"execution_env.js": {"1.0.0", "1.0.1"},
"bignumber.js": {"1.0.0", "1.0.1"},
"random.js": {"1.0.0", "1.0.1"},
"date.js": {"1.0.0", "1.0.1"},
"blockchain.js": {"1.0.0"},
"console.js": {"1.0.0"},
"event.js": {"1.0.0"},
"storage.js": {"1.0.0"},
"crypto.js": {"1.0.1"},
"uint.js": {"1.0.1"},
"execution_env.js": {"1.0.0", "1.0.1"},
"bignumber.js": {"1.0.0", "1.0.1"},
"random.js": {"1.0.0", "1.0.1"},
"date.js": {"1.0.0", "1.0.1"},
"tsc.js": {"1.0.0"},
"util.js": {"1.0.0"},
"esprima.js": {"1.0.0"},
"assert.js": {"1.0.0"},
"instruction_counter.js": {"1.0.0"},
"typescriptServices.js": {"1.0.0"},
"blockchain.js": {"1.0.0"},
"console.js": {"1.0.0"},
"event.js": {"1.0.0"},
"storage.js": {"1.0.0"},
"crypto.js": {"1.0.1"},
"uint.js": {"1.0.1"},
}
)

Expand Down
9 changes: 9 additions & 0 deletions core/state/account_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,12 @@ func (as *accountState) String() string {
as.storage,
)
}

// MockAccount nf/nvm/engine.CheckV8Run() & cmd/v8/main.go
func MockAccount(version string) Account {
return &account{
contractMeta: &corepb.ContractMeta{
Version: version,
},
}
}
19 changes: 19 additions & 0 deletions nf/nvm/cfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ int VerifyAddressFunc(void *handler, const char *address, size_t *gasCnt);
// event.
void EventTriggerFunc(void *handler, const char *topic, const char *data, size_t *gasCnt);
// crypto
char *Sha256Func(const char *data, size_t *gasCnt);
char *Sha3256Func(const char *data, size_t *gasCnt);
char *Ripemd160Func(const char *data, size_t *gasCnt);
char *RecoverAddressFunc(int alg, const char *data, const char *sign, size_t *gasCnt);
// The gateway functions.
void V8Log_cgo(int level, const char *msg) {
V8Log(level, msg);
Expand Down Expand Up @@ -82,5 +88,18 @@ void EventTriggerFunc_cgo(void *handler, const char *topic, const char *data, si
EventTriggerFunc(handler, topic, data, gasCnt);
};
char *Sha256Func_cgo(const char *data, size_t *gasCnt) {
return Sha256Func(data, gasCnt);
}
char *Sha3256Func_cgo(const char *data, size_t *gasCnt) {
return Sha3256Func(data, gasCnt);
}
char *Ripemd160Func_cgo(const char *data, size_t *gasCnt) {
return Ripemd160Func(data, gasCnt);
}
char *RecoverAddressFunc_cgo(int alg, const char *data, const char *sign, size_t *gasCnt) {
return RecoverAddressFunc(alg, data, sign, gasCnt);
}
*/
import "C"
99 changes: 99 additions & 0 deletions nf/nvm/crypto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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 <http://www.gnu.org/licenses/>.
//

package nvm

import "C"

import (
"github.com/nebulasio/go-nebulas/core"
"github.com/nebulasio/go-nebulas/crypto/hash"
"github.com/nebulasio/go-nebulas/crypto/keystore"
"github.com/nebulasio/go-nebulas/util/byteutils"
"github.com/nebulasio/go-nebulas/util/logging"
"github.com/sirupsen/logrus"
)

// Sha256Func ..
//export Sha256Func
func Sha256Func(data *C.char, gasCnt *C.size_t) *C.char {
s := C.GoString(data)
*gasCnt = C.size_t(1000 + len(s))

r := hash.Sha256([]byte(s))
return C.CString(byteutils.Hex(r))
}

// Sha3256Func ..
//export Sha3256Func
func Sha3256Func(data *C.char, gasCnt *C.size_t) *C.char {
s := C.GoString(data)
*gasCnt = C.size_t(1000 + len(s))

r := hash.Sha3256([]byte(s))
return C.CString(byteutils.Hex(r))
}

// Ripemd160Func ..
//export Ripemd160Func
func Ripemd160Func(data *C.char, gasCnt *C.size_t) *C.char {
s := C.GoString(data)
*gasCnt = C.size_t(1000 + len(s))

r := hash.Ripemd160([]byte(s))
return C.CString(byteutils.Hex(r))
}

// RecoverAddressFunc ..
//export RecoverAddressFunc
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)

plain, err := byteutils.FromHex(d)
if err != nil {
logging.VLog().WithFields(logrus.Fields{
"data": d,
"sign": s,
"alg": alg,
"err": err,
}).Debug("convert data to byte array error.")
}
cipher, err := byteutils.FromHex(s)
if err != nil {
logging.VLog().WithFields(logrus.Fields{
"data": d,
"sign": s,
"alg": alg,
"err": err,
}).Debug("convert sign to byte array error.")
}
addr, err := core.RecoverSignerFromSignature(keystore.Algorithm(alg), plain, cipher)
if err != nil {
logging.VLog().WithFields(logrus.Fields{
"data": d,
"sign": s,
"alg": alg,
"err": err,
}).Debug("recover address error.")
}

return C.CString(addr.String())
}
7 changes: 6 additions & 1 deletion nf/nvm/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ func (nvm *NebulasVM) CreateEngine(block *core.Block, tx *core.Transaction, cont

// CheckV8Run to check V8 env is OK
func (nvm *NebulasVM) CheckV8Run() error {
engine := NewV8Engine(nil)
engine := NewV8Engine(&Context{
block: core.MockBlock(nil, 1),
contract: state.MockAccount("1.0.0"),
tx: nil,
state: nil,
})
_, err := engine.RunScriptSource("", 0)
engine.Dispose()
return err
Expand Down
11 changes: 11 additions & 0 deletions nf/nvm/engine_v8.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ char *GetAccountStateFunc_cgo(void *handler, const char *address);
int TransferFunc_cgo(void *handler, const char *to, const char *value);
int VerifyAddressFunc_cgo(void *handler, const char *address);
char *Sha256Func_cgo(const char *data, size_t *gasCnt);
char *Sha3256Func_cgo(const char *data, size_t *gasCnt);
char *Ripemd160Func_cgo(const char *data, size_t *gasCnt);
char *RecoverAddressFunc_cgo(int alg, const char *data, const char *sign, size_t *gasCnt);
void EventTriggerFunc_cgo(void *handler, const char *topic, const char *data, size_t *gasCnt);
*/
Expand Down Expand Up @@ -122,6 +127,12 @@ func InitV8Engine() {

// Event.
C.InitializeEvent((C.EventTriggerFunc)(unsafe.Pointer(C.EventTriggerFunc_cgo)))

// Crypto
C.InitializeCrypto((C.Sha256Func)(unsafe.Pointer(C.Sha256Func_cgo)),
(C.Sha3256Func)(unsafe.Pointer(C.Sha3256Func_cgo)),
(C.Ripemd160Func)(unsafe.Pointer(C.Ripemd160Func_cgo)),
(C.RecoverAddressFunc)(unsafe.Pointer(C.RecoverAddressFunc_cgo)))
}

// DisposeV8Engine dispose the v8 engine.
Expand Down
Loading

0 comments on commit b403bb4

Please sign in to comment.