cli fixes
This commit is contained in:
		| @@ -130,6 +130,7 @@ macro_rules! byte_array_type { | ||||
|                 Self { bytes } | ||||
|             } | ||||
|  | ||||
|             // Big endian bit ordering | ||||
|             pub fn bit(&self, index: usize) -> bool { | ||||
|                 assert!(index < ($size * 8)); | ||||
|                 let bi = index / 8; | ||||
| @@ -152,6 +153,7 @@ macro_rules! byte_array_type { | ||||
|                 None | ||||
|             } | ||||
|  | ||||
|             // Big endian nibble ordering | ||||
|             pub fn nibble(&self, index: usize) -> u8 { | ||||
|                 assert!(index < ($size * 2)); | ||||
|                 let bi = index / 2; | ||||
|   | ||||
| @@ -353,10 +353,40 @@ async fn test_operations(vcrypto: CryptoSystemVersion) { | ||||
|     assert_eq!(d4.first_nonzero_bit(), Some(0)); | ||||
| } | ||||
|  | ||||
| pub async fn test_crypto_key_ordering() { | ||||
|     let k1 = CryptoKey::new([ | ||||
|         128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||||
|         0, 0, | ||||
|     ]); | ||||
|     let k2 = CryptoKey::new([ | ||||
|         1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||||
|         0, 0, | ||||
|     ]); | ||||
|     let k3 = CryptoKey::new([ | ||||
|         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||||
|         0, 128, | ||||
|     ]); | ||||
|     let k4 = CryptoKey::new([ | ||||
|         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||||
|         0, 1, | ||||
|     ]); | ||||
|     let k5 = CryptoKey::new([ | ||||
|         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||||
|         0, 0, | ||||
|     ]); | ||||
|  | ||||
|     assert!(k2 < k1); | ||||
|     assert!(k3 < k2); | ||||
|     assert!(k4 < k3); | ||||
|     assert!(k5 < k4); | ||||
| } | ||||
|  | ||||
| pub async fn test_all() { | ||||
|     let api = crypto_tests_startup().await; | ||||
|     let crypto = api.crypto().unwrap(); | ||||
|  | ||||
|     test_crypto_key_ordering().await; | ||||
|  | ||||
|     // Test versions | ||||
|     for v in VALID_CRYPTO_KINDS { | ||||
|         let vcrypto = crypto.get(v).unwrap(); | ||||
|   | ||||
| @@ -51,6 +51,7 @@ impl RoutingTable { | ||||
|             return NetworkResult::invalid_message("unsupported cryptosystem"); | ||||
|         }; | ||||
|         let own_distance = vcrypto.distance(&own_node_id.value, &key.value); | ||||
|         let vcrypto2 = vcrypto.clone(); | ||||
|  | ||||
|         let filter = Box::new( | ||||
|             move |rti: &RoutingTableInner, opt_entry: Option<Arc<BucketEntry>>| { | ||||
| @@ -98,6 +99,46 @@ impl RoutingTable { | ||||
|             }, | ||||
|         ); | ||||
|  | ||||
|         // xxx test | ||||
|         // Validate peers returned are, in fact, closer to the key than the node we sent this to | ||||
|         let valid = match Self::verify_peers_closer(vcrypto2, own_node_id, key, &closest_nodes) { | ||||
|             Ok(v) => v, | ||||
|             Err(e) => { | ||||
|                 panic!("missing cryptosystem in peers node ids: {}", e); | ||||
|             } | ||||
|         }; | ||||
|         if !valid { | ||||
|             panic!("non-closer peers returned"); | ||||
|         } | ||||
|  | ||||
|         NetworkResult::value(closest_nodes) | ||||
|     } | ||||
|  | ||||
|     /// Determine if set of peers is closer to key_near than key_far | ||||
|     pub(crate) fn verify_peers_closer( | ||||
|         vcrypto: CryptoSystemVersion, | ||||
|         key_far: TypedKey, | ||||
|         key_near: TypedKey, | ||||
|         peers: &[PeerInfo], | ||||
|     ) -> EyreResult<bool> { | ||||
|         let kind = vcrypto.kind(); | ||||
|  | ||||
|         if key_far.kind != kind || key_near.kind != kind { | ||||
|             bail!("keys all need the same cryptosystem"); | ||||
|         } | ||||
|  | ||||
|         let mut closer = true; | ||||
|         for peer in peers { | ||||
|             let Some(key_peer) = peer.node_ids().get(kind) else { | ||||
|                 bail!("peers need to have a key with the same cryptosystem"); | ||||
|             }; | ||||
|             let d_near = vcrypto.distance(&key_near.value, &key_peer.value); | ||||
|             let d_far = vcrypto.distance(&key_far.value, &key_peer.value); | ||||
|             if d_far < d_near { | ||||
|                 closer = false; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         Ok(closer) | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -404,37 +404,6 @@ impl RPCProcessor { | ||||
|         routing_table.signed_node_info_is_valid_in_routing_domain(routing_domain, &signed_node_info) | ||||
|     } | ||||
|  | ||||
|     /// Determine if set of peers is closer to key_near than key_far | ||||
|     fn verify_peers_closer( | ||||
|         &self, | ||||
|         vcrypto: CryptoSystemVersion, | ||||
|         key_far: TypedKey, | ||||
|         key_near: TypedKey, | ||||
|         peers: &[PeerInfo], | ||||
|     ) -> Result<bool, RPCError> { | ||||
|         let kind = vcrypto.kind(); | ||||
|  | ||||
|         if key_far.kind != kind || key_near.kind != kind { | ||||
|             return Err(RPCError::internal("keys all need the same cryptosystem")); | ||||
|         } | ||||
|  | ||||
|         let mut closer = true; | ||||
|         for peer in peers { | ||||
|             let Some(key_peer) = peer.node_ids().get(kind) else { | ||||
|                 return Err(RPCError::invalid_format( | ||||
|                     "peers need to have a key with the same cryptosystem", | ||||
|                 )); | ||||
|             }; | ||||
|             let d_near = vcrypto.distance(&key_near.value, &key_peer.value); | ||||
|             let d_far = vcrypto.distance(&key_far.value, &key_peer.value); | ||||
|             if d_far < d_near { | ||||
|                 closer = false; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         Ok(closer) | ||||
|     } | ||||
|  | ||||
|     ////////////////////////////////////////////////////////////////////// | ||||
|  | ||||
|     /// Search the DHT for a single node closest to a key and add it to the routing table and return the node reference | ||||
|   | ||||
| @@ -76,15 +76,13 @@ impl RPCProcessor { | ||||
|         let (value, peers, descriptor) = get_value_a.destructure(); | ||||
|  | ||||
|         // Validate peers returned are, in fact, closer to the key than the node we sent this to | ||||
|         let valid = match self.verify_peers_closer(vcrypto, target_node_id, key, &peers) { | ||||
|         let valid = match RoutingTable::verify_peers_closer(vcrypto, target_node_id, key, &peers) { | ||||
|             Ok(v) => v, | ||||
|             Err(e) => { | ||||
|                 if matches!(e, RPCError::Internal(_)) { | ||||
|                     return Err(e); | ||||
|                 } | ||||
|                 return Ok(NetworkResult::invalid_message( | ||||
|                     "missing cryptosystem in peers node ids", | ||||
|                 )); | ||||
|                 return Ok(NetworkResult::invalid_message(format!( | ||||
|                     "missing cryptosystem in peers node ids: {}", | ||||
|                     e | ||||
|                 ))); | ||||
|             } | ||||
|         }; | ||||
|         if !valid { | ||||
|   | ||||
| @@ -83,15 +83,13 @@ impl RPCProcessor { | ||||
|         let (set, value, peers) = set_value_a.destructure(); | ||||
|  | ||||
|         // Validate peers returned are, in fact, closer to the key than the node we sent this to | ||||
|         let valid = match self.verify_peers_closer(vcrypto, target_node_id, key, &peers) { | ||||
|         let valid = match RoutingTable::verify_peers_closer(vcrypto, target_node_id, key, &peers) { | ||||
|             Ok(v) => v, | ||||
|             Err(e) => { | ||||
|                 if matches!(e, RPCError::Internal(_)) { | ||||
|                     return Err(e); | ||||
|                 } | ||||
|                 return Ok(NetworkResult::invalid_message( | ||||
|                     "missing cryptosystem in peers node ids", | ||||
|                 )); | ||||
|                 return Ok(NetworkResult::invalid_message(format!( | ||||
|                     "missing cryptosystem in peers node ids: {}", | ||||
|                     e | ||||
|                 ))); | ||||
|             } | ||||
|         }; | ||||
|         if !valid { | ||||
|   | ||||
| @@ -341,7 +341,6 @@ impl StorageManager { | ||||
|         } else { | ||||
|             ValueData::new(data, writer.key) | ||||
|         }; | ||||
|         let seq = value_data.seq(); | ||||
|  | ||||
|         // Validate with schema | ||||
|         if !schema.check_subkey_value_data(descriptor.owner(), subkey, &value_data) { | ||||
| @@ -374,7 +373,6 @@ impl StorageManager { | ||||
|         drop(inner); | ||||
|  | ||||
|         // Use the safety selection we opened the record with | ||||
|  | ||||
|         let final_signed_value_data = self | ||||
|             .outbound_set_value( | ||||
|                 rpc_processor, | ||||
| @@ -386,13 +384,12 @@ impl StorageManager { | ||||
|             ) | ||||
|             .await?; | ||||
|  | ||||
|         // If we got a new value back then write it to the opened record | ||||
|         if final_signed_value_data.value_data().seq() != seq { | ||||
|             let mut inner = self.lock().await?; | ||||
|             inner | ||||
|                 .handle_set_local_value(key, subkey, final_signed_value_data.clone()) | ||||
|                 .await?; | ||||
|         } | ||||
|         // Whatever record we got back, store it locally, might be newer than the one we asked to save | ||||
|         let mut inner = self.lock().await?; | ||||
|         inner | ||||
|             .handle_set_local_value(key, subkey, final_signed_value_data.clone()) | ||||
|             .await?; | ||||
|  | ||||
|         Ok(Some(final_signed_value_data.into_value_data())) | ||||
|     } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user