forked from syscoin/syscoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathethereum_tests.cpp
More file actions
111 lines (98 loc) · 4.59 KB
/
ethereum_tests.cpp
File metadata and controls
111 lines (98 loc) · 4.59 KB
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
#include <boost/test/unit_test.hpp>
#include <test/data/ethspv_valid.json.h>
#include <test/data/ethspv_invalid.json.h>
#include <uint256.h>
#include <util/strencodings.h>
#include <ethereum/ethereum.h>
#include <ethereum/common.h>
#include <ethereum/rlp.h>
#include <script/interpreter.h>
#include <script/standard.h>
#include <policy/policy.h>
#include <services/asset.h>
#include <univalue.h>
extern UniValue read_json(const std::string& jsondata);
BOOST_AUTO_TEST_SUITE(ethereum_tests)
BOOST_AUTO_TEST_CASE(ethereum_parseabidata)
{
printf("Running ethereum_parseabidata...\n");
CAmount outputAmount = 0;
uint32_t nAsset = 0;
const std::vector<unsigned char> &expectedMethodHash = ParseHex("285c5bc6");
const std::vector<unsigned char> &rlpBytes = ParseHex("285c5bc600000000000000000000000000000000000000000000000000000000773594000000000000000000000000000000000000000000000000000000000065736d4700000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000015004322ec9eb713f37cf8d701d819c165549d53d14e0000000000000000000000");
CWitnessAddress expectedAddress(0, ParseHex("4322ec9eb713f37cf8d701d819c165549d53d14e"));
CWitnessAddress address;
BOOST_CHECK(parseEthMethodInputData(expectedMethodHash, rlpBytes, outputAmount, nAsset, address));
BOOST_CHECK_EQUAL(outputAmount, 20*COIN);
BOOST_CHECK_EQUAL(nAsset, 1702063431);
BOOST_CHECK(address == expectedAddress);
}
BOOST_AUTO_TEST_CASE(ethspv_valid)
{
printf("Running ethspv_valid...\n");
// Read tests from test/data/ethspv_valid.json
// Format is an array of arrays
// Inner arrays are either [ "comment" ]
// [[spv_root, spv_parent_node, spv_value, spv_path]]
UniValue tests = read_json(std::string(json_tests::ethspv_valid, json_tests::ethspv_valid + sizeof(json_tests::ethspv_valid)));
for (unsigned int idx = 0; idx < tests.size(); idx++) {
UniValue test = tests[idx];
std::string strTest = test.write();
if (test.size() != 4) {
// ignore comments
continue;
} else {
if ( !test[0].isStr() || !test[1].isStr() || !test[2].isStr() || !test[3].isStr()) {
BOOST_ERROR("Bad test: " << strTest);
continue;
}
std::string spv_tx_root = test[0].get_str();
std::string spv_parent_nodes = test[1].get_str();
std::string spv_value = test[2].get_str();
std::string spv_path = test[3].get_str();
const std::vector<unsigned char> &vchTxRoot = ParseHex(spv_tx_root);
dev::RLP rlpTxRoot(&vchTxRoot);
const std::vector<unsigned char> &vchTxParentNodes = ParseHex(spv_parent_nodes);
dev::RLP rlpTxParentNodes(&vchTxParentNodes);
const std::vector<unsigned char> &vchTxValue = ParseHex(spv_value);
dev::RLP rlpTxValue(&vchTxValue);
const std::vector<unsigned char> &vchTxPath = ParseHex(spv_path);
BOOST_CHECK(VerifyProof(&vchTxPath, rlpTxValue, rlpTxParentNodes, rlpTxRoot));
}
}
}
BOOST_AUTO_TEST_CASE(ethspv_invalid)
{
printf("Running ethspv_invalid...\n");
// Read tests from test/data/ethspv_invalid.json
// Format is an array of arrays
// Inner arrays are either [ "comment" ]
// [[spv_root, spv_parent_node, spv_value, spv_path]]
UniValue tests = read_json(std::string(json_tests::ethspv_invalid, json_tests::ethspv_invalid + sizeof(json_tests::ethspv_invalid)));
for (unsigned int idx = 0; idx < tests.size(); idx++) {
UniValue test = tests[idx];
std::string strTest = test.write();
if (test.size() != 4) {
// ignore comments
continue;
} else {
if ( !test[0].isStr() || !test[1].isStr() || !test[2].isStr() || !test[3].isStr()) {
BOOST_ERROR("Bad test: " << strTest);
continue;
}
std::string spv_tx_root = test[0].get_str();
std::string spv_parent_nodes = test[1].get_str();
std::string spv_value = test[2].get_str();
std::string spv_path = test[3].get_str();
const std::vector<unsigned char> &vchTxRoot = ParseHex(spv_tx_root);
dev::RLP rlpTxRoot(&vchTxRoot);
const std::vector<unsigned char> &vchTxParentNodes = ParseHex(spv_parent_nodes);
dev::RLP rlpTxParentNodes(&vchTxParentNodes);
const std::vector<unsigned char> &vchTxValue = ParseHex(spv_value);
dev::RLP rlpTxValue(&vchTxValue);
const std::vector<unsigned char> &vchTxPath = ParseHex(spv_path);
BOOST_CHECK(!VerifyProof(&vchTxPath, rlpTxValue, rlpTxParentNodes, rlpTxRoot));
}
}
}
BOOST_AUTO_TEST_SUITE_END()