Skip to content

Commit ace03ec

Browse files
committed
add tests
1 parent 1cb2b1e commit ace03ec

7 files changed

+318
-17
lines changed

ethereum/client.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ func toBlockNumArg(number *big.Int) string {
206206
return hexutil.EncodeBig(number)
207207
}
208208

209+
// Transaction returns the transaction response of the Transaction identified
210+
// by *RosettaTypes.TransactionIdentifier hash or index
209211
func (ec *Client) Transaction(
210212
ctx context.Context,
211213
blockIdentifier *RosettaTypes.BlockIdentifier,
@@ -228,7 +230,7 @@ func (ec *Client) Transaction(
228230
}
229231

230232
var header *types.Header
231-
if blockIdentifier.Hash != ""{
233+
if blockIdentifier.Hash != "" {
232234
header, err = ec.blockHeaderByHash(ctx, blockIdentifier.Hash)
233235
} else {
234236
header, err = ec.blockHeaderByNumber(ctx, big.NewInt(blockIdentifier.Index))
@@ -494,7 +496,6 @@ func (ec *Client) getTransactionTraces(
494496
var call *Call
495497
var raw json.RawMessage
496498
err := ec.c.CallContext(ctx, &raw, "debug_traceTransaction", transactionHash, ec.tc)
497-
498499
if err != nil {
499500
return nil, nil, err
500501
}
@@ -1137,7 +1138,7 @@ func (ec *Client) populateTransactions(
11371138
func (ec *Client) populateTransaction(
11381139
tx *loadedTransaction,
11391140
) (*RosettaTypes.Transaction, error) {
1140-
ops := []*RosettaTypes.Operation{}
1141+
var ops []*RosettaTypes.Operation
11411142

11421143
// Compute fee operations
11431144
feeOps := feeOps(tx)

ethereum/client_test.go

Lines changed: 129 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,20 +1406,135 @@ func jsonifyBlock(b *RosettaTypes.Block) (*RosettaTypes.Block, error) {
14061406
return &bo, nil
14071407
}
14081408

1409-
//func TestTransaction_Hash(t *testing.T) {
1410-
// mockJSONRPC := &mocks.JSONRPC{}
1411-
// mockGraphQL := &mocks.GraphQL{}
1412-
//
1413-
// tc, err := testTraceConfig()
1414-
// assert.NoError(t, err)
1415-
// c := &Client{
1416-
// c: mockJSONRPC,
1417-
// g: mockGraphQL,
1418-
// tc: tc,
1419-
// p: params.RopstenChainConfig,
1420-
// traceSemaphore: semaphore.NewWeighted(100),
1421-
// }
1422-
//}
1409+
func TestTransaction_Hash(t *testing.T) {
1410+
mockJSONRPC := &mocks.JSONRPC{}
1411+
mockGraphQL := &mocks.GraphQL{}
1412+
txHash := "0x9cc8e6a09ae9cbdb7da77515110a8e343a945df4269c53842dd26969d32c6cc4"
1413+
blockHash := "0xc10a51a3898a85c7165a9d883acc9a68f139934d0cb91dfad4c7d3a7c1a1960d"
1414+
1415+
tc, err := testTraceConfig()
1416+
assert.NoError(t, err)
1417+
c := &Client{
1418+
c: mockJSONRPC,
1419+
g: mockGraphQL,
1420+
tc: tc,
1421+
p: params.RopstenChainConfig,
1422+
traceSemaphore: semaphore.NewWeighted(100),
1423+
}
1424+
1425+
ctx := context.Background()
1426+
mockJSONRPC.On(
1427+
"CallContext",
1428+
ctx,
1429+
mock.Anything,
1430+
"eth_getTransactionByHash",
1431+
txHash,
1432+
).Return(
1433+
nil,
1434+
).Run(
1435+
func(args mock.Arguments) {
1436+
r := args.Get(1).(*json.RawMessage)
1437+
1438+
file, err := ioutil.ReadFile(
1439+
"testdata/transaction_0x9cc8e6a09ae9cbdb7da77515110a8e343a945df4269c53842dd26969d32c6cc4.json",
1440+
) // nolint
1441+
assert.NoError(t, err)
1442+
1443+
*r = json.RawMessage(file)
1444+
},
1445+
).Once()
1446+
1447+
mockJSONRPC.On(
1448+
"CallContext",
1449+
ctx,
1450+
mock.Anything,
1451+
"eth_getBlockByHash",
1452+
blockHash,
1453+
false,
1454+
).Return(
1455+
nil,
1456+
).Run(
1457+
func(args mock.Arguments) {
1458+
r := args.Get(1).(**types.Header)
1459+
1460+
file, err := ioutil.ReadFile(
1461+
"testdata/block_0xc10a51a3898a85c7165a9d883acc9a68f139934d0cb91dfad4c7d3a7c1a1960d.json",
1462+
) // nolint
1463+
assert.NoError(t, err)
1464+
1465+
*r = new(types.Header)
1466+
1467+
assert.NoError(t, (*r).UnmarshalJSON(file))
1468+
},
1469+
).Once()
1470+
1471+
mockJSONRPC.On(
1472+
"CallContext",
1473+
ctx,
1474+
mock.Anything,
1475+
"eth_getTransactionReceipt",
1476+
common.HexToHash(txHash),
1477+
).Return(
1478+
nil,
1479+
).Run(
1480+
func(args mock.Arguments) {
1481+
r := args.Get(1).(**types.Receipt)
1482+
1483+
file, err := ioutil.ReadFile(
1484+
"testdata/tx_receipt_0x9cc8e6a09ae9cbdb7da77515110a8e343a945df4269c53842dd26969d32c6cc4.json",
1485+
) // nolint
1486+
assert.NoError(t, err)
1487+
1488+
*r = new(types.Receipt)
1489+
1490+
assert.NoError(t, (*r).UnmarshalJSON(file))
1491+
},
1492+
).Once()
1493+
1494+
mockJSONRPC.On(
1495+
"CallContext",
1496+
ctx,
1497+
mock.Anything,
1498+
"debug_traceTransaction",
1499+
common.HexToHash(txHash),
1500+
tc,
1501+
).Return(
1502+
nil,
1503+
).Run(
1504+
func(args mock.Arguments) {
1505+
r := args.Get(1).(*json.RawMessage)
1506+
1507+
file, err := ioutil.ReadFile(
1508+
"testdata/transaction_trace_0x9cc8e6a09ae9cbdb7da77515110a8e343a945df4269c53842dd26969d32c6cc4.json",
1509+
) // nolint
1510+
assert.NoError(t, err)
1511+
1512+
*r = json.RawMessage(file)
1513+
},
1514+
).Once()
1515+
1516+
correctRaw, err := ioutil.ReadFile(
1517+
"testdata/transaction_response_0x9cc8e6a09ae9cbdb7da77515110a8e343a945df4269c53842dd26969d32c6cc4.json",
1518+
) // nolint
1519+
assert.NoError(t, err)
1520+
var correct *RosettaTypes.BlockTransactionResponse
1521+
assert.NoError(t, json.Unmarshal(correctRaw, &correct))
1522+
1523+
resp, err := c.Transaction(
1524+
ctx,
1525+
&RosettaTypes.BlockIdentifier{
1526+
Hash: "0xc10a51a3898a85c7165a9d883acc9a68f139934d0cb91dfad4c7d3a7c1a1960d",
1527+
},
1528+
&RosettaTypes.TransactionIdentifier{
1529+
Hash: "0x9cc8e6a09ae9cbdb7da77515110a8e343a945df4269c53842dd26969d32c6cc4",
1530+
},
1531+
)
1532+
assert.Equal(t, correct.Transaction, resp)
1533+
assert.NoError(t, err)
1534+
1535+
mockJSONRPC.AssertExpectations(t)
1536+
mockGraphQL.AssertExpectations(t)
1537+
}
14231538

14241539
// Block with transaction
14251540
func TestBlock_10994(t *testing.T) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"size": "0x7d8",
3+
"stateRoot": "0x398dea392039f685330007d8bf0dfa19a11b4416dca25da81eda376273a2f314",
4+
"timestamp": "0x5839bac0",
5+
"transactions": [
6+
"0xe4c2e3ed6213995ae2eae1159d249ec65d4878d567b93201ed11cd9c61111bad",
7+
"0xe5f20e54a54fcd8ff72d65dafd6329504d8b4928b7907c00da9c3f2d27ff5946",
8+
"0x026f61be527edd51fda90c20494fb2967a81042f425158121ad718a6e6769823",
9+
"0x9cc8e6a09ae9cbdb7da77515110a8e343a945df4269c53842dd26969d32c6cc4",
10+
"0xe85ce94f56959423940ef54977ba9270c882c67b513dba7a5d470e11402ef0f6",
11+
"0x59bc342f33b4df5913356dd41f8b7f8642d14bd81a22c6cdc97b0c14ad77e048"
12+
],
13+
"difficulty": "0x5e4e9b1",
14+
"logsBloom": "0x04002000000000000000000000000000000500000000000000000020000000000000000010000000000000000000000000000000000000000000100000000000000000000000000000000000004000000000000000000400000008000000000000000000020440000000000000000800400000000004000000000000000000000000008800000000000000000000000000000000000000000000000000080088000000000000000000000100000000000000000000000000000000000400000010000000100000200040000040000000000000000000000000000000400020000000000000000000000000000000000008000000008000000000000000004000",
15+
"totalDifficulty": "0x3bd0bc576e3",
16+
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
17+
"parentHash": "0xd027a9008c5cb9da99ce459159a187a15e476fbb20a420abbd0964ca5720cd8e",
18+
"hash": "0xc10a51a3898a85c7165a9d883acc9a68f139934d0cb91dfad4c7d3a7c1a1960d",
19+
"extraData": "0xd583010503846765746885676f312e37856c696e7578",
20+
"gasUsed": "0xcda60",
21+
"transactionsRoot": "0x0ab42b6a48a9eea1bd4611a39342a9eab0ac3fefb636c53b6de61eda0ac3b852",
22+
"gasLimit": "0x47e7c4",
23+
"number": "0xafc8",
24+
"miner": "0xffc614ee978630d7fb0c06758deb580c152154d3",
25+
"uncles": [],
26+
"nonce": "0x59dfb51a4ab60a79",
27+
"mixHash": "0x004f27be284f8536342d6e00ab9b1a12e03c40df3dfc4699ff55798dbd67f945",
28+
"receiptsRoot": "0x94fe13083791d63d3f1318ba6d73407cf059fe344cbad5d6d4abd9f00b80edfd"
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"hash":"0x9cc8e6a09ae9cbdb7da77515110a8e343a945df4269c53842dd26969d32c6cc4",
3+
"blockHash":"0xc10a51a3898a85c7165a9d883acc9a68f139934d0cb91dfad4c7d3a7c1a1960d",
4+
"blockNumber":"0xafc8",
5+
"from":"0x687422eea2cb73b5d3e242ba5456b782919afc85",
6+
"gas":"0x4cb26",
7+
"gasPrice":"0x4a817c800",
8+
"input":"0x",
9+
"nonce":"0x9c6",
10+
"r":"0x3dbbbf3bac12e7e5caa704a77d35a3dd7bf0374475f0970e6c3d0033d54436c0",
11+
"s":"0x31b023f55d877ee6888583f86dc019c283b3d7255314bcd753346475baceb964",
12+
"to":"0xc662a694fdaa5406a8ee2ca2e94890d58ab578d9",
13+
"transactionIndex":"0x3",
14+
"v":"0x1c",
15+
"value":"0xde0b6b3a7640000"
16+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
{
2+
"transaction": {
3+
"transaction_identifier": {
4+
"hash": "0x9cc8e6a09ae9cbdb7da77515110a8e343a945df4269c53842dd26969d32c6cc4"
5+
},
6+
"operations": [
7+
{
8+
"operation_identifier": {
9+
"index": 0
10+
},
11+
"type": "FEE",
12+
"status": "SUCCESS",
13+
"account": {
14+
"address": "0x687422eEA2cB73B5d3e242bA5456b782919AFc85"
15+
},
16+
"amount": {
17+
"value": "-420000000000000",
18+
"currency": {
19+
"symbol": "ETH",
20+
"decimals": 18
21+
}
22+
}
23+
},
24+
{
25+
"operation_identifier": {
26+
"index": 1
27+
},
28+
"related_operations": [
29+
{
30+
"index": 0
31+
}
32+
],
33+
"type": "FEE",
34+
"status": "SUCCESS",
35+
"account": {
36+
"address": "0xfFC614eE978630D7fB0C06758DeB580c152154d3"
37+
},
38+
"amount": {
39+
"value": "420000000000000",
40+
"currency": {
41+
"symbol": "ETH",
42+
"decimals": 18
43+
}
44+
}
45+
},
46+
{
47+
"operation_identifier": {
48+
"index": 2
49+
},
50+
"type": "CALL",
51+
"status": "SUCCESS",
52+
"account": {
53+
"address": "0x687422eEA2cB73B5d3e242bA5456b782919AFc85"
54+
},
55+
"amount": {
56+
"value": "-1000000000000000000",
57+
"currency": {
58+
"symbol": "ETH",
59+
"decimals": 18
60+
}
61+
}
62+
},
63+
{
64+
"operation_identifier": {
65+
"index": 3
66+
},
67+
"related_operations": [
68+
{
69+
"index": 2
70+
}
71+
],
72+
"type": "CALL",
73+
"status": "SUCCESS",
74+
"account": {
75+
"address": "0xC662A694FdAa5406A8Ee2CA2E94890d58aB578D9"
76+
},
77+
"amount": {
78+
"value": "1000000000000000000",
79+
"currency": {
80+
"symbol": "ETH",
81+
"decimals": 18
82+
}
83+
}
84+
}
85+
],
86+
"metadata": {
87+
"gas_limit": "0x4cb26",
88+
"gas_price": "0x4a817c800",
89+
"receipt": {
90+
"blockHash": "0xc10a51a3898a85c7165a9d883acc9a68f139934d0cb91dfad4c7d3a7c1a1960d",
91+
"blockNumber": "0xafc8",
92+
"contractAddress": "0x0000000000000000000000000000000000000000",
93+
"cumulativeGasUsed": "0x8b7c8",
94+
"gasUsed": "0x5208",
95+
"logs": [],
96+
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
97+
"root": "0x338b8ada6551b52f72804aa34c1846a9e101d8a52fcc13c10ba257efee769ddd",
98+
"status": "0x0",
99+
"transactionHash": "0x9cc8e6a09ae9cbdb7da77515110a8e343a945df4269c53842dd26969d32c6cc4",
100+
"transactionIndex": "0x3"
101+
},
102+
"trace": {
103+
"from": "0x687422eea2cb73b5d3e242ba5456b782919afc85",
104+
"gas": "0x4791e",
105+
"gasUsed": "0x0",
106+
"input": "0x",
107+
"output": "0x",
108+
"time": "13.5µs",
109+
"to": "0xc662a694fdaa5406a8ee2ca2e94890d58ab578d9",
110+
"type": "CALL",
111+
"value": "0xde0b6b3a7640000"
112+
}
113+
}
114+
}
115+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"type":"CALL",
3+
"from":"0x687422eea2cb73b5d3e242ba5456b782919afc85",
4+
"to":"0xc662a694fdaa5406a8ee2ca2e94890d58ab578d9",
5+
"value":"0xde0b6b3a7640000",
6+
"gas":"0x4791e",
7+
"gasUsed":"0x0",
8+
"input":"0x",
9+
"output":"0x",
10+
"time":"13.5µs"
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"transactionHash": "0x9cc8e6a09ae9cbdb7da77515110a8e343a945df4269c53842dd26969d32c6cc4",
3+
"blockHash": "0xc10a51a3898a85c7165a9d883acc9a68f139934d0cb91dfad4c7d3a7c1a1960d",
4+
"blockNumber": "0xafc8",
5+
"contractAddress": null,
6+
"cumulativeGasUsed": "0x8b7c8",
7+
"from": "0x687422eea2cb73b5d3e242ba5456b782919afc85",
8+
"gasUsed": "0x5208",
9+
"logs": [],
10+
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
11+
"root": "0x338b8ada6551b52f72804aa34c1846a9e101d8a52fcc13c10ba257efee769ddd",
12+
"to": "0xc662a694fdaa5406a8ee2ca2e94890d58ab578d9",
13+
"transactionIndex": "0x3"
14+
}

0 commit comments

Comments
 (0)