jail, params: moved into geth package, refactored

This commit is contained in:
Victor Farazdagi 2017-03-14 16:17:32 +03:00
parent 4159f97e74
commit 57fb99c4ca
15 changed files with 87 additions and 65 deletions

View File

@ -49,14 +49,11 @@ statusgo-ios-simulator-mainnet: xgo
ci: ci:
build/env.sh go test -v -cover ./geth build/env.sh go test -v -cover ./geth
build/env.sh go test -v -cover ./jail
build/env.sh go test -v -cover ./extkeys build/env.sh go test -v -cover ./extkeys
test-all: test-all:
@build/env.sh echo "mode: set" > coverage-all.out @build/env.sh echo "mode: set" > coverage-all.out
build/env.sh go test -coverprofile=coverage.out -covermode=set ./geth build/env.sh go test -coverprofile=coverage.out -covermode=set ./geth/...
@build/env.sh tail -n +2 coverage.out >> coverage-all.out
build/env.sh go test -coverprofile=coverage.out -covermode=set ./jail
@build/env.sh tail -n +2 coverage.out >> coverage-all.out @build/env.sh tail -n +2 coverage.out >> coverage-all.out
build/env.sh go test -coverprofile=coverage.out -covermode=set ./extkeys build/env.sh go test -coverprofile=coverage.out -covermode=set ./extkeys
@build/env.sh tail -n +2 coverage.out >> coverage-all.out @build/env.sh tail -n +2 coverage.out >> coverage-all.out
@ -68,12 +65,7 @@ test-all:
test: test-all test: test-all
test-geth: test-geth:
build/env.sh go test -v -coverprofile=coverage.out ./geth build/env.sh go test -v -coverprofile=coverage.out ./geth/...
@build/env.sh go tool cover -html=coverage.out -o coverage.html
@build/env.sh go tool cover -func=coverage.out
test-jail:
build/env.sh go test -v -coverprofile=coverage.out ./jail
@build/env.sh go tool cover -html=coverage.out -o coverage.html @build/env.sh go tool cover -html=coverage.out -o coverage.html
@build/env.sh go tool cover -func=coverage.out @build/env.sh go tool cover -func=coverage.out

View File

@ -8,7 +8,7 @@ import (
whisper "github.com/ethereum/go-ethereum/whisper/whisperv2" whisper "github.com/ethereum/go-ethereum/whisper/whisperv2"
"github.com/status-im/status-go/geth" "github.com/status-im/status-go/geth"
"github.com/status-im/status-go/jail" "github.com/status-im/status-go/geth/jail"
) )
//export CreateAccount //export CreateAccount

View File

@ -2,6 +2,7 @@ package jail_test
import ( import (
"encoding/json" "encoding/json"
"os"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
@ -10,12 +11,10 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/status-im/status-go/geth" "github.com/status-im/status-go/geth"
"github.com/status-im/status-go/jail" "github.com/status-im/status-go/geth/jail"
) )
const ( const (
TEST_ADDRESS = "0xadaf150b905cf5e6a778e553e15a139b6618bbb7"
TEST_ADDRESS_PASSWORD = "asdfasdf"
CHAT_ID_INIT = "CHAT_ID_INIT_TEST" CHAT_ID_INIT = "CHAT_ID_INIT_TEST"
CHAT_ID_CALL = "CHAT_ID_CALL_TEST" CHAT_ID_CALL = "CHAT_ID_CALL_TEST"
CHAT_ID_SEND = "CHAT_ID_CALL_SEND" CHAT_ID_SEND = "CHAT_ID_CALL_SEND"
@ -25,6 +24,23 @@ const (
TESTDATA_TX_SEND_JS = "testdata/tx-send/" TESTDATA_TX_SEND_JS = "testdata/tx-send/"
) )
var testConfig *geth.TestConfig
func TestMain(m *testing.M) {
// load shared test configuration
var err error
testConfig, err = geth.LoadTestConfig()
if err != nil {
panic(err)
}
// run tests
retCode := m.Run()
//time.Sleep(25 * time.Second) // to give some time to propagate txs to the rest of the network
os.Exit(retCode)
}
func TestJailUnInited(t *testing.T) { func TestJailUnInited(t *testing.T) {
errorWrapper := func(err error) string { errorWrapper := func(err error) string {
return `{"error":"` + err.Error() + `"}` return `{"error":"` + err.Error() + `"}`
@ -174,7 +190,7 @@ func TestJailRPCSend(t *testing.T) {
// internally (since we replaced `web3.send` with `jail.Send`) // internally (since we replaced `web3.send` with `jail.Send`)
// all requests to web3 are forwarded to `jail.Send` // all requests to web3 are forwarded to `jail.Send`
_, err = vm.Run(` _, err = vm.Run(`
var balance = web3.eth.getBalance("` + TEST_ADDRESS + `"); var balance = web3.eth.getBalance("` + testConfig.Account.Address + `");
var sendResult = web3.fromWei(balance, "ether") var sendResult = web3.fromWei(balance, "ether")
`) `)
if err != nil { if err != nil {
@ -199,7 +215,7 @@ func TestJailRPCSend(t *testing.T) {
return return
} }
t.Logf("Balance of %.2f ETH found on '%s' account", balance, TEST_ADDRESS) t.Logf("Balance of %.2f ETH found on '%s' account", balance, testConfig.Account.Address)
} }
func TestJailSendQueuedTransaction(t *testing.T) { func TestJailSendQueuedTransaction(t *testing.T) {
@ -210,13 +226,13 @@ func TestJailSendQueuedTransaction(t *testing.T) {
} }
// log into account from which transactions will be sent // log into account from which transactions will be sent
if err := geth.SelectAccount(TEST_ADDRESS, TEST_ADDRESS_PASSWORD); err != nil { if err := geth.SelectAccount(testConfig.Account.Address, testConfig.Account.Password); err != nil {
t.Errorf("cannot select account: %v", TEST_ADDRESS) t.Errorf("cannot select account: %v", testConfig.Account.Address)
return return
} }
txParams := `{ txParams := `{
"from": "` + TEST_ADDRESS + `", "from": "` + testConfig.Account.Address + `",
"to": "0xf82da7547534045b4e00442bc89e16186cf8c272", "to": "0xf82da7547534045b4e00442bc89e16186cf8c272",
"value": "0.000001" "value": "0.000001"
}` }`
@ -251,11 +267,10 @@ func TestJailSendQueuedTransaction(t *testing.T) {
return return
} }
} }
t.Logf("Transaction queued (will be completed in 5 secs): {id: %s}\n", event["id"].(string)) t.Logf("Transaction queued (will be completed shortly): {id: %s}\n", event["id"].(string))
time.Sleep(5 * time.Second)
var txHash common.Hash var txHash common.Hash
if txHash, err = geth.CompleteTransaction(event["id"].(string), TEST_ADDRESS_PASSWORD); err != nil { if txHash, err = geth.CompleteTransaction(event["id"].(string), testConfig.Account.Password); err != nil {
t.Errorf("cannot complete queued transation[%v]: %v", event["id"], err) t.Errorf("cannot complete queued transation[%v]: %v", event["id"], err)
} else { } else {
t.Logf("Transaction complete: https://testnet.etherscan.io/tx/%s", txHash.Hex()) t.Logf("Transaction complete: https://testnet.etherscan.io/tx/%s", txHash.Hex())
@ -293,7 +308,7 @@ func TestJailSendQueuedTransaction(t *testing.T) {
}, },
{ {
`["commands", "getBalance"]`, `["commands", "getBalance"]`,
`{"address": "` + TEST_ADDRESS + `"}`, `{"address": "` + testConfig.Account.Address + `"}`,
`{"result": {"balance":42}}`, `{"result": {"balance":42}}`,
}, },
}, },
@ -311,7 +326,7 @@ func TestJailSendQueuedTransaction(t *testing.T) {
}, },
{ {
`["commands", "getBalance"]`, `["commands", "getBalance"]`,
`{"address": "` + TEST_ADDRESS + `"}`, `{"address": "` + testConfig.Account.Address + `"}`,
`{"result": {"context":{},"result":{"balance":42}}}`, // note emtpy (but present) context! `{"result": {"context":{},"result":{"balance":42}}}`, // note emtpy (but present) context!
}, },
}, },
@ -329,7 +344,7 @@ func TestJailSendQueuedTransaction(t *testing.T) {
}, },
{ {
`["commands", "getBalance"]`, `["commands", "getBalance"]`,
`{"address": "` + TEST_ADDRESS + `"}`, `{"address": "` + testConfig.Account.Address + `"}`,
`{"result": {"balance":42}}`, // note emtpy context! `{"result": {"balance":42}}`, // note emtpy context!
}, },
}, },
@ -347,7 +362,7 @@ func TestJailSendQueuedTransaction(t *testing.T) {
}, },
{ {
`["commands", "getBalance"]`, `["commands", "getBalance"]`,
`{"address": "` + TEST_ADDRESS + `"}`, `{"address": "` + testConfig.Account.Address + `"}`,
`{"result": {"context":{"message_id":"42"},"result":{"balance":42}}}`, // message id in context, but default one is used! `{"result": {"context":{"message_id":"42"},"result":{"balance":42}}}`, // message id in context, but default one is used!
}, },
}, },
@ -596,15 +611,14 @@ func TestContractDeployment(t *testing.T) {
if envelope.Type == geth.EventTransactionQueued { if envelope.Type == geth.EventTransactionQueued {
event := envelope.Event.(map[string]interface{}) event := envelope.Event.(map[string]interface{})
t.Logf("Transaction queued (will be completed in 5 secs): {id: %s}\n", event["id"].(string)) t.Logf("Transaction queued (will be completed shortly): {id: %s}\n", event["id"].(string))
time.Sleep(5 * time.Second)
if err := geth.SelectAccount(TEST_ADDRESS, TEST_ADDRESS_PASSWORD); err != nil { if err := geth.SelectAccount(testConfig.Account.Address, testConfig.Account.Password); err != nil {
t.Errorf("cannot select account: %v", TEST_ADDRESS) t.Errorf("cannot select account: %v", testConfig.Account.Address)
return return
} }
if txHash, err = geth.CompleteTransaction(event["id"].(string), TEST_ADDRESS_PASSWORD); err != nil { if txHash, err = geth.CompleteTransaction(event["id"].(string), testConfig.Account.Password); err != nil {
t.Errorf("cannot complete queued transation[%v]: %v", event["id"], err) t.Errorf("cannot complete queued transation[%v]: %v", event["id"], err)
return return
} else { } else {
@ -620,7 +634,7 @@ func TestContractDeployment(t *testing.T) {
var testContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"a","type":"int256"}],"name":"double","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function"}]); var testContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"a","type":"int256"}],"name":"double","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function"}]);
var test = testContract.new( var test = testContract.new(
{ {
from: '` + TEST_ADDRESS + `', from: '` + testConfig.Account.Address + `',
data: '0x6060604052341561000c57fe5b5b60a58061001b6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636ffa1caa14603a575bfe5b3415604157fe5b60556004808035906020019091905050606b565b6040518082815260200191505060405180910390f35b60008160020290505b9190505600a165627a7a72305820ccdadd737e4ac7039963b54cee5e5afb25fa859a275252bdcf06f653155228210029', data: '0x6060604052341561000c57fe5b5b60a58061001b6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636ffa1caa14603a575bfe5b3415604157fe5b60556004808035906020019091905050606b565b6040518082815260200191505060405180910390f35b60008160020290505b9190505600a165627a7a72305820ccdadd737e4ac7039963b54cee5e5afb25fa859a275252bdcf06f653155228210029',
gas: '` + strconv.Itoa(geth.DefaultGas) + `' gas: '` + strconv.Itoa(geth.DefaultGas) + `'
}, function (e, contract){ }, function (e, contract){
@ -689,12 +703,12 @@ func TestGasEstimation(t *testing.T) {
t.Logf("Transaction queued (will be completed immediately): {id: %s}\n", event["id"].(string)) t.Logf("Transaction queued (will be completed immediately): {id: %s}\n", event["id"].(string))
if err := geth.SelectAccount(TEST_ADDRESS, TEST_ADDRESS_PASSWORD); err != nil { if err := geth.SelectAccount(testConfig.Account.Address, testConfig.Account.Password); err != nil {
t.Errorf("cannot select account: %v", TEST_ADDRESS) t.Errorf("cannot select account: %v", testConfig.Account.Address)
return return
} }
if txHash, err = geth.CompleteTransaction(event["id"].(string), TEST_ADDRESS_PASSWORD); err != nil { if txHash, err = geth.CompleteTransaction(event["id"].(string), testConfig.Account.Password); err != nil {
t.Errorf("cannot complete queued transation[%v]: %v", event["id"], err) t.Errorf("cannot complete queued transation[%v]: %v", event["id"], err)
return return
} else { } else {
@ -710,7 +724,7 @@ func TestGasEstimation(t *testing.T) {
var testContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"a","type":"int256"}],"name":"double","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function"}]); var testContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"a","type":"int256"}],"name":"double","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function"}]);
var test = testContract.new( var test = testContract.new(
{ {
from: '` + TEST_ADDRESS + `', from: '` + testConfig.Account.Address + `',
data: '0x6060604052341561000c57fe5b5b60a58061001b6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636ffa1caa14603a575bfe5b3415604157fe5b60556004808035906020019091905050606b565b6040518082815260200191505060405180910390f35b60008160020290505b9190505600a165627a7a72305820ccdadd737e4ac7039963b54cee5e5afb25fa859a275252bdcf06f653155228210029', data: '0x6060604052341561000c57fe5b5b60a58061001b6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636ffa1caa14603a575bfe5b3415604157fe5b60556004808035906020019091905050606b565b6040518082815260200191505060405180910390f35b60008160020290505b9190505600a165627a7a72305820ccdadd737e4ac7039963b54cee5e5afb25fa859a275252bdcf06f653155228210029',
}, function (e, contract){ }, function (e, contract){
if (!e) { if (!e) {

View File

@ -19,7 +19,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv2" whisper "github.com/ethereum/go-ethereum/whisper/whisperv2"
"github.com/status-im/status-go/params" "github.com/status-im/status-go/geth/params"
) )
// SelectedExtKey is a container for currently selected (logged in) account // SelectedExtKey is a container for currently selected (logged in) account

View File

@ -1,8 +1,7 @@
package params package params
// MainnetBootnodes are the enode URLs of the P2P bootstrap nodes running on the main Ethereum network. // MainnetBootnodes are the enode URLs of the P2P bootstrap nodes running on the main Ethereum network.
var MainnetBootnodes = []string{ var MainnetBootnodes = []string{}
}
// TestnetBootnodes are the enode URLs of the P2P bootstrap nodes running on the Ropsten test network. // TestnetBootnodes are the enode URLs of the P2P bootstrap nodes running on the Ropsten test network.
var TestnetBootnodes = []string{ var TestnetBootnodes = []string{
@ -39,5 +38,4 @@ var TestnetBootnodes = []string{
// DiscoveryV5Bootnodes are the enode URLs of the P2P bootstrap nodes for the // DiscoveryV5Bootnodes are the enode URLs of the P2P bootstrap nodes for the
// experimental RLPx v5 topic-discovery network. // experimental RLPx v5 topic-discovery network.
var DiscoveryV5Bootnodes = []string{ var DiscoveryV5Bootnodes = []string{}
}

View File

@ -11,8 +11,8 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"os" "os"
"path"
"path/filepath" "path/filepath"
"runtime"
"sync" "sync"
"time" "time"
@ -22,15 +22,20 @@ import (
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
) )
var muPrepareTestNode sync.Mutex
const ( const (
TestDataDir = "../.ethereumtest"
TestNodeSyncSeconds = 60 TestNodeSyncSeconds = 60
TestNodeHTTPPort = 8645 TestNodeHTTPPort = 8645
TestNodeWSPort = 8646 TestNodeWSPort = 8646
) )
var (
muPrepareTestNode sync.Mutex
_, basePath, _, _ = runtime.Caller(0)
RootDir = filepath.Join(filepath.Dir(basePath), "..")
DataDir = filepath.Join(RootDir, "data")
TestDataDir = filepath.Join(RootDir, ".ethereumtest")
)
type NodeNotificationHandler func(jsonEvent string) type NodeNotificationHandler func(jsonEvent string)
var notificationHandler NodeNotificationHandler = TriggerDefaultNodeNotificationHandler var notificationHandler NodeNotificationHandler = TriggerDefaultNodeNotificationHandler
@ -61,6 +66,31 @@ func TriggerTestSignal() {
C.StatusServiceSignalEvent(C.CString(`{"answer": 42}`)) C.StatusServiceSignalEvent(C.CString(`{"answer": 42}`))
} }
// TestConfig contains shared (among different test packages) parameters
type TestConfig struct {
Node struct {
SyncSeconds int
HTTPPort int
WSPort int
}
Account struct {
Address string
Password string
}
}
// LoadTestConfig loads test configuration values from disk
func LoadTestConfig() (*TestConfig, error) {
var testConfig TestConfig
configData := LoadFromFile(filepath.Join(DataDir, "test-data.json"))
if err := json.Unmarshal([]byte(configData), &testConfig); err != nil {
return nil, err
}
return &testConfig, nil
}
func CopyFile(dst, src string) error { func CopyFile(dst, src string) error {
s, err := os.Open(src) s, err := os.Open(src)
if err != nil { if err != nil {
@ -112,8 +142,7 @@ func PrepareTestNode() (err error) {
} }
// prepare node directory // prepare node directory
dataDir, err := PreprocessDataDir(TestDataDir) if err := os.MkdirAll(filepath.Join(TestDataDir, "testnet", "keystore"), os.ModePerm); err != nil {
if err != nil {
glog.V(logger.Warn).Infoln("make node failed:", err) glog.V(logger.Warn).Infoln("make node failed:", err)
return err return err
} }
@ -121,7 +150,7 @@ func PrepareTestNode() (err error) {
// import test account (with test ether on it) // import test account (with test ether on it)
dst := filepath.Join(TestDataDir, "testnet", "keystore", "test-account.pk") dst := filepath.Join(TestDataDir, "testnet", "keystore", "test-account.pk")
if _, err := os.Stat(dst); os.IsNotExist(err) { if _, err := os.Stat(dst); os.IsNotExist(err) {
err = CopyFile(dst, filepath.Join("../data", "test-account.pk")) err = CopyFile(dst, filepath.Join(RootDir, "data", "test-account.pk"))
if err != nil { if err != nil {
glog.V(logger.Warn).Infof("cannot copy test account PK: %v", err) glog.V(logger.Warn).Infof("cannot copy test account PK: %v", err)
return err return err
@ -130,7 +159,7 @@ func PrepareTestNode() (err error) {
// start geth node and wait for it to initialize // start geth node and wait for it to initialize
err = CreateAndRunNode(&NodeConfig{ err = CreateAndRunNode(&NodeConfig{
DataDir: dataDir, DataDir: TestDataDir,
IPCEnabled: false, IPCEnabled: false,
HTTPPort: TestNodeHTTPPort, // to avoid conflicts with running app, using different port in tests HTTPPort: TestNodeHTTPPort, // to avoid conflicts with running app, using different port in tests
WSEnabled: false, WSEnabled: false,
@ -172,17 +201,6 @@ func RemoveTestNode() {
} }
} }
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 // PanicAfter throws panic() after waitSeconds, unless abort channel receives notification
func PanicAfter(waitSeconds time.Duration, abort chan struct{}, desc string) { func PanicAfter(waitSeconds time.Duration, abort chan struct{}, desc string) {
go func() { go func() {