Skip to content

Commit

Permalink
test: Improve maintainability of JSON loading
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Nov 24, 2023
1 parent 2223e78 commit 8ed570e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
27 changes: 18 additions & 9 deletions test/blockchaintest/blockchaintest_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@ T load_if_exists(const json::json& j, std::string_view key)
template <>
BlockHeader from_json<BlockHeader>(const json::json& j)
{
return {from_json<hash256>(j.at("parentHash")), from_json<address>(j.at("coinbase")),
from_json<hash256>(j.at("stateRoot")), from_json<hash256>(j.at("receiptTrie")),
state::bloom_filter_from_bytes(from_json<bytes>(j.at("bloom"))),
load_if_exists<int64_t>(j, "difficulty"), load_if_exists<bytes32>(j, "mixHash"),
from_json<int64_t>(j.at("number")), from_json<int64_t>(j.at("gasLimit")),
from_json<int64_t>(j.at("gasUsed")), from_json<int64_t>(j.at("timestamp")),
from_json<bytes>(j.at("extraData")), load_if_exists<uint64_t>(j, "baseFeePerGas"),
from_json<hash256>(j.at("hash")), from_json<hash256>(j.at("transactionsTrie")),
load_if_exists<hash256>(j, "withdrawalsRoot")};
return {
.parent_hash = from_json<hash256>(j.at("parentHash")),
.coinbase = from_json<address>(j.at("coinbase")),
.state_root = from_json<hash256>(j.at("stateRoot")),
.receipts_root = from_json<hash256>(j.at("receiptTrie")),
.logs_bloom = state::bloom_filter_from_bytes(from_json<bytes>(j.at("bloom"))),
.difficulty = load_if_exists<int64_t>(j, "difficulty"),
.prev_randao = load_if_exists<bytes32>(j, "mixHash"),
.block_number = from_json<int64_t>(j.at("number")),
.gas_limit = from_json<int64_t>(j.at("gasLimit")),
.gas_used = from_json<int64_t>(j.at("gasUsed")),
.timestamp = from_json<int64_t>(j.at("timestamp")),
.extra_data = from_json<bytes>(j.at("extraData")),
.base_fee_per_gas = load_if_exists<uint64_t>(j, "baseFeePerGas"),
.hash = from_json<hash256>(j.at("hash")),
.transactions_root = from_json<hash256>(j.at("transactionsTrie")),
.withdrawal_root = load_if_exists<hash256>(j, "withdrawalsRoot"),
};
}

static TestBlock load_test_block(const json::json& j, evmc_revision rev)
Expand Down
20 changes: 15 additions & 5 deletions test/statetest/statetest_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,21 @@ state::BlockInfo from_json<state::BlockInfo>(const json::json& j)
if (parent_timestamp_it != j.end())
parent_timestamp = from_json<int64_t>(*parent_timestamp_it);

return {from_json<int64_t>(j.at("currentNumber")), from_json<int64_t>(j.at("currentTimestamp")),
parent_timestamp, from_json<int64_t>(j.at("currentGasLimit")),
from_json<evmc::address>(j.at("currentCoinbase")), current_difficulty, parent_difficulty,
parent_uncle_hash, prev_randao, base_fee, std::move(ommers), std::move(withdrawals),
std::move(block_hashes)};
return state::BlockInfo{
.number = from_json<int64_t>(j.at("currentNumber")),
.timestamp = from_json<int64_t>(j.at("currentTimestamp")),
.parent_timestamp = parent_timestamp,
.gas_limit = from_json<int64_t>(j.at("currentGasLimit")),
.coinbase = from_json<evmc::address>(j.at("currentCoinbase")),
.difficulty = current_difficulty,
.parent_difficulty = parent_difficulty,
.parent_ommers_hash = parent_uncle_hash,
.prev_randao = prev_randao,
.base_fee = base_fee,
.ommers = std::move(ommers),
.withdrawals = std::move(withdrawals),
.known_block_hashes = std::move(block_hashes),
};
}

template <>
Expand Down

0 comments on commit 8ed570e

Please sign in to comment.