#define PORT_CUBE_OWNER_EMBED #include "cube_owner_probe.cpp" #include "src/server/common/cube_receipt.h" static Store::Result prepareLive(TestDB& db,const Batch& b,const OwnerGrant& grant) { O owner(db,proto);Store store(db,proto);return store.Prepare(b,draw,[&](){return owner.CraftWithin(b,grant);}); } static Store::Result executeLive(TestDB& db,const Batch& b,const OwnerGrant& grant,Store::Receipt& receipt) { O owner(db,proto);Store store(db,proto);return store.Execute(b.token,b.account,b.player,receipt,[&](const Batch& saved){return Encode(saved)==Encode(b)&&owner.CraftWithin(saved,grant);}); } int main(int argc,char** argv) { assert(argc==2||argc==3);address=argv[1];executable=argv[0];TestDB db;const auto b=batch();O owner(db,proto);OwnerLeader leader;Store::Receipt receipt; if(argc==3) { assert(owner.Acquire(std::string(32,'e'),leader)==O::Acquired);frozen(db,b,leader,false,true);const auto grant=original(b,leader);const std::string mode=argv[2]; if(mode=="execute-write"||mode=="execute-commit")assert(prepareLive(db,b,grant)==Store::Prepared); db.after=[&](const std::string& q){if((mode=="prepare-write"&&q.find("INSERT INTO port_cube_requests")==0)||(mode=="execute-write"&&q.find("UPDATE item SET count=count-")==0)||((mode=="prepare-commit"||mode=="execute-commit")&&q=="COMMIT"))_Exit(96);}; if(mode.find("prepare")==0)prepareLive(db,b,grant);else executeLive(db,b,grant,receipt);assert(false); } initialize(db);initializeGoldAddon(db);initializeOwnerAddon(db);reset(db,b);assert(owner.Acquire(std::string(32,'d'),leader)==O::Acquired);auto grant=original(b,leader); frozen(db,b,leader,false,false);assert(prepareLive(db,b,grant)==Store::Blocked&&draws==0&&number(db,"SELECT COUNT(*) FROM port_cube_requests")==0); G gold(db,proto);assert(gold.CloseSource(b,source(b))==G::Stored);db.ambiguous=true;assert(prepareLive(db,b,grant)==Store::Retry&&draws==3); const auto rolls=value(db,"SELECT rolls FROM port_cube_requests");assert(prepareLive(db,b,grant)==Store::Prepared&&draws==3&&number(db,"SELECT gold FROM player")==1000); auto wrong=grant;wrong.revision++;assert(executeLive(db,b,wrong,receipt)==Store::Blocked&&number(db,"SELECT gold FROM player")==1000); db.ambiguous=true;assert(executeLive(db,b,grant,receipt)==Store::Retry);assert(executeLive(db,b,grant,receipt)==Store::Complete&&receipt.successes==2&&receipt.fee==300); const auto encoded=EncodeCraftReceipt({receipt.result,receipt.successes,receipt.fee,receipt.awards});CraftReceipt decoded;assert(DecodeCraftReceipt(encoded,decoded)&&ValidateCraftReceipt(b,decoded,proto)); assert(executeLive(db,b,grant,receipt)==Store::Complete&&EncodeCraftReceipt({receipt.result,receipt.successes,receipt.fee,receipt.awards})==encoded&&number(db,"SELECT gold FROM player")==700&&number(db,"SELECT count FROM item WHERE id=10")==4&&draws==3&&value(db,"SELECT rolls FROM port_cube_requests")==rolls); puts("PASS live-owner source-sealed Prepare/Execute: durable fixed rolls, original receipt replay, ambiguous commits and one atomic charge/award"); for(unsigned operation=0;operation<2;++operation) { auto setup=[&](){frozen(db,b,leader,false,true);if(operation)assert(prepareLive(db,b,grant)==Store::Prepared);}; auto action=[&](){return operation?executeLive(db,b,grant,receipt):prepareLive(db,b,grant);};const auto expected=operation?Store::Complete:Store::Prepared; setup();db.statements=0;assert(action()==expected);const auto count=db.statements; for(unsigned warning=0;warning<2;++warning)for(int at=1;at<=count;++at) { setup();db.statements=0;if(warning)db.warnAt=at;else db.failAt=at;assert(action()!=expected);db.failAt=db.warnAt=0; assert(number(db,"SELECT gold FROM player")==1000&&number(db,"SELECT count FROM item WHERE id=10")==10&&number(db,"SELECT COUNT(*) FROM item WHERE id>=100")==0); assert(number(db,"SELECT COUNT(*) FROM port_cube_requests")==operation);if(operation)assert(value(db,"SELECT state FROM port_cube_requests")=="PREPARED");assert(action()==expected); } } frozen(db,b,leader,false,true);assert(prepareLive(db,b,grant)==Store::Prepared);OwnerGrant revoked;assert(owner.Revoke(b,leader,1,&grant,revoked)==O::Revoked&&executeLive(db,b,grant,receipt)==Store::Blocked); frozen(db,b,leader,false,true);assert(prepareLive(db,b,grant)==Store::Prepared);sql(db,"UPDATE player SET gold=1001");assert(executeLive(db,b,grant,receipt)==Store::Complete&&receipt.result=="gold_changed"&&receipt.fee==0&&receipt.awards.empty()); assert(executeLive(db,b,grant,receipt)==Store::Complete&&number(db,"SELECT gold FROM player")==1001&&number(db,"SELECT count FROM item WHERE id=10")==10); puts("PASS every authorized prepare/execute SQL failure/warning, original-owner revoke refusal, non-ok DONE replay without refund/retry charge"); for(const auto& mode:{"prepare-write","prepare-commit","execute-write","execute-commit"}) { sql(db,"SELECT RELEASE_LOCK(CONCAT('m2_cube:',DATABASE()))");const auto child=spawnWorker(mode);int status=0;assert(waitpid(child,&status,0)==child&&WIFEXITED(status)&&WEXITSTATUS(status)==96); const bool paid=std::string(mode)=="execute-commit",prepared=std::string(mode)!="prepare-write"; assert(number(db,"SELECT gold FROM player")==uint64_t(paid?700:1000)&&number(db,"SELECT COUNT(*) FROM port_cube_requests")==prepared); assert(owner.Acquire(std::string(32,'d'),leader)==O::Acquired);const auto replacement=original(b,leader); assert(prepareLive(db,b,replacement)==Store::Blocked&&executeLive(db,b,replacement,receipt)!=Store::Complete); // newer broker alone is NOT a live owner or automatic retry permission } puts("PASS process exits before/after Prepare and Execute COMMIT: durable prepared/done states, no generation-based automatic execution/redraw/thaw"); }