Alice's spot price logic into dedicated behaviour

Move Alice's spot price logic into a dedicated network behaviour that handles all the logic.
The new behaviour encapsulates the complete state necessary for spot price request decision making.
The network behaviour cannot handle asynchronous calls, thus the balance is managed inside the spot price and has to updated regularly from the outside to ensure the spot price balance check has up to date data.
At the moment the balance is updated upon an incoming quote requests.

Code that is relevant for both ASB and CLI remains in the `network::spot_price` module (e.g. `network::spot_price::Error`).
This commit is contained in:
Daniel Karzel 2021-05-04 14:57:50 +10:00
parent ea76ae5821
commit 52f648e1de
No known key found for this signature in database
GPG key ID: 30C3FC2E438ADB6E
8 changed files with 304 additions and 126 deletions

View file

@ -1,6 +1,8 @@
use crate::monero;
use crate::network::quote::BidQuote;
use crate::network::{encrypted_signature, quote, spot_price, transfer_proof};
use crate::protocol::alice::{execution_setup, State3};
use crate::network::{encrypted_signature, quote, transfer_proof};
use crate::protocol::alice::event_loop::LatestRate;
use crate::protocol::alice::{execution_setup, spot_price, State3};
use anyhow::{anyhow, Error};
use libp2p::request_response::{RequestId, ResponseChannel};
use libp2p::{NetworkBehaviour, PeerId};
@ -8,10 +10,10 @@ use uuid::Uuid;
#[derive(Debug)]
pub enum OutEvent {
SpotPriceRequested {
request: spot_price::Request,
channel: ResponseChannel<spot_price::Response>,
ExecutionSetupStart {
peer: PeerId,
btc: bitcoin::Amount,
xmr: monero::Amount,
},
QuoteRequested {
channel: ResponseChannel<BidQuote>,
@ -60,19 +62,34 @@ impl OutEvent {
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "OutEvent", event_process = false)]
#[allow(missing_debug_implementations)]
pub struct Behaviour {
pub struct Behaviour<LR: LatestRate + Send + 'static> {
pub quote: quote::Behaviour,
pub spot_price: spot_price::Behaviour,
pub spot_price: spot_price::Behaviour<LR>,
pub execution_setup: execution_setup::Behaviour,
pub transfer_proof: transfer_proof::Behaviour,
pub encrypted_signature: encrypted_signature::Behaviour,
}
impl Default for Behaviour {
fn default() -> Self {
impl<LR> Behaviour<LR>
where
LR: LatestRate + Send + 'static,
{
pub fn new(
balance: monero::Amount,
lock_fee: monero::Amount,
max_buy: bitcoin::Amount,
latest_rate: LR,
resume_only: bool,
) -> Self {
Self {
quote: quote::alice(),
spot_price: spot_price::alice(),
spot_price: spot_price::Behaviour::new(
balance,
lock_fee,
max_buy,
latest_rate,
resume_only,
),
execution_setup: Default::default(),
transfer_proof: transfer_proof::alice(),
encrypted_signature: encrypted_signature::alice(),