Refactor/signals package (#359)

Move signals code to the separate package.
This commit is contained in:
Ivan Daniluk 2017-09-25 20:22:57 +02:00 committed by GitHub
parent 3afcff278f
commit 2acf1a1a6a
14 changed files with 88 additions and 77 deletions

View File

@ -22,6 +22,7 @@ import (
"github.com/status-im/status-go/geth/common" "github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/node" "github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/params" "github.com/status-im/status-go/geth/params"
"github.com/status-im/status-go/geth/signal"
. "github.com/status-im/status-go/geth/testing" . "github.com/status-im/status-go/geth/testing"
"github.com/status-im/status-go/static" "github.com/status-im/status-go/static"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -738,8 +739,8 @@ func testCompleteTransaction(t *testing.T) bool {
// replace transaction notification handler // replace transaction notification handler
var txHash = "" var txHash = ""
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope node.SignalEnvelope var envelope signal.Envelope
if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil { if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil {
t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent) t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent)
return return
@ -815,9 +816,9 @@ func testCompleteMultipleQueuedTransactions(t *testing.T) bool {
allTestTxCompleted := make(chan struct{}, 1) allTestTxCompleted := make(chan struct{}, 1)
// replace transaction notification handler // replace transaction notification handler
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var txID string var txID string
var envelope node.SignalEnvelope var envelope signal.Envelope
if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil { if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil {
t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent) t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent)
return return
@ -949,8 +950,8 @@ func testDiscardTransaction(t *testing.T) bool {
// replace transaction notification handler // replace transaction notification handler
var txID string var txID string
txFailedEventCalled := false txFailedEventCalled := false
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope node.SignalEnvelope var envelope signal.Envelope
if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil { if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil {
t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent) t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent)
return return
@ -1061,9 +1062,9 @@ func testDiscardMultipleQueuedTransactions(t *testing.T) bool {
// replace transaction notification handler // replace transaction notification handler
txFailedEventCallCount := 0 txFailedEventCallCount := 0
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var txID string var txID string
var envelope node.SignalEnvelope var envelope signal.Envelope
if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil { if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil {
t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent) t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent)
return return
@ -1335,24 +1336,24 @@ func startTestNode(t *testing.T) <-chan struct{} {
} }
waitForNodeStart := make(chan struct{}, 1) waitForNodeStart := make(chan struct{}, 1)
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
t.Log(jsonEvent) t.Log(jsonEvent)
var envelope node.SignalEnvelope var envelope signal.Envelope
if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil { if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil {
t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent) t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent)
return return
} }
if envelope.Type == node.EventNodeCrashed { if envelope.Type == signal.EventNodeCrashed {
node.TriggerDefaultNodeNotificationHandler(jsonEvent) signal.TriggerDefaultNodeNotificationHandler(jsonEvent)
return return
} }
if envelope.Type == node.EventTransactionQueued { if envelope.Type == node.EventTransactionQueued {
} }
if envelope.Type == node.EventNodeStarted { if envelope.Type == signal.EventNodeStarted {
t.Log("Node started, but we wait till it be ready") t.Log("Node started, but we wait till it be ready")
} }
if envelope.Type == node.EventNodeReady { if envelope.Type == signal.EventNodeReady {
// manually add static nodes (LES auto-discovery is not stable yet) // manually add static nodes (LES auto-discovery is not stable yet)
PopulateStaticPeers() PopulateStaticPeers()

View File

@ -10,6 +10,7 @@ import (
"github.com/status-im/status-go/geth/log" "github.com/status-im/status-go/geth/log"
"github.com/status-im/status-go/geth/node" "github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/params" "github.com/status-im/status-go/geth/params"
"github.com/status-im/status-go/geth/signal"
) )
// StatusBackend implements Status.im service // StatusBackend implements Status.im service
@ -98,8 +99,8 @@ func (m *StatusBackend) onNodeStart(nodeStarted <-chan struct{}, backendReady ch
log.Info("Account reselected") log.Info("Account reselected")
close(backendReady) close(backendReady)
node.SendSignal(node.SignalEnvelope{ signal.Send(signal.Envelope{
Type: node.EventNodeReady, Type: signal.EventNodeReady,
Event: struct{}{}, Event: struct{}{},
}) })
} }

View File

@ -16,6 +16,7 @@ import (
"github.com/status-im/status-go/geth/log" "github.com/status-im/status-go/geth/log"
"github.com/status-im/status-go/geth/node" "github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/params" "github.com/status-im/status-go/geth/params"
"github.com/status-im/status-go/geth/signal"
. "github.com/status-im/status-go/geth/testing" . "github.com/status-im/status-go/geth/testing"
"github.com/status-im/status-go/static" "github.com/status-im/status-go/static"
) )
@ -55,8 +56,8 @@ func (s *BackendTestSuite) TestJailSendQueuedTransaction() {
// replace transaction notification handler // replace transaction notification handler
requireMessageId := false requireMessageId := false
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope node.SignalEnvelope var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope) err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent)) s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
@ -214,8 +215,8 @@ func (s *BackendTestSuite) TestContractDeployment() {
// replace transaction notification handler // replace transaction notification handler
var txHash gethcommon.Hash var txHash gethcommon.Hash
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope node.SignalEnvelope var envelope signal.Envelope
var err error var err error
err = json.Unmarshal([]byte(jsonEvent), &envelope) err = json.Unmarshal([]byte(jsonEvent), &envelope)
require.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent)) require.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
@ -717,8 +718,8 @@ func (s *BackendTestSuite) TestJailVMPersistence() {
require.NotContains(parseResult, "error", "further will fail if initial parsing failed") require.NotContains(parseResult, "error", "further will fail if initial parsing failed")
var wg sync.WaitGroup var wg sync.WaitGroup
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope node.SignalEnvelope var envelope signal.Envelope
if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil { if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil {
s.T().Errorf("cannot unmarshal event's JSON: %s", jsonEvent) s.T().Errorf("cannot unmarshal event's JSON: %s", jsonEvent)
return return

View File

@ -15,6 +15,7 @@ import (
"github.com/status-im/status-go/geth/log" "github.com/status-im/status-go/geth/log"
"github.com/status-im/status-go/geth/node" "github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/params" "github.com/status-im/status-go/geth/params"
"github.com/status-im/status-go/geth/signal"
. "github.com/status-im/status-go/geth/testing" . "github.com/status-im/status-go/geth/testing"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
) )
@ -277,8 +278,8 @@ func (s *BackendTestSuite) TestCallRPCSendTransaction() {
transactionCompleted := make(chan struct{}) transactionCompleted := make(chan struct{})
var txHash gethcommon.Hash var txHash gethcommon.Hash
node.SetDefaultNodeNotificationHandler(func(rawSignal string) { signal.SetDefaultNodeNotificationHandler(func(rawSignal string) {
var signal node.SignalEnvelope var signal signal.Envelope
err := json.Unmarshal([]byte(rawSignal), &signal) err := json.Unmarshal([]byte(rawSignal), &signal)
s.NoError(err) s.NoError(err)
@ -335,8 +336,8 @@ func (s *BackendTestSuite) TestCallRPCSendTransactionUpstream() {
transactionCompleted := make(chan struct{}) transactionCompleted := make(chan struct{})
var txHash gethcommon.Hash var txHash gethcommon.Hash
node.SetDefaultNodeNotificationHandler(func(rawSignal string) { signal.SetDefaultNodeNotificationHandler(func(rawSignal string) {
var signal node.SignalEnvelope var signal signal.Envelope
err := json.Unmarshal([]byte(rawSignal), &signal) err := json.Unmarshal([]byte(rawSignal), &signal)
s.NoError(err) s.NoError(err)

View File

@ -14,6 +14,7 @@ import (
"github.com/status-im/status-go/geth/log" "github.com/status-im/status-go/geth/log"
"github.com/status-im/status-go/geth/node" "github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/params" "github.com/status-im/status-go/geth/params"
"github.com/status-im/status-go/geth/signal"
. "github.com/status-im/status-go/geth/testing" . "github.com/status-im/status-go/geth/testing"
) )
@ -34,8 +35,8 @@ func (s *BackendTestSuite) TestSendContractTx() {
// replace transaction notification handler // replace transaction notification handler
var txHash gethcommon.Hash var txHash gethcommon.Hash
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { // nolint :dupl signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) { // nolint :dupl
var envelope node.SignalEnvelope var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope) err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent)) s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
@ -128,8 +129,8 @@ func (s *BackendTestSuite) TestSendEtherTx() {
// replace transaction notification handler // replace transaction notification handler
var txHash = gethcommon.Hash{} var txHash = gethcommon.Hash{}
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { // nolint: dupl signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) { // nolint: dupl
var envelope node.SignalEnvelope var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope) err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent)) s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
@ -208,8 +209,8 @@ func (s *BackendTestSuite) TestSendEtherTxUpstream() {
// replace transaction notification handler // replace transaction notification handler
var txHash = gethcommon.Hash{} var txHash = gethcommon.Hash{}
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { // nolint: dupl signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) { // nolint: dupl
var envelope node.SignalEnvelope var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope) err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err, "cannot unmarshal JSON: %s", jsonEvent) s.NoError(err, "cannot unmarshal JSON: %s", jsonEvent)
@ -268,8 +269,8 @@ func (s *BackendTestSuite) TestDoubleCompleteQueuedTransactions() {
// replace transaction notification handler // replace transaction notification handler
txFailedEventCalled := false txFailedEventCalled := false
txHash := gethcommon.Hash{} txHash := gethcommon.Hash{}
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope node.SignalEnvelope var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope) err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent)) s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
@ -350,8 +351,8 @@ func (s *BackendTestSuite) TestDiscardQueuedTransaction() {
// replace transaction notification handler // replace transaction notification handler
txFailedEventCalled := false txFailedEventCalled := false
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope node.SignalEnvelope var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope) err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent)) s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
@ -432,8 +433,8 @@ func (s *BackendTestSuite) TestCompleteMultipleQueuedTransactions() {
allTestTxCompleted := make(chan struct{}) allTestTxCompleted := make(chan struct{})
// replace transaction notification handler // replace transaction notification handler
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope node.SignalEnvelope var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope) err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent)) s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
@ -533,8 +534,8 @@ func (s *BackendTestSuite) TestDiscardMultipleQueuedTransactions() {
// replace transaction notification handler // replace transaction notification handler
txFailedEventCallCount := 0 txFailedEventCallCount := 0
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope node.SignalEnvelope var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope) err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err) s.NoError(err)
if envelope.Type == node.EventTransactionQueued { if envelope.Type == node.EventTransactionQueued {
@ -642,7 +643,7 @@ func (s *BackendTestSuite) TestNonExistentQueuedTransactions() {
require.NoError(s.backend.AccountManager().SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password)) require.NoError(s.backend.AccountManager().SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password))
// replace transaction notification handler // replace transaction notification handler
node.SetDefaultNodeNotificationHandler(func(string) {}) signal.SetDefaultNodeNotificationHandler(func(string) {})
// try completing non-existing transaction // try completing non-existing transaction
_, err := s.backend.CompleteTransaction("some-bad-transaction-id", TestConfig.Account1.Password) _, err := s.backend.CompleteTransaction("some-bad-transaction-id", TestConfig.Account1.Password)

View File

@ -6,13 +6,13 @@ import (
"strings" "strings"
"github.com/robertkrimen/otto" "github.com/robertkrimen/otto"
"github.com/status-im/status-go/geth/node" "github.com/status-im/status-go/geth/signal"
) )
// Write provides the base function to write data to the underline writer // Write provides the base function to write data to the underline writer
// for the underline otto vm. // for the underline otto vm.
func Write(fn otto.FunctionCall, w io.Writer, consoleEventName string) otto.Value { func Write(fn otto.FunctionCall, w io.Writer, consoleEventName string) otto.Value {
node.SendSignal(node.SignalEnvelope{ signal.Send(signal.Envelope{
Type: consoleEventName, Type: consoleEventName,
Event: convertArgs(fn.ArgumentList), Event: convertArgs(fn.ArgumentList),
}) })

View File

@ -7,6 +7,7 @@ import (
"github.com/status-im/status-go/geth/common" "github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/jail/console" "github.com/status-im/status-go/geth/jail/console"
"github.com/status-im/status-go/geth/node" "github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/signal"
) )
// signals // signals
@ -130,7 +131,7 @@ func makeSignalHandler(chatID string) func(call otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value { return func(call otto.FunctionCall) otto.Value {
message := call.Argument(0).String() message := call.Argument(0).String()
node.SendSignal(node.SignalEnvelope{ signal.Send(signal.Envelope{
Type: EventSignal, Type: EventSignal,
Event: SignalEvent{ Event: SignalEvent{
ChatID: chatID, ChatID: chatID,

View File

@ -10,6 +10,7 @@ import (
"github.com/status-im/status-go/geth/jail" "github.com/status-im/status-go/geth/jail"
"github.com/status-im/status-go/geth/node" "github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/params" "github.com/status-im/status-go/geth/params"
"github.com/status-im/status-go/geth/signal"
. "github.com/status-im/status-go/geth/testing" . "github.com/status-im/status-go/geth/testing"
"github.com/status-im/status-go/static" "github.com/status-im/status-go/static"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
@ -232,8 +233,8 @@ func (s *JailTestSuite) TestEventSignal() {
opCompletedSuccessfully := make(chan struct{}, 1) opCompletedSuccessfully := make(chan struct{}, 1)
// replace transaction notification handler // replace transaction notification handler
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope node.SignalEnvelope var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope) err := json.Unmarshal([]byte(jsonEvent), &envelope)
require.NoError(err) require.NoError(err)

View File

@ -16,6 +16,7 @@ import (
"github.com/status-im/status-go/geth/log" "github.com/status-im/status-go/geth/log"
"github.com/status-im/status-go/geth/params" "github.com/status-im/status-go/geth/params"
"github.com/status-im/status-go/geth/rpc" "github.com/status-im/status-go/geth/rpc"
"github.com/status-im/status-go/geth/signal"
) )
// errors // errors
@ -82,9 +83,9 @@ func (m *NodeManager) startNode(config *params.NodeConfig) (<-chan struct{}, err
m.Lock() m.Lock()
m.nodeStarted = nil m.nodeStarted = nil
m.Unlock() m.Unlock()
SendSignal(SignalEnvelope{ signal.Send(signal.Envelope{
Type: EventNodeCrashed, Type: signal.EventNodeCrashed,
Event: NodeCrashEvent{ Event: signal.NodeCrashEvent{
Error: fmt.Errorf("%v: %v", ErrNodeStartFailure, err).Error(), Error: fmt.Errorf("%v: %v", ErrNodeStartFailure, err).Error(),
}, },
}) })
@ -101,9 +102,9 @@ func (m *NodeManager) startNode(config *params.NodeConfig) (<-chan struct{}, err
if err != nil { if err != nil {
log.Error("Init RPC client failed:", "error", err) log.Error("Init RPC client failed:", "error", err)
m.Unlock() m.Unlock()
SendSignal(SignalEnvelope{ signal.Send(signal.Envelope{
Type: EventNodeCrashed, Type: signal.EventNodeCrashed,
Event: NodeCrashEvent{ Event: signal.NodeCrashEvent{
Error: ErrRPCClient.Error(), Error: ErrRPCClient.Error(),
}, },
}) })
@ -120,8 +121,8 @@ func (m *NodeManager) startNode(config *params.NodeConfig) (<-chan struct{}, err
// notify all subscribers that Status node is started // notify all subscribers that Status node is started
close(m.nodeStarted) close(m.nodeStarted)
SendSignal(SignalEnvelope{ signal.Send(signal.Envelope{
Type: EventNodeStarted, Type: signal.EventNodeStarted,
Event: struct{}{}, Event: struct{}{},
}) })
@ -179,8 +180,8 @@ func (m *NodeManager) stopNode() (<-chan struct{}, error) {
log.Info("Node manager resets node params") log.Info("Node manager resets node params")
// notify application that it can send more requests now // notify application that it can send more requests now
SendSignal(SignalEnvelope{ signal.Send(signal.Envelope{
Type: EventNodeStopped, Type: signal.EventNodeStopped,
Event: struct{}{}, Event: struct{}{},
}) })
log.Info("Node manager notifed app, that node has stopped") log.Info("Node manager notifed app, that node has stopped")
@ -317,8 +318,8 @@ func (m *NodeManager) resetChainData() (<-chan struct{}, error) {
return nil, err return nil, err
} }
// send signal up to native app // send signal up to native app
SendSignal(SignalEnvelope{ signal.Send(signal.Envelope{
Type: EventChainDataRemoved, Type: signal.EventChainDataRemoved,
Event: struct{}{}, Event: struct{}{},
}) })
log.Info("Chain data has been removed", "dir", chainDataDir) log.Info("Chain data has been removed", "dir", chainDataDir)

View File

@ -16,6 +16,7 @@ import (
"github.com/status-im/status-go/geth/log" "github.com/status-im/status-go/geth/log"
"github.com/status-im/status-go/geth/node" "github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/params" "github.com/status-im/status-go/geth/params"
"github.com/status-im/status-go/geth/signal"
. "github.com/status-im/status-go/geth/testing" . "github.com/status-im/status-go/geth/testing"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
) )
@ -445,13 +446,13 @@ func (s *ManagerTestSuite) TestNodeStartCrash() {
// let's listen for node.crashed signal // let's listen for node.crashed signal
signalReceived := false signalReceived := false
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
log.Info("Notification Received", "event", jsonEvent) log.Info("Notification Received", "event", jsonEvent)
var envelope node.SignalEnvelope var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope) err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent)) s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
if envelope.Type == node.EventNodeCrashed { if envelope.Type == signal.EventNodeCrashed {
signalReceived = true signalReceived = true
} }
}) })
@ -478,5 +479,5 @@ func (s *ManagerTestSuite) TestNodeStartCrash() {
// cleanup // cleanup
s.NodeManager.StopNode() s.NodeManager.StopNode()
node.ResetDefaultNodeNotificationHandler() signal.ResetDefaultNodeNotificationHandler()
} }

View File

@ -14,6 +14,7 @@ import (
"github.com/pborman/uuid" "github.com/pborman/uuid"
"github.com/status-im/status-go/geth/common" "github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/log" "github.com/status-im/status-go/geth/log"
"github.com/status-im/status-go/geth/signal"
) )
const ( const (
@ -402,7 +403,7 @@ type SendTransactionEvent struct {
func (m *TxQueueManager) TransactionQueueHandler() func(queuedTx *common.QueuedTx) { func (m *TxQueueManager) TransactionQueueHandler() func(queuedTx *common.QueuedTx) {
return func(queuedTx *common.QueuedTx) { return func(queuedTx *common.QueuedTx) {
log.Info("calling TransactionQueueHandler") log.Info("calling TransactionQueueHandler")
SendSignal(SignalEnvelope{ signal.Send(signal.Envelope{
Type: EventTransactionQueued, Type: EventTransactionQueued,
Event: SendTransactionEvent{ Event: SendTransactionEvent{
ID: string(queuedTx.ID), ID: string(queuedTx.ID),
@ -441,7 +442,7 @@ func (m *TxQueueManager) TransactionReturnHandler() func(queuedTx *common.Queued
} }
// error occurred, signal up to application // error occurred, signal up to application
SendSignal(SignalEnvelope{ signal.Send(signal.Envelope{
Type: EventTransactionFailed, Type: EventTransactionFailed,
Event: ReturnSendTransactionEvent{ Event: ReturnSendTransactionEvent{
ID: string(queuedTx.ID), ID: string(queuedTx.ID),

View File

@ -3,10 +3,11 @@ package node
import ( import (
"fmt" "fmt"
"os" "os"
"os/signal" osSignal "os/signal"
"github.com/status-im/status-go/geth/common" "github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/log" "github.com/status-im/status-go/geth/log"
"github.com/status-im/status-go/geth/signal"
) )
// HaltOnPanic recovers from panic, logs issue, sends upward notification, and exits // HaltOnPanic recovers from panic, logs issue, sends upward notification, and exits
@ -15,9 +16,9 @@ func HaltOnPanic() {
err := fmt.Errorf("%v: %v", ErrNodeRunFailure, r) err := fmt.Errorf("%v: %v", ErrNodeRunFailure, r)
// send signal up to native app // send signal up to native app
SendSignal(SignalEnvelope{ signal.Send(signal.Envelope{
Type: EventNodeCrashed, Type: signal.EventNodeCrashed,
Event: NodeCrashEvent{ Event: signal.NodeCrashEvent{
Error: err.Error(), Error: err.Error(),
}, },
}) })
@ -29,8 +30,8 @@ func HaltOnPanic() {
// HaltOnInterruptSignal stops node and panics if you press Ctrl-C enough times // HaltOnInterruptSignal stops node and panics if you press Ctrl-C enough times
func HaltOnInterruptSignal(nodeManager *NodeManager) { func HaltOnInterruptSignal(nodeManager *NodeManager) {
sigc := make(chan os.Signal, 1) sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt) osSignal.Notify(sigc, os.Interrupt)
defer signal.Stop(sigc) defer osSignal.Stop(sigc)
<-sigc <-sigc
if nodeManager.node == nil { if nodeManager.node == nil {
return return

View File

@ -1,4 +1,4 @@
package node package signal
/* /*
#include <stddef.h> #include <stddef.h>
@ -30,8 +30,8 @@ const (
EventChainDataRemoved = "chaindata.removed" EventChainDataRemoved = "chaindata.removed"
) )
// SignalEnvelope is a general signal sent upward from node to RN app // Envelope is a general signal sent upward from node to RN app
type SignalEnvelope struct { type Envelope struct {
Type string `json:"type"` Type string `json:"type"`
Event interface{} `json:"event"` Event interface{} `json:"event"`
} }
@ -47,12 +47,12 @@ type NodeNotificationHandler func(jsonEvent string)
var notificationHandler NodeNotificationHandler = TriggerDefaultNodeNotificationHandler var notificationHandler NodeNotificationHandler = TriggerDefaultNodeNotificationHandler
// SetDefaultNodeNotificationHandler sets notification handler to invoke on SendSignal // SetDefaultNodeNotificationHandler sets notification handler to invoke on Send
func SetDefaultNodeNotificationHandler(fn NodeNotificationHandler) { func SetDefaultNodeNotificationHandler(fn NodeNotificationHandler) {
notificationHandler = fn notificationHandler = fn
} }
// ReetDefaultNodeNotificationHandler sets notification handler to default one // ResetDefaultNodeNotificationHandler sets notification handler to default one
func ResetDefaultNodeNotificationHandler() { func ResetDefaultNodeNotificationHandler() {
notificationHandler = TriggerDefaultNodeNotificationHandler notificationHandler = TriggerDefaultNodeNotificationHandler
} }
@ -62,8 +62,8 @@ func TriggerDefaultNodeNotificationHandler(jsonEvent string) {
log.Info("Notification received", "event", jsonEvent) log.Info("Notification received", "event", jsonEvent)
} }
// SendSignal sends application signal (JSON, normally) upwards to application (via default notification handler) // Send sends application signal (JSON, normally) upwards to application (via default notification handler)
func SendSignal(signal SignalEnvelope) { func Send(signal Envelope) {
data, _ := json.Marshal(&signal) data, _ := json.Marshal(&signal)
C.StatusServiceSignalEvent(C.CString(string(data))) C.StatusServiceSignalEvent(C.CString(string(data)))
} }