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

add chain id & chain config check to peering process #785

Merged
merged 9 commits into from
Dec 8, 2022

Conversation

leviathanbeak
Copy link
Contributor

@leviathanbeak leviathanbeak commented Nov 23, 2022

closes #646

libp2p allows us to implement Inbound and Outbound upgrades during connection initialization,
so I've implemented those 2 traits for our FuelUpgrade in which we compare the checksum (hash of the ChainConfig) and if they are not correct the connection is rejected.

Wrote a test to confirm that the connection will not be established, all other tests now implicitly confirm that the connection is established if the checksum matches.

Since we needed to pass ChainConfig variable into P2pArgs for P2pConfig initialization, I had to refactor P2pArgs a bit, now instead of implementing From Trait for P2pArgs -> P2pConfig I created an instance method that consumes P2pArgs with a reference to ChainConfig

Old:

let _: P2pConfig = p2p_args.into();

New:

let _: P2pConfig = p2p_args.into_config(&chain_conf);

@leviathanbeak leviathanbeak marked this pull request as ready for review December 8, 2022 19:26
@leviathanbeak leviathanbeak requested a review from a team December 8, 2022 19:27
Copy link
Member

@Voxelot Voxelot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work!

@leviathanbeak leviathanbeak merged commit 8441a3a into master Dec 8, 2022
@leviathanbeak leviathanbeak deleted the leviathanbeak/add_chain_id_to_peering_process branch December 8, 2022 20:45
@xgreenx
Copy link
Collaborator

xgreenx commented Dec 8, 2022

Ah, wait, why so fast, I almost finished my comment

@@ -66,13 +74,19 @@ const MAX_NUM_OF_FRAMES_BUFFERED: usize = 256;
/// inbound and outbound connections established through the transport.
const TRANSPORT_TIMEOUT: Duration = Duration::from_secs(20);

/// Sha256 hash of chain id and chain config
type Checksum = [u8; 32];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to use its own type to ensure that the checksum was correctly created.

fn from(args: P2pArgs) -> Self {
impl P2pArgs {
pub fn into_config(self, chain_config: &ChainConfig) -> anyhow::Result<P2PConfig> {
let checksum = *fuel_crypto::Hasher::default()
Copy link
Collaborator

@xgreenx xgreenx Dec 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to use the same serialization/merklezation mechanism as we use for the genesis block here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm this fn you linked to doesn't include the initial state root or chain name though

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then, maybe better to use the root of the genesis block=)

Regarding the name of the network, we can include it, if you think that we need this kind of information in the ChainConfig

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it should be a checksum of the full chainspec / entire genesis block.

Also, these root functions should exist in the chain-config module so that they can be used without fuel-core. As fuel-p2p is in it's own crate and won't be able to use the genesis module directly.

@leviathanbeak
Copy link
Contributor Author

@xgreenx sorry, it's late for me, I saw one approval and thought let me close the day with a merge, will address your comments tomorrow, thanks for the review :)

@xgreenx
Copy link
Collaborator

xgreenx commented Dec 9, 2022

Create an issue not to forget #824

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add chainId and chainspec validation to peering process
3 participants