Advertisement
Guest User

Satoshi Bitcoin client v0.1.0 market.cpp and market.h

a guest
Mar 25th, 2014
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.47 KB | None | 0 0
  1. ==> market.cpp <==
  2. // Copyright (c) 2009 Satoshi Nakamoto
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file license.txt or http://www.opensource.org/licenses/mit-license.php.
  5.  
  6. #include "headers.h"
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. //
  18. // Global state variables
  19. //
  20.  
  21. //// later figure out how these are persisted
  22. map<uint256, CProduct> mapMyProducts;
  23.  
  24.  
  25.  
  26.  
  27. map<uint256, CProduct> mapProducts;
  28. CCriticalSection cs_mapProducts;
  29.  
  30. bool AdvertInsert(const CProduct& product)
  31. {
  32.     uint256 hash = product.GetHash();
  33.     bool fNew = false;
  34.     bool fUpdated = false;
  35.  
  36.     CRITICAL_BLOCK(cs_mapProducts)
  37.     {
  38.         // Insert or find existing product
  39.         pair<map<uint256, CProduct>::iterator, bool> item = mapProducts.insert(make_pair(hash, product));
  40.         CProduct* pproduct = &(*(item.first)).second;
  41.         fNew = item.second;
  42.  
  43.         // Update if newer
  44.         if (product.nSequence > pproduct->nSequence)
  45.         {
  46.             *pproduct = product;
  47.             fUpdated = true;
  48.         }
  49.     }
  50.  
  51.     //if (fNew)
  52.     //    NotifyProductAdded(hash);
  53.     //else if (fUpdated)
  54.     //    NotifyProductUpdated(hash);
  55.  
  56.     return (fNew || fUpdated);
  57. }
  58.  
  59. void AdvertErase(const CProduct& product)
  60. {
  61.     uint256 hash = product.GetHash();
  62.     CRITICAL_BLOCK(cs_mapProducts)
  63.         mapProducts.erase(hash);
  64.     //NotifyProductDeleted(hash);
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. template<typename T>
  86. unsigned int Union(T& v1, T& v2)
  87. {
  88.     // v1 = v1 union v2
  89.     // v1 and v2 must be sorted
  90.     // returns the number of elements added to v1
  91.  
  92.     ///// need to check that this is equivalent, then delete this comment
  93.     //vector<unsigned short> vUnion(v1.size() + v2.size());
  94.     //vUnion.erase(set_union(v1.begin(), v1.end(),
  95.     //                       v2.begin(), v2.end(),
  96.     //                       vUnion.begin()),
  97.     //             vUnion.end());
  98.  
  99.     T vUnion;
  100.     vUnion.reserve(v1.size() + v2.size());
  101.     set_union(v1.begin(), v1.end(),
  102.               v2.begin(), v2.end(),
  103.               back_inserter(vUnion));
  104.     unsigned int nAdded = vUnion.size() - v1.size();
  105.     if (nAdded > 0)
  106.         v1 = vUnion;
  107.     return nAdded;
  108. }
  109.  
  110. void CUser::AddAtom(unsigned short nAtom, bool fOrigin)
  111. {
  112.     // Ignore duplicates
  113.     if (binary_search(vAtomsIn.begin(), vAtomsIn.end(), nAtom) ||
  114.         find(vAtomsNew.begin(), vAtomsNew.end(), nAtom) != vAtomsNew.end())
  115.         return;
  116.  
  117.     //// instead of zero atom, should change to free atom that propagates,
  118.     //// limited to lower than a certain value like 5 so conflicts quickly
  119.     // The zero atom never propagates,
  120.     // new atoms always propagate through the user that created them
  121.     if (nAtom == 0 || fOrigin)
  122.     {
  123.         vector<unsigned short> vTmp(1, nAtom);
  124.         Union(vAtomsIn, vTmp);
  125.         if (fOrigin)
  126.             vAtomsOut.push_back(nAtom);
  127.         return;
  128.     }
  129.  
  130.     vAtomsNew.push_back(nAtom);
  131.  
  132.     if (vAtomsNew.size() >= nFlowthroughRate || vAtomsOut.empty())
  133.     {
  134.         // Select atom to flow through to vAtomsOut
  135.         vAtomsOut.push_back(vAtomsNew[GetRand(vAtomsNew.size())]);
  136.  
  137.         // Merge vAtomsNew into vAtomsIn
  138.         sort(vAtomsNew.begin(), vAtomsNew.end());
  139.         Union(vAtomsIn, vAtomsNew);
  140.         vAtomsNew.clear();
  141.     }
  142. }
  143.  
  144. bool AddAtomsAndPropagate(uint256 hashUserStart, const vector<unsigned short>& vAtoms, bool fOrigin)
  145. {
  146.     CReviewDB reviewdb;
  147.     map<uint256, vector<unsigned short> > pmapPropagate[2];
  148.     pmapPropagate[0][hashUserStart] = vAtoms;
  149.  
  150.     for (int side = 0; !pmapPropagate[side].empty(); side = 1 - side)
  151.     {
  152.         map<uint256, vector<unsigned short> >& mapFrom = pmapPropagate[side];
  153.         map<uint256, vector<unsigned short> >& mapTo = pmapPropagate[1 - side];
  154.  
  155.         for (map<uint256, vector<unsigned short> >::iterator mi = mapFrom.begin(); mi != mapFrom.end(); ++mi)
  156.         {
  157.             const uint256& hashUser = (*mi).first;
  158.             const vector<unsigned short>& vReceived = (*mi).second;
  159.  
  160.             ///// this would be a lot easier on the database if it put the new atom at the beginning of the list,
  161.             ///// so the change would be right next to the vector size.
  162.  
  163.             // Read user
  164.             CUser user;
  165.             reviewdb.ReadUser(hashUser, user);
  166.             unsigned int nIn = user.vAtomsIn.size();
  167.             unsigned int nNew = user.vAtomsNew.size();
  168.             unsigned int nOut = user.vAtomsOut.size();
  169.  
  170.             // Add atoms received
  171.             foreach(unsigned short nAtom, vReceived)
  172.                 user.AddAtom(nAtom, fOrigin);
  173.             fOrigin = false;
  174.  
  175.             // Don't bother writing to disk if no changes
  176.             if (user.vAtomsIn.size() == nIn && user.vAtomsNew.size() == nNew)
  177.                 continue;
  178.  
  179.             // Propagate
  180.             if (user.vAtomsOut.size() > nOut)
  181.                 foreach(const uint256& hash, user.vLinksOut)
  182.                     mapTo[hash].insert(mapTo[hash].end(), user.vAtomsOut.begin() + nOut, user.vAtomsOut.end());
  183.  
  184.             // Write back
  185.             if (!reviewdb.WriteUser(hashUser, user))
  186.                 return false;
  187.         }
  188.         mapFrom.clear();
  189.     }
  190.     return true;
  191. }
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198. bool CReview::AcceptReview()
  199. {
  200.     // Timestamp
  201.     nTime = GetTime();
  202.  
  203.     // Check signature
  204.     if (!CKey::Verify(vchPubKeyFrom, GetSigHash(), vchSig))
  205.         return false;
  206.  
  207.     CReviewDB reviewdb;
  208.  
  209.     // Add review text to recipient
  210.     vector<CReview> vReviews;
  211.     reviewdb.ReadReviews(hashTo, vReviews);
  212.     vReviews.push_back(*this);
  213.     if (!reviewdb.WriteReviews(hashTo, vReviews))
  214.         return false;
  215.  
  216.     // Add link from sender
  217.     CUser user;
  218.     uint256 hashFrom = Hash(vchPubKeyFrom.begin(), vchPubKeyFrom.end());
  219.     reviewdb.ReadUser(hashFrom, user);
  220.     user.vLinksOut.push_back(hashTo);
  221.     if (!reviewdb.WriteUser(hashFrom, user))
  222.         return false;
  223.  
  224.     reviewdb.Close();
  225.  
  226.     // Propagate atoms to recipient
  227.     vector<unsigned short> vZeroAtom(1, 0);
  228.     if (!AddAtomsAndPropagate(hashTo, user.vAtomsOut.size() ? user.vAtomsOut : vZeroAtom, false))
  229.         return false;
  230.  
  231.     return true;
  232. }
  233.  
  234.  
  235.  
  236.  
  237.  
  238. bool CProduct::CheckSignature()
  239. {
  240.     return (CKey::Verify(vchPubKeyFrom, GetSigHash(), vchSig));
  241. }
  242.  
  243. bool CProduct::CheckProduct()
  244. {
  245.     if (!CheckSignature())
  246.         return false;
  247.  
  248.     // Make sure it's a summary product
  249.     if (!mapDetails.empty() || !vOrderForm.empty())
  250.         return false;
  251.  
  252.     // Look up seller's atom count
  253.     CReviewDB reviewdb("r");
  254.     CUser user;
  255.     reviewdb.ReadUser(GetUserHash(), user);
  256.     nAtoms = user.GetAtomCount();
  257.     reviewdb.Close();
  258.  
  259.     ////// delme, this is now done by AdvertInsert
  260.     //// Store to memory
  261.     //CRITICAL_BLOCK(cs_mapProducts)
  262.     //    mapProducts[GetHash()] = *this;
  263.  
  264.     return true;
  265. }
  266.  
  267. ==> market.h <==
  268. // Copyright (c) 2009 Satoshi Nakamoto
  269. // Distributed under the MIT/X11 software license, see the accompanying
  270. // file license.txt or http://www.opensource.org/licenses/mit-license.php.
  271.  
  272. class CUser;
  273. class CReview;
  274. class CProduct;
  275.  
  276. static const unsigned int nFlowthroughRate = 2;
  277.  
  278.  
  279.  
  280.  
  281. bool AdvertInsert(const CProduct& product);
  282. void AdvertErase(const CProduct& product);
  283. bool AddAtomsAndPropagate(uint256 hashUserStart, const vector<unsigned short>& vAtoms, bool fOrigin);
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292. class CUser
  293. {
  294. public:
  295.     vector<unsigned short> vAtomsIn;
  296.     vector<unsigned short> vAtomsNew;
  297.     vector<unsigned short> vAtomsOut;
  298.     vector<uint256> vLinksOut;
  299.  
  300.     CUser()
  301.     {
  302.     }
  303.  
  304.     IMPLEMENT_SERIALIZE
  305.     (
  306.         if (!(nType & SER_GETHASH))
  307.             READWRITE(nVersion);
  308.         READWRITE(vAtomsIn);
  309.         READWRITE(vAtomsNew);
  310.         READWRITE(vAtomsOut);
  311.         READWRITE(vLinksOut);
  312.     )
  313.  
  314.     void SetNull()
  315.     {
  316.         vAtomsIn.clear();
  317.         vAtomsNew.clear();
  318.         vAtomsOut.clear();
  319.         vLinksOut.clear();
  320.     }
  321.  
  322.     uint256 GetHash() const { return SerializeHash(*this); }
  323.  
  324.  
  325.     int GetAtomCount() const
  326.     {
  327.         return (vAtomsIn.size() + vAtomsNew.size());
  328.     }
  329.  
  330.     void AddAtom(unsigned short nAtom, bool fOrigin);
  331. };
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339. class CReview
  340. {
  341. public:
  342.     int nVersion;
  343.     uint256 hashTo;
  344.     map<string, string> mapValue;
  345.     vector<unsigned char> vchPubKeyFrom;
  346.     vector<unsigned char> vchSig;
  347.  
  348.     // memory only
  349.     unsigned int nTime;
  350.     int nAtoms;
  351.  
  352.  
  353.     CReview()
  354.     {
  355.         nVersion = 1;
  356.         hashTo = 0;
  357.         nTime = 0;
  358.         nAtoms = 0;
  359.     }
  360.  
  361.     IMPLEMENT_SERIALIZE
  362.     (
  363.         READWRITE(this->nVersion);
  364.         nVersion = this->nVersion;
  365.         if (!(nType & SER_DISK))
  366.             READWRITE(hashTo);
  367.         READWRITE(mapValue);
  368.         READWRITE(vchPubKeyFrom);
  369.         if (!(nType & SER_GETHASH))
  370.             READWRITE(vchSig);
  371.     )
  372.  
  373.     uint256 GetHash() const { return SerializeHash(*this); }
  374.     uint256 GetSigHash() const { return SerializeHash(*this, SER_GETHASH|SER_SKIPSIG); }
  375.     uint256 GetUserHash() const { return Hash(vchPubKeyFrom.begin(), vchPubKeyFrom.end()); }
  376.  
  377.  
  378.     bool AcceptReview();
  379. };
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387. class CProduct
  388. {
  389. public:
  390.     int nVersion;
  391.     CAddress addr;
  392.     map<string, string> mapValue;
  393.     map<string, string> mapDetails;
  394.     vector<pair<string, string> > vOrderForm;
  395.     unsigned int nSequence;
  396.     vector<unsigned char> vchPubKeyFrom;
  397.     vector<unsigned char> vchSig;
  398.  
  399.     // disk only
  400.     int nAtoms;
  401.  
  402.     // memory only
  403.     set<unsigned int> setSources;
  404.  
  405.     CProduct()
  406.     {
  407.         nVersion = 1;
  408.         nAtoms = 0;
  409.         nSequence = 0;
  410.     }
  411.  
  412.     IMPLEMENT_SERIALIZE
  413.     (
  414.         READWRITE(this->nVersion);
  415.         nVersion = this->nVersion;
  416.         READWRITE(addr);
  417.         READWRITE(mapValue);
  418.         if (!(nType & SER_GETHASH))
  419.         {
  420.             READWRITE(mapDetails);
  421.             READWRITE(vOrderForm);
  422.             READWRITE(nSequence);
  423.         }
  424.         READWRITE(vchPubKeyFrom);
  425.         if (!(nType & SER_GETHASH))
  426.             READWRITE(vchSig);
  427.         if (nType & SER_DISK)
  428.             READWRITE(nAtoms);
  429.     )
  430.  
  431.     uint256 GetHash() const { return SerializeHash(*this); }
  432.     uint256 GetSigHash() const { return SerializeHash(*this, SER_GETHASH|SER_SKIPSIG); }
  433.     uint256 GetUserHash() const { return Hash(vchPubKeyFrom.begin(), vchPubKeyFrom.end()); }
  434.  
  435.  
  436.     bool CheckSignature();
  437.     bool CheckProduct();
  438. };
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447. extern map<uint256, CProduct> mapProducts;
  448. extern CCriticalSection cs_mapProducts;
  449. extern map<uint256, CProduct> mapMyProducts;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement