44#include " state_transition.hpp"
55
66#include < bit>
7+ #include < format>
78#include < fstream>
8- #include < iostream>
99#include < stdexcept>
1010
1111#include < magic_enum.hpp>
2222#include < zilk_core/core/types/address.hpp>
2323#include < zilk_core/core/types/evmc_bytes32.hpp>
2424#include < zilk_core/dev/common/ecc_key_pair.hpp>
25+ #include < zilk_core/print.hpp>
2526
2627#include " expected_state.hpp"
2728
@@ -47,7 +48,7 @@ StateTransition::StateTransition(const std::string& unified_rlp_str) noexcept {
4748
4849 // Read from binary
4950 unified_rlp_ = ByteView{reinterpret_cast <const uint8_t *>(unified_rlp_str.data ()), unified_rlp_str.size ()};
50- std::cout << " in ctor unified_rlp_str RLP length: " << unified_rlp_str. size () << " unified_rlp_ RLP length: " << unified_rlp_.size () << " \n " ;
51+ sys_println ( std::format ( " in ctor unified_rlp_str RLP length: {} unified_rlp_ RLP length: {} " , unified_rlp_str. size (), unified_rlp_.size ()). c_str ()) ;
5152}
5253
5354StateTransition::StateTransition (const bool terminate_on_error, const bool show_diagnostics) noexcept
@@ -335,7 +336,7 @@ namespace {
335336 if (invalid) {
336337 return Status::kPassed ;
337338 }
338- std::cout << " Failure to read hex" << std::endl ;
339+ sys_println ( " Failure to read hex" ) ;
339340 return Status::kFailed ;
340341 }
341342
@@ -361,7 +362,7 @@ namespace {
361362 if (invalid) {
362363 return Status::kPassed ;
363364 }
364- std::cout << " Failure to decode RLP" << std::endl ;
365+ sys_println ( " Failure to decode RLP" ) ;
365366 return Status::kFailed ;
366367 }
367368
@@ -370,13 +371,13 @@ namespace {
370371 if (invalid) {
371372 return Status::kPassed ;
372373 }
373- std::cout << " Validation error " << magic_enum::enum_name<ValidationResult>(err) << std::endl ;
374+ sys_println ( std::format ( " Validation error {} " , magic_enum::enum_name<ValidationResult>(err)). c_str ()) ;
374375 return Status::kFailed ;
375376 }
376377
377378 if (invalid) {
378- std::cout << " Invalid block executed successfully\n " ;
379- std::cout << " Expected: " << json_block[" expectException" ] << std::endl ;
379+ sys_println ( " Invalid block executed successfully" ) ;
380+ sys_println ( std::format ( " Expected: {} " , json_block[" expectException" ]. dump ()). c_str ()) ;
380381 return Status::kFailed ;
381382 }
382383
@@ -385,13 +386,12 @@ namespace {
385386
386387 bool post_check (const InMemoryState& state, const nlohmann::json& expected) {
387388 if (state.accounts ().size () != expected.size ()) {
388- std::cout << " Account number mismatch: " << state.accounts ().size () << " != " << expected.size ()
389- << std::endl;
389+ sys_println (std::format (" Account number mismatch: {} != {}" , state.accounts ().size (), expected.size ()).c_str ());
390390
391391 // Find and report accounts missing from the expected set.
392392 for (const auto & [addr, _] : state.accounts ()) {
393393 if (const auto addr_hex = " 0x" + hex (addr); !expected.contains (addr_hex)) {
394- std::cout << " Unexpected account: " << addr_hex << std::endl ;
394+ sys_println ( std::format ( " Unexpected account: {} " , addr_hex). c_str ()) ;
395395 }
396396 }
397397
@@ -404,36 +404,32 @@ namespace {
404404
405405 std::optional<Account> account{state.read_account (address)};
406406 if (!account) {
407- std::cout << " Missing account " << entry.key () << std::endl ;
407+ sys_println ( std::format ( " Missing account {} " , entry.key ()). c_str ()) ;
408408 return false ;
409409 }
410410
411411 const auto expected_balance{intx::from_string<intx::uint256>(j[" balance" ].get <std::string>())};
412412 if (account->balance != expected_balance) {
413- std::cout << " Balance mismatch for " << entry.key () << " :\n "
414- << to_string (account->balance , 16 ) << " != " << j[" balance" ] << std::endl;
413+ sys_println (std::format (" Balance mismatch for {}:\n {} != {}" , entry.key (), to_string (account->balance , 16 ), j[" balance" ].get <std::string>()).c_str ());
415414 return false ;
416415 }
417416
418417 const auto expected_nonce{intx::from_string<intx::uint256>(j[" nonce" ].get <std::string>())};
419418 if (account->nonce != expected_nonce) {
420- std::cout << " Nonce mismatch for " << entry.key () << " :\n "
421- << account->nonce << " != " << j[" nonce" ] << std::endl;
419+ sys_println (std::format (" Nonce mismatch for {}:\n {} != {}" , entry.key (), account->nonce , j[" nonce" ].get <std::string>()).c_str ());
422420 return false ;
423421 }
424422
425423 auto expected_code{j[" code" ].get <std::string>()};
426424 Bytes actual_code{state.read_code (address, account->code_hash )};
427425 if (actual_code != from_hex (expected_code)) {
428- std::cout << " Code mismatch for " << entry.key () << " :\n "
429- << to_hex (actual_code) << " != " << expected_code << std::endl;
426+ sys_println (std::format (" Code mismatch for {}:\n {} != {}" , entry.key (), to_hex (actual_code), expected_code).c_str ());
430427 return false ;
431428 }
432429
433430 size_t storage_size{state.storage_size (address, account->incarnation )};
434431 if (storage_size != j[" storage" ].size ()) {
435- std::cout << " Storage size mismatch for " << entry.key () << " :\n "
436- << storage_size << " != " << j[" storage" ].size () << std::endl;
432+ sys_println (std::format (" Storage size mismatch for {}:\n {} != {}" , entry.key (), storage_size, j[" storage" ].size ()).c_str ());
437433 return false ;
438434 }
439435
@@ -442,8 +438,7 @@ namespace {
442438 Bytes expected_value{from_hex (storage.value ().get <std::string>()).value ()};
443439 evmc::bytes32 actual_value{state.read_storage (address, account->incarnation , to_bytes32 (key))};
444440 if (actual_value != to_bytes32 (expected_value)) {
445- std::cout << " Storage mismatch for " << entry.key () << " at " << storage.key () << " :\n "
446- << to_hex (actual_value) << " != " << to_hex (expected_value) << std::endl;
441+ sys_println (std::format (" Storage mismatch for {} at {}:\n {} != {}" , entry.key (), storage.key (), to_hex (actual_value), to_hex (expected_value)).c_str ());
447442 return false ;
448443 }
449444 }
@@ -487,15 +482,15 @@ namespace {
487482 const auto network{json_test[" network" ].get <std::string>()};
488483 const auto config_it{test::kNetworkConfig .find (network)};
489484 if (config_it == test::kNetworkConfig .end ()) {
490- std::cout << " unknown network " << network << std::endl ;
485+ sys_println ( std::format ( " unknown network {} " , network). c_str ()) ;
491486 return Status::kSkipped ;
492487 }
493488 auto genesisRLPStr = json_test[" genesisRLP" ].get <std::string>();
494489 Bytes genesis_rlp{from_hex (genesisRLPStr).value ()};
495490 ByteView genesis_view{genesis_rlp};
496491 Block genesis_block;
497492 if (!rlp::decode (genesis_view, genesis_block)) {
498- std::cout << " Failure to decode genesisRLP" << std::endl ;
493+ sys_println ( " Failure to decode genesisRLP" ) ;
499494 return Status::kFailed ;
500495 }
501496
@@ -514,8 +509,7 @@ namespace {
514509 evmc::bytes32 state_root{state.state_root_hash ()};
515510 std::string expected_hex{json_test[" postStateHash" ].get <std::string>()};
516511 if (state_root != to_bytes32 (from_hex (expected_hex).value ())) {
517- std::cout << " postStateHash mismatch:\n "
518- << to_hex (state_root) << " != " << expected_hex << std::endl;
512+ sys_println (std::format (" postStateHash mismatch:\n {} != {}" , to_hex (state_root), expected_hex).c_str ());
519513 return Status::kFailed ;
520514 }
521515 return Status::kPassed ;
@@ -538,16 +532,16 @@ uint64_t StateTransition::run(uint32_t num_runs, bool is_test) {
538532 bool any_failed = false ;
539533 bool any_skipped = false ;
540534 for (const auto & [name, test] : base_json_.items ()) {
541- std::cout << " " << name << " : \n " ;
535+ sys_println ( std::format ( " {}: " , name). c_str ()) ;
542536 const auto result = blockchain_test (test);
543537 if (result.failed != 0 ) {
544538 any_failed = true ;
545- std::cout << " FAILED\n " ;
539+ sys_println ( " FAILED" ) ;
546540 } else if (result.skipped != 0 ) {
547- std::cout << " SKIPPED\n " ;
541+ sys_println ( " SKIPPED" ) ;
548542 any_skipped = true ;
549543 } else {
550- std::cout << " passed\n " ;
544+ sys_println ( " passed" ) ;
551545 }
552546 }
553547 if (any_failed)
@@ -557,7 +551,7 @@ uint64_t StateTransition::run(uint32_t num_runs, bool is_test) {
557551 return 0 ;
558552 }
559553
560- std::cout << " Running State transition. num_runs=" << num_runs << " \n " ;
554+ sys_println ( std::format ( " Running State transition. num_runs={} " , num_runs). c_str ()) ;
561555 failed_count_ = 0 ;
562556 total_count_ = 0 ;
563557 uint64_t total_gas = 0 ;
@@ -591,7 +585,7 @@ uint64_t StateTransition::run(uint32_t num_runs, bool is_test) {
591585 txn_validation == ValidationResult::kOk ) {
592586 // ============== [TESTING ONLY] SIMULATING MULTIPLE RUNS=====
593587 for (uint32_t i = 0 ; i < num_runs; i++) {
594- std::cout << " Inside multiple runs loop\n " ;
588+ sys_println ( " Inside multiple runs loop" ) ;
595589
596590 auto state_cp = read_genesis_allocation (test_data_[" pre" ]);
597591 ExecutionProcessor ccprocessor{block, *rule_set, state_cp, config, true };
0 commit comments