fix(block): add Randomness when using celo (#44)

* fix(block): add Randomness when using celo

Also do not compile the fields which were removed in https://github.com/celo-org/celo-blockchain/pull/913/

* test(block): add celo block test
This commit is contained in:
Georgios Konstantopoulos 2020-07-02 16:05:27 +03:00 committed by GitHub
parent f88f0129f9
commit 1cbd86dbd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 1 deletions

View File

@ -2,6 +2,16 @@
use crate::types::{Address, Bloom, Bytes, H256, U256, U64}; use crate::types::{Address, Bloom, Bytes, H256, U256, U64};
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer}; use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
#[cfg(feature = "celo")]
///
pub struct Randomness {
/// The committed randomness for that block
pub committed: Bytes,
/// The revealed randomness for that block
pub revealed: Bytes,
}
/// The block type returned from RPC calls. /// The block type returned from RPC calls.
/// This is generic over a `TX` type which will be either the hash or the /// This is generic over a `TX` type which will be either the hash or the
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)] #[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
@ -12,6 +22,7 @@ pub struct Block<TX> {
#[serde(rename = "parentHash")] #[serde(rename = "parentHash")]
pub parent_hash: H256, pub parent_hash: H256,
/// Hash of the uncles /// Hash of the uncles
#[cfg(not(feature = "celo"))]
#[serde(rename = "sha3Uncles")] #[serde(rename = "sha3Uncles")]
pub uncles_hash: H256, pub uncles_hash: H256,
/// Miner/author's address. /// Miner/author's address.
@ -32,6 +43,7 @@ pub struct Block<TX> {
#[serde(rename = "gasUsed")] #[serde(rename = "gasUsed")]
pub gas_used: U256, pub gas_used: U256,
/// Gas Limit /// Gas Limit
#[cfg(not(feature = "celo"))]
#[serde(rename = "gasLimit")] #[serde(rename = "gasLimit")]
pub gas_limit: U256, pub gas_limit: U256,
/// Extra data /// Extra data
@ -43,6 +55,7 @@ pub struct Block<TX> {
/// Timestamp /// Timestamp
pub timestamp: U256, pub timestamp: U256,
/// Difficulty /// Difficulty
#[cfg(not(feature = "celo"))]
pub difficulty: U256, pub difficulty: U256,
/// Total difficulty /// Total difficulty
#[serde(rename = "totalDifficulty")] #[serde(rename = "totalDifficulty")]
@ -51,6 +64,7 @@ pub struct Block<TX> {
#[serde(default, rename = "sealFields")] #[serde(default, rename = "sealFields")]
pub seal_fields: Vec<Bytes>, pub seal_fields: Vec<Bytes>,
/// Uncles' hashes /// Uncles' hashes
#[cfg(not(feature = "celo"))]
pub uncles: Vec<H256>, pub uncles: Vec<H256>,
/// Transactions /// Transactions
pub transactions: Vec<TX>, pub transactions: Vec<TX>,
@ -58,9 +72,16 @@ pub struct Block<TX> {
pub size: Option<U256>, pub size: Option<U256>,
/// Mix Hash /// Mix Hash
#[serde(rename = "mixHash")] #[serde(rename = "mixHash")]
#[cfg(not(feature = "celo"))]
pub mix_hash: Option<H256>, pub mix_hash: Option<H256>,
/// Nonce /// Nonce
#[cfg(not(feature = "celo"))]
pub nonce: Option<U64>, pub nonce: Option<U64>,
#[cfg(feature = "celo")]
#[cfg_attr(docsrs, doc(cfg(feature = "celo")))]
/// The block's randomness
pub randomness: Randomness,
} }
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
@ -148,6 +169,7 @@ impl Serialize for BlockNumber {
} }
#[cfg(test)] #[cfg(test)]
#[cfg(not(feature = "celo"))]
mod tests { mod tests {
use super::*; use super::*;
use crate::types::{Transaction, TxHash}; use crate::types::{Transaction, TxHash};
@ -164,3 +186,16 @@ mod tests {
let _block: Block<Transaction> = serde_json::from_str(&block).unwrap(); let _block: Block<Transaction> = serde_json::from_str(&block).unwrap();
} }
} }
#[cfg(test)]
#[cfg(feature = "celo")]
mod celo_tests {
use super::*;
use crate::types::Transaction;
#[test]
fn block() {
let block = r#"{"extraData":"0xd983010000846765746889676f312e31332e3130856c696e7578000000000000f8b2c0c080b841cfa11585812ec794c4baa46178690971b3c72e367211d68a9ea318ff500c5aeb7099cafc965240e3b57cf7355341cf76bdca74530334658370d2df7b2e030ab200f582027db017810fa05b4f35927f968f6be1a61e322d4ace3563feb8a489690a91c031fda640c55c216f6712a7bdde994338a5610080f58203ffb093cd643f5154979952791ff714eb885df0f18012f2211fb8c29a8947130dc3adf4ecb48a3c4a142a0faa51e5c60b048180","gasUsed":"0xbef6","hash":"0x37ac2818e50e61f0566caea102ed98677f2552fa86fed53443315ed11fe0eaad","logsBloom":"0x00000800000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000100000000000000000000000000000000000000000000000000000000000000080000000000001020000400000000000000000000000000000000000000000000000000000000000080000000000000000000000000400000000000000000000000000000000000000100000040004000000000000800000000000000000084000000000000000000000000000000000020000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000","miner":"0xcda518f6b5a797c3ec45d37c65b83e0b0748edca","number":"0x1b4","parentHash":"0xa6b4775f600c2981f9142cbc1361db02c7ba8c185a1110537b255356876301a2","randomness":{"committed":"0x049e84c89f1aa0e3a770b2545b05a30eb814dae322e7247fd2bf27e6cacb1f51","revealed":"0x5a8826bf59a7ed1ee86a9d6464fa9c1fcece78ffa7cf32b11a03ad251ddcefe6"},"receiptsRoot":"0x1724dc3e7c2bfa03974c1deedf5ea20ad30b72e25f3c62fbb5fd06fc107068d7","size":"0x3a0","stateRoot":"0xc45fa03e69dccb54b4981d23d77328ab8906ddd7a0d8238b9c54ae1a14df4d1c","timestamp":"0x5e90166d","totalDifficulty":"0x1b5","transactions":[{"blockHash":"0x37ac2818e50e61f0566caea102ed98677f2552fa86fed53443315ed11fe0eaad","blockNumber":"0x1b4","from":"0x456f41406b32c45d59e539e4bba3d7898c3584da","gas":"0x1312d00","gasPrice":"0x174876e800","feeCurrency":null,"gatewayFeeRecipient":null,"gatewayFee":"0x0","hash":"0xf7b1b588b1fc03305f556805812273d80fb61fc0ba7f812de27189e95c5ecfc5","input":"0xed385274000000000000000000000000b9ff7ab50a2f0fd3e2fb2814b016ac90c91df98f03386ba30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000be951906eba2aa800000","nonce":"0x147","to":"0xa12a699c641cc875a7ca57495861c79c33d293b4","transactionIndex":"0x0","value":"0x0","v":"0x15e08","r":"0x5787d040d09a34cb2b9ffcd096be7fe66aa6a3ed0632f182d1f3045640a9ef8b","s":"0x7897f58740f2a1c645826579106a620c306fc56381520ae2f28880bb284c4abd"}],"transactionsRoot":"0xbc8cb40b809914b9cd735b12e9b1802cf5d85de5223a22bbdb249a7e8b45ec93"}"#;
let _block: Block<Transaction> = serde_json::from_str(&block).unwrap();
}
}

View File

@ -16,6 +16,9 @@ pub use bytes::Bytes;
mod block; mod block;
pub use block::{Block, BlockId, BlockNumber}; pub use block::{Block, BlockId, BlockNumber};
#[cfg(feature = "celo")]
pub use block::Randomness;
mod log; mod log;
pub use log::{Filter, Log, ValueOrArray}; pub use log::{Filter, Log, ValueOrArray};

View File

@ -89,8 +89,12 @@ mod eth_tests {
#[cfg(feature = "celo")] #[cfg(feature = "celo")]
mod celo_tests { mod celo_tests {
use super::*; use super::*;
use ethers::{providers::FilterStream, types::H256}; use ethers::{
providers::FilterStream,
types::{Randomness, H256},
};
use futures_util::stream::StreamExt; use futures_util::stream::StreamExt;
use rustc_hex::FromHex;
#[tokio::test] #[tokio::test]
// https://alfajores-blockscout.celo-testnet.org/tx/0x544ea96cddb16aeeaedaf90885c1e02be4905f3eb43d6db3f28cac4dbe76a625/internal_transactions // https://alfajores-blockscout.celo-testnet.org/tx/0x544ea96cddb16aeeaedaf90885c1e02be4905f3eb43d6db3f28cac4dbe76a625/internal_transactions
@ -108,6 +112,27 @@ mod celo_tests {
assert_eq!(tx.block_number.unwrap(), 1100845.into()) assert_eq!(tx.block_number.unwrap(), 1100845.into())
} }
#[tokio::test]
async fn get_block() {
let provider =
Provider::<Http>::try_from("https://alfajores-forno.celo-testnet.org").unwrap();
let block = provider.get_block(1342561).await.unwrap();
assert_eq!(
block.randomness,
Randomness {
committed: "a3a64b7a29bb4ddd49b7d9e3cf3dd14ecbb7f0321061706c634d14b15425dd30"
.from_hex::<Vec<u8>>()
.unwrap()
.into(),
revealed: "3c5f2f71941783cbe7f2dbd387c35503ca0470b300e1613866b988a1db8902a3"
.from_hex::<Vec<u8>>()
.unwrap()
.into(),
}
);
}
#[tokio::test] #[tokio::test]
async fn watch_blocks() { async fn watch_blocks() {
let provider = Provider::<Http>::try_from("https://alfajores-forno.celo-testnet.org") let provider = Provider::<Http>::try_from("https://alfajores-forno.celo-testnet.org")