Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vm] Temporarily brought back otto as a VM #105

Open
wants to merge 43 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
12c54f9
[documents] added document notarize call
Sep 11, 2018
bbc8cf9
[docs] added notarise command
Sep 11, 2018
2993dad
[eth] fix
Sep 13, 2018
13cd42d
[deps] updated protobuf
Sep 17, 2018
acaefbf
[docs] ipfs fix attempt
Sep 18, 2018
007ac65
[docs] Added more logs for notarise call error.
seland Sep 18, 2018
6d6ae2e
[docs] removed ipfs
Sep 18, 2018
1729c3e
[docs] fixed merge conflicts
Sep 18, 2018
f627e5b
[chat] added base group chat
Sep 18, 2018
bbba096
[tests] fixed tests
Sep 18, 2018
b549b5b
[chat] worked on group chat fixes
Sep 19, 2018
dee7277
[chat] worked on group id for messages
Sep 19, 2018
c56480f
[chat] fixed tests
Sep 19, 2018
009a263
[fix] small changes
Sep 19, 2018
733ffb4
[chat] added method to create private chat
Sep 19, 2018
4dfdf47
[notary] added signer
Sep 19, 2018
8733c8e
[notary] Fixed typo.
seland Sep 19, 2018
0c2d825
[notary] fix for submit
Sep 19, 2018
0a99922
Merge branch 'feature/notary' of github.com:Bit-Nation/panthalassa in…
Sep 19, 2018
c8cc640
[docs] fixed eth sign action
Sep 24, 2018
d726f39
[documents] fixed notary calls
Sep 25, 2018
69e7970
[api] changed tx data
Sep 25, 2018
102df01
[km] fix for signing with int encoding
Sep 25, 2018
1d9329d
[km] added tx transform for unmarshal tx
Sep 25, 2018
ba9897c
[chat] fixed chat return arguments
Sep 26, 2018
c51ede5
[chat] fixed group chat tests
Sep 26, 2018
3288de0
[chat] fixed tests added group name
Sep 26, 2018
f3b2ffd
[chat] renamed chat name to group chat name
Sep 26, 2018
7c48539
[panthalassa] added chat_partner and partners to chat
Sep 26, 2018
92be238
[ios] fixed compile error
Sep 26, 2018
3b672a6
[chat] added sender to ui events
Sep 26, 2018
b936d95
[vm] Temporarily brought back otto as a VM
borjantrajanoski Sep 27, 2018
f5b977c
[docs] decrypt content
Sep 27, 2018
ceebf38
[docs] added content decryption
Sep 27, 2018
d8c893a
[docs] added new notary contract
Sep 28, 2018
1ed8f4f
[panthalassa] added tx fee to notary
Sep 28, 2018
6b8d026
[panthalassa] fixed chat storage nil bug
Sep 28, 2018
7ee0154
[chat] added sender field
Sep 28, 2018
3048611
[docs] fixed decryption
Sep 28, 2018
452f71d
Merge branch 'feature/notary' into feature/old_vm
borjantrajanoski Sep 28, 2018
c7a865c
[tests] fixed api test
Sep 28, 2018
36206a4
[docs] fixed call to notarize document
Sep 28, 2018
76b1d90
Merge branch 'feature/group-chat' into feature/old_vm
borjantrajanoski Sep 29, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
383 changes: 375 additions & 8 deletions Gopkg.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

[[constraint]]
name = "github.com/ethereum/go-ethereum"
version = "1.8.11"
version = "1.8.15"

[[constraint]]
branch = "master"
Expand Down
2 changes: 1 addition & 1 deletion api/dapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func (a *DAppApi) SendEthereumTransaction(value, to, data string) (string, error
"v": ethTx.V,
"r": ethTx.R,
"s": ethTx.S,
"chainId": ethTx.ChainID,
"from": ethTx.From,
"hash": ethTx.Hash,
"chainId": ethTx.ChainID,
}

raw, err := json.Marshal(objTx)
Expand Down
24 changes: 13 additions & 11 deletions dapp/dapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
storm "github.com/asdine/storm"
log "github.com/ipfs/go-log"
logger "github.com/op/go-logging"
duktape "gopkg.in/olebedev/go-duktape.v3"
otto "github.com/robertkrimen/otto"
)

var sysLog = log.Logger("dapp")

type DApp struct {
vm *duktape.Context
vm *otto.Otto
logger *logger.Logger
app *Data
// will be called when the app shut down
Expand All @@ -34,14 +34,15 @@ type DApp struct {

// close DApp
func (d *DApp) Close() {
defer d.vm.DestroyHeap()
d.logger.Info(fmt.Sprintf("shutting down: %s (%s)", hex.EncodeToString(d.app.UsedSigningKey), d.app.Name))
for _, mod := range d.vmModules {
if err := mod.Close(); err != nil {
sysLog.Error(err)
d.vm.Interrupt <- func() {
d.logger.Info(fmt.Sprintf("shutting down: %s (%s)", hex.EncodeToString(d.app.UsedSigningKey), d.app.Name))
for _, mod := range d.vmModules {
if err := mod.Close(); err != nil {
sysLog.Error(err)
}
}
d.closeChan <- d.app
}
d.closeChan <- d.app
}

func (d *DApp) ID() string {
Expand Down Expand Up @@ -73,7 +74,8 @@ func New(l *logger.Logger, app *Data, vmModules []module.Module, closer chan<- *
}

// create VM
vm := duktape.New()
vm := otto.New()
vm.Interrupt = make(chan func(), 1)

// register all vm modules
for _, m := range vmModules {
Expand Down Expand Up @@ -130,8 +132,7 @@ func New(l *logger.Logger, app *Data, vmModules []module.Module, closer chan<- *

// start the DApp async
go func() {
// Synonymous to vm.Run in Otto
err := vm.PevalString(string(app.Code))
_, err := vm.Run(app.Code)
if err != nil {
l.Errorf(err.Error())
}
Expand All @@ -146,6 +147,7 @@ func New(l *logger.Logger, app *Data, vmModules []module.Module, closer chan<- *
}
return dApp, nil
case <-time.After(timeOut):
vm.Interrupt <- func() {}
closer <- app
return nil, errors.New("timeout - failed to start DApp")
}
Expand Down
2 changes: 1 addition & 1 deletion dapp/dapp_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

uiApi "github.com/Bit-Nation/panthalassa/uiapi"
storm "github.com/asdine/storm"
bolt "github.com/coreos/bbolt"
require "github.com/stretchr/testify/require"
bolt "go.etcd.io/bbolt"
ed25519 "golang.org/x/crypto/ed25519"
)

Expand Down
104 changes: 51 additions & 53 deletions dapp/module/callbacks/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
validator "github.com/Bit-Nation/panthalassa/dapp/validator"
log "github.com/ipfs/go-log"
logger "github.com/op/go-logging"
duktape "gopkg.in/olebedev/go-duktape.v3"
otto "github.com/robertkrimen/otto"
)

var debugger = log.Logger("callbacks")
Expand All @@ -34,7 +34,7 @@ func New(l *logger.Logger) *Module {

var count uint

functions := map[uint]*duktape.Context{}
functions := map[uint]*otto.Value{}
cbChans := map[*chan error]bool{}

for {
Expand Down Expand Up @@ -110,7 +110,7 @@ func New(l *logger.Logger) *Module {
}

type addFunction struct {
fn *duktape.Context
fn *otto.Value
respChan chan struct {
id uint
error error
Expand All @@ -119,12 +119,12 @@ type addFunction struct {

type fetchFunction struct {
id uint
respChan chan *duktape.Context
respChan chan *otto.Value
}

type Module struct {
logger *logger.Logger
vm *duktape.Context
vm *otto.Otto
reqLim *reqLim.Count
addFunctionChan chan addFunction
fetchFunctionChan chan fetchFunction
Expand All @@ -146,33 +146,37 @@ func (m *Module) Close() error {
// e.g. myRegisteredFunction(payloadObj, cb)
// the callback must be called in order to "return" from the function
func (m *Module) CallFunction(id uint, payload string) error {

debugger.Debug(fmt.Errorf("call function with id: %d and payload: %s", id, payload))

respChan := make(chan *duktape.Context)
respChan := make(chan *otto.Value)
m.fetchFunctionChan <- fetchFunction{
id: id,
respChan: respChan,
}
vm := <-respChan
fn := <-respChan

if vm == nil || vm.GetType(0).IsNone() {
if fn == nil {
return errors.New(fmt.Sprintf("function with id: %d does not exist", id))
}

// check if function is registered
if !vm.IsFunction(0) {
if !fn.IsFunction() {
return errors.New(fmt.Sprintf("function with id: %d does not exist", id))
}

// parse params
objArgs := "(" + payload + ")"
objArgs, err := m.vm.Object("(" + payload + ")")
if err != nil {
return err
}

done := make(chan error, 1)
m.addCBChan <- &done

alreadyCalled := false

_, err := vm.PushGlobalGoFunction("callbackCallFunction", func(context *duktape.Context) int {
_, err = fn.Call(*fn, objArgs, func(call otto.FunctionCall) otto.Value {

defer func() {
m.rmCBChan <- &done
Expand All @@ -181,43 +185,26 @@ func (m *Module) CallFunction(id uint, payload string) error {
// check if callback has already been called
if alreadyCalled {
m.logger.Error("Already called callback")
if context.IsFunction(1) {
context.PushString("Callback: Already called callback")
context.Call(1)
}
return 1
return m.vm.MakeCustomError("Callback", "Already called callback")
}
alreadyCalled = true

if !context.IsUndefined(0) {
firstParameter := context.ToString(0)
if context.IsFunction(1) {
context.PushString(firstParameter)
context.Call(1)
}
done <- errors.New(firstParameter)
return 1
// check parameters
err := call.Argument(0)

if !err.IsUndefined() {
done <- errors.New(err.String())
return otto.Value{}
}

done <- nil
return 0
return otto.Value{}

})
if err != nil {
m.logger.Error(err.Error())
}
// Push objArgs to the stack as first parameter
err = vm.PevalString(objArgs)
if err != nil {
m.logger.Error(err.Error())
}
// Push callbackCallFunction to the stack as second parameter
err = vm.PevalString(`callbackCallFunction`)
if err != nil {
m.logger.Error(err.Error())
}
// Use 2 when calling as we just pushed 2 parameters to the stack
vm.Call(2)

return <-done

}
Expand All @@ -228,37 +215,43 @@ func (m *Module) CallFunction(id uint, payload string) error {
// a registered function will be called with an object containing information
// and a callback that should be called (with an optional error) in order to
// "return"
func (m *Module) Register(vm *duktape.Context) error {
func (m *Module) Register(vm *otto.Otto) error {
m.vm = vm
_, err := vm.PushGlobalGoFunction("registerFunction", func(context *duktape.Context) int {
err := vm.Set("registerFunction", func(call otto.FunctionCall) otto.Value {
// validate function call
v := validator.New()
v.Set(0, &validator.TypeFunction)
if err := v.Validate(context); err != nil {
m.logger.Error(fmt.Sprintf(`registerFunction needs a callback as it's first param %s`, err.Error()))
return 1
if err := v.Validate(vm, call); err != nil {
m.logger.Error(fmt.Sprintf(`registerFunction needs a callback as it's first param %s`, err.String()))
return *err
}

// callback
fn := call.Argument(0)

// add function to stack
idRespChan := make(chan struct {
id uint
error error
}, 1)
m.addFunctionChan <- addFunction{
respChan: idRespChan,
fn: context,
fn: &fn,
}
// response
addResp := <-idRespChan

// exit vm on error
if addResp.error != nil {
m.logger.Error(addResp.error.Error())
return 1
return otto.Value{}
}

// convert function id to int
functionId := int(addResp.id)
// convert function id to otto value
functionId, err := otto.ToValue(addResp.id)
if err != nil {
m.logger.Error(err.Error())
}

return functionId

Expand All @@ -267,22 +260,27 @@ func (m *Module) Register(vm *duktape.Context) error {
return err
}

_, err = vm.PushGlobalGoFunction("unRegisterFunction", func(context *duktape.Context) int {
return vm.Set("unRegisterFunction", func(call otto.FunctionCall) otto.Value {

// validate function call
v := validator.New()
v.Set(0, &validator.TypeNumber)
if err := v.Validate(context); err != nil {
return 1
if err := v.Validate(vm, call); err != nil {
return *err
}

// function id
funcID := context.ToInt(0)
funcID := call.Argument(0)
id, err := funcID.ToInteger()
if err != nil {
m.logger.Error(err.Error())
return otto.Value{}
}

// delete function from channel
m.deleteFunctionChan <- uint(funcID)
m.deleteFunctionChan <- uint(id)

return 0
return otto.Value{}
})
return err

}
Loading