|
19 | 19 | #include <net.h>
|
20 | 20 | #include <node/context.h>
|
21 | 21 | #include <rpc/blockchain.h>
|
| 22 | +#include <rpc/index_util.h> |
22 | 23 | #include <rpc/server.h>
|
23 | 24 | #include <rpc/server_util.h>
|
24 | 25 | #include <rpc/util.h>
|
@@ -700,16 +701,6 @@ static bool getAddressesFromParams(const UniValue& params, std::vector<std::pair
|
700 | 701 | return true;
|
701 | 702 | }
|
702 | 703 |
|
703 |
| -static bool heightSort(std::pair<CAddressUnspentKey, CAddressUnspentValue> a, |
704 |
| - std::pair<CAddressUnspentKey, CAddressUnspentValue> b) { |
705 |
| - return a.second.m_block_height < b.second.m_block_height; |
706 |
| -} |
707 |
| - |
708 |
| -static bool timestampSort(std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta> a, |
709 |
| - std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta> b) { |
710 |
| - return a.second.m_time < b.second.m_time; |
711 |
| -} |
712 |
| - |
713 | 704 | static RPCHelpMan getaddressmempool()
|
714 | 705 | {
|
715 | 706 | return RPCHelpMan{"getaddressmempool",
|
@@ -741,22 +732,22 @@ static RPCHelpMan getaddressmempool()
|
741 | 732 | },
|
742 | 733 | [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
743 | 734 | {
|
| 735 | + CTxMemPool& mempool = EnsureAnyMemPool(request.context); |
744 | 736 |
|
745 |
| - std::vector<std::pair<uint160, AddressType> > addresses; |
746 |
| - |
| 737 | + std::vector<std::pair<uint160, AddressType>> addresses; |
747 | 738 | if (!getAddressesFromParams(request.params, addresses)) {
|
748 | 739 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
|
749 | 740 | }
|
750 | 741 |
|
751 |
| - std::vector<std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta> > indexes; |
752 |
| - |
753 |
| - CTxMemPool& mempool = EnsureAnyMemPool(request.context); |
754 |
| - if (!mempool.getAddressIndex(addresses, indexes)) { |
| 742 | + std::vector<CMempoolAddressDeltaKey> input_addresses; |
| 743 | + std::vector<CMempoolAddressDeltaEntry> indexes; |
| 744 | + for (const auto& [hash, type] : addresses) { |
| 745 | + input_addresses.push_back({type, hash}); |
| 746 | + } |
| 747 | + if (!GetMempoolAddressDeltaIndex(mempool, input_addresses, indexes, /* timestamp_sort = */ true)) { |
755 | 748 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
|
756 | 749 | }
|
757 | 750 |
|
758 |
| - std::sort(indexes.begin(), indexes.end(), timestampSort); |
759 |
| - |
760 | 751 | UniValue result(UniValue::VARR);
|
761 | 752 |
|
762 | 753 | for (const auto& [mempoolAddressKey, mempoolAddressDelta] : indexes) {
|
@@ -820,16 +811,18 @@ static RPCHelpMan getaddressutxos()
|
820 | 811 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
|
821 | 812 | }
|
822 | 813 |
|
823 |
| - std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs; |
| 814 | + std::vector<CAddressUnspentIndexEntry> unspentOutputs; |
824 | 815 |
|
825 |
| - for (const auto& address : addresses) { |
826 |
| - if (!GetAddressUnspent(address.first, address.second, unspentOutputs)) { |
827 |
| - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); |
| 816 | + { |
| 817 | + LOCK(::cs_main); |
| 818 | + for (const auto& address : addresses) { |
| 819 | + if (!GetAddressUnspentIndex(*pblocktree, address.first, address.second, unspentOutputs, |
| 820 | + /* height_sort = */ true)) { |
| 821 | + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); |
| 822 | + } |
828 | 823 | }
|
829 | 824 | }
|
830 | 825 |
|
831 |
| - std::sort(unspentOutputs.begin(), unspentOutputs.end(), heightSort); |
832 |
| - |
833 | 826 | UniValue result(UniValue::VARR);
|
834 | 827 |
|
835 | 828 | for (const auto& [unspentKey, unspentValue] : unspentOutputs) {
|
@@ -905,16 +898,19 @@ static RPCHelpMan getaddressdeltas()
|
905 | 898 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
|
906 | 899 | }
|
907 | 900 |
|
908 |
| - std::vector<std::pair<CAddressIndexKey, CAmount> > addressIndex; |
| 901 | + std::vector<CAddressIndexEntry> addressIndex; |
909 | 902 |
|
910 |
| - for (const auto& address : addresses) { |
911 |
| - if (start > 0 && end > 0) { |
912 |
| - if (!GetAddressIndex(address.first, address.second, addressIndex, start, end)) { |
913 |
| - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); |
914 |
| - } |
915 |
| - } else { |
916 |
| - if (!GetAddressIndex(address.first, address.second, addressIndex)) { |
917 |
| - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); |
| 903 | + { |
| 904 | + LOCK(::cs_main); |
| 905 | + for (const auto& address : addresses) { |
| 906 | + if (start > 0 && end > 0) { |
| 907 | + if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex, start, end)) { |
| 908 | + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); |
| 909 | + } |
| 910 | + } else { |
| 911 | + if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex)) { |
| 912 | + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); |
| 913 | + } |
918 | 914 | }
|
919 | 915 | }
|
920 | 916 | }
|
@@ -974,16 +970,21 @@ static RPCHelpMan getaddressbalance()
|
974 | 970 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
|
975 | 971 | }
|
976 | 972 |
|
977 |
| - std::vector<std::pair<CAddressIndexKey, CAmount> > addressIndex; |
| 973 | + std::vector<CAddressIndexEntry> addressIndex; |
978 | 974 |
|
979 |
| - for (const auto& address : addresses) { |
980 |
| - if (!GetAddressIndex(address.first, address.second, addressIndex)) { |
981 |
| - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); |
| 975 | + ChainstateManager& chainman = EnsureAnyChainman(request.context); |
| 976 | + |
| 977 | + int nHeight; |
| 978 | + { |
| 979 | + LOCK(::cs_main); |
| 980 | + for (const auto& address : addresses) { |
| 981 | + if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex)) { |
| 982 | + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); |
| 983 | + } |
982 | 984 | }
|
| 985 | + nHeight = chainman.ActiveChain().Height(); |
983 | 986 | }
|
984 | 987 |
|
985 |
| - ChainstateManager& chainman = EnsureAnyChainman(request.context); |
986 |
| - int nHeight = WITH_LOCK(cs_main, return chainman.ActiveChain().Height()); |
987 | 988 |
|
988 | 989 | CAmount balance = 0;
|
989 | 990 | CAmount balance_spendable = 0;
|
@@ -1053,16 +1054,19 @@ static RPCHelpMan getaddresstxids()
|
1053 | 1054 | }
|
1054 | 1055 | }
|
1055 | 1056 |
|
1056 |
| - std::vector<std::pair<CAddressIndexKey, CAmount> > addressIndex; |
| 1057 | + std::vector<CAddressIndexEntry> addressIndex; |
1057 | 1058 |
|
1058 |
| - for (const auto& address : addresses) { |
1059 |
| - if (start > 0 && end > 0) { |
1060 |
| - if (!GetAddressIndex(address.first, address.second, addressIndex, start, end)) { |
1061 |
| - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); |
1062 |
| - } |
1063 |
| - } else { |
1064 |
| - if (!GetAddressIndex(address.first, address.second, addressIndex)) { |
1065 |
| - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); |
| 1059 | + { |
| 1060 | + LOCK(::cs_main); |
| 1061 | + for (const auto& address : addresses) { |
| 1062 | + if (start > 0 && end > 0) { |
| 1063 | + if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex, start, end)) { |
| 1064 | + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); |
| 1065 | + } |
| 1066 | + } else { |
| 1067 | + if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex)) { |
| 1068 | + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); |
| 1069 | + } |
1066 | 1070 | }
|
1067 | 1071 | }
|
1068 | 1072 | }
|
@@ -1134,7 +1138,7 @@ static RPCHelpMan getspentinfo()
|
1134 | 1138 | CSpentIndexValue value;
|
1135 | 1139 |
|
1136 | 1140 | CTxMemPool& mempool = EnsureAnyMemPool(request.context);
|
1137 |
| - if (!GetSpentIndex(mempool, key, value)) { |
| 1141 | + if (LOCK(::cs_main); !GetSpentIndex(*pblocktree, mempool, key, value)) { |
1138 | 1142 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to get spent info");
|
1139 | 1143 | }
|
1140 | 1144 |
|
|
0 commit comments