diff options
Diffstat (limited to 'src/net.h')
| -rw-r--r-- | src/net.h | 280 |
1 files changed, 144 insertions, 136 deletions
| @@ -5,7 +5,6 @@ | |||
| 5 | class CMessageHeader; | 5 | class CMessageHeader; |
| 6 | class CAddress; | 6 | class CAddress; |
| 7 | class CInv; | 7 | class CInv; |
| 8 | class CRequestTracker; | ||
| 9 | class CNode; | 8 | class CNode; |
| 10 | // Defined in main.h, which is included after this file. Only ever used here as | 9 | // Defined in main.h, which is included after this file. Only ever used here as |
| 11 | // a pointer, so the forward declaration is enough. | 10 | // a pointer, so the forward declaration is enough. |
| @@ -13,6 +12,10 @@ class CBlockIndex; | |||
| 13 | 12 | ||
| 14 | 13 | ||
| 15 | 14 | ||
| 15 | // Held in host byte order, unlike the DEFAULT_PORT constant these replace, | ||
| 16 | // which was htons(8433). Nothing is lost: net.cpp's GetDefaultPort() applies | ||
| 17 | // htons() itself and is what every caller now uses, so the value that reaches | ||
| 18 | // a sockaddr is byte-for-byte what it was. Mainnet stays on 8433. | ||
| 16 | static const unsigned short MAINNET_PORT = 8433; | 19 | static const unsigned short MAINNET_PORT = 8433; |
| 17 | static const unsigned short TESTNET_PORT = 18433; | 20 | static const unsigned short TESTNET_PORT = 18433; |
| 18 | static const unsigned int PUBLISH_HOPS = 5; | 21 | static const unsigned int PUBLISH_HOPS = 5; |
| @@ -26,6 +29,12 @@ enum | |||
| 26 | 29 | ||
| 27 | 30 | ||
| 28 | 31 | ||
| 32 | // Defined in main.cpp. True while this node is tracking the tip rather than | ||
| 33 | // still catching up, measured off the age of our own best block -- see there | ||
| 34 | // for why that and not a peer-reported height. Declared here because net.h is | ||
| 35 | // parsed before main.h and CNode::AskFor needs it. | ||
| 36 | bool IsBlockFetchNearTip(); | ||
| 37 | |||
| 29 | bool GetMyExternalIP(unsigned int& ipRet); | 38 | bool GetMyExternalIP(unsigned int& ipRet); |
| 30 | bool IsTestNet(); | 39 | bool IsTestNet(); |
| 31 | unsigned short GetDefaultPort(); | 40 | unsigned short GetDefaultPort(); |
| @@ -95,12 +104,20 @@ static const int BTF_RECV_TIMEOUT_SECS = 30 * 60; | |||
| 95 | static const int BTF_SEND_STALL_SECS = 10 * 60; | 104 | static const int BTF_SEND_STALL_SECS = 10 * 60; |
| 96 | static const int BTF_HANDSHAKE_GRACE_SECS = 60; | 105 | static const int BTF_HANDSHAKE_GRACE_SECS = 60; |
| 97 | 106 | ||
| 107 | // How long a requested block gets before it is asked for again, in seconds. | ||
| 108 | // Used twice: CNode::AskFor spaces the queued per-announcer block retries this | ||
| 109 | // far apart instead of the two minutes transactions get, and the failover | ||
| 110 | // sweep in main.cpp re-issues a getdata to a different peer once a sent | ||
| 111 | // request has gone unanswered this long. A block body is at most 1 MB and | ||
| 112 | // somebody demonstrably has it -- they announced it -- so on this timescale | ||
| 113 | // "not here yet" means "not coming from that peer", not "still in transit". | ||
| 114 | static const int BLOCK_FETCH_RETRY_SECS = 15; | ||
| 115 | |||
| 98 | // A serialized object cannot be larger than MAX_SIZE, so no valid network | 116 | // A serialized object cannot be larger than MAX_SIZE, so no valid network |
| 99 | // message needs the old 256 MB header allowance. Keep the wire cap aligned with | 117 | // message needs the old 256 MB header allowance. Keep the wire cap aligned with |
| 100 | // what the deserializer can accept and bound how long one peer may sit on a | 118 | // what the deserializer can accept. How long a peer may sit on a partial |
| 101 | // partial payload after a complete header has arrived. | 119 | // payload after a complete header has arrived is BTF_MSG_WAIT_MAX_SECS, below. |
| 102 | static const unsigned int MAX_PROTOCOL_MESSAGE_SIZE = MAX_SIZE; | 120 | static const unsigned int MAX_PROTOCOL_MESSAGE_SIZE = MAX_SIZE; |
| 103 | static const int BTF_INCOMPLETE_MESSAGE_TIMEOUT_SECS = 2 * 60; | ||
| 104 | 121 | ||
| 105 | // Ceiling on simultaneous connections. | 122 | // Ceiling on simultaneous connections. |
| 106 | // | 123 | // |
| @@ -140,6 +157,42 @@ static const unsigned int MAX_CONNECTIONS_PER_IP = 32; | |||
| 140 | // this long regardless of what is still buffered. | 157 | // this long regardless of what is still buffered. |
| 141 | static const int64 DISCONNECT_DRAIN_SECS = 60; | 158 | static const int64 DISCONNECT_DRAIN_SECS = 60; |
| 142 | 159 | ||
| 160 | // How long a pool may go without hearing a height from ANY peer before it | ||
| 161 | // stops issuing jobs. Long enough that ordinary peer churn and a quiet | ||
| 162 | // handshake window do not trip it; short enough that a deaf pool cannot build | ||
| 163 | // more than a few blocks of private fork before it is stopped. Heights | ||
| 164 | // 3966-4085 -- 120 blocks, 5890.50 BTF -- were mined in exactly this state. | ||
| 165 | static const int64 BTF_NO_PEER_HEIGHT_MAX_SECS = 20 * 60; | ||
| 166 | |||
| 167 | // How long a peer may leave this node waiting for the rest of a message it | ||
| 168 | // announced in a header. | ||
| 169 | // | ||
| 170 | // ProcessMessages rewinds the header and breaks when the body has not all | ||
| 171 | // arrived, which is correct for a message still in flight and unbounded for | ||
| 172 | // one that never completes: the same header is re-read, the loop breaks | ||
| 173 | // again, and that peer is never parsed again for the life of the process. | ||
| 174 | // Satoshi left the note -- "need a mechanism to give up waiting for overlong | ||
| 175 | // message size error" -- and this is it. | ||
| 176 | // | ||
| 177 | // It never self-corrects, because any byte that arrives refreshes nLastRecv, | ||
| 178 | // so the inactivity timeout that would otherwise drop the peer never fires. | ||
| 179 | // The socket stays healthy, select() keeps reporting it, and the node goes | ||
| 180 | // silently deaf one peer at a time. | ||
| 181 | // | ||
| 182 | // A header may legitimately outrun its body by a round trip or two; two | ||
| 183 | // minutes is far beyond that, and well inside BTF_RECV_TIMEOUT_SECS. | ||
| 184 | static const int64 BTF_MSG_WAIT_MAX_SECS = 2 * 60; | ||
| 185 | |||
| 186 | // Largest payload a peer may announce in a message header: MAX_PROTOCOL_MESSAGE_SIZE | ||
| 187 | // is defined further up this file. This tree reached the same cap by the same | ||
| 188 | // reasoning as upstream #109 -- a 256 MB allowance is eight times what the | ||
| 189 | // deserializer will accept (serialize.h throws above MAX_SIZE), so the header | ||
| 190 | // check admitted an allocation the parser would only reject later, after | ||
| 191 | // BTF_MSG_WAIT_MAX_SECS of a stalled peer slot -- so only the note survives. | ||
| 192 | // | ||
| 193 | // Both definitions were textually clean and 59 lines apart, so the replay onto | ||
| 194 | // upstream merged them without a conflict and only the compiler objected. | ||
| 195 | |||
| 143 | // Send a ping after this long with nothing to say, so a peer running the | 196 | // Send a ping after this long with nothing to say, so a peer running the |
| 144 | // inactivity check above does not mistake a quiet node for a dead one. Must | 197 | // inactivity check above does not mistake a quiet node for a dead one. Must |
| 145 | // stay well under BTF_RECV_TIMEOUT_SECS. | 198 | // stay well under BTF_RECV_TIMEOUT_SECS. |
| @@ -180,6 +233,7 @@ extern int nBestHeight; | |||
| 180 | // has told us anything. Median rather than maximum so one peer claiming an | 233 | // has told us anything. Median rather than maximum so one peer claiming an |
| 181 | // absurd height cannot move it. | 234 | // absurd height cannot move it. |
| 182 | int GetPeerMedianHeight(); | 235 | int GetPeerMedianHeight(); |
| 236 | int64 GetPeerHeightAgeSecs(); | ||
| 183 | 237 | ||
| 184 | // Lightweight .btf churn accounting. These counters are diagnostics only: they | 238 | // Lightweight .btf churn accounting. These counters are diagnostics only: they |
| 185 | // let an operator tell stale descriptors, relay pairing failures, and paired | 239 | // let an operator tell stale descriptors, relay pairing failures, and paired |
| @@ -197,8 +251,6 @@ void BtfChurnNoteHandshakeTimeout(const string& strBtfAddr, const string& strMee | |||
| 197 | void BtfPexCollect(std::vector<std::string>& vDescOut); | 251 | void BtfPexCollect(std::vector<std::string>& vDescOut); |
| 198 | // Verify descriptors a peer sent and remember the good ones. Returns how many. | 252 | // Verify descriptors a peer sent and remember the good ones. Returns how many. |
| 199 | int BtfPexAccept(const std::vector<std::string>& vDesc); | 253 | int BtfPexAccept(const std::vector<std::string>& vDesc); |
| 200 | void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1); | ||
| 201 | bool AnySubscribed(unsigned int nChannel); | ||
| 202 | void ThreadBitcoinMiner(void* parg); | 254 | void ThreadBitcoinMiner(void* parg); |
| 203 | // Starts MinerThreadCount() miners; returns how many came up. | 255 | // Starts MinerThreadCount() miners; returns how many came up. |
| 204 | int StartMinerThreads(); | 256 | int StartMinerThreads(); |
| @@ -474,13 +526,19 @@ public: | |||
| 474 | 526 | ||
| 475 | 527 | ||
| 476 | 528 | ||
| 529 | // MSG_REVIEW, MSG_PRODUCT and MSG_TABLE no longer have a handler, a producer | ||
| 530 | // or a store -- the marketplace they belonged to is gone. They stay in the | ||
| 531 | // enum, and their names stay in ppszTypeName below, because CInv serialises | ||
| 532 | // `type` as a plain integer: dropping them would renumber nothing today but | ||
| 533 | // would make it tempting to renumber later, and MSG_TX and MSG_BLOCK are wire | ||
| 534 | // values shared with every other node. Reserved, never emitted, never served. | ||
| 477 | enum | 535 | enum |
| 478 | { | 536 | { |
| 479 | MSG_TX = 1, | 537 | MSG_TX = 1, |
| 480 | MSG_BLOCK, | 538 | MSG_BLOCK, |
| 481 | MSG_REVIEW, | 539 | MSG_REVIEW, // reserved, dead |
| 482 | MSG_PRODUCT, | 540 | MSG_PRODUCT, // reserved, dead |
| 483 | MSG_TABLE, | 541 | MSG_TABLE, // reserved, dead |
| 484 | }; | 542 | }; |
| 485 | 543 | ||
| 486 | static const char* ppszTypeName[] = | 544 | static const char* ppszTypeName[] = |
| @@ -567,23 +625,9 @@ public: | |||
| 567 | 625 | ||
| 568 | 626 | ||
| 569 | 627 | ||
| 570 | class CRequestTracker | 628 | // CRequestTracker and the PushRequest/"reply" round trip lived here. Nothing |
| 571 | { | 629 | // in this tree ever called PushRequest, so the table was always empty and the |
| 572 | public: | 630 | // "reply" handler was pure inbound parser. Both removed. |
| 573 | void (*fn)(void*, CDataStream&); | ||
| 574 | void* param1; | ||
| 575 | |||
| 576 | explicit CRequestTracker(void (*fnIn)(void*, CDataStream&)=NULL, void* param1In=NULL) | ||
| 577 | { | ||
| 578 | fn = fnIn; | ||
| 579 | param1 = param1In; | ||
| 580 | } | ||
| 581 | |||
| 582 | bool IsNull() | ||
| 583 | { | ||
| 584 | return fn == NULL; | ||
| 585 | } | ||
| 586 | }; | ||
| 587 | 631 | ||
| 588 | 632 | ||
| 589 | 633 | ||
| @@ -629,9 +673,6 @@ protected: | |||
| 629 | public: | 673 | public: |
| 630 | int64 nReleaseTime; | 674 | int64 nReleaseTime; |
| 631 | int64 nDisconnectSince; | 675 | int64 nDisconnectSince; |
| 632 | map<uint256, CRequestTracker> mapRequests; | ||
| 633 | CCriticalSection cs_mapRequests; | ||
| 634 | |||
| 635 | // inventory based relay | 676 | // inventory based relay |
| 636 | set<CInv> setInventoryKnown; | 677 | set<CInv> setInventoryKnown; |
| 637 | set<CInv> setInventoryKnown2; | 678 | set<CInv> setInventoryKnown2; |
| @@ -639,9 +680,6 @@ public: | |||
| 639 | CCriticalSection cs_inventory; | 680 | CCriticalSection cs_inventory; |
| 640 | multimap<int64, CInv> mapAskFor; | 681 | multimap<int64, CInv> mapAskFor; |
| 641 | 682 | ||
| 642 | // publish and subscription | ||
| 643 | vector<char> vfSubscribe; | ||
| 644 | |||
| 645 | // Last "btfpeers" we accepted from this node, to rate-limit the exchange. | 683 | // Last "btfpeers" we accepted from this node, to rate-limit the exchange. |
| 646 | int64 nLastPexRecv; | 684 | int64 nLastPexRecv; |
| 647 | 685 | ||
| @@ -659,12 +697,17 @@ public: | |||
| 659 | int64 nLastSend; | 697 | int64 nLastSend; |
| 660 | int64 nLastRecv; | 698 | int64 nLastRecv; |
| 661 | int64 nLastSendEmpty; | 699 | int64 nLastSendEmpty; |
| 662 | int64 nIncompleteMessageStart; | ||
| 663 | unsigned int nIncompleteMessageSize; | ||
| 664 | string strIncompleteMessageCommand; | ||
| 665 | string strBtfAddr; | 700 | string strBtfAddr; |
| 666 | string strBtfMeeting; | 701 | string strBtfMeeting; |
| 667 | 702 | ||
| 703 | // When this peer's message loop last rewound a header without having made | ||
| 704 | // progress, or 0 when it is not waiting, plus how much was buffered then. | ||
| 705 | // The pair times a STALL rather than a duration, so a large message | ||
| 706 | // arriving slowly is never mistaken for one that is never arriving. | ||
| 707 | // See ProcessMessages. | ||
| 708 | int64 nMsgWaitSince; | ||
| 709 | unsigned int nMsgWaitSize; | ||
| 710 | |||
| 668 | // Height this peer announced in its version message, or -1 if it sent a | 711 | // Height this peer announced in its version message, or -1 if it sent a |
| 669 | // version message that predates the field. Knowing how far along everyone | 712 | // version message that predates the field. Knowing how far along everyone |
| 670 | // else is, is the only way a node can tell "in sync" from "not hearing". | 713 | // else is, is the only way a node can tell "in sync" from "not hearing". |
| @@ -694,13 +737,14 @@ public: | |||
| 694 | nLastSend = 0; | 737 | nLastSend = 0; |
| 695 | nLastRecv = 0; | 738 | nLastRecv = 0; |
| 696 | nLastSendEmpty = GetTime(); | 739 | nLastSendEmpty = GetTime(); |
| 697 | nIncompleteMessageStart = 0; | ||
| 698 | nIncompleteMessageSize = 0; | ||
| 699 | strIncompleteMessageCommand.clear(); | ||
| 700 | strBtfAddr.clear(); | 740 | strBtfAddr.clear(); |
| 701 | strBtfMeeting.clear(); | 741 | strBtfMeeting.clear(); |
| 742 | // Not waiting on anything yet. Left indeterminate, the first partial | ||
| 743 | // message this peer sends is measured against whatever was in this | ||
| 744 | // memory, and ProcessMessages drops it as unreadable on first contact. | ||
| 745 | nMsgWaitSince = 0; | ||
| 746 | nMsgWaitSize = 0; | ||
| 702 | nStartingHeight = -1; | 747 | nStartingHeight = -1; |
| 703 | vfSubscribe.assign(256, false); | ||
| 704 | 748 | ||
| 705 | // Push a version message | 749 | // Push a version message |
| 706 | /// when NTP implemented, change to just nTime = GetAdjustedTime() | 750 | /// when NTP implemented, change to just nTime = GetAdjustedTime() |
| @@ -728,6 +772,11 @@ public: | |||
| 728 | { | 772 | { |
| 729 | if (hSocket != INVALID_SOCKET) | 773 | if (hSocket != INVALID_SOCKET) |
| 730 | { | 774 | { |
| 775 | // Route through BtfCloseSocket so the accounting sees this close. | ||
| 776 | // A raw closesocket() here was invisible to sockcount, which is | ||
| 777 | // why the leak numbers never added up, and leaving the descriptor | ||
| 778 | // set afterwards is how a number that now belongs to somebody else | ||
| 779 | // gets closed a second time. (#103) | ||
| 731 | BtfCloseSocket(hSocket); | 780 | BtfCloseSocket(hSocket); |
| 732 | hSocket = INVALID_SOCKET; | 781 | hSocket = INVALID_SOCKET; |
| 733 | } | 782 | } |
| @@ -789,13 +838,51 @@ public: | |||
| 789 | static int64 nLastTime; | 838 | static int64 nLastTime; |
| 790 | nLastTime = nNow = max(nNow, ++nLastTime); | 839 | nLastTime = nNow = max(nNow, ++nLastTime); |
| 791 | 840 | ||
| 792 | // Each retry is 2 minutes after the last | 841 | // Each retry is 2 minutes after the last -- and because nRequestTime |
| 793 | nRequestTime = max(nRequestTime + 2 * 60 * 1000000, nNow); | 842 | // is shared across peers, the same spacing is what strings requests |
| 843 | // for one inv across its announcers. For transactions that is pacing; | ||
| 844 | // for a new block it was the stall of issue #33: nine peers announce | ||
| 845 | // the block, the first one asked does not serve it, and the ninth | ||
| 846 | // candidate is not even asked until sixteen minutes in, with the pool | ||
| 847 | // mining on the stale tip the whole time. | ||
| 848 | // | ||
| 849 | // Blocks retry on the fetch timescale, but only while we are at the | ||
| 850 | // tip. That is where the stall costs something -- the pool keeps | ||
| 851 | // handing out jobs against a dead tip -- and it is also where the | ||
| 852 | // spacing is nearly free: one block is in flight, and its body has | ||
| 853 | // almost always arrived before the second announcer's turn comes | ||
| 854 | // round, so AlreadyHave drops the duplicate before a getdata is sent. | ||
| 855 | // | ||
| 856 | // While catching up neither half holds. A peer working through a | ||
| 857 | // 500-deep queue can be minutes away from block N without anything | ||
| 858 | // being wrong, so at 15s the second announcer is asked at +15s, the | ||
| 859 | // third at +30s, and the bulk download arrives once per announcer -- | ||
| 860 | // up to eight times over. Nothing caps that: the eight-entry cap is on | ||
| 861 | // mapBlockFetch, which is a different mechanism from this spacing. | ||
| 862 | // Two minutes is the right number there, and it is what this line did | ||
| 863 | // before. Failover at the tip does not depend on this being 15s in any | ||
| 864 | // case: mapBlockFetch re-issues a stalled getdata to the next peer on | ||
| 865 | // its own timer, which is what actually fixes #33. | ||
| 866 | bool fFast = (inv.type == MSG_BLOCK && IsBlockFetchNearTip()); | ||
| 867 | int64 nRetryUsecs = (fFast ? BLOCK_FETCH_RETRY_SECS : 2 * 60) * 1000000LL; | ||
| 868 | nRequestTime = max(nRequestTime + nRetryUsecs, nNow); | ||
| 794 | mapAskFor.insert(make_pair(nRequestTime, inv)); | 869 | mapAskFor.insert(make_pair(nRequestTime, inv)); |
| 795 | } | 870 | } |
| 796 | 871 | ||
| 797 | 872 | ||
| 798 | 873 | ||
| 874 | // "sending: tx (N bytes)" alone was ~71% of a 13-hour production | ||
| 875 | // debug.log (issue #35), so tx sends are demoted to the /debugrelay-gated | ||
| 876 | // "relay" category; every other command's send trace stays on "net". | ||
| 877 | // The line is emitted in pieces across BeginMessage/EndMessage/ | ||
| 878 | // AbortMessage, so all three must agree on the category or orphaned | ||
| 879 | // "(N bytes)" fragments would leak into a plain -debug log. | ||
| 880 | // Valid only mid-message (nPushPos != -1). | ||
| 881 | const char* SendLogCategory() const | ||
| 882 | { | ||
| 883 | return strcmp(GetMessageCommand(), "tx") == 0 ? "relay" : "net"; | ||
| 884 | } | ||
| 885 | |||
| 799 | void BeginMessage(const char* pszCommand) | 886 | void BeginMessage(const char* pszCommand) |
| 800 | { | 887 | { |
| 801 | EnterCriticalSection(&cs_vSend); | 888 | EnterCriticalSection(&cs_vSend); |
| @@ -803,17 +890,19 @@ public: | |||
| 803 | AbortMessage(); | 890 | AbortMessage(); |
| 804 | nPushPos = vSend.size(); | 891 | nPushPos = vSend.size(); |
| 805 | vSend << CMessageHeader(pszCommand, 0); | 892 | vSend << CMessageHeader(pszCommand, 0); |
| 806 | if (LogAcceptsCategory("net")) printf("sending: %-12s ", pszCommand); | 893 | if (LogAcceptsCategory(SendLogCategory())) printf("sending: %-12s ", pszCommand); |
| 807 | } | 894 | } |
| 808 | 895 | ||
| 809 | void AbortMessage() | 896 | void AbortMessage() |
| 810 | { | 897 | { |
| 811 | if (nPushPos == -1) | 898 | if (nPushPos == -1) |
| 812 | return; | 899 | return; |
| 900 | // Capture before nPushPos is cleared; GetMessageCommand() needs it. | ||
| 901 | const char* pszCategory = SendLogCategory(); | ||
| 813 | vSend.resize(nPushPos); | 902 | vSend.resize(nPushPos); |
| 814 | nPushPos = -1; | 903 | nPushPos = -1; |
| 815 | LeaveCriticalSection(&cs_vSend); | 904 | LeaveCriticalSection(&cs_vSend); |
| 816 | if (LogAcceptsCategory("net")) printf("(aborted)\n"); | 905 | if (LogAcceptsCategory(pszCategory)) printf("(aborted)\n"); |
| 817 | } | 906 | } |
| 818 | 907 | ||
| 819 | void EndMessage() | 908 | void EndMessage() |
| @@ -821,7 +910,11 @@ public: | |||
| 821 | extern int nDropMessagesTest; | 910 | extern int nDropMessagesTest; |
| 822 | if (nDropMessagesTest > 0 && GetRand(nDropMessagesTest) == 0) | 911 | if (nDropMessagesTest > 0 && GetRand(nDropMessagesTest) == 0) |
| 823 | { | 912 | { |
| 824 | if (LogAcceptsCategory("net")) printf("dropmessages DROPPING SEND MESSAGE\n"); | 913 | // Same category as the "sending: " this line interrupts, so a tx |
| 914 | // does not announce its own drop under "net" having been gated | ||
| 915 | // off under "relay". nPushPos can be -1 here, and then | ||
| 916 | // GetMessageCommand() returns "" and this is "net" as before. | ||
| 917 | if (LogAcceptsCategory(SendLogCategory())) printf("dropmessages DROPPING SEND MESSAGE\n"); | ||
| 825 | AbortMessage(); | 918 | AbortMessage(); |
| 826 | return; | 919 | return; |
| 827 | } | 920 | } |
| @@ -833,10 +926,10 @@ public: | |||
| 833 | unsigned int nSize = vSend.size() - nPushPos - sizeof(CMessageHeader); | 926 | unsigned int nSize = vSend.size() - nPushPos - sizeof(CMessageHeader); |
| 834 | memcpy((char*)&vSend[nPushPos] + offsetof(CMessageHeader, nMessageSize), &nSize, sizeof(nSize)); | 927 | memcpy((char*)&vSend[nPushPos] + offsetof(CMessageHeader, nMessageSize), &nSize, sizeof(nSize)); |
| 835 | 928 | ||
| 836 | if (LogAcceptsCategory("net")) printf("(%d bytes) ", nSize); | 929 | if (LogAcceptsCategory(SendLogCategory())) printf("(%d bytes) ", nSize); |
| 837 | //for (int i = nPushPos+sizeof(CMessageHeader); i < min(vSend.size(), nPushPos+sizeof(CMessageHeader)+20U); i++) | 930 | //for (int i = nPushPos+sizeof(CMessageHeader); i < min(vSend.size(), nPushPos+sizeof(CMessageHeader)+20U); i++) |
| 838 | // printf("%02x ", vSend[i] & 0xff); | 931 | // printf("%02x ", vSend[i] & 0xff); |
| 839 | if (LogAcceptsCategory("net")) printf("\n"); | 932 | if (LogAcceptsCategory(SendLogCategory())) printf("\n"); |
| 840 | 933 | ||
| 841 | nPushPos = -1; | 934 | nPushPos = -1; |
| 842 | LeaveCriticalSection(&cs_vSend); | 935 | LeaveCriticalSection(&cs_vSend); |
| @@ -974,49 +1067,6 @@ public: | |||
| 974 | } | 1067 | } |
| 975 | 1068 | ||
| 976 | 1069 | ||
| 977 | void PushRequest(const char* pszCommand, | ||
| 978 | void (*fn)(void*, CDataStream&), void* param1) | ||
| 979 | { | ||
| 980 | uint256 hashReply; | ||
| 981 | RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply)); | ||
| 982 | |||
| 983 | CRITICAL_BLOCK(cs_mapRequests) | ||
| 984 | mapRequests[hashReply] = CRequestTracker(fn, param1); | ||
| 985 | |||
| 986 | PushMessage(pszCommand, hashReply); | ||
| 987 | } | ||
| 988 | |||
| 989 | template<typename T1> | ||
| 990 | void PushRequest(const char* pszCommand, const T1& a1, | ||
| 991 | void (*fn)(void*, CDataStream&), void* param1) | ||
| 992 | { | ||
| 993 | uint256 hashReply; | ||
| 994 | RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply)); | ||
| 995 | |||
| 996 | CRITICAL_BLOCK(cs_mapRequests) | ||
| 997 | mapRequests[hashReply] = CRequestTracker(fn, param1); | ||
| 998 | |||
| 999 | PushMessage(pszCommand, hashReply, a1); | ||
| 1000 | } | ||
| 1001 | |||
| 1002 | template<typename T1, typename T2> | ||
| 1003 | void PushRequest(const char* pszCommand, const T1& a1, const T2& a2, | ||
| 1004 | void (*fn)(void*, CDataStream&), void* param1) | ||
| 1005 | { | ||
| 1006 | uint256 hashReply; | ||
| 1007 | RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply)); | ||
| 1008 | |||
| 1009 | CRITICAL_BLOCK(cs_mapRequests) | ||
| 1010 | mapRequests[hashReply] = CRequestTracker(fn, param1); | ||
| 1011 | |||
| 1012 | PushMessage(pszCommand, hashReply, a1, a2); | ||
| 1013 | } | ||
| 1014 | |||
| 1015 | |||
| 1016 | |||
| 1017 | bool IsSubscribed(unsigned int nChannel); | ||
| 1018 | void Subscribe(unsigned int nChannel, unsigned int nHops=0); | ||
| 1019 | void CancelSubscribe(unsigned int nChannel); | ||
| 1020 | void Disconnect(); | 1070 | void Disconnect(); |
| 1021 | }; | 1071 | }; |
| 1022 | 1072 | ||
| @@ -1073,50 +1123,8 @@ inline void RelayMessage<>(const CInv& inv, const CDataStream& ss) | |||
| 1073 | 1123 | ||
| 1074 | 1124 | ||
| 1075 | 1125 | ||
| 1076 | // | 1126 | // The publish/subscribe advert templates that used to close this file |
| 1077 | // Templates for the publish and subscription system. | 1127 | // (AdvertStartPublish / AdvertStopPublish / AdvertRemoveSource) are gone along |
| 1078 | // The object being published as T& obj needs to have: | 1128 | // with the marketplace objects they carried. Nothing ever handled an inbound |
| 1079 | // a set<unsigned int> setSources member | 1129 | // "publish" or "pub-cancel", so the machinery only ever pushed; what it pushed |
| 1080 | // specializations of AdvertInsert and AdvertErase | 1130 | // was CProduct, which no longer exists. |
| 1081 | // Currently implemented for CTable and CProduct. | ||
| 1082 | // | ||
| 1083 | |||
| 1084 | template<typename T> | ||
| 1085 | void AdvertStartPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj) | ||
| 1086 | { | ||
| 1087 | // Add to sources | ||
| 1088 | obj.setSources.insert(pfrom->addr.ip); | ||
| 1089 | |||
| 1090 | if (!AdvertInsert(obj)) | ||
| 1091 | return; | ||
| 1092 | |||
| 1093 | // Relay | ||
| 1094 | CRITICAL_BLOCK(cs_vNodes) | ||
| 1095 | foreach(CNode* pnode, vNodes) | ||
| 1096 | if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel))) | ||
| 1097 | pnode->PushMessage("publish", nChannel, nHops, obj); | ||
| 1098 | } | ||
| 1099 | |||
| 1100 | template<typename T> | ||
| 1101 | void AdvertStopPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj) | ||
| 1102 | { | ||
| 1103 | uint256 hash = obj.GetHash(); | ||
| 1104 | |||
| 1105 | CRITICAL_BLOCK(cs_vNodes) | ||
| 1106 | foreach(CNode* pnode, vNodes) | ||
| 1107 | if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel))) | ||
| 1108 | pnode->PushMessage("pub-cancel", nChannel, nHops, hash); | ||
| 1109 | |||
| 1110 | AdvertErase(obj); | ||
| 1111 | } | ||
| 1112 | |||
| 1113 | template<typename T> | ||
| 1114 | void AdvertRemoveSource(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj) | ||
| 1115 | { | ||
| 1116 | // Remove a source | ||
| 1117 | obj.setSources.erase(pfrom->addr.ip); | ||
| 1118 | |||
| 1119 | // If no longer supported by any sources, cancel it | ||
| 1120 | if (obj.setSources.empty()) | ||
| 1121 | AdvertStopPublish(pfrom, nChannel, nHops, obj); | ||
| 1122 | } | ||