instantx.h 2.69 KB
Newer Older
1

Infernoman's avatar
Infernoman committed
2
// Copyright (c) 2009-2012 The Crown developers
3
4
5
6
7
8
9
10
11
12
13
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef INSTANTX_H
#define INSTANTX_H

#include "sync.h"
#include "net.h"
#include "key.h"
#include "util.h"
#include "base58.h"
#include "main.h"
14
#include "spork.h"
15

16
/*
Alastair Clark's avatar
Alastair Clark committed
17
    At 15 signatures, 1/2 of the masternode network can be owned by
18
    one party without comprimising the security of InstantX
19
20
21
22
23
    (1000/2150.0)**10 = 0.00047382219560689856
    (1000/2900.0)**10 = 2.3769498616783657e-05

    ### getting 5 of 10 signatures w/ 1000 nodes of 2900
    (1000/2900.0)**5 = 0.004875397277841433
24
*/
Evan Duffield's avatar
Evan Duffield committed
25
#define INSTANTX_SIGNATURES_REQUIRED           6
Evan Duffield's avatar
Evan Duffield committed
26
#define INSTANTX_SIGNATURES_TOTAL              10
27

28
29
30
31
32
33
34
using namespace std;
using namespace boost;

class CConsensusVote;
class CTransaction;
class CTransactionLock;

Alastair Clark's avatar
Alastair Clark committed
35
static const int MIN_INSTANTX_PROTO_VERSION = 70040;
36
37

extern map<uint256, CTransaction> mapTxLockReq;
38
39
extern map<uint256, CTransaction> mapTxLockReqRejected;
extern map<uint256, CConsensusVote> mapTxLockVote;
40
extern map<uint256, CTransactionLock> mapTxLocks;
Evan Duffield's avatar
Evan Duffield committed
41
extern std::map<COutPoint, uint256> mapLockedInputs;
42
extern int nCompleteTXLocks;
43

44
45
46

int64_t CreateNewLock(CTransaction tx);

Evan Duffield's avatar
Evan Duffield committed
47
48
bool IsIXTXValid(const CTransaction& txCollateral);

Evan Duffield's avatar
Evan Duffield committed
49
50
51
// if two conflicting locks are approved by the network, they will cancel out
bool CheckForConflictingLocks(CTransaction& tx);

52
53
54
void ProcessMessageInstantX(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);

//check if we need to vote on this transaction
55
void DoConsensusVote(CTransaction& tx, int64_t nBlockHeight);
56
57

//process consensus vote message
58
bool ProcessConsensusVote(CNode *pnode, CConsensusVote& ctx);
59
60
61
62

// keep transaction locks in memory for an hour
void CleanTransactionLocksList();

63
64
int64_t GetAverageVoteTime();

65
66
67
class CConsensusVote
{
public:
Alastair Clark's avatar
Alastair Clark committed
68
    CTxIn vinMasternode;
69
    uint256 txHash;
70
    int nBlockHeight;
Alastair Clark's avatar
Alastair Clark committed
71
    std::vector<unsigned char> vchMasterNodeSignature;
Evan Duffield's avatar
Evan Duffield committed
72
73

    uint256 GetHash() const;
74
75
76
77

    bool SignatureValid();
    bool Sign();

78
79
80
81
    ADD_SERIALIZE_METHODS;

    template <typename Stream, typename Operation>
    inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
82
        READWRITE(txHash);
Alastair Clark's avatar
Alastair Clark committed
83
        READWRITE(vinMasternode);
Alastair Clark's avatar
Alastair Clark committed
84
        READWRITE(vchMasterNodeSignature);
85
        READWRITE(nBlockHeight);
86
    }
87
88
89
90
91
92
};

class CTransactionLock
{
public:
    int nBlockHeight;
93
    uint256 txHash;
94
    std::vector<CConsensusVote> vecConsensusVotes;
95
    int nExpiration;
Evan Duffield's avatar
Evan Duffield committed
96
    int nTimeout;
97
98
99

    bool SignaturesValid();
    int CountSignatures();
100
    void AddSignature(CConsensusVote& cv);
101

102
103
    uint256 GetHash()
    {
104
        return txHash;
105
106
107
108
    }
};


109
#endif