From 8c411db79ace33d2ae23434b70c4631df62d99e1 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 1 Oct 2014 16:33:28 -0700 Subject: [PATCH] conversion work. --- src/bitcoindjs.cc | 158 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 125 insertions(+), 33 deletions(-) diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 101a14a3..832b45ec 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -2676,17 +2676,74 @@ hextx_to_ctx(std::string tx_hex, CTransaction& ctx) { } } -#if 0 +#if 1 +/* +class CBlockHeader +{ +public: + // header + static const int CURRENT_VERSION=2; + int nVersion; + uint256 hashPrevBlock; + uint256 hashMerkleRoot; + unsigned int nTime; + unsigned int nBits; + unsigned int nNonce; + +class CBlock : public CBlockHeader +{ +public: + // network and disk + std::vector vtx; + + // memory only + mutable std::vector vMerkleTree; + +class CTransaction +{ +public: + static int64_t nMinTxFee; + static int64_t nMinRelayTxFee; + static const int CURRENT_VERSION=1; + int nVersion; + std::vector vin; + std::vector vout; + unsigned int nLockTime; + +class CTxIn +{ +public: + COutPoint prevout; + CScript scriptSig; + unsigned int nSequence; + +class CTxOut +{ +public: + int64_t nValue; + CScript scriptPubKey; + +*/ + static inline void jsblock_to_cblock(const Local obj, CBlock& block, CBlockIndex* blockindex) { - block.GetHash().GetHex().c_str() = obj->Get(NanNew("hash"))->ToString(); - txGen.GetDepthInMainChain()= obj->Get(NanNew("confirmations"))->IntegerValue(); - (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION) = - obj->Get(NanNew("size"))->IntegerValue(); - blockindex->nHeight = obj->Get(NanNew("height"))->IntegerValue(); - block->nVersion = obj->Get(NanNew("version"))->IntegerValue(); - block->hashMerkleRoot = obj->Get(NanNew("merkleroot"))->ToString(); - block->vMerkleTree = obj->Get(NanNew("merkletree"))->ToString(); + // block.GetHash().GetHex().c_str() = obj->Get(NanNew("hash"))->ToString(); + // txGen.GetDepthInMainChain()= obj->Get(NanNew("confirmations"))->IntegerValue(); + // (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION) = + // obj->Get(NanNew("size"))->IntegerValue(); + if (blockindex) { + blockindex->nHeight = obj->Get(NanNew("height"))->IntegerValue(); + } + block->nVersion = (int)obj->Get(NanNew("version"))->IntegerValue(); + + String::Utf8Value mhash__(obj->Get(NanNew("previousblockhash"))->ToString()); + std::string mhash_ = *mhash__; + if (mhash_[1] != 'x') mhash_ = "0x" + mhash_; + uint256 mhash(mhash_); + block->hashMerkleRoot = mhash; + + // Local merkletree = Local::Cast(obj->Get("merkletree")); + // block->vMerkleTree = NULL; Local txs = Local::Cast(obj->Get("tx")); for (int ti = 0; ti < txs->Length(); ti++) { @@ -2695,11 +2752,11 @@ jsblock_to_cblock(const Local obj, CBlock& block, CBlockIndex* blockinde Local entry = NanNew(); - CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); - ssTx << tx; - std::string strHex = HexStr(ssTx.begin(), ssTx.end()); - strHex = entry->Get(NanNew("hex"))->ToString(); - tx.GetHash().GetHex() = entry->Get(NanNew("txid"))->ToString(); + // CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); + // ssTx << tx; + // std::string strHex = HexStr(ssTx.begin(), ssTx.end()); + // strHex = entry->Get(NanNew("hex"))->ToString(); + // tx.GetHash().GetHex() = entry->Get(NanNew("txid"))->ToString(); tx.nVersion = entry->Get(NanNew("version"))->IntegerValue(); tx.nLockTime = entry->Get(NanNew("locktime"))->IntegerValue(); @@ -2708,13 +2765,27 @@ jsblock_to_cblock(const Local obj, CBlock& block, CBlockIndex* blockinde CTxIn txin; Local in = vin->Get(vi); if (in->Get(NanNew("coinbase"))->IsString()) { - HexStr(txin.scriptSig.begin(), txin.scriptSig.end()) = in->Get(NanNew("coinbase"))->ToString(); + String::Utf8Value shash_(obj->Get(NanNew("coinbase"))->ToString()); + std::string shash = *shash_; + CScript scriptSig(shash); + txin.scriptSig = scriptSig; + // HexStr(txin.scriptSig.begin(), txin.scriptSig.end()) = in->Get(NanNew("coinbase"))->ToString(); } else { - txin.prevout.hash.GetHex() = in->Get(NanNew("txid"))->ToString(); - (boost::int64_t)txin.prevout.n = in->Get(NanNew("vout"))->IntegerValue(); - Local o = in->Get(NanNew("scriptSig")); - txin.scriptSig.ToString() = o->Get(NanNew("asm"))->ToString(); - HexStr(txin.scriptSig.begin(), txin.scriptSig.end()) = o->Get(NanNew("hex"))->ToString(); + String::Utf8Value shash_(obj->Get(NanNew("scriptSig"))->ToString()); + std::string shash = *shash_; + CScript scriptSig(shash); + txin.scriptSig = scriptSig; + + // txin.prevout.hash.GetHex() = in->Get(NanNew("txid"))->ToString(); + String::Utf8Value phash__(in->Get(NanNew("txid"))->ToString()); + std::string phash_ = *phash__; + if (phash_[1] != 'x') phash_ = "0x" + phash_; + uint256 phash(phash_); + txin.prevout.hash = phash; + txin.prevout.n = (boost::int64_t)in->Get(NanNew("vout"))->IntegerValue(); + // Local o = in->Get(NanNew("scriptSig")); + // txin.scriptSig.ToString() = o->Get(NanNew("asm"))->ToString(); + // HexStr(txin.scriptSig.begin(), txin.scriptSig.end()) = o->Get(NanNew("hex"))->ToString(); } (boost::int64_t)txin.nSequence = in->Get(NanNew("sequence"))->IntegerValue(); } @@ -2724,9 +2795,19 @@ jsblock_to_cblock(const Local obj, CBlock& block, CBlockIndex* blockinde const CTxOut txout; Local out = vout->Get(vo); - txout.nValue = out->Get(NanNew("value"))->IntegerValue(); - (boost::int64_t)vo = out->Get(NanNew("n"))->IntegerValue(); + txout.nValue = (int64_t)out->Get(NanNew("value"))->IntegerValue(); + // vo = (boost::int64_t)out->Get(NanNew("n"))->IntegerValue(); + + String::Utf8Value phash__(in->Get(NanNew("scriptPubKey"))->Get(NanNew("hex"))); + std::string phash_ = *phash__; + if (phash_[1] != 'x') phash_ = "0x" + phash_; + uint256 phash(phash_); + CScriptPubKey scriptPubKey(phash); + + txout.scriptPubKey = scriptPubKey; + +/* Local o = out->Get(NanNew("scriptPubKey")); { CScript scriptPubKey; @@ -2752,6 +2833,7 @@ jsblock_to_cblock(const Local obj, CBlock& block, CBlockIndex* blockinde } } } +*/ } if (entry->Get(NanNew("blockhash"))->IsString()) { @@ -2767,19 +2849,29 @@ jsblock_to_cblock(const Local obj, CBlock& block, CBlockIndex* blockinde } } - (boost::int64_t)block.GetBlockTime() = obj->Get(NanNew("time"))->IntegerValue(); - (boost::uint64_t)block.nNonce = obj->Get(NanNew("nonce"))->IntegerValue(); - block.nBits = obj->Get(NanNew("bits"))->IntegerValue(); - GetDifficulty(blockindex) = obj->Get(NanNew("difficulty"))->IntegerValue(); - blockindex->nChainWork.GetHex() = obj->Get(NanNew("chainwork"))->ToString(); + block->nTime = (unsigned int)obj->Get(NanNew("time"))->IntegerValue(); + block->nNonce = (unsigned int)obj->Get(NanNew("nonce"))->IntegerValue(); + block->nBits = (unsigned int)obj->Get(NanNew("bits"))->IntegerValue(); + // GetDifficulty(blockindex) = obj->Get(NanNew("difficulty"))->IntegerValue(); + // blockindex->nChainWork.GetHex() = obj->Get(NanNew("chainwork"))->ToString(); if (obj->Get(NanNew("previousblockhash"))->IsString()) { - blockindex->pprev->GetBlockHash().GetHex() = obj->Get(NanNew("previousblockhash"))->ToString(); - } - if (obj->Get(NanNew("nextblockhash"))->IsString()) { - CBlockIndex pnext; - CBlockIndex *pnext = chainActive.Next(blockindex); - pnext->GetBlockHash().GetHex() = obj->Get(NanNew("nextblockhash"))->ToString(); + String::Utf8Value hash__(obj->Get(NanNew("previousblockhash"))->ToString()); + std::string hash_ = *hash__; + if (hash_[1] != 'x') hash_ = "0x" + hash_; + uint256 hash(hash_); + // if (blockindex) { + // blockindex->pprev->GetBlockHash().GetHex() = obj->Get(NanNew("previousblockhash"))->ToString(); + // } + block->hashPrevBlock = hash; + } else { + uint256 hash(std::string("0000000000000000000000000000000000000000000000000000000000000000")); + block->hashPrevBlock = hash; } + // if (obj->Get(NanNew("nextblockhash"))->IsString()) { + // CBlockIndex pnext; + // CBlockIndex *pnext = chainActive.Next(blockindex); + // pnext->GetBlockHash().GetHex() = obj->Get(NanNew("nextblockhash"))->ToString(); + // } } static inline void