Skip to content

Commit

Permalink
feat: change a service field approver to attesters
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jan 14, 2024
1 parent abe3d29 commit 72d7fdb
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 58 deletions.
4 changes: 2 additions & 2 deletions crates/ns-fetcher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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 }
Expand Down
5 changes: 3 additions & 2 deletions crates/ns-fetcher/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand All @@ -111,7 +111,8 @@ impl Client {
let data = res.bytes().await?;
let output: Response<T> = 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)
)
Expand Down
4 changes: 2 additions & 2 deletions crates/ns-indexer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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" }
Expand Down
4 changes: 2 additions & 2 deletions crates/ns-indexer/src/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ mod tests {
subcode: 1,
params: Value::from(&params),
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand All @@ -126,7 +126,7 @@ mod tests {
subcode: 1,
params: Value::from(&params),
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand Down
30 changes: 16 additions & 14 deletions crates/ns-indexer/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions crates/ns-inscriber/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions crates/ns-inscriber/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ async fn main() -> anyhow::Result<()> {
subcode: 1,
params: Value::from(&params),
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand Down Expand Up @@ -600,7 +600,7 @@ async fn main() -> anyhow::Result<()> {
subcode: 1,
params: Value::from(&params),
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand Down
4 changes: 2 additions & 2 deletions crates/ns-inscriber/src/inscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Name>,
Expand Down Expand Up @@ -738,7 +738,7 @@ mod tests {
subcode: 1,
params: Value::from(&params),
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand Down
2 changes: 1 addition & 1 deletion crates/ns-protocol/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
54 changes: 39 additions & 15 deletions crates/ns-protocol/src/ns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Name {
pub struct Service {
pub code: u64,
pub operations: Vec<Operation>,
pub approver: Option<String>, // approver's name
pub attesters: Option<Vec<String>>, // attester's name
}

#[derive(Clone, PartialEq, Debug)]
Expand Down Expand Up @@ -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
)));
}
}
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -691,16 +698,33 @@ impl TryFrom<&Value> for Service {
.iter()
.map(Operation::try_from)
.collect::<Result<Vec<Operation>, 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<Vec<String>, 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)
}
Expand Down Expand Up @@ -992,7 +1016,7 @@ mod tests {
subcode: 1,
params: Value::from(&params),
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand Down Expand Up @@ -1054,7 +1078,7 @@ mod tests {
subcode: 1,
params: Value::from(&params),
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand Down
26 changes: 13 additions & 13 deletions crates/ns-protocol/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ mod tests {
kind: None,
}),
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand Down Expand Up @@ -736,7 +736,7 @@ mod tests {
kind: None,
}),
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand Down Expand Up @@ -785,7 +785,7 @@ mod tests {
kind: None,
}),
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand Down Expand Up @@ -860,7 +860,7 @@ mod tests {
}),
},
],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand Down Expand Up @@ -911,7 +911,7 @@ mod tests {
kind: None,
}),
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand Down Expand Up @@ -955,7 +955,7 @@ mod tests {
kind: None,
}),
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand All @@ -982,7 +982,7 @@ mod tests {
subcode: 0,
params: ns::Value::Null,
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand Down Expand Up @@ -1024,7 +1024,7 @@ mod tests {
kind: None,
}),
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand All @@ -1051,7 +1051,7 @@ mod tests {
subcode: 0,
params: ns::Value::Null,
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand Down Expand Up @@ -1100,7 +1100,7 @@ mod tests {
subcode: 0,
params: ns::Value::Null,
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand All @@ -1120,7 +1120,7 @@ mod tests {
subcode: 0,
params: ns::Value::Text("hello".to_string()),
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand All @@ -1143,7 +1143,7 @@ mod tests {
subcode: 3,
params: ns::Value::Null,
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand All @@ -1168,7 +1168,7 @@ mod tests {
subcode: 2,
params: ns::Value::Text("hello2".to_string()),
}],
approver: None,
attesters: None,
},
signatures: vec![],
};
Expand Down

0 comments on commit 72d7fdb

Please sign in to comment.