1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
package natspec
import (
"fmt"
"io/ioutil"
"math/big"
"os"
"testing"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/docserver"
"github.com/ethereum/go-ethereum/common/natspec"
"github.com/ethereum/go-ethereum/common/resolver"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
//"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/rpc"
xe "github.com/ethereum/go-ethereum/xeth"
)
type testFrontend struct {
t *testing.T
ethereum *eth.Ethereum
xeth *xe.XEth
api *rpc.EthereumApi
coinbase string
stateDb *state.StateDB
txc uint64
lastConfirm string
makeNatSpec bool
}
const testNotice = "Register key `utils.toHex(_key)` <- content `utils.toHex(_content)`"
const testExpNotice = "Register key 0xadd1a7d961cff0242089674ec2ef6fca671ab15e1fe80e38859fc815b98d88ab <- content 0xc00d5bcc872e17813df6ec5c646bb281a6e2d3b454c2c400c78192adf3344af9"
const testExpNotice2 = `About to submit transaction (NatSpec notice error "abi key %!x(MISSING) does not match any method %!v(MISSING)"): {"id":6,"jsonrpc":"2.0","method":"eth_transact","params":[{"from":"0xe273f01c99144c438695e10f24926dc1f9fbf62d","to":"0xb737b91f8e95cf756766fc7c62c9a8ff58470381","value":"100000000000","gas":"100000","gasPrice":"100000","data":"0x31e12c20"}]}`
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":"0xd66d6c10c00d5bcc872e17813df6ec5c646bb281a6e2d3b454c2c400c78192adf3344af900000000000000000000000066696c653a2f2f2f746573742e636f6e74656e74"}]}`
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 + `
}
`
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
}
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)
}
f.lastConfirm = natspec.GetNotice(f.xeth, tx, ds)
}
return true
}
var port = 30300
func testEth(t *testing.T) (ethereum *eth.Ethereum, err error) {
os.RemoveAll("/tmp/eth/")
err = os.MkdirAll("/tmp/eth/keys/e273f01c99144c438695e10f24926dc1f9fbf62d/", os.ModePerm)
if err != nil {
t.Errorf("%v", err)
return
}
err = os.MkdirAll("/tmp/eth/data", os.ModePerm)
if err != nil {
t.Errorf("%v", err)
return
}
ks := crypto.NewKeyStorePlain("/tmp/eth/keys")
ioutil.WriteFile("/tmp/eth/keys/e273f01c99144c438695e10f24926dc1f9fbf62d/e273f01c99144c438695e10f24926dc1f9fbf62d",
[]byte(`{"Id":"RhRXD+fNRKS4jx+7ZfEsNA==","Address":"4nPwHJkUTEOGleEPJJJtwfn79i0=","PrivateKey":"h4ACVpe74uIvi5Cg/2tX/Yrm2xdr3J7QoMbMtNX2CNc="}`), os.ModePerm)
port++
ethereum, err = eth.New(ð.Config{
DataDir: "/tmp/eth",
AccountManager: accounts.NewManager(ks),
Port: fmt.Sprintf("%d", port),
MaxPeers: 10,
Name: "test",
})
if err != nil {
t.Errorf("%v", err)
return
}
return
}
func testInit(t *testing.T) (self *testFrontend) {
ethereum, err := testEth(t)
if err != nil {
t.Errorf("error creating jsre, got %v", err)
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)
self.api = rpc.NewEthereumApi(self.xeth)
addr := self.xeth.Coinbase()
self.coinbase = addr
if addr != "0x"+core.TestAccount {
t.Errorf("CoinBase %v does not match TestAccount 0x%v", addr, core.TestAccount)
}
t.Logf("CoinBase is %v", addr)
balance := self.xeth.BalanceAt(core.TestAccount)
/*if balance != core.TestBalance {
t.Errorf("Balance %v does not match TestBalance %v", balance, core.TestBalance)
}*/
t.Logf("Balance is %v", balance)
self.stateDb = self.ethereum.ChainManager().State().Copy()
return
}
func (self *testFrontend) insertTx(addr, contract, fnsig string, args []string) {
//cb := common.HexToAddress(self.coinbase)
//coinbase := self.ethereum.ChainManager().State().GetStateObject(cb)
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)
jsontx := `
[{
"from": "` + addr + `",
"to": "0x` + contract + `",
"value": "100000000000",
"gas": "100000",
"gasPrice": "100000",
"data": "` + data + `"
}]
`
req := &rpc.RpcRequest{
Jsonrpc: "2.0",
Method: "eth_transact",
Params: []byte(jsontx),
Id: 6,
}
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)
}
func (self *testFrontend) applyTxs() {
cb := common.HexToAddress(self.coinbase)
block := self.ethereum.ChainManager().NewBlock(cb)
coinbase := self.stateDb.GetStateObject(cb)
coinbase.SetGasPool(big.NewInt(10000000))
txs := self.ethereum.TxPool().GetTransactions()
for i := 0; i < len(txs); i++ {
for _, tx := range txs {
//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)
//self.ethereum.TxPool().RemoveSet([]*types.Transaction{tx})
self.t.Logf("ApplyMessage: gas %v err %v", gas, err)
self.txc++
}
}
}
//self.ethereum.TxPool().RemoveSet(txs)
self.xeth = self.xeth.WithState(self.stateDb)
}
func (self *testFrontend) registerURL(hash common.Hash, url string) {
hashHex := common.Bytes2Hex(hash[:])
urlHex := common.Bytes2Hex([]byte(url))
self.insertTx(self.coinbase, resolver.URLHintContractAddress, "register(uint256,uint256)", []string{hashHex, urlHex})
}
func (self *testFrontend) setOwner() {
self.insertTx(self.coinbase, resolver.HashRegContractAddress, "setowner()", []string{})
/*owner := self.xeth.StorageAt("0x"+resolver.HashRegContractAddress, "0x0000000000000000000000000000000000000000000000000000000000000000")
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[:])
self.insertTx(self.coinbase, resolver.HashRegContractAddress, "register(uint256,uint256)", []string{codeHex, docHex})
}
func (self *testFrontend) testResolver() *resolver.Resolver {
return resolver.New(self.xeth, resolver.URLHintContractAddress, resolver.HashRegContractAddress)
}
func TestNatspecE2E(t *testing.T) {
tf := testInit(t)
defer tf.ethereum.Stop()
resolver.CreateContracts(tf.xeth, core.TestAccount)
t.Logf("URLHint contract registered at %v", resolver.URLHintContractAddress)
t.Logf("HashReg contract registered at %v", resolver.HashRegContractAddress)
tf.applyTxs()
ioutil.WriteFile("/tmp/test.content", []byte(testDocs), os.ModePerm)
dochash := common.BytesToHash(crypto.Sha3([]byte(testDocs)))
codehex := tf.xeth.CodeAt(resolver.HashRegContractAddress)
codehash := common.BytesToHash(crypto.Sha3(common.Hex2Bytes(codehex[2:])))
tf.setOwner()
tf.registerNatSpec(codehash, dochash)
tf.registerURL(dochash, "file:///test.content")
tf.applyTxs()
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)
// NatSpec info for register method of HashReg contract installed
// now using the same transactions to check confirm messages
tf.makeNatSpec = true
tf.registerNatSpec(codehash, dochash)
t.Logf("Confirm message: %v\n", tf.lastConfirm)
if tf.lastConfirm != testExpNotice {
t.Errorf("Wrong confirm message, expected '%v', got '%v'", testExpNotice, tf.lastConfirm)
}
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)
}
}
|