Skip to content

feat: changed peer id location#1138

Merged
antouhou merged 6 commits into
masterfrom
feat-change-peer-id
Feb 16, 2026
Merged

feat: changed peer id location#1138
antouhou merged 6 commits into
masterfrom
feat-change-peer-id

Conversation

@antouhou

@antouhou antouhou commented Feb 13, 2026

Copy link
Copy Markdown
Contributor

Describe the changes
This PR moves peer id from the DB to a file, so we can have cleaner snapshots

Related Issue(s)

Checklist

  • Tests have been added/updated for the changes.
  • Documentation has been updated for the changes (if applicable).
  • The code follows Rust's style guidelines.

Additional Context

Summary by CodeRabbit

  • Refactor
    • Peer identification now uses a local peer-info directory: the node reads a peer key file, derives the peer ID, and generates + persists a new key if missing.
    • The database no longer stores peer IDs.
    • Startup sequence adjusted to obtain peer ID before initializing storage.
    • Configuration exposes the peer-info directory path.

@coderabbitai

coderabbitai Bot commented Feb 13, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉


📝 Walkthrough

Walkthrough

Peer ID storage moved from the database to a file-based approach: startup now reads/writes a secp256k1 key at peer_info/peer_key.bin via NodeConfig to derive the PeerId before initializing the database. Database APIs and metadata keys for PeerId were removed; NodeConfig::peer_info_dir() added.

Changes

Cohort / File(s) Summary
Chain startup / Peer ID
crates/chain/src/chain.rs
Reordered startup to obtain PeerId from NodeConfig/filesystem before DB init. get_or_create_peer_id signature changed to accept &NodeConfig and now loads/generates peer_info/peer_key.bin (secp256k1) to derive PeerId; removed DB-based peer_id flows.
Database API & metadata
crates/database/src/database.rs, crates/database/src/metadata.rs
Removed get_peer_id and set_peer_id DB APIs and eliminated PeerId variant from MetadataKey/Decode; metadata no longer stores peer_id.
Node config
crates/types/src/config/node.rs
Added pub fn peer_info_dir(&self) -> PathBuf accessor to point at the peer_info directory used for peer_key.bin.
Manifest
Cargo.toml, manifest_file
Cargo/manifest lines updated (added/removed entries) to reflect the API/feature changes.

Sequence Diagram(s)

mermaid
sequenceDiagram
rect rgba(200,200,255,0.5)
participant ChainInit
end
rect rgba(200,255,200,0.5)
participant NodeConfig
end
rect rgba(255,200,200,0.5)
participant FileSystem
end
rect rgba(255,255,200,0.5)
participant Database
end

Note over ChainInit,NodeConfig: Node startup
ChainInit->>NodeConfig: request peer_info_dir()
ChainInit->>FileSystem: get_or_create_peer_id(peer_info_dir)
alt peer_key exists
    FileSystem-->>ChainInit: load key -> derive PeerId
else no peer_key
    FileSystem-->>FileSystem: generate 32-byte key, write peer_key.bin
    FileSystem-->>ChainInit: derive PeerId
end
ChainInit->>Database: init_irys_db() (after PeerId obtained)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • feat: peer ids #1110: Changes to peer ID handling and NodeConfig peer identity APIs that touch the same peer_id initialization and usage paths.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: changed peer id location' directly corresponds to the main objective and primary change in the changeset: moving peer id storage from database to file-based storage.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into master

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat-change-peer-id

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🤖 Fix all issues with AI agents
In `@crates/chain/src/chain.rs`:
- Around line 2195-2231: Run rustfmt (cargo fmt) to fix formatting issues in the
get_or_create_peer_id function; specifically reformat the block handling
key_path, the creation branch that defines signing_key, and the final peer_id
return so that spacing/line breaks around key_path.display(), signing_key, and
peer_id match project formatting. Ensure you run `cargo fmt` on the crate
(crates/chain) and recommit the changed formatting for the get_or_create_peer_id
function (references: get_or_create_peer_id, key_path, signing_key, peer_id).
- Around line 2216-2224: The private key is being created with std::fs::write
which may produce world-readable permissions; change the write to explicitly
create the file with owner-only permissions on Unix: open the key_path using
std::fs::OpenOptions (or OpenOptionsExt on Unix) and set mode to 0o600 when
creating, write key.to_bytes().as_slice() into that handle, and fallback to the
existing std::fs::write behavior on non-Unix via cfg(unix) so Windows semantics
are unchanged; update the block around peer_info_dir, key_path, and the key
write to use this secure create-and-write approach.
- Line 2205: The TOCTOU arises because the code checks key_path.exists() before
reading; change to a read-first pattern by attempting std::fs::read(&key_path)
and matching the result: on Ok(data) parse/deserialize to produce signing_key,
on Err(e) if e.kind() == std::io::ErrorKind::NotFound then handle the "missing
key" path (generate a new signing key, create parent dirs if needed, and write
it out), and for other errors return or log the I/O error. Update the logic
surrounding the signing_key variable and key_path usage (the block that
currently starts with let signing_key = if key_path.exists()) to use this
read-first approach.
- Around line 2205-2231: The code derives peer_id three times from the same
private key (inside both branches and again after the if/else); refactor to
derive peer_id once after obtaining signing_key: keep the existing logic that
reads or generates the SigningKey in the if/else (functions/values: key_path,
peer_info_dir, k256::ecdsa::SigningKey, signing_key), remove the duplicate
IrysPeerId::from(irys_types::IrysAddress::from_private_key(...)) calls inside
each branch, and compute let peer_id =
irys_types::IrysPeerId::from(irys_types::IrysAddress::from_private_key(&signing_key))
once after the if/else so you can use peer_id for logging and for the returned
Ok(peer_id).

Comment thread crates/chain/src/chain.rs
Comment thread crates/chain/src/chain.rs Outdated
Comment thread crates/chain/src/chain.rs Outdated
Comment thread crates/chain/src/chain.rs Outdated
Comment on lines +2216 to +2224
std::fs::create_dir_all(&peer_info_dir).with_context(|| {
format!(
"Failed to create peer info directory {}",
peer_info_dir.display()
)
})?;
std::fs::write(&key_path, key.to_bytes().as_slice()).with_context(|| {
format!("Failed to write peer key to {}", key_path.display())
})?;

@coderabbitai coderabbitai Bot Feb 13, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Private key file written with default permissions — may be world-readable.

std::fs::write uses the OS default umask, which on many Linux systems means the file is created with 0644 (world-readable). Since this is a private key that constitutes the node's P2P identity, it should be restricted to owner-only access.

🔒 Proposed fix: set restrictive permissions on Unix
         std::fs::write(&key_path, key.to_bytes().as_slice()).with_context(|| {
             format!("Failed to write peer key to {}", key_path.display())
         })?;
+        #[cfg(unix)]
+        {
+            use std::os::unix::fs::PermissionsExt;
+            std::fs::set_permissions(&key_path, std::fs::Permissions::from_mode(0o600))
+                .with_context(|| {
+                    format!("Failed to set permissions on {}", key_path.display())
+                })?;
+        }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
std::fs::create_dir_all(&peer_info_dir).with_context(|| {
format!(
"Failed to create peer info directory {}",
peer_info_dir.display()
)
})?;
std::fs::write(&key_path, key.to_bytes().as_slice()).with_context(|| {
format!("Failed to write peer key to {}", key_path.display())
})?;
std::fs::create_dir_all(&peer_info_dir).with_context(|| {
format!(
"Failed to create peer info directory {}",
peer_info_dir.display()
)
})?;
std::fs::write(&key_path, key.to_bytes().as_slice()).with_context(|| {
format!("Failed to write peer key to {}", key_path.display())
})?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&key_path, std::fs::Permissions::from_mode(0o600))
.with_context(|| {
format!("Failed to set permissions on {}", key_path.display())
})?;
}
🧰 Tools
🪛 GitHub Check: cargo fmt

[warning] 2219-2219:
Diff in /home/runner/actions-runner/_work/irys/irys/crates/chain/src/chain.rs

🤖 Prompt for AI Agents
In `@crates/chain/src/chain.rs` around lines 2216 - 2224, The private key is being
created with std::fs::write which may produce world-readable permissions; change
the write to explicitly create the file with owner-only permissions on Unix:
open the key_path using std::fs::OpenOptions (or OpenOptionsExt on Unix) and set
mode to 0o600 when creating, write key.to_bytes().as_slice() into that handle,
and fallback to the existing std::fs::write behavior on non-Unix via cfg(unix)
so Windows semantics are unchanged; update the block around peer_info_dir,
key_path, and the key write to use this secure create-and-write approach.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is fine, it's not used for anything - it's just there just in case, in fact we were throwing it out before

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@antouhou, understood! Thank you for the clarification. If the peer key isn't used for security-critical operations and was being regenerated before, then the default file permissions are perfectly fine.


✏️ Learnings added
Learnt from: antouhou
Repo: Irys-xyz/irys PR: 1138
File: crates/chain/src/chain.rs:2216-2224
Timestamp: 2026-02-13T20:39:55.498Z
Learning: In the Irys repository, the peer key file (`<peer_info_dir>/peer_key.bin`) used for deriving the P2P PeerId does not require restrictive file permissions (e.g., 0o600), as it is not used for security-critical operations and was previously regenerated/not persisted permanently.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


🧠 Learnings used
Learnt from: glottologist
Repo: Irys-xyz/irys PR: 1074
File: docker/configs/irys-2.toml:4-5
Timestamp: 2026-01-16T11:57:20.272Z
Learning: In the Irys repository, hardcoded test mining keys and reward addresses in docker/configs/*.toml files (e.g., docker/configs/irys-1.toml, docker/configs/irys-2.toml) are intentional and should not be flagged, even though they appear to be realistic hex values. These files already include `#changeme` comments to indicate they are placeholders.

Comment thread crates/chain/src/chain.rs

@JesseTheRobot JesseTheRobot left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, feel free to merge once CI passes

@antouhou antouhou merged commit 42f0f9d into master Feb 16, 2026
18 checks passed
@antouhou antouhou deleted the feat-change-peer-id branch February 16, 2026 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants