lighthouse/testing/remote_signer_test/src/consumer.rs

153 lines
4.0 KiB
Rust
Raw Normal View History

use crate::*;
use remote_signer_client::api_response::SignatureApiResponse;
use remote_signer_consumer::{Error, RemoteSignerHttpConsumer, RemoteSignerObject};
use reqwest::ClientBuilder;
use sensitive_url::SensitiveUrl;
use serde::Serialize;
use tokio::runtime::Builder;
use tokio::time::Duration;
use types::{AttestationData, BeaconBlock, Epoch, EthSpec, Fork, Hash256};
pub fn set_up_test_consumer(test_signer_address: &str) -> RemoteSignerHttpConsumer {
set_up_test_consumer_with_timeout(test_signer_address, 12)
}
pub fn set_up_test_consumer_with_timeout(
test_signer_address: &str,
timeout: u64,
) -> RemoteSignerHttpConsumer {
let url = SensitiveUrl::parse(test_signer_address).unwrap();
let reqwest_client = ClientBuilder::new()
.timeout(Duration::from_secs(timeout))
.build()
.unwrap();
RemoteSignerHttpConsumer::from_components(url, reqwest_client)
}
pub fn do_sign_request<E: EthSpec, T: RemoteSignerObject>(
test_client: &RemoteSignerHttpConsumer,
test_input: RemoteSignerTestData<E, T>,
) -> Result<String, Error> {
let runtime = Builder::new_multi_thread().enable_all().build().unwrap();
Update to tokio 1.1 (#2172) ## Issue Addressed resolves #2129 resolves #2099 addresses some of #1712 unblocks #2076 unblocks #2153 ## Proposed Changes - Updates all the dependencies mentioned in #2129, except for web3. They haven't merged their tokio 1.0 update because they are waiting on some dependencies of their own. Since we only use web3 in tests, I think updating it in a separate issue is fine. If they are able to merge soon though, I can update in this PR. - Updates `tokio_util` to 0.6.2 and `bytes` to 1.0.1. - We haven't made a discv5 release since merging tokio 1.0 updates so I'm using a commit rather than release atm. **Edit:** I think we should merge an update of `tokio_util` to 0.6.2 into discv5 before this release because it has panic fixes in `DelayQueue` --> PR in discv5: https://github.com/sigp/discv5/pull/58 ## Additional Info tokio 1.0 changes that required some changes in lighthouse: - `interval.next().await.is_some()` -> `interval.tick().await` - `sleep` future is now `!Unpin` -> https://github.com/tokio-rs/tokio/issues/3028 - `try_recv` has been temporarily removed from `mpsc` -> https://github.com/tokio-rs/tokio/issues/3350 - stream features have moved to `tokio-stream` and `broadcast::Receiver::into_stream()` has been temporarily removed -> `https://github.com/tokio-rs/tokio/issues/2870 - I've copied over the `BroadcastStream` wrapper from this PR, but can update to use `tokio-stream` once it's merged https://github.com/tokio-rs/tokio/pull/3384 Co-authored-by: realbigsean <seananderson33@gmail.com>
2021-02-10 23:29:49 +00:00
runtime.block_on(test_client.sign(
&test_input.public_key,
test_input.bls_domain,
test_input.data,
test_input.fork,
test_input.genesis_validators_root,
Update to tokio 1.1 (#2172) ## Issue Addressed resolves #2129 resolves #2099 addresses some of #1712 unblocks #2076 unblocks #2153 ## Proposed Changes - Updates all the dependencies mentioned in #2129, except for web3. They haven't merged their tokio 1.0 update because they are waiting on some dependencies of their own. Since we only use web3 in tests, I think updating it in a separate issue is fine. If they are able to merge soon though, I can update in this PR. - Updates `tokio_util` to 0.6.2 and `bytes` to 1.0.1. - We haven't made a discv5 release since merging tokio 1.0 updates so I'm using a commit rather than release atm. **Edit:** I think we should merge an update of `tokio_util` to 0.6.2 into discv5 before this release because it has panic fixes in `DelayQueue` --> PR in discv5: https://github.com/sigp/discv5/pull/58 ## Additional Info tokio 1.0 changes that required some changes in lighthouse: - `interval.next().await.is_some()` -> `interval.tick().await` - `sleep` future is now `!Unpin` -> https://github.com/tokio-rs/tokio/issues/3028 - `try_recv` has been temporarily removed from `mpsc` -> https://github.com/tokio-rs/tokio/issues/3350 - stream features have moved to `tokio-stream` and `broadcast::Receiver::into_stream()` has been temporarily removed -> `https://github.com/tokio-rs/tokio/issues/2870 - I've copied over the `BroadcastStream` wrapper from this PR, but can update to use `tokio-stream` once it's merged https://github.com/tokio-rs/tokio/pull/3384 Co-authored-by: realbigsean <seananderson33@gmail.com>
2021-02-10 23:29:49 +00:00
))
}
#[derive(Serialize)]
pub struct BlockRequestBody<E: EthSpec> {
bls_domain: String,
data: BeaconBlock<E>,
fork: Fork,
genesis_validators_root: Hash256,
}
pub fn get_test_block_body(seed: u64) -> String {
let block: BeaconBlock<E> = get_block(seed);
let epoch = block.epoch();
let fork = Fork {
previous_version: [1; 4],
current_version: [2; 4],
epoch,
};
let genesis_validators_root = Hash256::from_low_u64_be(seed);
let block_request_body = BlockRequestBody {
bls_domain: "beacon_proposer".to_string(),
data: block,
fork,
genesis_validators_root,
};
serde_json::to_string(&block_request_body).unwrap()
}
#[derive(Serialize)]
pub struct AttestationRequestBody {
bls_domain: String,
data: AttestationData,
fork: Fork,
genesis_validators_root: Hash256,
}
pub fn get_test_attestation_body(seed: u64) -> String {
let attestation = get_attestation::<E>(seed);
let epoch = attestation.target.epoch;
let fork = Fork {
previous_version: [1; 4],
current_version: [2; 4],
epoch,
};
let genesis_validators_root = Hash256::from_low_u64_be(seed);
let attestation_request_body = AttestationRequestBody {
bls_domain: "beacon_attester".to_string(),
data: attestation,
fork,
genesis_validators_root,
};
serde_json::to_string(&attestation_request_body).unwrap()
}
#[derive(Serialize)]
pub struct RandaoRequestBody {
bls_domain: String,
data: Epoch,
fork: Fork,
genesis_validators_root: Hash256,
}
pub fn get_test_randao_body(seed: u64) -> String {
let epoch = Epoch::new(seed);
let fork = Fork {
previous_version: [1; 4],
current_version: [2; 4],
epoch,
};
let genesis_validators_root = Hash256::from_low_u64_be(seed);
let randao_request_body = RandaoRequestBody {
bls_domain: "randao".to_string(),
data: epoch,
fork,
genesis_validators_root,
};
serde_json::to_string(&randao_request_body).unwrap()
}
pub fn assert_sign_ok(resp: ApiTestResponse, expected_signature: &str) {
assert_eq!(resp.status, 200);
assert_eq!(
serde_json::from_value::<SignatureApiResponse>(resp.json)
.unwrap()
.signature,
expected_signature
);
}
pub fn assert_sign_error(resp: ApiTestResponse, http_status: u16, error_msg: &str) {
assert_eq!(resp.status, http_status);
assert_eq!(
resp.json["error"]
.as_str()
.unwrap()
// cross-platform compatiblity
.replace("\\", ""),
error_msg
);
}