status-go/geth/utils.go

225 lines
5.4 KiB
Go
Raw Normal View History

package geth
2016-09-15 03:08:06 +00:00
/*
#include <stddef.h>
#include <stdbool.h>
extern bool StatusServiceSignalEvent(const char *jsonEvent);
*/
import "C"
import (
"bytes"
"encoding/json"
"io"
"os"
"path"
"path/filepath"
"sync"
"time"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
)
var muPrepareTestNode sync.Mutex
const (
TestDataDir = "../.ethereumtest"
2017-02-11 01:33:02 +00:00
TestNodeSyncSeconds = 60
2017-01-26 00:39:20 +00:00
TestNodeHTTPPort = 8645
TestNodeWSPort = 8646
)
2016-09-15 03:08:06 +00:00
type NodeNotificationHandler func(jsonEvent string)
2017-01-12 17:56:36 +00:00
var notificationHandler NodeNotificationHandler = TriggerDefaultNodeNotificationHandler
2016-09-15 03:08:06 +00:00
2017-01-12 17:56:36 +00:00
// SetDefaultNodeNotificationHandler sets notification handler to invoke on SendSignal
2016-09-15 03:08:06 +00:00
func SetDefaultNodeNotificationHandler(fn NodeNotificationHandler) {
notificationHandler = fn
}
2017-01-12 17:56:36 +00:00
// TriggerDefaultNodeNotificationHandler triggers default notification handler (helpful in tests)
func TriggerDefaultNodeNotificationHandler(jsonEvent string) {
glog.V(logger.Info).Infof("notification received (default notification handler): %s\n", jsonEvent)
}
// SendSignal sends application signal (JSON, normally) upwards to application (via default notification handler)
func SendSignal(signal SignalEnvelope) {
data, _ := json.Marshal(&signal)
C.StatusServiceSignalEvent(C.CString(string(data)))
}
//export NotifyNode
func NotifyNode(jsonEvent *C.char) {
2016-09-15 03:08:06 +00:00
notificationHandler(C.GoString(jsonEvent))
}
//export TriggerTestSignal
2016-09-15 03:08:06 +00:00
func TriggerTestSignal() {
C.StatusServiceSignalEvent(C.CString(`{"answer": 42}`))
}
func CopyFile(dst, src string) error {
s, err := os.Open(src)
if err != nil {
return err
}
defer s.Close()
d, err := os.Create(dst)
if err != nil {
return err
}
defer d.Close()
if _, err := io.Copy(d, s); err != nil {
return err
}
return nil
}
// LoadFromFile is usefull for loading test data, from testdata/filename into a variable
func LoadFromFile(filename string) string {
f, err := os.Open(filename)
if err != nil {
return ""
}
buf := bytes.NewBuffer(nil)
io.Copy(buf, f)
f.Close()
return string(buf.Bytes())
}
func PrepareTestNode() (err error) {
muPrepareTestNode.Lock()
defer muPrepareTestNode.Unlock()
manager := NodeManagerInstance()
if manager.NodeInited() {
return nil
}
2017-01-12 17:56:36 +00:00
defer HaltOnPanic()
syncRequired := false
2016-09-28 08:07:30 +00:00
if _, err := os.Stat(filepath.Join(TestDataDir, "testnet")); os.IsNotExist(err) {
syncRequired = true
}
// prepare node directory
dataDir, err := PreprocessDataDir(TestDataDir)
if err != nil {
glog.V(logger.Warn).Infoln("make node failed:", err)
return err
}
// import test account (with test ether on it)
dst := filepath.Join(TestDataDir, "testnet", "keystore", "test-account.pk")
if _, err := os.Stat(dst); os.IsNotExist(err) {
err = CopyFile(dst, filepath.Join("../data", "test-account.pk"))
if err != nil {
glog.V(logger.Warn).Infof("cannot copy test account PK: %v", err)
return err
}
}
// start geth node and wait for it to initialize
2017-01-26 00:39:20 +00:00
err = CreateAndRunNode(&NodeConfig{
DataDir: dataDir,
IPCEnabled: false,
2017-01-26 00:39:20 +00:00
HTTPPort: TestNodeHTTPPort, // to avoid conflicts with running app, using different port in tests
WSEnabled: false,
2017-01-26 00:39:20 +00:00
WSPort: TestNodeWSPort, // ditto
TLSEnabled: false,
})
2017-01-12 17:56:36 +00:00
if err != nil {
panic(err)
}
manager = NodeManagerInstance()
if !manager.NodeInited() {
2016-09-15 03:08:06 +00:00
panic(ErrInvalidGethNode)
}
if service, err := manager.RPCClient(); err != nil || service == nil {
2016-09-15 03:08:06 +00:00
panic(ErrInvalidGethNode)
}
if service, err := manager.WhisperService(); err != nil || service == nil {
2016-09-15 03:08:06 +00:00
panic(ErrInvalidGethNode)
}
if service, err := manager.LightEthereumService(); err != nil || service == nil {
2016-09-15 03:08:06 +00:00
panic(ErrInvalidGethNode)
}
if syncRequired {
glog.V(logger.Warn).Infof("Sync is required, it will take %d seconds", TestNodeSyncSeconds)
time.Sleep(TestNodeSyncSeconds * time.Second) // LES syncs headers, so that we are up do date when it is done
} else {
time.Sleep(5 * time.Second)
}
return nil
}
func RemoveTestNode() {
err := os.RemoveAll(TestDataDir)
if err != nil {
glog.V(logger.Warn).Infof("could not clean up temporary datadir")
}
}
func PreprocessDataDir(dataDir string) (string, error) {
testDataDir := path.Join(dataDir, "testnet", "keystore")
if _, err := os.Stat(testDataDir); os.IsNotExist(err) {
if err := os.MkdirAll(testDataDir, 0755); err != nil {
return dataDir, ErrDataDirPreprocessingFailed
}
}
return dataDir, nil
}
// PanicAfter throws panic() after waitSeconds, unless abort channel receives notification
func PanicAfter(waitSeconds time.Duration, abort chan struct{}, desc string) {
go func() {
select {
case <-abort:
return
case <-time.After(waitSeconds):
2016-09-15 03:08:06 +00:00
panic("whatever you were doing takes toooo long: " + desc)
}
}()
}
func FromAddress(accountAddress string) common.Address {
2017-02-27 12:52:10 +00:00
from, err := ParseAccountString(accountAddress)
if err != nil {
return common.Address{}
}
return from.Address
}
func ToAddress(accountAddress string) *common.Address {
2017-02-27 12:52:10 +00:00
to, err := ParseAccountString(accountAddress)
if err != nil {
return nil
}
return &to.Address
}
2017-02-27 12:52:10 +00:00
// ParseAccountString parses hex encoded string and returns is as accounts.Account.
func ParseAccountString(account string) (accounts.Account, error) {
// valid address, convert to account
if common.IsHexAddress(account) {
return accounts.Account{Address: common.HexToAddress(account)}, nil
}
2017-02-27 12:52:10 +00:00
return accounts.Account{}, ErrInvalidAccountAddressOrKey
}