removing dev branch, many changes

This commit is contained in:
John Smith
2023-05-29 19:24:57 +00:00
parent 1430f3f656
commit 0a890c8707
250 changed files with 18084 additions and 8040 deletions
+17 -2
View File
@@ -19,11 +19,12 @@ mod signature512;
mod signed_direct_node_info;
mod signed_node_info;
mod signed_relayed_node_info;
mod signed_value_data;
mod signed_value_descriptor;
mod socket_address;
mod tunnel;
mod typed_key;
mod typed_signature;
mod value_data;
pub use address::*;
pub use address_type_set::*;
@@ -46,10 +47,24 @@ pub use signature512::*;
pub use signed_direct_node_info::*;
pub use signed_node_info::*;
pub use signed_relayed_node_info::*;
pub use signed_value_data::*;
pub use signed_value_descriptor::*;
pub use socket_address::*;
pub use tunnel::*;
pub use typed_key::*;
pub use typed_signature::*;
pub use value_data::*;
use super::*;
#[derive(Debug, Clone)]
pub enum QuestionContext {
GetValue(ValidateGetValueContext),
SetValue(ValidateSetValueContext),
}
#[derive(Clone)]
pub struct RPCValidateContext {
pub crypto: Crypto,
pub rpc_processor: RPCProcessor,
pub question_context: Option<QuestionContext>,
}
@@ -4,27 +4,27 @@ pub fn encode_node_info(
node_info: &NodeInfo,
builder: &mut veilid_capnp::node_info::Builder,
) -> Result<(), RPCError> {
builder.set_network_class(encode_network_class(node_info.network_class));
builder.set_network_class(encode_network_class(node_info.network_class()));
let mut ps_builder = builder.reborrow().init_outbound_protocols();
encode_protocol_type_set(&node_info.outbound_protocols, &mut ps_builder)?;
encode_protocol_type_set(&node_info.outbound_protocols(), &mut ps_builder)?;
let mut ats_builder = builder.reborrow().init_address_types();
encode_address_type_set(&node_info.address_types, &mut ats_builder)?;
encode_address_type_set(&node_info.address_types(), &mut ats_builder)?;
let mut es_builder = builder
.reborrow()
.init_envelope_support(node_info.envelope_support.len() as u32);
.init_envelope_support(node_info.envelope_support().len() as u32);
if let Some(s) = es_builder.as_slice() {
s.clone_from_slice(&node_info.envelope_support);
s.clone_from_slice(&node_info.envelope_support());
}
let mut cs_builder = builder
.reborrow()
.init_crypto_support(node_info.crypto_support.len() as u32);
.init_crypto_support(node_info.crypto_support().len() as u32);
if let Some(s) = cs_builder.as_slice() {
let csvec: Vec<u32> = node_info
.crypto_support
.crypto_support()
.iter()
.map(|x| u32::from_be_bytes(x.0))
.collect();
@@ -33,7 +33,7 @@ pub fn encode_node_info(
let mut didl_builder = builder.reborrow().init_dial_info_detail_list(
node_info
.dial_info_detail_list
.dial_info_detail_list()
.len()
.try_into()
.map_err(RPCError::map_protocol(
@@ -41,9 +41,9 @@ pub fn encode_node_info(
))?,
);
for idx in 0..node_info.dial_info_detail_list.len() {
for idx in 0..node_info.dial_info_detail_list().len() {
let mut did_builder = didl_builder.reborrow().get(idx as u32);
encode_dial_info_detail(&node_info.dial_info_detail_list[idx], &mut did_builder)?;
encode_dial_info_detail(&node_info.dial_info_detail_list()[idx], &mut did_builder)?;
}
Ok(())
@@ -131,12 +131,12 @@ pub fn decode_node_info(reader: &veilid_capnp::node_info::Reader) -> Result<Node
dial_info_detail_list.push(decode_dial_info_detail(&did)?)
}
Ok(NodeInfo {
Ok(NodeInfo::new(
network_class,
outbound_protocols,
address_types,
envelope_support,
crypto_support,
dial_info_detail_list,
})
))
}
@@ -9,18 +9,18 @@ impl RPCAnswer {
pub fn new(detail: RPCAnswerDetail) -> Self {
Self { detail }
}
pub fn into_detail(self) -> RPCAnswerDetail {
self.detail
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
self.detail.validate(validate_context)
}
pub fn desc(&self) -> &'static str {
self.detail.desc()
}
pub fn decode(
reader: &veilid_capnp::answer::Reader,
crypto: Crypto,
) -> Result<RPCAnswer, RPCError> {
pub fn destructure(self) -> RPCAnswerDetail {
self.detail
}
pub fn decode(reader: &veilid_capnp::answer::Reader) -> Result<RPCAnswer, RPCError> {
let d_reader = reader.get_detail();
let detail = RPCAnswerDetail::decode(&d_reader, crypto)?;
let detail = RPCAnswerDetail::decode(&d_reader)?;
Ok(RPCAnswer { detail })
}
pub fn encode(&self, builder: &mut veilid_capnp::answer::Builder) -> Result<(), RPCError> {
@@ -60,10 +60,23 @@ impl RPCAnswerDetail {
RPCAnswerDetail::CancelTunnelA(_) => "CancelTunnelA",
}
}
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
match self {
RPCAnswerDetail::StatusA(r) => r.validate(validate_context),
RPCAnswerDetail::FindNodeA(r) => r.validate(validate_context),
RPCAnswerDetail::AppCallA(r) => r.validate(validate_context),
RPCAnswerDetail::GetValueA(r) => r.validate(validate_context),
RPCAnswerDetail::SetValueA(r) => r.validate(validate_context),
RPCAnswerDetail::WatchValueA(r) => r.validate(validate_context),
RPCAnswerDetail::SupplyBlockA(r) => r.validate(validate_context),
RPCAnswerDetail::FindBlockA(r) => r.validate(validate_context),
RPCAnswerDetail::StartTunnelA(r) => r.validate(validate_context),
RPCAnswerDetail::CompleteTunnelA(r) => r.validate(validate_context),
RPCAnswerDetail::CancelTunnelA(r) => r.validate(validate_context),
}
}
pub fn decode(
reader: &veilid_capnp::answer::detail::Reader,
crypto: Crypto,
) -> Result<RPCAnswerDetail, RPCError> {
let which_reader = reader.which().map_err(RPCError::protocol)?;
let out = match which_reader {
@@ -74,7 +87,7 @@ impl RPCAnswerDetail {
}
veilid_capnp::answer::detail::FindNodeA(r) => {
let op_reader = r.map_err(RPCError::protocol)?;
let out = RPCOperationFindNodeA::decode(&op_reader, crypto)?;
let out = RPCOperationFindNodeA::decode(&op_reader)?;
RPCAnswerDetail::FindNodeA(out)
}
veilid_capnp::answer::detail::AppCallA(r) => {
@@ -84,27 +97,27 @@ impl RPCAnswerDetail {
}
veilid_capnp::answer::detail::GetValueA(r) => {
let op_reader = r.map_err(RPCError::protocol)?;
let out = RPCOperationGetValueA::decode(&op_reader, crypto)?;
let out = RPCOperationGetValueA::decode(&op_reader)?;
RPCAnswerDetail::GetValueA(out)
}
veilid_capnp::answer::detail::SetValueA(r) => {
let op_reader = r.map_err(RPCError::protocol)?;
let out = RPCOperationSetValueA::decode(&op_reader, crypto)?;
let out = RPCOperationSetValueA::decode(&op_reader)?;
RPCAnswerDetail::SetValueA(out)
}
veilid_capnp::answer::detail::WatchValueA(r) => {
let op_reader = r.map_err(RPCError::protocol)?;
let out = RPCOperationWatchValueA::decode(&op_reader, crypto)?;
let out = RPCOperationWatchValueA::decode(&op_reader)?;
RPCAnswerDetail::WatchValueA(out)
}
veilid_capnp::answer::detail::SupplyBlockA(r) => {
let op_reader = r.map_err(RPCError::protocol)?;
let out = RPCOperationSupplyBlockA::decode(&op_reader, crypto)?;
let out = RPCOperationSupplyBlockA::decode(&op_reader)?;
RPCAnswerDetail::SupplyBlockA(out)
}
veilid_capnp::answer::detail::FindBlockA(r) => {
let op_reader = r.map_err(RPCError::protocol)?;
let out = RPCOperationFindBlockA::decode(&op_reader, crypto)?;
let out = RPCOperationFindBlockA::decode(&op_reader)?;
RPCAnswerDetail::FindBlockA(out)
}
veilid_capnp::answer::detail::StartTunnelA(r) => {
@@ -16,25 +16,30 @@ impl RPCOperationKind {
}
}
pub fn decode(
kind_reader: &veilid_capnp::operation::kind::Reader,
crypto: Crypto,
) -> Result<Self, RPCError> {
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
match self {
RPCOperationKind::Question(r) => r.validate(validate_context),
RPCOperationKind::Statement(r) => r.validate(validate_context),
RPCOperationKind::Answer(r) => r.validate(validate_context),
}
}
pub fn decode(kind_reader: &veilid_capnp::operation::kind::Reader) -> Result<Self, RPCError> {
let which_reader = kind_reader.which().map_err(RPCError::protocol)?;
let out = match which_reader {
veilid_capnp::operation::kind::Which::Question(r) => {
let q_reader = r.map_err(RPCError::protocol)?;
let out = RPCQuestion::decode(&q_reader, crypto)?;
let out = RPCQuestion::decode(&q_reader)?;
RPCOperationKind::Question(out)
}
veilid_capnp::operation::kind::Which::Statement(r) => {
let q_reader = r.map_err(RPCError::protocol)?;
let out = RPCStatement::decode(&q_reader, crypto)?;
let out = RPCStatement::decode(&q_reader)?;
RPCOperationKind::Statement(out)
}
veilid_capnp::operation::kind::Which::Answer(r) => {
let q_reader = r.map_err(RPCError::protocol)?;
let out = RPCAnswer::decode(&q_reader, crypto)?;
let out = RPCAnswer::decode(&q_reader)?;
RPCOperationKind::Answer(out)
}
};
@@ -93,6 +98,17 @@ impl RPCOperation {
}
}
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
// Validate sender peer info
if let Some(sender_peer_info) = &self.opt_sender_peer_info {
sender_peer_info
.validate(validate_context.crypto.clone())
.map_err(RPCError::protocol)?;
}
// Validate operation kind
self.kind.validate(validate_context)
}
pub fn op_id(&self) -> OperationId {
self.op_id
}
@@ -108,21 +124,23 @@ impl RPCOperation {
&self.kind
}
pub fn into_kind(self) -> RPCOperationKind {
self.kind
pub fn destructure(self) -> (OperationId, Option<PeerInfo>, Timestamp, RPCOperationKind) {
(
self.op_id,
self.opt_sender_peer_info,
self.target_node_info_ts,
self.kind,
)
}
pub fn decode(
operation_reader: &veilid_capnp::operation::Reader,
crypto: Crypto,
) -> Result<Self, RPCError> {
pub fn decode(operation_reader: &veilid_capnp::operation::Reader) -> Result<Self, RPCError> {
let op_id = OperationId::new(operation_reader.get_op_id());
let sender_peer_info = if operation_reader.has_sender_peer_info() {
let pi_reader = operation_reader
.get_sender_peer_info()
.map_err(RPCError::protocol)?;
let pi = decode_peer_info(&pi_reader, crypto.clone())?;
let pi = decode_peer_info(&pi_reader)?;
Some(pi)
} else {
None
@@ -131,7 +149,7 @@ impl RPCOperation {
let target_node_info_ts = Timestamp::new(operation_reader.get_target_node_info_ts());
let kind_reader = operation_reader.get_kind();
let kind = RPCOperationKind::decode(&kind_reader, crypto)?;
let kind = RPCOperationKind::decode(&kind_reader)?;
Ok(RPCOperation {
op_id,
@@ -1,16 +1,40 @@
use super::*;
const MAX_APP_CALL_Q_MESSAGE_LEN: usize = 32768;
const MAX_APP_CALL_A_MESSAGE_LEN: usize = 32768;
#[derive(Debug, Clone)]
pub struct RPCOperationAppCallQ {
pub message: Vec<u8>,
message: Vec<u8>,
}
impl RPCOperationAppCallQ {
pub fn decode(
reader: &veilid_capnp::operation_app_call_q::Reader,
) -> Result<RPCOperationAppCallQ, RPCError> {
let message = reader.get_message().map_err(RPCError::protocol)?.to_vec();
Ok(RPCOperationAppCallQ { message })
pub fn new(message: Vec<u8>) -> Result<Self, RPCError> {
if message.len() > MAX_APP_CALL_Q_MESSAGE_LEN {
return Err(RPCError::protocol("AppCallQ message too long to set"));
}
Ok(Self { message })
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
// pub fn message(&self) -> &[u8] {
// &self.message
// }
pub fn destructure(self) -> Vec<u8> {
self.message
}
pub fn decode(reader: &veilid_capnp::operation_app_call_q::Reader) -> Result<Self, RPCError> {
let mr = reader.get_message().map_err(RPCError::protocol)?;
if mr.len() > MAX_APP_CALL_Q_MESSAGE_LEN {
return Err(RPCError::protocol("AppCallQ message too long to set"));
}
Ok(Self {
message: mr.to_vec(),
})
}
pub fn encode(
&self,
@@ -23,15 +47,37 @@ impl RPCOperationAppCallQ {
#[derive(Debug, Clone)]
pub struct RPCOperationAppCallA {
pub message: Vec<u8>,
message: Vec<u8>,
}
impl RPCOperationAppCallA {
pub fn decode(
reader: &veilid_capnp::operation_app_call_a::Reader,
) -> Result<RPCOperationAppCallA, RPCError> {
let message = reader.get_message().map_err(RPCError::protocol)?.to_vec();
Ok(RPCOperationAppCallA { message })
pub fn new(message: Vec<u8>) -> Result<Self, RPCError> {
if message.len() > MAX_APP_CALL_A_MESSAGE_LEN {
return Err(RPCError::protocol("AppCallA message too long to set"));
}
Ok(Self { message })
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
// pub fn message(&self) -> &[u8] {
// &self.message
// }
pub fn destructure(self) -> Vec<u8> {
self.message
}
pub fn decode(reader: &veilid_capnp::operation_app_call_a::Reader) -> Result<Self, RPCError> {
let mr = reader.get_message().map_err(RPCError::protocol)?;
if mr.len() > MAX_APP_CALL_A_MESSAGE_LEN {
return Err(RPCError::protocol("AppCallA message too long to set"));
}
Ok(Self {
message: mr.to_vec(),
})
}
pub fn encode(
&self,
@@ -1,16 +1,39 @@
use super::*;
const MAX_APP_MESSAGE_MESSAGE_LEN: usize = 32768;
#[derive(Debug, Clone)]
pub struct RPCOperationAppMessage {
pub message: Vec<u8>,
message: Vec<u8>,
}
impl RPCOperationAppMessage {
pub fn decode(
reader: &veilid_capnp::operation_app_message::Reader,
) -> Result<RPCOperationAppMessage, RPCError> {
let message = reader.get_message().map_err(RPCError::protocol)?.to_vec();
Ok(RPCOperationAppMessage { message })
pub fn new(message: Vec<u8>) -> Result<Self, RPCError> {
if message.len() > MAX_APP_MESSAGE_MESSAGE_LEN {
return Err(RPCError::protocol("AppMessage message too long to set"));
}
Ok(Self { message })
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
// pub fn message(&self) -> &[u8] {
// &self.message
// }
pub fn destructure(self) -> Vec<u8> {
self.message
}
pub fn decode(reader: &veilid_capnp::operation_app_message::Reader) -> Result<Self, RPCError> {
let mr = reader.get_message().map_err(RPCError::protocol)?;
if mr.len() > MAX_APP_MESSAGE_MESSAGE_LEN {
return Err(RPCError::protocol("AppMessage message too long to set"));
}
Ok(Self {
message: mr.to_vec(),
})
}
pub fn encode(
&self,
@@ -2,16 +2,30 @@ use super::*;
#[derive(Debug, Clone)]
pub struct RPCOperationCancelTunnelQ {
pub id: TunnelId,
id: TunnelId,
}
impl RPCOperationCancelTunnelQ {
pub fn new(id: TunnelId) -> Self {
Self { id }
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
pub fn id(&self) -> TunnelId {
self.id
}
pub fn destructure(self) -> TunnelId {
self.id
}
pub fn decode(
reader: &veilid_capnp::operation_cancel_tunnel_q::Reader,
) -> Result<RPCOperationCancelTunnelQ, RPCError> {
) -> Result<Self, RPCError> {
let id = TunnelId::new(reader.get_id());
Ok(RPCOperationCancelTunnelQ { id })
Ok(Self { id })
}
pub fn encode(
&self,
@@ -30,16 +44,25 @@ pub enum RPCOperationCancelTunnelA {
}
impl RPCOperationCancelTunnelA {
pub fn new_tunnel(id: TunnelId) -> Self {
Self::Tunnel(id)
}
pub fn new_error(error: TunnelError) -> Self {
Self::Error(error)
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
pub fn decode(
reader: &veilid_capnp::operation_cancel_tunnel_a::Reader,
) -> Result<RPCOperationCancelTunnelA, RPCError> {
) -> Result<Self, RPCError> {
match reader.which().map_err(RPCError::protocol)? {
veilid_capnp::operation_cancel_tunnel_a::Which::Tunnel(r) => {
Ok(RPCOperationCancelTunnelA::Tunnel(TunnelId::new(r)))
Ok(Self::Tunnel(TunnelId::new(r)))
}
veilid_capnp::operation_cancel_tunnel_a::Which::Error(r) => {
let tunnel_error = decode_tunnel_error(r.map_err(RPCError::protocol)?);
Ok(RPCOperationCancelTunnelA::Error(tunnel_error))
Ok(Self::Error(tunnel_error))
}
}
}
@@ -48,10 +71,10 @@ impl RPCOperationCancelTunnelA {
builder: &mut veilid_capnp::operation_cancel_tunnel_a::Builder,
) -> Result<(), RPCError> {
match self {
RPCOperationCancelTunnelA::Tunnel(p) => {
Self::Tunnel(p) => {
builder.set_tunnel(p.as_u64());
}
RPCOperationCancelTunnelA::Error(e) => {
Self::Error(e) => {
builder.set_error(encode_tunnel_error(*e));
}
}
@@ -2,16 +2,45 @@ use super::*;
#[derive(Debug, Clone)]
pub struct RPCOperationCompleteTunnelQ {
pub id: TunnelId,
pub local_mode: TunnelMode,
pub depth: u8,
pub endpoint: TunnelEndpoint,
id: TunnelId,
local_mode: TunnelMode,
depth: u8,
endpoint: TunnelEndpoint,
}
impl RPCOperationCompleteTunnelQ {
pub fn new(id: TunnelId, local_mode: TunnelMode, depth: u8, endpoint: TunnelEndpoint) -> Self {
Self {
id,
local_mode,
depth,
endpoint,
}
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
pub fn id(&self) -> TunnelId {
self.id
}
pub fn local_mode(&self) -> TunnelMode {
self.local_mode
}
pub fn depth(&self) -> u8 {
self.depth
}
pub fn endpoint(&self) -> &TunnelEndpoint {
&self.endpoint
}
pub fn destructure(self) -> (TunnelId, TunnelMode, u8, TunnelEndpoint) {
(self.id, self.local_mode, self.depth, self.endpoint)
}
pub fn decode(
reader: &veilid_capnp::operation_complete_tunnel_q::Reader,
) -> Result<RPCOperationCompleteTunnelQ, RPCError> {
) -> Result<Self, RPCError> {
let id = TunnelId::new(reader.get_id());
let local_mode = match reader.get_local_mode().map_err(RPCError::protocol)? {
veilid_capnp::TunnelEndpointMode::Raw => TunnelMode::Raw,
@@ -21,7 +50,7 @@ impl RPCOperationCompleteTunnelQ {
let te_reader = reader.get_endpoint().map_err(RPCError::protocol)?;
let endpoint = decode_tunnel_endpoint(&te_reader)?;
Ok(RPCOperationCompleteTunnelQ {
Ok(Self {
id,
local_mode,
depth,
@@ -52,18 +81,28 @@ pub enum RPCOperationCompleteTunnelA {
}
impl RPCOperationCompleteTunnelA {
pub fn new_tunnel(tunnel: FullTunnel) -> Self {
Self::Tunnel(tunnel)
}
pub fn new_error(error: TunnelError) -> Self {
Self::Error(error)
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
pub fn decode(
reader: &veilid_capnp::operation_complete_tunnel_a::Reader,
) -> Result<RPCOperationCompleteTunnelA, RPCError> {
) -> Result<Self, RPCError> {
match reader.which().map_err(RPCError::protocol)? {
veilid_capnp::operation_complete_tunnel_a::Which::Tunnel(r) => {
let ft_reader = r.map_err(RPCError::protocol)?;
let full_tunnel = decode_full_tunnel(&ft_reader)?;
Ok(RPCOperationCompleteTunnelA::Tunnel(full_tunnel))
Ok(Self::Tunnel(full_tunnel))
}
veilid_capnp::operation_complete_tunnel_a::Which::Error(r) => {
let tunnel_error = decode_tunnel_error(r.map_err(RPCError::protocol)?);
Ok(RPCOperationCompleteTunnelA::Error(tunnel_error))
Ok(Self::Error(tunnel_error))
}
}
}
@@ -72,10 +111,10 @@ impl RPCOperationCompleteTunnelA {
builder: &mut veilid_capnp::operation_complete_tunnel_a::Builder,
) -> Result<(), RPCError> {
match self {
RPCOperationCompleteTunnelA::Tunnel(p) => {
Self::Tunnel(p) => {
encode_full_tunnel(p, &mut builder.reborrow().init_tunnel())?;
}
RPCOperationCompleteTunnelA::Error(e) => {
Self::Error(e) => {
builder.set_error(encode_tunnel_error(*e));
}
}
@@ -1,18 +1,37 @@
use super::*;
const MAX_FIND_BLOCK_A_DATA_LEN: usize = 32768;
const MAX_FIND_BLOCK_A_SUPPLIERS_LEN: usize = 10;
const MAX_FIND_BLOCK_A_PEERS_LEN: usize = 10;
#[derive(Debug, Clone)]
pub struct RPCOperationFindBlockQ {
pub block_id: TypedKey,
block_id: TypedKey,
}
impl RPCOperationFindBlockQ {
pub fn new(block_id: TypedKey) -> Self {
Self { block_id }
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
pub fn block_id(&self) -> TypedKey {
self.block_id
}
pub fn destructure(self) -> TypedKey {
self.block_id
}
pub fn decode(
reader: &veilid_capnp::operation_find_block_q::Reader,
) -> Result<RPCOperationFindBlockQ, RPCError> {
let bi_reader = reader.get_block_id().map_err(RPCError::protocol)?;
let block_id = decode_typed_key(&bi_reader)?;
Ok(RPCOperationFindBlockQ { block_id })
Ok(Self { block_id })
}
pub fn encode(
&self,
@@ -27,19 +46,68 @@ impl RPCOperationFindBlockQ {
#[derive(Debug, Clone)]
pub struct RPCOperationFindBlockA {
pub data: Vec<u8>,
pub suppliers: Vec<PeerInfo>,
pub peers: Vec<PeerInfo>,
data: Vec<u8>,
suppliers: Vec<PeerInfo>,
peers: Vec<PeerInfo>,
}
impl RPCOperationFindBlockA {
pub fn decode(
reader: &veilid_capnp::operation_find_block_a::Reader,
crypto: Crypto,
) -> Result<RPCOperationFindBlockA, RPCError> {
let data = reader.get_data().map_err(RPCError::protocol)?.to_vec();
pub fn new(
data: Vec<u8>,
suppliers: Vec<PeerInfo>,
peers: Vec<PeerInfo>,
) -> Result<Self, RPCError> {
if data.len() > MAX_FIND_BLOCK_A_DATA_LEN {
return Err(RPCError::protocol("find block data length too long"));
}
if suppliers.len() > MAX_FIND_BLOCK_A_SUPPLIERS_LEN {
return Err(RPCError::protocol("find block suppliers length too long"));
}
if peers.len() > MAX_FIND_BLOCK_A_PEERS_LEN {
return Err(RPCError::protocol("find block peers length too long"));
}
Ok(Self {
data,
suppliers,
peers,
})
}
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
PeerInfo::validate_vec(&mut self.suppliers, validate_context.crypto.clone());
PeerInfo::validate_vec(&mut self.peers, validate_context.crypto.clone());
Ok(())
}
pub fn data(&self) -> &[u8] {
&self.data
}
pub fn suppliers(&self) -> &[PeerInfo] {
&self.suppliers
}
pub fn peers(&self) -> &[PeerInfo] {
&self.peers
}
pub fn destructure(self) -> (Vec<u8>, Vec<PeerInfo>, Vec<PeerInfo>) {
(self.data, self.suppliers, self.peers)
}
pub fn decode(reader: &veilid_capnp::operation_find_block_a::Reader) -> Result<Self, RPCError> {
let data = reader.get_data().map_err(RPCError::protocol)?;
if data.len() > MAX_FIND_BLOCK_A_DATA_LEN {
return Err(RPCError::protocol("find block data length too long"));
}
let suppliers_reader = reader.get_suppliers().map_err(RPCError::protocol)?;
if suppliers_reader.len() as usize > MAX_FIND_BLOCK_A_SUPPLIERS_LEN {
return Err(RPCError::protocol("find block suppliers length too long"));
}
let peers_reader = reader.get_peers().map_err(RPCError::protocol)?;
if peers_reader.len() as usize > MAX_FIND_BLOCK_A_PEERS_LEN {
return Err(RPCError::protocol("find block peers length too long"));
}
let mut suppliers = Vec::<PeerInfo>::with_capacity(
suppliers_reader
.len()
@@ -47,11 +115,10 @@ impl RPCOperationFindBlockA {
.map_err(RPCError::map_internal("too many suppliers"))?,
);
for s in suppliers_reader.iter() {
let peer_info = decode_peer_info(&s, crypto.clone())?;
let peer_info = decode_peer_info(&s)?;
suppliers.push(peer_info);
}
let peers_reader = reader.get_peers().map_err(RPCError::protocol)?;
let mut peers = Vec::<PeerInfo>::with_capacity(
peers_reader
.len()
@@ -59,12 +126,12 @@ impl RPCOperationFindBlockA {
.map_err(RPCError::map_internal("too many peers"))?,
);
for p in peers_reader.iter() {
let peer_info = decode_peer_info(&p, crypto.clone())?;
let peer_info = decode_peer_info(&p)?;
peers.push(peer_info);
}
Ok(RPCOperationFindBlockA {
data,
Ok(Self {
data: data.to_vec(),
suppliers,
peers,
})
@@ -1,17 +1,32 @@
use super::*;
const MAX_FIND_NODE_A_PEERS_LEN: usize = 20;
#[derive(Debug, Clone)]
pub struct RPCOperationFindNodeQ {
pub node_id: TypedKey,
node_id: TypedKey,
}
impl RPCOperationFindNodeQ {
pub fn decode(
reader: &veilid_capnp::operation_find_node_q::Reader,
) -> Result<RPCOperationFindNodeQ, RPCError> {
pub fn new(node_id: TypedKey) -> Self {
Self { node_id }
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
// pub fn node_id(&self) -> &TypedKey {
// &self.node_id
// }
pub fn destructure(self) -> TypedKey {
self.node_id
}
pub fn decode(reader: &veilid_capnp::operation_find_node_q::Reader) -> Result<Self, RPCError> {
let ni_reader = reader.get_node_id().map_err(RPCError::protocol)?;
let node_id = decode_typed_key(&ni_reader)?;
Ok(RPCOperationFindNodeQ { node_id })
Ok(Self { node_id })
}
pub fn encode(
&self,
@@ -25,15 +40,40 @@ impl RPCOperationFindNodeQ {
#[derive(Debug, Clone)]
pub struct RPCOperationFindNodeA {
pub peers: Vec<PeerInfo>,
peers: Vec<PeerInfo>,
}
impl RPCOperationFindNodeA {
pub fn new(peers: Vec<PeerInfo>) -> Result<Self, RPCError> {
if peers.len() > MAX_FIND_NODE_A_PEERS_LEN {
return Err(RPCError::protocol("find node peers length too long"));
}
Ok(Self { peers })
}
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
PeerInfo::validate_vec(&mut self.peers, validate_context.crypto.clone());
Ok(())
}
// pub fn peers(&self) -> &[PeerInfo] {
// &self.peers
// }
pub fn destructure(self) -> Vec<PeerInfo> {
self.peers
}
pub fn decode(
reader: &veilid_capnp::operation_find_node_a::Reader,
crypto: Crypto,
) -> Result<RPCOperationFindNodeA, RPCError> {
let peers_reader = reader.get_peers().map_err(RPCError::protocol)?;
if peers_reader.len() as usize > MAX_FIND_NODE_A_PEERS_LEN {
return Err(RPCError::protocol("find node peers length too long"));
}
let mut peers = Vec::<PeerInfo>::with_capacity(
peers_reader
.len()
@@ -41,11 +81,11 @@ impl RPCOperationFindNodeA {
.map_err(RPCError::map_internal("too many peers"))?,
);
for p in peers_reader.iter() {
let peer_info = decode_peer_info(&p, crypto.clone())?;
let peer_info = decode_peer_info(&p)?;
peers.push(peer_info);
}
Ok(RPCOperationFindNodeA { peers })
Ok(Self { peers })
}
pub fn encode(
&self,
@@ -1,19 +1,67 @@
use super::*;
use crate::storage_manager::{SignedValueData, SignedValueDescriptor};
const MAX_GET_VALUE_A_PEERS_LEN: usize = 20;
#[derive(Clone)]
pub struct ValidateGetValueContext {
pub last_descriptor: Option<SignedValueDescriptor>,
pub subkey: ValueSubkey,
pub vcrypto: CryptoSystemVersion,
}
impl fmt::Debug for ValidateGetValueContext {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ValidateGetValueContext")
.field("last_descriptor", &self.last_descriptor)
.field("subkey", &self.subkey)
.field("vcrypto", &self.vcrypto.kind().to_string())
.finish()
}
}
#[derive(Debug, Clone)]
pub struct RPCOperationGetValueQ {
pub key: TypedKey,
pub subkey: ValueSubkey,
key: TypedKey,
subkey: ValueSubkey,
want_descriptor: bool,
}
impl RPCOperationGetValueQ {
pub fn decode(
reader: &veilid_capnp::operation_get_value_q::Reader,
) -> Result<RPCOperationGetValueQ, RPCError> {
let k_reader = reader.get_key().map_err(RPCError::protocol)?;
pub fn new(key: TypedKey, subkey: ValueSubkey, want_descriptor: bool) -> Self {
Self {
key,
subkey,
want_descriptor,
}
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
// pub fn key(&self) -> &TypedKey {
// &self.key
// }
// pub fn subkey(&self) -> ValueSubkey {
// self.subkey
// }
// pub fn want_descriptor(&self) -> bool {
// self.want_descriptor
// }
pub fn destructure(self) -> (TypedKey, ValueSubkey, bool) {
(self.key, self.subkey, self.want_descriptor)
}
pub fn decode(reader: &veilid_capnp::operation_get_value_q::Reader) -> Result<Self, RPCError> {
let k_reader = reader.reborrow().get_key().map_err(RPCError::protocol)?;
let key = decode_typed_key(&k_reader)?;
let subkey = reader.get_subkey();
Ok(RPCOperationGetValueQ { key, subkey })
let subkey = reader.reborrow().get_subkey();
let want_descriptor = reader.reborrow().get_want_descriptor();
Ok(Self {
key,
subkey,
want_descriptor,
})
}
pub fn encode(
&self,
@@ -22,64 +70,171 @@ impl RPCOperationGetValueQ {
let mut k_builder = builder.reborrow().init_key();
encode_typed_key(&self.key, &mut k_builder);
builder.set_subkey(self.subkey);
builder.set_want_descriptor(self.want_descriptor);
Ok(())
}
}
#[derive(Debug, Clone)]
pub enum RPCOperationGetValueA {
Data(ValueData),
Peers(Vec<PeerInfo>),
pub struct RPCOperationGetValueA {
value: Option<SignedValueData>,
peers: Vec<PeerInfo>,
descriptor: Option<SignedValueDescriptor>,
}
impl RPCOperationGetValueA {
pub fn decode(
reader: &veilid_capnp::operation_get_value_a::Reader,
crypto: Crypto,
) -> Result<RPCOperationGetValueA, RPCError> {
match reader.which().map_err(RPCError::protocol)? {
veilid_capnp::operation_get_value_a::Which::Data(r) => {
let data = decode_value_data(&r.map_err(RPCError::protocol)?)?;
Ok(RPCOperationGetValueA::Data(data))
}
veilid_capnp::operation_get_value_a::Which::Peers(r) => {
let peers_reader = r.map_err(RPCError::protocol)?;
let mut peers = Vec::<PeerInfo>::with_capacity(
peers_reader
.len()
.try_into()
.map_err(RPCError::map_internal("too many peers"))?,
);
for p in peers_reader.iter() {
let peer_info = decode_peer_info(&p, crypto.clone())?;
peers.push(peer_info);
}
pub fn new(
value: Option<SignedValueData>,
peers: Vec<PeerInfo>,
descriptor: Option<SignedValueDescriptor>,
) -> Result<Self, RPCError> {
if peers.len() > MAX_GET_VALUE_A_PEERS_LEN {
return Err(RPCError::protocol("GetValueA peers length too long"));
}
if descriptor.is_some() && !value.is_some() {
return Err(RPCError::protocol(
"GetValueA should not return descriptor without value",
));
}
Ok(Self {
value,
peers,
descriptor,
})
}
Ok(RPCOperationGetValueA::Peers(peers))
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
let question_context = validate_context
.question_context
.as_ref()
.expect("GetValueA requires question context");
let QuestionContext::GetValue(get_value_context) = question_context else {
panic!("Wrong context type for GetValueA");
};
if let Some(value) = &self.value {
// Get descriptor to validate with
let descriptor = if let Some(descriptor) = &self.descriptor {
if let Some(last_descriptor) = &get_value_context.last_descriptor {
if descriptor.cmp_no_sig(last_descriptor) != cmp::Ordering::Equal {
return Err(RPCError::protocol(
"getvalue descriptor does not match last descriptor",
));
}
}
descriptor
} else {
let Some(descriptor) = &get_value_context.last_descriptor else {
return Err(RPCError::protocol(
"no last descriptor, requires a descriptor",
));
};
descriptor
};
// Ensure the descriptor itself validates
descriptor
.validate(get_value_context.vcrypto.clone())
.map_err(RPCError::protocol)?;
// And the signed value data
value
.validate(
descriptor.owner(),
get_value_context.subkey,
get_value_context.vcrypto.clone(),
)
.map_err(RPCError::protocol)?;
} else {
// No value, should not have descriptor
if self.descriptor.is_some() {
return Err(RPCError::protocol("descriptor returned without a value"));
}
}
PeerInfo::validate_vec(&mut self.peers, validate_context.crypto.clone());
Ok(())
}
// pub fn value(&self) -> Option<&SignedValueData> {
// self.value.as_ref()
// }
// pub fn peers(&self) -> &[PeerInfo] {
// &self.peers
// }
// pub fn descriptor(&self) -> Option<&SignedValueDescriptor> {
// self.descriptor.as_ref()
// }
pub fn destructure(
self,
) -> (
Option<SignedValueData>,
Vec<PeerInfo>,
Option<SignedValueDescriptor>,
) {
(self.value, self.peers, self.descriptor)
}
pub fn decode(reader: &veilid_capnp::operation_get_value_a::Reader) -> Result<Self, RPCError> {
let value = if reader.has_value() {
let value_reader = reader.get_value().map_err(RPCError::protocol)?;
let value = decode_signed_value_data(&value_reader)?;
Some(value)
} else {
None
};
let peers_reader = reader.get_peers().map_err(RPCError::protocol)?;
if peers_reader.len() as usize > MAX_GET_VALUE_A_PEERS_LEN {
return Err(RPCError::protocol("GetValueA peers length too long"));
}
let mut peers = Vec::<PeerInfo>::with_capacity(
peers_reader
.len()
.try_into()
.map_err(RPCError::map_internal("too many peers"))?,
);
for p in peers_reader.iter() {
let peer_info = decode_peer_info(&p)?;
peers.push(peer_info);
}
let descriptor = if reader.has_descriptor() {
let d_reader = reader.get_descriptor().map_err(RPCError::protocol)?;
let descriptor = decode_signed_value_descriptor(&d_reader)?;
Some(descriptor)
} else {
None
};
Ok(Self {
value,
peers,
descriptor,
})
}
pub fn encode(
&self,
builder: &mut veilid_capnp::operation_get_value_a::Builder,
) -> Result<(), RPCError> {
match self {
RPCOperationGetValueA::Data(data) => {
let mut d_builder = builder.reborrow().init_data();
encode_value_data(&data, &mut d_builder)?;
}
RPCOperationGetValueA::Peers(peers) => {
let mut peers_builder = builder.reborrow().init_peers(
peers
.len()
.try_into()
.map_err(RPCError::map_internal("invalid peers list length"))?,
);
for (i, peer) in peers.iter().enumerate() {
let mut pi_builder = peers_builder.reborrow().get(i as u32);
encode_peer_info(peer, &mut pi_builder)?;
}
}
if let Some(value) = &self.value {
let mut v_builder = builder.reborrow().init_value();
encode_signed_value_data(value, &mut v_builder)?;
}
let mut peers_builder = builder.reborrow().init_peers(
self.peers
.len()
.try_into()
.map_err(RPCError::map_internal("invalid peers list length"))?,
);
for (i, peer) in self.peers.iter().enumerate() {
let mut pi_builder = peers_builder.reborrow().get(i as u32);
encode_peer_info(peer, &mut pi_builder)?;
}
if let Some(descriptor) = &self.descriptor {
let mut d_builder = builder.reborrow().init_descriptor();
encode_signed_value_descriptor(descriptor, &mut d_builder)?;
}
Ok(())
@@ -2,17 +2,46 @@ use super::*;
#[derive(Debug, Clone)]
pub struct RPCOperationReturnReceipt {
pub receipt: Vec<u8>,
receipt: Vec<u8>,
}
impl RPCOperationReturnReceipt {
pub fn new(receipt: Vec<u8>) -> Result<Self, RPCError> {
if receipt.len() < MIN_RECEIPT_SIZE {
return Err(RPCError::protocol("ReturnReceipt receipt too short to set"));
}
if receipt.len() > MAX_RECEIPT_SIZE {
return Err(RPCError::protocol("ReturnReceipt receipt too long to set"));
}
Ok(Self { receipt })
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
// pub fn receipt(&self) -> &[u8] {
// &self.receipt
// }
pub fn destructure(self) -> Vec<u8> {
self.receipt
}
pub fn decode(
reader: &veilid_capnp::operation_return_receipt::Reader,
) -> Result<RPCOperationReturnReceipt, RPCError> {
let rcpt_reader = reader.get_receipt().map_err(RPCError::protocol)?;
let receipt = rcpt_reader.to_vec();
) -> Result<Self, RPCError> {
let rr = reader.get_receipt().map_err(RPCError::protocol)?;
if rr.len() < MIN_RECEIPT_SIZE {
return Err(RPCError::protocol("ReturnReceipt receipt too short to set"));
}
if rr.len() > MAX_RECEIPT_SIZE {
return Err(RPCError::protocol("ReturnReceipt receipt too long to set"));
}
Ok(RPCOperationReturnReceipt { receipt })
Ok(Self {
receipt: rr.to_vec(),
})
}
pub fn encode(
&self,
@@ -2,10 +2,10 @@ use super::*;
#[derive(Debug, Clone)]
pub struct RoutedOperation {
pub sequencing: Sequencing,
pub signatures: Vec<Signature>,
pub nonce: Nonce,
pub data: Vec<u8>,
sequencing: Sequencing,
signatures: Vec<Signature>,
nonce: Nonce,
data: Vec<u8>,
}
impl RoutedOperation {
@@ -17,10 +17,33 @@ impl RoutedOperation {
data,
}
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
//xxx
Ok(())
}
pub fn sequencing(&self) -> Sequencing {
self.sequencing
}
pub fn signatures(&self) -> &[Signature] {
&self.signatures
}
pub fn decode(
reader: &veilid_capnp::routed_operation::Reader,
) -> Result<RoutedOperation, RPCError> {
pub fn add_signature(&mut self, signature: Signature) {
self.signatures.push(signature);
}
pub fn nonce(&self) -> &Nonce {
&self.nonce
}
pub fn data(&self) -> &[u8] {
&self.data
}
pub fn destructure(self) -> (Sequencing, Vec<Signature>, Nonce, Vec<u8>) {
(self.sequencing, self.signatures, self.nonce, self.data)
}
pub fn decode(reader: &veilid_capnp::routed_operation::Reader) -> Result<Self, RPCError> {
let sigs_reader = reader.get_signatures().map_err(RPCError::protocol)?;
let mut signatures = Vec::<Signature>::with_capacity(
sigs_reader
@@ -36,13 +59,13 @@ impl RoutedOperation {
let sequencing = decode_sequencing(reader.get_sequencing().map_err(RPCError::protocol)?);
let n_reader = reader.get_nonce().map_err(RPCError::protocol)?;
let nonce = decode_nonce(&n_reader);
let data = reader.get_data().map_err(RPCError::protocol)?.to_vec();
let data = reader.get_data().map_err(RPCError::protocol)?;
Ok(RoutedOperation {
Ok(Self {
sequencing,
signatures,
nonce,
data,
data: data.to_vec(),
})
}
@@ -73,22 +96,39 @@ impl RoutedOperation {
#[derive(Debug, Clone)]
pub struct RPCOperationRoute {
pub safety_route: SafetyRoute,
pub operation: RoutedOperation,
safety_route: SafetyRoute,
operation: RoutedOperation,
}
impl RPCOperationRoute {
pub fn decode(
reader: &veilid_capnp::operation_route::Reader,
crypto: Crypto,
) -> Result<RPCOperationRoute, RPCError> {
pub fn new(safety_route: SafetyRoute, operation: RoutedOperation) -> Self {
Self {
safety_route,
operation,
}
}
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
self.operation.validate(validate_context)
}
pub fn safety_route(&self) -> &SafetyRoute {
&self.safety_route
}
pub fn operation(&self) -> &RoutedOperation {
&self.operation
}
pub fn destructure(self) -> (SafetyRoute, RoutedOperation) {
(self.safety_route, self.operation)
}
pub fn decode(reader: &veilid_capnp::operation_route::Reader) -> Result<Self, RPCError> {
let sr_reader = reader.get_safety_route().map_err(RPCError::protocol)?;
let safety_route = decode_safety_route(&sr_reader, crypto)?;
let safety_route = decode_safety_route(&sr_reader)?;
let o_reader = reader.get_operation().map_err(RPCError::protocol)?;
let operation = RoutedOperation::decode(&o_reader)?;
Ok(RPCOperationRoute {
Ok(Self {
safety_route,
operation,
})
@@ -1,22 +1,96 @@
use super::*;
use crate::storage_manager::{SignedValueData, SignedValueDescriptor};
const MAX_SET_VALUE_A_PEERS_LEN: usize = 20;
#[derive(Clone)]
pub struct ValidateSetValueContext {
pub descriptor: SignedValueDescriptor,
pub subkey: ValueSubkey,
pub vcrypto: CryptoSystemVersion,
}
impl fmt::Debug for ValidateSetValueContext {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ValidateSetValueContext")
.field("descriptor", &self.descriptor)
.field("subkey", &self.subkey)
.field("vcrypto", &self.vcrypto.kind().to_string())
.finish()
}
}
#[derive(Debug, Clone)]
pub struct RPCOperationSetValueQ {
pub key: TypedKey,
pub subkey: ValueSubkey,
pub value: ValueData,
key: TypedKey,
subkey: ValueSubkey,
value: SignedValueData,
descriptor: Option<SignedValueDescriptor>,
}
impl RPCOperationSetValueQ {
pub fn decode(
reader: &veilid_capnp::operation_set_value_q::Reader,
) -> Result<RPCOperationSetValueQ, RPCError> {
pub fn new(
key: TypedKey,
subkey: ValueSubkey,
value: SignedValueData,
descriptor: Option<SignedValueDescriptor>,
) -> Self {
Self {
key,
subkey,
value,
descriptor,
}
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
// pub fn key(&self) -> &TypedKey {
// &self.key
// }
// pub fn subkey(&self) -> ValueSubkey {
// self.subkey
// }
// pub fn value(&self) -> &SignedValueData {
// &self.value
// }
// pub fn descriptor(&self) -> Option<&SignedValueDescriptor> {
// self.descriptor.as_ref()
// }
pub fn destructure(
self,
) -> (
TypedKey,
ValueSubkey,
SignedValueData,
Option<SignedValueDescriptor>,
) {
(self.key, self.subkey, self.value, self.descriptor)
}
pub fn decode(reader: &veilid_capnp::operation_set_value_q::Reader) -> Result<Self, RPCError> {
let k_reader = reader.get_key().map_err(RPCError::protocol)?;
let key = decode_typed_key(&k_reader)?;
let subkey = reader.get_subkey();
let v_reader = reader.get_value().map_err(RPCError::protocol)?;
let value = decode_value_data(&v_reader)?;
Ok(RPCOperationSetValueQ { key, subkey, value })
let value = decode_signed_value_data(&v_reader)?;
let descriptor = if reader.has_descriptor() {
let d_reader = reader.get_descriptor().map_err(RPCError::protocol)?;
let descriptor = decode_signed_value_descriptor(&d_reader)?;
Some(descriptor)
} else {
None
};
Ok(Self {
key,
subkey,
value,
descriptor,
})
}
pub fn encode(
&self,
@@ -26,65 +100,123 @@ impl RPCOperationSetValueQ {
encode_typed_key(&self.key, &mut k_builder);
builder.set_subkey(self.subkey);
let mut v_builder = builder.reborrow().init_value();
encode_value_data(&self.value, &mut v_builder)?;
encode_signed_value_data(&self.value, &mut v_builder)?;
if let Some(descriptor) = &self.descriptor {
let mut d_builder = builder.reborrow().init_descriptor();
encode_signed_value_descriptor(descriptor, &mut d_builder)?;
}
Ok(())
}
}
#[derive(Debug, Clone)]
pub enum RPCOperationSetValueA {
Data(ValueData),
Peers(Vec<PeerInfo>),
pub struct RPCOperationSetValueA {
set: bool,
value: Option<SignedValueData>,
peers: Vec<PeerInfo>,
}
impl RPCOperationSetValueA {
pub fn decode(
reader: &veilid_capnp::operation_set_value_a::Reader,
crypto: Crypto,
) -> Result<RPCOperationSetValueA, RPCError> {
match reader.which().map_err(RPCError::protocol)? {
veilid_capnp::operation_set_value_a::Which::Data(r) => {
let data = decode_value_data(&r.map_err(RPCError::protocol)?)?;
Ok(RPCOperationSetValueA::Data(data))
}
veilid_capnp::operation_set_value_a::Which::Peers(r) => {
let peers_reader = r.map_err(RPCError::protocol)?;
let mut peers = Vec::<PeerInfo>::with_capacity(
peers_reader
.len()
.try_into()
.map_err(RPCError::map_internal("too many peers"))?,
);
for p in peers_reader.iter() {
let peer_info = decode_peer_info(&p, crypto.clone())?;
peers.push(peer_info);
}
Ok(RPCOperationSetValueA::Peers(peers))
}
pub fn new(
set: bool,
value: Option<SignedValueData>,
peers: Vec<PeerInfo>,
) -> Result<Self, RPCError> {
if peers.len() as usize > MAX_SET_VALUE_A_PEERS_LEN {
return Err(RPCError::protocol("SetValueA peers length too long"));
}
Ok(Self { set, value, peers })
}
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
let question_context = validate_context
.question_context
.as_ref()
.expect("SetValueA requires question context");
let QuestionContext::SetValue(set_value_context) = question_context else {
panic!("Wrong context type for SetValueA");
};
if let Some(value) = &self.value {
// Ensure the descriptor itself validates
set_value_context
.descriptor
.validate(set_value_context.vcrypto.clone())
.map_err(RPCError::protocol)?;
// And the signed value data
value
.validate(
set_value_context.descriptor.owner(),
set_value_context.subkey,
set_value_context.vcrypto.clone(),
)
.map_err(RPCError::protocol)?;
}
PeerInfo::validate_vec(&mut self.peers, validate_context.crypto.clone());
Ok(())
}
// pub fn set(&self) -> bool {
// self.set
// }
// pub fn value(&self) -> Option<&SignedValueData> {
// self.value.as_ref()
// }
// pub fn peers(&self) -> &[PeerInfo] {
// &self.peers
// }
pub fn destructure(self) -> (bool, Option<SignedValueData>, Vec<PeerInfo>) {
(self.set, self.value, self.peers)
}
pub fn decode(reader: &veilid_capnp::operation_set_value_a::Reader) -> Result<Self, RPCError> {
let set = reader.get_set();
let value = if reader.has_value() {
let v_reader = reader.get_value().map_err(RPCError::protocol)?;
let value = decode_signed_value_data(&v_reader)?;
Some(value)
} else {
None
};
let peers_reader = reader.get_peers().map_err(RPCError::protocol)?;
if peers_reader.len() as usize > MAX_SET_VALUE_A_PEERS_LEN {
return Err(RPCError::protocol("SetValueA peers length too long"));
}
let mut peers = Vec::<PeerInfo>::with_capacity(
peers_reader
.len()
.try_into()
.map_err(RPCError::map_internal("too many peers"))?,
);
for p in peers_reader.iter() {
let peer_info = decode_peer_info(&p)?;
peers.push(peer_info);
}
Ok(Self { set, value, peers })
}
pub fn encode(
&self,
builder: &mut veilid_capnp::operation_set_value_a::Builder,
) -> Result<(), RPCError> {
match self {
RPCOperationSetValueA::Data(data) => {
let mut d_builder = builder.reborrow().init_data();
encode_value_data(&data, &mut d_builder)?;
}
RPCOperationSetValueA::Peers(peers) => {
let mut peers_builder = builder.reborrow().init_peers(
peers
.len()
.try_into()
.map_err(RPCError::map_internal("invalid peers list length"))?,
);
for (i, peer) in peers.iter().enumerate() {
let mut pi_builder = peers_builder.reborrow().get(i as u32);
encode_peer_info(peer, &mut pi_builder)?;
}
}
builder.set_set(self.set);
if let Some(value) = &self.value {
let mut v_builder = builder.reborrow().init_value();
encode_signed_value_data(value, &mut v_builder)?;
}
let mut peers_builder = builder.reborrow().init_peers(
self.peers
.len()
.try_into()
.map_err(RPCError::map_internal("invalid peers list length"))?,
);
for (i, peer) in self.peers.iter().enumerate() {
let mut pi_builder = peers_builder.reborrow().get(i as u32);
encode_peer_info(peer, &mut pi_builder)?;
}
Ok(())
@@ -2,16 +2,26 @@ use super::*;
#[derive(Debug, Clone)]
pub struct RPCOperationSignal {
pub signal_info: SignalInfo,
signal_info: SignalInfo,
}
impl RPCOperationSignal {
pub fn decode(
reader: &veilid_capnp::operation_signal::Reader,
crypto: Crypto,
) -> Result<RPCOperationSignal, RPCError> {
let signal_info = decode_signal_info(reader, crypto)?;
Ok(RPCOperationSignal { signal_info })
pub fn new(signal_info: SignalInfo) -> Self {
Self { signal_info }
}
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
self.signal_info.validate(validate_context.crypto.clone())
}
// pub fn signal_info(&self) -> &SignalInfo {
// &self.signal_info
// }
pub fn destructure(self) -> SignalInfo {
self.signal_info
}
pub fn decode(reader: &veilid_capnp::operation_signal::Reader) -> Result<Self, RPCError> {
let signal_info = decode_signal_info(reader)?;
Ok(Self { signal_info })
}
pub fn encode(
&self,
@@ -2,15 +2,40 @@ use super::*;
#[derive(Debug, Clone)]
pub struct RPCOperationStartTunnelQ {
pub id: TunnelId,
pub local_mode: TunnelMode,
pub depth: u8,
id: TunnelId,
local_mode: TunnelMode,
depth: u8,
}
impl RPCOperationStartTunnelQ {
pub fn new(id: TunnelId, local_mode: TunnelMode, depth: u8) -> Self {
Self {
id,
local_mode,
depth,
}
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
pub fn id(&self) -> TunnelId {
self.id
}
pub fn local_mode(&self) -> TunnelMode {
self.local_mode
}
pub fn depth(&self) -> u8 {
self.depth
}
pub fn destructure(self) -> (TunnelId, TunnelMode, u8) {
(self.id, self.local_mode, self.depth)
}
pub fn decode(
reader: &veilid_capnp::operation_start_tunnel_q::Reader,
) -> Result<RPCOperationStartTunnelQ, RPCError> {
) -> Result<Self, RPCError> {
let id = TunnelId::new(reader.get_id());
let local_mode = match reader.get_local_mode().map_err(RPCError::protocol)? {
veilid_capnp::TunnelEndpointMode::Raw => TunnelMode::Raw,
@@ -18,7 +43,7 @@ impl RPCOperationStartTunnelQ {
};
let depth = reader.get_depth();
Ok(RPCOperationStartTunnelQ {
Ok(Self {
id,
local_mode,
depth,
@@ -46,18 +71,28 @@ pub enum RPCOperationStartTunnelA {
}
impl RPCOperationStartTunnelA {
pub fn new_partial(partial_tunnel: PartialTunnel) -> Self {
Self::Partial(partial_tunnel)
}
pub fn new_error(tunnel_error: TunnelError) -> Self {
Self::Error(tunnel_error)
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
pub fn decode(
reader: &veilid_capnp::operation_start_tunnel_a::Reader,
) -> Result<RPCOperationStartTunnelA, RPCError> {
) -> Result<Self, RPCError> {
match reader.which().map_err(RPCError::protocol)? {
veilid_capnp::operation_start_tunnel_a::Which::Partial(r) => {
let pt_reader = r.map_err(RPCError::protocol)?;
let partial_tunnel = decode_partial_tunnel(&pt_reader)?;
Ok(RPCOperationStartTunnelA::Partial(partial_tunnel))
Ok(Self::Partial(partial_tunnel))
}
veilid_capnp::operation_start_tunnel_a::Which::Error(r) => {
let tunnel_error = decode_tunnel_error(r.map_err(RPCError::protocol)?);
Ok(RPCOperationStartTunnelA::Error(tunnel_error))
Ok(Self::Error(tunnel_error))
}
}
}
@@ -66,10 +101,10 @@ impl RPCOperationStartTunnelA {
builder: &mut veilid_capnp::operation_start_tunnel_a::Builder,
) -> Result<(), RPCError> {
match self {
RPCOperationStartTunnelA::Partial(p) => {
Self::Partial(p) => {
encode_partial_tunnel(p, &mut builder.reborrow().init_partial())?;
}
RPCOperationStartTunnelA::Error(e) => {
Self::Error(e) => {
builder.set_error(encode_tunnel_error(*e));
}
}
@@ -2,13 +2,25 @@ use super::*;
#[derive(Debug, Clone)]
pub struct RPCOperationStatusQ {
pub node_status: Option<NodeStatus>,
node_status: Option<NodeStatus>,
}
impl RPCOperationStatusQ {
pub fn decode(
reader: &veilid_capnp::operation_status_q::Reader,
) -> Result<RPCOperationStatusQ, RPCError> {
pub fn new(node_status: Option<NodeStatus>) -> Self {
Self { node_status }
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
// pub fn node_status(&self) -> Option<&NodeStatus> {
// self.node_status.as_ref()
// }
pub fn destructure(self) -> Option<NodeStatus> {
self.node_status
}
pub fn decode(reader: &veilid_capnp::operation_status_q::Reader) -> Result<Self, RPCError> {
let node_status = if reader.has_node_status() {
let ns_reader = reader.get_node_status().map_err(RPCError::protocol)?;
let node_status = decode_node_status(&ns_reader)?;
@@ -16,7 +28,7 @@ impl RPCOperationStatusQ {
} else {
None
};
Ok(RPCOperationStatusQ { node_status })
Ok(Self { node_status })
}
pub fn encode(
&self,
@@ -32,14 +44,33 @@ impl RPCOperationStatusQ {
#[derive(Debug, Clone)]
pub struct RPCOperationStatusA {
pub node_status: Option<NodeStatus>,
pub sender_info: Option<SenderInfo>,
node_status: Option<NodeStatus>,
sender_info: Option<SenderInfo>,
}
impl RPCOperationStatusA {
pub fn decode(
reader: &veilid_capnp::operation_status_a::Reader,
) -> Result<RPCOperationStatusA, RPCError> {
pub fn new(node_status: Option<NodeStatus>, sender_info: Option<SenderInfo>) -> Self {
Self {
node_status,
sender_info,
}
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
// pub fn node_status(&self) -> Option<&NodeStatus> {
// self.node_status.as_ref()
// }
// pub fn sender_info(&self) -> Option<&SenderInfo> {
// self.sender_info.as_ref()
// }
pub fn destructure(self) -> (Option<NodeStatus>, Option<SenderInfo>) {
(self.node_status, self.sender_info)
}
pub fn decode(reader: &veilid_capnp::operation_status_a::Reader) -> Result<Self, RPCError> {
let node_status = if reader.has_node_status() {
let ns_reader = reader.get_node_status().map_err(RPCError::protocol)?;
let node_status = decode_node_status(&ns_reader)?;
@@ -56,7 +87,7 @@ impl RPCOperationStatusA {
None
};
Ok(RPCOperationStatusA {
Ok(Self {
node_status,
sender_info,
})
@@ -1,18 +1,35 @@
use super::*;
const MAX_SUPPLY_BLOCK_A_PEERS_LEN: usize = 20;
#[derive(Debug, Clone)]
pub struct RPCOperationSupplyBlockQ {
pub block_id: TypedKey,
block_id: TypedKey,
}
impl RPCOperationSupplyBlockQ {
pub fn new(block_id: TypedKey) -> Self {
Self { block_id }
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
pub fn block_id(&self) -> &TypedKey {
&self.block_id
}
pub fn destructure(self) -> TypedKey {
self.block_id
}
pub fn decode(
reader: &veilid_capnp::operation_supply_block_q::Reader,
) -> Result<RPCOperationSupplyBlockQ, RPCError> {
) -> Result<Self, RPCError> {
let bi_reader = reader.get_block_id().map_err(RPCError::protocol)?;
let block_id = decode_typed_key(&bi_reader)?;
Ok(RPCOperationSupplyBlockQ { block_id })
Ok(Self { block_id })
}
pub fn encode(
&self,
@@ -26,57 +43,68 @@ impl RPCOperationSupplyBlockQ {
}
#[derive(Debug, Clone)]
pub enum RPCOperationSupplyBlockA {
Expiration(u64),
Peers(Vec<PeerInfo>),
pub struct RPCOperationSupplyBlockA {
expiration: u64,
peers: Vec<PeerInfo>,
}
impl RPCOperationSupplyBlockA {
pub fn new(expiration: u64, peers: Vec<PeerInfo>) -> Result<Self, RPCError> {
if peers.len() > MAX_SUPPLY_BLOCK_A_PEERS_LEN {
return Err(RPCError::protocol("SupplyBlockA peers length too long"));
}
Ok(Self { expiration, peers })
}
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
PeerInfo::validate_vec(&mut self.peers, validate_context.crypto.clone());
Ok(())
}
pub fn expiration(&self) -> u64 {
self.expiration
}
pub fn peers(&self) -> &[PeerInfo] {
&self.peers
}
pub fn destructure(self) -> (u64, Vec<PeerInfo>) {
(self.expiration, self.peers)
}
pub fn decode(
reader: &veilid_capnp::operation_supply_block_a::Reader,
crypto: Crypto,
) -> Result<RPCOperationSupplyBlockA, RPCError> {
match reader.which().map_err(RPCError::protocol)? {
veilid_capnp::operation_supply_block_a::Which::Expiration(r) => {
Ok(RPCOperationSupplyBlockA::Expiration(r))
}
veilid_capnp::operation_supply_block_a::Which::Peers(r) => {
let peers_reader = r.map_err(RPCError::protocol)?;
let mut peers = Vec::<PeerInfo>::with_capacity(
peers_reader
.len()
.try_into()
.map_err(RPCError::map_internal("too many peers"))?,
);
for p in peers_reader.iter() {
let peer_info = decode_peer_info(&p, crypto.clone())?;
peers.push(peer_info);
}
) -> Result<Self, RPCError> {
let expiration = reader.get_expiration();
Ok(RPCOperationSupplyBlockA::Peers(peers))
}
let peers_reader = reader.get_peers().map_err(RPCError::protocol)?;
if peers_reader.len() as usize > MAX_SUPPLY_BLOCK_A_PEERS_LEN {
return Err(RPCError::protocol("SupplyBlockA peers length too long"));
}
let mut peers = Vec::<PeerInfo>::with_capacity(
peers_reader
.len()
.try_into()
.map_err(RPCError::map_internal("too many peers"))?,
);
for p in peers_reader.iter() {
let peer_info = decode_peer_info(&p)?;
peers.push(peer_info);
}
Ok(Self { expiration, peers })
}
pub fn encode(
&self,
builder: &mut veilid_capnp::operation_supply_block_a::Builder,
) -> Result<(), RPCError> {
match self {
RPCOperationSupplyBlockA::Expiration(e) => {
builder.set_expiration(*e);
}
RPCOperationSupplyBlockA::Peers(peers) => {
let mut peers_builder = builder.reborrow().init_peers(
peers
.len()
.try_into()
.map_err(RPCError::map_internal("invalid peers list length"))?,
);
for (i, peer) in peers.iter().enumerate() {
let mut pi_builder = peers_builder.reborrow().get(i as u32);
encode_peer_info(peer, &mut pi_builder)?;
}
}
builder.set_expiration(self.expiration);
let mut peers_builder = builder.reborrow().init_peers(
self.peers
.len()
.try_into()
.map_err(RPCError::map_internal("invalid peers list length"))?,
);
for (i, peer) in self.peers.iter().enumerate() {
let mut pi_builder = peers_builder.reborrow().get(i as u32);
encode_peer_info(peer, &mut pi_builder)?;
}
Ok(())
@@ -2,22 +2,68 @@ use super::*;
#[derive(Debug, Clone)]
pub struct RPCOperationValidateDialInfo {
pub dial_info: DialInfo,
pub receipt: Vec<u8>,
pub redirect: bool,
dial_info: DialInfo,
receipt: Vec<u8>,
redirect: bool,
}
impl RPCOperationValidateDialInfo {
pub fn new(dial_info: DialInfo, receipt: Vec<u8>, redirect: bool) -> Result<Self, RPCError> {
if receipt.len() < MIN_RECEIPT_SIZE {
return Err(RPCError::protocol(
"ValidateDialInfo receipt too short to set",
));
}
if receipt.len() > MAX_RECEIPT_SIZE {
return Err(RPCError::protocol(
"ValidateDialInfo receipt too long to set",
));
}
Ok(Self {
dial_info,
receipt,
redirect,
})
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
Ok(())
}
// pub fn dial_info(&self) -> &DialInfo {
// &self.dial_info
// }
// pub fn receipt(&self) -> &[u8] {
// &self.receipt
// }
// pub fn redirect(&self) -> bool {
// self.redirect
// }
pub fn destructure(self) -> (DialInfo, Vec<u8>, bool) {
(self.dial_info, self.receipt, self.redirect)
}
pub fn decode(
reader: &veilid_capnp::operation_validate_dial_info::Reader,
) -> Result<RPCOperationValidateDialInfo, RPCError> {
) -> Result<Self, RPCError> {
let di_reader = reader.get_dial_info().map_err(RPCError::protocol)?;
let dial_info = decode_dial_info(&di_reader)?;
let rcpt_reader = reader.get_receipt().map_err(RPCError::protocol)?;
if rcpt_reader.len() < MIN_RECEIPT_SIZE {
return Err(RPCError::protocol(
"ValidateDialInfo receipt too short to set",
));
}
if rcpt_reader.len() > MAX_RECEIPT_SIZE {
return Err(RPCError::protocol(
"ValidateDialInfo receipt too long to set",
));
}
let receipt = rcpt_reader.to_vec();
let redirect = reader.get_redirect();
Ok(RPCOperationValidateDialInfo {
Ok(Self {
dial_info,
receipt,
redirect,
@@ -1,17 +1,53 @@
use super::*;
use crate::storage_manager::SignedValueData;
#[derive(Debug, Clone)]
pub struct RPCOperationValueChanged {
pub key: TypedKey,
pub subkeys: Vec<ValueSubkeyRange>,
pub count: u32,
pub value: ValueData,
key: TypedKey,
subkeys: Vec<ValueSubkeyRange>,
count: u32,
value: SignedValueData,
}
impl RPCOperationValueChanged {
pub fn new(
key: TypedKey,
subkeys: Vec<ValueSubkeyRange>,
count: u32,
value: SignedValueData,
) -> Self {
Self {
key,
subkeys,
count,
value,
}
}
pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> {
// validation must be done by storage manager as this is more complicated
Ok(())
}
pub fn key(&self) -> &TypedKey {
&self.key
}
pub fn subkeys(&self) -> &[ValueSubkeyRange] {
&self.subkeys
}
pub fn count(&self) -> u32 {
self.count
}
pub fn value(&self) -> &SignedValueData {
&self.value
}
pub fn destructure(self) -> (TypedKey, Vec<ValueSubkeyRange>, u32, SignedValueData) {
(self.key, self.subkeys, self.count, self.value)
}
pub fn decode(
reader: &veilid_capnp::operation_value_changed::Reader,
) -> Result<RPCOperationValueChanged, RPCError> {
) -> Result<Self, RPCError> {
let k_reader = reader.get_key().map_err(RPCError::protocol)?;
let key = decode_typed_key(&k_reader)?;
@@ -38,8 +74,8 @@ impl RPCOperationValueChanged {
}
let count = reader.get_count();
let v_reader = reader.get_value().map_err(RPCError::protocol)?;
let value = decode_value_data(&v_reader)?;
Ok(RPCOperationValueChanged {
let value = decode_signed_value_data(&v_reader)?;
Ok(Self {
key,
subkeys,
count,
@@ -68,7 +104,7 @@ impl RPCOperationValueChanged {
builder.set_count(self.count);
let mut v_builder = builder.reborrow().init_value();
encode_value_data(&self.value, &mut v_builder)?;
encode_signed_value_data(&self.value, &mut v_builder)?;
Ok(())
}
}
@@ -1,21 +1,117 @@
use super::*;
const MAX_WATCH_VALUE_Q_SUBKEYS_LEN: usize = 512;
const MAX_WATCH_VALUE_A_PEERS_LEN: usize = 20;
#[derive(Debug, Clone)]
pub struct RPCOperationWatchValueQ {
pub key: TypedKey,
pub subkeys: Vec<ValueSubkeyRange>,
pub expiration: u64,
pub count: u32,
key: TypedKey,
subkeys: Vec<ValueSubkeyRange>,
expiration: u64,
count: u32,
watcher: PublicKey,
signature: Signature,
}
impl RPCOperationWatchValueQ {
pub fn new(
key: TypedKey,
subkeys: Vec<ValueSubkeyRange>,
expiration: u64,
count: u32,
watcher: PublicKey,
signature: Signature,
) -> Result<Self, RPCError> {
if subkeys.len() > MAX_WATCH_VALUE_Q_SUBKEYS_LEN {
return Err(RPCError::protocol("WatchValueQ subkeys length too long"));
}
Ok(Self {
key,
subkeys,
expiration,
count,
watcher,
signature,
})
}
// signature covers: key, subkeys, expiration, count, using watcher key
fn make_signature_data(&self) -> Vec<u8> {
let mut sig_data =
Vec::with_capacity(PUBLIC_KEY_LENGTH + 4 + (self.subkeys.len() * 8) + 8 + 4);
sig_data.extend_from_slice(&self.key.kind.0);
sig_data.extend_from_slice(&self.key.value.bytes);
for sk in &self.subkeys {
sig_data.extend_from_slice(&sk.0.to_le_bytes());
sig_data.extend_from_slice(&sk.1.to_le_bytes());
}
sig_data.extend_from_slice(&self.expiration.to_le_bytes());
sig_data.extend_from_slice(&self.count.to_le_bytes());
sig_data
}
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
let Some(vcrypto) = validate_context.crypto.get(self.key.kind) else {
return Err(RPCError::protocol("unsupported cryptosystem"));
};
let sig_data = self.make_signature_data();
vcrypto
.verify(&self.watcher, &sig_data, &self.signature)
.map_err(RPCError::protocol)?;
Ok(())
}
pub fn key(&self) -> &TypedKey {
&self.key
}
pub fn subkeys(&self) -> &[ValueSubkeyRange] {
&self.subkeys
}
pub fn expiration(&self) -> u64 {
self.expiration
}
pub fn count(&self) -> u32 {
self.count
}
pub fn watcher(&self) -> &PublicKey {
&self.watcher
}
pub fn signature(&self) -> &Signature {
&self.signature
}
pub fn destructure(
self,
) -> (
TypedKey,
Vec<ValueSubkeyRange>,
u64,
u32,
PublicKey,
Signature,
) {
(
self.key,
self.subkeys,
self.expiration,
self.count,
self.watcher,
self.signature,
)
}
pub fn decode(
reader: &veilid_capnp::operation_watch_value_q::Reader,
) -> Result<RPCOperationWatchValueQ, RPCError> {
) -> Result<Self, RPCError> {
let k_reader = reader.get_key().map_err(RPCError::protocol)?;
let key = decode_typed_key(&k_reader)?;
let sk_reader = reader.get_subkeys().map_err(RPCError::protocol)?;
if sk_reader.len() as usize > MAX_WATCH_VALUE_Q_SUBKEYS_LEN {
return Err(RPCError::protocol("WatchValueQ subkeys length too long"));
}
let mut subkeys = Vec::<ValueSubkeyRange>::with_capacity(
sk_reader
.len()
@@ -40,13 +136,22 @@ impl RPCOperationWatchValueQ {
let expiration = reader.get_expiration();
let count = reader.get_count();
Ok(RPCOperationWatchValueQ {
let w_reader = reader.get_watcher().map_err(RPCError::protocol)?;
let watcher = decode_key256(&w_reader);
let s_reader = reader.get_signature().map_err(RPCError::protocol)?;
let signature = decode_signature512(&s_reader);
Ok(Self {
key,
subkeys,
expiration,
count,
watcher,
signature,
})
}
pub fn encode(
&self,
builder: &mut veilid_capnp::operation_watch_value_q::Builder,
@@ -67,23 +172,54 @@ impl RPCOperationWatchValueQ {
}
builder.set_expiration(self.expiration);
builder.set_count(self.count);
let mut w_builder = builder.reborrow().init_watcher();
encode_key256(&self.watcher, &mut w_builder);
let mut s_builder = builder.reborrow().init_signature();
encode_signature512(&self.signature, &mut s_builder);
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct RPCOperationWatchValueA {
pub expiration: u64,
pub peers: Vec<PeerInfo>,
expiration: u64,
peers: Vec<PeerInfo>,
}
impl RPCOperationWatchValueA {
pub fn new(expiration: u64, peers: Vec<PeerInfo>) -> Result<Self, RPCError> {
if peers.len() > MAX_WATCH_VALUE_A_PEERS_LEN {
return Err(RPCError::protocol("WatchValueA peers length too long"));
}
Ok(Self { expiration, peers })
}
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
PeerInfo::validate_vec(&mut self.peers, validate_context.crypto.clone());
Ok(())
}
pub fn expiration(&self) -> u64 {
self.expiration
}
pub fn peers(&self) -> &[PeerInfo] {
&self.peers
}
pub fn destructure(self) -> (u64, Vec<PeerInfo>) {
(self.expiration, self.peers)
}
pub fn decode(
reader: &veilid_capnp::operation_watch_value_a::Reader,
crypto: Crypto,
) -> Result<RPCOperationWatchValueA, RPCError> {
) -> Result<Self, RPCError> {
let expiration = reader.get_expiration();
let peers_reader = reader.get_peers().map_err(RPCError::protocol)?;
if peers_reader.len() as usize > MAX_WATCH_VALUE_A_PEERS_LEN {
return Err(RPCError::protocol("WatchValueA peers length too long"));
}
let mut peers = Vec::<PeerInfo>::with_capacity(
peers_reader
.len()
@@ -91,11 +227,11 @@ impl RPCOperationWatchValueA {
.map_err(RPCError::map_internal("too many peers"))?,
);
for p in peers_reader.iter() {
let peer_info = decode_peer_info(&p, crypto.clone())?;
let peer_info = decode_peer_info(&p)?;
peers.push(peer_info);
}
Ok(RPCOperationWatchValueA { expiration, peers })
Ok(Self { expiration, peers })
}
pub fn encode(
&self,
@@ -10,6 +10,10 @@ impl RPCQuestion {
pub fn new(respond_to: RespondTo, detail: RPCQuestionDetail) -> Self {
Self { respond_to, detail }
}
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
self.respond_to.validate(validate_context.crypto.clone())?;
self.detail.validate(validate_context)
}
pub fn respond_to(&self) -> &RespondTo {
&self.respond_to
}
@@ -19,12 +23,12 @@ impl RPCQuestion {
pub fn desc(&self) -> &'static str {
self.detail.desc()
}
pub fn decode(
reader: &veilid_capnp::question::Reader,
crypto: Crypto,
) -> Result<RPCQuestion, RPCError> {
pub fn destructure(self) -> (RespondTo, RPCQuestionDetail) {
(self.respond_to, self.detail)
}
pub fn decode(reader: &veilid_capnp::question::Reader) -> Result<RPCQuestion, RPCError> {
let rt_reader = reader.get_respond_to();
let respond_to = RespondTo::decode(&rt_reader, crypto)?;
let respond_to = RespondTo::decode(&rt_reader)?;
let d_reader = reader.get_detail();
let detail = RPCQuestionDetail::decode(&d_reader)?;
Ok(RPCQuestion { respond_to, detail })
@@ -68,6 +72,21 @@ impl RPCQuestionDetail {
RPCQuestionDetail::CancelTunnelQ(_) => "CancelTunnelQ",
}
}
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
match self {
RPCQuestionDetail::StatusQ(r) => r.validate(validate_context),
RPCQuestionDetail::FindNodeQ(r) => r.validate(validate_context),
RPCQuestionDetail::AppCallQ(r) => r.validate(validate_context),
RPCQuestionDetail::GetValueQ(r) => r.validate(validate_context),
RPCQuestionDetail::SetValueQ(r) => r.validate(validate_context),
RPCQuestionDetail::WatchValueQ(r) => r.validate(validate_context),
RPCQuestionDetail::SupplyBlockQ(r) => r.validate(validate_context),
RPCQuestionDetail::FindBlockQ(r) => r.validate(validate_context),
RPCQuestionDetail::StartTunnelQ(r) => r.validate(validate_context),
RPCQuestionDetail::CompleteTunnelQ(r) => r.validate(validate_context),
RPCQuestionDetail::CancelTunnelQ(r) => r.validate(validate_context),
}
}
pub fn decode(
reader: &veilid_capnp::question::detail::Reader,
@@ -7,6 +7,13 @@ pub enum RespondTo {
}
impl RespondTo {
pub fn validate(&mut self, crypto: Crypto) -> Result<(), RPCError> {
match self {
RespondTo::Sender => Ok(()),
RespondTo::PrivateRoute(pr) => pr.validate(crypto).map_err(RPCError::protocol),
}
}
pub fn encode(
&self,
builder: &mut veilid_capnp::question::respond_to::Builder,
@@ -23,15 +30,12 @@ impl RespondTo {
Ok(())
}
pub fn decode(
reader: &veilid_capnp::question::respond_to::Reader,
crypto: Crypto,
) -> Result<Self, RPCError> {
pub fn decode(reader: &veilid_capnp::question::respond_to::Reader) -> Result<Self, RPCError> {
let respond_to = match reader.which().map_err(RPCError::protocol)? {
veilid_capnp::question::respond_to::Sender(()) => RespondTo::Sender,
veilid_capnp::question::respond_to::PrivateRoute(pr_reader) => {
let pr_reader = pr_reader.map_err(RPCError::protocol)?;
let pr = decode_private_route(&pr_reader, crypto)?;
let pr = decode_private_route(&pr_reader)?;
RespondTo::PrivateRoute(pr)
}
};
@@ -9,21 +9,21 @@ impl RPCStatement {
pub fn new(detail: RPCStatementDetail) -> Self {
Self { detail }
}
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
self.detail.validate(validate_context)
}
pub fn detail(&self) -> &RPCStatementDetail {
&self.detail
}
pub fn into_detail(self) -> RPCStatementDetail {
self.detail
}
pub fn desc(&self) -> &'static str {
self.detail.desc()
}
pub fn decode(
reader: &veilid_capnp::statement::Reader,
crypto: Crypto,
) -> Result<RPCStatement, RPCError> {
pub fn destructure(self) -> RPCStatementDetail {
self.detail
}
pub fn decode(reader: &veilid_capnp::statement::Reader) -> Result<RPCStatement, RPCError> {
let d_reader = reader.get_detail();
let detail = RPCStatementDetail::decode(&d_reader, crypto)?;
let detail = RPCStatementDetail::decode(&d_reader)?;
Ok(RPCStatement { detail })
}
pub fn encode(&self, builder: &mut veilid_capnp::statement::Builder) -> Result<(), RPCError> {
@@ -53,9 +53,18 @@ impl RPCStatementDetail {
RPCStatementDetail::AppMessage(_) => "AppMessage",
}
}
pub fn validate(&mut self, validate_context: &RPCValidateContext) -> Result<(), RPCError> {
match self {
RPCStatementDetail::ValidateDialInfo(r) => r.validate(validate_context),
RPCStatementDetail::Route(r) => r.validate(validate_context),
RPCStatementDetail::ValueChanged(r) => r.validate(validate_context),
RPCStatementDetail::Signal(r) => r.validate(validate_context),
RPCStatementDetail::ReturnReceipt(r) => r.validate(validate_context),
RPCStatementDetail::AppMessage(r) => r.validate(validate_context),
}
}
pub fn decode(
reader: &veilid_capnp::statement::detail::Reader,
crypto: Crypto,
) -> Result<RPCStatementDetail, RPCError> {
let which_reader = reader.which().map_err(RPCError::protocol)?;
let out = match which_reader {
@@ -66,7 +75,7 @@ impl RPCStatementDetail {
}
veilid_capnp::statement::detail::Route(r) => {
let op_reader = r.map_err(RPCError::protocol)?;
let out = RPCOperationRoute::decode(&op_reader, crypto)?;
let out = RPCOperationRoute::decode(&op_reader)?;
RPCStatementDetail::Route(out)
}
veilid_capnp::statement::detail::ValueChanged(r) => {
@@ -76,7 +85,7 @@ impl RPCStatementDetail {
}
veilid_capnp::statement::detail::Signal(r) => {
let op_reader = r.map_err(RPCError::protocol)?;
let out = RPCOperationSignal::decode(&op_reader, crypto)?;
let out = RPCOperationSignal::decode(&op_reader)?;
RPCStatementDetail::Signal(out)
}
veilid_capnp::statement::detail::ReturnReceipt(r) => {
@@ -7,12 +7,12 @@ pub fn encode_peer_info(
//
let mut nids_builder = builder.reborrow().init_node_ids(
peer_info
.node_ids
.node_ids()
.len()
.try_into()
.map_err(RPCError::map_invalid_format("out of bound error"))?,
);
for (i, nid) in peer_info.node_ids.iter().enumerate() {
for (i, nid) in peer_info.node_ids().iter().enumerate() {
encode_typed_key(
nid,
&mut nids_builder.reborrow().get(
@@ -22,15 +22,12 @@ pub fn encode_peer_info(
);
}
let mut sni_builder = builder.reborrow().init_signed_node_info();
encode_signed_node_info(&peer_info.signed_node_info, &mut sni_builder)?;
encode_signed_node_info(peer_info.signed_node_info(), &mut sni_builder)?;
Ok(())
}
pub fn decode_peer_info(
reader: &veilid_capnp::peer_info::Reader,
crypto: Crypto,
) -> Result<PeerInfo, RPCError> {
pub fn decode_peer_info(reader: &veilid_capnp::peer_info::Reader) -> Result<PeerInfo, RPCError> {
let nids_reader = reader
.reborrow()
.get_node_ids()
@@ -43,12 +40,9 @@ pub fn decode_peer_info(
for nid_reader in nids_reader.iter() {
node_ids.add(decode_typed_key(&nid_reader)?);
}
let signed_node_info = decode_signed_node_info(&sni_reader, crypto, &mut node_ids)?;
let signed_node_info = decode_signed_node_info(&sni_reader)?;
if node_ids.len() == 0 {
return Err(RPCError::protocol("no verified node ids"));
}
Ok(PeerInfo {
node_ids,
signed_node_info,
})
Ok(PeerInfo::new(node_ids, signed_node_info))
}
@@ -67,10 +67,7 @@ pub fn encode_route_hop(
Ok(())
}
pub fn decode_route_hop(
reader: &veilid_capnp::route_hop::Reader,
crypto: Crypto,
) -> Result<RouteHop, RPCError> {
pub fn decode_route_hop(reader: &veilid_capnp::route_hop::Reader) -> Result<RouteHop, RPCError> {
let n_reader = reader.reborrow().get_node();
let node = match n_reader.which().map_err(RPCError::protocol)? {
veilid_capnp::route_hop::node::Which::NodeId(ni) => {
@@ -80,7 +77,7 @@ pub fn decode_route_hop(
veilid_capnp::route_hop::node::Which::PeerInfo(pi) => {
let pi_reader = pi.map_err(RPCError::protocol)?;
RouteNode::PeerInfo(
decode_peer_info(&pi_reader, crypto)
decode_peer_info(&pi_reader)
.map_err(RPCError::map_protocol("invalid peer info in route hop"))?,
)
}
@@ -128,7 +125,6 @@ pub fn encode_private_route(
pub fn decode_private_route(
reader: &veilid_capnp::private_route::Reader,
crypto: Crypto,
) -> Result<PrivateRoute, RPCError> {
let public_key = decode_typed_key(&reader.get_public_key().map_err(
RPCError::map_protocol("invalid public key in private route"),
@@ -138,7 +134,7 @@ pub fn decode_private_route(
let hops = match reader.get_hops().which().map_err(RPCError::protocol)? {
veilid_capnp::private_route::hops::Which::FirstHop(rh_reader) => {
let rh_reader = rh_reader.map_err(RPCError::protocol)?;
PrivateRouteHops::FirstHop(decode_route_hop(&rh_reader, crypto)?)
PrivateRouteHops::FirstHop(decode_route_hop(&rh_reader)?)
}
veilid_capnp::private_route::hops::Which::Data(rhd_reader) => {
let rhd_reader = rhd_reader.map_err(RPCError::protocol)?;
@@ -182,7 +178,6 @@ pub fn encode_safety_route(
pub fn decode_safety_route(
reader: &veilid_capnp::safety_route::Reader,
crypto: Crypto,
) -> Result<SafetyRoute, RPCError> {
let public_key = decode_typed_key(
&reader
@@ -197,7 +192,7 @@ pub fn decode_safety_route(
}
veilid_capnp::safety_route::hops::Which::Private(pr_reader) => {
let pr_reader = pr_reader.map_err(RPCError::protocol)?;
SafetyRouteHops::Private(decode_private_route(&pr_reader, crypto)?)
SafetyRouteHops::Private(decode_private_route(&pr_reader)?)
}
};
@@ -34,7 +34,6 @@ pub fn encode_signal_info(
pub fn decode_signal_info(
reader: &veilid_capnp::operation_signal::Reader,
crypto: Crypto,
) -> Result<SignalInfo, RPCError> {
Ok(
match reader
@@ -53,7 +52,7 @@ pub fn decode_signal_info(
let pi_reader = r.get_peer_info().map_err(RPCError::map_protocol(
"invalid peer info in hole punch signal info",
))?;
let peer_info = decode_peer_info(&pi_reader, crypto)?;
let peer_info = decode_peer_info(&pi_reader)?;
SignalInfo::HolePunch { receipt, peer_info }
}
@@ -69,7 +68,7 @@ pub fn decode_signal_info(
let pi_reader = r.get_peer_info().map_err(RPCError::map_protocol(
"invalid peer info in reverse connect signal info",
))?;
let peer_info = decode_peer_info(&pi_reader, crypto)?;
let peer_info = decode_peer_info(&pi_reader)?;
SignalInfo::ReverseConnect { receipt, peer_info }
}
@@ -6,20 +6,20 @@ pub fn encode_signed_direct_node_info(
) -> Result<(), RPCError> {
//
let mut ni_builder = builder.reborrow().init_node_info();
encode_node_info(&signed_direct_node_info.node_info, &mut ni_builder)?;
encode_node_info(signed_direct_node_info.node_info(), &mut ni_builder)?;
builder
.reborrow()
.set_timestamp(signed_direct_node_info.timestamp.into());
.set_timestamp(signed_direct_node_info.timestamp().into());
let mut sigs_builder = builder.reborrow().init_signatures(
signed_direct_node_info
.signatures
.signatures()
.len()
.try_into()
.map_err(RPCError::map_invalid_format("out of bound error"))?,
);
for (i, typed_signature) in signed_direct_node_info.signatures.iter().enumerate() {
for (i, typed_signature) in signed_direct_node_info.signatures().iter().enumerate() {
encode_typed_signature(
typed_signature,
&mut sigs_builder.reborrow().get(
@@ -34,8 +34,6 @@ pub fn encode_signed_direct_node_info(
pub fn decode_signed_direct_node_info(
reader: &veilid_capnp::signed_direct_node_info::Reader,
crypto: Crypto,
node_ids: &mut TypedKeySet,
) -> Result<SignedDirectNodeInfo, RPCError> {
let ni_reader = reader
.reborrow()
@@ -61,6 +59,9 @@ pub fn decode_signed_direct_node_info(
typed_signatures.push(typed_signature);
}
SignedDirectNodeInfo::new(crypto, node_ids, node_info, timestamp, typed_signatures)
.map_err(RPCError::protocol)
Ok(SignedDirectNodeInfo::new(
node_info,
timestamp,
typed_signatures,
))
}
@@ -20,8 +20,6 @@ pub fn encode_signed_node_info(
pub fn decode_signed_node_info(
reader: &veilid_capnp::signed_node_info::Reader,
crypto: Crypto,
node_ids: &mut TypedKeySet,
) -> Result<SignedNodeInfo, RPCError> {
match reader
.which()
@@ -29,12 +27,12 @@ pub fn decode_signed_node_info(
{
veilid_capnp::signed_node_info::Direct(d) => {
let d_reader = d.map_err(RPCError::protocol)?;
let sdni = decode_signed_direct_node_info(&d_reader, crypto, node_ids)?;
let sdni = decode_signed_direct_node_info(&d_reader)?;
Ok(SignedNodeInfo::Direct(sdni))
}
veilid_capnp::signed_node_info::Relayed(r) => {
let r_reader = r.map_err(RPCError::protocol)?;
let srni = decode_signed_relayed_node_info(&r_reader, crypto, node_ids)?;
let srni = decode_signed_relayed_node_info(&r_reader)?;
Ok(SignedNodeInfo::Relayed(srni))
}
}
@@ -6,16 +6,16 @@ pub fn encode_signed_relayed_node_info(
) -> Result<(), RPCError> {
//
let mut ni_builder = builder.reborrow().init_node_info();
encode_node_info(&signed_relayed_node_info.node_info, &mut ni_builder)?;
encode_node_info(signed_relayed_node_info.node_info(), &mut ni_builder)?;
let mut rids_builder = builder.reborrow().init_relay_ids(
signed_relayed_node_info
.relay_ids
.relay_ids()
.len()
.try_into()
.map_err(RPCError::map_invalid_format("out of bound error"))?,
);
for (i, typed_key) in signed_relayed_node_info.relay_ids.iter().enumerate() {
for (i, typed_key) in signed_relayed_node_info.relay_ids().iter().enumerate() {
encode_typed_key(
typed_key,
&mut rids_builder.reborrow().get(
@@ -26,20 +26,20 @@ pub fn encode_signed_relayed_node_info(
}
let mut ri_builder = builder.reborrow().init_relay_info();
encode_signed_direct_node_info(&signed_relayed_node_info.relay_info, &mut ri_builder)?;
encode_signed_direct_node_info(signed_relayed_node_info.relay_info(), &mut ri_builder)?;
builder
.reborrow()
.set_timestamp(signed_relayed_node_info.timestamp.into());
.set_timestamp(signed_relayed_node_info.timestamp().into());
let mut sigs_builder = builder.reborrow().init_signatures(
signed_relayed_node_info
.signatures
.signatures()
.len()
.try_into()
.map_err(RPCError::map_invalid_format("out of bound error"))?,
);
for (i, typed_signature) in signed_relayed_node_info.signatures.iter().enumerate() {
for (i, typed_signature) in signed_relayed_node_info.signatures().iter().enumerate() {
encode_typed_signature(
typed_signature,
&mut sigs_builder.reborrow().get(
@@ -54,8 +54,6 @@ pub fn encode_signed_relayed_node_info(
pub fn decode_signed_relayed_node_info(
reader: &veilid_capnp::signed_relayed_node_info::Reader,
crypto: Crypto,
node_ids: &mut TypedKeySet,
) -> Result<SignedRelayedNodeInfo, RPCError> {
let ni_reader = reader
.reborrow()
@@ -81,20 +79,7 @@ pub fn decode_signed_relayed_node_info(
.reborrow()
.get_relay_info()
.map_err(RPCError::protocol)?;
let relay_info = decode_signed_direct_node_info(&ri_reader, crypto.clone(), &mut relay_ids)?;
// Ensure the relay info for the node has a superset of the crypto kinds of the node it is relaying
if common_crypto_kinds(
&node_info.crypto_support,
&relay_info.node_info.crypto_support,
)
.len()
!= node_info.crypto_support.len()
{
return Err(RPCError::protocol(
"relay should have superset of node crypto kinds",
));
}
let relay_info = decode_signed_direct_node_info(&ri_reader)?;
let timestamp = reader.reborrow().get_timestamp().into();
@@ -113,14 +98,11 @@ pub fn decode_signed_relayed_node_info(
let typed_signature = decode_typed_signature(&sig_reader)?;
typed_signatures.push(typed_signature);
}
SignedRelayedNodeInfo::new(
crypto,
node_ids,
Ok(SignedRelayedNodeInfo::new(
node_info,
relay_ids,
relay_info,
timestamp,
typed_signatures,
)
.map_err(RPCError::protocol)
))
}
@@ -0,0 +1,31 @@
use super::*;
use crate::storage_manager::*;
pub fn encode_signed_value_data(
signed_value_data: &SignedValueData,
builder: &mut veilid_capnp::signed_value_data::Builder,
) -> Result<(), RPCError> {
builder.set_seq(signed_value_data.value_data().seq());
builder.set_data(signed_value_data.value_data().data());
let mut wb = builder.reborrow().init_writer();
encode_key256(signed_value_data.value_data().writer(), &mut wb);
let mut sb = builder.reborrow().init_signature();
encode_signature512(signed_value_data.signature(), &mut sb);
Ok(())
}
pub fn decode_signed_value_data(
reader: &veilid_capnp::signed_value_data::Reader,
) -> Result<SignedValueData, RPCError> {
let seq = reader.get_seq();
let data = reader.get_data().map_err(RPCError::protocol)?.to_vec();
let wr = reader.get_writer().map_err(RPCError::protocol)?;
let writer = decode_key256(&wr);
let sr = reader.get_signature().map_err(RPCError::protocol)?;
let signature = decode_signature512(&sr);
Ok(SignedValueData::new(
ValueData::new_with_seq(seq, data, writer),
signature,
))
}
@@ -0,0 +1,28 @@
use super::*;
use crate::storage_manager::SignedValueDescriptor;
pub fn encode_signed_value_descriptor(
signed_value_descriptor: &SignedValueDescriptor,
builder: &mut veilid_capnp::signed_value_descriptor::Builder,
) -> Result<(), RPCError> {
let mut ob = builder.reborrow().init_owner();
encode_key256(signed_value_descriptor.owner(), &mut ob);
builder.set_schema_data(signed_value_descriptor.schema_data());
let mut sb = builder.reborrow().init_signature();
encode_signature512(signed_value_descriptor.signature(), &mut sb);
Ok(())
}
pub fn decode_signed_value_descriptor(
reader: &veilid_capnp::signed_value_descriptor::Reader,
) -> Result<SignedValueDescriptor, RPCError> {
let or = reader.get_owner().map_err(RPCError::protocol)?;
let owner = decode_key256(&or);
let schema_data = reader
.get_schema_data()
.map_err(RPCError::protocol)?
.to_vec();
let sr = reader.get_signature().map_err(RPCError::protocol)?;
let signature = decode_signature512(&sr);
Ok(SignedValueDescriptor::new(owner, schema_data, signature))
}
@@ -1,18 +1,30 @@
use super::*;
pub fn encode_value_data(
value_data: &ValueData,
builder: &mut veilid_capnp::value_data::Builder,
pub fn encode_signed_value_data(
signed_value_data: &SignedValueData,
builder: &mut veilid_capnp::signed_value_data::Builder,
) -> Result<(), RPCError> {
builder.set_data(&value_data.data);
builder.set_schema(u32::from_be_bytes(value_data.schema.0));
builder.set_seq(value_data.seq);
builder.set_seq(signed_value_data.value_data().seq());
builder.set_data(signed_value_data.value_data().data());
let mut wb = builder.reborrow().init_writer();
encode_key256(signed_value_data.value_data().writer(), &mut wb);
let mut sb = builder.reborrow().init_signature();
encode_signature512(signed_value_data.signature(), &mut sb);
Ok(())
}
pub fn decode_value_data(reader: &veilid_capnp::value_data::Reader) -> Result<ValueData, RPCError> {
let data = reader.get_data().map_err(RPCError::protocol)?.to_vec();
pub fn decode_signed_value_data(
reader: &veilid_capnp::signed_value_data::Reader,
) -> Result<ValueData, RPCError> {
let seq = reader.get_seq();
let schema = FourCC::from(reader.get_schema().to_be_bytes());
Ok(ValueData { data, schema, seq })
let data = reader.get_data().map_err(RPCError::protocol)?.to_vec();
let wr = reader.get_writer().map_err(RPCError::protocol)?;
let writer = decode_key256(&wr);
let sr = reader.get_signature().map_err(RPCError::protocol)?;
let signature = decode_signature512(&sr);
Ok(SignedValueData {
value_data: ValueData { seq, data, writer },
signature,
})
}