Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EPROD-956 unstable reqwest tests #187

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: ALCHEMY_API_KEY
  • Loading branch information
veeso committed Aug 12, 2024
commit 9afa555bcddd9549c6b5d2d44a6701f416ffa188
26 changes: 15 additions & 11 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
name: 'Build Test'
name: "Build Test"

on:
pull_request:
branches: [main]
paths-ignore:
- '**/README.md'
- "**/README.md"
push:
branches: [main]
tags:
- 'v*'
- "v*"
paths-ignore:
- '**/README.md'
- "**/README.md"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down Expand Up @@ -42,12 +42,16 @@ jobs:
cargo install ic-wasm
./scripts/build.sh
./scripts/test.sh

env:
ALCHEMY_API_KEY: ${{ secrets.ALCHEMY_API_KEY }}

- name: test i686-unknown-linux-gnu
run: |
sudo apt update
sudo apt install gcc-multilib
./scripts/test.sh --target i686-unknown-linux-gnu
env:
ALCHEMY_API_KEY: ${{ secrets.ALCHEMY_API_KEY }}

release-binaries:
if: ${{github.ref_type == 'tag'}}
Expand Down Expand Up @@ -86,14 +90,14 @@ jobs:
target: ${{ matrix.platform.target }}
command: build
strip: true
args: '--release -p register_evm_agent'
args: "--release -p register_evm_agent"

- name: Prepare artifact files
run: |
mkdir -p ./target/artifact
mv target/${{ matrix.platform.target }}/release/${{ matrix.platform.bin }} ./target/artifact/${{ matrix.platform.bin }}

- name: 'Uploading artifact'
- name: "Uploading artifact"
uses: actions/upload-artifact@v3
with:
if-no-files-found: error
Expand All @@ -106,25 +110,25 @@ jobs:
needs: [release-binaries]
runs-on: ubuntu-latest
steps:
- name: 'Artifact Linux'
- name: "Artifact Linux"
uses: actions/download-artifact@v3
with:
name: Linux-x86_64
path: ./linux

- name: 'Artifact MacOs'
- name: "Artifact MacOs"
uses: actions/download-artifact@v3
with:
name: MacOS-x86_64
path: ./macos

- name: 'Artifact MacOs M1'
- name: "Artifact MacOs M1"
uses: actions/download-artifact@v3
with:
name: MacOS-M1
path: ./macos-m1

- name: 'Artifact Windows'
- name: "Artifact Windows"
uses: actions/download-artifact@v3
with:
name: Windows-x86_64
Expand Down
5 changes: 5 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ set -x #echo on

export RUST_BACKTRACE=full

if [ -z "$ALCHEMY_API_KEY" ]; then
echo "ALCHEMY_API_KEY is not set"
exit 1
fi

# before testing, the build.sh script should be executed
cargo test $@
cargo test $@ --all-features
94 changes: 45 additions & 49 deletions src/ethereum-json-rpc-client/tests/reqwest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,8 @@ use ethereum_json_rpc_client::{Client, EthGetLogsParams, EthJsonRpcClient};
use ethers_core::abi::{Function, Param, ParamType, StateMutability, Token};
use ethers_core::types::{BlockNumber, TransactionRequest, H160, H256, U256};
use jsonrpc_core::{Output, Response};
use rand::SeedableRng as _;
use serial_test::serial;

const ETHEREUM_JSON_API_ENDPOINTS: &[&str] = &[
"https://cloudflare-eth.com/",
"https://ethereum.publicnode.com",
"https://rpc.ankr.com/eth",
"https://nodes.mewapi.io/rpc/eth",
"https://eth-mainnet.gateway.pokt.network/v1/5f3453978e354ab992c4da79",
"https://eth-mainnet.nodereal.io/v1/1659dfb40aa24bbb8153a677b98064d7",
"https://eth.llamarpc.com",
"https://eth-mainnet.public.blastapi.io",
];
const MAX_BATCH_SIZE: usize = 5;

fn to_hash(string: &str) -> H256 {
Expand All @@ -33,59 +22,66 @@ fn to_hash(string: &str) -> H256 {
///
/// This was necessary because some RPC providers have rate limits and running the CI was more like a nightmare.
#[derive(Clone)]
pub struct MultiRpcReqwestClient;
pub struct AlchemyRpcReqwestClient {
apikey: String,
}

impl AlchemyRpcReqwestClient {
/// Get endpoint for Alchemy API
#[inline]
fn endpoint(&self) -> String {
format!(
"https://eth-mainnet.alchemyapi.io/v2/{apikey}",
apikey = self.apikey
)
}
}

impl Client for MultiRpcReqwestClient {
impl Client for AlchemyRpcReqwestClient {
fn send_rpc_request(
&self,
request: jsonrpc_core::Request,
) -> std::pin::Pin<
Box<dyn std::future::Future<Output = anyhow::Result<jsonrpc_core::Response>> + Send>,
> {
let endpoint = self.endpoint();

Box::pin(async move {
let mut rng = rand::rngs::StdRng::from_entropy();

use rand::seq::SliceRandom;
let mut err = None;
let mut endpoints = ETHEREUM_JSON_API_ENDPOINTS.to_vec();
endpoints.shuffle(&mut rng);
for rpc_endpoint in endpoints {
let client = ReqwestClient::new_with_client(
rpc_endpoint.to_string(),
reqwest::ClientBuilder::new()
.timeout(Duration::from_secs(10))
.build()
.unwrap(),
);
let result = client.send_rpc_request(request.clone()).await;

match result {
Ok(Response::Single(Output::Success(_))) => return result,
let client = ReqwestClient::new_with_client(
endpoint,
reqwest::ClientBuilder::new()
.timeout(Duration::from_secs(10))
.build()
.unwrap(),
);
let result = client.send_rpc_request(request.clone()).await;

match result {
Ok(Response::Single(Output::Success(_))) => result,
Ok(Response::Batch(batch))
if batch
.iter()
.all(|output| matches!(output, Output::Success(_))) =>
{
Ok(Response::Batch(batch))
if batch
.iter()
.all(|output| matches!(output, Output::Success(_))) =>
{
return Ok(Response::Batch(batch))
}
Ok(result) => {
println!("call failed: {result:?}");
err = Some(anyhow::anyhow!("call failed: {result:?}"));
}
Err(e) => {
println!("call failed: {e}");
err = Some(e);
}
}
Ok(result) => {
println!("call failed: {result:?}");
anyhow::bail!("call failed: {result:?}")
}
Err(e) => {
println!("call failed: {e}");
anyhow::bail!("call failed: {e}")
}
}

Err(err.unwrap())
})
}
}

fn reqwest_client() -> EthJsonRpcClient<MultiRpcReqwestClient> {
EthJsonRpcClient::new(MultiRpcReqwestClient)
fn reqwest_client() -> EthJsonRpcClient<AlchemyRpcReqwestClient> {
let apikey = std::env::var("ALCHEMY_API_KEY").expect("ALCHEMY_API_KEY is not set");
veeso marked this conversation as resolved.
Show resolved Hide resolved

EthJsonRpcClient::new(AlchemyRpcReqwestClient { apikey })
}

#[tokio::test]
Expand Down