Skip to content

Commit a3343da

Browse files
Seulgi Kimsgkim126
authored andcommitted
Add seq to IncreaseAssetSupply and AssetSchemeChange
This seq should match the seq in AssetScheme.
1 parent 715b2ff commit a3343da

File tree

9 files changed

+496
-42
lines changed

9 files changed

+496
-42
lines changed

rpc/src/v1/types/action.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub enum Action {
5959
network_id: NetworkId,
6060
shard_id: ShardId,
6161
asset_type: H160,
62+
seq: u64,
6263
metadata: String,
6364
approver: Option<PlatformAddress>,
6465
registrar: Option<PlatformAddress>,
@@ -71,6 +72,7 @@ pub enum Action {
7172
network_id: NetworkId,
7273
shard_id: ShardId,
7374
asset_type: H160,
75+
seq: u64,
7476
output: Box<AssetMintOutput>,
7577

7678
approvals: Vec<Signature>,
@@ -183,6 +185,7 @@ pub enum ActionWithTracker {
183185
network_id: NetworkId,
184186
shard_id: ShardId,
185187
asset_type: H160,
188+
seq: u64,
186189
metadata: String,
187190
approver: Option<PlatformAddress>,
188191
registrar: Option<PlatformAddress>,
@@ -197,6 +200,7 @@ pub enum ActionWithTracker {
197200
network_id: NetworkId,
198201
shard_id: ShardId,
199202
asset_type: H160,
203+
seq: u64,
200204
output: Box<AssetMintOutput>,
201205

202206
approvals: Vec<Signature>,
@@ -329,6 +333,7 @@ impl ActionWithTracker {
329333
network_id,
330334
shard_id,
331335
asset_type,
336+
seq,
332337
metadata,
333338
approver,
334339
registrar,
@@ -338,6 +343,7 @@ impl ActionWithTracker {
338343
network_id,
339344
shard_id,
340345
asset_type,
346+
seq: seq as u64,
341347
metadata,
342348
approver: approver.map(|approver| PlatformAddress::new_v1(network_id, approver)),
343349
registrar: registrar.map(|registrar| PlatformAddress::new_v1(network_id, registrar)),
@@ -349,12 +355,14 @@ impl ActionWithTracker {
349355
network_id,
350356
shard_id,
351357
asset_type,
358+
seq,
352359
output,
353360
approvals,
354361
} => ActionWithTracker::IncreaseAssetSupply {
355362
network_id,
356363
shard_id,
357364
asset_type,
365+
seq: seq as u64,
358366
output: Box::new((*output).into()),
359367
approvals,
360368
tracker: tracker.unwrap(),
@@ -543,6 +551,7 @@ impl From<Action> for Result<ActionType, ConversionError> {
543551
network_id,
544552
shard_id,
545553
asset_type,
554+
seq,
546555
metadata,
547556
approver,
548557
registrar,
@@ -562,6 +571,7 @@ impl From<Action> for Result<ActionType, ConversionError> {
562571
network_id,
563572
shard_id,
564573
asset_type,
574+
seq: seq as usize,
565575
metadata,
566576
approver,
567577
registrar,
@@ -573,13 +583,15 @@ impl From<Action> for Result<ActionType, ConversionError> {
573583
network_id,
574584
shard_id,
575585
asset_type,
586+
seq,
576587
output,
577588
approvals,
578589
} => {
579590
let output_content = Result::<AssetMintOutputType, FromHexError>::from(*output)?;
580591
ActionType::IncreaseAssetSupply {
581592
network_id,
582593
shard_id,
594+
seq: seq as usize,
583595
asset_type,
584596
output: Box::new(output_content),
585597
approvals,

rpc/src/v1/types/asset_scheme.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub struct AssetScheme {
3030
registrar: Option<PlatformAddress>,
3131
allowed_script_hashes: Vec<H160>,
3232
pool: Vec<Asset>,
33+
seq: u64,
3334
}
3435

3536
impl AssetScheme {
@@ -44,6 +45,7 @@ impl AssetScheme {
4445
.map(|registrar| PlatformAddress::new_v1(network_id, *registrar)),
4546
allowed_script_hashes: asset_scheme.allowed_script_hashes().to_owned(),
4647
pool: asset_scheme.pool().iter().map(|asset| asset.clone().into()).collect(),
48+
seq: asset_scheme.seq() as u64,
4749
}
4850
}
4951
}

state/src/impls/shard_level.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ impl<'db> ShardLevelState<'db> {
157157
ShardTransaction::ChangeAssetScheme {
158158
shard_id,
159159
asset_type,
160+
seq,
160161
metadata,
161162
approver,
162163
registrar,
@@ -168,6 +169,7 @@ impl<'db> ShardLevelState<'db> {
168169
sender,
169170
approvers,
170171
asset_type,
172+
*seq,
171173
metadata,
172174
approver,
173175
registrar,
@@ -178,10 +180,11 @@ impl<'db> ShardLevelState<'db> {
178180
shard_id,
179181
asset_type,
180182
output,
183+
seq,
181184
..
182185
} => {
183186
assert_eq!(*shard_id, self.shard_id);
184-
self.increase_asset_supply(transaction.tracker(), sender, approvers, asset_type, output)
187+
self.increase_asset_supply(transaction.tracker(), *seq, sender, approvers, asset_type, output)
185188
}
186189
ShardTransaction::ComposeAsset {
187190
metadata,
@@ -456,6 +459,7 @@ impl<'db> ShardLevelState<'db> {
456459
sender: &Address,
457460
approvers: &[Address],
458461
asset_type: &H160,
462+
seq: usize,
459463
metadata: &str,
460464
approver: &Option<Address>,
461465
registrar: &Option<Address>,
@@ -466,19 +470,31 @@ impl<'db> ShardLevelState<'db> {
466470
}
467471

468472
let mut asset_scheme = self.get_asset_scheme_mut(self.shard_id, *asset_type)?;
473+
if asset_scheme.seq() != seq {
474+
return Err(RuntimeError::InvalidSeqOfAssetScheme {
475+
asset_type: *asset_type,
476+
shard_id: self.shard_id,
477+
expected: asset_scheme.seq(),
478+
actual: seq,
479+
}
480+
.into())
481+
}
482+
469483
asset_scheme.change_data(
470484
metadata.to_string(),
471485
approver.clone(),
472486
registrar.clone(),
473487
allowed_script_hashes.to_vec(),
474488
);
489+
asset_scheme.increase_seq();
475490

476491
Ok(())
477492
}
478493

479494
fn increase_asset_supply(
480495
&mut self,
481496
transaction_tracker: H256,
497+
seq: usize,
482498
sender: &Address,
483499
approvers: &[Address],
484500
asset_type: &H160,
@@ -492,7 +508,17 @@ impl<'db> ShardLevelState<'db> {
492508
assert!(output.supply > 0, "Supply increasing quantity must be specified and greater than 0");
493509

494510
let mut asset_scheme = self.get_asset_scheme_mut(self.shard_id, *asset_type)?;
511+
if seq != asset_scheme.seq() {
512+
return Err(RuntimeError::InvalidSeqOfAssetScheme {
513+
asset_type: *asset_type,
514+
shard_id: self.shard_id,
515+
expected: asset_scheme.seq(),
516+
actual: seq,
517+
}
518+
.into())
519+
}
495520
let previous_supply = asset_scheme.increase_supply(output.supply)?;
521+
asset_scheme.increase_seq();
496522
self.create_asset(
497523
transaction_tracker,
498524
0,
@@ -502,7 +528,7 @@ impl<'db> ShardLevelState<'db> {
502528
output.supply,
503529
None,
504530
)?;
505-
ctrace!(TX, "Increased asset supply {:?} {:?} {:?}", asset_type, previous_supply, output.supply);
531+
ctrace!(TX, "Increased asset supply {:?} {:?} => {:?}", asset_type, previous_supply, output.supply);
506532
ctrace!(TX, "Created asset on {}:{}", self.shard_id, transaction_tracker);
507533

508534
Ok(())
@@ -2439,6 +2465,7 @@ mod tests {
24392465
network_id: "tc".into(),
24402466
shard_id: SHARD_ID,
24412467
asset_type,
2468+
seq: 0,
24422469
metadata: "New metadata".to_string(),
24432470
approver: Some(approver),
24442471
registrar: None,
@@ -2485,6 +2512,7 @@ mod tests {
24852512
network_id: "tc".into(),
24862513
shard_id: SHARD_ID,
24872514
asset_type,
2515+
seq: 0,
24882516
output: AssetMintOutput {
24892517
lock_script_hash: H160::random(),
24902518
parameters: vec![],

state/src/item/asset_scheme.rs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct AssetScheme {
3232
registrar: Option<Address>,
3333
allowed_script_hashes: Vec<H160>,
3434
pool: Vec<Asset>,
35+
seq: usize,
3536
}
3637

3738
impl AssetScheme {
@@ -49,6 +50,7 @@ impl AssetScheme {
4950
registrar,
5051
allowed_script_hashes,
5152
pool: Vec::new(),
53+
seq: 0,
5254
}
5355
}
5456

@@ -67,6 +69,7 @@ impl AssetScheme {
6769
registrar,
6870
allowed_script_hashes,
6971
pool,
72+
seq: 0,
7073
}
7174
}
7275

@@ -90,6 +93,10 @@ impl AssetScheme {
9093
&self.allowed_script_hashes
9194
}
9295

96+
pub fn seq(&self) -> usize {
97+
self.seq
98+
}
99+
93100
pub fn is_permissioned(&self) -> bool {
94101
self.approver.is_some()
95102
}
@@ -130,6 +137,10 @@ impl AssetScheme {
130137
Ok(previous)
131138
}
132139

140+
pub fn increase_seq(&mut self) {
141+
self.seq += 1;
142+
}
143+
133144
pub fn reduce_supply(&mut self, quantity: u64) -> u64 {
134145
assert!(self.supply >= quantity, "AssetScheme supply shouldn't be depleted");
135146
let previous = self.supply;
@@ -148,26 +159,36 @@ impl Default for AssetScheme {
148159

149160
impl Encodable for AssetScheme {
150161
fn rlp_append(&self, s: &mut RlpStream) {
151-
s.begin_list(7)
152-
.append(&PREFIX)
162+
if self.seq == 0 {
163+
s.begin_list(7);
164+
} else {
165+
s.begin_list(8);
166+
}
167+
s.append(&PREFIX)
153168
.append(&self.metadata)
154169
.append(&self.supply)
155170
.append(&self.approver)
156171
.append(&self.registrar)
157172
.append_list(&self.allowed_script_hashes)
158173
.append_list(&self.pool);
174+
if self.seq != 0 {
175+
s.append(&self.seq);
176+
}
159177
}
160178
}
161179

162180
impl Decodable for AssetScheme {
163181
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
164-
let item_count = rlp.item_count()?;
165-
if item_count != 7 {
166-
return Err(DecoderError::RlpInvalidLength {
167-
got: item_count,
168-
expected: 7,
169-
})
170-
}
182+
let seq = match rlp.item_count()? {
183+
7 => 0,
184+
8 => rlp.val_at(7)?,
185+
item_count => {
186+
return Err(DecoderError::RlpInvalidLength {
187+
got: item_count,
188+
expected: 7,
189+
})
190+
}
191+
};
171192

172193
let prefix = rlp.val_at::<u8>(0)?;
173194
if PREFIX != prefix {
@@ -181,6 +202,7 @@ impl Decodable for AssetScheme {
181202
registrar: rlp.val_at(4)?,
182203
allowed_script_hashes: rlp.list_at(5)?,
183204
pool: rlp.list_at(6)?,
205+
seq,
184206
})
185207
}
186208
}

0 commit comments

Comments
 (0)