Skip to content

Commit

Permalink
remove mainnet feature
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnReedV committed Nov 21, 2024
1 parent 40f220b commit c92519e
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 115 deletions.
3 changes: 0 additions & 3 deletions pallets/drand/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,3 @@ try-runtime = [
"frame-system/try-runtime",
"sp-runtime/try-runtime",
]

# use the drand mainnet configuration
mainnet = []
8 changes: 0 additions & 8 deletions pallets/drand/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,9 @@ use crate::Pallet as Drand;
use frame_benchmarking::v2::*;
use frame_system::RawOrigin;

#[cfg(not(feature = "mainnet"))]
pub const DRAND_PULSE: &str = "{\"round\":1000,\"randomness\":\"fe290beca10872ef2fb164d2aa4442de4566183ec51c56ff3cd603d930e54fdd\",\"signature\":\"b44679b9a59af2ec876b1a6b1ad52ea9b1615fc3982b19576350f93447cb1125e342b73a8dd2bacbe47e4b6b63ed5e39\"}";
#[cfg(not(feature = "mainnet"))]
pub const DRAND_INFO_RESPONSE: &str = "{\"public_key\":\"83cf0f2896adee7eb8b5f01fcad3912212c437e0073e911fb90022d3e760183c8c4b450b6a0a6c3ac6a5776a2d1064510d1fec758c921cc22b0e17e63aaf4bcb5ed66304de9cf809bd274ca73bab4af5a6e9c76a4bc09e76eae8991ef5ece45a\",\"period\":3,\"genesis_time\":1692803367,\"hash\":\"52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971\",\"groupHash\":\"f477d5c89f21a17c863a7f937c6a6d15859414d2be09cd448d4279af331c5d3e\",\"schemeID\":\"bls-unchained-g1-rfc9380\",\"metadata\":{\"beaconID\":\"quicknet\"}}";

// mainnet parameters
#[cfg(feature = "mainnet")]
pub const DRAND_PULSE: &str = "{\"round\":1000,\"randomness\":\"a40d3e0e7e3c71f28b7da2fd339f47f0bcf10910309f5253d7c323ec8cea3212\",\"signature\":\"99bf96de133c3d3937293cfca10c8152b18ab2d034ccecf115658db324d2edc00a16a2044cd04a8a38e2a307e5ecff3511315be8d282079faf24098f283e0ed2c199663b334d2e84c55c032fe469b212c5c2087ebb83a5b25155c3283f5b79ac\",\"previous_signature\":\"af0d93299a363735fe847f5ea241442c65843dc1bd3a7b79646b3b10072e908bf034d35cd69d378e3341f139100cd4cd03030399864ef8803a5a4f5e64fccc20bbae36d1ca22a6ddc43d2630c41105e90598fab11e5c7456df3925d4b577b113\"}";
#[cfg(feature = "mainnet")]
pub const DRAND_INFO_RESPONSE: &str = "{\"public_key\":\"868f005eb8e6e4ca0a47c8a77ceaa5309a47978a7c71bc5cce96366b5d7a569937c529eeda66c7293784a9402801af31\",\"period\":30,\"genesis_time\":1595431050,\"hash\":\"8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce\",\"groupHash\":\"176f93498eac9ca337150b46d21dd58673ea4e3581185f869672e59fa4cb390a\",\"schemeID\":\"pedersen-bls-chained\",\"metadata\":{\"beaconID\":\"default\"}}";

#[benchmarks(
where
T::Public: From<sp_core::sr25519::Public>,
Expand Down
8 changes: 0 additions & 8 deletions pallets/drand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,7 @@ pub const API_ENDPOINT: &str = "https://drand.cloudflare.com";
/// quicknet uses 'Tiny' BLS381, with small 48-byte sigs in G1 and 96-byte pubkeys in G2
pub const QUICKNET_CHAIN_HASH: &str =
"52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971";
/// the drand mainnet chain hash
/// mainnext uses 'Usual' BLS381, with 96-byte sigs in G2 and 48-byte pubkeys in G1
pub const MAINNET_CHAIN_HASH: &str =
"8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce";

#[cfg(feature = "mainnet")]
const CHAIN_HASH: &str = MAINNET_CHAIN_HASH;

#[cfg(not(feature = "mainnet"))]
const CHAIN_HASH: &str = QUICKNET_CHAIN_HASH;

pub const MAX_PULSES_TO_FETCH: u64 = 50;
Expand Down
5 changes: 0 additions & 5 deletions pallets/drand/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,7 @@ impl pallet_drand_bridge::Config for Test {
type AuthorityId = crypto::TestAuthId;
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_drand_bridge::weights::SubstrateWeight<Test>;

#[cfg(feature = "mainnet")]
type Verifier = MainnetVerifier;
#[cfg(not(feature = "mainnet"))]
type Verifier = QuicknetVerifier;

type UnsignedPriority = UnsignedPriority;
type HttpFetchTimeout = ConstU64<1_000>;
}
Expand Down
15 changes: 2 additions & 13 deletions pallets/drand/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,9 @@ use sp_runtime::{
pub const ROUND_NUMBER: u64 = 1000;

// Quicknet parameters
#[cfg(not(feature = "mainnet"))]
pub const DRAND_PULSE: &str = "{\"round\":1000,\"randomness\":\"fe290beca10872ef2fb164d2aa4442de4566183ec51c56ff3cd603d930e54fdd\",\"signature\":\"b44679b9a59af2ec876b1a6b1ad52ea9b1615fc3982b19576350f93447cb1125e342b73a8dd2bacbe47e4b6b63ed5e39\"}";
#[cfg(not(feature = "mainnet"))]
pub const DRAND_INFO_RESPONSE: &str = "{\"public_key\":\"83cf0f2896adee7eb8b5f01fcad3912212c437e0073e911fb90022d3e760183c8c4b450b6a0a6c3ac6a5776a2d1064510d1fec758c921cc22b0e17e63aaf4bcb5ed66304de9cf809bd274ca73bab4af5a6e9c76a4bc09e76eae8991ef5ece45a\",\"period\":3,\"genesis_time\":1692803367,\"hash\":\"52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971\",\"groupHash\":\"f477d5c89f21a17c863a7f937c6a6d15859414d2be09cd448d4279af331c5d3e\",\"schemeID\":\"bls-unchained-g1-rfc9380\",\"metadata\":{\"beaconID\":\"quicknet\"}}";

// Mainnet parameters
#[cfg(feature = "mainnet")]
pub const DRAND_PULSE: &str = "{\"round\":1000,\"randomness\":\"a40d3e0e7e3c71f28b7da2fd339f47f0bcf10910309f5253d7c323ec8cea3212\",\"signature\":\"99bf96de133c3d3937293cfca10c8152b18ab2d034ccecf115658db324d2edc00a16a2044cd04a8a38e2a307e5ecff3511315be8d282079faf24098f283e0ed2c199663b334d2e84c55c032fe469b212c5c2087ebb83a5b25155c3283f5b79ac\",\"previous_signature\":\"af0d93299a363735fe847f5ea241442c65843dc1bd3a7b79646b3b10072e908bf034d35cd69d378e3341f139100cd4cd03030399864ef8803a5a4f5e64fccc20bbae36d1ca22a6ddc43d2630c41105e90598fab11e5c7456df3925d4b577b113\"}";
#[cfg(feature = "mainnet")]
pub const DRAND_INFO_RESPONSE: &str = "{\"public_key\":\"868f005eb8e6e4ca0a47c8a77ceaa5309a47978a7c71bc5cce96366b5d7a569937c529eeda66c7293784a9402801af31\",\"period\":30,\"genesis_time\":1595431050,\"hash\":\"8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce\",\"groupHash\":\"176f93498eac9ca337150b46d21dd58673ea4e3581185f869672e59fa4cb390a\",\"schemeID\":\"pedersen-bls-chained\",\"metadata\":{\"beaconID\":\"default\"}}";

#[test]
fn it_can_submit_valid_pulse_when_beacon_config_exists() {
new_test_ext().execute_with(|| {
Expand Down Expand Up @@ -117,10 +109,7 @@ fn it_rejects_invalid_pulse_due_to_bad_signature() {
));

// Get a bad pulse (invalid signature within the pulse data)
#[cfg(not(feature = "mainnet"))]
let bad_http_response = "{\"round\":1000,\"randomness\":\"87f03ef5f62885390defedf60d5b8132b4dc2115b1efc6e99d166a37ab2f3a02\",\"signature\":\"b0a8b04e009cf72534321aca0f50048da596a3feec1172a0244d9a4a623a3123d0402da79854d4c705e94bc73224c341\"}";
#[cfg(feature = "mainnet")]
let bad_http_response = "{\"round\":1000,\"randomness\":\"87f03ef5f62885390defedf60d5b8132b4dc2115b1efc6e99d166a37ab2f3a02\",\"signature\":\"b0a8b04e009cf72534321aca0f50048da596a3feec1172a0244d9a4a623a3123d0402da79854d4c705e94bc73224c341\", \"previous_signature\":\"af0d93299a363735fe847f5ea241442c65843dc1bd3a7b79646b3b10072e908bf034d35cd69d378e3341f139100cd4cd03030399864ef8803a5a4f5e64fccc20bbae36d1ca22a6ddc43d2630c41105e90598fab11e5c7456df3925d4b577b113\"}";
let u_p: DrandResponseBody = serde_json::from_str(bad_http_response).unwrap();
let p: Pulse = u_p.try_into_pulse().unwrap();

Expand Down Expand Up @@ -380,14 +369,14 @@ fn can_execute_and_handle_valid_http_responses() {
let mut state = state.write();
state.expect_request(PendingRequest {
method: "GET".into(),
uri: "https://drand.cloudflare.com/8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce/public/1".into(),
uri: "https://drand.cloudflare.com/52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971/public/1".into(),
response: Some(DRAND_PULSE.as_bytes().to_vec()),
sent: true,
..Default::default()
});
state.expect_request(PendingRequest {
method: "GET".into(),
uri: "https://drand.cloudflare.com/8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce/public/latest".into(),
uri: "https://drand.cloudflare.com/52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971/public/latest".into(),
response: Some(DRAND_PULSE.as_bytes().to_vec()),
sent: true,
..Default::default()
Expand Down
24 changes: 4 additions & 20 deletions pallets/drand/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ use frame_support::pallet_prelude::*;
use serde::{Deserialize, Serialize};
use subtensor_macros::freeze_struct;

/// Represents an opaque public key used in drand's mainnet
#[cfg(not(feature = "mainnet"))]
pub type OpaquePublicKey = BoundedVec<u8, ConstU32<96>>;
/// Represents an opaque public key used in drand's quicknet
#[cfg(feature = "mainnet")]
pub type OpaquePublicKey = BoundedVec<u8, ConstU32<48>>;
pub type OpaquePublicKey = BoundedVec<u8, ConstU32<96>>;

/// an opaque hash type
pub type BoundedHash = BoundedVec<u8, ConstU32<32>>;
/// the round number to track rounds of the beacon
Expand Down Expand Up @@ -87,7 +84,7 @@ impl BeaconInfoResponse {

/// a pulse from the drand beacon
/// the expected response body from the drand api endpoint `api.drand.sh/{chainId}/public/latest`
#[freeze_struct("fa1e760d5c707d26")]
#[freeze_struct("a3fed2c99a0638bf")]
#[derive(Debug, Decode, Default, PartialEq, Encode, Serialize, Deserialize)]
pub struct DrandResponseBody {
/// the randomness round number
Expand All @@ -100,10 +97,6 @@ pub struct DrandResponseBody {
// TODO: use Signature (https://github.com/ideal-lab5/pallet-drand/issues/2)
#[serde(with = "hex::serde")]
pub signature: Vec<u8>,
/// only used when running in 'chained' mode using drand's mainnet
#[cfg(feature = "mainnet")]
#[serde(with = "hex::serde")]
pub previous_signature: Vec<u8>,
}

impl DrandResponseBody {
Expand All @@ -115,17 +108,10 @@ impl DrandResponseBody {
let bounded_signature = BoundedVec::<u8, ConstU32<144>>::try_from(self.signature.clone())
.map_err(|_| "Failed to convert signature")?;

#[cfg(feature = "mainnet")]
let bounded_prev_signature =
BoundedVec::<u8, ConstU32<144>>::try_from(self.signature.clone())
.map_err(|_| "Failed to convert signature")?;

Ok(Pulse {
round: self.round,
randomness: bounded_randomness,
signature: bounded_signature,
#[cfg(feature = "mainnet")]
previous_signature: bounded_prev_signature,
})
}
}
Expand Down Expand Up @@ -182,7 +168,7 @@ pub struct Metadata {
}

/// A pulse from the drand beacon
#[freeze_struct("cedd7283835f3f56")]
#[freeze_struct("de1a209f66f482b4")]
#[derive(
Clone,
Debug,
Expand All @@ -206,8 +192,6 @@ pub struct Pulse {
// TODO: use Signature (https://github.com/ideal-lab5/pallet-drand/issues/2)
// maybe add the sig size as a generic?
pub signature: BoundedVec<u8, ConstU32<144>>,
#[cfg(feature = "mainnet")]
pub previous_signature: BoundedVec<u8, ConstU32<144>>,
}

/// Payload used by to hold the pulse
Expand Down
58 changes: 0 additions & 58 deletions pallets/drand/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,64 +102,6 @@ impl Verifier for QuicknetVerifier {
}
}

/// A verifier to check values received from drand's mainnet. It outputs true if valid, false otherwise
///
/// The [Mainnet](https://drand.love/) operates in an chained mode.
/// so each round signs messages that hash the previous signature with the round number.
/// It uses a 'usual' BLS approach, with 48-byte public keys in G1 and 96-byte signatures in G2
///
/// Values are valid if the pairing equality holds:
/// $e(g_1, sig) == e(pk, msg_on_curve)$
///
/// where
/// $sig \in \mathbb{G}_2$ is the signature
/// $g_1 \in \mathbb{G}_1$ is a generator
/// $msg_on_curve \in \mathbb{G}_1$ is a hash of the previous signature and current round number (hash(prev_sig || round_number))
/// $pk \in \mathbb{G}_1$ is the public key, read from the input public parameters
///
pub struct MainnetVerifier;
#[cfg(feature = "mainnet")]
use w3f_bls::ZBLS;
#[cfg(feature = "mainnet")]
#[allow(clippy::let_unit_value)]
#[allow(clippy::unit_cmp)]
impl Verifier for MainnetVerifier {
fn verify(beacon_config: BeaconConfiguration, pulse: Pulse) -> Result<bool, String> {
// decode public key (pk)
let pk =
ArkScale::<G1AffineOpt>::decode(&mut beacon_config.public_key.into_inner().as_slice())
.map_err(|e| format!("Failed to decode public key: {}", e))?;

// decode signature (sigma)
let signature =
ArkScale::<G2AffineOpt>::decode(&mut pulse.signature.into_inner().as_slice())
.map_err(|e| format!("Failed to decode signature: {}", e))?;

// m = sha256(previous_signature || round)
let message = message(pulse.round, &pulse.previous_signature);
let hasher = <ZBLS as EngineBLS>::hash_to_curve_map();
// H(m) \in G1
let message_hash = hasher
.hash(&message)
.map_err(|e| format!("Failed to hash message: {}", e))?;

let mut bytes = Vec::new();
message_hash
.serialize_compressed(&mut bytes)
.map_err(|e| format!("Failed to serialize message hash: {}", e))?;

let message_on_curve = ArkScale::<G2AffineOpt>::decode(&mut &bytes[..])
.map_err(|e| format!("Failed to decode message on curve: {}", e))?;

let g1 = G1AffineOpt::generator();

let p1 = bls12_381::pairing_opt(g1, -signature.0);
let p2 = bls12_381::pairing_opt(pk.0, message_on_curve.0);

Ok(p1 == p2)
}
}

/// The unsafe skip verifier is just a pass-through verification, always returns true
pub struct UnsafeSkipVerifier;
impl Verifier for UnsafeSkipVerifier {
Expand Down

0 comments on commit c92519e

Please sign in to comment.