-
Notifications
You must be signed in to change notification settings - Fork 70
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
Validator address as key for the Dht #3014
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
[package] | ||
name = "nimiq-dht" | ||
version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
description = "Nimiq Dht verifier implementation." | ||
homepage.workspace = true | ||
repository.workspace = true | ||
license.workspace = true | ||
categories.workspace = true | ||
keywords.workspace = true | ||
|
||
[badges] | ||
travis-ci = { repository = "nimiq/core-rs", branch = "master" } | ||
is-it-maintained-issue-resolution = { repository = "nimiq/core-rs" } | ||
is-it-maintained-open-issues = { repository = "nimiq/core-rs" } | ||
maintenance = { status = "experimental" } | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
log = { workspace = true } | ||
|
||
nimiq-blockchain-interface = { workspace = true } | ||
nimiq-blockchain-proxy = { workspace = true } | ||
nimiq-keys = { workspace = true } | ||
nimiq-log = { workspace = true, optional = true } | ||
nimiq-network-libp2p = { workspace = true } | ||
nimiq-serde = { workspace = true } | ||
nimiq-utils = { workspace = true } | ||
nimiq-validator-network = { workspace = true } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use nimiq_blockchain_proxy::BlockchainProxy; | ||
use nimiq_keys::{Address, KeyPair}; | ||
use nimiq_network_libp2p::{ | ||
dht::{DhtRecord, DhtVerifierError, Verifier as DhtVerifier}, | ||
libp2p::kad::Record, | ||
PeerId, | ||
}; | ||
use nimiq_serde::Deserialize; | ||
use nimiq_utils::tagged_signing::{TaggedSignable, TaggedSigned}; | ||
use nimiq_validator_network::validator_record::ValidatorRecord; | ||
|
||
pub struct Verifier { | ||
blockchain: BlockchainProxy, | ||
} | ||
|
||
impl Verifier { | ||
pub fn new(blockchain: BlockchainProxy) -> Self { | ||
Self { blockchain } | ||
} | ||
|
||
fn verify_validator_record(&self, record: &Record) -> Result<DhtRecord, DhtVerifierError> { | ||
// Deserialize the value of the record, which is a ValidatorRecord. If it fails return an error. | ||
let validator_record = | ||
TaggedSigned::<ValidatorRecord<PeerId>, KeyPair>::deserialize_from_vec(&record.value) | ||
.map_err(DhtVerifierError::MalformedValue)?; | ||
|
||
// Deserialize the key of the record which is an Address. If it fails return an error. | ||
let validator_address = Address::deserialize_from_vec(record.key.as_ref()) | ||
.map_err(DhtVerifierError::MalformedKey)?; | ||
|
||
// Acquire blockchain read access. For now exclude Light clients. | ||
let blockchain = match self.blockchain { | ||
BlockchainProxy::Light(ref _light_blockchain) => { | ||
return Err(DhtVerifierError::UnknownTag) | ||
} | ||
BlockchainProxy::Full(ref full_blockchain) => full_blockchain, | ||
}; | ||
let blockchain_read = blockchain.read(); | ||
|
||
// Get the staking contract to retrieve the public key for verification. | ||
let staking_contract = blockchain_read | ||
.get_staking_contract_if_complete(None) | ||
.ok_or(DhtVerifierError::StateIncomplete)?; | ||
|
||
// Get the public key needed for verification. | ||
let data_store = blockchain_read.get_staking_contract_store(); | ||
let txn = blockchain_read.read_transaction(); | ||
let public_key = staking_contract | ||
.get_validator(&data_store.read(&txn), &validator_address) | ||
.ok_or(DhtVerifierError::UnknownValidator(validator_address))? | ||
.signing_key; | ||
|
||
// Verify the record. | ||
validator_record | ||
.verify(&public_key) | ||
.then(|| { | ||
DhtRecord::Validator( | ||
record.publisher.unwrap(), | ||
validator_record.record, | ||
record.clone(), | ||
) | ||
}) | ||
.ok_or(DhtVerifierError::InvalidSignature) | ||
} | ||
} | ||
|
||
impl DhtVerifier for Verifier { | ||
fn verify(&self, record: &Record) -> Result<DhtRecord, DhtVerifierError> { | ||
// Peek the tag to know what kind of record this is. | ||
let Some(tag) = TaggedSigned::<ValidatorRecord<PeerId>, KeyPair>::peek_tag(&record.value) | ||
else { | ||
log::warn!(?record, "DHT Tag not peekable."); | ||
return Err(DhtVerifierError::MalformedTag); | ||
}; | ||
|
||
// Depending on tag perform the verification. | ||
match tag { | ||
ValidatorRecord::<PeerId>::TAG => self.verify_validator_record(record), | ||
_ => { | ||
log::error!(tag, "DHT invalid record tag received"); | ||
Err(DhtVerifierError::UnknownTag) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if we have the
full-consensus
feature enabled at compile time, we might still start a light client.