From f26f734a3f1102fbb001fd6c2134d5432d69decc Mon Sep 17 00:00:00 2001 From: Sean Darcy Date: Tue, 23 Apr 2024 12:51:22 +1000 Subject: [PATCH] local ci instead of sepolia --- .github/workflows/ci.yml | 14 +++++++++++ test/src/ethereum_client.cpp | 49 ++++++++++++++---------------------- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b3ca493..2c49e1a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,6 +21,16 @@ jobs: - name: Install prerequisites run: sudo apt-get update && sudo apt-get install -y cmake build-essential libboost-all-dev libssl-dev libcurl4-openssl-dev libsodium-dev + - name: Install Foundry + run: | + curl -L https://foundry.paradigm.xyz | bash + /home/runner/.config/.foundry/bin/foundryup + + - name: Start node in background + run: | + anvil & + echo "NODE_PID=$!" >> $GITHUB_ENV + - name: Create Build Directory run: mkdir -p build @@ -33,3 +43,7 @@ jobs: - name: Run tests run: ctest -VV --repeat until-pass:2 --test-dir build + - name: Kill node + run: kill $NODE_PID + if: always() + diff --git a/test/src/ethereum_client.cpp b/test/src/ethereum_client.cpp index 218b6cc..6407e36 100644 --- a/test/src/ethereum_client.cpp +++ b/test/src/ethereum_client.cpp @@ -7,13 +7,19 @@ #include #include -TEST_CASE( "Get balance from sepolia network", "[ethereum]" ) { - // Construct the client with the local RPC URL - //Provider client("Sepolia Client", std::string("https://rpc.sepolia.org")); - Provider client("Sepolia Client", std::string("https://eth-sepolia.g.alchemy.com/v2/xjUjCAfxli88pqe7UjR4Tt1Jp2GKPJvy")); +// Construct the client with the local RPC URL +inline constexpr std::string_view PRIVATE_KEY = "96a656cbd64281ea82257ca9978093b25117592287e4e07f5be660d1701f03e9"; +inline constexpr std::string_view ADDRESS = "0x2ccb8b65024e4aa9615a8e704dfb11be76674f1f"; +inline constexpr std::string_view ANVIL_ADDRESS = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"; +inline constexpr std::string_view ANVIL_PRIVATE_KEY = "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"; +std::vector seckey = utils::fromHexString(std::string(PRIVATE_KEY)); +std::vector anvilseckey = utils::fromHexString(std::string(ANVIL_PRIVATE_KEY)); +auto provider = std::make_shared("Client", std::string("127.0.0.1:8545")); +Signer signer(provider); +TEST_CASE( "Get balance from sepolia network", "[ethereum]" ) { // Get the balance of the test address - auto balance = client.getBalance("0x2Ccb8b65024E4aA9615a8E704DFb11BE76674f1F"); + auto balance = provider->getBalance(std::string(ANVIL_ADDRESS)); // Check that the balance is greater than zero REQUIRE( balance != "" ); @@ -24,11 +30,7 @@ TEST_CASE( "HashTest", "[utils]" ) { REQUIRE( hash_hello_world == "57caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd6" ); } -inline constexpr std::string_view PRIVATE_KEY = "96a656cbd64281ea82257ca9978093b25117592287e4e07f5be660d1701f03e9"; -inline constexpr std::string_view ADDRESS = "0x2ccb8b65024e4aa9615a8e704dfb11be76674f1f"; TEST_CASE( "SigningTest", "[signer]" ) { - std::vector seckey = utils::fromHexString(std::string(PRIVATE_KEY)); - Signer signer; std::string hash_hello_world = utils::toHexString(utils::hash("Hello World!\n")); const auto signature_bytes = signer.signMessage("Hello World!", seckey); std::string signature_hex = utils::toHexString(signature_bytes); @@ -36,8 +38,6 @@ TEST_CASE( "SigningTest", "[signer]" ) { } TEST_CASE( "Get address from private key", "[signer]" ) { - std::vector seckey = utils::fromHexString(std::string(PRIVATE_KEY)); - Signer signer; std::string created_address = signer.secretKeyToAddressString(seckey); REQUIRE( created_address == ADDRESS ); } @@ -77,8 +77,6 @@ TEST_CASE( "Hashes an unsigned transaction correctly", "[transaction]" ) { } TEST_CASE( "Signs an unsigned transaction correctly", "[transaction]" ) { - std::vector seckey = utils::fromHexString(std::string(PRIVATE_KEY)); - Signer signer; Transaction tx("0xA6C077fd9283421C657EcEa8a9c1422cc6CEbc80", 1000000000000000000, 21000); tx.nonce = 1; tx.chainId = 1; @@ -86,31 +84,22 @@ TEST_CASE( "Signs an unsigned transaction correctly", "[transaction]" ) { REQUIRE( signature_hex_string == "0x02f86a0101808082520894a6c077fd9283421c657ecea8a9c1422cc6cebc80880de0b6b3a764000080c080a084987299f8dd115333356ab03430ca8de593e03ba03d4ecd72daf15205119cf8a0216c9869da3497ae96dcb98713908af1a0abf866c12d51def821caf0374cccbb" ); } -inline constexpr std::string_view LOCAL_PRIVATE_KEY = "11070278466b8a23d8709c6c583015f1c0b386c91f63d29b87d48a1174fe7276"; -TEST_CASE( "Does a self transfer on Sepolia", "[transaction]" ) { - //auto provider = std::make_shared("Sepolia Client", std::string("https://rpc.sepolia.org")); - auto provider = std::make_shared("Sepolia Client", std::string("https://eth-sepolia.g.alchemy.com/v2/xjUjCAfxli88pqe7UjR4Tt1Jp2GKPJvy")); - Signer signer(provider); - Transaction tx(std::string(ADDRESS), 100000000000000, 21000); - tx.chainId = 11155111; //SEPOLIA - tx.nonce = provider->getTransactionCount(std::string(ADDRESS), "pending"); +TEST_CASE( "Does a self transfer", "[transaction]" ) { + Transaction tx(std::string(ANVIL_ADDRESS), 100000000000000, 21000); + tx.chainId = 31337; //LOCAL + tx.nonce = provider->getTransactionCount(std::string(ANVIL_ADDRESS), "pending"); const auto feeData = provider->getFeeData(); tx.maxFeePerGas = feeData.maxFeePerGas; tx.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas; - std::vector seckey = utils::fromHexString(std::string(PRIVATE_KEY)); - const auto signature_hex_string = signer.signTransaction(tx, seckey); + const auto signature_hex_string = signer.signTransaction(tx, anvilseckey); const auto hash = provider->sendTransaction(tx); REQUIRE(hash != ""); REQUIRE(provider->transactionSuccessful(hash)); } -TEST_CASE( "Does a self transfer on Sepolia using signer to populate", "[transaction]" ) { - //auto provider = std::make_shared("Sepolia Client", std::string("https://rpc.sepolia.org")); - auto provider = std::make_shared("Sepolia Client", std::string("https://eth-sepolia.g.alchemy.com/v2/xjUjCAfxli88pqe7UjR4Tt1Jp2GKPJvy")); - Signer signer(provider); - Transaction tx("0x2Ccb8b65024E4aA9615a8E704DFb11BE76674f1F", 100000000000000, 21000); - std::vector seckey = utils::fromHexString(std::string(PRIVATE_KEY)); - const auto hash = signer.sendTransaction(tx, seckey); +TEST_CASE( "Does a self transfer on network using signer to populate", "[transaction]" ) { + Transaction tx(std::string(ANVIL_ADDRESS), 100000000000000, 21000); + const auto hash = signer.sendTransaction(tx, anvilseckey); REQUIRE(hash != ""); REQUIRE(provider->transactionSuccessful(hash)); }