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

refactor: move signature verification functions to EpochManagerAdapter #7656

Merged
merged 2 commits into from
Sep 21, 2022
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
116 changes: 58 additions & 58 deletions chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,53 +500,6 @@ impl EpochManagerAdapter for KeyValueRuntime {
) -> Result<(ValidatorStake, bool), Error> {
Err(Error::NotAValidator)
}
}

impl RuntimeAdapter for KeyValueRuntime {
fn genesis_state(&self) -> (Store, Vec<StateRoot>) {
(self.store.clone(), ((0..self.num_shards).map(|_| Trie::EMPTY_ROOT).collect()))
}

fn get_store(&self) -> Store {
self.store.clone()
}

fn get_tries(&self) -> ShardTries {
self.tries.clone()
}

fn get_trie_for_shard(
&self,
shard_id: ShardId,
_block_hash: &CryptoHash,
state_root: StateRoot,
) -> Result<Trie, Error> {
Ok(self
.tries
.get_trie_for_shard(ShardUId { version: 0, shard_id: shard_id as u32 }, state_root))
}

fn get_view_trie_for_shard(
&self,
shard_id: ShardId,
_block_hash: &CryptoHash,
state_root: StateRoot,
) -> Result<Trie, Error> {
Ok(self.tries.get_view_trie_for_shard(
ShardUId { version: 0, shard_id: shard_id as u32 },
state_root,
))
}

fn get_flat_storage_state_for_shard(&self, _shard_id: ShardId) -> Option<FlatStorageState> {
None
}
fn add_flat_storage_state_for_shard(
&self,
_shard_id: ShardId,
_flat_storage_state: FlatStorageState,
) {
}

fn verify_block_vrf(
&self,
Expand All @@ -570,6 +523,17 @@ impl RuntimeAdapter for KeyValueRuntime {
Ok(true)
}

fn verify_validator_or_fisherman_signature(
&self,
_epoch_id: &EpochId,
_last_known_block_hash: &CryptoHash,
_account_id: &AccountId,
_data: &[u8],
_signature: &Signature,
) -> Result<bool, Error> {
Ok(true)
}

fn verify_header_signature(&self, header: &BlockHeader) -> Result<bool, Error> {
let validator = self.get_block_producer(&header.epoch_id(), header.height())?;
let validator_stake = &self.validators[&validator];
Expand Down Expand Up @@ -597,6 +561,53 @@ impl RuntimeAdapter for KeyValueRuntime {
) -> Result<bool, Error> {
Ok(true)
}
}

impl RuntimeAdapter for KeyValueRuntime {
fn genesis_state(&self) -> (Store, Vec<StateRoot>) {
(self.store.clone(), ((0..self.num_shards).map(|_| Trie::EMPTY_ROOT).collect()))
}

fn get_store(&self) -> Store {
self.store.clone()
}

fn get_tries(&self) -> ShardTries {
self.tries.clone()
}

fn get_trie_for_shard(
&self,
shard_id: ShardId,
_block_hash: &CryptoHash,
state_root: StateRoot,
) -> Result<Trie, Error> {
Ok(self
.tries
.get_trie_for_shard(ShardUId { version: 0, shard_id: shard_id as u32 }, state_root))
}

fn get_view_trie_for_shard(
&self,
shard_id: ShardId,
_block_hash: &CryptoHash,
state_root: StateRoot,
) -> Result<Trie, Error> {
Ok(self.tries.get_view_trie_for_shard(
ShardUId { version: 0, shard_id: shard_id as u32 },
state_root,
))
}

fn get_flat_storage_state_for_shard(&self, _shard_id: ShardId) -> Option<FlatStorageState> {
None
}
fn add_flat_storage_state_for_shard(
&self,
_shard_id: ShardId,
_flat_storage_state: FlatStorageState,
) {
}

fn verify_approvals_and_threshold_orphan(
&self,
Expand Down Expand Up @@ -1273,17 +1284,6 @@ impl RuntimeAdapter for KeyValueRuntime {
Ok(false)
}

fn verify_validator_or_fisherman_signature(
&self,
_epoch_id: &EpochId,
_last_known_block_hash: &CryptoHash,
_account_id: &AccountId,
_data: &[u8],
_signature: &Signature,
) -> Result<bool, Error> {
Ok(true)
}

fn get_protocol_config(&self, _epoch_id: &EpochId) -> Result<ProtocolConfig, Error> {
unreachable!("get_protocol_config should not be called in KeyValueRuntime");
}
Expand Down
76 changes: 0 additions & 76 deletions chain/chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use near_primitives::hash::CryptoHash;
use near_primitives::merkle::{merklize, MerklePath};
use near_primitives::receipt::Receipt;
use near_primitives::shard_layout::{ShardLayout, ShardUId};
use near_primitives::sharding::{ChunkHash, ShardChunkHeader};
use near_primitives::state_part::PartId;
use near_primitives::transaction::{ExecutionOutcomeWithId, SignedTransaction};
use near_primitives::types::validator_stake::{ValidatorStake, ValidatorStakeIter};
Expand Down Expand Up @@ -296,15 +295,6 @@ pub trait RuntimeAdapter: EpochManagerAdapter + Send + Sync {
flat_storage_state: FlatStorageState,
);

fn verify_block_vrf(
&self,
epoch_id: &EpochId,
block_height: BlockHeight,
prev_random_value: &CryptoHash,
vrf_value: &near_crypto::vrf::Value,
vrf_proof: &near_crypto::vrf::Proof,
) -> Result<(), Error>;

/// Validates a given signed transaction.
/// If the state root is given, then the verification will use the account. Otherwise it will
/// only validate the transaction math, limits and signatures.
Expand Down Expand Up @@ -342,72 +332,6 @@ pub trait RuntimeAdapter: EpochManagerAdapter + Send + Sync {
current_protocol_version: ProtocolVersion,
) -> Result<Vec<SignedTransaction>, Error>;

/// Verify validator signature for the given epoch.
/// Note: doesnt't account for slashed accounts within given epoch. USE WITH CAUTION.
fn verify_validator_signature(
&self,
epoch_id: &EpochId,
last_known_block_hash: &CryptoHash,
account_id: &AccountId,
data: &[u8],
signature: &Signature,
) -> Result<bool, Error>;

/// Verify signature for validator or fisherman. Used for validating challenges.
fn verify_validator_or_fisherman_signature(
&self,
epoch_id: &EpochId,
last_known_block_hash: &CryptoHash,
account_id: &AccountId,
data: &[u8],
signature: &Signature,
) -> Result<bool, Error>;

/// Verify header signature.
fn verify_header_signature(&self, header: &BlockHeader) -> Result<bool, Error>;

/// Verify chunk header signature.
/// return false if the header signature does not match the key for the assigned chunk producer
/// for this chunk, or if the chunk producer has been slashed
/// return `Error::NotAValidator` if cannot find chunk producer info for this chunk
/// `header`: chunk header
/// `epoch_id`: epoch_id that the chunk header belongs to
/// `last_known_hash`: used to determine the list of chunk producers that are slashed
fn verify_chunk_header_signature(
&self,
header: &ShardChunkHeader,
epoch_id: &EpochId,
last_known_hash: &CryptoHash,
) -> Result<bool, Error> {
self.verify_chunk_signature_with_header_parts(
&header.chunk_hash(),
header.signature(),
epoch_id,
last_known_hash,
header.height_created(),
header.shard_id(),
)
}

fn verify_chunk_signature_with_header_parts(
&self,
chunk_hash: &ChunkHash,
signature: &Signature,
epoch_id: &EpochId,
last_known_hash: &CryptoHash,
height_created: BlockHeight,
shard_id: ShardId,
) -> Result<bool, Error>;

/// Verify aggregated bls signature
fn verify_approval(
&self,
prev_block_hash: &CryptoHash,
prev_block_height: BlockHeight,
block_height: BlockHeight,
approvals: &[Option<Signature>],
) -> Result<bool, Error>;

/// Verify approvals and check threshold, but ignore next epoch approvals and slashing
fn verify_approvals_and_threshold_orphan(
&self,
Expand Down
Loading