tests for bet protocol done; verify notarisation still to test

This commit is contained in:
Scott Sadler
2018-04-05 05:06:22 -03:00
parent 92df780015
commit 561f3e18c1
23 changed files with 1195 additions and 443 deletions

View File

@@ -1,117 +1,79 @@
#include <cryptoconditions.h>
#include "streams.h"
#include "komodo_cc.h"
#include "cc/eval.h"
#include "cc/betprotocol.h"
#include "primitives/transaction.h"
class DisputeHeader
{
public:
int waitBlocks;
std::vector<unsigned char> vmParams;
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(VARINT(waitBlocks));
READWRITE(vmParams);
}
};
static CScript CCPubKey(const CC *cond)
{
unsigned char buf[1000];
size_t len = cc_conditionBinary(cond, buf);
return CScript() << std::vector<unsigned char>(buf, buf+len) << OP_CHECKCRYPTOCONDITION;
}
static CScript CCSig(const CC *cond)
{
unsigned char buf[1000];
size_t len = cc_fulfillmentBinary(cond, buf, 1000);
auto ffill = std::vector<unsigned char>(buf, buf+len);
ffill.push_back(SIGHASH_ALL);
return CScript() << ffill;
}
static unsigned char* CopyPubKey(CPubKey pkIn)
{
auto *pk = malloc(33);
unsigned char* pk = (unsigned char*) malloc(33);
memcpy(pk, pkIn.begin(), 33); // TODO: compressed?
return pk;
}
class PokerProtocol
CC* CCNewThreshold(int t, std::vector<CC*> v)
{
public:
CAmount MINFEE = 1;
std::vector<CPubKey> players;
DisputeHeader disputeHeader;
// on PANGEA
CC* MakeDisputeCond();
CMutableTransaction MakeSessionTx(CTxIn dealerInput);
CMutableTransaction MakeDisputeTx(uint256 signedSessionTxHash);
CMutableTransaction MakePostEvidenceTx(uint256 signedSessionTxHash,
CPubKey playerKey, std::vector<unsigned char> state);
// on KMD
CC* MakePayoutCond();
CMutableTransaction MakeStakeTx(CAmount totalPayout, std::vector<CTxIn> playerInputs,
uint256 signedSessionTx);
CMutableTransaction MakeAgreePayoutTx(std::vector<CTxOut> payouts,
CC *signedPayoutCond, uint256 signedStakeTxHash);
CMutableTransaction MakeImportPayoutTx(std::vector<CTxOut> payouts,
CC *signedPayoutCond, CTransaction signedDisputeTx);
}
CC* PokerProtocol::MakeDisputeCond()
{
CC *disputePoker = cond->subconditions[0] = cc_new(CC_Eval);
char err[1000];
std::vector<unsigned char> headerData;
disputeHeader >> CDataStream(headerData, SER_DISK, PROTOCOL_VERSION);
disputePoker->paramsBin = malloc(headerData.size());
memcpy(disputePoker->paramsBin, headerData.data());
disputePoker.paramsBinLength = headerData.size();
CC *spendSig = cond->subconditions[1] = cc_new(CC_Threshold);
spendSig->threshold = 1;
spendSig->size = players.size() + 1;
spendSig->subconditions = malloc(spendSig->size, sizeof(CC*));
for (int i=0; i<players.size()+1; i++) {
CC *sub = spendSig->subconditions[i] = cc_new(CC_Secp256k1);
sub->publicKey = CopyPubKey(players[i]);
}
CC *cond = cc_new(CC_Threshold);
cond->threshold = 2;
cond->size = 2;
cond->subconditions = calloc(2, sizeof(CC*));
cond->subconditions[0] = disputePoker;
cond->subconditions[1] = spendSig;
cond->threshold = t;
cond->size = v.size();
cond->subconditions = (CC**) calloc(v.size(), sizeof(CC*));
memcpy(cond->subconditions, v.data(), v.size() * sizeof(CC*));
return cond;
}
CMutableTransaction PokerProtocol::MakeSessionTx(CTxIn dealerInput)
CC* CCNewSecp256k1(CPubKey k)
{
CC *cond = cc_new(CC_Secp256k1);
cond->publicKey = CopyPubKey(k);
return cond;
}
CC* CCNewEval(std::string method, std::vector<unsigned char> paramsBin)
{
CC *cond = cc_new(CC_Eval);
strcpy(cond->method, method.data());
cond->paramsBin = (unsigned char*) malloc(paramsBin.size());
memcpy(cond->paramsBin, paramsBin.data(), paramsBin.size());
cond->paramsBinLength = paramsBin.size();
return cond;
}
std::vector<CC*> BetProtocol::PlayerConditions()
{
std::vector<CC*> subs;
for (int i=0; i<players.size(); i++)
subs.push_back(CCNewSecp256k1(players[i]));
return subs;
}
CC* BetProtocol::MakeDisputeCond()
{
CC *disputePoker = CCNewEval(disputeFunc, CheckSerialize(disputeHeader));
CC *anySig = CCNewThreshold(1, PlayerConditions());
return CCNewThreshold(2, {disputePoker, anySig});
}
CMutableTransaction BetProtocol::MakeSessionTx()
{
CMutableTransaction mtx;
mtx.vin.push_back(dealerInput);
CC *disputeCond = MakeDisputeCond(players, disputeHeader);
CC *disputeCond = MakeDisputeCond();
mtx.vout.push_back(CTxOut(MINFEE, CCPubKey(disputeCond)));
cc_free(disputeCond);
for (int i=0; i<players.size(); i++) {
CC *cond = cc_new(CC_Secp256k1);
cond->publicKey = CopyPubKey(players[i]);
CC *cond = CCNewSecp256k1(players[i]);
mtx.vout.push_back(CTxOut(MINFEE, CCPubKey(cond)));
cc_free(cond);
}
@@ -119,12 +81,12 @@ CMutableTransaction PokerProtocol::MakeSessionTx(CTxIn dealerInput)
}
CMutableTransaction PokerProtocol::MakeDisputeTx(uint256 signedSessionTxHash, CC *signedDisputeCond, uint256 vmResultHash)
CMutableTransaction BetProtocol::MakeDisputeTx(uint256 signedSessionTxHash, uint256 vmResultHash)
{
CMutableTransaction mtx;
CC *disputeCond = MakeDisputeCond();
mtx.vin.push_back(CTxIn(signedSessionTxHash, 0, CCSig(signedDisputeCond)));
mtx.vin.push_back(CTxIn(signedSessionTxHash, 0, CScript()));
std::vector<unsigned char> result(vmResultHash.begin(), vmResultHash.begin()+32);
mtx.vout.push_back(CTxOut(0, CScript() << OP_RETURN << result));
@@ -132,69 +94,29 @@ CMutableTransaction PokerProtocol::MakeDisputeTx(uint256 signedSessionTxHash, CC
}
CMutableTransaction PokerProtocol::MakePostEvidenceTx(uint256 signedSessionTxHash,
CMutableTransaction BetProtocol::MakePostEvidenceTx(uint256 signedSessionTxHash,
int playerIdx, std::vector<unsigned char> state)
{
CMutableTransaction mtx;
CC *cond = cc_new(CC_Secp256k1);
cond->publicKey = CopyPubKey(players[i]);
mtx.vin.push_back(CTxIn(signedSessionTxHash, playerIdx+1, CCSig(cond)));
mtx.vin.push_back(CTxIn(signedSessionTxHash, playerIdx+1, CScript()));
mtx.vout.push_back(CTxOut(0, CScript() << OP_RETURN << state));
return mtx;
}
CC* CCNewThreshold(int t, std::vector<CC*> v)
CC* BetProtocol::MakePayoutCond(uint256 signedSessionTxHash)
{
CC *cond = cc_new(CC_Threshold);
cond->threshold = t;
cond->size = v.size();
cond->subconditions = calloc(1, sizeof(CC*));
memcpy(cond->subconditions, v.data(), v.size() * sizeof(CC*));
return cond;
}
CCNewSecp256k1(CPubKey &k)
{
cond = cc_new(CC_Secp256k1);
cond->publicKey = CopyPubKey(players[i]);
return cond;
}
CCNewEval(char *method, unsigned char* paramsBin, size_t len)
{
CC *cond = cc_new(CC_Eval);
strcpy(cond->method, method);
cond->paramsBin = malloc(32);
memcpy(cond->paramsBin, bin, len);
cond->paramsBinLength = len;
return cond;
}
CC* MakePayoutCond(uint256 signedSessionTx)
{
CC* agree;
{
// TODO: 2/3 majority
std::vector<CC*> subs;
for (int i=0; i<players.size(); i++)
subs[i] = CCNewSecp256k1(players[i]);
agree = CCNewThreshold(players.size(), subs);
}
// TODO: 2/3 majority
CC* agree = CCNewThreshold(players.size(), PlayerConditions());
CC *import;
{
CC *importEval = CCNewEval("ImportPayout", signedSessionTx.begin(), 32);
std::vector<unsigned char> vHash(signedSessionTxHash.begin(), signedSessionTxHash.end());
CC *importEval = CCNewEval("ImportPayout", vHash);
std::vector<CC*> subs;
for (int i=0; i<players.size(); i++)
subs[i] = CCNewSecp256k1(players[i]);
CC *oneof = CCNewThreshold(1, subs);
CC *oneof = CCNewThreshold(1, PlayerConditions());
import = CCNewThreshold(2, {oneof, importEval});
}
@@ -203,13 +125,11 @@ CC* MakePayoutCond(uint256 signedSessionTx)
}
CMutableTransaction PokerProtocol::MakeStakeTx(CAmount totalPayout, std::vector<CTxIn> playerInputs,
uint256 signedSessionTx)
CMutableTransaction BetProtocol::MakeStakeTx(CAmount totalPayout, uint256 signedSessionTxHash)
{
CMutableTransaction mtx;
mtx.vin = playerInputs;
CC *payoutCond = MakePayoutCond(signedSessionTx);push
CC *payoutCond = MakePayoutCond(signedSessionTxHash);
mtx.vout.push_back(CTxOut(totalPayout, CCPubKey(payoutCond)));
cc_free(payoutCond);
@@ -217,24 +137,23 @@ CMutableTransaction PokerProtocol::MakeStakeTx(CAmount totalPayout, std::vector<
}
CMutableTransaction PokerProtocol::MakeAgreePayoutTx(std::vector<CTxOut> payouts,
CC *signedPayoutCond, uint256 signedStakeTxHash)
CMutableTransaction BetProtocol::MakeAgreePayoutTx(std::vector<CTxOut> payouts,
uint256 signedStakeTxHash)
{
CMutableTransaction mtx;
mtx.vin.push_back(CTxIn(signedStakeTxHash, 0, CCSig(signedPayoutCond)));
mtx.vouts = payouts;
mtx.vin.push_back(CTxIn(signedStakeTxHash, 0, CScript()));
mtx.vout = payouts;
return mtx;
}
CMutableTransaction PokerProtocol::MakeImportPayoutTx(std::vector<CTxOut> payouts,
CC *signedPayoutCond, CTransaction signedDisputeTx, uint256 signedStakeTxHash)
CMutableTransaction BetProtocol::MakeImportPayoutTx(std::vector<CTxOut> payouts,
CTransaction signedDisputeTx, uint256 signedStakeTxHash, MoMProof momProof)
{
std::vector<unsigned char> vDisputeTx;
signedDisputeTx >> CDataStream(vDisputeTx, SER_DISK, PROTOCOL_VERSION);
CMutableTransaction mtx;
mtx.vin.push_back(CTxInput(signedStakeTxHash, 0, CCSig(signedPayoutCond)));
mtx.vin.push_back(CTxIn(signedStakeTxHash, 0, CScript()));
mtx.vout = payouts;
CMutableTransaction.vout.insert(0, CTxOutput(0, CScript() << OP_RETURN << "PROOF HERE"));
CMutableTransaction.vout.insert(1, CTxOutput(0, CScript() << OP_RETURN << vDisputeTx));
mtx.vout.insert(mtx.vout.begin(), CTxOut(0, CScript() << OP_RETURN << CheckSerialize(momProof)));
mtx.vout.insert(mtx.vout.begin()+1, CTxOut(0, CScript() << OP_RETURN << CheckSerialize(signedDisputeTx)));
return mtx;
}