warning to 32bit users
[electrum-server.git] / patches / bitcoin-0.6.3.diff
1 diff -ur bitcoin-0.6.3-orig/src/bitcoinrpc.cpp bitcoin-0.6.3/src/bitcoinrpc.cpp
2 --- bitcoin-0.6.3-orig/src/bitcoinrpc.cpp       2012-06-19 13:44:55.000000000 -0700
3 +++ bitcoin-0.6.3/src/bitcoinrpc.cpp    2012-07-09 11:56:21.329502578 -0700
4 @@ -1501,6 +1501,43 @@
5  }
6  
7  
8 +Value importtransaction(const Array& params, bool fHelp)
9 +{
10 +  string hexdump;
11 +  if (fHelp || params.size() != 1 || (hexdump=params[0].get_str()).size()&1)
12 +    throw runtime_error(
13 +            "importtransaction <hexdata>\n"
14 +            "Import an offline transaction to announce it into the network");
15 +
16 +  std::vector<unsigned char> rawtx;
17 +  for (int i=0; i<hexdump.size(); i+=2)
18 +    {
19 +      int v;
20 +      if (sscanf(hexdump.substr(i,2).c_str(), "%x", &v)!=1)
21 +       throw JSONRPCError(-4, "Error in hex data.");
22 +      rawtx.push_back((unsigned char)v);
23 +    }
24 +try
25 +  {
26 +    CDataStream ss(rawtx, SER_NETWORK, PROTOCOL_VERSION);
27 +    CTransaction tx;
28 +    ss >> tx;
29 +    CInv inv(MSG_TX, tx.GetHash());
30 +
31 +    CTxDB txdb("r");
32 +    if(! tx.AcceptToMemoryPool(txdb, true)) throw JSONRPCError(-4, "Transaction not accepted to memory pool.");
33 +    CDataStream msg(rawtx, SER_NETWORK, PROTOCOL_VERSION);
34 +    RelayMessage(inv, msg);
35 +    return tx.GetHash().GetHex();
36 +  }
37 + catch (std::exception& e)
38 +   {
39 +     throw JSONRPCError(-4, "Exception while parsing the transaction data.");
40 +   }
41 +
42 +}
43 +
44 +
45  Value backupwallet(const Array& params, bool fHelp)
46  {
47      if (fHelp || params.size() != 1)
48 @@ -1937,8 +1974,6 @@
49          result.push_back(Pair("version", pblock->nVersion));
50          result.push_back(Pair("previousblockhash", pblock->hashPrevBlock.GetHex()));
51          result.push_back(Pair("transactions", transactions));
52 -        result.push_back(Pair("coinbasevalue", (int64_t)pblock->vtx[0].vout[0].nValue));
53 -        result.push_back(Pair("coinbaseflags", HexStr(COINBASE_FLAGS.begin(), COINBASE_FLAGS.end())));
54          result.push_back(Pair("time", (int64_t)pblock->nTime));
55          result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1));
56          result.push_back(Pair("curtime", (int64_t)GetAdjustedTime()));
57 @@ -2059,6 +2094,7 @@
58      { "listsinceblock",         &listsinceblock,         false },
59      { "dumpprivkey",            &dumpprivkey,            false },
60      { "importprivkey",          &importprivkey,          false },
61 +    { "importtransaction",      &importtransaction,      false },
62  };
63  
64  CRPCTable::CRPCTable()
65 diff -ur bitcoin-0.6.3-orig/src/main.cpp bitcoin-0.6.3/src/main.cpp
66 --- bitcoin-0.6.3-orig/src/main.cpp     2012-06-19 13:44:55.000000000 -0700
67 +++ bitcoin-0.6.3/src/main.cpp  2012-07-09 12:00:26.989497203 -0700
68 @@ -3189,156 +3189,28 @@
69      if (!pblock.get())
70          return NULL;
71  
72 -    // Create coinbase tx
73 -    CTransaction txNew;
74 -    txNew.vin.resize(1);
75 -    txNew.vin[0].prevout.SetNull();
76 -    txNew.vout.resize(1);
77 -    txNew.vout[0].scriptPubKey << reservekey.GetReservedKey() << OP_CHECKSIG;
78 -
79 -    // Add our coinbase tx as first transaction
80 -    pblock->vtx.push_back(txNew);
81 -
82      // Collect memory pool transactions into the block
83 -    int64 nFees = 0;
84      {
85          LOCK2(cs_main, mempool.cs);
86          CTxDB txdb("r");
87  
88 -        // Priority order to process transactions
89 -        list<COrphan> vOrphan; // list memory doesn't move
90 -        map<uint256, vector<COrphan*> > mapDependers;
91 -        multimap<double, CTransaction*> mapPriority;
92 -        for (map<uint256, CTransaction>::iterator mi = mempool.mapTx.begin(); mi != mempool.mapTx.end(); ++mi)
93 -        {
94 -            CTransaction& tx = (*mi).second;
95 -            if (tx.IsCoinBase() || !tx.IsFinal())
96 -                continue;
97 -
98 -            COrphan* porphan = NULL;
99 -            double dPriority = 0;
100 -            BOOST_FOREACH(const CTxIn& txin, tx.vin)
101 -            {
102 -                // Read prev transaction
103 -                CTransaction txPrev;
104 -                CTxIndex txindex;
105 -                if (!txPrev.ReadFromDisk(txdb, txin.prevout, txindex))
106 -                {
107 -                    // Has to wait for dependencies
108 -                    if (!porphan)
109 -                    {
110 -                        // Use list for automatic deletion
111 -                        vOrphan.push_back(COrphan(&tx));
112 -                        porphan = &vOrphan.back();
113 -                    }
114 -                    mapDependers[txin.prevout.hash].push_back(porphan);
115 -                    porphan->setDependsOn.insert(txin.prevout.hash);
116 -                    continue;
117 -                }
118 -                int64 nValueIn = txPrev.vout[txin.prevout.n].nValue;
119 -
120 -                // Read block header
121 -                int nConf = txindex.GetDepthInMainChain();
122 -
123 -                dPriority += (double)nValueIn * nConf;
124 -
125 -                if (fDebug && GetBoolArg("-printpriority"))
126 -                    printf("priority     nValueIn=%-12"PRI64d" nConf=%-5d dPriority=%-20.1f\n", nValueIn, nConf, dPriority);
127 -            }
128 -
129 -            // Priority is sum(valuein * age) / txsize
130 -            dPriority /= ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
131 -
132 -            if (porphan)
133 -                porphan->dPriority = dPriority;
134 -            else
135 -                mapPriority.insert(make_pair(-dPriority, &(*mi).second));
136 -
137 -            if (fDebug && GetBoolArg("-printpriority"))
138 -            {
139 -                printf("priority %-20.1f %s\n%s", dPriority, tx.GetHash().ToString().substr(0,10).c_str(), tx.ToString().c_str());
140 -                if (porphan)
141 -                    porphan->print();
142 -                printf("\n");
143 -            }
144 -        }
145 -
146 -        // Collect transactions into block
147 -        map<uint256, CTxIndex> mapTestPool;
148 -        uint64 nBlockSize = 1000;
149          uint64 nBlockTx = 0;
150 -        int nBlockSigOps = 100;
151 -        while (!mapPriority.empty())
152 +        for (map<uint256, CTransaction>::iterator mi = mempool.mapTx.begin(); mi != mempool.mapTx.end(); ++mi)
153          {
154 -            // Take highest priority transaction off priority queue
155 -            double dPriority = -(*mapPriority.begin()).first;
156 -            CTransaction& tx = *(*mapPriority.begin()).second;
157 -            mapPriority.erase(mapPriority.begin());
158 -
159 -            // Size limits
160 -            unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
161 -            if (nBlockSize + nTxSize >= MAX_BLOCK_SIZE_GEN)
162 -                continue;
163 -
164 -            // Legacy limits on sigOps:
165 -            unsigned int nTxSigOps = tx.GetLegacySigOpCount();
166 -            if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
167 -                continue;
168 -
169 -            // Transaction fee required depends on block size
170 -            bool fAllowFree = (nBlockSize + nTxSize < 4000 || CTransaction::AllowFree(dPriority));
171 -            int64 nMinFee = tx.GetMinFee(nBlockSize, fAllowFree, GMF_BLOCK);
172 +          CTransaction& tx = (*mi).second;
173 +          if (tx.IsCoinBase() || !tx.IsFinal())
174 +            continue;
175  
176 -            // Connecting shouldn't fail due to dependency on other memory pool transactions
177 -            // because we're already processing them in order of dependency
178 -            map<uint256, CTxIndex> mapTestPoolTmp(mapTestPool);
179 -            MapPrevTx mapInputs;
180 -            bool fInvalid;
181 -            if (!tx.FetchInputs(txdb, mapTestPoolTmp, false, true, mapInputs, fInvalid))
182 -                continue;
183 -
184 -            int64 nTxFees = tx.GetValueIn(mapInputs)-tx.GetValueOut();
185 -            if (nTxFees < nMinFee)
186 -                continue;
187 -
188 -            nTxSigOps += tx.GetP2SHSigOpCount(mapInputs);
189 -            if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
190 -                continue;
191 -
192 -            if (!tx.ConnectInputs(mapInputs, mapTestPoolTmp, CDiskTxPos(1,1,1), pindexPrev, false, true))
193 -                continue;
194 -            mapTestPoolTmp[tx.GetHash()] = CTxIndex(CDiskTxPos(1,1,1), tx.vout.size());
195 -            swap(mapTestPool, mapTestPoolTmp);
196 -
197 -            // Added
198 -            pblock->vtx.push_back(tx);
199 -            nBlockSize += nTxSize;
200 -            ++nBlockTx;
201 -            nBlockSigOps += nTxSigOps;
202 -            nFees += nTxFees;
203 -
204 -            // Add transactions that depend on this one to the priority queue
205 -            uint256 hash = tx.GetHash();
206 -            if (mapDependers.count(hash))
207 +          if (!tx.get_electrum_flag())
208              {
209 -                BOOST_FOREACH(COrphan* porphan, mapDependers[hash])
210 -                {
211 -                    if (!porphan->setDependsOn.empty())
212 -                    {
213 -                        porphan->setDependsOn.erase(hash);
214 -                        if (porphan->setDependsOn.empty())
215 -                            mapPriority.insert(make_pair(-porphan->dPriority, porphan->ptx));
216 -                    }
217 -                }
218 +                tx.set_electrum_flag(true);
219 +                pblock->vtx.push_back(tx);
220 +                ++nBlockTx;
221              }
222          }
223  
224 -        nLastBlockTx = nBlockTx;
225 -        nLastBlockSize = nBlockSize;
226 -        printf("CreateNewBlock(): total size %lu\n", nBlockSize);
227 -
228 +        printf("CreateNewBlock(): transactions: %lu\n", nBlockTx);
229      }
230 -    pblock->vtx[0].vout[0].nValue = GetBlockValue(pindexPrev->nHeight+1, nFees);
231  
232      // Fill in header
233      pblock->hashPrevBlock  = pindexPrev->GetBlockHash();
234 diff -ur bitcoin-0.6.3-orig/src/main.h bitcoin-0.6.3/src/main.h
235 --- bitcoin-0.6.3-orig/src/main.h       2012-06-19 13:44:55.000000000 -0700
236 +++ bitcoin-0.6.3/src/main.h    2012-07-09 11:56:21.333502571 -0700
237 @@ -395,6 +395,16 @@
238      mutable int nDoS;
239      bool DoS(int nDoSIn, bool fIn) const { nDoS += nDoSIn; return fIn; }
240  
241 +    bool electrum_flag;
242 +
243 +    void set_electrum_flag(bool x){
244 +      electrum_flag = x;
245 +    }
246 +
247 +    bool get_electrum_flag(){
248 +      return electrum_flag;
249 +    }
250 +
251      CTransaction()
252      {
253          SetNull();
254 @@ -416,6 +426,7 @@
255          vout.clear();
256          nLockTime = 0;
257          nDoS = 0;  // Denial-of-service prevention
258 +       set_electrum_flag(false);
259      }
260  
261      bool IsNull() const