Skip to content

Commit

Permalink
refactor: Integrate extracted and published near-account-id (#10019)
Browse files Browse the repository at this point in the history
This PR supersedes #10001

Some context:
#10001 (comment)

Part of: near/near-account-id-rs#5
  • Loading branch information
frol authored Nov 1, 2023
2 parents ff4c7c9 + 0dbedda commit da8393b
Show file tree
Hide file tree
Showing 50 changed files with 151 additions and 1,285 deletions.
17 changes: 3 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ members = [
"chain/rosetta-rpc",
"chain/telemetry",
"core/async",
"core/account-id",
"core/account-id/fuzz",
"core/chain-configs",
"core/crypto",
"core/dyn-configs",
Expand Down Expand Up @@ -176,7 +174,7 @@ lru = "0.7.2"
memmap2 = "0.5"
memoffset = "0.8"
more-asserts = "0.2"
near-account-id = { path = "core/account-id", features = ["internal_unstable"] }
near-account-id = { version = "1.0.0-alpha.1", features = ["internal_unstable", "serde", "borsh"] }
near-actix-test-utils = { path = "test-utils/actix-test-utils" }
near-amend-genesis = { path = "tools/amend-genesis" }
near-database-tool = { path = "tools/database" }
Expand Down
6 changes: 4 additions & 2 deletions chain/client/src/test_utils/test_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,10 @@ impl TestEnv {
relayer: AccountId,
receiver_id: AccountId,
) -> SignedTransaction {
let inner_signer = InMemorySigner::from_seed(sender.clone(), KeyType::ED25519, &sender);
let relayer_signer = InMemorySigner::from_seed(relayer.clone(), KeyType::ED25519, &relayer);
let inner_signer =
InMemorySigner::from_seed(sender.clone(), KeyType::ED25519, sender.as_str());
let relayer_signer =
InMemorySigner::from_seed(relayer.clone(), KeyType::ED25519, relayer.as_str());
let tip = self.clients[0].chain.head().unwrap();
let user_nonce = tip.height + 1;
let relayer_nonce = tip.height + 1;
Expand Down
2 changes: 1 addition & 1 deletion chain/client/src/tests/bug_repros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ fn test_long_gap_between_blocks() {
if approval_message.approval.target_height < target_height {
(NetworkResponses::NoResponse.into(), false)
} else {
if approval_message.target.as_ref() == "test1" {
if approval_message.target == "test1" {
(NetworkResponses::NoResponse.into(), true)
} else {
(NetworkResponses::NoResponse.into(), false)
Expand Down
5 changes: 3 additions & 2 deletions chain/client/src/tests/catching_up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use near_network::types::{AccountIdOrPeerTrackingShard, PeerInfo};
use near_network::types::{NetworkRequests, NetworkResponses, PeerManagerMessageRequest};
use near_o11y::testonly::init_integration_logger;
use near_o11y::WithSpanContextExt;
use near_primitives::account::id::AccountIdRef;
use near_primitives::hash::{hash as hash_func, CryptoHash};
use near_primitives::network::PeerId;
use near_primitives::receipt::Receipt;
Expand Down Expand Up @@ -689,8 +690,8 @@ fn test_chunk_grieving() {
let archive = vec![false; vs.all_block_producers().count()];
let epoch_sync_enabled = vec![true; vs.all_block_producers().count()];

let malicious_node = "test3.6".parse().unwrap();
let victim_node = "test3.5".parse().unwrap();
let malicious_node = AccountIdRef::new_or_panic("test3.6");
let victim_node = AccountIdRef::new_or_panic("test3.5");
let phase = Arc::new(RwLock::new(ChunkGrievingPhases::FirstAttack));
let grieving_chunk_hash = Arc::new(RwLock::new(ChunkHash::default()));
let unaccepted_block_hash = Arc::new(RwLock::new(CryptoHash::default()));
Expand Down
69 changes: 37 additions & 32 deletions chain/epoch-manager/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::test_utils::{
record_with_block_info, reward, setup_default_epoch_manager, setup_epoch_manager, stake,
DEFAULT_TOTAL_SUPPLY,
};
use near_primitives::account::id::AccountIdRef;
use near_primitives::challenge::SlashedValidator;
use near_primitives::epoch_manager::EpochConfig;
use near_primitives::hash::hash;
Expand Down Expand Up @@ -167,7 +168,7 @@ fn test_validator_change_of_stake() {
],
);
matches!(
epoch_info.validator_kickout().get("test1"),
epoch_info.validator_kickout().get(AccountIdRef::new_or_panic("test1")),
Some(ValidatorKickoutReason::NotEnoughStake { stake: 10, .. })
);
}
Expand Down Expand Up @@ -211,7 +212,7 @@ fn test_fork_finalization() {
let block_producer_id = EpochManager::block_producer_from_info(&epoch_info, height);
let block_producer = epoch_info.get_validator(block_producer_id);
let account_id = block_producer.account_id();
if validator_accounts.iter().any(|v| *v == account_id.as_ref()) {
if validator_accounts.iter().any(|v| *v == account_id) {
record_block(epoch_manager, prev_block, *curr_block, height, vec![]);
prev_block = *curr_block;
branch_blocks.push(*curr_block);
Expand Down Expand Up @@ -332,10 +333,10 @@ fn test_validator_kickout() {
let height = i as u64;
let epoch_id = epoch_manager.get_epoch_id_from_prev_block(&prev_block).unwrap();
let block_producer = epoch_manager.get_block_producer_info(&epoch_id, height).unwrap();
if block_producer.account_id().as_ref() == "test2" && epoch_id == init_epoch_id {
if block_producer.account_id() == "test2" && epoch_id == init_epoch_id {
// test2 skips its blocks in the first epoch
test2_expected_blocks += 1;
} else if block_producer.account_id().as_ref() == "test1" && epoch_id != init_epoch_id {
} else if block_producer.account_id() == "test1" && epoch_id != init_epoch_id {
// test1 skips its blocks in subsequent epochs
()
} else {
Expand Down Expand Up @@ -733,8 +734,8 @@ fn test_validator_reward_one_validator() {
PROTOCOL_VERSION,
epoch_length * NUM_NS_IN_SECOND,
);
let test2_reward = *validator_reward.get("test2").unwrap();
let protocol_reward = *validator_reward.get("near").unwrap();
let test2_reward = *validator_reward.get(AccountIdRef::new_or_panic("test2")).unwrap();
let protocol_reward = *validator_reward.get(AccountIdRef::new_or_panic("near")).unwrap();

let epoch_info = epoch_manager.get_epoch_info(&EpochId(h[2])).unwrap();
check_validators(&epoch_info, &[("test2", stake_amount + test2_reward)]);
Expand Down Expand Up @@ -831,10 +832,10 @@ fn test_validator_reward_weight_by_stake() {
PROTOCOL_VERSION,
epoch_length * NUM_NS_IN_SECOND,
);
let test1_reward = *validator_reward.get("test1").unwrap();
let test2_reward = *validator_reward.get("test2").unwrap();
let test1_reward = *validator_reward.get(AccountIdRef::new_or_panic("test1")).unwrap();
let test2_reward = *validator_reward.get(AccountIdRef::new_or_panic("test2")).unwrap();
assert_eq!(test1_reward, test2_reward * 2);
let protocol_reward = *validator_reward.get("near").unwrap();
let protocol_reward = *validator_reward.get(AccountIdRef::new_or_panic("near")).unwrap();

let epoch_info = epoch_manager.get_epoch_info(&EpochId(h[2])).unwrap();
check_validators(
Expand Down Expand Up @@ -915,9 +916,7 @@ fn test_reward_multiple_shards() {
let expected_chunk_producer = epoch_manager
.get_chunk_producer_info(&epoch_id, height, shard_index as u64)
.unwrap();
if expected_chunk_producer.account_id().as_ref() == "test1"
&& epoch_id == init_epoch_id
{
if expected_chunk_producer.account_id() == "test1" && epoch_id == init_epoch_id {
expected_chunks += 1;
false
} else {
Expand Down Expand Up @@ -949,8 +948,8 @@ fn test_reward_multiple_shards() {
PROTOCOL_VERSION,
epoch_length * NUM_NS_IN_SECOND,
);
let test2_reward = *validator_reward.get("test2").unwrap();
let protocol_reward = *validator_reward.get("near").unwrap();
let test2_reward = *validator_reward.get(AccountIdRef::new_or_panic("test2")).unwrap();
let protocol_reward = *validator_reward.get(AccountIdRef::new_or_panic("near")).unwrap();
let epoch_infos: Vec<_> =
h.iter().filter_map(|x| epoch_manager.get_epoch_info(&EpochId(*x)).ok()).collect();
let epoch_info = &epoch_infos[1];
Expand Down Expand Up @@ -1371,7 +1370,7 @@ fn count_missing_blocks(
let mut result = ValidatorStats { produced: 0, expected: 0 };
for h in height_range {
let block_producer = epoch_manager.get_block_producer_info(epoch_id, h).unwrap();
if validator == block_producer.account_id().as_ref() {
if validator == block_producer.account_id() {
if produced_heights.contains(&h) {
result.produced += 1;
}
Expand Down Expand Up @@ -1627,8 +1626,14 @@ fn test_fishermen_unstake() {
],
);
let kickout = epoch_info.validator_kickout();
assert_eq!(kickout.get("test2").unwrap(), &ValidatorKickoutReason::Unstaked);
matches!(kickout.get("test3"), Some(ValidatorKickoutReason::NotEnoughStake { .. }));
assert_eq!(
kickout.get(AccountIdRef::new_or_panic("test2")).unwrap(),
&ValidatorKickoutReason::Unstaked
);
matches!(
kickout.get(AccountIdRef::new_or_panic("test3")),
Some(ValidatorKickoutReason::NotEnoughStake { .. })
);
}

#[test]
Expand Down Expand Up @@ -1711,7 +1716,7 @@ fn test_kickout_set() {
let epoch_info1 = epoch_manager.get_epoch_info(&EpochId(h[2])).unwrap();
assert_eq!(
epoch_info1.validators_iter().map(|r| r.account_id().clone()).collect::<Vec<_>>(),
vec!["test1".parse().unwrap()]
vec!["test1"]
);
assert_eq!(
epoch_info1.stake_change().clone(),
Expand Down Expand Up @@ -1798,15 +1803,15 @@ fn test_unstake_slash() {
let epoch_info3 = epoch_manager.get_epoch_info(&EpochId(h[3])).unwrap();
let epoch_info4 = epoch_manager.get_epoch_info(&EpochId(h[4])).unwrap();
assert_eq!(
epoch_info1.validator_kickout().get("test1"),
epoch_info1.validator_kickout().get(AccountIdRef::new_or_panic("test1")),
Some(&ValidatorKickoutReason::Unstaked)
);
assert_eq!(
epoch_info2.validator_kickout().get("test1"),
epoch_info2.validator_kickout().get(AccountIdRef::new_or_panic("test1")),
Some(&ValidatorKickoutReason::Slashed)
);
assert_eq!(
epoch_info3.validator_kickout().get("test1"),
epoch_info3.validator_kickout().get(AccountIdRef::new_or_panic("test1")),
Some(&ValidatorKickoutReason::Slashed)
);
assert!(epoch_info4.validator_kickout().is_empty());
Expand Down Expand Up @@ -1848,15 +1853,15 @@ fn test_no_unstake_slash() {
let epoch_info3 = epoch_manager.get_epoch_info(&EpochId(h[3])).unwrap();
let epoch_info4 = epoch_manager.get_epoch_info(&EpochId(h[4])).unwrap();
assert_eq!(
epoch_info1.validator_kickout().get("test1"),
epoch_info1.validator_kickout().get(AccountIdRef::new_or_panic("test1")),
Some(&ValidatorKickoutReason::Slashed)
);
assert_eq!(
epoch_info2.validator_kickout().get("test1"),
epoch_info2.validator_kickout().get(AccountIdRef::new_or_panic("test1")),
Some(&ValidatorKickoutReason::Slashed)
);
assert_eq!(
epoch_info3.validator_kickout().get("test1"),
epoch_info3.validator_kickout().get(AccountIdRef::new_or_panic("test1")),
Some(&ValidatorKickoutReason::Slashed)
);
assert!(epoch_info4.validator_kickout().is_empty());
Expand Down Expand Up @@ -1900,16 +1905,16 @@ fn test_slash_non_validator() {
let epoch_info4 = epoch_manager.get_epoch_info(&EpochId(h[4])).unwrap(); // Slashed
let epoch_info5 = epoch_manager.get_epoch_info(&EpochId(h[5])).unwrap(); // Ok
assert_eq!(
epoch_info1.validator_kickout().get("test1"),
epoch_info1.validator_kickout().get(AccountIdRef::new_or_panic("test1")),
Some(&ValidatorKickoutReason::Unstaked)
);
assert!(epoch_info2.validator_kickout().is_empty());
assert_eq!(
epoch_info3.validator_kickout().get("test1"),
epoch_info3.validator_kickout().get(AccountIdRef::new_or_panic("test1")),
Some(&ValidatorKickoutReason::Slashed)
);
assert_eq!(
epoch_info4.validator_kickout().get("test1"),
epoch_info4.validator_kickout().get(AccountIdRef::new_or_panic("test1")),
Some(&ValidatorKickoutReason::Slashed)
);
assert!(epoch_info5.validator_kickout().is_empty());
Expand Down Expand Up @@ -1952,9 +1957,9 @@ fn test_slash_restake() {
vec![stake("test1".parse().unwrap(), stake_amount)],
);
let epoch_info2 = epoch_manager.get_epoch_info(&EpochId(h[2])).unwrap();
assert!(epoch_info2.stake_change().get("test1").is_none());
assert!(epoch_info2.stake_change().get(AccountIdRef::new_or_panic("test1")).is_none());
let epoch_info4 = epoch_manager.get_epoch_info(&EpochId(h[4])).unwrap();
assert!(epoch_info4.stake_change().get("test1").is_some());
assert!(epoch_info4.stake_change().get(AccountIdRef::new_or_panic("test1")).is_some());
}

#[test]
Expand All @@ -1979,7 +1984,7 @@ fn test_all_kickout_edge_case() {
let block_producer = epoch_info.validator_account_id(block_producer);
if height < EPOCH_LENGTH {
// kickout test2 during first epoch
if block_producer.as_ref() == "test1" || block_producer.as_ref() == "test3" {
if block_producer == "test1" || block_producer == "test3" {
record_block(&mut epoch_manager, prev_block, *curr_block, height, Vec::new());
prev_block = *curr_block;
}
Expand Down Expand Up @@ -2018,15 +2023,15 @@ fn check_validators(epoch_info: &EpochInfo, expected_validators: &[(&str, u128)]
for (v, (account_id, stake)) in
epoch_info.validators_iter().zip(expected_validators.into_iter())
{
assert_eq!(v.account_id().as_ref(), *account_id);
assert_eq!(v.account_id(), *account_id);
assert_eq!(v.stake(), *stake);
}
}

fn check_fishermen(epoch_info: &EpochInfo, expected_fishermen: &[(&str, u128)]) {
for (v, (account_id, stake)) in epoch_info.fishermen_iter().zip(expected_fishermen.into_iter())
{
assert_eq!(v.account_id().as_ref(), *account_id);
assert_eq!(v.account_id(), *account_id);
assert_eq!(v.stake(), *stake);
}
}
Expand Down
11 changes: 6 additions & 5 deletions chain/epoch-manager/src/validator_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ impl Ord for OrderedValidatorStake {
mod tests {
use super::*;
use near_crypto::{KeyType, PublicKey};
use near_primitives::account::id::AccountIdRef;
use near_primitives::epoch_manager::epoch_info::{EpochInfo, EpochInfoV3};
use near_primitives::epoch_manager::ValidatorSelectionConfig;
use near_primitives::shard_layout::ShardLayout;
Expand Down Expand Up @@ -496,11 +497,11 @@ mod tests {
let kickout = epoch_info.validator_kickout();
assert_eq!(kickout.len(), 2);
assert_eq!(
kickout.get("test1").unwrap(),
kickout.get(AccountIdRef::new_or_panic("test1")).unwrap(),
&ValidatorKickoutReason::NotEnoughStake { stake: test1_stake, threshold: 2011 },
);
assert_eq!(
kickout.get("test2").unwrap(),
kickout.get(AccountIdRef::new_or_panic("test2")).unwrap(),
&ValidatorKickoutReason::NotEnoughStake { stake: 2002, threshold: 2011 },
);
}
Expand Down Expand Up @@ -663,7 +664,7 @@ mod tests {

// stake below validator threshold, but above fishermen threshold become fishermen
let fishermen: Vec<_> = epoch_info.fishermen_iter().map(|v| v.take_account_id()).collect();
assert_eq!(fishermen, vec!["test4".parse().unwrap()]);
assert_eq!(fishermen, vec!["test4"]);

// too low stakes are kicked out
let kickout = epoch_info.validator_kickout();
Expand All @@ -673,11 +674,11 @@ mod tests {
#[cfg(not(feature = "protocol_feature_fix_staking_threshold"))]
let expected_threshold = 300;
assert_eq!(
kickout.get("test5").unwrap(),
kickout.get(AccountIdRef::new_or_panic("test5")).unwrap(),
&ValidatorKickoutReason::NotEnoughStake { stake: 100, threshold: expected_threshold },
);
assert_eq!(
kickout.get("test6").unwrap(),
kickout.get(AccountIdRef::new_or_panic("test6")).unwrap(),
&ValidatorKickoutReason::NotEnoughStake { stake: 50, threshold: expected_threshold },
);

Expand Down
10 changes: 5 additions & 5 deletions chain/jsonrpc/jsonrpc-tests/tests/rpc_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use near_jsonrpc_tests::{self as test_utils, test_with_client};
fn test_block_by_id_height() {
test_with_client!(test_utils::NodeType::NonValidator, client, async move {
let block = client.block_by_id(BlockId::Height(0)).await.unwrap();
assert_eq!(block.author, "test1".parse().unwrap());
assert_eq!(block.author, "test1");
assert_eq!(block.header.height, 0);
assert_eq!(block.header.epoch_id.0.as_ref(), &[0; 32]);
assert_eq!(block.header.hash.0.as_ref().len(), 32);
Expand Down Expand Up @@ -69,7 +69,7 @@ fn test_block_query() {
for block in
&[block_response1, block_response2, block_response3, block_response4, block_response5]
{
assert_eq!(block.author, "test1".parse().unwrap());
assert_eq!(block.author, "test1");
assert_eq!(block.header.height, 0);
assert_eq!(block.header.epoch_id.as_ref(), &[0; 32]);
assert_eq!(block.header.hash.as_ref().len(), 32);
Expand All @@ -89,7 +89,7 @@ fn test_block_query() {
fn test_chunk_by_hash() {
test_with_client!(test_utils::NodeType::NonValidator, client, async move {
let chunk = client.chunk(ChunkId::BlockShardId(BlockId::Height(0), 0u64)).await.unwrap();
assert_eq!(chunk.author, "test1".parse().unwrap());
assert_eq!(chunk.author, "test1");
assert_eq!(chunk.header.balance_burnt, 0);
assert_eq!(chunk.header.chunk_hash.as_ref().len(), 32);
assert_eq!(chunk.header.encoded_length, 8);
Expand Down Expand Up @@ -454,7 +454,7 @@ fn test_validators_ordered() {
.unwrap();
assert_eq!(
validators.into_iter().map(|v| v.take_account_id()).collect::<Vec<_>>(),
vec!["test1".parse().unwrap()]
vec!["test1"]
)
});
}
Expand Down Expand Up @@ -560,7 +560,7 @@ fn test_get_chunk_with_object_in_params() {
)
.await
.unwrap();
assert_eq!(chunk.author, "test1".parse().unwrap());
assert_eq!(chunk.author, "test1");
assert_eq!(chunk.header.balance_burnt, 0);
assert_eq!(chunk.header.chunk_hash.as_ref().len(), 32);
assert_eq!(chunk.header.encoded_length, 8);
Expand Down
Loading

0 comments on commit da8393b

Please sign in to comment.