Deliver paid kits in the active MySQL inventory transaction
All checks were successful
DuneRealms Kit Delivery CI / kits (push) Successful in 54s

This commit is contained in:
unimatrixgit 2026-11-16 03:19:07 +01:00
commit 976ce1c2fc
3 changed files with 54 additions and 9 deletions

View file

@ -48,16 +48,21 @@ public final class DuneKits extends JavaPlugin implements CommandExecutor, org.b
@Override public void onDisable() { inflight.clear(); notices.clear(); }
private static boolean mysql(Connection connection) throws SQLException { return connection.getMetaData().getDatabaseProductName().equalsIgnoreCase("MySQL"); }
private static void identity(PreparedStatement statement, int index, UUID value, boolean mysql) throws SQLException {
if (mysql) statement.setString(index, value.toString()); else statement.setObject(index, value);
}
private Connection connect() throws Exception {
String url = core.getConfig().getString("database.jdbc-url", "");
if (!url.startsWith("jdbc:postgresql://")) throw new SQLException("Private shared inventory database required");
if (!url.startsWith("jdbc:postgresql://") && !url.startsWith("jdbc:mysql://")) throw new SQLException("Private shared inventory database required");
Properties settings = new Properties();
settings.setProperty("user", core.getConfig().getString("database.username", ""));
settings.setProperty("password", core.getConfig().getString("database.password", ""));
settings.setProperty("connectTimeout", "3"); settings.setProperty("socketTimeout", "5");
settings.setProperty("ApplicationName", "DuneKits");
// The running DuneCore already contains its matching PostgreSQL driver.
Driver driver = (Driver) Class.forName("org.postgresql.Driver", true, core.getClass().getClassLoader())
Driver driver = (Driver) Class.forName(url.startsWith("jdbc:mysql://") ? "com.mysql.cj.jdbc.Driver" : "org.postgresql.Driver", true, core.getClass().getClassLoader())
.getDeclaredConstructor().newInstance();
Connection connection = driver.connect(url, settings);
if (connection == null) throw new SQLException("Private inventory connection unavailable");
@ -70,9 +75,9 @@ public final class DuneKits extends JavaPlugin implements CommandExecutor, org.b
getServer().getScheduler().runTaskAsynchronously(this, () -> {
List<UUID> pending = new ArrayList<>();
try (Connection connection = connect(); PreparedStatement q = connection.prepareStatement(
"SELECT id FROM dune_kit_deliveries WHERE buyer_uuid=? AND delivered_at IS NULL ORDER BY created_at,id LIMIT 5")) {
q.setObject(1, buyer);
try (ResultSet rows = q.executeQuery()) { while (rows.next()) pending.add((UUID) rows.getObject(1)); }
KitDeliverySQL.pending(mysql(connection)))) {
identity(q, 1, buyer, mysql(connection));
try (ResultSet rows = q.executeQuery()) { while (rows.next()) pending.add(UUID.fromString(rows.getString(1))); }
} catch (Exception ex) {
// Never log credentials, snapshot contents or private item codes.
getLogger().fine("Pending kit read unavailable: " + ex.getClass().getSimpleName());
@ -122,8 +127,8 @@ public final class DuneKits extends JavaPlugin implements CommandExecutor, org.b
try {
Consumer<Connection> operation = connection -> {
try (PreparedStatement q = connection.prepareStatement(
"SELECT snapshot_json,snapshot_hash FROM dune_kit_deliveries WHERE id=? AND buyer_uuid=? AND delivered_at IS NULL FOR UPDATE")) {
q.setObject(1, id); q.setObject(2, player.getUniqueId());
KitDeliverySQL.lock(mysql(connection)))) {
identity(q, 1, id, mysql(connection)); identity(q, 2, player.getUniqueId(), mysql(connection));
String raw, hash;
try (ResultSet rows = q.executeQuery()) {
if (!rows.next()) return;
@ -136,8 +141,8 @@ public final class DuneKits extends JavaPlugin implements CommandExecutor, org.b
if (!player.getInventory().addItem(items.toArray(ItemStack[]::new)).isEmpty())
throw new NoRoom();
try (PreparedStatement update = connection.prepareStatement(
"UPDATE dune_kit_deliveries SET delivered_at=(EXTRACT(EPOCH FROM clock_timestamp())*1000)::bigint WHERE id=? AND buyer_uuid=? AND delivered_at IS NULL")) {
update.setObject(1, id); update.setObject(2, player.getUniqueId());
KitDeliverySQL.acknowledge(mysql(connection)))) {
identity(update, 1, id, mysql(connection)); identity(update, 2, player.getUniqueId(), mysql(connection));
if (update.executeUpdate() != 1) throw new SQLException("Delivery changed");
}
delivered[0] = true;

View file

@ -0,0 +1,18 @@
package com.example.dunekits;
/** MySQL receipts commit directly with the active network inventory checkpoint. */
final class KitDeliverySQL {
static final String PAID = "delivered_at IS NULL AND redeem_requested_at IS NOT NULL AND redeem_requested_at>0 AND quantity=1 AND gross_cents>0 AND completed_at>0";
static String pending(boolean mysql) {
return mysql ? "SELECT id FROM dune_kit_sales WHERE buyer_uuid=? AND " + PAID + " ORDER BY redeem_requested_at,id LIMIT 5"
: "SELECT id FROM dune_kit_deliveries WHERE buyer_uuid=? AND delivered_at IS NULL ORDER BY created_at,id LIMIT 5";
}
static String lock(boolean mysql) {
return mysql ? "SELECT snapshot_json,SHA2(snapshot_json,256) FROM dune_kit_sales WHERE id=? AND buyer_uuid=? AND " + PAID + " FOR UPDATE"
: "SELECT snapshot_json,snapshot_hash FROM dune_kit_deliveries WHERE id=? AND buyer_uuid=? AND delivered_at IS NULL FOR UPDATE";
}
static String acknowledge(boolean mysql) {
return mysql ? "UPDATE dune_kit_sales SET delivered_at=CAST(UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3))*1000 AS UNSIGNED) WHERE id=? AND buyer_uuid=? AND " + PAID
: "UPDATE dune_kit_deliveries SET delivered_at=(EXTRACT(EPOCH FROM clock_timestamp())*1000)::bigint WHERE id=? AND buyer_uuid=? AND delivered_at IS NULL";
}
}

View file

@ -0,0 +1,22 @@
package com.example.dunekits;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class KitDeliverySQLTest {
@Test void mysqlRequiresPaidRequestedBuyerBoundReceiptsThroughoutTheTransaction() {
for (String sql : new String[]{KitDeliverySQL.pending(true), KitDeliverySQL.lock(true), KitDeliverySQL.acknowledge(true)}) {
assertTrue(sql.contains("dune_kit_sales"));assertTrue(sql.contains("buyer_uuid=?"));
assertTrue(sql.contains("quantity=1"));assertTrue(sql.contains("gross_cents>0"));
assertTrue(sql.contains("completed_at>0"));assertTrue(sql.contains("redeem_requested_at>0"));
assertTrue(sql.contains("delivered_at IS NULL"));
}
assertTrue(KitDeliverySQL.lock(true).endsWith("FOR UPDATE"));
assertTrue(KitDeliverySQL.lock(true).contains("SHA2(snapshot_json,256)"));
assertFalse(KitDeliverySQL.acknowledge(true).contains("dune_kit_deliveries"));
}
@Test void postgresKeepsItsBuyerBoundInventoryOutboxSeparate() {
assertTrue(KitDeliverySQL.lock(false).endsWith("FOR UPDATE"));
assertTrue(KitDeliverySQL.acknowledge(false).contains("buyer_uuid=? AND delivered_at IS NULL"));
assertFalse(KitDeliverySQL.pending(false).contains("dune_kit_sales"));
}
}