Skip to content

Commit

Permalink
rename Tx code to code_or_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 committed Apr 17, 2023
1 parent 4e38089 commit e2489f7
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 48 deletions.
2 changes: 1 addition & 1 deletion apps/src/lib/node/ledger/shell/process_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ mod test_process_proposal {
.try_to_vec()
.expect("Test failed");
Tx {
code: vec![],
code_or_hash: vec![],
data: Some(
SignedTxData {
sig,
Expand Down
2 changes: 1 addition & 1 deletion core/src/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod tests {
#[test]
fn encoding_round_trip() {
let tx = Tx {
code: "wasm code".as_bytes().to_owned(),
code_or_hash: "wasm code".as_bytes().to_owned(),
data: Some("arbitrary data".as_bytes().to_owned()),
timestamp: Some(SystemTime::now().into()),
chain_id: ChainId::default().0,
Expand Down
24 changes: 13 additions & 11 deletions core/src/proto/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl SigningTx {
let expiration = self.expiration.map(|e| e.into());
let mut bytes = vec![];
types::Tx {
code: self.code_hash.to_vec(),
code_or_hash: self.code_hash.to_vec(),
data: self.data.clone(),
timestamp,
chain_id: self.chain_id.as_str().to_owned(),
Expand Down Expand Up @@ -205,7 +205,7 @@ impl SigningTx {
pub fn expand(self, code: Vec<u8>) -> Option<Tx> {
if hash_tx(&code).0 == self.code_hash {
Some(Tx {
code,
code_or_hash: code,
data: self.data,
timestamp: self.timestamp,
chain_id: self.chain_id,
Expand All @@ -220,7 +220,7 @@ impl SigningTx {
impl From<Tx> for SigningTx {
fn from(tx: Tx) -> SigningTx {
SigningTx {
code_hash: hash_tx(&tx.code).0,
code_hash: hash_tx(&tx.code_or_hash).0,
data: tx.data,
timestamp: tx.timestamp,
chain_id: tx.chain_id,
Expand All @@ -236,7 +236,7 @@ impl From<Tx> for SigningTx {
Clone, Debug, PartialEq, BorshSerialize, BorshDeserialize, BorshSchema, Hash,
)]
pub struct Tx {
pub code: Vec<u8>,
pub code_or_hash: Vec<u8>,
pub data: Option<Vec<u8>>,
pub timestamp: DateTimeUtc,
pub chain_id: ChainId,
Expand All @@ -259,7 +259,7 @@ impl TryFrom<&[u8]> for Tx {
};

Ok(Tx {
code: tx.code,
code_or_hash: tx.code_or_hash,
data: tx.data,
timestamp,
chain_id,
Expand All @@ -274,7 +274,7 @@ impl From<Tx> for types::Tx {
let expiration = tx.expiration.map(|e| e.into());

types::Tx {
code: tx.code,
code_or_hash: tx.code_or_hash,
data: tx.data,
timestamp,
chain_id: tx.chain_id.as_str().to_owned(),
Expand Down Expand Up @@ -370,14 +370,16 @@ impl From<Tx> for ResponseDeliverTx {
}

impl Tx {
/// Create a new transaction. `code_or_hash` should be set as the wasm code
/// bytes or hash.
pub fn new(
code: Vec<u8>,
code_or_hash: Vec<u8>,
data: Option<Vec<u8>>,
chain_id: ChainId,
expiration: Option<DateTimeUtc>,
) -> Self {
Tx {
code,
code_or_hash,
data,
timestamp: DateTimeUtc::now(),
chain_id,
Expand All @@ -404,7 +406,7 @@ impl Tx {
Ok(signed_data) => {
// Reconstruct unsigned tx
let unsigned_tx = Tx {
code: self.code.clone(),
code_or_hash: self.code_or_hash.clone(),
data: signed_data.data,
timestamp: self.timestamp,
chain_id: self.chain_id.clone(),
Expand All @@ -431,7 +433,7 @@ impl Tx {

/// Sign a transaction using [`SignedTxData`].
pub fn sign(self, keypair: &common::SecretKey) -> Self {
let code = self.code.clone();
let code = self.code_or_hash.clone();
SigningTx::from(self)
.sign(keypair)
.expand(code)
Expand Down Expand Up @@ -541,7 +543,7 @@ mod tests {
assert_eq!(tx_from_bytes, tx);

let types_tx = types::Tx {
code,
code_or_hash: code,
data: Some(data),
timestamp: None,
chain_id: chain_id.0,
Expand Down
4 changes: 2 additions & 2 deletions core/src/types/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,15 @@ pub mod tx_types {
.map(|data| SignedTxData::try_from_slice(&data[..]))
{
let signed_hash = Tx {
code: tx.code,
code_or_hash: tx.code_or_hash,
data: Some(data.clone()),
timestamp: tx.timestamp,
chain_id: tx.chain_id.clone(),
expiration: tx.expiration,
}
.hash();
match TxType::try_from(Tx {
code: vec![],
code_or_hash: vec![],
data: Some(data),
timestamp: tx.timestamp,
chain_id: tx.chain_id,
Expand Down
2 changes: 1 addition & 1 deletion proto/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "google/protobuf/timestamp.proto";
package types;

message Tx {
bytes code = 1;
bytes code_or_hash = 1;
// TODO this optional is useless because it's default on proto3
optional bytes data = 2;
google.protobuf.Timestamp timestamp = 3;
Expand Down
2 changes: 1 addition & 1 deletion shared/src/ledger/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ where
write_log,
gas_meter,
tx_index,
&tx.code,
&tx.code_or_hash,
tx_data,
vp_wasm_cache,
tx_wasm_cache,
Expand Down
4 changes: 2 additions & 2 deletions shared/src/ledger/vp_host_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ pub fn get_tx_code_hash(
gas_meter: &mut VpGasMeter,
tx: &Tx,
) -> EnvResult<Hash> {
let hash = if tx.code.len() == HASH_LENGTH {
Hash::try_from(&tx.code[..])
let hash = if tx.code_or_hash.len() == HASH_LENGTH {
Hash::try_from(&tx.code_or_hash[..])
.map_err(|_| RuntimeError::InvalidCodeHash)?
} else {
Hash(tx.code_hash())
Expand Down
Loading

0 comments on commit e2489f7

Please sign in to comment.