Cleanup nonce changes

This commit is contained in:
Michael Toutonghi
2018-07-20 19:57:46 -07:00
parent 503bd93fa8
commit 17d0160a17
10 changed files with 53 additions and 33 deletions

View File

@@ -271,6 +271,7 @@ libbitcoin_server_a_SOURCES = \
crypto/haraka.h \
crypto/haraka_portable.h \
crypto/verus_hash.h \
crypto/verus_hash.cpp \
deprecation.cpp \
httprpc.cpp \
httpserver.cpp \
@@ -410,6 +411,7 @@ libbitcoin_common_a_SOURCES = \
crypto/haraka.h \
crypto/haraka_portable.h \
crypto/verus_hash.h \
crypto/verus_hash.cpp \
hash.cpp \
key.cpp \
keystore.cpp \

View File

@@ -27,7 +27,7 @@ class CVerusHash
static void init();
CVerusHash() {}
CVerusHash() { }
CVerusHash &Write(const unsigned char *data, size_t len);
@@ -37,6 +37,7 @@ class CVerusHash
result = buf2;
curPos = 0;
std::fill(buf1, buf1 + sizeof(buf1), 0);
return *this;
}
int64_t *ExtraI64Ptr() { return (int64_t *)(curBuf + 32); }

View File

@@ -203,7 +203,7 @@ public:
int nType;
int nVersion;
CVerusHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn), state() {}
CVerusHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn), state() { }
CVerusHashWriter& write(const char *pch, size_t size) {
state.Write((const unsigned char*)pch, size);

View File

@@ -1271,7 +1271,12 @@ bool verusCheckPOSBlock(int32_t slowflag, CBlock *pblock, int32_t height)
CTransaction tx;
if (!pblock->IsVerusPOSBlock())
{
printf("%s, height %d not POS block\n", pblock->nNonce.GetHex().c_str(), height);
pblock->nNonce.SetPOSTarget(pblock->nNonce.GetPOSTarget());
printf("%s after setting POS target\n", pblock->nNonce.GetHex().c_str());
return false;
}
char voutaddr[64], destaddr[64], cbaddr[64];

View File

@@ -462,9 +462,9 @@ CBlockTemplate* CreateNewBlock(const CScript& _scriptPubKeyIn, bool isStake)
{
uint32_t nBitsPOS;
arith_uint256 posHash;
siglen = verus_staked(pblock, key, txStaked, nBitsPOS, posHash, utxosig);
blocktime = GetAdjustedTime();
pblock->SetVerusPOSTarget(nBitsPOS);
// change the scriptPubKeyIn to the same output script exactly as the staking transaction
if (siglen > 0)

View File

@@ -95,7 +95,7 @@ public:
return (int64_t)nTime;
}
int32_t GetVerusPOSTarget() const
uint32_t GetVerusPOSTarget() const
{
uint32_t nBits = 0;
@@ -112,19 +112,22 @@ public:
return nNonce.IsPOSNonce();
}
void SetVerusPOSTarget(int32_t nBits)
void SetVerusPOSTarget(uint32_t nBits)
{
CVerusHashWriter hashWriter = CVerusHashWriter(SER_GETHASH, PROTOCOL_VERSION);
uint256 hash;
arith_uint256 tmpNonce;
arith_uint256 arNonce = UintToArith256(nNonce);
arNonce = ((arNonce >> 32) << 32) | nBits;
tmpNonce = ((arNonce << 128) >> 128);
hashWriter << ArithToUint256(tmpNonce);
// printf("before svpt: %s\n", ArithToUint256(arNonce).GetHex().c_str());
nNonce = ArithToUint256(UintToArith256(hashWriter.GetHash()) << 128 | tmpNonce);
arNonce = (arNonce & CPOSNonce::entropyMask) | nBits;
// printf("after clear: %s\n", ArithToUint256(arNonce).GetHex().c_str());
hashWriter << ArithToUint256(arNonce);
nNonce = CPOSNonce(ArithToUint256(UintToArith256(hashWriter.GetHash()) << 128 | arNonce));
// printf(" after svpt: %s\n", nNonce.GetHex().c_str());
}
};

View File

@@ -8,6 +8,9 @@
extern char ASSETCHAINS_SYMBOL[65];
arith_uint256 CPOSNonce::entropyMask = UintToArith256(uint256S("00000000000000000000000000000000ffffffffffffffffffffffff00000000"));
arith_uint256 CPOSNonce::posDiffMask = UintToArith256(uint256S("00000000000000000000000000000000000000000000000000000000ffffffff"));
bool CPOSNonce::NewPOSActive(int32_t height)
{
if ((strcmp(ASSETCHAINS_SYMBOL, "VRSC") == 0) && (height < (96480 + 100)))
@@ -38,13 +41,17 @@ void CPOSNonce::SetPOSEntropy(const uint256 &pastHash, uint256 txid, int32_t vou
hashWriter << txid;
hashWriter << voutNum;
arith_uint256 arNonce = (UintToArith256(*this) & 0xffffffff) |
((UintToArith256(hashWriter.GetHash()) & UintToArith256(uint256S("0000000000000000000000000000000000000000ffffffffffffffffffffffff"))) << 32);
arith_uint256 arNonce = (UintToArith256(*this) & posDiffMask) |
(UintToArith256(hashWriter.GetHash()) & entropyMask);
hashWriter = CVerusHashWriter(SER_GETHASH, PROTOCOL_VERSION);
hashWriter << ArithToUint256(arNonce);
// printf("before %s\n", ArithToUint256(arNonce).GetHex().c_str());
*this = CPOSNonce(ArithToUint256(UintToArith256(hashWriter.GetHash()) << 128 | arNonce));
CVerusHashWriter newWriter = CVerusHashWriter(SER_GETHASH, PROTOCOL_VERSION);
newWriter << ArithToUint256(arNonce);
*this = CPOSNonce(ArithToUint256((UintToArith256(newWriter.GetHash()) << 128) | arNonce));
// printf("after %s\n", this->GetHex().c_str());
}
bool CPOSNonce::CheckPOSEntropy(const uint256 &pastHash, uint256 txid, int32_t voutNum)
@@ -57,8 +64,8 @@ bool CPOSNonce::CheckPOSEntropy(const uint256 &pastHash, uint256 txid, int32_t v
hashWriter << txid;
hashWriter << voutNum;
arith_uint256 arNonce = (UintToArith256(*this) & 0xffffffff) |
((UintToArith256(hashWriter.GetHash()) & UintToArith256(uint256S("0000000000000000000000000000000000000000ffffffffffffffffffffffff"))) << 32);
arith_uint256 arNonce = (UintToArith256(*this) & posDiffMask) |
(UintToArith256(hashWriter.GetHash()) & entropyMask);
hashWriter = CVerusHashWriter(SER_GETHASH, PROTOCOL_VERSION);
hashWriter << ArithToUint256(arNonce);

View File

@@ -19,9 +19,12 @@ public:
static bool NewPOSActive(int32_t height);
static bool NewNonceActive(int32_t height);
CPOSNonce() {}
CPOSNonce(const base_blob<256> &b) : uint256(b) {}
CPOSNonce(const std::vector<unsigned char> &vch) : uint256(vch) {}
static arith_uint256 entropyMask;
static arith_uint256 posDiffMask;
CPOSNonce() : uint256() { }
CPOSNonce(const base_blob<256> &b) : uint256(b) { }
CPOSNonce(const std::vector<unsigned char> &vch) : uint256(vch) { }
int32_t GetPOSTarget() const
{
@@ -44,18 +47,14 @@ public:
return (*this == ArithToUint256(UintToArith256(hashWriter.GetHash()) << 128 | tmpNonce));
}
void SetPOSTarget(int32_t nBits)
void SetPOSTarget(uint32_t nBits)
{
CVerusHashWriter hashWriter = CVerusHashWriter(SER_GETHASH, PROTOCOL_VERSION);
arith_uint256 tmpNonce;
arith_uint256 arNonce = UintToArith256(*this);
arNonce = ((arNonce >> 32) << 32) | nBits;
arith_uint256 arNonce = (UintToArith256(*this) & entropyMask) | nBits;
hashWriter << ArithToUint256(arNonce);
tmpNonce = ((arNonce << 128) >> 128);
hashWriter << ArithToUint256(tmpNonce);
(uint256 &)(*this) = ArithToUint256(UintToArith256(hashWriter.GetHash()) << 128 | tmpNonce);
(uint256 &)(*this) = ArithToUint256(UintToArith256(hashWriter.GetHash()) << 128 | arNonce);
}
void SetPOSEntropy(const uint256 &pastHash, uint256 txid, int32_t voutNum);

View File

@@ -998,7 +998,7 @@ CWallet::TxItems CWallet::OrderedTxItems(std::list<CAccountingEntry>& acentries,
// UTXO with the smallest coin age if there is more than one, as larger coin age will win more often and is worth saving
// each attempt consists of taking a VerusHash of the following values:
// ASSETCHAINS_MAGIC, nHeight, txid, voutNum
bool CWallet::VerusSelectStakeOutput(CBlock *pBlock, arith_uint256 &hashResult, CTransaction &stakeSource, int32_t &voutNum, int32_t nHeight, uint32_t bnTarget) const
bool CWallet::VerusSelectStakeOutput(CBlock *pBlock, arith_uint256 &hashResult, CTransaction &stakeSource, int32_t &voutNum, int32_t nHeight, uint32_t &bnTarget) const
{
arith_uint256 target;
arith_uint256 curHash;
@@ -1017,6 +1017,7 @@ bool CWallet::VerusSelectStakeOutput(CBlock *pBlock, arith_uint256 &hashResult,
{
CBlockHeader bh = pastBlockIndex->GetBlockHeader();
uint256 pastHash = bh.GetVerusEntropyHash(nHeight);
CPOSNonce curNonce;
BOOST_FOREACH(COutput &txout, vecOutputs)
{
@@ -1026,19 +1027,21 @@ bool CWallet::VerusSelectStakeOutput(CBlock *pBlock, arith_uint256 &hashResult,
if (Solver(txout.tx->vout[txout.i].scriptPubKey, whichType, vSolutions) && (whichType == TX_PUBKEY || whichType == TX_PUBKEYHASH) &&
(!pwinner || pwinner->tx->vout[pwinner->i].nValue > txout.tx->vout[txout.i].nValue))
pwinner = &txout;
curNonce = pBlock->nNonce;
}
}
if (pwinner)
{
stakeSource = *(pwinner->tx);
voutNum = pwinner->i;
pBlock->nNonce = curNonce;
return true;
}
}
return false;
}
int32_t CWallet::VerusStakeTransaction(CBlock *pBlock, CMutableTransaction &txNew, uint32_t bnTarget, arith_uint256 &hashResult, uint8_t *utxosig) const
int32_t CWallet::VerusStakeTransaction(CBlock *pBlock, CMutableTransaction &txNew, uint32_t &bnTarget, arith_uint256 &hashResult, uint8_t *utxosig) const
{
CTransaction stakeSource;
int32_t voutNum, siglen = 0;

View File

@@ -1149,8 +1149,8 @@ public:
bool ignoreUnspendable=true);
// staking functions
bool VerusSelectStakeOutput(CBlock *pBlock, arith_uint256 &hashResult, CTransaction &stakeSource, int32_t &voutNum, int32_t nHeight, uint32_t bnTarget) const;
int32_t VerusStakeTransaction(CBlock *pBlock, CMutableTransaction &txNew, uint32_t bnTarget, arith_uint256 &hashResult, uint8_t *utxosig) const;
bool VerusSelectStakeOutput(CBlock *pBlock, arith_uint256 &hashResult, CTransaction &stakeSource, int32_t &voutNum, int32_t nHeight, uint32_t &bnTarget) const;
int32_t VerusStakeTransaction(CBlock *pBlock, CMutableTransaction &txNew, uint32_t &bnTarget, arith_uint256 &hashResult, uint8_t *utxosig) const;
};
/** A key allocated from the key pool. */