From 72d7fdb47a5723b88b3149e2c3c5d180b8b89ede Mon Sep 17 00:00:00 2001 From: 0xZensh Date: Sun, 14 Jan 2024 18:17:26 +0800 Subject: [PATCH] feat: change a service field `approver` to `attesters` --- crates/ns-fetcher/Cargo.toml | 4 +-- crates/ns-fetcher/src/indexer.rs | 5 +-- crates/ns-indexer/Cargo.toml | 4 +-- crates/ns-indexer/src/envelope.rs | 4 +-- crates/ns-indexer/src/indexer.rs | 30 ++++++++-------- crates/ns-inscriber/Cargo.toml | 6 ++-- crates/ns-inscriber/src/bin/main.rs | 4 +-- crates/ns-inscriber/src/inscriber.rs | 4 +-- crates/ns-protocol/Cargo.toml | 2 +- crates/ns-protocol/src/ns.rs | 54 ++++++++++++++++++++-------- crates/ns-protocol/src/state.rs | 26 +++++++------- 11 files changed, 85 insertions(+), 58 deletions(-) diff --git a/crates/ns-fetcher/Cargo.toml b/crates/ns-fetcher/Cargo.toml index a539bcc..7c55437 100644 --- a/crates/ns-fetcher/Cargo.toml +++ b/crates/ns-fetcher/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ns-fetcher" -version = "0.2.0" +version = "0.2.1" edition = "2021" rust-version = "1.64" description = "Fetch and validate inscriptions from ns-indexer service" @@ -11,7 +11,7 @@ license = "CC0-1.0" [lib] [dependencies] -ns-protocol = { path = "../ns-protocol", version = "0.7" } +ns-protocol = { path = "../ns-protocol", version = "0.8" } anyhow = { workspace = true } bytes = { workspace = true } base64 = { workspace = true } diff --git a/crates/ns-fetcher/src/indexer.rs b/crates/ns-fetcher/src/indexer.rs index 7e2c3d6..09d074d 100644 --- a/crates/ns-fetcher/src/indexer.rs +++ b/crates/ns-fetcher/src/indexer.rs @@ -102,7 +102,7 @@ impl Client { sleep(Duration::from_secs(retry_secs)).await; continue; } else { - anyhow::bail!("Client: {}", err.to_string()); + anyhow::bail!("request({}): {}", url, err.to_string()); } } } @@ -111,7 +111,8 @@ impl Client { let data = res.bytes().await?; let output: Response = from_reader(&data[..]).map_err(|err| { anyhow::anyhow!( - "Client: failed to parse response, {}, data: {}", + "request({}): failed to parse response, {}, data: {}", + url, err.to_string(), String::from_utf8_lossy(&data) ) diff --git a/crates/ns-indexer/Cargo.toml b/crates/ns-indexer/Cargo.toml index 0426847..0df1c64 100644 --- a/crates/ns-indexer/Cargo.toml +++ b/crates/ns-indexer/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ns-indexer" -version = "0.5.0" +version = "0.6.0" edition = "2021" rust-version = "1.64" description = "Name & Service Protocol indexer service in Rust" @@ -15,7 +15,7 @@ name = "ns-indexer" path = "src/bin/main.rs" [dependencies] -ns-protocol = { path = "../ns-protocol", version = "0.7" } +ns-protocol = { path = "../ns-protocol", version = "0.8" } ns-axum-web = { path = "../ns-axum-web", version = "0.1" } ns-scylla-orm = { path = "../ns-scylla-orm", version = "0.1" } ns-scylla-orm-macros = { path = "../ns-scylla-orm-macros", version = "0.1" } diff --git a/crates/ns-indexer/src/envelope.rs b/crates/ns-indexer/src/envelope.rs index d58aad2..2085a76 100644 --- a/crates/ns-indexer/src/envelope.rs +++ b/crates/ns-indexer/src/envelope.rs @@ -108,7 +108,7 @@ mod tests { subcode: 1, params: Value::from(¶ms), }], - approver: None, + attesters: None, }, signatures: vec![], }; @@ -126,7 +126,7 @@ mod tests { subcode: 1, params: Value::from(¶ms), }], - approver: None, + attesters: None, }, signatures: vec![], }; diff --git a/crates/ns-indexer/src/indexer.rs b/crates/ns-indexer/src/indexer.rs index 85d709e..2707c58 100644 --- a/crates/ns-indexer/src/indexer.rs +++ b/crates/ns-indexer/src/indexer.rs @@ -289,20 +289,22 @@ impl Indexer { }; service_protocol.validate(&name.service)?; - if let Some(ref approver) = name.service.approver { - let mut approver_state = db::NameState::with_pk(approver.clone()); - approver_state - .get_one(&self.scylla, vec![]) - .await - .map_err(|err| { - anyhow::anyhow!( - "failed to get approver state, name: {}, err: {}", - approver, - err - ) - })?; - let approver_state = approver_state.to_index()?; - name.verify(&approver_state.public_key_params(), ThresholdLevel::Default)?; + if let Some(attesters) = &name.service.attesters { + for attester in attesters { + let mut attester_state = db::NameState::with_pk(attester.clone()); + attester_state + .get_one(&self.scylla, vec![]) + .await + .map_err(|err| { + anyhow::anyhow!( + "failed to get attester state, name: {}, err: {}", + attester, + err + ) + })?; + let attester_state = attester_state.to_index()?; + name.verify(&attester_state.public_key_params(), ThresholdLevel::Default)?; + } } // pre-check state cache in read lock diff --git a/crates/ns-inscriber/Cargo.toml b/crates/ns-inscriber/Cargo.toml index f88a1db..abc7aed 100644 --- a/crates/ns-inscriber/Cargo.toml +++ b/crates/ns-inscriber/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ns-inscriber" -version = "0.4.0" +version = "0.5.0" edition = "2021" rust-version = "1.64" description = "Name & Service Protocol inscriber service in Rust" @@ -15,8 +15,8 @@ name = "ns-inscriber" path = "src/bin/main.rs" [dependencies] -ns-protocol = { path = "../ns-protocol", version = "0.7" } -ns-indexer = { path = "../ns-indexer", version = "0.5" } +ns-protocol = { path = "../ns-protocol", version = "0.8" } +ns-indexer = { path = "../ns-indexer", version = "0.6" } anyhow = { workspace = true } bytes = { workspace = true } base64 = { workspace = true } diff --git a/crates/ns-inscriber/src/bin/main.rs b/crates/ns-inscriber/src/bin/main.rs index 104fdc0..4dfc18a 100644 --- a/crates/ns-inscriber/src/bin/main.rs +++ b/crates/ns-inscriber/src/bin/main.rs @@ -523,7 +523,7 @@ async fn main() -> anyhow::Result<()> { subcode: 1, params: Value::from(¶ms), }], - approver: None, + attesters: None, }, signatures: vec![], }; @@ -600,7 +600,7 @@ async fn main() -> anyhow::Result<()> { subcode: 1, params: Value::from(¶ms), }], - approver: None, + attesters: None, }, signatures: vec![], }; diff --git a/crates/ns-inscriber/src/inscriber.rs b/crates/ns-inscriber/src/inscriber.rs index f359257..d226219 100644 --- a/crates/ns-inscriber/src/inscriber.rs +++ b/crates/ns-inscriber/src/inscriber.rs @@ -386,7 +386,7 @@ impl Inscriber { Ok(txid) } - // return (to_spent_tx_out, unsigned_commit_tx, signed_reveal_tx) + // return (unsigned_commit_tx, signed_reveal_tx) pub async fn build_inscription_transactions( &self, names: &Vec, @@ -738,7 +738,7 @@ mod tests { subcode: 1, params: Value::from(¶ms), }], - approver: None, + attesters: None, }, signatures: vec![], }; diff --git a/crates/ns-protocol/Cargo.toml b/crates/ns-protocol/Cargo.toml index e0ba7d0..951c893 100644 --- a/crates/ns-protocol/Cargo.toml +++ b/crates/ns-protocol/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ns-protocol" -version = "0.7.0" +version = "0.8.0" edition = "2021" rust-version = "1.64" description = "Name & Service Protocol in Rust" diff --git a/crates/ns-protocol/src/ns.rs b/crates/ns-protocol/src/ns.rs index d7fe330..e613481 100644 --- a/crates/ns-protocol/src/ns.rs +++ b/crates/ns-protocol/src/ns.rs @@ -27,7 +27,7 @@ pub struct Name { pub struct Service { pub code: u64, pub operations: Vec, - pub approver: Option, // approver's name + pub attesters: Option>, // attester's name } #[derive(Clone, PartialEq, Debug)] @@ -443,12 +443,17 @@ impl Name { ))); } - if let Some(approver) = &self.service.approver { - if !valid_name(approver) { - return Err(Error::Custom(format!( - "Name: invalid approver {}", - approver - ))); + if let Some(attesters) = &self.service.attesters { + if attesters.is_empty() { + return Err(Error::Custom("Name: empty attesters".to_string())); + } + for attester in attesters { + if !valid_name(attester) { + return Err(Error::Custom(format!( + "Name: invalid attester {}", + attester + ))); + } } } @@ -596,8 +601,10 @@ impl From<&Service> for Value { service.code.into(), Value::Array(service.operations.iter().map(Value::from).collect()), ]; - if let Some(ref approver) = service.approver { - arr.push(Value::Text(approver.clone())); + if let Some(attesters) = &service.attesters { + arr.push(Value::Array( + attesters.clone().into_iter().map(Value::from).collect(), + )); } Value::Array(arr) } @@ -691,16 +698,33 @@ impl TryFrom<&Value> for Service { .iter() .map(Operation::try_from) .collect::, Self::Error>>()?, - approver: None, + attesters: None, }; if v == 3 { - let approver = arr[2].as_text().ok_or_else(|| { + let attesters = arr[2].as_array().ok_or_else(|| { Error::Custom(format!( - "Service: expected text, got {}", + "Service: expected array, got {}", kind_of_value(&arr[2]) )) })?; - srv.approver = Some(approver.to_string()); + if attesters.is_empty() { + return Err(Error::Custom( + "Service: expected non-empty array of attesters".to_string(), + )); + } + + let attesters: Result, Error> = attesters + .iter() + .map(|v| { + v.as_text().map(String::from).ok_or_else(|| { + Error::Custom(format!( + "Name: expected text, got {}", + kind_of_value(&arr[0]) + )) + }) + }) + .collect(); + srv.attesters = Some(attesters?); } Ok(srv) } @@ -992,7 +1016,7 @@ mod tests { subcode: 1, params: Value::from(¶ms), }], - approver: None, + attesters: None, }, signatures: vec![], }; @@ -1054,7 +1078,7 @@ mod tests { subcode: 1, params: Value::from(¶ms), }], - approver: None, + attesters: None, }, signatures: vec![], }; diff --git a/crates/ns-protocol/src/state.rs b/crates/ns-protocol/src/state.rs index 7e62674..d605468 100644 --- a/crates/ns-protocol/src/state.rs +++ b/crates/ns-protocol/src/state.rs @@ -702,7 +702,7 @@ mod tests { kind: None, }), }], - approver: None, + attesters: None, }, signatures: vec![], }; @@ -736,7 +736,7 @@ mod tests { kind: None, }), }], - approver: None, + attesters: None, }, signatures: vec![], }; @@ -785,7 +785,7 @@ mod tests { kind: None, }), }], - approver: None, + attesters: None, }, signatures: vec![], }; @@ -860,7 +860,7 @@ mod tests { }), }, ], - approver: None, + attesters: None, }, signatures: vec![], }; @@ -911,7 +911,7 @@ mod tests { kind: None, }), }], - approver: None, + attesters: None, }, signatures: vec![], }; @@ -955,7 +955,7 @@ mod tests { kind: None, }), }], - approver: None, + attesters: None, }, signatures: vec![], }; @@ -982,7 +982,7 @@ mod tests { subcode: 0, params: ns::Value::Null, }], - approver: None, + attesters: None, }, signatures: vec![], }; @@ -1024,7 +1024,7 @@ mod tests { kind: None, }), }], - approver: None, + attesters: None, }, signatures: vec![], }; @@ -1051,7 +1051,7 @@ mod tests { subcode: 0, params: ns::Value::Null, }], - approver: None, + attesters: None, }, signatures: vec![], }; @@ -1100,7 +1100,7 @@ mod tests { subcode: 0, params: ns::Value::Null, }], - approver: None, + attesters: None, }, signatures: vec![], }; @@ -1120,7 +1120,7 @@ mod tests { subcode: 0, params: ns::Value::Text("hello".to_string()), }], - approver: None, + attesters: None, }, signatures: vec![], }; @@ -1143,7 +1143,7 @@ mod tests { subcode: 3, params: ns::Value::Null, }], - approver: None, + attesters: None, }, signatures: vec![], }; @@ -1168,7 +1168,7 @@ mod tests { subcode: 2, params: ns::Value::Text("hello2".to_string()), }], - approver: None, + attesters: None, }, signatures: vec![], };