Skip to content

Commit

Permalink
Simplify payment system, remove chat-related code and clear session m…
Browse files Browse the repository at this point in the history
…ethod
  • Loading branch information
nullchinchilla committed Nov 21, 2024
1 parent 028f6da commit cd27d74
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 445 deletions.
2 changes: 1 addition & 1 deletion refactor.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ Instead, once we have "xirtam" working, we can have a semi-official community of
## Action plan

- [x] Completely remove chat
- [ ] Implement storage system for debts
- [x] Implement storage system for debts
- [ ] Test
- [ ] Implement payment system
2 changes: 1 addition & 1 deletion src/link_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub use link_store::*;

use netgraph::NetGraph;

pub use payment_system::{Dummy, OnChain, PaymentSystem, PoW};
pub use payment_system::{Dummy, PaymentSystem};

use rand::seq::SliceRandom;
use relay_proc::{RelayMsg, RelayProcess};
Expand Down
2 changes: 0 additions & 2 deletions src/link_node/link_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ pub struct InfoResponse {
/// Errors that can occur during a Link RPC call.
#[derive(Error, Debug, Serialize, Deserialize)]
pub enum LinkRpcErr {
#[error("push chat failed")]
PushChatFailed,
#[error("invalid payment proof")]
InvalidPaymentProof,
#[error("unaccepted payment system")]
Expand Down
80 changes: 18 additions & 62 deletions src/link_node/payment_system.rs
Original file line number Diff line number Diff line change
@@ -1,75 +1,31 @@
mod dummy;
mod onchain;
mod pow;

use std::collections::HashMap;

use async_trait::async_trait;
use bytes::Bytes;
use serde::{Deserialize, Serialize};

use super::types::NeighborId;
pub use dummy::Dummy;
pub use onchain::OnChain;
pub use pow::PoW;

#[async_trait]
/// A trait that all payment systems implement. A payment system can be understood a
pub trait PaymentSystem: Send + Sync + 'static {
/// `amount` is in micromel. Returns proof of payment
async fn pay(
&self,
my_id: NeighborId,
to: &str,
amount: u64,
payment_id: &str,
) -> anyhow::Result<String>;

/// returns Some(payment_id) if payment is valid, None otherwise
async fn verify_payment(
&self,
from: NeighborId,
amount: u64,
proof: &str,
) -> anyhow::Result<Option<String>>;
async fn new_payment(&self, info: PaymentInfo) -> anyhow::Result<PaymentProof>;
async fn verify_proof(&self, proof: PaymentProof) -> anyhow::Result<PaymentInfo>;

fn my_addr(&self) -> String;

fn name(&self) -> String;

/// max amount a payment can be, in micromels
fn max_granularity(&self) -> u64;
}

pub struct PaymentSystemSelector {
inner: HashMap<String, Box<dyn PaymentSystem>>,
fn protocol_name(&self) -> String;
}

impl PaymentSystemSelector {
pub fn new() -> Self {
Self {
inner: HashMap::new(),
}
}

pub fn get(&self, payment_system: &str) -> Option<&dyn PaymentSystem> {
self.inner.get(payment_system).map(|s| s.as_ref())
}

pub fn insert(&mut self, payment_system: Box<dyn PaymentSystem>) {
self.inner.insert(payment_system.name(), payment_system);
}

pub fn get_available(&self) -> Vec<(String, String)> {
self.inner
.iter()
.map(|(name, ps)| (name.clone(), ps.my_addr()))
.collect()
}

pub fn select(&self, name_addrs: &[(String, String)]) -> Option<(&dyn PaymentSystem, String)> {
for (name, addr) in name_addrs {
if let Some(ret) = self.get(name) {
return Some((ret, addr.to_string()));
}
}
None
}
/// The proof that a payment happened. This is an opaque bytestring that must be encoded and decoded by system-specific code, and which must carry enough information to verify the payment and reconstruct the original PaymentInfo.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(transparent)]
pub struct PaymentProof(pub Bytes);

/// The information needed to create a new payment.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PaymentInfo {
pub dst_addr: String,
pub src_addr: String,
pub amount_micromel: u64,
pub nonce: u64,
}
40 changes: 8 additions & 32 deletions src/link_node/payment_system/dummy.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

use anyhow::Context;
use async_trait::async_trait;
use stdcode::StdcodeSerializeExt;

use crate::NeighborId;

use super::PaymentSystem;
use super::{PaymentInfo, PaymentProof, PaymentSystem};

#[derive(Clone)]
pub struct Dummy {
Expand All @@ -26,42 +25,19 @@ impl Default for Dummy {

#[async_trait]
impl PaymentSystem for Dummy {
async fn pay(
&self,
my_id: NeighborId,
to: &str,
amount: u64,
payment_id: &str,
) -> anyhow::Result<String> {
let proof =
serde_json::to_string(&(format!("{my_id},{to},{amount}"), payment_id.to_string()))?;
// smol::Timer::after(Duration::from_secs(100)).await;
Ok(proof)
async fn new_payment(&self, info: PaymentInfo) -> anyhow::Result<PaymentProof> {
Ok(PaymentProof(info.stdcode().into()))
}

async fn verify_payment(
&self,
from: NeighborId,
amount: u64,
proof: &str,
) -> anyhow::Result<Option<String>> {
let (proof, payment_id): (String, String) = serde_json::from_str(proof)?;
if proof == format!("{from},{},{amount}", self.my_addr) {
Ok(Some(payment_id))
} else {
Ok(None)
}
async fn verify_proof(&self, proof: PaymentProof) -> anyhow::Result<PaymentInfo> {
stdcode::deserialize(&proof.0).context("invalid proof")
}

fn my_addr(&self) -> String {
self.my_addr.to_string()
}

fn name(&self) -> String {
fn protocol_name(&self) -> String {
"dummy".to_string()
}

fn max_granularity(&self) -> u64 {
u64::MAX
}
}
147 changes: 0 additions & 147 deletions src/link_node/payment_system/onchain.rs

This file was deleted.

Loading

0 comments on commit cd27d74

Please sign in to comment.