2015-07-07 03:54:22 +03:00
// Copyright 2015 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
2015-07-22 19:48:40 +03:00
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2015-07-07 03:54:22 +03:00
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
2015-07-22 19:48:40 +03:00
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
2015-07-07 03:54:22 +03:00
2015-03-15 08:43:48 +02:00
package main
import (
"fmt"
2015-03-25 16:58:52 +02:00
"io/ioutil"
2015-06-23 17:48:33 +03:00
"math/big"
2015-03-15 08:43:48 +02:00
"os"
2015-04-21 20:08:47 +03:00
"path/filepath"
2015-04-23 01:11:11 +03:00
"regexp"
"runtime"
"strconv"
2015-03-15 08:43:48 +02:00
"testing"
2015-06-23 17:48:33 +03:00
"time"
2015-03-15 08:43:48 +02:00
"github.com/ethereum/go-ethereum/accounts"
2015-04-23 01:11:11 +03:00
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/compiler"
2015-10-27 00:24:09 +03:00
"github.com/ethereum/go-ethereum/common/httpclient"
2015-04-23 01:11:11 +03:00
"github.com/ethereum/go-ethereum/core"
2015-03-15 08:43:48 +02:00
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth"
2015-07-10 15:29:40 +03:00
"github.com/ethereum/go-ethereum/ethdb"
2015-11-17 18:33:25 +02:00
"github.com/ethereum/go-ethereum/node"
2015-03-15 08:43:48 +02:00
)
2015-04-23 01:11:11 +03:00
const (
testSolcPath = ""
2016-02-05 15:08:48 +02:00
solcVersion = "0.9.23"
2016-03-02 14:57:15 +02:00
testAddress = "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
testBalance = "10000000000000000000"
2016-02-05 15:08:48 +02:00
// of empty string
2015-05-08 20:37:35 +03:00
testHash = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
2015-04-23 01:11:11 +03:00
)
var (
2016-03-02 14:57:15 +02:00
versionRE = regexp . MustCompile ( strconv . Quote ( ` "compilerVersion":" ` + solcVersion + ` " ` ) )
testNodeKey , _ = crypto . HexToECDSA ( "4b50fa71f5c3eeb8fdc452224b2395af2fcc3d125e06c32c82e048c0559db03f" )
testAccount , _ = crypto . HexToECDSA ( "e6fab74a43941f82d89cb7faa408e227cdad3153c4720e540e855c19b15e6674" )
testGenesis = ` { " ` + testAddress [ 2 : ] + ` ": { "balance": " ` + testBalance + ` "}} `
2015-04-23 01:11:11 +03:00
)
type testjethre struct {
* jsre
lastConfirm string
2015-10-27 00:24:09 +03:00
client * httpclient . HTTPClient
2015-04-23 01:11:11 +03:00
}
2015-03-15 08:43:48 +02:00
2015-12-16 11:58:01 +02:00
// Temporary disabled while natspec hasn't been migrated
//func (self *testjethre) ConfirmTransaction(tx string) bool {
// var ethereum *eth.Ethereum
// self.stack.Service(ðereum)
//
// if ethereum.NatSpec {
// self.lastConfirm = natspec.GetNotice(self.xeth, tx, self.client)
// }
// return true
//}
2015-04-23 01:11:11 +03:00
2015-11-17 18:33:25 +02:00
func testJEthRE ( t * testing . T ) ( string , * testjethre , * node . Node ) {
2015-06-23 17:48:33 +03:00
return testREPL ( t , nil )
}
2015-11-17 18:33:25 +02:00
func testREPL ( t * testing . T , config func ( * eth . Config ) ) ( string , * testjethre , * node . Node ) {
2015-04-22 11:59:27 +03:00
tmp , err := ioutil . TempDir ( "" , "geth-test" )
2015-03-15 08:43:48 +02:00
if err != nil {
2015-04-22 11:59:27 +03:00
t . Fatal ( err )
2015-03-15 08:43:48 +02:00
}
2015-11-17 18:33:25 +02:00
// Create a networkless protocol stack
2016-03-21 15:05:22 +02:00
stack , err := node . New ( & node . Config { DataDir : tmp , PrivateKey : testNodeKey , Name : "test" , NoDiscovery : true } )
2015-11-17 18:33:25 +02:00
if err != nil {
t . Fatalf ( "failed to create node: %v" , err )
}
// Initialize and register the Ethereum protocol
2016-03-02 14:57:15 +02:00
accman := accounts . NewPlaintextManager ( filepath . Join ( tmp , "keystore" ) )
2015-07-10 15:29:40 +03:00
db , _ := ethdb . NewMemDatabase ( )
2015-08-17 15:01:41 +03:00
core . WriteGenesisBlockForTesting ( db , core . GenesisAccount { common . HexToAddress ( testAddress ) , common . String2Big ( testBalance ) } )
2015-11-17 18:33:25 +02:00
ethConf := & eth . Config {
2016-03-02 00:32:43 +02:00
ChainConfig : & core . ChainConfig { HomesteadBlock : new ( big . Int ) } ,
2015-11-17 18:33:25 +02:00
TestGenesisState : db ,
AccountManager : accman ,
DocRoot : "/" ,
SolcPath : testSolcPath ,
PowTest : true ,
2015-06-23 17:48:33 +03:00
}
if config != nil {
2015-11-17 18:33:25 +02:00
config ( ethConf )
2015-06-23 17:48:33 +03:00
}
2015-12-16 11:58:01 +02:00
if err := stack . Register ( func ( ctx * node . ServiceContext ) ( node . Service , error ) {
return eth . New ( ctx , ethConf )
} ) ; err != nil {
2015-11-17 18:33:25 +02:00
t . Fatalf ( "failed to register ethereum protocol: %v" , err )
2015-03-15 08:43:48 +02:00
}
2015-11-17 18:33:25 +02:00
// Initialize all the keys for testing
2016-03-02 14:57:15 +02:00
a , err := accman . ImportECDSA ( testAccount , "" )
2015-04-23 01:11:11 +03:00
if err != nil {
t . Fatal ( err )
}
2016-03-03 02:09:16 +02:00
if err := accman . Unlock ( a , "" ) ; err != nil {
2015-04-23 01:11:11 +03:00
t . Fatal ( err )
}
2015-11-17 18:33:25 +02:00
// Start the node and assemble the REPL tester
if err := stack . Start ( ) ; err != nil {
t . Fatalf ( "failed to start test stack: %v" , err )
}
var ethereum * eth . Ethereum
2015-11-26 18:35:44 +02:00
stack . Service ( & ethereum )
2015-04-23 01:11:11 +03:00
2015-05-12 15:24:11 +03:00
assetPath := filepath . Join ( os . Getenv ( "GOPATH" ) , "src" , "github.com" , "ethereum" , "go-ethereum" , "cmd" , "mist" , "assets" , "ext" )
2016-02-09 14:10:40 +02:00
client , err := stack . Attach ( )
2016-02-05 15:08:48 +02:00
if err != nil {
t . Fatalf ( "failed to attach to node: %v" , err )
}
2015-10-27 00:24:09 +03:00
tf := & testjethre { client : ethereum . HTTPClient ( ) }
2015-12-16 11:58:01 +02:00
repl := newJSRE ( stack , assetPath , "" , client , false )
2015-04-23 01:11:11 +03:00
tf . jsre = repl
2015-11-17 18:33:25 +02:00
return tmp , tf , stack
2015-03-15 08:43:48 +02:00
}
func TestNodeInfo ( t * testing . T ) {
2015-06-23 17:48:33 +03:00
t . Skip ( "broken after p2p update" )
2015-04-23 01:11:11 +03:00
tmp , repl , ethereum := testJEthRE ( t )
2015-03-15 08:43:48 +02:00
defer ethereum . Stop ( )
2015-04-23 01:11:11 +03:00
defer os . RemoveAll ( tmp )
2015-07-03 00:58:00 +03:00
2015-05-26 19:35:31 +03:00
want := ` { "DiscPort":0,"IP":"0.0.0.0","ListenAddr":"","Name":"test","NodeID":"4cb2fc32924e94277bf94b5e4c983beedb2eabd5a0bc941db32202735c6625d020ca14a5963d1738af43b6ac0a711d61b1a06de931a499fe2aa0b1a132a902b5","NodeUrl":"enode://4cb2fc32924e94277bf94b5e4c983beedb2eabd5a0bc941db32202735c6625d020ca14a5963d1738af43b6ac0a711d61b1a06de931a499fe2aa0b1a132a902b5@0.0.0.0:0","TCPPort":0,"Td":"131072"} `
2015-06-22 09:14:39 +03:00
checkEvalJSON ( t , repl , ` admin.nodeInfo ` , want )
2015-03-15 08:43:48 +02:00
}
func TestAccounts ( t * testing . T ) {
2015-11-17 18:33:25 +02:00
tmp , repl , node := testJEthRE ( t )
defer node . Stop ( )
2015-04-23 01:11:11 +03:00
defer os . RemoveAll ( tmp )
2015-03-15 08:43:48 +02:00
2016-02-05 15:08:48 +02:00
checkEvalJSON ( t , repl , ` eth.accounts ` , ` [" ` + testAddress + ` "] ` )
checkEvalJSON ( t , repl , ` eth.coinbase ` , ` " ` + testAddress + ` " ` )
2015-10-15 17:07:19 +03:00
val , err := repl . re . Run ( ` jeth.newAccount("password") ` )
2015-03-15 08:43:48 +02:00
if err != nil {
t . Errorf ( "expected no error, got %v" , err )
}
2015-04-22 11:59:27 +03:00
addr := val . String ( )
if ! regexp . MustCompile ( ` 0x[0-9a-f] { 40} ` ) . MatchString ( addr ) {
t . Errorf ( "address not hex: %q" , addr )
2015-03-15 08:43:48 +02:00
}
2016-02-05 15:08:48 +02:00
checkEvalJSON ( t , repl , ` eth.accounts ` , ` [" ` + testAddress + ` "," ` + addr + ` "] ` )
2015-07-27 11:50:29 +03:00
2015-03-15 08:43:48 +02:00
}
func TestBlockChain ( t * testing . T ) {
2015-11-17 18:33:25 +02:00
tmp , repl , node := testJEthRE ( t )
defer node . Stop ( )
2015-04-23 01:11:11 +03:00
defer os . RemoveAll ( tmp )
2015-04-22 11:59:27 +03:00
// get current block dump before export/import.
2015-06-22 09:14:39 +03:00
val , err := repl . re . Run ( "JSON.stringify(debug.dumpBlock(eth.blockNumber))" )
2015-03-15 08:43:48 +02:00
if err != nil {
t . Errorf ( "expected no error, got %v" , err )
}
2015-04-22 11:59:27 +03:00
beforeExport := val . String ( )
2015-03-15 08:43:48 +02:00
2015-04-22 11:59:27 +03:00
// do the export
2015-04-23 01:11:11 +03:00
extmp , err := ioutil . TempDir ( "" , "geth-test-export" )
2015-03-15 08:43:48 +02:00
if err != nil {
2015-04-22 11:59:27 +03:00
t . Fatal ( err )
2015-03-15 08:43:48 +02:00
}
2015-04-23 01:11:11 +03:00
defer os . RemoveAll ( extmp )
tmpfile := filepath . Join ( extmp , "export.chain" )
2015-04-22 11:59:27 +03:00
tmpfileq := strconv . Quote ( tmpfile )
2015-03-15 08:43:48 +02:00
2015-11-17 18:33:25 +02:00
var ethereum * eth . Ethereum
2015-11-26 18:35:44 +02:00
node . Service ( & ethereum )
2015-08-31 18:09:50 +03:00
ethereum . BlockChain ( ) . Reset ( )
2015-05-18 17:04:10 +03:00
2016-02-05 15:08:48 +02:00
checkEvalJSON ( t , repl , ` admin.exportChain( ` + tmpfileq + ` ) ` , ` true ` )
2015-04-22 11:59:27 +03:00
if _ , err := os . Stat ( tmpfile ) ; err != nil {
t . Fatal ( err )
2015-03-15 08:43:48 +02:00
}
2015-04-22 11:59:27 +03:00
// check import, verify that dumpBlock gives the same result.
2016-02-05 15:08:48 +02:00
checkEvalJSON ( t , repl , ` admin.importChain( ` + tmpfileq + ` ) ` , ` true ` )
2015-06-22 09:14:39 +03:00
checkEvalJSON ( t , repl , ` debug.dumpBlock(eth.blockNumber) ` , beforeExport )
2015-03-15 08:43:48 +02:00
}
func TestMining ( t * testing . T ) {
2015-11-17 18:33:25 +02:00
tmp , repl , node := testJEthRE ( t )
defer node . Stop ( )
2015-04-23 01:11:11 +03:00
defer os . RemoveAll ( tmp )
2015-04-22 11:59:27 +03:00
checkEvalJSON ( t , repl , ` eth.mining ` , ` false ` )
2015-03-15 08:43:48 +02:00
}
func TestRPC ( t * testing . T ) {
2015-11-17 18:33:25 +02:00
tmp , repl , node := testJEthRE ( t )
defer node . Stop ( )
2015-04-23 01:11:11 +03:00
defer os . RemoveAll ( tmp )
2015-03-15 08:43:48 +02:00
2015-06-22 13:47:32 +03:00
checkEvalJSON ( t , repl , ` admin.startRPC("127.0.0.1", 5004, "*", "web3,eth,net") ` , ` true ` )
2015-04-22 11:59:27 +03:00
}
2015-04-23 01:11:11 +03:00
func TestCheckTestAccountBalance ( t * testing . T ) {
2015-06-08 13:12:13 +03:00
t . Skip ( ) // i don't think it tests the correct behaviour here. it's actually testing
// internals which shouldn't be tested. This now fails because of a change in the core
// and i have no means to fix this, sorry - @obscuren
2015-11-17 18:33:25 +02:00
tmp , repl , node := testJEthRE ( t )
defer node . Stop ( )
2015-04-23 01:11:11 +03:00
defer os . RemoveAll ( tmp )
repl . re . Run ( ` primary = " ` + testAddress + ` " ` )
2016-02-05 15:08:48 +02:00
checkEvalJSON ( t , repl , ` eth.getBalance(primary) ` , ` " ` + testBalance + ` " ` )
2015-04-23 01:11:11 +03:00
}
2015-05-08 20:37:35 +03:00
func TestSignature ( t * testing . T ) {
2015-11-17 18:33:25 +02:00
tmp , repl , node := testJEthRE ( t )
defer node . Stop ( )
2015-05-08 20:37:35 +03:00
defer os . RemoveAll ( tmp )
2015-05-20 06:38:20 +03:00
val , err := repl . re . Run ( ` eth.sign(" ` + testAddress + ` ", " ` + testHash + ` ") ` )
2015-05-08 20:37:35 +03:00
// This is a very preliminary test, lacking actual signature verification
if err != nil {
2015-05-20 06:11:48 +03:00
t . Errorf ( "Error running js: %v" , err )
2015-05-08 20:37:35 +03:00
return
}
output := val . String ( )
t . Logf ( "Output: %v" , output )
regex := regexp . MustCompile ( ` ^0x[0-9a-f] { 130}$ ` )
if ! regex . MatchString ( output ) {
t . Errorf ( "Signature is not 65 bytes represented in hexadecimal." )
return
}
}
2015-04-23 01:11:11 +03:00
func TestContract ( t * testing . T ) {
2015-07-05 21:19:42 +03:00
t . Skip ( "contract testing is implemented with mining in ethash test mode. This takes about 7seconds to run. Unskip and run on demand" )
2015-07-05 00:12:14 +03:00
coinbase := common . HexToAddress ( testAddress )
tmp , repl , ethereum := testREPL ( t , func ( conf * eth . Config ) {
2015-07-07 13:18:05 +03:00
conf . Etherbase = coinbase
2015-07-05 00:12:14 +03:00
conf . PowTest = true
} )
2015-04-23 01:11:11 +03:00
if err := ethereum . Start ( ) ; err != nil {
t . Errorf ( "error starting ethereum: %v" , err )
return
}
defer ethereum . Stop ( )
defer os . RemoveAll ( tmp )
2015-12-16 11:58:01 +02:00
// Temporary disabled while registrar isn't migrated
//reg := registrar.New(repl.xeth)
//_, err := reg.SetGlobalRegistrar("", coinbase)
//if err != nil {
// t.Errorf("error setting HashReg: %v", err)
//}
//_, err = reg.SetHashReg("", coinbase)
//if err != nil {
// t.Errorf("error setting HashReg: %v", err)
//}
//_, err = reg.SetUrlHint("", coinbase)
//if err != nil {
// t.Errorf("error setting HashReg: %v", err)
//}
2015-07-07 08:00:58 +03:00
/ * TODO :
* lookup receipt and contract addresses by tx hash
* name registration for HashReg and UrlHint addresses
* mine those transactions
* then set once more SetHashReg SetUrlHint
* /
2015-04-23 01:11:11 +03:00
source := ` contract test { \n ` +
2016-02-05 15:08:48 +02:00
" /// @notice Will multiply `a` by 7." + ` \n ` +
` function multiply(uint a) returns(uint d) { \n ` +
` return a * 7;\n ` +
` }\n ` +
` }\n `
2015-04-23 01:11:11 +03:00
2015-07-03 14:45:12 +03:00
if checkEvalJSON ( t , repl , ` admin.stopNatSpec() ` , ` true ` ) != nil {
2015-06-23 17:48:33 +03:00
return
}
2015-04-23 01:11:11 +03:00
contractInfo , err := ioutil . ReadFile ( "info_test.json" )
if err != nil {
t . Fatalf ( "%v" , err )
}
2016-02-05 15:08:48 +02:00
if checkEvalJSON ( t , repl , ` primary = eth.accounts[0] ` , ` " ` + testAddress + ` " ` ) != nil {
2015-06-23 17:48:33 +03:00
return
}
2016-02-05 15:08:48 +02:00
if checkEvalJSON ( t , repl , ` source = " ` + source + ` " ` , ` " ` + source + ` " ` ) != nil {
2015-06-23 17:48:33 +03:00
return
}
2015-04-23 01:11:11 +03:00
2015-05-10 14:46:38 +03:00
// if solc is found with right version, test it, otherwise read from file
sol , err := compiler . New ( "" )
2015-04-23 01:11:11 +03:00
if err != nil {
2015-05-18 17:31:26 +03:00
t . Logf ( "solc not found: mocking contract compilation step" )
2015-05-10 14:46:38 +03:00
} else if sol . Version ( ) != solcVersion {
2015-05-18 17:31:26 +03:00
t . Logf ( "WARNING: solc different version found (%v, test written for %v, may need to update)" , sol . Version ( ) , solcVersion )
2015-05-10 14:46:38 +03:00
}
if err != nil {
2015-04-23 01:11:11 +03:00
info , err := ioutil . ReadFile ( "info_test.json" )
if err != nil {
t . Fatalf ( "%v" , err )
}
_ , err = repl . re . Run ( ` contract = JSON.parse( ` + strconv . Quote ( string ( info ) ) + ` ) ` )
if err != nil {
t . Errorf ( "%v" , err )
}
} else {
2015-06-23 17:48:33 +03:00
if checkEvalJSON ( t , repl , ` contract = eth.compile.solidity(source).test ` , string ( contractInfo ) ) != nil {
return
}
2015-04-23 01:11:11 +03:00
}
2015-05-10 14:46:38 +03:00
2015-06-23 17:48:33 +03:00
if checkEvalJSON ( t , repl , ` contract.code ` , ` "0x605880600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b603d6004803590602001506047565b8060005260206000f35b60006007820290506053565b91905056" ` ) != nil {
return
}
2015-04-23 01:11:11 +03:00
2015-06-23 17:48:33 +03:00
if checkEvalJSON (
2015-04-23 01:11:11 +03:00
t , repl ,
2015-06-23 17:48:33 +03:00
` contractaddress = eth.sendTransaction( { from: primary, data: contract.code}) ` ,
2015-07-05 00:12:14 +03:00
` "0x46d69d55c3c4b86a924a92c9fc4720bb7bce1d74" ` ,
2015-06-23 17:48:33 +03:00
) != nil {
return
}
2015-07-05 00:12:14 +03:00
if ! processTxs ( repl , t , 8 ) {
2015-06-23 17:48:33 +03:00
return
}
2015-04-23 01:11:11 +03:00
callSetup := ` abiDef = JSON . parse ( ' [ { "constant" : false , "inputs" : [ { "name" : "a" , "type" : "uint256" } ] , "name" : "multiply" , "outputs" : [ { "name" : "d" , "type" : "uint256" } ] , "type" : "function" } ] ' ) ;
Multiply7 = eth . contract ( abiDef ) ;
2015-05-20 06:11:48 +03:00
multiply7 = Multiply7 . at ( contractaddress ) ;
2015-04-23 01:11:11 +03:00
`
_ , err = repl . re . Run ( callSetup )
if err != nil {
2015-05-18 17:31:26 +03:00
t . Errorf ( "unexpected error setting up contract, got %v" , err )
2015-06-23 17:48:33 +03:00
return
2015-04-23 01:11:11 +03:00
}
expNotice := ""
if repl . lastConfirm != expNotice {
t . Errorf ( "incorrect confirmation message: expected %v, got %v" , expNotice , repl . lastConfirm )
2015-06-23 17:48:33 +03:00
return
2015-04-23 01:11:11 +03:00
}
2015-07-03 14:45:12 +03:00
if checkEvalJSON ( t , repl , ` admin.startNatSpec() ` , ` true ` ) != nil {
2015-06-23 17:48:33 +03:00
return
}
2015-07-05 00:12:14 +03:00
if checkEvalJSON ( t , repl , ` multiply7.multiply.sendTransaction(6, { from: primary }) ` , ` "0x4ef9088431a8033e4580d00e4eb2487275e031ff4163c7529df0ef45af17857b" ` ) != nil {
2015-06-23 17:48:33 +03:00
return
}
if ! processTxs ( repl , t , 1 ) {
return
}
2015-04-23 01:11:11 +03:00
2015-07-05 00:12:14 +03:00
expNotice = ` About to submit transaction (no NatSpec info found for contract: content hash not found for '0x87e2802265838c7f14bb69eecd2112911af6767907a702eeaa445239fb20711b'): { "params":[ { "to":"0x46d69d55c3c4b86a924a92c9fc4720bb7bce1d74","data": "0xc6888fa10000000000000000000000000000000000000000000000000000000000000006"}]} `
2015-04-23 01:11:11 +03:00
if repl . lastConfirm != expNotice {
2015-06-23 17:48:33 +03:00
t . Errorf ( "incorrect confirmation message: expected\n%v, got\n%v" , expNotice , repl . lastConfirm )
return
2015-04-23 01:11:11 +03:00
}
2015-06-23 17:48:33 +03:00
var contentHash = ` "0x86d2b7cf1e72e9a7a3f8d96601f0151742a2f780f1526414304fbe413dc7f9bd" `
if sol != nil && solcVersion != sol . Version ( ) {
2016-02-05 15:08:48 +02:00
modContractInfo := versionRE . ReplaceAll ( contractInfo , [ ] byte ( ` "compilerVersion":" ` + sol . Version ( ) + ` " ` ) )
2015-06-23 17:48:33 +03:00
fmt . Printf ( "modified contractinfo:\n%s\n" , modContractInfo )
2016-02-21 20:40:27 +02:00
contentHash = ` " ` + common . ToHex ( crypto . Keccak256 ( [ ] byte ( modContractInfo ) ) ) + ` " `
2015-05-18 17:31:26 +03:00
}
2015-06-23 17:48:33 +03:00
if checkEvalJSON ( t , repl , ` filename = "/tmp/info.json" ` , ` "/tmp/info.json" ` ) != nil {
return
}
2015-07-03 14:45:12 +03:00
if checkEvalJSON ( t , repl , ` contentHash = admin.saveInfo(contract.info, filename) ` , contentHash ) != nil {
2015-06-23 17:48:33 +03:00
return
}
2015-07-03 14:45:12 +03:00
if checkEvalJSON ( t , repl , ` admin.register(primary, contractaddress, contentHash) ` , ` true ` ) != nil {
2015-06-23 17:48:33 +03:00
return
}
2015-07-03 14:45:12 +03:00
if checkEvalJSON ( t , repl , ` admin.registerUrl(primary, contentHash, "file://"+filename) ` , ` true ` ) != nil {
2015-06-23 17:48:33 +03:00
return
2015-04-23 01:11:11 +03:00
}
2015-07-03 14:45:12 +03:00
if checkEvalJSON ( t , repl , ` admin.startNatSpec() ` , ` true ` ) != nil {
2015-06-23 17:48:33 +03:00
return
}
if ! processTxs ( repl , t , 3 ) {
return
}
2015-07-05 00:12:14 +03:00
if checkEvalJSON ( t , repl , ` multiply7.multiply.sendTransaction(6, { from: primary }) ` , ` "0x66d7635c12ad0b231e66da2f987ca3dfdca58ffe49c6442aa55960858103fd0c" ` ) != nil {
2015-06-23 17:48:33 +03:00
return
}
2015-04-23 01:11:11 +03:00
2015-06-23 17:48:33 +03:00
if ! processTxs ( repl , t , 1 ) {
return
}
2015-04-23 01:11:11 +03:00
expNotice = "Will multiply 6 by 7."
if repl . lastConfirm != expNotice {
2015-06-23 17:48:33 +03:00
t . Errorf ( "incorrect confirmation message: expected\n%v, got\n%v" , expNotice , repl . lastConfirm )
return
}
}
func pendingTransactions ( repl * testjethre , t * testing . T ) ( txc int64 , err error ) {
2015-11-17 18:33:25 +02:00
var ethereum * eth . Ethereum
2015-11-26 18:35:44 +02:00
repl . stack . Service ( & ethereum )
2015-11-17 18:33:25 +02:00
txs := ethereum . TxPool ( ) . GetTransactions ( )
2015-06-23 17:48:33 +03:00
return int64 ( len ( txs ) ) , nil
}
func processTxs ( repl * testjethre , t * testing . T , expTxc int ) bool {
var txc int64
var err error
for i := 0 ; i < 50 ; i ++ {
txc , err = pendingTransactions ( repl , t )
if err != nil {
t . Errorf ( "unexpected error checking pending transactions: %v" , err )
return false
}
if expTxc < int ( txc ) {
t . Errorf ( "too many pending transactions: expected %v, got %v" , expTxc , txc )
return false
} else if expTxc == int ( txc ) {
break
}
time . Sleep ( 100 * time . Millisecond )
}
if int ( txc ) != expTxc {
t . Errorf ( "incorrect number of pending transactions, expected %v, got %v" , expTxc , txc )
return false
2015-04-23 01:11:11 +03:00
}
2015-11-17 18:33:25 +02:00
var ethereum * eth . Ethereum
2015-11-26 18:35:44 +02:00
repl . stack . Service ( & ethereum )
2015-11-17 18:33:25 +02:00
err = ethereum . StartMining ( runtime . NumCPU ( ) , "" )
2015-06-23 17:48:33 +03:00
if err != nil {
t . Errorf ( "unexpected error mining: %v" , err )
return false
}
2015-11-17 18:33:25 +02:00
defer ethereum . StopMining ( )
2015-06-23 17:48:33 +03:00
timer := time . NewTimer ( 100 * time . Second )
2015-12-16 11:58:01 +02:00
blockNr := ethereum . BlockChain ( ) . CurrentBlock ( ) . Number ( )
height := new ( big . Int ) . Add ( blockNr , big . NewInt ( 1 ) )
2015-06-23 17:48:33 +03:00
repl . wait <- height
select {
case <- timer . C :
2016-02-05 15:08:48 +02:00
// if times out make sure the xeth loop does not block
2015-06-23 17:48:33 +03:00
go func ( ) {
select {
case repl . wait <- nil :
case <- repl . wait :
}
} ( )
case <- repl . wait :
}
txc , err = pendingTransactions ( repl , t )
if err != nil {
t . Errorf ( "unexpected error checking pending transactions: %v" , err )
return false
}
if txc != 0 {
t . Errorf ( "%d trasactions were not mined" , txc )
return false
}
return true
2015-04-23 01:11:11 +03:00
}
func checkEvalJSON ( t * testing . T , re * testjethre , expr , want string ) error {
2015-04-21 20:08:47 +03:00
val , err := re . re . Run ( "JSON.stringify(" + expr + ")" )
2015-04-22 11:59:27 +03:00
if err == nil && val . String ( ) != want {
err = fmt . Errorf ( "Output mismatch for `%s`:\ngot: %s\nwant: %s" , expr , val . String ( ) , want )
2015-03-15 08:43:48 +02:00
}
2015-04-22 11:59:27 +03:00
if err != nil {
_ , file , line , _ := runtime . Caller ( 1 )
2015-05-12 15:24:11 +03:00
file = filepath . Base ( file )
2015-04-22 11:59:27 +03:00
fmt . Printf ( "\t%s:%d: %v\n" , file , line , err )
t . Fail ( )
2015-03-15 08:43:48 +02:00
}
2015-04-22 11:59:27 +03:00
return err
2015-03-15 08:43:48 +02:00
}