1 // Aleth: Ethereum C++ client, tools and libraries.
2 // Copyright 2017-2019 Aleth Authors.
3 // Licensed under the GNU General Public License, Version 3.
4
5 #include <test/tools/libtesteth/BlockChainHelper.h>
6 #include <test/tools/libtesteth/TestHelper.h>
7 #include <test/tools/libtestutils/TestLastBlockHashes.h>
8
9 #include <libethereum/Block.h>
10 #include <libethereum/ExtVM.h>
11
12 using namespace dev;
13 using namespace dev::eth;
14 using namespace dev::test;
15
16 class ExtVMExperimentalTestFixture : public TestOutputHelperFixture
17 {
18 public:
ExtVMExperimentalTestFixture()19 ExtVMExperimentalTestFixture()
20 : networkSelector(eth::Network::ExperimentalTransitionTest),
21 testBlockchain(TestBlockChain::defaultGenesisBlock()),
22 genesisBlock(testBlockchain.testGenesis()),
23 genesisDB(genesisBlock.state().db()),
24 blockchain(testBlockchain.getInterface())
25 {
26 TestBlock testBlock;
27 // block 1 - before Experimental
28 testBlock.mine(testBlockchain);
29 testBlockchain.addBlock(testBlock);
30 preExperimentalBlockHash = testBlock.blockHeader().hash();
31
32 // block 2 - first Experimental block
33 testBlock.mine(testBlockchain);
34 testBlockchain.addBlock(testBlock);
35 experimentalBlockHash = testBlock.blockHeader().hash();
36 }
37
createEnvInfo(BlockHeader const & _header) const38 EnvInfo createEnvInfo(BlockHeader const& _header) const
39 {
40 return {_header, lastBlockHashes, 0, blockchain.chainID()};
41 }
42
43 NetworkSelector networkSelector;
44 TestBlockChain testBlockchain;
45 TestBlock const& genesisBlock;
46 OverlayDB const& genesisDB;
47 BlockChain const& blockchain;
48 h256 preExperimentalBlockHash;
49 h256 experimentalBlockHash;
50 TestLastBlockHashes lastBlockHashes{{}};
51 };
52
BOOST_FIXTURE_TEST_SUITE(ExtVmExperimentalSuite,ExtVMExperimentalTestFixture)53 BOOST_FIXTURE_TEST_SUITE(ExtVmExperimentalSuite, ExtVMExperimentalTestFixture)
54
55 BOOST_AUTO_TEST_CASE(BlockhashOutOfBoundsRetunsZero)
56 {
57 Block block = blockchain.genesisBlock(genesisDB);
58 block.sync(blockchain);
59
60 EnvInfo envInfo(createEnvInfo(block.info()));
61 Address addr("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");
62 ExtVM extVM(block.mutableState(), envInfo, *blockchain.sealEngine(), addr, addr, addr, 0, 0, {},
63 {}, {}, 0, 0, false, false);
64
65 BOOST_CHECK_EQUAL(extVM.blockHash(100), h256());
66 }
67
BOOST_AUTO_TEST_CASE(BlockhashBeforeExperimentalReliesOnLastHashes)68 BOOST_AUTO_TEST_CASE(BlockhashBeforeExperimentalReliesOnLastHashes)
69 {
70 Block block = blockchain.genesisBlock(genesisDB);
71 block.sync(blockchain);
72
73 h256s lastHashes{h256("0xaaabbbccc"), h256("0xdddeeefff")};
74 TestLastBlockHashes lastBlockHashes(lastHashes);
75 EnvInfo envInfo{block.info(), lastBlockHashes, 0, blockchain.chainID()};
76 Address addr("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");
77 ExtVM extVM(block.mutableState(), envInfo, *blockchain.sealEngine(), addr, addr, addr, 0, 0, {},
78 {}, {}, 0, 0, false, false);
79 h256 hash = extVM.blockHash(1);
80 BOOST_REQUIRE_EQUAL(hash, lastHashes[0]);
81 }
82
BOOST_AUTO_TEST_CASE(BlockhashDoesntNeedLastHashesInExperimental)83 BOOST_AUTO_TEST_CASE(BlockhashDoesntNeedLastHashesInExperimental)
84 {
85 // BLOCKHASH starts to work through the call to a contract 256 block after Experimental fork
86 // block
87 TestBlock testBlock;
88 for (int i = 0; i < 256; ++i)
89 {
90 testBlock.mine(testBlockchain);
91 testBlockchain.addBlock(testBlock);
92 }
93
94 Block block = blockchain.genesisBlock(genesisDB);
95 block.sync(blockchain);
96
97 EnvInfo envInfo(createEnvInfo(block.info()));
98 Address addr("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");
99 ExtVM extVM(block.mutableState(), envInfo, *blockchain.sealEngine(), addr, addr, addr, 0, 0, {},
100 {}, {}, 0, 0, false, false);
101
102 // older than 256 not available
103 BOOST_CHECK_EQUAL(extVM.blockHash(1), h256());
104
105 h256 hash = extVM.blockHash(200);
106 BOOST_REQUIRE_EQUAL(hash, blockchain.numberHash(200));
107 }
108
BOOST_AUTO_TEST_CASE(ScheduleAccordingToForkBeforeExperimental)109 BOOST_AUTO_TEST_CASE(ScheduleAccordingToForkBeforeExperimental)
110 {
111 Block block = blockchain.genesisBlock(genesisDB);
112 block.sync(blockchain, preExperimentalBlockHash);
113
114 EnvInfo envInfo(createEnvInfo(block.info()));
115 Address addr("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");
116 ExtVM extVM(block.mutableState(), envInfo, *blockchain.sealEngine(), addr, addr, addr, 0, 0, {},
117 {}, {}, 0, 0, false, false);
118
119 BOOST_CHECK_EQUAL(extVM.evmSchedule().accountVersion, 0);
120 BOOST_CHECK(
121 extVM.evmSchedule().txDataNonZeroGas == 16 && extVM.evmSchedule().blockhashGas == 20);
122 }
123
BOOST_AUTO_TEST_CASE(IstanbulScheduleForVersionZeroInExperimental)124 BOOST_AUTO_TEST_CASE(IstanbulScheduleForVersionZeroInExperimental)
125 {
126 Block block = blockchain.genesisBlock(genesisDB);
127 block.sync(blockchain, experimentalBlockHash);
128
129 EnvInfo envInfo(createEnvInfo(block.info()));
130 Address addr("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");
131 u256 const version = 0;
132 ExtVM extVM(block.mutableState(), envInfo, *blockchain.sealEngine(), addr, addr, addr, 0, 0, {},
133 {}, {}, version, 0, false, false);
134
135 BOOST_CHECK_EQUAL(extVM.evmSchedule().accountVersion, version);
136 BOOST_CHECK(
137 extVM.evmSchedule().txDataNonZeroGas == 16 && extVM.evmSchedule().blockhashGas == 20);
138 }
139
BOOST_AUTO_TEST_CASE(ExperimentalScheduleForVersionOneInExperimental)140 BOOST_AUTO_TEST_CASE(ExperimentalScheduleForVersionOneInExperimental)
141 {
142 Block block = blockchain.genesisBlock(genesisDB);
143 block.sync(blockchain, experimentalBlockHash);
144
145 EnvInfo envInfo(createEnvInfo(block.info()));
146 Address addr("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");
147 u256 const version = 1;
148 ExtVM extVM(block.mutableState(), envInfo, *blockchain.sealEngine(), addr, addr, addr, 0, 0, {},
149 {}, {}, version, 0, false, false);
150
151 BOOST_CHECK_EQUAL(extVM.evmSchedule().accountVersion, version);
152 }
153
154
155 BOOST_AUTO_TEST_SUITE_END()
156