Initial merge changes

Added Execution Payload from Rayonism Fork

Updated new Containers to match Merge Spec

Updated BeaconBlockBody for Merge Spec

Completed updating BeaconState and BeaconBlockBody

Modified ExecutionPayload<T> to use Transaction<T>

Mostly Finished Changes for beacon-chain.md

Added some things for fork-choice.md

Update to match new fork-choice.md/fork.md changes

ran cargo fmt

Added Missing Pieces in eth2_libp2p for Merge

fix ef test

Various Changes to Conform Closer to Merge Spec
This commit is contained in:
Mark Mackey
2021-09-08 13:45:22 -05:00
committed by Paul Hauner
parent fe75a0a9a1
commit 5687c56d51
50 changed files with 1241 additions and 133 deletions

View File

@@ -19,7 +19,7 @@ use std::fmt;
use std::ops::Range;
use std::str::FromStr;
use std::time::Duration;
use types::Hash256;
use types::{Hash256, PowBlock, Uint256};
/// `keccak("DepositEvent(bytes,bytes,bytes,bytes,bytes)")`
pub const DEPOSIT_EVENT_TOPIC: &str =
@@ -49,6 +49,7 @@ pub enum Eth1Id {
#[derive(Clone, Copy)]
pub enum BlockQuery {
Number(u64),
Hash(Hash256),
Latest,
}
@@ -135,13 +136,6 @@ pub async fn get_chain_id(endpoint: &SensitiveUrl, timeout: Duration) -> Result<
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct Block {
pub hash: Hash256,
pub timestamp: u64,
pub number: u64,
}
/// Returns the current block number.
///
/// Uses HTTP JSON RPC at `endpoint`. E.g., `http://localhost:8545`.
@@ -156,40 +150,74 @@ pub async fn get_block_number(endpoint: &SensitiveUrl, timeout: Duration) -> Res
.map_err(|e| format!("Failed to get block number: {}", e))
}
/// Gets a block hash by block number.
/// Gets a block by hash or block number.
///
/// Uses HTTP JSON RPC at `endpoint`. E.g., `http://localhost:8545`.
pub async fn get_block(
endpoint: &SensitiveUrl,
query: BlockQuery,
timeout: Duration,
) -> Result<Block, String> {
) -> Result<PowBlock, String> {
let query_param = match query {
BlockQuery::Number(block_number) => format!("0x{:x}", block_number),
BlockQuery::Hash(hash) => format!("{:?}", hash), // debug formatting ensures output not truncated
BlockQuery::Latest => "latest".to_string(),
};
let rpc_method = match query {
BlockQuery::Number(_) | BlockQuery::Latest => "eth_getBlockByNumber",
BlockQuery::Hash(_) => "eth_getBlockByHash",
};
let params = json!([
query_param,
false // do not return full tx objects.
]);
let response_body = send_rpc_request(endpoint, "eth_getBlockByNumber", params, timeout).await?;
let response_body = send_rpc_request(endpoint, rpc_method, params, timeout).await?;
let response = response_result_or_error(&response_body)
.map_err(|e| format!("eth_getBlockByNumber failed: {}", e))?;
.map_err(|e| format!("{} failed: {}", rpc_method, e))?;
let hash: Vec<u8> = hex_to_bytes(
let block_hash: Vec<u8> = hex_to_bytes(
response
.get("hash")
.ok_or("No hash for block")?
.as_str()
.ok_or("Block hash was not string")?,
)?;
let hash: Hash256 = if hash.len() == 32 {
Hash256::from_slice(&hash)
let block_hash: Hash256 = if block_hash.len() == 32 {
Hash256::from_slice(&block_hash)
} else {
return Err(format!("Block has was not 32 bytes: {:?}", hash));
return Err(format!("Block hash was not 32 bytes: {:?}", block_hash));
};
let parent_hash: Vec<u8> = hex_to_bytes(
response
.get("parentHash")
.ok_or("No parent hash for block")?
.as_str()
.ok_or("Parent hash was not string")?,
)?;
let parent_hash: Hash256 = if parent_hash.len() == 32 {
Hash256::from_slice(&parent_hash)
} else {
return Err(format!("parent hash was not 32 bytes: {:?}", parent_hash));
};
let total_difficulty_str = response
.get("totalDifficulty")
.ok_or("No total difficulty for block")?
.as_str()
.ok_or("Total difficulty was not a string")?;
let total_difficulty = Uint256::from_str(total_difficulty_str)
.map_err(|e| format!("total_difficulty from_str {:?}", e))?;
let difficulty_str = response
.get("difficulty")
.ok_or("No difficulty for block")?
.as_str()
.ok_or("Difficulty was not a string")?;
let difficulty =
Uint256::from_str(difficulty_str).map_err(|e| format!("difficulty from_str {:?}", e))?;
let timestamp = hex_to_u64_be(
response
.get("timestamp")
@@ -198,7 +226,7 @@ pub async fn get_block(
.ok_or("Block timestamp was not string")?,
)?;
let number = hex_to_u64_be(
let block_number = hex_to_u64_be(
response
.get("number")
.ok_or("No number for block")?
@@ -206,14 +234,20 @@ pub async fn get_block(
.ok_or("Block number was not string")?,
)?;
if number <= usize::max_value() as u64 {
Ok(Block {
hash,
if block_number <= usize::max_value() as u64 {
Ok(PowBlock {
block_hash,
parent_hash,
total_difficulty,
difficulty,
timestamp,
number,
block_number,
})
} else {
Err(format!("Block number {} is larger than a usize", number))
Err(format!(
"Block number {} is larger than a usize",
block_number
))
}
.map_err(|e| format!("Failed to get block number: {}", e))
}

View File

@@ -21,7 +21,7 @@ use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::sync::RwLock as TRwLock;
use tokio::time::{interval_at, Duration, Instant};
use types::{ChainSpec, EthSpec, Unsigned};
use types::{ChainSpec, EthSpec, ExecutionPayload, Unsigned};
/// Indicates the default eth1 network id we use for the deposit contract.
pub const DEFAULT_NETWORK_ID: Eth1Id = Eth1Id::Goerli;
@@ -331,6 +331,8 @@ pub enum SingleEndpointError {
GetDepositCountFailed(String),
/// Failed to read the deposit contract root from the eth1 node.
GetDepositLogsFailed(String),
/// Failed to run engine_ExecutePayload
EngineExecutePayloadFailed,
}
#[derive(Debug, PartialEq)]
@@ -669,6 +671,21 @@ impl Service {
}
}
/// This is were we call out to engine_executePayload to determine if payload is valid
pub async fn on_payload<T: EthSpec>(
&self,
_execution_payload: ExecutionPayload<T>,
) -> Result<bool, Error> {
let endpoints = self.init_endpoints();
// TODO: call engine_executePayload and figure out how backup endpoint works..
endpoints
.first_success(|_e| async move { Ok(true) })
.await
.map(|(res, _)| res)
.map_err(Error::FallbackError)
}
/// Update the deposit and block cache, returning an error if either fail.
///
/// ## Returns
@@ -1242,7 +1259,7 @@ async fn download_eth1_block(
});
// Performs a `get_blockByNumber` call to an eth1 node.
let http_block = get_block(
let pow_block = get_block(
endpoint,
block_number_opt
.map(BlockQuery::Number)
@@ -1253,9 +1270,9 @@ async fn download_eth1_block(
.await?;
Ok(Eth1Block {
hash: http_block.hash,
number: http_block.number,
timestamp: http_block.timestamp,
hash: pow_block.block_hash,
number: pow_block.block_number,
timestamp: pow_block.timestamp,
deposit_root,
deposit_count,
})