use hex decode instead of parse

This commit is contained in:
realbigsean 2023-02-07 21:32:36 -05:00
parent a42d07592c
commit 8661477675
No known key found for this signature in database
GPG Key ID: BE1B3DB104F6C788
2 changed files with 68 additions and 22 deletions

View File

@ -14,7 +14,7 @@ pub use engine_api::{http, http::deposit_methods, http::HttpJsonRpc};
use engines::{Engine, EngineError};
pub use engines::{EngineState, ForkchoiceState};
use eth2::types::{builder_bid::SignedBuilderBid, ForkVersionedResponse};
use ethers_core::types::Transaction as EthersTransaction;
use ethers_core::types::{Transaction as EthersTransaction, U64};
use fork_choice::ForkchoiceUpdateParameters;
use lru::LruCache;
use payload_status::process_payload_status;
@ -39,8 +39,10 @@ use tokio::{
};
use tokio_stream::wrappers::WatchStream;
use types::consts::eip4844::BLOB_TX_TYPE;
use types::transaction::{AccessTuple, BlobTransaction};
use types::{AbstractExecPayload, BeaconStateError, Blob, ExecPayload, KzgCommitment};
use types::transaction::{AccessTuple, BlobTransaction, EcdsaSignature, SignedBlobTransaction};
use types::{
AbstractExecPayload, BeaconStateError, Blob, ExecPayload, KzgCommitment, VersionedHash,
};
use types::{
BlindedPayload, BlockType, ChainSpec, Epoch, ExecutionBlockHash, ForkName,
ProposerPreparationData, PublicKeyBytes, Signature, SignedBeaconBlock, Slot, Transaction,
@ -2030,10 +2032,14 @@ pub enum BlobTxConversionError {
MaxFeePerDataGasMissing,
/// Missing the `blob_versioned_hashes` field.
BlobVersionedHashesMissing,
/// `y_parity` field was greater than one.
InvalidYParity,
/// There was an error converting the transaction to SSZ.
SszError(ssz_types::Error),
/// There was an error converting the transaction from JSON.
SerdeJson(serde_json::Error),
/// There was an error converting the transaction from hex.
FromHexError(String),
}
impl From<ssz_types::Error> for BlobTxConversionError {
@ -2096,23 +2102,36 @@ fn ethers_tx_to_bytes<T: EthSpec>(
})
.collect::<Result<Vec<AccessTuple>, BlobTxConversionError>>()?,
)?;
let max_fee_per_data_gas = transaction
let max_fee_per_data_gas = Uint256::from_big_endian(
&eth2_serde_utils::hex::decode(
transaction
.other
.get("maxFeePerDataGas")
.ok_or(BlobTxConversionError::MaxFeePerDataGasMissing)?
.as_str()
.ok_or(BlobTxConversionError::MaxFeePerDataGasMissing)?,
)
.map_err(BlobTxConversionError::FromHexError)?,
);
let blob_versioned_hashes = transaction
.other
.get("maxFeePerDataGas")
.ok_or(BlobTxConversionError::MaxFeePerDataGasMissing)?
.as_str()
.ok_or(BlobTxConversionError::MaxFeePerDataGasMissing)?
.parse()
.map_err(|_| BlobTxConversionError::MaxFeePerDataGasMissing)?;
let blob_versioned_hashes = serde_json::from_str(
transaction
.other
.get("blobVersionedHashes")
.ok_or(BlobTxConversionError::BlobVersionedHashesMissing)?
.as_str()
.ok_or(BlobTxConversionError::BlobVersionedHashesMissing)?,
)?;
BlobTransaction {
.get("blobVersionedHashes")
.ok_or(BlobTxConversionError::BlobVersionedHashesMissing)?
.as_array()
.ok_or(BlobTxConversionError::BlobVersionedHashesMissing)?
.into_iter()
.map(|versioned_hash| {
Ok(Hash256::from_slice(
&eth2_serde_utils::hex::decode(
versioned_hash
.as_str()
.ok_or(BlobTxConversionError::BlobVersionedHashesMissing)?,
)
.map_err(BlobTxConversionError::FromHexError)?,
))
})
.collect::<Result<Vec<VersionedHash>, BlobTxConversionError>>()?;
let message = BlobTransaction {
chain_id,
nonce,
max_priority_fee_per_gas,
@ -2123,9 +2142,23 @@ fn ethers_tx_to_bytes<T: EthSpec>(
data,
access_list,
max_fee_per_data_gas,
blob_versioned_hashes,
}
.as_ssz_bytes()
blob_versioned_hashes: VariableList::new(blob_versioned_hashes)?,
};
let y_parity = if transaction.v == U64::zero() {
false
} else if transaction.v == U64::one() {
true
} else {
return Err(BlobTxConversionError::InvalidYParity);
};
let r = transaction.r;
let s = transaction.s;
let signature = EcdsaSignature { y_parity, r, s };
let mut signed_tx = SignedBlobTransaction { message, signature }.as_ssz_bytes();
signed_tx.insert(0, BLOB_TX_TYPE);
signed_tx
} else {
transaction.rlp().to_vec()
};

View File

@ -9,6 +9,12 @@ pub type MaxAccessListSize = U16777216;
pub type MaxVersionedHashesListSize = U16777216;
pub type MaxAccessListStorageKeys = U16777216;
#[derive(Debug, Clone, PartialEq, Encode, Decode)]
pub struct SignedBlobTransaction {
pub message: BlobTransaction,
pub signature: EcdsaSignature,
}
#[derive(Debug, Clone, PartialEq, Encode, Decode)]
pub struct BlobTransaction {
pub chain_id: Uint256,
@ -29,3 +35,10 @@ pub struct AccessTuple {
pub address: Address,
pub storage_keys: VariableList<Hash256, MaxAccessListStorageKeys>,
}
#[derive(Debug, Clone, PartialEq, Encode, Decode)]
pub struct EcdsaSignature {
pub y_parity: bool,
pub r: Uint256,
pub s: Uint256,
}