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

View File

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

View File

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

View File

@ -15,6 +15,7 @@ import (
"github.com/status-im/status-go/geth/log"
"github.com/status-im/status-go/geth/node"
"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/stretchr/testify/suite"
)
@ -277,8 +278,8 @@ func (s *BackendTestSuite) TestCallRPCSendTransaction() {
transactionCompleted := make(chan struct{})
var txHash gethcommon.Hash
node.SetDefaultNodeNotificationHandler(func(rawSignal string) {
var signal node.SignalEnvelope
signal.SetDefaultNodeNotificationHandler(func(rawSignal string) {
var signal signal.Envelope
err := json.Unmarshal([]byte(rawSignal), &signal)
s.NoError(err)
@ -335,8 +336,8 @@ func (s *BackendTestSuite) TestCallRPCSendTransactionUpstream() {
transactionCompleted := make(chan struct{})
var txHash gethcommon.Hash
node.SetDefaultNodeNotificationHandler(func(rawSignal string) {
var signal node.SignalEnvelope
signal.SetDefaultNodeNotificationHandler(func(rawSignal string) {
var signal signal.Envelope
err := json.Unmarshal([]byte(rawSignal), &signal)
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/node"
"github.com/status-im/status-go/geth/params"
"github.com/status-im/status-go/geth/signal"
. "github.com/status-im/status-go/geth/testing"
)
@ -34,8 +35,8 @@ func (s *BackendTestSuite) TestSendContractTx() {
// replace transaction notification handler
var txHash gethcommon.Hash
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { // nolint :dupl
var envelope node.SignalEnvelope
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) { // nolint :dupl
var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
@ -128,8 +129,8 @@ func (s *BackendTestSuite) TestSendEtherTx() {
// replace transaction notification handler
var txHash = gethcommon.Hash{}
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { // nolint: dupl
var envelope node.SignalEnvelope
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) { // nolint: dupl
var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
@ -208,8 +209,8 @@ func (s *BackendTestSuite) TestSendEtherTxUpstream() {
// replace transaction notification handler
var txHash = gethcommon.Hash{}
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) { // nolint: dupl
var envelope node.SignalEnvelope
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) { // nolint: dupl
var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err, "cannot unmarshal JSON: %s", jsonEvent)
@ -268,8 +269,8 @@ func (s *BackendTestSuite) TestDoubleCompleteQueuedTransactions() {
// replace transaction notification handler
txFailedEventCalled := false
txHash := gethcommon.Hash{}
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope node.SignalEnvelope
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
@ -350,8 +351,8 @@ func (s *BackendTestSuite) TestDiscardQueuedTransaction() {
// replace transaction notification handler
txFailedEventCalled := false
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope node.SignalEnvelope
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
@ -432,8 +433,8 @@ func (s *BackendTestSuite) TestCompleteMultipleQueuedTransactions() {
allTestTxCompleted := make(chan struct{})
// replace transaction notification handler
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope node.SignalEnvelope
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
@ -533,8 +534,8 @@ func (s *BackendTestSuite) TestDiscardMultipleQueuedTransactions() {
// replace transaction notification handler
txFailedEventCallCount := 0
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope node.SignalEnvelope
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err)
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))
// replace transaction notification handler
node.SetDefaultNodeNotificationHandler(func(string) {})
signal.SetDefaultNodeNotificationHandler(func(string) {})
// try completing non-existing transaction
_, err := s.backend.CompleteTransaction("some-bad-transaction-id", TestConfig.Account1.Password)

View File

@ -6,13 +6,13 @@ import (
"strings"
"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
// for the underline otto vm.
func Write(fn otto.FunctionCall, w io.Writer, consoleEventName string) otto.Value {
node.SendSignal(node.SignalEnvelope{
signal.Send(signal.Envelope{
Type: consoleEventName,
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/jail/console"
"github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/signal"
)
// signals
@ -130,7 +131,7 @@ func makeSignalHandler(chatID string) func(call otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value {
message := call.Argument(0).String()
node.SendSignal(node.SignalEnvelope{
signal.Send(signal.Envelope{
Type: EventSignal,
Event: SignalEvent{
ChatID: chatID,

View File

@ -10,6 +10,7 @@ import (
"github.com/status-im/status-go/geth/jail"
"github.com/status-im/status-go/geth/node"
"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/static"
"github.com/stretchr/testify/suite"
@ -232,8 +233,8 @@ func (s *JailTestSuite) TestEventSignal() {
opCompletedSuccessfully := make(chan struct{}, 1)
// replace transaction notification handler
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope node.SignalEnvelope
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope)
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/params"
"github.com/status-im/status-go/geth/rpc"
"github.com/status-im/status-go/geth/signal"
)
// errors
@ -82,9 +83,9 @@ func (m *NodeManager) startNode(config *params.NodeConfig) (<-chan struct{}, err
m.Lock()
m.nodeStarted = nil
m.Unlock()
SendSignal(SignalEnvelope{
Type: EventNodeCrashed,
Event: NodeCrashEvent{
signal.Send(signal.Envelope{
Type: signal.EventNodeCrashed,
Event: signal.NodeCrashEvent{
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 {
log.Error("Init RPC client failed:", "error", err)
m.Unlock()
SendSignal(SignalEnvelope{
Type: EventNodeCrashed,
Event: NodeCrashEvent{
signal.Send(signal.Envelope{
Type: signal.EventNodeCrashed,
Event: signal.NodeCrashEvent{
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
close(m.nodeStarted)
SendSignal(SignalEnvelope{
Type: EventNodeStarted,
signal.Send(signal.Envelope{
Type: signal.EventNodeStarted,
Event: struct{}{},
})
@ -179,8 +180,8 @@ func (m *NodeManager) stopNode() (<-chan struct{}, error) {
log.Info("Node manager resets node params")
// notify application that it can send more requests now
SendSignal(SignalEnvelope{
Type: EventNodeStopped,
signal.Send(signal.Envelope{
Type: signal.EventNodeStopped,
Event: struct{}{},
})
log.Info("Node manager notifed app, that node has stopped")
@ -317,8 +318,8 @@ func (m *NodeManager) resetChainData() (<-chan struct{}, error) {
return nil, err
}
// send signal up to native app
SendSignal(SignalEnvelope{
Type: EventChainDataRemoved,
signal.Send(signal.Envelope{
Type: signal.EventChainDataRemoved,
Event: struct{}{},
})
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/node"
"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/stretchr/testify/suite"
)
@ -445,13 +446,13 @@ func (s *ManagerTestSuite) TestNodeStartCrash() {
// let's listen for node.crashed signal
signalReceived := false
node.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
log.Info("Notification Received", "event", jsonEvent)
var envelope node.SignalEnvelope
var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
if envelope.Type == node.EventNodeCrashed {
if envelope.Type == signal.EventNodeCrashed {
signalReceived = true
}
})
@ -478,5 +479,5 @@ func (s *ManagerTestSuite) TestNodeStartCrash() {
// cleanup
s.NodeManager.StopNode()
node.ResetDefaultNodeNotificationHandler()
signal.ResetDefaultNodeNotificationHandler()
}

View File

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

View File

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

View File

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