Skip to content

Commit

Permalink
Replace the bits256 with [u8; 32] in peers crate #423
Browse files Browse the repository at this point in the history
  • Loading branch information
artemii235 committed Jul 3, 2019
1 parent 4a10a6c commit 7c64c1d
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 63 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions mm2src/common/build.rs
Expand Up @@ -187,7 +187,6 @@ fn generate_bindings() {
"LP_quoteparse",
"LP_requestinit",
"LP_tradecommand_log",
"bits256_str", // cf. `impl fmt::Display for bits256`
"vcalc_sha256",
"calc_rmd160_sha256",
"bitcoin_address",
Expand Down Expand Up @@ -229,7 +228,6 @@ fn generate_bindings() {
.iter(),
// types
[
"_bits256",
"cJSON",
"iguana_info",
"LP_utxoinfo",
Expand Down
2 changes: 1 addition & 1 deletion mm2src/lp_native_dex.rs
Expand Up @@ -1053,7 +1053,7 @@ pub unsafe fn lp_initpeers (ctx: &MmArc, pubsock: i32, mut mypeer: *mut lp::LP_p
}
*try_s! (ctx.seeds.lock()) = seed_ips;

try_s! (peers::initialize (ctx, netid, lp::G.LP_mypub25519, pubport + 1));
try_s! (peers::initialize (ctx, netid, lp::G.LP_mypub25519.bytes, pubport + 1));

Ok(())
}
Expand Down
12 changes: 6 additions & 6 deletions mm2src/lp_swap.rs
Expand Up @@ -867,7 +867,7 @@ impl MakerSwap {
};

let bytes = serialize(&maker_negotiation_data);
let sending_f = match send!(&self.ctx, self.taker, fomat!(("negotiation") '@' (self.uuid)), 30, bytes.as_slice()) {
let sending_f = match send!(&self.ctx, unsafe {self.taker.bytes}, fomat!(("negotiation") '@' (self.uuid)), 30, bytes.as_slice()) {
Ok(f) => f,
Err(e) => return Ok((
Some(MakerSwapCommand::Finish),
Expand Down Expand Up @@ -903,7 +903,7 @@ impl MakerSwap {

fn wait_taker_fee(&self) -> Result<(Option<MakerSwapCommand>, Vec<MakerSwapEvent>), String> {
let negotiated = serialize(&true);
let sending_f = match send!(&self.ctx, self.taker, fomat!(("negotiated") '@' (self.uuid)), 30, negotiated.as_slice()) {
let sending_f = match send!(&self.ctx, unsafe{self.taker.bytes}, fomat!(("negotiated") '@' (self.uuid)), 30, negotiated.as_slice()) {
Ok(f) => f,
Err(e) => return Ok((
Some(MakerSwapCommand::Finish),
Expand Down Expand Up @@ -1034,7 +1034,7 @@ impl MakerSwap {

fn wait_for_taker_payment(&self) -> Result<(Option<MakerSwapCommand>, Vec<MakerSwapEvent>), String> {
let maker_payment_hex = self.maker_payment.as_ref().unwrap().tx_hex.clone();
let sending_f = match send!(&self.ctx, self.taker, fomat!(("maker-payment") '@' (self.uuid)), 60, maker_payment_hex) {
let sending_f = match send!(&self.ctx, unsafe {self.taker.bytes}, fomat!(("maker-payment") '@' (self.uuid)), 60, maker_payment_hex) {
Ok(f) => f,
Err(e) => return Ok((
Some(MakerSwapCommand::RefundMakerPayment),
Expand Down Expand Up @@ -1656,7 +1656,7 @@ impl TakerSwap {
persistent_pubkey: self.my_persistent_pub.clone(),
};
let bytes = serialize(&taker_data);
let sending_f = match send! (&self.ctx, self.maker, fomat!(("negotiation-reply") '@' (self.uuid)), 30, bytes.as_slice()) {
let sending_f = match send! (&self.ctx, unsafe{self.maker.bytes}, fomat!(("negotiation-reply") '@' (self.uuid)), 30, bytes.as_slice()) {
Ok(f) => f,
Err(e) => return Ok((
Some(TakerSwapCommand::Finish),
Expand Down Expand Up @@ -1738,7 +1738,7 @@ impl TakerSwap {

fn wait_for_maker_payment(&self) -> Result<(Option<TakerSwapCommand>, Vec<TakerSwapEvent>), String> {
let tx_hex = self.taker_fee.as_ref().unwrap().tx_hex.clone();
let sending_f = match send! (&self.ctx, self.maker, fomat!(("taker-fee") '@' (self.uuid)), 60, tx_hex) {
let sending_f = match send! (&self.ctx, unsafe {self.maker.bytes}, fomat!(("taker-fee") '@' (self.uuid)), 60, tx_hex) {
Ok(f) => f,
Err (err) => return Ok((
Some(TakerSwapCommand::Finish),
Expand Down Expand Up @@ -1883,7 +1883,7 @@ impl TakerSwap {

fn wait_for_taker_payment_spend(&self) -> Result<(Option<TakerSwapCommand>, Vec<TakerSwapEvent>), String> {
let tx_hex = self.taker_payment.as_ref().unwrap().tx_hex.clone();
let sending_f = match send! (&self.ctx, self.maker, fomat!(("taker-payment") '@' (self.uuid)), 60, tx_hex) {
let sending_f = match send! (&self.ctx, unsafe {self.maker.bytes}, fomat!(("taker-payment") '@' (self.uuid)), 60, tx_hex) {
Ok(f) => f,
Err(e) => return Ok((
Some(TakerSwapCommand::RefundTakerPayment),
Expand Down
1 change: 1 addition & 0 deletions mm2src/peers/Cargo.toml
Expand Up @@ -27,6 +27,7 @@ fomat-macros = "0.2"
futures = "0.1"
gstuff = "0.5"
hashbrown = {version = "0.1.8", features = ["serde", "nightly"]}
hex = "0.3.2"
http = "0.1"
http-body = "0.1"
hyper = { version = "0.12", optional = true }
Expand Down
20 changes: 10 additions & 10 deletions mm2src/peers/http_fallback.rs
Expand Up @@ -29,7 +29,7 @@ use zstd_sys::{ZSTD_CDict, ZSTD_createCDict_byReference, ZSTD_freeCDict, ZSTD_co
ZSTD_frameParameters, ZSTD_createCCtx, ZSTD_freeCCtx, ZSTD_isError, ZSTD_compressBound,
ZSTD_createDCtx, ZSTD_freeDCtx, ZSTD_DDict, ZSTD_createDDict, ZSTD_freeDDict, ZSTD_decompress_usingDDict};

use common::{bits256, binprint, rpc_response, HyRes};
use common::{binprint, rpc_response, HyRes};
use common::wio::{slurp_req};
#[cfg(feature = "native")]
use common::wio::{CORE, HTTP};
Expand Down Expand Up @@ -375,7 +375,7 @@ pub struct HttpFallbackTargetTrack {

/// Plugged into `fn transmit` to send the chunks via HTTP fallback when necessary.
#[cfg(feature = "native")]
pub fn hf_transmit (pctx: &super::PeersContext, hf_addr: &Option<SocketAddr>, our_public_key: &bits256,
pub fn hf_transmit (pctx: &super::PeersContext, hf_addr: &Option<SocketAddr>, our_public_key: &[u8; 32],
packages: &mut Vec<super::Package>) -> Result<(), String> {
let hf_addr = match hf_addr {Some (a) => a, None => return Ok(())};

Expand Down Expand Up @@ -429,7 +429,7 @@ pub fn hf_transmit (pctx: &super::PeersContext, hf_addr: &Option<SocketAddr>, ou

for (salt, (payload, _meta)) in deliver_to_seed {
// We're only sending our own packages here.
assert_eq! (&payload.from[..], unsafe {&our_public_key.bytes[..]});
assert_eq! (payload.from, our_public_key);

let saltᵊ = base64::encode_config (&salt, base64::STANDARD_NO_PAD);
let chunk = if let Some (ref chunk) = payload.chunk {
Expand Down Expand Up @@ -460,10 +460,10 @@ pub fn hf_transmit (pctx: &super::PeersContext, hf_addr: &Option<SocketAddr>, ou
// TODO: Patch the integration test to use an alternative method of checking if the fallback has worked.
log! ("transmit] TBD, time to use the HTTP fallback...");

let mut hf_id = Vec::with_capacity (unsafe {seed.bytes.len() + 1 + our_public_key.bytes.len()});
hf_id.extend_from_slice (unsafe {&seed.bytes});
let mut hf_id = Vec::with_capacity (seed.len() + 1 + our_public_key.len());
hf_id.extend_from_slice (&seed);
hf_id.push (b'<');
hf_id.extend_from_slice (unsafe {&our_public_key.bytes});
hf_id.extend_from_slice (&our_public_key[..]);
let merge_f = merge_map (&hf_addr, hf_id, &track.rep_map);
let merge_f = merge_f.then (|r| -> Result<(), ()> {
let _merged_rep_map = match r {
Expand All @@ -480,7 +480,7 @@ log! ("transmit] TBD, time to use the HTTP fallback...");
}

#[cfg(not(feature = "native"))]
pub fn hf_transmit (_pctx: &super::PeersContext, _hf_addr: &Option<SocketAddr>, _our_public_key: &bits256,
pub fn hf_transmit (_pctx: &super::PeersContext, _hf_addr: &Option<SocketAddr>, _our_public_key: &[u8; 32],
_packages: &mut Vec<super::Package>) -> Result<(), String> {ERR! ("not implemented")}

/// Invoked when a delayed retrieval is detected by the peers loop.
Expand Down Expand Up @@ -531,9 +531,9 @@ fn process_pulled_maps (pctx: &Arc<super::PeersContext>, status: StatusCode, _he

if hf_id.len() != 65 || hf_id[32] != b'<' {return ERR! ("Bad hf_id: {}", binprint (hf_id, b'.'))}
let to = &hf_id[0..32];
if unsafe {to != &our_public_key.bytes[..]} {return ERR! ("Bad to: {}", binprint (to, b'.'))}
if to != our_public_key {return ERR! ("Bad to: {}", binprint (to, b'.'))}
let from = &hf_id[33..]; // The public key of the sender.
let from = bits256 {bytes: *array_ref! (from, 0, 32)};
let from = *array_ref! (from, 0, 32);
// TODO: See if we can verify that payload is coming from `from`.

let rep_map: RepStrMap = try_s! (json::from_slice (rep_map));
Expand Down Expand Up @@ -606,7 +606,7 @@ pub fn hf_poll (pctx: &Arc<super::PeersContext>, hf_addr: &Option<SocketAddr>) -
let mut hf_id_prefix = Vec::with_capacity (1 + 4 + 32 + 1);
hf_id_prefix.push (1); // Version of the query protocol.
try_s! (hf_id_prefix.write_u64::<BigEndian> (pctx.hf_last_poll_id.load (Ordering::Relaxed)));
hf_id_prefix.extend_from_slice (&unsafe {try_s! (pctx.our_public_key.lock()) .bytes} [..]);
hf_id_prefix.extend_from_slice (&try_s! (pctx.our_public_key.lock()) [..]);
hf_id_prefix.push (b'<');

let hf_url = fallback_url (hf_addr, "fetch_maps_by_prefix");
Expand Down

0 comments on commit 7c64c1d

Please sign in to comment.