Skip to content
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

feat: don't assign sub-accounts to fixed shards #8343

Closed
wants to merge 2 commits into from
Closed
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
25 changes: 9 additions & 16 deletions core/primitives/src/shard_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type ShardSplitMap = Vec<Vec<ShardId>>;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct ShardLayoutV1 {
/// num_shards = fixed_shards.len() + boundary_accounts.len() + 1
/// Each account and all sub-accounts map to the shard of position in this array.
/// Each account maps to the shard of position in this array.
fixed_shards: Vec<AccountId>,
/// The rest are divided by boundary_accounts to ranges, each range is mapped to a shard
boundary_accounts: Vec<AccountId>,
Expand Down Expand Up @@ -228,9 +228,9 @@ impl ShardLayout {
/// Maps an account to the shard that it belongs to given a shard_layout
/// For V0, maps according to hash of account id
/// For V1, accounts are divided to ranges, each range of account is mapped to a shard.
/// There are also some fixed shards, each of which is mapped to an account and all sub-accounts.
/// There are also some fixed shards, each of which is mapped to an account.
/// For example, for ShardLayoutV1{ fixed_shards: ["aurora"], boundary_accounts: ["near"]}
/// Account "aurora" and all its sub-accounts will be mapped to shard_id 0.
/// Account "aurora" will be mapped to shard_id 0.
/// For the rest of accounts, accounts <= "near" will be mapped to shard_id 1 and
/// accounts > "near" will be mapped shard_id 2.
pub fn account_id_to_shard_id(account_id: &AccountId, shard_layout: &ShardLayout) -> ShardId {
Expand All @@ -242,7 +242,7 @@ pub fn account_id_to_shard_id(account_id: &AccountId, shard_layout: &ShardLayout
}
ShardLayout::V1(ShardLayoutV1 { fixed_shards, boundary_accounts, .. }) => {
for (shard_id, fixed_account) in fixed_shards.iter().enumerate() {
if is_top_level_account(fixed_account, account_id) {
if account_id == fixed_account {
return shard_id as ShardId;
}
}
Expand All @@ -266,13 +266,6 @@ pub fn account_id_to_shard_uid(account_id: &AccountId, shard_layout: &ShardLayou
)
}

fn is_top_level_account(top_account: &AccountId, account: &AccountId) -> bool {
match account.as_ref().strip_suffix(top_account.as_ref()) {
None => false,
Some(rest) => rest.is_empty() || rest.ends_with('.'),
}
}

/// ShardUId is an unique representation for shards from different shard layout
#[derive(Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct ShardUId {
Expand Down Expand Up @@ -493,19 +486,19 @@ mod tests {
}

assert_eq!(account_id_to_shard_id(&"aurora".parse().unwrap(), &shard_layout), 0);
assert_eq!(account_id_to_shard_id(&"foo.aurora".parse().unwrap(), &shard_layout), 0);
assert_eq!(account_id_to_shard_id(&"bar.foo.aurora".parse().unwrap(), &shard_layout), 0);
assert_eq!(account_id_to_shard_id(&"bar".parse().unwrap(), &shard_layout), 1);
assert_eq!(account_id_to_shard_id(&"bar.bar".parse().unwrap(), &shard_layout), 1);
assert_eq!(account_id_to_shard_id(&"foo".parse().unwrap(), &shard_layout), 2);
assert_eq!(account_id_to_shard_id(&"baz.foo".parse().unwrap(), &shard_layout), 2);
assert_eq!(account_id_to_shard_id(&"foo.baz".parse().unwrap(), &shard_layout), 3);
assert_eq!(account_id_to_shard_id(&"a.foo.baz".parse().unwrap(), &shard_layout), 3);

assert_eq!(account_id_to_shard_id(&"aaa".parse().unwrap(), &shard_layout), 4);
assert_eq!(account_id_to_shard_id(&"a.foo.baz".parse().unwrap(), &shard_layout), 4);
assert_eq!(account_id_to_shard_id(&"abc".parse().unwrap(), &shard_layout), 5);
assert_eq!(account_id_to_shard_id(&"bar.bar".parse().unwrap(), &shard_layout), 5);
assert_eq!(account_id_to_shard_id(&"bar.foo.aurora".parse().unwrap(), &shard_layout), 5);
assert_eq!(account_id_to_shard_id(&"baz.foo".parse().unwrap(), &shard_layout), 5);
assert_eq!(account_id_to_shard_id(&"bbb".parse().unwrap(), &shard_layout), 5);
assert_eq!(account_id_to_shard_id(&"foo.goo".parse().unwrap(), &shard_layout), 6);
assert_eq!(account_id_to_shard_id(&"foo.aurora".parse().unwrap(), &shard_layout), 6);
assert_eq!(account_id_to_shard_id(&"goo".parse().unwrap(), &shard_layout), 6);
assert_eq!(account_id_to_shard_id(&"zoo".parse().unwrap(), &shard_layout), 7);
}
Expand Down