2015-04-03 10:37:59 -05:00
package natspec
import (
"io/ioutil"
"math/big"
"os"
"testing"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
2015-04-06 01:01:36 -05:00
"github.com/ethereum/go-ethereum/common/docserver"
2015-04-03 10:37:59 -05:00
"github.com/ethereum/go-ethereum/common/resolver"
"github.com/ethereum/go-ethereum/core"
2015-04-14 08:20:52 -05:00
"github.com/ethereum/go-ethereum/core/state"
2015-04-03 10:37:59 -05:00
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth"
2015-04-07 04:50:17 -05:00
"github.com/ethereum/go-ethereum/rpc"
2015-04-03 10:37:59 -05:00
xe "github.com/ethereum/go-ethereum/xeth"
)
type testFrontend struct {
2015-04-06 01:01:36 -05:00
t * testing . T
ethereum * eth . Ethereum
xeth * xe . XEth
2015-04-07 04:50:17 -05:00
api * rpc . EthereumApi
2015-04-06 01:01:36 -05:00
coinbase string
2015-04-14 08:20:52 -05:00
stateDb * state . StateDB
txc uint64
2015-04-06 01:01:36 -05:00
lastConfirm string
2015-04-07 04:50:17 -05:00
makeNatSpec bool
2015-04-03 10:37:59 -05:00
}
2015-04-14 09:01:25 -05:00
const (
testAccount = "e273f01c99144c438695e10f24926dc1f9fbf62d"
testBalance = "1000000000000"
)
2015-04-17 06:46:38 -05:00
const testFileName = "long_file_name_for_testing_registration_of_URLs_longer_than_32_bytes.content"
2015-04-13 07:35:41 -05:00
const testNotice = "Register key `utils.toHex(_key)` <- content `utils.toHex(_content)`"
const testExpNotice = "Register key 0xadd1a7d961cff0242089674ec2ef6fca671ab15e1fe80e38859fc815b98d88ab <- content 0xc00d5bcc872e17813df6ec5c646bb281a6e2d3b454c2c400c78192adf3344af9"
2015-04-19 13:24:46 -05:00
const testExpNotice2 = ` About to submit transaction (NatSpec notice error "abi key does not match any method"): { "id":6,"jsonrpc":"2.0","method":"eth_transact","params":[ { "from":"0xe273f01c99144c438695e10f24926dc1f9fbf62d","to":"0xb737b91f8e95cf756766fc7c62c9a8ff58470381","value":"100000000000","gas":"100000","gasPrice":"100000","data":"0x31e12c20"}]} `
2015-04-17 06:46:38 -05:00
const testExpNotice3 = ` About to submit transaction (no NatSpec info found for contract): { "id":6,"jsonrpc":"2.0","method":"eth_transact","params":[ { "from":"0xe273f01c99144c438695e10f24926dc1f9fbf62d","to":"0x8b839ad85686967a4f418eccc81962eaee314ac3","value":"100000000000","gas":"100000","gasPrice":"100000","data":"0x300a3bbfc00d5bcc872e17813df6ec5c646bb281a6e2d3b454c2c400c78192adf3344af900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000"}]} `
2015-04-07 04:50:17 -05:00
const testUserDoc = `
{
"source" : "..." ,
"language" : "Solidity" ,
"languageVersion" : 1 ,
"methods" : {
"register(uint256,uint256)" : {
"notice" : "` + testNotice + `"
}
} ,
"invariants" : [
{ "notice" : "" }
] ,
"construction" : [
{ "notice" : "" }
]
}
`
const testABI = `
[ {
"name" : "register" ,
"constant" : false ,
"type" : "function" ,
"inputs" : [ {
"name" : "_key" ,
"type" : "uint256"
} , {
"name" : "_content" ,
"type" : "uint256"
} ] ,
"outputs" : [ ]
} ]
`
const testDocs = `
{
"userdoc" : ` + testUserDoc + ` ,
"abi" : ` + testABI + `
}
`
2015-04-03 10:37:59 -05:00
func ( f * testFrontend ) UnlockAccount ( acc [ ] byte ) bool {
f . t . Logf ( "Unlocking account %v\n" , common . Bytes2Hex ( acc ) )
f . ethereum . AccountManager ( ) . Unlock ( acc , "password" )
return true
}
2015-04-07 04:50:17 -05:00
func ( f * testFrontend ) ConfirmTransaction ( tx string ) bool {
//f.t.Logf("ConfirmTransaction called tx = %v", tx)
if f . makeNatSpec {
ds , err := docserver . New ( "/tmp/" )
if err != nil {
f . t . Errorf ( "Error creating DocServer: %v" , err )
}
2015-04-19 13:22:15 -05:00
f . lastConfirm = GetNotice ( f . xeth , tx , ds )
2015-04-07 04:50:17 -05:00
}
2015-04-06 01:01:36 -05:00
return true
}
2015-04-03 10:37:59 -05:00
var port = 30300
2015-04-06 01:01:36 -05:00
func testEth ( t * testing . T ) ( ethereum * eth . Ethereum , err error ) {
2015-04-19 13:24:46 -05:00
os . RemoveAll ( "/tmp/eth-natspec/" )
err = os . MkdirAll ( "/tmp/eth-natspec/keys/e273f01c99144c438695e10f24926dc1f9fbf62d/" , os . ModePerm )
2015-04-03 10:37:59 -05:00
if err != nil {
t . Errorf ( "%v" , err )
return
}
2015-04-19 13:24:46 -05:00
err = os . MkdirAll ( "/tmp/eth-natspec/data" , os . ModePerm )
2015-04-03 10:37:59 -05:00
if err != nil {
t . Errorf ( "%v" , err )
return
}
2015-04-19 13:24:46 -05:00
ks := crypto . NewKeyStorePlain ( "/tmp/eth-natspec/keys" )
ioutil . WriteFile ( "/tmp/eth-natspec/keys/e273f01c99144c438695e10f24926dc1f9fbf62d/e273f01c99144c438695e10f24926dc1f9fbf62d" ,
2015-04-03 10:37:59 -05:00
[ ] byte ( ` { "Id":"RhRXD+fNRKS4jx+7ZfEsNA==","Address":"4nPwHJkUTEOGleEPJJJtwfn79i0=","PrivateKey":"h4ACVpe74uIvi5Cg/2tX/Yrm2xdr3J7QoMbMtNX2CNc="} ` ) , os . ModePerm )
port ++
ethereum , err = eth . New ( & eth . Config {
2015-04-19 13:24:46 -05:00
DataDir : "/tmp/eth-natspec" ,
2015-04-03 10:37:59 -05:00
AccountManager : accounts . NewManager ( ks ) ,
Name : "test" ,
} )
if err != nil {
t . Errorf ( "%v" , err )
return
}
return
}
2015-04-06 01:01:36 -05:00
func testInit ( t * testing . T ) ( self * testFrontend ) {
2015-04-14 09:01:25 -05:00
core . GenesisData = [ ] byte ( ` {
"` + testAccount + `" : { "balance" : "` + testBalance + `" }
} ` )
2015-04-06 01:01:36 -05:00
ethereum , err := testEth ( t )
if err != nil {
2015-04-19 13:24:46 -05:00
t . Errorf ( "error creating ethereum: %v" , err )
2015-04-06 01:01:36 -05:00
return
}
err = ethereum . Start ( )
if err != nil {
t . Errorf ( "error starting ethereum: %v" , err )
return
}
self = & testFrontend { t : t , ethereum : ethereum }
self . xeth = xe . New ( ethereum , self )
2015-04-07 04:50:17 -05:00
self . api = rpc . NewEthereumApi ( self . xeth )
2015-04-06 01:01:36 -05:00
addr := self . xeth . Coinbase ( )
self . coinbase = addr
2015-04-14 09:01:25 -05:00
if addr != "0x" + testAccount {
t . Errorf ( "CoinBase %v does not match TestAccount 0x%v" , addr , testAccount )
2015-04-06 01:01:36 -05:00
}
t . Logf ( "CoinBase is %v" , addr )
2015-04-14 09:01:25 -05:00
balance := self . xeth . BalanceAt ( testAccount )
2015-04-13 07:35:41 -05:00
/ * if balance != core . TestBalance {
2015-04-06 01:01:36 -05:00
t . Errorf ( "Balance %v does not match TestBalance %v" , balance , core . TestBalance )
2015-04-13 07:35:41 -05:00
} * /
2015-04-06 01:01:36 -05:00
t . Logf ( "Balance is %v" , balance )
2015-04-14 08:20:52 -05:00
self . stateDb = self . ethereum . ChainManager ( ) . State ( ) . Copy ( )
2015-04-06 01:01:36 -05:00
return
}
2015-04-03 10:37:59 -05:00
func ( self * testFrontend ) insertTx ( addr , contract , fnsig string , args [ ] string ) {
2015-04-07 07:46:14 -05:00
//cb := common.HexToAddress(self.coinbase)
//coinbase := self.ethereum.ChainManager().State().GetStateObject(cb)
2015-04-03 10:37:59 -05:00
hash := common . Bytes2Hex ( crypto . Sha3 ( [ ] byte ( fnsig ) ) )
data := "0x" + hash [ 0 : 8 ]
for _ , arg := range args {
data = data + common . Bytes2Hex ( common . Hex2BytesFixed ( arg , 32 ) )
}
self . t . Logf ( "Tx data: %v" , data )
2015-04-07 04:50:17 -05:00
jsontx := `
[ {
"from" : "` + addr + `" ,
2015-04-19 13:24:46 -05:00
"to" : "` + contract + `" ,
2015-04-07 04:50:17 -05:00
"value" : "100000000000" ,
"gas" : "100000" ,
"gasPrice" : "100000" ,
"data" : "` + data + `"
} ]
`
req := & rpc . RpcRequest {
Jsonrpc : "2.0" ,
Method : "eth_transact" ,
Params : [ ] byte ( jsontx ) ,
2015-04-07 07:46:14 -05:00
Id : 6 ,
2015-04-07 04:50:17 -05:00
}
var reply interface { }
err0 := self . api . GetRequestReply ( req , & reply )
if err0 != nil {
self . t . Errorf ( "GetRequestReply error: %v" , err0 )
}
//self.xeth.Transact(addr, contract, "100000000000", "100000", "100000", data)
2015-04-14 08:20:52 -05:00
2015-04-07 04:50:17 -05:00
}
func ( self * testFrontend ) applyTxs ( ) {
2015-04-03 10:37:59 -05:00
2015-04-07 04:50:17 -05:00
cb := common . HexToAddress ( self . coinbase )
2015-04-03 10:37:59 -05:00
block := self . ethereum . ChainManager ( ) . NewBlock ( cb )
2015-04-14 08:20:52 -05:00
coinbase := self . stateDb . GetStateObject ( cb )
coinbase . SetGasPool ( big . NewInt ( 10000000 ) )
2015-04-23 03:51:47 -05:00
txs := self . ethereum . TxPool ( ) . GetQueuedTransactions ( )
2015-04-03 10:37:59 -05:00
2015-04-07 07:46:14 -05:00
for i := 0 ; i < len ( txs ) ; i ++ {
for _ , tx := range txs {
2015-04-14 08:20:52 -05:00
//self.t.Logf("%v %v %v", i, tx.Nonce(), self.txc)
if tx . Nonce ( ) == self . txc {
_ , gas , err := core . ApplyMessage ( core . NewEnv ( self . stateDb , self . ethereum . ChainManager ( ) , tx , block ) , tx , coinbase )
2015-04-07 07:46:14 -05:00
//self.ethereum.TxPool().RemoveSet([]*types.Transaction{tx})
self . t . Logf ( "ApplyMessage: gas %v err %v" , gas , err )
2015-04-14 08:20:52 -05:00
self . txc ++
2015-04-07 07:46:14 -05:00
}
}
2015-04-07 04:50:17 -05:00
}
2015-04-03 10:37:59 -05:00
2015-04-14 08:20:52 -05:00
//self.ethereum.TxPool().RemoveSet(txs)
self . xeth = self . xeth . WithState ( self . stateDb )
2015-04-03 10:37:59 -05:00
}
2015-04-06 01:01:36 -05:00
func ( self * testFrontend ) registerURL ( hash common . Hash , url string ) {
hashHex := common . Bytes2Hex ( hash [ : ] )
2015-04-17 06:46:38 -05:00
urlBytes := [ ] byte ( url )
var bb bool = true
var cnt byte
for bb {
bb = len ( urlBytes ) > 0
urlb := urlBytes
if len ( urlb ) > 32 {
urlb = urlb [ : 32 ]
}
urlHex := common . Bytes2Hex ( urlb )
self . insertTx ( self . coinbase , resolver . URLHintContractAddress , "register(uint256,uint8,uint256)" , [ ] string { hashHex , common . Bytes2Hex ( [ ] byte { cnt } ) , urlHex } )
if len ( urlBytes ) > 32 {
urlBytes = urlBytes [ 32 : ]
} else {
urlBytes = nil
}
cnt ++
}
2015-04-07 04:50:17 -05:00
}
func ( self * testFrontend ) setOwner ( ) {
2015-04-14 08:20:52 -05:00
self . insertTx ( self . coinbase , resolver . HashRegContractAddress , "setowner()" , [ ] string { } )
2015-04-07 04:50:17 -05:00
2015-04-14 08:20:52 -05:00
/ * owner := self . xeth . StorageAt ( "0x" + resolver . HashRegContractAddress , "0x0000000000000000000000000000000000000000000000000000000000000000" )
2015-04-07 04:50:17 -05:00
self . t . Logf ( "owner = %v" , owner )
if owner != self . coinbase {
self . t . Errorf ( "setowner() unsuccessful, owner != coinbase" )
} * /
}
func ( self * testFrontend ) registerNatSpec ( codehash , dochash common . Hash ) {
codeHex := common . Bytes2Hex ( codehash [ : ] )
docHex := common . Bytes2Hex ( dochash [ : ] )
2015-04-14 08:20:52 -05:00
self . insertTx ( self . coinbase , resolver . HashRegContractAddress , "register(uint256,uint256)" , [ ] string { codeHex , docHex } )
2015-04-06 01:01:36 -05:00
}
2015-04-03 10:37:59 -05:00
2015-04-06 01:01:36 -05:00
func ( self * testFrontend ) testResolver ( ) * resolver . Resolver {
2015-04-07 04:50:17 -05:00
return resolver . New ( self . xeth , resolver . URLHintContractAddress , resolver . HashRegContractAddress )
2015-04-06 01:01:36 -05:00
}
2015-04-03 10:37:59 -05:00
2015-04-06 01:01:36 -05:00
func TestNatspecE2E ( t * testing . T ) {
2015-04-03 10:37:59 -05:00
2015-04-06 01:01:36 -05:00
tf := testInit ( t )
defer tf . ethereum . Stop ( )
2015-04-03 10:37:59 -05:00
2015-04-14 09:01:25 -05:00
resolver . CreateContracts ( tf . xeth , testAccount )
2015-04-14 08:20:52 -05:00
t . Logf ( "URLHint contract registered at %v" , resolver . URLHintContractAddress )
t . Logf ( "HashReg contract registered at %v" , resolver . HashRegContractAddress )
tf . applyTxs ( )
2015-04-17 06:46:38 -05:00
ioutil . WriteFile ( "/tmp/" + testFileName , [ ] byte ( testDocs ) , os . ModePerm )
2015-04-07 04:50:17 -05:00
dochash := common . BytesToHash ( crypto . Sha3 ( [ ] byte ( testDocs ) ) )
2015-04-03 10:37:59 -05:00
2015-04-14 08:20:52 -05:00
codehex := tf . xeth . CodeAt ( resolver . HashRegContractAddress )
2015-04-08 05:35:02 -05:00
codehash := common . BytesToHash ( crypto . Sha3 ( common . Hex2Bytes ( codehex [ 2 : ] ) ) )
2015-04-03 10:37:59 -05:00
2015-04-07 04:50:17 -05:00
tf . setOwner ( )
tf . registerNatSpec ( codehash , dochash )
2015-04-17 06:46:38 -05:00
tf . registerURL ( dochash , "file:///" + testFileName )
2015-04-07 04:50:17 -05:00
tf . applyTxs ( )
2015-04-03 10:37:59 -05:00
2015-04-07 04:50:17 -05:00
chash , err := tf . testResolver ( ) . KeyToContentHash ( codehash )
if err != nil {
t . Errorf ( "Can't find content hash" )
}
t . Logf ( "chash = %x err = %v" , chash , err )
url , err2 := tf . testResolver ( ) . ContentHashToUrl ( dochash )
if err2 != nil {
t . Errorf ( "Can't find URL hint" )
}
t . Logf ( "url = %v err = %v" , url , err2 )
2015-04-08 05:35:02 -05:00
// NatSpec info for register method of HashReg contract installed
// now using the same transactions to check confirm messages
2015-04-07 04:50:17 -05:00
tf . makeNatSpec = true
2015-04-08 05:35:02 -05:00
tf . registerNatSpec ( codehash , dochash )
2015-04-07 04:50:17 -05:00
t . Logf ( "Confirm message: %v\n" , tf . lastConfirm )
if tf . lastConfirm != testExpNotice {
t . Errorf ( "Wrong confirm message, expected '%v', got '%v'" , testExpNotice , tf . lastConfirm )
}
2015-04-03 10:37:59 -05:00
2015-04-08 05:35:02 -05:00
tf . setOwner ( )
t . Logf ( "Confirm message for unknown method: %v\n" , tf . lastConfirm )
if tf . lastConfirm != testExpNotice2 {
t . Errorf ( "Wrong confirm message, expected '%v', got '%v'" , testExpNotice2 , tf . lastConfirm )
}
tf . registerURL ( dochash , "file:///test.content" )
t . Logf ( "Confirm message for unknown contract: %v\n" , tf . lastConfirm )
if tf . lastConfirm != testExpNotice3 {
t . Errorf ( "Wrong confirm message, expected '%v', got '%v'" , testExpNotice3 , tf . lastConfirm )
}
2015-04-03 10:37:59 -05:00
}