Skip to content

Add seq to IncreaseAssetSupply and AssetSchemeChange #1442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions rpc/src/v1/types/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub enum Action {
network_id: NetworkId,
shard_id: ShardId,
asset_type: H160,
seq: u64,
metadata: String,
approver: Option<PlatformAddress>,
registrar: Option<PlatformAddress>,
Expand All @@ -71,6 +72,7 @@ pub enum Action {
network_id: NetworkId,
shard_id: ShardId,
asset_type: H160,
seq: u64,
output: Box<AssetMintOutput>,

approvals: Vec<Signature>,
Expand Down Expand Up @@ -183,6 +185,7 @@ pub enum ActionWithTracker {
network_id: NetworkId,
shard_id: ShardId,
asset_type: H160,
seq: u64,
metadata: String,
approver: Option<PlatformAddress>,
registrar: Option<PlatformAddress>,
Expand All @@ -197,6 +200,7 @@ pub enum ActionWithTracker {
network_id: NetworkId,
shard_id: ShardId,
asset_type: H160,
seq: u64,
output: Box<AssetMintOutput>,

approvals: Vec<Signature>,
Expand Down Expand Up @@ -329,6 +333,7 @@ impl ActionWithTracker {
network_id,
shard_id,
asset_type,
seq,
metadata,
approver,
registrar,
Expand All @@ -338,6 +343,7 @@ impl ActionWithTracker {
network_id,
shard_id,
asset_type,
seq: seq as u64,
metadata,
approver: approver.map(|approver| PlatformAddress::new_v1(network_id, approver)),
registrar: registrar.map(|registrar| PlatformAddress::new_v1(network_id, registrar)),
Expand All @@ -349,12 +355,14 @@ impl ActionWithTracker {
network_id,
shard_id,
asset_type,
seq,
output,
approvals,
} => ActionWithTracker::IncreaseAssetSupply {
network_id,
shard_id,
asset_type,
seq: seq as u64,
output: Box::new((*output).into()),
approvals,
tracker: tracker.unwrap(),
Expand Down Expand Up @@ -543,6 +551,7 @@ impl From<Action> for Result<ActionType, ConversionError> {
network_id,
shard_id,
asset_type,
seq,
metadata,
approver,
registrar,
Expand All @@ -562,6 +571,7 @@ impl From<Action> for Result<ActionType, ConversionError> {
network_id,
shard_id,
asset_type,
seq: seq as usize,
metadata,
approver,
registrar,
Expand All @@ -573,13 +583,15 @@ impl From<Action> for Result<ActionType, ConversionError> {
network_id,
shard_id,
asset_type,
seq,
output,
approvals,
} => {
let output_content = Result::<AssetMintOutputType, FromHexError>::from(*output)?;
ActionType::IncreaseAssetSupply {
network_id,
shard_id,
seq: seq as usize,
asset_type,
output: Box::new(output_content),
approvals,
Expand Down
2 changes: 2 additions & 0 deletions rpc/src/v1/types/asset_scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct AssetScheme {
registrar: Option<PlatformAddress>,
allowed_script_hashes: Vec<H160>,
pool: Vec<Asset>,
seq: u64,
}

impl AssetScheme {
Expand All @@ -44,6 +45,7 @@ impl AssetScheme {
.map(|registrar| PlatformAddress::new_v1(network_id, *registrar)),
allowed_script_hashes: asset_scheme.allowed_script_hashes().to_owned(),
pool: asset_scheme.pool().iter().map(|asset| asset.clone().into()).collect(),
seq: asset_scheme.seq() as u64,
}
}
}
32 changes: 30 additions & 2 deletions state/src/impls/shard_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ impl<'db> ShardLevelState<'db> {
ShardTransaction::ChangeAssetScheme {
shard_id,
asset_type,
seq,
metadata,
approver,
registrar,
Expand All @@ -168,6 +169,7 @@ impl<'db> ShardLevelState<'db> {
sender,
approvers,
asset_type,
*seq,
metadata,
approver,
registrar,
Expand All @@ -178,10 +180,11 @@ impl<'db> ShardLevelState<'db> {
shard_id,
asset_type,
output,
seq,
..
} => {
assert_eq!(*shard_id, self.shard_id);
self.increase_asset_supply(transaction.tracker(), sender, approvers, asset_type, output)
self.increase_asset_supply(transaction.tracker(), *seq, sender, approvers, asset_type, output)
}
ShardTransaction::ComposeAsset {
metadata,
Expand Down Expand Up @@ -456,6 +459,7 @@ impl<'db> ShardLevelState<'db> {
sender: &Address,
approvers: &[Address],
asset_type: &H160,
seq: usize,
metadata: &str,
approver: &Option<Address>,
registrar: &Option<Address>,
Expand All @@ -466,19 +470,31 @@ impl<'db> ShardLevelState<'db> {
}

let mut asset_scheme = self.get_asset_scheme_mut(self.shard_id, *asset_type)?;
if asset_scheme.seq() != seq {
return Err(RuntimeError::InvalidSeqOfAssetScheme {
asset_type: *asset_type,
shard_id: self.shard_id,
expected: asset_scheme.seq(),
actual: seq,
}
.into())
}

asset_scheme.change_data(
metadata.to_string(),
approver.clone(),
registrar.clone(),
allowed_script_hashes.to_vec(),
);
asset_scheme.increase_seq();

Ok(())
}

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

let mut asset_scheme = self.get_asset_scheme_mut(self.shard_id, *asset_type)?;
if seq != asset_scheme.seq() {
return Err(RuntimeError::InvalidSeqOfAssetScheme {
asset_type: *asset_type,
shard_id: self.shard_id,
expected: asset_scheme.seq(),
actual: seq,
}
.into())
}
let previous_supply = asset_scheme.increase_supply(output.supply)?;
asset_scheme.increase_seq();
self.create_asset(
transaction_tracker,
0,
Expand All @@ -502,7 +528,7 @@ impl<'db> ShardLevelState<'db> {
output.supply,
None,
)?;
ctrace!(TX, "Increased asset supply {:?} {:?} {:?}", asset_type, previous_supply, output.supply);
ctrace!(TX, "Increased asset supply {:?} {:?} => {:?}", asset_type, previous_supply, output.supply);
ctrace!(TX, "Created asset on {}:{}", self.shard_id, transaction_tracker);

Ok(())
Expand Down Expand Up @@ -2439,6 +2465,7 @@ mod tests {
network_id: "tc".into(),
shard_id: SHARD_ID,
asset_type,
seq: 0,
metadata: "New metadata".to_string(),
approver: Some(approver),
registrar: None,
Expand Down Expand Up @@ -2485,6 +2512,7 @@ mod tests {
network_id: "tc".into(),
shard_id: SHARD_ID,
asset_type,
seq: 0,
output: AssetMintOutput {
lock_script_hash: H160::random(),
parameters: vec![],
Expand Down
40 changes: 31 additions & 9 deletions state/src/item/asset_scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct AssetScheme {
registrar: Option<Address>,
allowed_script_hashes: Vec<H160>,
pool: Vec<Asset>,
seq: usize,
}

impl AssetScheme {
Expand All @@ -49,6 +50,7 @@ impl AssetScheme {
registrar,
allowed_script_hashes,
pool: Vec::new(),
seq: 0,
}
}

Expand All @@ -67,6 +69,7 @@ impl AssetScheme {
registrar,
allowed_script_hashes,
pool,
seq: 0,
}
}

Expand All @@ -90,6 +93,10 @@ impl AssetScheme {
&self.allowed_script_hashes
}

pub fn seq(&self) -> usize {
self.seq
}

pub fn is_permissioned(&self) -> bool {
self.approver.is_some()
}
Expand Down Expand Up @@ -130,6 +137,10 @@ impl AssetScheme {
Ok(previous)
}

pub fn increase_seq(&mut self) {
self.seq += 1;
}

pub fn reduce_supply(&mut self, quantity: u64) -> u64 {
assert!(self.supply >= quantity, "AssetScheme supply shouldn't be depleted");
let previous = self.supply;
Expand All @@ -148,26 +159,36 @@ impl Default for AssetScheme {

impl Encodable for AssetScheme {
fn rlp_append(&self, s: &mut RlpStream) {
s.begin_list(7)
.append(&PREFIX)
if self.seq == 0 {
s.begin_list(7);
} else {
s.begin_list(8);
}
s.append(&PREFIX)
.append(&self.metadata)
.append(&self.supply)
.append(&self.approver)
.append(&self.registrar)
.append_list(&self.allowed_script_hashes)
.append_list(&self.pool);
if self.seq != 0 {
s.append(&self.seq);
}
}
}

impl Decodable for AssetScheme {
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
let item_count = rlp.item_count()?;
if item_count != 7 {
return Err(DecoderError::RlpInvalidLength {
got: item_count,
expected: 7,
})
}
let seq = match rlp.item_count()? {
7 => 0,
8 => rlp.val_at(7)?,
item_count => {
return Err(DecoderError::RlpInvalidLength {
got: item_count,
expected: 7,
})
}
};

let prefix = rlp.val_at::<u8>(0)?;
if PREFIX != prefix {
Expand All @@ -181,6 +202,7 @@ impl Decodable for AssetScheme {
registrar: rlp.val_at(4)?,
allowed_script_hashes: rlp.list_at(5)?,
pool: rlp.list_at(6)?,
seq,
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"blakejs": "^1.1.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"codechain-primitives": "^0.4.8",
"codechain-sdk": "https://github.com/sgkim126/codechain-sdk-js.git#result-lib",
"codechain-primitives": "^0.5.0",
"codechain-sdk": "https://github.com/sgkim126/codechain-sdk-js.git#seq-lib",
"crypto": "^1.0.1",
"dgram": "^1.0.1",
"elliptic": "^6.4.1",
Expand Down
Loading