4ca40fd3bc
* Starting on cosmwasm smart contracts * Mixnet contract now builds * Removing license and notice files, the monorepo already has these. * Removing generated README content * Simplified development instructions a bit. * Converted some network monitor files to use SPDX license headers * Renamed packaget to mixnet-contracts * Depending on the Nym topology crate * Renaming contract package in usage * Renamed "announce" to "register_node" in the defined messages * Fixed package name for mixnet contracts in defined release annotations * Added the mixnet contracts to the Cargo lock file * Renamed some fields in our contract topology * Using the stringy mixnode from the validator client. * Removing mix nodes count from state, we can infer that * Saving generated code in comment as it's a useful example for now * Renamed "count" to "get_topology" * Adding the beginnings of a validator client (in Typescript) * Starting to integrate example code. WIP. * Ignoring generated accounts * Making a few less mixnodes :) * Adding shebang to start script * Cranking up the Nym-related gas limits, as otherwise contract upload fails. * Simplest mixnode example is now working * Removing the external client code, it messed up wasm compilation. Will copy/paste for now. * Contract now wants to add a MixNode rather than an IP string * Adding mixnodes via contract now works (!) * Simplified mixnode registration example * Further mixnode-adding simplification. * Adding author name * Fixed description * Sent funds are now required to bond a mixnode * Ensuring that we send correct coin denomination * Unbonding now works (!). Quite primitivist. * Checking that unbonding works from the client. * Setting up a thief account to play with * Checking to see whether thief can unbond a node (it fails, happily) * Adding a more specific error for when an account attempts to unbond but owns no bonds * Figured out how to test contract balances * Set the console messages to explain things a little more nicely * Tests for insufficient funds result * Using more async in driver example * Added a bit more explanation of the actions taken by the driver example * Locking down wasm instantiate a bit more * Docs clarifications on how to run example * README clarification * Corrected the commit hash in the wasmd build command, it was still set for 0.14.0 * Moved models from types into state * Starting work on range queries * whitespace * Going back to slow but reliabel node uploads and disabling new contract upload * Cranking gas fees temporarily * Mixnode key retrieval working and tested * Range retrievals now working well * Removing unused clone * Compressing tests a bit * Testing node retrieval on large numbers of nodes. Not sure whether MockStorage has the same space limitations as production storage does. * Getting rid of spelling warning * Removing unused responses * Minor cleanup * Starting to map my way out of the tuples * Slightly more meaningful variable names * Returning a StdResult from nodes query * Fighting through the unwraps :) * Unfucking a bit more * Starting to use ranged nodes in contract * Testing node retrieval from range store * Ditching generated tests * Adding works, still need to test removing mixnodes * Attempting to remove a mixnode returns an error when no nodes exist * Un-registering when no accounts exist (edit) * un-registering someone else's mixnode fails * Ensuring proper ownership * Testing for only 1 mixnode getting deleted * Testing single-node retrieval * Removing mixnode working * Removed unused imports and unused variable warnings * Made handler functions private * Tested for error response on mixnode removal * Ensured proper post-state on mixnode removal * Using Vec<Coin> for currency equality comparisons * Removed todo, this amount is only for logging purposes anyway * Refactoring tests a bit * Adding a few storytelling comments * Putting helper methods into alphabetical order * Drying up mixnode adding in tests * Using the new add_mixnode helper * Checking full object equality in test * Removing the GetNodes handler * Taking a more "storytelling" approach to the contract tests * We need a few more methods to run our example driver * We now need to make a new address for each node we want to have, as each sending account can only have one node * HumanAddr not needed * Making call sequence a little more readable * Added the results of today's experiments with the REST API to the validator client readme * Corrected console.log message * Adding a note about how to run tests * More contract exercising fun * Updating mocha * Whitespace * Adding a note about running tests * Adding typed rest client * Starting to mess with typescript paging client * Removing the rest client, we'll use the cosmjs one for this * Noting a few more contract requirements * Starting client restructuring * Importing cosmjs stargate client * Starting to work on the chain cache * Cleanup * Removing type annotations which hilariously worked, confusing the compiler * Might as well do each cache individually * Renaming chaincache so that it handles only mixnodes * Renaming chaincache * Setting dynamic per-page value to ease testing * Using perPage in tests * Moving tests back into their own special home so they don't bloat our package * Ignoring generated docs * Adding TypeDoc documentation generator * Removing unused NetClient import * Added docs generation * Noting existence of docs generation * Starting to test paged responses * Working paging tests * Clarified test names a bit * Removed console.logs * Added a test for two full pages. * Formatting * Starting to query for mix nodes * Removed the topology in preparation for paging * Removing unused struct * Getting ready for series-based paging * We're now setting page size limits on list retrievals * Pagination starting to work, needs more testing * Moved test support stuff into its own home * Removing duplicate testy code * Testing all paging stuff in the contract * Removed useless method duplicate * Moving queries into their own file * Removing redundant tests * Testing default paging limit * Testing max paging limit * We don't need to c/p pagination stuff from the cw-plus contracts, removing * Testing pagination * Making next key calculation explict via a function * Removing temporary variable * Commenting final state * Incorporating the PagedResponse * On the road to a working TypeScript client * Adding some logging utilities * Paged retrieval working but needs improvement - it's very brittle * Getting the loop right * Removing unused logger * Setting up a request count * Documenting the ins and outs of the client network interface * Removing requestCount as we're not using it yet * Success! Making paginated requests for mixnodes! * Differentiating between MixNode and MixNodeBond * Checking that Fred can upload a mixnode * Fixing export * Adding the ability for client to get balances * Docs fix * Converting interfaces to types * Changing `mixNodes()` to `getMixNodes()` on client * We might as well return the nodes we've just retrieved when we refresh * Starting work on unbonding * Fixed a caching bug which was causing multiple result sets to be cached * Using the sender address as the key for removal * Importing some result stuff so we can find out what happened on execution * Minor messing around to prove that the sequence fully works * Displaying a nicer message on mixnode unbond * Renamed announce to bond in validator client * Fixed unstable clippy warnings * Removing commented fields * Comment spacing * Changed announce to bond in example code * Making the test accounts directory configurable * Rebuilt * Loading keys from the local ./accounts directory * Ignoring contract lockfile * Saving out a contract lockfile so things continue working after contract upload * Splitting the driver example into smaller self-contained examples * Deleting the example that Andrew hates so much * Making dependabot happy * Stricter equals * Removing unused import
118 lines
3.4 KiB
Rust
118 lines
3.4 KiB
Rust
use crate::ConnectionId;
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
pub enum ResponseError {
|
|
ConnectionIdTooShort,
|
|
NoData,
|
|
}
|
|
/// A remote network response retrieved by the Socks5 service provider. This
|
|
/// can be serialized and sent back through the mixnet to the requesting
|
|
/// application.
|
|
#[derive(Debug)]
|
|
pub struct Response {
|
|
pub data: Vec<u8>,
|
|
pub connection_id: ConnectionId,
|
|
pub is_closed: bool,
|
|
}
|
|
|
|
impl Response {
|
|
/// Constructor for responses
|
|
pub fn new(connection_id: ConnectionId, data: Vec<u8>, is_closed: bool) -> Self {
|
|
Response {
|
|
data,
|
|
connection_id,
|
|
is_closed,
|
|
}
|
|
}
|
|
|
|
pub fn try_from_bytes(b: &[u8]) -> Result<Response, ResponseError> {
|
|
if b.is_empty() {
|
|
return Err(ResponseError::NoData);
|
|
}
|
|
|
|
let is_closed = b[0] != 0;
|
|
|
|
if b.len() < 9 {
|
|
return Err(ResponseError::ConnectionIdTooShort);
|
|
}
|
|
|
|
let mut connection_id_bytes = b.to_vec();
|
|
let data = connection_id_bytes.split_off(9);
|
|
|
|
let connection_id = u64::from_be_bytes([
|
|
connection_id_bytes[1],
|
|
connection_id_bytes[2],
|
|
connection_id_bytes[3],
|
|
connection_id_bytes[4],
|
|
connection_id_bytes[5],
|
|
connection_id_bytes[6],
|
|
connection_id_bytes[7],
|
|
connection_id_bytes[8],
|
|
]);
|
|
|
|
let response = Response::new(connection_id, data, is_closed);
|
|
Ok(response)
|
|
}
|
|
|
|
/// Serializes the response into bytes so that it can be sent back through
|
|
/// the mixnet to the requesting application.
|
|
pub fn into_bytes(self) -> Vec<u8> {
|
|
std::iter::once(self.is_closed as u8)
|
|
.chain(self.connection_id.to_be_bytes().iter().cloned())
|
|
.chain(self.data.into_iter())
|
|
.collect()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod constructing_socks5_responses_from_bytes {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn fails_when_zero_bytes_are_supplied() {
|
|
let response_bytes = Vec::new();
|
|
|
|
assert_eq!(
|
|
ResponseError::NoData,
|
|
Response::try_from_bytes(&response_bytes).unwrap_err()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn fails_when_connection_id_bytes_are_too_short() {
|
|
let response_bytes = vec![0, 1, 2, 3, 4, 5, 6];
|
|
assert_eq!(
|
|
ResponseError::ConnectionIdTooShort,
|
|
Response::try_from_bytes(&response_bytes).unwrap_err()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn works_when_there_is_no_data() {
|
|
let response_bytes = vec![0, 0, 1, 2, 3, 4, 5, 6, 7];
|
|
let expected = Response::new(
|
|
u64::from_be_bytes([0, 1, 2, 3, 4, 5, 6, 7]),
|
|
Vec::new(),
|
|
false,
|
|
);
|
|
let actual = Response::try_from_bytes(&response_bytes).unwrap();
|
|
assert_eq!(expected.connection_id, actual.connection_id);
|
|
assert_eq!(expected.data, actual.data);
|
|
assert_eq!(expected.is_closed, actual.is_closed);
|
|
}
|
|
|
|
#[test]
|
|
fn works_when_there_is_data() {
|
|
let response_bytes = vec![0, 0, 1, 2, 3, 4, 5, 6, 7, 255, 255, 255];
|
|
let expected = Response::new(
|
|
u64::from_be_bytes([0, 1, 2, 3, 4, 5, 6, 7]),
|
|
vec![255, 255, 255],
|
|
false,
|
|
);
|
|
let actual = Response::try_from_bytes(&response_bytes).unwrap();
|
|
assert_eq!(expected.connection_id, actual.connection_id);
|
|
assert_eq!(expected.data, actual.data);
|
|
assert_eq!(expected.is_closed, actual.is_closed);
|
|
}
|
|
}
|