2019-11-21 16:19:22 +00:00
|
|
|
// +build nimbus
|
|
|
|
|
|
|
|
package nimbusbridge
|
|
|
|
|
|
|
|
// https://golang.org/cmd/cgo/
|
|
|
|
|
|
|
|
/*
|
2019-12-18 13:02:43 +00:00
|
|
|
#cgo LDFLAGS: -Wl,-rpath,'$ORIGIN' -L${SRCDIR} -lnimbus -lm
|
2019-11-21 16:19:22 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <libnimbus.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-12-18 13:02:43 +00:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
"unsafe"
|
2019-11-21 16:19:22 +00:00
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
enstypes "github.com/status-im/status-go/eth-node/types/ens"
|
|
|
|
"go.uber.org/zap"
|
2019-11-21 16:19:22 +00:00
|
|
|
)
|
|
|
|
|
2019-12-18 13:02:43 +00:00
|
|
|
type nimbusNodeWrapper struct {
|
|
|
|
mu sync.Mutex
|
2019-11-21 16:19:22 +00:00
|
|
|
|
2019-12-18 13:02:43 +00:00
|
|
|
routineQueue *RoutineQueue
|
|
|
|
tid int
|
|
|
|
nodeStarted bool
|
|
|
|
cancelPollingChan chan struct{}
|
2019-11-21 16:19:22 +00:00
|
|
|
|
2019-12-18 13:02:43 +00:00
|
|
|
w types.Whisper
|
|
|
|
}
|
2019-11-21 16:19:22 +00:00
|
|
|
|
2019-12-18 13:02:43 +00:00
|
|
|
type Node interface {
|
|
|
|
types.Node
|
2019-11-21 16:19:22 +00:00
|
|
|
|
2019-12-18 13:02:43 +00:00
|
|
|
StartNimbus(privateKey *ecdsa.PrivateKey, listenAddr string, staging bool) error
|
|
|
|
Stop()
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
2019-11-23 17:57:05 +00:00
|
|
|
|
2019-12-18 13:02:43 +00:00
|
|
|
func NewNodeBridge() Node {
|
|
|
|
c := make(chan Node, 1)
|
|
|
|
go func(c chan<- Node, delay time.Duration) {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
n := &nimbusNodeWrapper{
|
|
|
|
routineQueue: NewRoutineQueue(),
|
|
|
|
tid: syscall.Gettid(),
|
|
|
|
cancelPollingChan: make(chan struct{}, 1),
|
|
|
|
}
|
|
|
|
c <- n
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-time.After(delay):
|
|
|
|
n.poll()
|
|
|
|
case <-n.cancelPollingChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(c, 50*time.Millisecond)
|
|
|
|
|
|
|
|
return <-c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *nimbusNodeWrapper) StartNimbus(privateKey *ecdsa.PrivateKey, listenAddr string, staging bool) error {
|
2019-12-18 13:18:31 +00:00
|
|
|
return n.routineQueue.Send(func(c chan<- callReturn) {
|
|
|
|
c <- callReturn{err: startNimbus(privateKey, listenAddr, staging)}
|
2019-12-18 13:02:43 +00:00
|
|
|
n.nodeStarted = true
|
2019-12-18 13:18:31 +00:00
|
|
|
}).err
|
2019-11-23 17:57:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 13:02:43 +00:00
|
|
|
func (n *nimbusNodeWrapper) Stop() {
|
|
|
|
if n.cancelPollingChan != nil {
|
|
|
|
close(n.cancelPollingChan)
|
|
|
|
n.nodeStarted = false
|
|
|
|
n.cancelPollingChan = nil
|
|
|
|
}
|
2019-11-23 17:57:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 13:02:43 +00:00
|
|
|
func (n *nimbusNodeWrapper) NewENSVerifier(_ *zap.Logger) enstypes.ENSVerifier {
|
2019-11-23 17:57:05 +00:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2019-12-18 13:02:43 +00:00
|
|
|
func (n *nimbusNodeWrapper) GetWhisper(ctx interface{}) (types.Whisper, error) {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
|
|
|
|
if n.w == nil {
|
|
|
|
n.w = NewNimbusWhisperWrapper(n.routineQueue)
|
|
|
|
}
|
|
|
|
return n.w, nil
|
2019-11-23 17:57:05 +00:00
|
|
|
}
|
2019-12-12 16:06:00 +00:00
|
|
|
|
2019-12-18 13:02:43 +00:00
|
|
|
func (n *nimbusNodeWrapper) AddPeer(url string) error {
|
2019-12-12 16:06:00 +00:00
|
|
|
urlC := C.CString(url)
|
|
|
|
defer C.free(unsafe.Pointer(urlC))
|
|
|
|
if !C.nimbus_add_peer(urlC) {
|
|
|
|
return fmt.Errorf("failed to add peer: %s", url)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 13:02:43 +00:00
|
|
|
func (n *nimbusNodeWrapper) RemovePeer(url string) error {
|
2019-12-12 16:06:00 +00:00
|
|
|
panic("TODO: RemovePeer")
|
|
|
|
}
|
2019-12-18 13:02:43 +00:00
|
|
|
|
|
|
|
func (n *nimbusNodeWrapper) poll() {
|
|
|
|
if syscall.Gettid() != n.tid {
|
|
|
|
panic("poll called from wrong thread")
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n.nodeStarted) {
|
|
|
|
C.nimbus_poll()
|
|
|
|
}
|
|
|
|
|
|
|
|
n.routineQueue.HandleEvent()
|
|
|
|
}
|
|
|
|
|
|
|
|
func startNimbus(privateKey *ecdsa.PrivateKey, listenAddr string, staging bool) error {
|
|
|
|
C.NimMain()
|
|
|
|
|
|
|
|
if listenAddr == "" {
|
|
|
|
listenAddr = ":30304"
|
|
|
|
}
|
|
|
|
addrParts := strings.Split(listenAddr, ":")
|
|
|
|
port, err := strconv.Atoi(addrParts[len(addrParts)-1])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse port number from %s", listenAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
var privateKeyC unsafe.Pointer
|
|
|
|
if privateKey != nil {
|
|
|
|
privateKeyC = C.CBytes(crypto.FromECDSA(privateKey))
|
|
|
|
defer C.free(privateKeyC)
|
|
|
|
}
|
|
|
|
if !C.nimbus_start(C.ushort(port), true, false, 0.002, (*C.uchar)(privateKeyC), C.bool(staging)) {
|
|
|
|
return errors.New("failed to start Nimbus node")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|