Files
metin2-server/tests/features/fixtures/cube_gold_probe.cpp
T

97 lines
8.8 KiB
C++

#define main cubeBaseMain
#include "cube_batch_probe.cpp"
#undef main
#include "src/server/common/cube_gold_store.h"
using G=GoldIntentStore<TestDB>;
static void initializeGold(TestDB& db) {
initializeGoldAddon(db);
}
static void resetGold(TestDB& db,const Batch& b) {
db.failAt=db.warnAt=0;db.after={};db.ambiguous=db.rejectCommit=false;db.Rollback();
sql(db,"DELETE FROM port_cube_gold_sources");sql(db,"DELETE FROM port_cube_gold_intents");reset(db,b);
}
static GoldIntent credit(const Batch& b,unsigned sequence=1,uint64_t amount=250) {
GoldIntent i;i.account=b.account;i.player=b.player;i.epoch=b.epoch;i.sequence=sequence;i.amount=amount;i.source=2;
i.owner=b.owner;i.request=b.token;i.event=std::string(31,'e')+char('0'+sequence);return i;
}
static void settle(TestDB& db,const Batch& b) {
Store store(db,proto);Store::Receipt receipt;
assert(store.Prepare(b,[]{return 1u;})==Store::Prepared);
assert(store.Execute(b.token,1,1,receipt)==Store::Complete&&receipt.fee==300);
}
static void waitWorker(pid_t child,int code) {int status=0;assert(waitpid(child,&status,0)==child&&WIFEXITED(status)&&WEXITSTATUS(status)==code);}
int main(int argc,char** argv) {
assert(argc==2||argc==3);address=argv[1];executable=argv[0];TestDB db;G gold(db,proto);G::Receipt receipt;
if(argc==3) {
Batch b;std::string phase,image;FenceStore<TestDB> fence(db,proto);
assert(fence.Recover(1,1,b,phase,image)==FenceStore<TestDB>::Done);
std::vector<GoldIntent> pending;assert(gold.Pending(b,pending)==G::Stored);
const auto i=credit(b);
const std::string mode=argv[2];
if(mode.find("crash-")==0)db.after=[&](const std::string& statement){
if((mode=="crash-wallet"&&statement.find("UPDATE player SET gold=gold+")==0)||
(mode=="crash-receipt"&&statement.find("UPDATE port_cube_gold_intents SET state='APPLIED'")==0)||
(mode=="crash-commit"&&statement=="COMMIT"))_Exit(79);
};
assert(gold.Apply(i.event,1,1,receipt)==G::Applied);return 77;
}
initialize(db);initializeGold(db);auto b=batch();auto i=credit(b);std::vector<GoldIntent> pending;FenceStore<TestDB> fence(db,proto);std::string image;
assert(EncodeGoldIntent(i).size()==GoldIntentBytes);GoldIntent decoded;
assert(DecodeGoldIntent(EncodeGoldIntent(i),decoded));for(unsigned n=0;n<GoldIntentBytes;++n)assert(!DecodeGoldIntent(EncodeGoldIntent(i).substr(0,n),decoded));
assert(!DecodeGoldIntent(EncodeGoldIntent(i)+"x",decoded));auto bad=i;bad.amount=0;assert(!ValidGoldIntent(bad));bad=i;bad.sequence=GoldIntentLimit+1;assert(!ValidGoldIntent(bad));
resetGold(db,b);assert(gold.Ready());assert(gold.Enqueue(b,i)==G::Stored&&gold.Enqueue(b,i)==G::Stored);
auto different=i;different.amount++;assert(gold.Enqueue(b,different)==G::Refused);
auto second=credit(b,2,400);assert(gold.Enqueue(b,second)==G::Stored);auto gap=credit(b,4);assert(gold.Enqueue(b,gap)==G::Refused);
assert(gold.Apply(i.event,1,1,receipt)==G::Blocked); // no craft result, no implicit Execute
settle(db,b);assert(fence.Resync(b,image)==FenceStore<TestDB>::Refused&&image.empty());
assert(gold.Apply(second.event,1,1,receipt)==G::Blocked);
assert(gold.Apply(i.event,2,1,receipt)==G::Refused);
db.ambiguous=true;assert(gold.Apply(i.event,1,1,receipt)==G::Retry);assert(number(db,"SELECT gold FROM player")==950);
assert(gold.Apply(i.event,1,1,receipt)==G::Applied&&receipt.before==700&&receipt.after==950);assert(gold.Enqueue(b,i)==G::Applied);
assert(gold.Pending(b,pending)==G::Stored&&pending.size()==1&&pending[0].event==second.event);
assert(fence.Resync(b,image)==FenceStore<TestDB>::Refused);
assert(gold.Apply(second.event,1,1,receipt)==G::Applied&&number(db,"SELECT gold FROM player")==1350);
assert(fence.Resync(b,image)==FenceStore<TestDB>::Refused); // SQL has no pending credit, source still open
GoldSourceSeal seal;static_cast<GoldSourceBinding&>(seal)=GoldSourceFor(b,8,7,6);seal.events={i.event,second.event};
assert(gold.CloseSource(b,seal)==G::Stored);
assert(gold.Enqueue(b,credit(b,3))==G::Blocked); // sealing closes new admission before Resync
assert(fence.Resync(b,image)==FenceStore<TestDB>::Done);
auto third=credit(b,3);assert(gold.Enqueue(b,third)==G::Blocked); // resync is a closed admission boundary
sql(db,"UPDATE port_cube_gold_intents SET state='QUEUED',before_gold=0,after_gold=0 WHERE seq=2");assert(fence.Release(b,image)==FenceStore<TestDB>::Refused);
sql(db,"UPDATE port_cube_gold_intents SET state='APPLIED',before_gold=950,after_gold=1350 WHERE seq=2");
assert(fence.Release(b,image)==FenceStore<TestDB>::Done);sql(db,"UPDATE player SET gold=1400");
assert(gold.Apply(i.event,1,1,receipt)==G::Applied&&number(db,"SELECT gold FROM player")==1400); // history replay never re-adds
assert(fence.Release(b,image)==FenceStore<TestDB>::Done); // release ACK retry after normal later wallet writes
puts("PASS durable positive intents, order/idempotency, immutable body, no implicit craft, resync/release barrier and historical replay");
resetGold(db,b);db.ambiguous=true;assert(gold.Enqueue(b,i)==G::Retry);assert(gold.Enqueue(b,i)==G::Stored&&number(db,"SELECT COUNT(*) FROM port_cube_gold_intents")==1);
settle(db,b);sql(db,"UPDATE player SET gold=701");assert(gold.Apply(i.event,1,1,receipt)==G::Blocked&&number(db,"SELECT gold FROM player")==701);sql(db,"UPDATE player SET gold=700");
sql(db,"UPDATE port_cube_requests SET successes=0");assert(gold.Apply(i.event,1,1,receipt)==G::Blocked);sql(db,"UPDATE port_cube_requests SET successes=3");
sql(db,"UPDATE port_cube_fences SET phase='ABORTED',drain_state=2");assert(gold.Apply(i.event,1,1,receipt)==G::Blocked);
sql(db,"UPDATE port_cube_fences SET phase='FROZEN',drain_state=1");
auto overflow=credit(b,2,PortEconomy::GoldLimit);assert(gold.Enqueue(b,overflow)==G::Stored);
assert(gold.Apply(i.event,1,1,receipt)==G::Applied);assert(gold.Apply(overflow.event,1,1,receipt)==G::Blocked);
assert(gold.Pending(b,pending)==G::Stored&&pending.size()==1);
sql(db,"UPDATE port_cube_gold_intents SET after_gold=951 WHERE seq=1");assert(gold.Pending(b,pending)==G::Blocked&&fence.Resync(b,image)!=FenceStore<TestDB>::Done);
sql(db,"UPDATE port_cube_gold_intents SET after_gold=950 WHERE seq=1");
sql(db,"UPDATE port_cube_gold_version SET version=1");assert(!gold.Ready());sql(db,"UPDATE port_cube_gold_version SET version=2");
#ifdef PORT_CUBE_MYSQL
sql(db,"ALTER TABLE port_cube_gold_intents ENGINE=MyISAM");assert(!gold.Ready());sql(db,"ALTER TABLE port_cube_gold_intents ENGINE=InnoDB");
#endif
puts("PASS stale wallet, corrupt receipt/journal, aborted drain, overflow and incompatible addon refused without losing accepted intent");
resetGold(db,b);db.statements=0;assert(gold.Enqueue(b,i)==G::Stored);const int enqueueCount=db.statements;
for(int at=1;at<=enqueueCount;++at){resetGold(db,b);db.statements=0;db.failAt=at;auto r=gold.Enqueue(b,i);db.failAt=0;assert(r!=G::Stored&&r!=G::Applied);assert(number(db,"SELECT COUNT(*) FROM port_cube_gold_intents")==0&&number(db,"SELECT gold FROM player")==1000);assert(gold.Enqueue(b,i)==G::Stored);}
resetGold(db,b);assert(gold.Enqueue(b,i)==G::Stored);settle(db,b);db.statements=0;assert(gold.Apply(i.event,1,1,receipt)==G::Applied);const int applyCount=db.statements;
for(unsigned warning=0;warning<2;++warning)for(int at=1;at<=applyCount;++at){resetGold(db,b);assert(gold.Enqueue(b,i)==G::Stored);settle(db,b);db.statements=0;if(warning)db.warnAt=at;else db.failAt=at;auto r=gold.Apply(i.event,1,1,receipt);db.failAt=db.warnAt=0;assert(r!=G::Applied&&number(db,"SELECT gold FROM player")==700&&value(db,"SELECT state FROM port_cube_gold_intents")=="QUEUED");assert(gold.Apply(i.event,1,1,receipt)==G::Applied);}
resetGold(db,b);assert(gold.Enqueue(b,i)==G::Stored);settle(db,b);db.rejectCommit=true;assert(gold.Apply(i.event,1,1,receipt)==G::Retry&&number(db,"SELECT gold FROM player")==700);assert(gold.Apply(i.event,1,1,receipt)==G::Applied);
puts("PASS every enqueue/apply SQL failure, warning rollback, rejected and ambiguous commit");
for(const auto& mode:{"crash-wallet","crash-receipt","crash-commit"}) {
resetGold(db,b);assert(gold.Enqueue(b,i)==G::Stored);settle(db,b);waitWorker(spawnWorker(mode),79);
assert(number(db,"SELECT gold FROM player")==(std::string(mode)=="crash-commit"?950:700));
waitWorker(spawnWorker("recover"),77);assert(number(db,"SELECT gold FROM player")==950&&value(db,"SELECT state FROM port_cube_gold_intents")=="APPLIED");
}
resetGold(db,b);assert(gold.Enqueue(b,i)==G::Stored);settle(db,b);const auto first=spawnWorker("recover"),other=spawnWorker("recover");waitWorker(first,77);waitWorker(other,77);assert(number(db,"SELECT gold FROM player")==950);
puts("PASS separate-process crash before/after receipt/COMMIT, restart pending lookup and concurrent duplicate application; no live reward producer");
}