Skip to content

Commit

Permalink
feat: Add input field to CallRequest (#1069)
Browse files Browse the repository at this point in the history
## What ❔

Add input field to CallRequest struct.

## Why ❔

To be compatible with JSON-RPC API

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
- [ ] Spellcheck has been run via `zk spellcheck`.
- [ ] Linkcheck has been run via `zk linkcheck`.
  • Loading branch information
Artemka374 committed Feb 14, 2024
1 parent 805218c commit 5087121
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
33 changes: 32 additions & 1 deletion core/lib/types/src/transaction_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub struct CallRequest {
/// Data (None for empty data)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub data: Option<Bytes>,
/// Input (None for empty)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub input: Option<Bytes>,
/// Nonce
#[serde(default, skip_serializing_if = "Option::is_none")]
pub nonce: Option<U256>,
Expand Down Expand Up @@ -123,6 +126,11 @@ impl CallRequestBuilder {
self
}

pub fn input(mut self, input: Bytes) -> Self {
self.call_request.input = Some(input);
self
}

/// Set transaction type, Some(1) for AccessList transaction, None for Legacy
pub fn transaction_type(mut self, transaction_type: U64) -> Self {
self.call_request.transaction_type = Some(transaction_type);
Expand Down Expand Up @@ -869,7 +877,7 @@ impl From<CallRequest> for TransactionRequest {
value: call_request.value.unwrap_or_default(),
gas_price: call_request.gas_price.unwrap_or_default(),
gas: call_request.gas.unwrap_or_default(),
input: call_request.data.unwrap_or_default(),
input: call_request.input.or(call_request.data).unwrap_or_default(),
transaction_type: call_request.transaction_type,
access_list: call_request.access_list,
eip712_meta: call_request.eip712_meta,
Expand Down Expand Up @@ -1457,6 +1465,7 @@ mod tests {
max_priority_fee_per_gas: Some(U256::from(12u32)),
value: Some(U256::from(12u32)),
data: Some(Bytes(factory_dep)),
input: None,
nonce: None,
transaction_type: Some(U64::from(EIP_712_TX_TYPE)),
access_list: None,
Expand All @@ -1483,6 +1492,7 @@ mod tests {
max_priority_fee_per_gas: Some(U256::from(12u32)),
value: Some(U256::from(12u32)),
data: Some(Bytes(vec![1, 2, 3])),
input: None,
nonce: Some(U256::from(123u32)),
transaction_type: Some(U64::from(EIP_712_TX_TYPE)),
access_list: None,
Expand All @@ -1499,4 +1509,25 @@ mod tests {
L2Tx::from_request(call_request_without_nonce.into(), MAX_ENCODED_TX_SIZE).unwrap();
assert_eq!(l2_tx.nonce(), Nonce(0u32));
}

#[test]
fn test_correct_data_field() {
let mut call_request = CallRequest {
input: Some(Bytes(vec![1, 2, 3])),
data: Some(Bytes(vec![3, 2, 1])),
..Default::default()
};

let tx_request = TransactionRequest::from(call_request.clone());
assert_eq!(tx_request.input, call_request.input.unwrap());

call_request.input = None;
let tx_request = TransactionRequest::from(call_request.clone());
assert_eq!(tx_request.input, call_request.data.unwrap());

call_request.input = Some(Bytes(vec![1, 2, 3]));
call_request.data = None;
let tx_request = TransactionRequest::from(call_request.clone());
assert_eq!(tx_request.input, call_request.input.unwrap());
}
}
9 changes: 8 additions & 1 deletion core/lib/zksync_core/src/api_server/web3/tests/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,14 @@ impl TraceCallTest {
assert_eq!(call_result.from, Address::zero());
assert_eq!(call_result.gas, call_request.gas.unwrap());
assert_eq!(call_result.value, call_request.value.unwrap());
assert_eq!(call_result.input, *call_request.data.as_ref().unwrap());
assert_eq!(
call_result.input,
call_request
.clone()
.input
.or(call_request.clone().data)
.unwrap()
);
assert_eq!(call_result.output.0, b"output");
}
}
Expand Down

0 comments on commit 5087121

Please sign in to comment.