-
Notifications
You must be signed in to change notification settings - Fork 138
feat(funding-service): add server and status endpoint #2600
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1c35a87
feat(funding-service): add server and status endpoint
SantiagoPittella ab2c8b6
review: enforce name and check for duplications in genesis
SantiagoPittella 7564eb4
review: use only partial storage
SantiagoPittella 570d68f
review: use ECDSA for key
SantiagoPittella c25b878
review: refresh fee parameters
SantiagoPittella 81b5faa
review: rename LISTEN to IP:PORT
SantiagoPittella 6067bbb
review: move to HTTP
SantiagoPittella 8274628
chore: remove --genesis, use data from block headers
SantiagoPittella e549cc3
chore: update with latest protocol changes
SantiagoPittella 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
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,41 @@ | ||
| [package] | ||
| authors.workspace = true | ||
| description = "Miden funding service" | ||
| edition.workspace = true | ||
| homepage.workspace = true | ||
| keywords = ["funding", "miden"] | ||
| license.workspace = true | ||
| name = "miden-funding-service" | ||
| readme = "README.md" | ||
| repository.workspace = true | ||
| rust-version.workspace = true | ||
| version.workspace = true | ||
|
|
||
| [lints] | ||
| workspace = true | ||
|
|
||
| [lib] | ||
| doctest = false | ||
|
|
||
| [dependencies] | ||
| anyhow = { workspace = true } | ||
| axum = { workspace = true } | ||
| backon = { workspace = true } | ||
| clap = { features = ["env", "string"], workspace = true } | ||
| humantime = { workspace = true } | ||
| miden-node-proto = { workspace = true } | ||
| miden-node-tracing = { workspace = true } | ||
| miden-node-utils = { workspace = true } | ||
| miden-protocol = { features = ["std"], workspace = true } | ||
| serde = { workspace = true } | ||
| tokio = { features = ["macros", "net", "rt-multi-thread", "sync", "time"], workspace = true } | ||
| tower-http = { features = ["timeout"], workspace = true } | ||
| url = { workspace = true } | ||
|
|
||
| [dev-dependencies] | ||
| miden-protocol = { features = ["std", "testing"], workspace = true } | ||
| miden-standards = { workspace = true } | ||
| rand = { workspace = true } | ||
| rand_chacha = { workspace = true } | ||
| tempfile = { workspace = true } | ||
| tower = { features = ["util"], workspace = true } |
This file contains hidden or 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,20 @@ | ||
| # Miden funding service | ||
|
|
||
| `miden-funding-service` is a Miden node binary that sends the chain's native asset to any account that asks for it. | ||
|
|
||
| ## Operation | ||
|
|
||
| The service holds no chain state. It reads the funding account from the node, so a restart needs no recovery. Only the | ||
| account file, which holds the account ID and its signing key, is on disk. | ||
|
|
||
| The service reads the chain's protocol configuration from the node at startup, together with the genesis block header. | ||
|
|
||
| The service serves a JSON HTTP API. `GET /status` reports the funding account, its balance, and the block that balance | ||
| was read at. An operator alerts on that balance, because the service does not refill itself. | ||
|
|
||
| The service does not authenticate requests. An operator must restrict access to its HTTP API at the infrastructure | ||
| level. | ||
|
|
||
| ## License | ||
|
|
||
| This project is [MIT licensed](../../LICENSE). |
This file contains hidden or 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,107 @@ | ||
| //! Loading of the funding account. | ||
|
|
||
| use std::path::Path; | ||
|
|
||
| use anyhow::{Context, Result}; | ||
| use miden_protocol::account::{AccountFile, AccountId, AccountType}; | ||
|
|
||
| // FUNDER KEY | ||
| // ================================================================================================ | ||
|
|
||
| /// The identity of the funding account, loaded from its account file. | ||
| #[derive(Clone, Debug)] | ||
| pub struct FunderKey { | ||
| account_id: AccountId, | ||
| } | ||
|
|
||
| impl FunderKey { | ||
| /// Reads the funding account from an account file. | ||
| pub fn load(path: &Path) -> Result<Self> { | ||
| let account_file = AccountFile::read(path) | ||
| .with_context(|| format!("failed to read the account file at {}", path.display()))?; | ||
|
|
||
| let account = account_file.account; | ||
| anyhow::ensure!( | ||
| account.id().account_type() == AccountType::Public, | ||
| "the funding account {} is not public: the service reads its state from the node, \ | ||
| which only stores the full state of a public account", | ||
| account.id(), | ||
| ); | ||
|
|
||
| Ok(Self { account_id: account.id() }) | ||
| } | ||
|
|
||
| pub fn account_id(&self) -> AccountId { | ||
| self.account_id | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use miden_protocol::ONE; | ||
| use miden_protocol::account::auth::{AuthScheme, AuthSecretKey}; | ||
| use miden_protocol::account::{Account, AccountType}; | ||
| use miden_protocol::crypto::dsa::falcon512_poseidon2::SecretKey; | ||
| use miden_standards::account::auth::Approver; | ||
| use miden_standards::account::wallets::create_basic_wallet; | ||
| use rand::{RngExt, SeedableRng}; | ||
| use rand_chacha::ChaCha20Rng; | ||
|
|
||
| use super::*; | ||
|
|
||
| /// Builds a wallet the way the genesis configuration does, so the test covers the file the | ||
| /// service actually loads. | ||
| fn genesis_wallet(account_type: AccountType) -> (Account, SecretKey) { | ||
| let mut rng = ChaCha20Rng::from_seed([7; 32]); | ||
| let secret_key = SecretKey::with_rng(&mut rng); | ||
| let auth = Approver::new(secret_key.public_key().into(), AuthScheme::Falcon512Poseidon2); | ||
| let init_seed: [u8; 32] = rng.random(); | ||
| let mut account = | ||
| create_basic_wallet(init_seed, auth, account_type).expect("wallet should build"); | ||
| account.set_nonce(ONE).expect("nonce should be settable"); | ||
| (account, secret_key) | ||
| } | ||
|
|
||
| fn write_account_file( | ||
| dir: &Path, | ||
| account: &Account, | ||
| keys: Vec<AuthSecretKey>, | ||
| ) -> std::path::PathBuf { | ||
| let path = dir.join("funding_service.mac"); | ||
| AccountFile::new(account.clone(), keys) | ||
| .write(&path) | ||
| .expect("file should be written"); | ||
| path | ||
| } | ||
|
|
||
| #[test] | ||
| fn loads_a_public_wallet_with_its_key() { | ||
| let dir = tempfile::tempdir().unwrap(); | ||
| let (account, secret_key) = genesis_wallet(AccountType::Public); | ||
| let path = write_account_file( | ||
| dir.path(), | ||
| &account, | ||
| vec![AuthSecretKey::Falcon512Poseidon2(secret_key)], | ||
| ); | ||
|
|
||
| let funder = FunderKey::load(&path).expect("a public wallet with a key should load"); | ||
|
|
||
| assert_eq!(funder.account_id(), account.id()); | ||
| } | ||
|
|
||
| /// The service reads the funder's vault from the node, which is only possible for a public | ||
| /// account. | ||
| #[test] | ||
| fn rejects_a_private_account() { | ||
| let dir = tempfile::tempdir().unwrap(); | ||
| let (account, secret_key) = genesis_wallet(AccountType::Private); | ||
| let path = write_account_file( | ||
| dir.path(), | ||
| &account, | ||
| vec![AuthSecretKey::Falcon512Poseidon2(secret_key)], | ||
| ); | ||
|
|
||
| let err = FunderKey::load(&path).expect_err("a private account must be rejected"); | ||
| assert!(err.to_string().contains("is not public"), "unexpected error: {err}"); | ||
| } | ||
| } | ||
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.
Perhaps a question whether we should be doing this or not 🤔 I guess the value owned would be small..
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.
One thing about limiting the service to public accounts is that it simplifies operations. The service does not even need to keep track of the account itself, just the private key. So I would say if the service is limited to public accounts only, scrap any functionality that tries to keep and validate the local state of the account versus the state provided by the network, and just rely on the latter. Only if the service supports private accounts should you take care of keeping the local state valid.
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.
It does simplify things, also means it easy to check if its out of funds via the explorer.