Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit e690185

Browse files
authored
Merge pull request #4822 from ethereum/int64
Change type for block number to int64
2 parents 619a643 + 6b651c8 commit e690185

File tree

12 files changed

+44
-32
lines changed

12 files changed

+44
-32
lines changed

ethvm/main.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,10 @@ int main(int argc, char** argv)
158158
("help,h", "Show this help message and exit.")
159159
("author", po::value<Address>(), "<a> Set author")
160160
("difficulty", po::value<u256>(), "<n> Set difficulty")
161-
("number", po::value<u256>(), "<n> Set number")
161+
("number", po::value<int64_t>()->default_value(0)->value_name("<number>")
162+
->notifier([&](int64_t _n) { blockHeader.setNumber(_n); }), "Set the block number")
162163
("timestamp", po::value<int64_t>()->default_value(0)->value_name("<timestamp>")
163-
->notifier([&](int64_t _t){ blockHeader.setTimestamp(_t); }), "Set timestamp");
164+
->notifier([&](int64_t _t){ blockHeader.setTimestamp(_t); }), "Set the block timestamp");
164165

165166
po::options_description allowedOptions(
166167
"Usage ethvm <options> [trace|stats|output|test] (<file>|-)");
@@ -220,8 +221,6 @@ int main(int argc, char** argv)
220221
gasPrice = vm["gas-price"].as<u256>();
221222
if (vm.count("author"))
222223
blockHeader.setAuthor(vm["author"].as<Address>());
223-
if (vm.count("number"))
224-
blockHeader.setNumber(vm["number"].as<u256>());
225224
if (vm.count("difficulty"))
226225
blockHeader.setDifficulty(vm["difficulty"].as<u256>());
227226
if (vm.count("gas-limit"))

libethcore/BlockHeader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void BlockHeader::populate(RLP const& _header)
181181
m_receiptsRoot = _header[field = 5].toHash<h256>(RLP::VeryStrict);
182182
m_logBloom = _header[field = 6].toHash<LogBloom>(RLP::VeryStrict);
183183
m_difficulty = _header[field = 7].toInt<u256>();
184-
m_number = _header[field = 8].toInt<u256>();
184+
m_number = _header[field = 8].toPositiveInt64();
185185
m_gasLimit = _header[field = 9].toInt<u256>();
186186
m_gasUsed = _header[field = 10].toInt<u256>();
187187
m_timestamp = _header[field = 11].toPositiveInt64();

libethcore/BlockHeader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class BlockHeader
146146
void setAuthor(Address const& _v) { m_author = _v; noteDirty(); }
147147
void setRoots(h256 const& _t, h256 const& _r, h256 const& _u, h256 const& _s) { m_transactionsRoot = _t; m_receiptsRoot = _r; m_stateRoot = _s; m_sha3Uncles = _u; noteDirty(); }
148148
void setGasUsed(u256 const& _v) { m_gasUsed = _v; noteDirty(); }
149-
void setNumber(u256 const& _v) { m_number = _v; noteDirty(); }
149+
void setNumber(int64_t _v) { m_number = _v; noteDirty(); }
150150
void setGasLimit(u256 const& _v) { m_gasLimit = _v; noteDirty(); }
151151
void setExtraData(bytes const& _v) { m_extraData = _v; noteDirty(); }
152152
void setLogBloom(LogBloom const& _v) { m_logBloom = _v; noteDirty(); }
@@ -163,7 +163,7 @@ class BlockHeader
163163
h256 const& transactionsRoot() const { return m_transactionsRoot; }
164164
h256 const& receiptsRoot() const { return m_receiptsRoot; }
165165
u256 const& gasUsed() const { return m_gasUsed; }
166-
u256 const& number() const { return m_number; }
166+
int64_t number() const { return m_number; }
167167
u256 const& gasLimit() const { return m_gasLimit; }
168168
bytes const& extraData() const { return m_extraData; }
169169
LogBloom const& logBloom() const { return m_logBloom; }
@@ -195,7 +195,7 @@ class BlockHeader
195195
h256 m_transactionsRoot;
196196
h256 m_receiptsRoot;
197197
LogBloom m_logBloom;
198-
u256 m_number;
198+
int64_t m_number = 0;
199199
u256 m_gasLimit;
200200
u256 m_gasUsed;
201201
bytes m_extraData;

libethereum/SnapshotImporter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void SnapshotImporter::importBlockChunks(SnapshotStorageFace const& _snapshotSto
175175
if (blockChunk.itemCount() < 3)
176176
BOOST_THROW_EXCEPTION(InvalidBlockChunkData());
177177

178-
u256 const firstBlockNumber = blockChunk[0].toInt<u256>(RLP::VeryStrict);
178+
int64_t const firstBlockNumber = blockChunk[0].toPositiveInt64(RLP::VeryStrict);
179179
h256 const firstBlockHash = blockChunk[1].toHash<h256>(RLP::VeryStrict);
180180
u256 const firstBlockDifficulty = blockChunk[2].toInt<u256>(RLP::VeryStrict);
181181
if (!firstBlockNumber || !firstBlockHash || !firstBlockDifficulty)
@@ -185,7 +185,7 @@ void SnapshotImporter::importBlockChunks(SnapshotStorageFace const& _snapshotSto
185185

186186
size_t const itemCount = blockChunk.itemCount();
187187
h256 parentHash = firstBlockHash;
188-
u256 number = firstBlockNumber + 1;
188+
int64_t number = firstBlockNumber + 1;
189189
u256 totalDifficulty = firstBlockDifficulty;
190190
for (size_t i = 3; i < itemCount; ++i, ++number)
191191
{

libevm/EVMC.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ EVM::EVM(evm_instance* _instance) noexcept : m_instance(_instance)
2323

2424
owning_bytes_ref EVMC::exec(u256& io_gas, ExtVMFace& _ext, const OnOpFunc& _onOp)
2525
{
26+
assert(_ext.envInfo().number() >= 0);
27+
assert(_ext.envInfo().timestamp() >= 0);
28+
2629
constexpr int64_t int64max = std::numeric_limits<int64_t>::max();
2730

2831
// TODO: The following checks should be removed by changing the types
2932
// used for gas, block number and timestamp.
3033
(void)int64max;
3134
assert(io_gas <= int64max);
32-
assert(_ext.envInfo().number() <= int64max);
33-
assert(_ext.envInfo().timestamp() <= int64max);
3435
assert(_ext.envInfo().gasLimit() <= int64max);
3536
assert(_ext.depth <= std::numeric_limits<int32_t>::max());
3637

libevm/ExtVMFace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class EnvInfo
154154

155155
BlockHeader const& header() const { return m_headerInfo; }
156156

157-
u256 const& number() const { return m_headerInfo.number(); }
157+
int64_t number() const { return m_headerInfo.number(); }
158158
Address const& author() const { return m_headerInfo.author(); }
159159
int64_t timestamp() const { return m_headerInfo.timestamp(); }
160160
u256 const& difficulty() const { return m_headerInfo.difficulty(); }

test/tools/jsontests/BlockChainTests.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ void overwriteBlockHeaderForTest(mObject const& _blObj, TestBlock& _block, Chain
641641
if (ho.count("RelTimestamp"))
642642
{
643643
BlockHeader parentHeader = importedBlocks.at(importedBlocks.size() - 1).blockHeader();
644-
tmp.setTimestamp(toInt64(ho["RelTimestamp"]) + parentHeader.timestamp());
644+
tmp.setTimestamp(toPositiveInt64(ho["RelTimestamp"]) + parentHeader.timestamp());
645645
tmp.setDifficulty(((const Ethash*)sealEngine)->calculateDifficulty(tmp, parentHeader));
646646
}
647647

@@ -755,7 +755,8 @@ void overwriteUncleHeaderForTest(mObject& uncleHeaderObj, TestBlock& uncle, std:
755755
if (uncleHeaderObj.count("RelTimestamp"))
756756
{
757757
BlockHeader parentHeader = importedBlocks.at(number).blockHeader();
758-
uncleHeader.setTimestamp(toInt64(uncleHeaderObj["RelTimestamp"]) + parentHeader.timestamp());
758+
uncleHeader.setTimestamp(
759+
toPositiveInt64(uncleHeaderObj["RelTimestamp"]) + parentHeader.timestamp());
759760
uncleHeader.setDifficulty(((const Ethash*)sealEngine)->calculateDifficulty(uncleHeader, parentHeader));
760761
uncleHeaderObj.erase("RelTimestamp");
761762
}

test/tools/jsontests/vm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ EnvInfo FakeExtVM::importEnv(mObject const& _o, LastBlockHashesFace const& _last
104104
BlockHeader blockHeader;
105105
blockHeader.setGasLimit(gasLimit.convert_to<int64_t>());
106106
blockHeader.setDifficulty(toInt(_o.at("currentDifficulty")));
107-
blockHeader.setTimestamp(toInt64(_o.at("currentTimestamp")));
107+
blockHeader.setTimestamp(toPositiveInt64(_o.at("currentTimestamp")));
108108
blockHeader.setAuthor(Address(_o.at("currentCoinbase").get_str()));
109-
blockHeader.setNumber(toInt(_o.at("currentNumber")));
109+
blockHeader.setNumber(toPositiveInt64(_o.at("currentNumber")));
110110
return EnvInfo(blockHeader, _lastBlockHashes, 0);
111111
}
112112

test/tools/libtesteth/ImportTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ void ImportTest::importEnv(json_spirit::mObject const& _o)
347347
BlockHeader header;
348348
header.setGasLimit(gasLimit.convert_to<int64_t>());
349349
header.setDifficulty(toInt(_o.at("currentDifficulty")));
350-
header.setNumber(toInt(_o.at("currentNumber")));
351-
header.setTimestamp(toInt64(_o.at("currentTimestamp")));
350+
header.setNumber(toPositiveInt64(_o.at("currentNumber")));
351+
header.setTimestamp(toPositiveInt64(_o.at("currentTimestamp")));
352352
header.setAuthor(Address(_o.at("currentCoinbase").get_str()));
353353

354354
m_lastBlockHashes.reset(new TestLastBlockHashes(lastHashes(header.number())));

test/tools/libtesteth/TestHelper.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,18 +277,25 @@ u256 toInt(json_spirit::mValue const& _v)
277277
return 0;
278278
}
279279

280-
int64_t toInt64(const json_spirit::mValue& _v)
280+
int64_t toPositiveInt64(const json_spirit::mValue& _v)
281281
{
282+
int64_t n = 0;
282283
switch (_v.type())
283284
{
284285
case json_spirit::str_type:
285-
return std::stoll(_v.get_str(), nullptr, 0);
286+
n = std::stoll(_v.get_str(), nullptr, 0);
287+
break;
286288
case json_spirit::int_type:
287-
return _v.get_int64();
289+
n = _v.get_int64();
290+
break;
288291
default:
289292
cwarn << "Bad type for scalar: " << _v.type();
290293
}
291-
return 0;
294+
295+
if (n < 0)
296+
throw std::out_of_range{"unexpected negative value: " + std::to_string(n)};
297+
298+
return n;
292299
}
293300

294301
byte toByte(json_spirit::mValue const& _v)

0 commit comments

Comments
 (0)