Skip to content

Commit

Permalink
Implement new raw transaction (#2279)
Browse files Browse the repository at this point in the history
* Implement new raw transaction

Signed-off-by: deniallugo <[email protected]>

* Fix gas price

Signed-off-by: deniallugo <[email protected]>
  • Loading branch information
Deniallugo committed Jul 29, 2022
1 parent 3dae0a8 commit 0d9b71e
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 99 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/lib/config/src/configs/eth_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::envy_load;
#[derive(Debug, Deserialize, Clone, PartialEq)]
pub struct ETHClientConfig {
/// Numeric identifier of the L1 network (e.g. `9` for localhost).
pub chain_id: u8,
pub chain_id: u64,
/// How much do we want to increase gas price provided by the network?
/// Normally it's 1, we use the network-provided price (and limit it with the gas adjuster in eth sender).
/// However, it can be increased to speed up the transaction mining time.
Expand Down
10 changes: 7 additions & 3 deletions core/lib/eth_client/src/clients/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct ETHDirectClientInner<S: EthereumSigner> {
sender_account: Address,
contract_addr: H160,
contract: ethabi::Contract,
chain_id: u8,
chain_id: u64,
gas_price_factor: f64,
web3: Web3<Http>,
}
Expand Down Expand Up @@ -64,7 +64,7 @@ impl<S: EthereumSigner> ETHDirectClient<S> {
operator_eth_addr: H160,
eth_signer: S,
contract_eth_addr: H160,
chain_id: u8,
chain_id: u64,
gas_price_factor: f64,
) -> Self {
Self {
Expand Down Expand Up @@ -200,12 +200,16 @@ impl<S: EthereumSigner> ETHDirectClient<S> {
// form and sign tx
let tx = RawTransaction {
chain_id: self.inner.chain_id,
transaction_type: None,
access_list: None,
max_fee_per_gas: Default::default(),
nonce,
to: Some(contract_addr),
value: options.value.unwrap_or_default(),
gas_price,
gas,
data,
max_priority_fee_per_gas: Default::default(),
};

let signed_tx = self.inner.eth_signer.sign_transaction(tx).await?;
Expand Down Expand Up @@ -476,7 +480,7 @@ impl<S: EthereumSigner> ETHDirectClient<S> {
self.inner.contract_addr
}

pub fn chain_id(&self) -> u8 {
pub fn chain_id(&self) -> u64 {
self.inner.chain_id
}

Expand Down
2 changes: 2 additions & 0 deletions core/lib/eth_signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ thiserror = "1.0"

jsonrpc-core = "17"
async-trait = "0.1"
web3 = "0.18.0"
secp256k1 = { version = "0.21", features = ["std", "recovery"] }

[dev-dependencies]
actix-rt = "2"
Expand Down
4 changes: 4 additions & 0 deletions core/lib/eth_signer/src/json_rpc_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,16 @@ mod tests {
let transaction_signature = client
.sign_transaction(RawTransaction {
chain_id: 0,
transaction_type: None,
access_list: None,
max_fee_per_gas: Default::default(),
nonce: Default::default(),
to: None,
value: Default::default(),
gas_price: Default::default(),
gas: Default::default(),
data: vec![],
max_priority_fee_per_gas: Default::default(),
})
.await
.unwrap();
Expand Down
65 changes: 44 additions & 21 deletions core/lib/eth_signer/src/pk_signer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::raw_ethereum_tx::RawTransaction;
use crate::raw_ethereum_tx::{RawTransaction, Transaction};
use crate::{EthereumSigner, SignerError};

use parity_crypto::publickey::sign;
use secp256k1::SecretKey;

use zksync_types::tx::{PackedEthSignature, TxEthSignature};
use zksync_types::{Address, H256};
Expand Down Expand Up @@ -41,9 +41,26 @@ impl EthereumSigner for PrivateKeySigner {

/// Signs and returns the RLP-encoded transaction.
async fn sign_transaction(&self, raw_tx: RawTransaction) -> Result<Vec<u8>, SignerError> {
let sig = sign(&self.private_key.into(), &raw_tx.hash().into())
.map_err(|_| SignerError::NoSigningKey)?;
Ok(raw_tx.rlp_encode_tx(sig))
let key = SecretKey::from_slice(self.private_key.as_bytes()).unwrap();

let gas_price = match raw_tx.max_fee_per_gas {
Some(val) => val,
None => raw_tx.gas_price,
};
let tx = Transaction {
to: raw_tx.to,
nonce: raw_tx.nonce,
gas: raw_tx.gas,
gas_price,
value: raw_tx.value,
data: raw_tx.data,
transaction_type: raw_tx.transaction_type,
access_list: raw_tx.access_list.unwrap_or_default(),
max_priority_fee_per_gas: raw_tx.max_priority_fee_per_gas.unwrap_or_default(),
};

let signed = tx.sign(&key, raw_tx.chain_id);
Ok(signed.raw_transaction.0)
}
}

Expand All @@ -52,34 +69,40 @@ mod test {
use super::PrivateKeySigner;
use super::RawTransaction;
use crate::EthereumSigner;
use web3::types::U64;
use zksync_types::{H160, H256, U256};

#[tokio::test]
async fn test_generating_signature() {
async fn test_generating_signed_raw_transaction() {
let private_key = H256::from([5; 32]);
let signer = PrivateKeySigner::new(private_key);
let raw_transaction = RawTransaction {
chain_id: 1,
nonce: U256::from(1),
to: Some(H160::zero()),
value: U256::from(10),
gas_price: U256::from(1),
gas: U256::from(2),
nonce: U256::from(1u32),
to: Some(H160::default()),
gas: Default::default(),
gas_price: U256::from(2u32),
max_fee_per_gas: Some(U256::from(2u32)),
max_priority_fee_per_gas: Some(U256::from(1u32)),
value: Default::default(),
data: vec![1, 2, 3],
chain_id: 270,
transaction_type: Some(U64::from(1u32)),
access_list: None,
};
let signature = signer
let raw_tx = signer
.sign_transaction(raw_transaction.clone())
.await
.unwrap();
assert_ne!(signature.len(), 1);
assert_ne!(raw_tx.len(), 1);
// precalculated signature with right algorithm implementation
let precalculated_signature: Vec<u8> = vec![
248, 96, 1, 1, 2, 148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10,
131, 1, 2, 3, 37, 160, 152, 202, 15, 174, 50, 167, 190, 239, 206, 183, 109, 215, 135,
60, 43, 71, 11, 74, 252, 97, 83, 86, 66, 249, 237, 111, 118, 121, 105, 214, 130, 249,
160, 106, 110, 143, 138, 113, 12, 177, 239, 121, 188, 247, 21, 236, 236, 163, 254, 28,
48, 250, 5, 20, 234, 54, 58, 162, 103, 252, 20, 243, 121, 7, 19,
let precalculated_raw_tx: Vec<u8> = vec![
1, 248, 100, 130, 1, 14, 1, 2, 128, 148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 128, 131, 1, 2, 3, 192, 1, 160, 98, 201, 238, 158, 215, 98, 23, 231,
221, 161, 170, 16, 54, 85, 187, 107, 12, 228, 218, 139, 103, 164, 17, 196, 178, 185,
252, 243, 186, 175, 93, 230, 160, 93, 204, 205, 5, 46, 187, 231, 211, 102, 133, 200,
254, 119, 94, 206, 81, 8, 143, 204, 14, 138, 43, 183, 214, 209, 166, 16, 116, 176, 44,
52, 133,
];
assert_eq!(signature, precalculated_signature);
assert_eq!(raw_tx, precalculated_raw_tx);
}
}
Loading

0 comments on commit 0d9b71e

Please sign in to comment.