Skip to content

Commit 009018d

Browse files
author
Seulgi Kim
committed
Make multiple users can share a shard
1 parent 7d39768 commit 009018d

File tree

14 files changed

+88
-70
lines changed

14 files changed

+88
-70
lines changed

core/res/blake_pow.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"shards": {
4343
"0": {
4444
"nonce": 0,
45-
"owner": "0x9cce34f7ab185c7aba1b7c8140d620b4bda941d6",
45+
"owners": ["0x9cce34f7ab185c7aba1b7c8140d620b4bda941d6"],
4646
"worlds": [{
4747
"nonce": 0,
4848
"owners": [

core/res/cuckoo.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"shards": {
4646
"0": {
4747
"nonce": 0,
48-
"owner": "0x9cce34f7ab185c7aba1b7c8140d620b4bda941d6",
48+
"owners": ["0x9cce34f7ab185c7aba1b7c8140d620b4bda941d6"],
4949
"worlds": [{
5050
"nonce": 0,
5151
"owners": [

core/res/null.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"shards": {
4040
"0": {
4141
"nonce": 0,
42-
"owner": "0x9cce34f7ab185c7aba1b7c8140d620b4bda941d6",
42+
"owners": ["0x9cce34f7ab185c7aba1b7c8140d620b4bda941d6"],
4343
"worlds": [{
4444
"nonce": 0,
4545
"owners": [

core/res/solo.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"shards": {
4141
"0": {
4242
"nonce": 0,
43-
"owner": "0x9cce34f7ab185c7aba1b7c8140d620b4bda941d6",
43+
"owners": ["0x9cce34f7ab185c7aba1b7c8140d620b4bda941d6"],
4444
"worlds": [{
4545
"nonce": 0,
4646
"owners": [

core/res/solo_authority.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"shards": {
4545
"0": {
4646
"nonce": 0,
47-
"owner": "0x9cce34f7ab185c7aba1b7c8140d620b4bda941d6",
47+
"owners": ["0x9cce34f7ab185c7aba1b7c8140d620b4bda941d6"],
4848
"worlds": [{
4949
"nonce": 0,
5050
"owners": [

core/res/tendermint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"shards": {
5757
"0": {
5858
"nonce": 0,
59-
"owner": "0x9cce34f7ab185c7aba1b7c8140d620b4bda941d6",
59+
"owners": ["0x9cce34f7ab185c7aba1b7c8140d620b4bda941d6"],
6060
"worlds": [{
6161
"nonce": 0,
6262
"owners": [

core/src/spec/pod_shard_metadata.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use super::pod_world::PodWorld;
2525

2626
#[derive(Clone, Debug, Eq, PartialEq)]
2727
pub struct PodShardMetadata {
28-
pub owner: Address,
28+
pub owners: Vec<Address>,
2929
pub nonce: u64,
3030
pub worlds: Vec<PodWorld>,
3131
}
@@ -48,14 +48,14 @@ impl From<cjson::spec::Shard> for PodShardMetadata {
4848
fn from(s: cjson::spec::Shard) -> Self {
4949
Self {
5050
nonce: s.nonce.map(Into::into).unwrap_or(0),
51-
owner: s.owner.into(),
51+
owners: s.owners.into_iter().map(Into::into).collect(),
5252
worlds: s.worlds.unwrap_or_else(Vec::new).into_iter().map(Into::into).collect(),
5353
}
5454
}
5555
}
5656

5757
impl fmt::Display for PodShardMetadata {
5858
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59-
write!(f, "(#nonce={}; owner={}; worlds={:#?})", self.nonce, self.owner, self.worlds)
59+
write!(f, "(#nonce={}; owners={:#?}; worlds={:#?})", self.nonce, self.owners, self.worlds)
6060
}
6161
}

core/src/spec/spec.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use cstate::{
2525
ActionHandler, Backend, Metadata, MetadataAddress, Shard, ShardAddress, ShardMetadataAddress, StateDB, StateResult,
2626
WorldAddress,
2727
};
28+
use ctypes::transaction::Error as TransactionError;
2829
use ctypes::ShardId;
2930
use hashdb::HashDB;
3031
use parking_lot::RwLock;
@@ -177,7 +178,7 @@ impl Spec {
177178
}
178179

179180
fn initialize_shards<DB: Backend>(&self, mut db: DB, mut root: H256) -> StateResult<(DB, H256)> {
180-
let mut shard_roots = Vec::<(ShardId, H256, Address)>::with_capacity(self.genesis_shards.len());
181+
let mut shard_roots = Vec::<(ShardId, H256, Vec<Address>)>::with_capacity(self.genesis_shards.len());
181182

182183
// Initialize shard-level tries
183184
for (shard_id, shard) in &*self.genesis_shards {
@@ -200,8 +201,11 @@ impl Spec {
200201
r?;
201202
}
202203
}
203-
let owner = shard.owner;
204-
shard_roots.push((*shard_id, shard_root, owner));
204+
let owners = shard.owners.clone();
205+
if owners.is_empty() {
206+
return Err(TransactionError::EmptyShardOwners(*shard_id).into())
207+
}
208+
shard_roots.push((*shard_id, shard_root, owners));
205209
}
206210

207211
debug_assert_eq!(::std::mem::size_of::<u16>(), ::std::mem::size_of::<ShardId>());
@@ -214,12 +218,12 @@ impl Spec {
214218
let global_metadata = Metadata::new(shard_roots.len() as ShardId);
215219

216220
// Initialize shards
217-
for (shard_id, shard_root, owner) in shard_roots.into_iter() {
221+
for (shard_id, shard_root, owners) in shard_roots.into_iter() {
218222
{
219223
let mut t = TrieFactory::from_existing(db.as_hashdb_mut(), &mut root)?;
220224
let address = ShardAddress::new(shard_id);
221225

222-
let shard = Shard::new(shard_root, owner);
226+
let shard = Shard::new(shard_root, owners);
223227
let r = t.insert(&*address, &shard.rlp_bytes());
224228
debug_assert_eq!(Ok(None), r);
225229
r?;

json/src/spec/shard.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::World;
2121
#[derive(Debug, PartialEq, Deserialize)]
2222
pub struct Shard {
2323
pub nonce: Option<Uint>,
24-
pub owner: Address,
24+
pub owners: Vec<Address>,
2525
pub worlds: Option<Vec<World>>,
2626
}
2727

@@ -37,7 +37,7 @@ mod tests {
3737
fn shard_deserialization() {
3838
let s = r#"{
3939
"nonce": 0,
40-
"owner": "0x01234567890abcdef0123456789abcdef0123456",
40+
"owners": ["0x01234567890abcdef0123456789abcdef0123456"],
4141
"worlds": [{
4242
"nonce": 3,
4343
"owners": ["0x01234567890abcdef0123456789abcdef0123457"]
@@ -47,7 +47,7 @@ mod tests {
4747
assert_eq!(
4848
Shard {
4949
nonce: Some(Uint(U256::from(0))),
50-
owner: Address(CoreAddress::from("01234567890abcdef0123456789abcdef0123456")),
50+
owners: vec![Address(CoreAddress::from("01234567890abcdef0123456789abcdef0123456"))],
5151
worlds: Some(vec![World {
5252
nonce: Some(Uint(U256::from(3))),
5353
owners: Some(vec![Address(CoreAddress::from("01234567890abcdef0123456789abcdef0123457"))]),
@@ -61,13 +61,13 @@ mod tests {
6161
fn shard_with_non_zero_nonce_deserialization() {
6262
let s = r#"{
6363
"nonce": 100,
64-
"owner": "0x01234567890abcdef0123456789abcdef0123456"
64+
"owners": ["0x01234567890abcdef0123456789abcdef0123456"]
6565
}"#;
6666
let shard: Shard = serde_json::from_str(s).unwrap();
6767
assert_eq!(
6868
Shard {
6969
nonce: Some(Uint(U256::from(100))),
70-
owner: Address(CoreAddress::from("01234567890abcdef0123456789abcdef0123456")),
70+
owners: vec![Address(CoreAddress::from("01234567890abcdef0123456789abcdef0123456"))],
7171
worlds: None,
7272
},
7373
shard
@@ -86,13 +86,13 @@ mod tests {
8686
#[test]
8787
fn shard_without_nonce_deserialization() {
8888
let s = r#"{
89-
"owner": "0x01234567890abcdef0123456789abcdef0123456"
89+
"owners": ["0x01234567890abcdef0123456789abcdef0123456"]
9090
}"#;
9191
let shard: Shard = serde_json::from_str(s).unwrap();
9292
assert_eq!(
9393
Shard {
9494
nonce: None,
95-
owner: Address(CoreAddress::from("01234567890abcdef0123456789abcdef0123456")),
95+
owners: vec![Address(CoreAddress::from("01234567890abcdef0123456789abcdef0123456"))],
9696
worlds: None,
9797
},
9898
shard

state/src/impls/shard_level.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<B: Backend + ShardBackend> ShardLevelState<B> {
108108
shard_id: ShardId,
109109
transaction: &Transaction,
110110
sender: &Address,
111-
shard_owner: &Address,
111+
shard_owners: &[Address],
112112
) -> StateResult<()> {
113113
debug_assert_eq!(Ok(()), transaction.verify());
114114
match transaction {
@@ -123,7 +123,7 @@ impl<B: Backend + ShardBackend> ShardLevelState<B> {
123123
nonce,
124124
owners,
125125
..
126-
} => Ok(self.set_world_owners(*shard_id, *world_id, *nonce, &owners, sender, shard_owner)?),
126+
} => Ok(self.set_world_owners(*shard_id, *world_id, *nonce, &owners, sender, shard_owners)?),
127127
Transaction::AssetMint {
128128
metadata,
129129
registrar,
@@ -173,11 +173,11 @@ impl<B: Backend + ShardBackend> ShardLevelState<B> {
173173
nonce: u64,
174174
owners: &[Address],
175175
sender: &Address,
176-
shard_owner: &Address,
176+
shard_owners: &[Address],
177177
) -> StateResult<()> {
178178
let world: World = self.world(world_id)?.ok_or_else(|| TransactionError::InvalidWorldId(world_id))?;
179179

180-
if shard_owner != sender && !world.world_owners().contains(sender) {
180+
if !shard_owners.contains(sender) && !world.world_owners().contains(sender) {
181181
return Err(TransactionError::InsufficientPermission.into())
182182
}
183183

@@ -510,12 +510,12 @@ impl<B: Backend + ShardBackend> ShardState<B> for ShardLevelState<B> {
510510
shard_id: ShardId,
511511
transaction: &Transaction,
512512
sender: &Address,
513-
shard_owner: &Address,
513+
shard_owners: &[Address],
514514
) -> StateResult<TransactionInvoice> {
515515
ctrace!(TX, "Execute {:?}(TxHash:{:?})", transaction, transaction.hash());
516516

517517
self.create_checkpoint(TRANSACTION_CHECKPOINT);
518-
let result = self.apply_internal(shard_id, transaction, sender, shard_owner);
518+
let result = self.apply_internal(shard_id, transaction, sender, shard_owners);
519519
match result {
520520
Ok(_) => {
521521
cinfo!(TX, "Tx({}) is applied", transaction.hash());
@@ -573,7 +573,7 @@ mod tests {
573573

574574
let sender = address();
575575
let shard_owner = address();
576-
let result = state.apply(shard_id, &transaction, &sender, &shard_owner);
576+
let result = state.apply(shard_id, &transaction, &sender, &[shard_owner]);
577577
assert_eq!(Ok(TransactionInvoice::Success), result);
578578

579579
let metadata = state.metadata();
@@ -602,7 +602,7 @@ mod tests {
602602

603603
let sender = address();
604604
let shard_owner = address();
605-
let result = state.apply(shard_id, &transaction, &sender, &shard_owner);
605+
let result = state.apply(shard_id, &transaction, &sender, &[shard_owner]);
606606
assert_eq!(Ok(TransactionInvoice::Success), result);
607607

608608
let metadata = state.metadata();
@@ -636,7 +636,7 @@ mod tests {
636636
expected: 0,
637637
found: 1
638638
}))),
639-
state.apply(shard_id, &transaction, &sender, &shard_owner)
639+
state.apply(shard_id, &transaction, &sender, &[shard_owner])
640640
);
641641

642642
let metadata = state.metadata();
@@ -672,7 +672,7 @@ mod tests {
672672

673673
let sender = address();
674674
let shard_owner = address();
675-
let result = state.apply(shard_id, &transaction, &sender, &shard_owner);
675+
let result = state.apply(shard_id, &transaction, &sender, &[shard_owner]);
676676
assert_eq!(Ok(TransactionInvoice::Success), result);
677677

678678
let transaction_hash = transaction.hash();
@@ -709,7 +709,7 @@ mod tests {
709709

710710
let sender = address();
711711
let shard_owner = address();
712-
let result = state.apply(shard_id, &transaction, &sender, &shard_owner);
712+
let result = state.apply(shard_id, &transaction, &sender, &[shard_owner]);
713713
assert_eq!(Ok(TransactionInvoice::Success), result);
714714

715715
let transaction_hash = transaction.hash();
@@ -752,7 +752,7 @@ mod tests {
752752

753753
let sender = address();
754754
let shard_owner = address();
755-
assert_eq!(Ok(TransactionInvoice::Success), state.apply(shard_id, &mint, &sender, &shard_owner));
755+
assert_eq!(Ok(TransactionInvoice::Success), state.apply(shard_id, &mint, &sender, &[shard_owner]));
756756

757757
let asset_scheme_address = AssetSchemeAddress::new(mint_hash, shard_id);
758758
let asset_scheme = state.asset_scheme(&asset_scheme_address);
@@ -802,7 +802,7 @@ mod tests {
802802
};
803803
let transfer_hash = transfer.hash();
804804

805-
assert_eq!(Ok(TransactionInvoice::Success), state.apply(shard_id, &transfer, &sender, &shard_owner));
805+
assert_eq!(Ok(TransactionInvoice::Success), state.apply(shard_id, &transfer, &sender, &[shard_owner]));
806806

807807
let asset0_address = AssetAddress::new(transfer_hash, 0, shard_id);
808808
let asset0 = state.asset(&asset0_address);
@@ -844,7 +844,7 @@ mod tests {
844844

845845
let sender = address();
846846
let shard_owner = address();
847-
assert_eq!(Ok(TransactionInvoice::Success), state.apply(shard_id, &mint, &sender, &shard_owner));
847+
assert_eq!(Ok(TransactionInvoice::Success), state.apply(shard_id, &mint, &sender, &[shard_owner]));
848848

849849
let asset_scheme_address = AssetSchemeAddress::new(mint_hash, shard_id);
850850
let asset_scheme = state.asset_scheme(&asset_scheme_address);
@@ -881,7 +881,7 @@ mod tests {
881881

882882
let sender = address();
883883
let shard_owner = address();
884-
let failed_invoice = state.apply(shard_id, &failed_transfer, &sender, &shard_owner).unwrap();
884+
let failed_invoice = state.apply(shard_id, &failed_transfer, &sender, &[shard_owner]).unwrap();
885885
assert_eq!(
886886
TransactionInvoice::Fail(TransactionError::ScriptHashMismatch(Mismatch {
887887
expected: lock_script_hash,
@@ -928,7 +928,10 @@ mod tests {
928928
};
929929
let successful_transfer_hash = successful_transfer.hash();
930930

931-
assert_eq!(Ok(TransactionInvoice::Success), state.apply(shard_id, &successful_transfer, &sender, &shard_owner));
931+
assert_eq!(
932+
Ok(TransactionInvoice::Success),
933+
state.apply(shard_id, &successful_transfer, &sender, &[shard_owner])
934+
);
932935

933936
let asset0_address = AssetAddress::new(successful_transfer_hash, 0, shard_id);
934937
let asset0 = state.asset(&asset0_address);
@@ -979,7 +982,7 @@ mod tests {
979982
}
980983
}
981984
};
982-
assert_eq!(Ok(TransactionInvoice::Success), state.apply(shard_id, &transaction, &shard_owner, &shard_owner));
985+
assert_eq!(Ok(TransactionInvoice::Success), state.apply(shard_id, &transaction, &shard_owner, &[shard_owner]));
983986

984987
let world = state.world(world_id);
985988
assert_eq!(Ok(Some(World::new_with_nonce(new_owners, 1))), world);
@@ -1015,7 +1018,7 @@ mod tests {
10151018
};
10161019

10171020
let shard_owner = Address::random();
1018-
assert_eq!(Ok(TransactionInvoice::Success), state.apply(shard_id, &transaction, &sender, &shard_owner));
1021+
assert_eq!(Ok(TransactionInvoice::Success), state.apply(shard_id, &transaction, &sender, &[shard_owner]));
10191022

10201023
let world = state.world(world_id);
10211024
assert_eq!(Ok(Some(World::new_with_nonce(owners, 1))), world);
@@ -1061,7 +1064,7 @@ mod tests {
10611064
let shard_owner = address();
10621065
assert_eq!(
10631066
Ok(TransactionInvoice::Fail(TransactionError::InsufficientPermission)),
1064-
state.apply(shard_id, &transaction, &sender, &shard_owner)
1067+
state.apply(shard_id, &transaction, &sender, &[shard_owner])
10651068
);
10661069
let world = state.world(world_id);
10671070
assert_eq!(Ok(Some(World::new_with_nonce(owners, 0))), world);

0 commit comments

Comments
 (0)