Skip to content

Commit 91e191f

Browse files
committed
update icrc3
1 parent 3c6c083 commit 91e191f

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

src/icrc3/src/icrc3.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use candid::Nat;
77
use ic_certification::AsHashTree;
88
use icrc_ledger_types::icrc::generic_value::ICRC3Value;
99
use serde::{Deserialize, Serialize};
10+
use serde_bytes::ByteBuf;
1011
use std::collections::VecDeque;
1112
use std::time::Duration;
1213

@@ -29,6 +30,7 @@ pub struct ICRC3 {
2930
pub blockchain: Blockchain,
3031
pub ledger: VecDeque<ICRC3Value>,
3132
pub last_index: u64,
33+
pub last_phash: Option<ByteBuf>,
3234
pub icrc3_config: ICRC3Config,
3335
}
3436

@@ -50,6 +52,7 @@ impl ICRC3 {
5052
blockchain: Blockchain::default(),
5153
ledger: VecDeque::new(),
5254
last_index: 0,
55+
last_phash: None,
5356
icrc3_config,
5457
}
5558
}
@@ -177,6 +180,22 @@ impl ICRC3 {
177180
.root_hash();
178181
hash_tree_root.to_vec()
179182
}
183+
184+
pub fn add_phash(&mut self, icrc3_transaction: &mut ICRC3Value) {
185+
if let ICRC3Value::Map(map) = icrc3_transaction {
186+
if let Some(last_phash) = self.last_phash.clone() {
187+
map.insert(
188+
"phash".to_string(),
189+
ICRC3Value::Blob(ByteBuf::from(last_phash.to_vec())),
190+
);
191+
} else {
192+
map.insert(
193+
"phash".to_string(),
194+
ICRC3Value::Blob(ByteBuf::from(vec![0; 32])),
195+
);
196+
}
197+
}
198+
}
180199
}
181200

182201
use ic_certification::fork;

src/icrc3/src/interface.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::icrc3::ICRC3;
2-
use crate::transaction::{BasicTransaction, TransactionKind, TransactionType};
2+
use crate::transaction::{self, BasicTransaction, TransactionKind, TransactionType};
33
use crate::types::Icrc3Error;
44
use crate::utils::trace;
55

@@ -115,6 +115,10 @@ impl<T: TransactionType> ICRC3Interface<T> for ICRC3 {
115115
}
116116
}
117117

118+
let mut transaction_as_icrc3: ICRC3Value = transaction.clone().into();
119+
120+
self.add_phash(&mut transaction_as_icrc3);
121+
118122
let basic_transaction = BasicTransaction::new(transaction.clone().into());
119123

120124
let mut checked_transaction = match basic_transaction.validate_transaction_fields() {
@@ -163,6 +167,7 @@ impl<T: TransactionType> ICRC3Interface<T> for ICRC3 {
163167

164168
self.ledger.push_back(checked_transaction.clone());
165169
self.last_index += 1;
170+
self.last_phash = Some(ByteBuf::from(checked_transaction.clone().hash().to_vec()));
166171

167172
let block =
168173
DefaultBlock::from_transaction(self.blockchain.last_hash, checked_transaction, now);

src/icrc3/src/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl BasicTransaction {
104104
ICRC3Value::Map(map) => {
105105
trace(&format!("validate_transaction_fields {:?}", map));
106106

107-
let phash = match map.get("phash") {
107+
match map.get("phash") {
108108
Some(phash) => match phash {
109109
ICRC3Value::Blob(_) => {
110110
trace(&format!("phash is a blob"));
15.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)