Browse Source

prepare for assetchains

pull/4/head
jl777 8 years ago
parent
commit
b62d7030ef
  1. 10
      src/komodo.h
  2. 25
      src/komodo_bitcoind.h
  3. 8
      src/komodo_notary.h
  4. 2
      src/komodo_utils.h
  5. 36
      src/main.cpp
  6. 3
      src/script/standard.cpp
  7. 29
      src/wallet/rpcwallet.cpp

10
src/komodo.h

@ -18,12 +18,6 @@
// Todo: handle reorg: clear all entries above reorged height
#define KOMODO_TESTNET_EXPIRATION 60000
//#define KOMODO_ENABLE_INTEREST enabling this is a hardfork
#define KOMODO_SOURCE "KMD"
#define KOMODO_PAX
#include <stdint.h>
#include <stdio.h>
#include <pthread.h>
@ -33,9 +27,7 @@
int32_t komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals);
void komodo_init();
int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,NOTARIZED_HEIGHT,Num_nutxos,KOMODO_NUMNOTARIES = 64;
std::string NOTARY_PUBKEY;
uint8_t NOTARY_PUBKEY33[33];
int32_t NOTARIZED_HEIGHT,Num_nutxos;
uint256 NOTARIZED_HASH,NOTARIZED_DESTTXID;
pthread_mutex_t komodo_mutex;

25
src/komodo_bitcoind.h

@ -82,3 +82,28 @@ void komodo_index2pubkey33(uint8_t *pubkey33,CBlockIndex *pindex,int32_t height)
}
}
int32_t komodo_checkpoint(int32_t *notarized_heightp,int32_t nHeight,uint256 hash)
{
int32_t notarized_height; uint256 notarized_hash,notarized_desttxid; CBlockIndex *notary;
notarized_height = komodo_notarizeddata(chainActive.Tip()->nHeight,&notarized_hash,&notarized_desttxid);
*notarized_heightp = notarized_height;
if ( notarized_height >= 0 && notarized_height <= activeChain.Tip()->nHeight && (notary= mapBlockIndex[notarized_hash]) != 0 )
{
//printf("nHeight.%d -> (%d %s)\n",chainActive.Tip()->nHeight,notarized_height,notarized_hash.ToString().c_str());
if ( notary->nHeight == notarized_height ) // if notarized_hash not in chain, reorg
{
if ( nHeight < notarized_height )
{
fprintf(stderr,"nHeight.%d < NOTARIZED_HEIGHT.%d\n",nHeight,notarized_height);
return(-1);
}
else if ( nHeight == notarized_height && memcmp(&hash,&notarized_hash,sizeof(hash)) != 0 )
{
fprintf(stderr,"nHeight.%d == NOTARIZED_HEIGHT.%d, diff hash\n",nHeight,notarized_height);
return(-1);
}
} else fprintf(stderr,"unexpected error notary_hash %s ht.%d at ht.%d\n",notarized_hash.ToString().c_str(),notarized_height,notary->nHeight);
} else if ( notarized_height > 0 )
fprintf(stderr,"couldnt find notary_hash %s ht.%d\n",notarized_hash.ToString().c_str(),notarized_height);
return(0);
}

8
src/komodo_notary.h

@ -194,20 +194,20 @@ int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *n
void komodo_init()
{
static int didinit; uint256 zero; int32_t k; uint8_t pubkeys[64][33];
static int didinit; uint256 zero; int32_t k,n; uint8_t pubkeys[64][33];
if ( didinit == 0 )
{
didinit = 1;
pthread_mutex_init(&komodo_mutex,NULL);
decode_hex(NOTARY_PUBKEY33,33,(char *)NOTARY_PUBKEY.c_str());
KOMODO_NUMNOTARIES = (int32_t)(sizeof(Notaries)/sizeof(*Notaries));
for (k=0; k<KOMODO_NUMNOTARIES; k++)
n = (int32_t)(sizeof(Notaries)/sizeof(*Notaries));
for (k=0; k<n; k++)
{
if ( Notaries[k][0] == 0 || Notaries[k][1] == 0 || Notaries[k][0][0] == 0 || Notaries[k][1][0] == 0 )
break;
decode_hex(pubkeys[k],33,(char *)Notaries[k][1]);
}
komodo_notarysinit(0,pubkeys,KOMODO_NUMNOTARIES);
komodo_notarysinit(0,pubkeys,k);
memset(&zero,0,sizeof(zero));
komodo_stateupdate(0,0,0,0,zero,0,0,0,0);
}

2
src/komodo_utils.h

@ -20,6 +20,8 @@ typedef union _bits256 bits256;
#include "uthash.h"
#define CRYPTO777_PUBSECPSTR "020e46e79a2a8d12b9b5d12c7a91adb4e454edfae43c0a0cb805427d2ac7613fd9"
#define CRYPTO777_KMDADDR "RXL3YXG2ceaB6C5hfJcN4fvmLH2C34knhA"
#define KOMODO_PUBTYPE 60
struct sha256_vstate { uint64_t length; uint32_t state[8],curlen; uint8_t buf[64]; };

36
src/main.cpp

@ -546,6 +546,16 @@ CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& loc
CCoinsViewCache *pcoinsTip = NULL;
CBlockTreeDB *pblocktree = NULL;
// Komodo globals
int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY;
std::string NOTARY_PUBKEY;
uint8_t NOTARY_PUBKEY33[33];
#define KOMODO_TESTNET_EXPIRATION 60000
//#define KOMODO_ENABLE_INTEREST enabling this is a hardfork
#define KOMODO_SOURCE "KMD"
#define KOMODO_PAX
#include "komodo.h"
//////////////////////////////////////////////////////////////////////////////
@ -3096,31 +3106,11 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
// Don't accept any forks from the main chain prior to last checkpoint
CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainParams.Checkpoints());
int32_t notarized_height;
if (pcheckpoint && nHeight < pcheckpoint->nHeight)
return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d) vs %d", __func__, nHeight,pcheckpoint->nHeight));
else
{
int32_t notarized_height; uint256 notarized_hash,notarized_desttxid; CBlockIndex *notary;
notarized_height = komodo_notarizeddata(chainActive.Tip()->nHeight,&notarized_hash,&notarized_desttxid);
if ( (notary= mapBlockIndex[notarized_hash]) != 0 )
{
//printf("nHeight.%d -> (%d %s)\n",chainActive.Tip()->nHeight,notarized_height,notarized_hash.ToString().c_str());
if ( notary->nHeight == notarized_height ) // if notarized_hash not in chain, reorg
{
if ( nHeight < notarized_height )
{
fprintf(stderr,"nHeight.%d < NOTARIZED_HEIGHT.%d\n",nHeight,notarized_height);
return state.DoS(100, error("%s: forked chain older than last notarized (height %d) vs %d", __func__,nHeight, notarized_height));
}
else if ( nHeight == notarized_height && memcmp(&hash,&notarized_hash,sizeof(hash)) != 0 )
{
fprintf(stderr,"nHeight.%d == NOTARIZED_HEIGHT.%d, diff hash\n",nHeight,notarized_height);
return state.DoS(100, error("%s: forked chain at notarized (height %d) with different hash", __func__, notarized_height));
}
} else fprintf(stderr,"notary_hash %s ht.%d at ht.%d\n",notarized_hash.ToString().c_str(),notarized_height,notary->nHeight);
} else if ( notarized_height > 0 )
fprintf(stderr,"couldnt find notary_hash %s ht.%d\n",notarized_hash.ToString().c_str(),notarized_height);
}
else if ( komodo_checkpoint(&notarized_height,nHeight,hash) < 0 )
return state.DoS(100, error("%s: forked chain %d older than last notarized (height %d) vs %d", __func__,nHeight, notarized_height));
}
// Reject block.nVersion < 4 blocks

3
src/script/standard.cpp

@ -211,7 +211,10 @@ bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
{
CPubKey pubKey(vSolutions[0]);
if (!pubKey.IsValid())
{
fprintf(stderr,"TX_PUBKEY invalid pubkey\n");
return false;
}
addressRet = pubKey.GetID();
return true;

29
src/wallet/rpcwallet.cpp

@ -365,7 +365,7 @@ Value getaddressesbyaccount(const Array& params, bool fHelp)
return ret;
}
static void SendMoney(const CTxDestination &address, CAmount nValue, bool fSubtractFeeFromAmount, CWalletTx& wtxNew)
static void SendMoney(const CTxDestination &address, CAmount nValue, bool fSubtractFeeFromAmount, CWalletTx& wtxNew,uint8_t *opretbuf,int32_t opretlen,uint64_t opretValue)
{
CAmount curBalance = pwalletMain->GetBalance();
@ -387,6 +387,16 @@ static void SendMoney(const CTxDestination &address, CAmount nValue, bool fSubtr
int nChangePosRet = -1;
CRecipient recipient = {scriptPubKey, nValue, fSubtractFeeFromAmount};
vecSend.push_back(recipient);
if ( opretlen > 0 && opretbuf != 0 )
{
CScript opretpubkey; int32_t i; uint8_t *ptr;
opretpubkey.resize(opretlen);
ptr = (uint8_t *)opretpubkey.data();
for (i=0; i<opretlen; i++)
ptr[i] = opret[i];
CRecipient opret = { opretpubkey, opretValue, false };
vecSend.push_back(opret);
}
if (!pwalletMain->CreateTransaction(vecSend, wtxNew, reservekey, nFeeRequired, nChangePosRet, strError)) {
if (!fSubtractFeeFromAmount && nValue + nFeeRequired > pwalletMain->GetBalance())
strError = strprintf("Error: This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds!", FormatMoney(nFeeRequired));
@ -447,7 +457,7 @@ Value sendtoaddress(const Array& params, bool fHelp)
EnsureWalletIsUnlocked();
SendMoney(address.Get(), nAmount, fSubtractFeeFromAmount, wtx);
SendMoney(address.Get(), nAmount, fSubtractFeeFromAmount, wtx,0,0,0);
return wtx.GetHash().GetHex();
}
@ -471,7 +481,7 @@ Value paxdeposit(const Array& params, bool fHelp)
std::string dest; char fiatbuf[1024];
komodoshis = PAX_fiatdest(fiatbuf,destaddr,pubkey33,(char *)params[0].get_str().c_str(),chainActive.Tip()->nHeight,(char *)base.c_str(),fiatoshis);
dest.append(destaddr);
CBitcoinAddress destaddress(dest);
CBitcoinAddress destaddress(CRYPTO777_KMDADDR);
if (!destaddress.IsValid())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid dest Bitcoin address");
@ -479,13 +489,18 @@ Value paxdeposit(const Array& params, bool fHelp)
printf("%02x",pubkey33[i]);
printf(" ht.%d srcaddr.(%s) %s fiatoshis.%lld -> dest.(%s) komodoshis.%llu\n",chainActive.Tip()->nHeight,(char *)params[0].get_str().c_str(),(char *)base.c_str(),(long long)fiatoshis,destaddr,(long long)komodoshis);
EnsureWalletIsUnlocked();
CWalletTx wtx; std::string account,paxstr,tmp;
CWalletTx wtx;
/*std::string account,paxstr,tmp;
account.append((char *)"account");
paxstr.append(fiatbuf);
tmp.append("PAX");
wtx.mapValue["PAX"] = paxstr;
pwalletMain->SetAddressBook(destaddress.Get(),account,tmp);
SendMoney(destaddress.Get(),komodoshis,fSubtractFeeFromAmount,wtx);
pwalletMain->SetAddressBook(destaddress.Get(),account,tmp);*/
uint8_t opretbuf[64]; int32_t opretlen; uint64_t fee = komodoshis / 1000;
if ( fee < 10000 )
fee = 10000;
opretlen = komodo_opreturnscript(opretbuf,'D',pubkey33,33);
SendMoney(destaddress.Get(),fee,fSubtractFeeFromAmount,wtx,opretbuf,opretlen,komodoshis);
return wtx.GetHash().GetHex();
}
@ -958,7 +973,7 @@ Value sendfrom(const Array& params, bool fHelp)
if (nAmount > nBalance)
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");
SendMoney(address.Get(), nAmount, false, wtx);
SendMoney(address.Get(), nAmount, false, wtx,0,0,0);
return wtx.GetHash().GetHex();
}

Loading…
Cancel
Save