Improve Transaction Logging - Log Transactions at Initiation #8

Closed
opened 2025-06-19 20:40:11 +00:00 by saulteafarmer · 1 comment

Improve Transaction Logging - Log Transactions at Initiation

Problem Description

Currently, the Orangemart plugin only logs transactions after they complete (either successfully or fail). This creates a gap in transaction tracking when:

  1. Server restarts occur while transactions are actively being processed
  2. Plugin reloads happen during pending transactions
  3. Unexpected crashes interrupt transaction flows

The current logging behavior:

  • Buy transactions: Only logged in CheckPendingInvoices() when invoice is paid/expired
  • Send transactions: Only logged in CmdSendCurrency() after SendBitcoin() completes
  • No record exists of initiated but incomplete transactions

Current Code Issues

Send Currency Flow

// In CmdSendCurrency() - logging only happens after SendBitcoin completes
SendBitcoin(lightningAddress, amount * satsPerCurrencyUnit, (success, paymentHash) =>
{
    if (success && !string.IsNullOrEmpty(paymentHash))
    {
        LogSellTransaction(new SellInvoiceLogEntry { ... }); // ← Only logs on completion
    }
    else
    {
        LogSellTransaction(new SellInvoiceLogEntry { ... }); // ← Only logs on failure
    }
});

Buy Currency Flow

// In CheckPendingInvoices() - only logs when payment detected
if (isPaid)
{
    var logEntry = CreateBuyInvoiceLogEntry(...); // ← Only logs when paid
    LogBuyInvoice(logEntry);
}

Proposed Solution

Implement immediate transaction logging with status updates:

1. Log Transaction Initiation

  • Create log entry as soon as transaction is initiated
  • Status: "INITIATED" or "PENDING"
  • Include all available details (player, amount, timestamp, etc.)

2. Update Log Entry During Processing

  • Add Status field to log entry classes
  • Update existing log entries rather than creating new ones
  • Track status progression: INITIATED → PROCESSING → COMPLETED/FAILED

3. Enhanced Log Entry Structure

private class SellInvoiceLogEntry
{
    public string SteamID { get; set; }
    public string LightningAddress { get; set; }
    public string Status { get; set; } // NEW: INITIATED, PROCESSING, COMPLETED, FAILED
    public bool Success { get; set; }
    public int SatsAmount { get; set; }
    public string PaymentHash { get; set; }
    public bool CurrencyReturned { get; set; }
    public DateTime Timestamp { get; set; }
    public DateTime? CompletedTimestamp { get; set; } // NEW: When transaction finished
    public int RetryCount { get; set; }
    public string TransactionId { get; set; } // NEW: Unique ID for tracking
}

4. Recovery on Plugin Load

  • On plugin initialization, check for INITIATED or PROCESSING transactions
  • Attempt to recover/resume or mark as failed with appropriate currency refunds

Implementation Steps

  1. Add status tracking to SellInvoiceLogEntry and BuyInvoiceLogEntry
  2. Create immediate logging in CmdSendCurrency() before calling SendBitcoin()
  3. Create immediate logging in CmdBuyCurrency() when invoice is created
  4. Update existing entries instead of creating new ones in completion handlers
  5. Add recovery logic in OnServerInitialized() to handle interrupted transactions
  6. Update log helper methods to support entry updates by transaction ID

Benefits

  • Complete audit trail of all transaction attempts
  • Server restart resilience - no lost transaction records
  • Better debugging - can see exactly when transactions were initiated
  • Recovery capability - can handle interrupted transactions on restart
  • Status visibility - track transaction progression through states

Files to Modify

  • Orangemart.cs - Main plugin file
    • CmdSendCurrency() method
    • CmdBuyCurrency() method
    • CheckPendingInvoices() method
    • OnServerInitialized() method
    • Log entry classes
    • Log helper methods

Priority

High - This addresses a potential data loss issue and improves transaction reliability.


Labels: enhancement, logging, reliability, transaction-tracking

# Improve Transaction Logging - Log Transactions at Initiation ## Problem Description Currently, the Orangemart plugin only logs transactions after they complete (either successfully or fail). This creates a gap in transaction tracking when: 1. **Server restarts** occur while transactions are actively being processed 2. **Plugin reloads** happen during pending transactions 3. **Unexpected crashes** interrupt transaction flows The current logging behavior: - **Buy transactions**: Only logged in `CheckPendingInvoices()` when invoice is paid/expired - **Send transactions**: Only logged in `CmdSendCurrency()` after `SendBitcoin()` completes - **No record** exists of initiated but incomplete transactions ## Current Code Issues ### Send Currency Flow ```csharp // In CmdSendCurrency() - logging only happens after SendBitcoin completes SendBitcoin(lightningAddress, amount * satsPerCurrencyUnit, (success, paymentHash) => { if (success && !string.IsNullOrEmpty(paymentHash)) { LogSellTransaction(new SellInvoiceLogEntry { ... }); // ← Only logs on completion } else { LogSellTransaction(new SellInvoiceLogEntry { ... }); // ← Only logs on failure } }); ``` ### Buy Currency Flow ```csharp // In CheckPendingInvoices() - only logs when payment detected if (isPaid) { var logEntry = CreateBuyInvoiceLogEntry(...); // ← Only logs when paid LogBuyInvoice(logEntry); } ``` ## Proposed Solution Implement **immediate transaction logging** with **status updates**: ### 1. Log Transaction Initiation - Create log entry as soon as transaction is initiated - Status: `"INITIATED"` or `"PENDING"` - Include all available details (player, amount, timestamp, etc.) ### 2. Update Log Entry During Processing - Add `Status` field to log entry classes - Update existing log entries rather than creating new ones - Track status progression: `INITIATED → PROCESSING → COMPLETED/FAILED` ### 3. Enhanced Log Entry Structure ```csharp private class SellInvoiceLogEntry { public string SteamID { get; set; } public string LightningAddress { get; set; } public string Status { get; set; } // NEW: INITIATED, PROCESSING, COMPLETED, FAILED public bool Success { get; set; } public int SatsAmount { get; set; } public string PaymentHash { get; set; } public bool CurrencyReturned { get; set; } public DateTime Timestamp { get; set; } public DateTime? CompletedTimestamp { get; set; } // NEW: When transaction finished public int RetryCount { get; set; } public string TransactionId { get; set; } // NEW: Unique ID for tracking } ``` ### 4. Recovery on Plugin Load - On plugin initialization, check for `INITIATED` or `PROCESSING` transactions - Attempt to recover/resume or mark as failed with appropriate currency refunds ## Implementation Steps 1. **Add status tracking** to `SellInvoiceLogEntry` and `BuyInvoiceLogEntry` 2. **Create immediate logging** in `CmdSendCurrency()` before calling `SendBitcoin()` 3. **Create immediate logging** in `CmdBuyCurrency()` when invoice is created 4. **Update existing entries** instead of creating new ones in completion handlers 5. **Add recovery logic** in `OnServerInitialized()` to handle interrupted transactions 6. **Update log helper methods** to support entry updates by transaction ID ## Benefits - ✅ **Complete audit trail** of all transaction attempts - ✅ **Server restart resilience** - no lost transaction records - ✅ **Better debugging** - can see exactly when transactions were initiated - ✅ **Recovery capability** - can handle interrupted transactions on restart - ✅ **Status visibility** - track transaction progression through states ## Files to Modify - `Orangemart.cs` - Main plugin file - `CmdSendCurrency()` method - `CmdBuyCurrency()` method - `CheckPendingInvoices()` method - `OnServerInitialized()` method - Log entry classes - Log helper methods ## Priority **High** - This addresses a potential data loss issue and improves transaction reliability. --- **Labels:** `enhancement`, `logging`, `reliability`, `transaction-tracking`
Author
Owner

Fixed on 0.4.0

Fixed on 0.4.0
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: rustysats/orangemart.cs#8
No description provided.