Skip to content

Commit

Permalink
paymint + middlewares
Browse files Browse the repository at this point in the history
cors and mint pay method + scratch of invoice handler
  • Loading branch information
StringNick committed Sep 12, 2024
1 parent 5cadccd commit 8373611
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/core/mint/mint.zig
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,21 @@ pub const Mint = struct {
.keysets = keysets,
};
}

/// Flag mint quote as paid
pub fn payMintQuoteForRequestId(
self: *Mint,
request_lookup_id: []const u8,
) !void {
const mint_quote = (try self.localstore.value.getMintQuoteByRequestLookupId(self.allocator, request_lookup_id)) orelse return;

std.log.debug("Quote {any} paid by lookup id {s}", .{
mint_quote,
request_lookup_id,
});

_ = try self.localstore.value.updateMintQuoteState(mint_quote.id, .paid);
}
};

/// Generate new [`MintKeySetInfo`] from path
Expand Down
13 changes: 10 additions & 3 deletions src/fake_wallet/fake_wallet.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub const FakeWallet = struct {
const Self = @This();

fee_reserve: core.mint.FeeReserve = .{},
chan: *Channel([]const u8) = undefined, // we using signle channel for sending invoices
chan: *Channel(std.ArrayList(u8)) = undefined, // we using signle channel for sending invoices
mint_settings: MintMeltSettings = .{},
melt_settings: MintMeltSettings = .{},

Expand All @@ -39,7 +39,7 @@ pub const FakeWallet = struct {
melt_settings: MintMeltSettings,
) !FakeWallet {
return .{
.chan = try Channel([]const u8).init(allocator, 0),
.chan = try Channel(std.ArrayList(u8)).init(allocator, 0),
.fee_reserve = fee_reserve,
.mint_settings = mint_settings,
.melt_settings = melt_settings,
Expand All @@ -62,7 +62,7 @@ pub const FakeWallet = struct {
// Result is channel with invoices, caller must free result
pub fn waitAnyInvoice(
self: *const Self,
) !Channel([]const u8).Rx {
) !Channel(std.ArrayList(u8)).Rx {
return self.chan.getRx();
}

Expand Down Expand Up @@ -183,6 +183,13 @@ pub const FakeWallet = struct {
}
}).sign);

// Create a random delay between 3 and 6 seconds
const duration = std.crypto.random.intRangeLessThanBiased(u64, 3, 7);
_ = duration; // autofix

// let sender = self.sender.clone();
// let label_clone = label.clone();

_ = label; // autofix
_ = self; // autofix
}
Expand Down
45 changes: 45 additions & 0 deletions src/mint.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ const MintDatabase = core.mint_memory.MintMemoryDatabase;
const ContactInfo = core.nuts.ContactInfo;
const MintVersion = core.nuts.MintVersion;
const MintInfo = core.nuts.MintInfo;
const Channel = @import("channels/channels.zig").Channel;

const default_quote_ttl_secs: u64 = 1800;

/// Update mint quote when called for a paid invoice
fn handlePaidInvoice(mint: *Mint, request_lookup_id: []const u8) !void {
std.log.debug("Invoice with lookup id paid: {s}", .{request_lookup_id});

try mint.payMintQuoteForRequestId(request_lookup_id);
}

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{
.stack_trace_frames = 20,
Expand Down Expand Up @@ -194,6 +202,13 @@ pub fn main() !void {
var srv = try router.createMintServer(gpa.allocator(), mint_url, &mint, ln_backends, quote_ttl, .{
.port = listen_port,
.address = listen_addr,
}, &.{
.{
httpz.middleware.Cors, .{
.origin = "*",
.headers = "*",
},
},
});
defer srv.deinit();

Expand All @@ -202,6 +217,36 @@ pub fn main() !void {

// Spawn task to wait for invoces to be paid and update mint quotes
// handle invoices
const threads = v: {
var threads = try std.ArrayList(std.Thread).initCapacity(gpa.allocator(), ln_backends.count());
errdefer threads.deinit();
errdefer for (threads.items) |t| t.detach();

const thread_fn = (struct {
fn handleLnInvoice(m: *Mint, wait_ch: Channel(std.ArrayList(u8)).Rx) void {
while (true) {
var request_lookup_id = wait_ch.recv();
defer request_lookup_id.deinit();

handlePaidInvoice(m, request_lookup_id.items) catch |err| {
std.log.warn("handle paid invoice error, lookup_id {s}, err={s}", .{ request_lookup_id.items, @errorName(err) });
continue;
};
}
}
}).handleLnInvoice;

var it = ln_backends.iterator();
while (it.next()) |ln_entry| {
threads.appendAssumeCapacity(try std.Thread.spawn(.{}, thread_fn, .{
&mint, try ln_entry.value_ptr.waitAnyInvoice(),
}));
}
break :v threads;
};
defer threads.deinit();
defer for (threads.items) |t| t.detach();

// for (_, ln) in ln_backends {
// let mint = Arc::clone(&mint);
// tokio::spawn(async move {
Expand Down
11 changes: 10 additions & 1 deletion src/router/router.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn createMintServer(
ln: LnBackendsMap,
quote_ttl: u64,
server_options: httpz.Config,
middlewares: anytype,
) !httpz.Server(MintState) {
// TODO do we need copy
const state = MintState{
Expand All @@ -34,8 +35,16 @@ pub fn createMintServer(
var srv = try httpz.Server(MintState).init(allocator, server_options, state);
errdefer srv.deinit();

var _middlewares = try srv.arena.alloc(httpz.Middleware(MintState), middlewares.len);

inline for (middlewares, 0..) |m, i| {
_middlewares[i] = try srv.middleware(m[0], m[1]);
}

// apply routes
var router = srv.router(.{});
var router = srv.router(.{
.middlewares = _middlewares,
});

router.get("/v1/keys", router_handlers.getKeys, .{});
router.get("/v1/keysets", router_handlers.getKeysets, .{});
Expand Down

0 comments on commit 8373611

Please sign in to comment.