-
Notifications
You must be signed in to change notification settings - Fork 1
add proof-of-work for token acquisition #77
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
base: v0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| use crate::cmd::{parse_response, ApiError, ApiErrorKind, Cmd, ETagCmd, ProtocolError}; | ||
| use crate::cmd::{ApiError, ApiErrorBody, ApiErrorKind, Cmd, ETagCmd, ProtocolError, parse_response}; | ||
| use crate::resolver_fallback::{GaiResolverWithFallback, NoResolverFallbackCache, ResolverFallbackCache}; | ||
| use crate::response::Response; | ||
| use crate::token::{AcquireToken, AcquireToken2Output}; | ||
| use crate::types::{AccountId, AuthToken}; | ||
| use anyhow::{anyhow, Context}; | ||
| use anyhow::{Context, anyhow}; | ||
| use http::HeaderValue; | ||
| use itertools::Itertools; | ||
| use reqwest::ClientBuilder; | ||
|
|
@@ -128,9 +128,31 @@ impl Client { | |
| }); | ||
| } | ||
| let account_id = self.account_id.clone(); | ||
| let request = AcquireToken { account_id }.to_request2(&self.base_url)?; | ||
| let res = self.send_http(request).await?; | ||
| let res = parse_response::<AcquireToken2Output>(res).await?; | ||
| let mut pow = None; | ||
| let res = loop { | ||
| let request = AcquireToken { | ||
| account_id: account_id.clone(), | ||
| pow: pow.clone(), | ||
| } | ||
| .to_request2(&self.base_url)?; | ||
| let res = self.send_http(request).await?; | ||
| let res = parse_response::<AcquireToken2Output>(res).await; | ||
| if let Err(ClientError::ApiError(ApiError { | ||
| status: _, | ||
| body: | ||
| ApiErrorBody { | ||
| error: ApiErrorKind::RateLimitExceeded { pow_input: Some(pow_input) }, | ||
| msg: _, | ||
| detail: _, | ||
| }, | ||
| })) = &res | ||
| && pow.is_none() | ||
| { | ||
| pow = Some(pow_input.solve(&account_id)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the hard part of this is that we should be giving client feedback. But we can add that later. There are basically two cases:
|
||
| continue; | ||
| } | ||
| break res; | ||
| }?; | ||
| let body = res.into_body().context("No auth token in response")?; | ||
| self.set_auth_token(Some(body.auth_token.clone().into())); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| use crate::types::AccountId; | ||
| use serde::{Deserialize, Serialize}; | ||
| use sha2::{Digest, Sha256}; | ||
|
|
||
| #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] | ||
| pub struct PowInput { | ||
| pub nonce: u128, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. decided to use string |
||
| pub difficulty: u8, | ||
| pub puzzles: u16, | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Deserialize, Serialize)] | ||
| pub struct PowOutput { | ||
| pub nonce: u128, | ||
| pub solutions: Vec<u128>, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. decided to use strings |
||
| } | ||
|
|
||
| impl PowInput { | ||
| pub fn solve(&self, account_id: &AccountId) -> PowOutput { | ||
| let account_id = account_id.to_string(); | ||
| let mut solutions = Vec::with_capacity(self.puzzles.into()); | ||
| for puzzle in 0..self.puzzles { | ||
| let mut candidate = 0u128; | ||
| loop { | ||
| let mut hasher = Sha256::new(); | ||
| hasher.update(self.nonce.to_be_bytes()); | ||
| hasher.update(&account_id); | ||
| hasher.update(candidate.to_be_bytes()); | ||
| hasher.update(puzzle.to_be_bytes()); | ||
| let hash = hasher.finalize(); | ||
| if is_solution(self.difficulty, hash.as_ref()) { | ||
| solutions.push(candidate); | ||
| break; | ||
| } | ||
| candidate = candidate.wrapping_add(1); | ||
| } | ||
| } | ||
| PowOutput { | ||
| solutions, | ||
| nonce: self.nonce, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn is_solution(difficulty: u8, hash: &[u8; 32]) -> bool { | ||
| let mut difficulty: u32 = difficulty.into(); | ||
| for chunk in hash.as_chunks::<8>().0 { | ||
| let chunk = u64::from_be_bytes(*chunk); | ||
| let chunk_zeros = chunk.leading_zeros(); | ||
| if chunk_zeros >= difficulty { | ||
| return true; | ||
| } | ||
| if chunk_zeros < 64 { | ||
| return false; | ||
| } | ||
| difficulty -= 64; | ||
| } | ||
| true | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| use crate::pow::PowOutput; | ||
| use crate::types::AccountId; | ||
| use serde::{Deserialize, Serialize}; | ||
| use url::Url; | ||
|
|
||
| use crate::types::AccountId; | ||
|
|
||
| #[derive(Clone, Debug, Deserialize, Serialize)] | ||
| pub struct UrlOverride { | ||
| pub api: String, | ||
|
|
@@ -18,6 +18,7 @@ pub struct AcquireToken2Output { | |
| #[derive(Debug, Serialize, Deserialize, Clone)] | ||
| pub struct AcquireToken { | ||
| pub account_id: AccountId, | ||
| pub pow: Option<PowOutput>, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Expand this or at least add a doc comment. |
||
| } | ||
|
|
||
| impl AcquireToken { | ||
|
|
||
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.
Needs to terminate.