You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

580 lines
15 KiB

abci: Refactor tagging events using list of lists (#3643) ## PR This PR introduces a fundamental breaking change to the structure of ABCI response and tx tags and the way they're processed. Namely, the SDK can support more complex and aggregated events for distribution and slashing. In addition, block responses can include duplicate keys in events. Implement new Event type. An event has a type and a list of KV pairs (ie. list-of-lists). Typical events may look like: "rewards": [{"amount": "5000uatom", "validator": "...", "recipient": "..."}] "sender": [{"address": "...", "balance": "100uatom"}] The events are indexed by {even.type}.{even.attribute[i].key}/.... In this case a client would subscribe or query for rewards.recipient='...' ABCI response types and related types now include Events []Event instead of Tags []cmn.KVPair. PubSub logic now publishes/matches against map[string][]string instead of map[string]string to support duplicate keys in response events (from #1385). A match is successful if the value is found in the slice of strings. closes: #1859 closes: #2905 ## Commits: * Implement Event ABCI type and updates responses to use events * Update messages_test.go * Update kvstore.go * Update event_bus.go * Update subscription.go * Update pubsub.go * Update kvstore.go * Update query logic to handle slice of strings in events * Update Empty#Matches and unit tests * Update pubsub logic * Update EventBus#Publish * Update kv tx indexer * Update godocs * Update ResultEvent to use slice of strings; update RPC * Update more tests * Update abci.md * Check for key in validateAndStringifyEvents * Fix KV indexer to skip empty keys * Fix linting errors * Update CHANGELOG_PENDING.md * Update docs/spec/abci/abci.md Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update abci/types/types.proto Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update docs/spec/abci/abci.md Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update libs/pubsub/query/query.go Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update match function to match if ANY value matches * Implement TestSubscribeDuplicateKeys * Update TestMatches to include multi-key test cases * Update events.go * Update Query interface godoc * Update match godoc * Add godoc for matchValue * DRY-up tx indexing * Return error from PublishWithEvents in EventBus#Publish * Update PublishEventNewBlockHeader to return an error * Fix build * Update events doc in ABCI * Update ABCI events godoc * Implement TestEventBusPublishEventTxDuplicateKeys * Update TestSubscribeDuplicateKeys to be table-driven * Remove mod file * Remove markdown from events godoc * Implement TestTxSearchDeprecatedIndexing test
6 years ago
7 years ago
abci: Refactor tagging events using list of lists (#3643) ## PR This PR introduces a fundamental breaking change to the structure of ABCI response and tx tags and the way they're processed. Namely, the SDK can support more complex and aggregated events for distribution and slashing. In addition, block responses can include duplicate keys in events. Implement new Event type. An event has a type and a list of KV pairs (ie. list-of-lists). Typical events may look like: "rewards": [{"amount": "5000uatom", "validator": "...", "recipient": "..."}] "sender": [{"address": "...", "balance": "100uatom"}] The events are indexed by {even.type}.{even.attribute[i].key}/.... In this case a client would subscribe or query for rewards.recipient='...' ABCI response types and related types now include Events []Event instead of Tags []cmn.KVPair. PubSub logic now publishes/matches against map[string][]string instead of map[string]string to support duplicate keys in response events (from #1385). A match is successful if the value is found in the slice of strings. closes: #1859 closes: #2905 ## Commits: * Implement Event ABCI type and updates responses to use events * Update messages_test.go * Update kvstore.go * Update event_bus.go * Update subscription.go * Update pubsub.go * Update kvstore.go * Update query logic to handle slice of strings in events * Update Empty#Matches and unit tests * Update pubsub logic * Update EventBus#Publish * Update kv tx indexer * Update godocs * Update ResultEvent to use slice of strings; update RPC * Update more tests * Update abci.md * Check for key in validateAndStringifyEvents * Fix KV indexer to skip empty keys * Fix linting errors * Update CHANGELOG_PENDING.md * Update docs/spec/abci/abci.md Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update abci/types/types.proto Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update docs/spec/abci/abci.md Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update libs/pubsub/query/query.go Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update match function to match if ANY value matches * Implement TestSubscribeDuplicateKeys * Update TestMatches to include multi-key test cases * Update events.go * Update Query interface godoc * Update match godoc * Add godoc for matchValue * DRY-up tx indexing * Return error from PublishWithEvents in EventBus#Publish * Update PublishEventNewBlockHeader to return an error * Fix build * Update events doc in ABCI * Update ABCI events godoc * Implement TestEventBusPublishEventTxDuplicateKeys * Update TestSubscribeDuplicateKeys to be table-driven * Remove mod file * Remove markdown from events godoc * Implement TestTxSearchDeprecatedIndexing test
6 years ago
abci: Refactor tagging events using list of lists (#3643) ## PR This PR introduces a fundamental breaking change to the structure of ABCI response and tx tags and the way they're processed. Namely, the SDK can support more complex and aggregated events for distribution and slashing. In addition, block responses can include duplicate keys in events. Implement new Event type. An event has a type and a list of KV pairs (ie. list-of-lists). Typical events may look like: "rewards": [{"amount": "5000uatom", "validator": "...", "recipient": "..."}] "sender": [{"address": "...", "balance": "100uatom"}] The events are indexed by {even.type}.{even.attribute[i].key}/.... In this case a client would subscribe or query for rewards.recipient='...' ABCI response types and related types now include Events []Event instead of Tags []cmn.KVPair. PubSub logic now publishes/matches against map[string][]string instead of map[string]string to support duplicate keys in response events (from #1385). A match is successful if the value is found in the slice of strings. closes: #1859 closes: #2905 ## Commits: * Implement Event ABCI type and updates responses to use events * Update messages_test.go * Update kvstore.go * Update event_bus.go * Update subscription.go * Update pubsub.go * Update kvstore.go * Update query logic to handle slice of strings in events * Update Empty#Matches and unit tests * Update pubsub logic * Update EventBus#Publish * Update kv tx indexer * Update godocs * Update ResultEvent to use slice of strings; update RPC * Update more tests * Update abci.md * Check for key in validateAndStringifyEvents * Fix KV indexer to skip empty keys * Fix linting errors * Update CHANGELOG_PENDING.md * Update docs/spec/abci/abci.md Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update abci/types/types.proto Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update docs/spec/abci/abci.md Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update libs/pubsub/query/query.go Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update match function to match if ANY value matches * Implement TestSubscribeDuplicateKeys * Update TestMatches to include multi-key test cases * Update events.go * Update Query interface godoc * Update match godoc * Add godoc for matchValue * DRY-up tx indexing * Return error from PublishWithEvents in EventBus#Publish * Update PublishEventNewBlockHeader to return an error * Fix build * Update events doc in ABCI * Update ABCI events godoc * Implement TestEventBusPublishEventTxDuplicateKeys * Update TestSubscribeDuplicateKeys to be table-driven * Remove mod file * Remove markdown from events godoc * Implement TestTxSearchDeprecatedIndexing test
6 years ago
7 years ago
7 years ago
abci: Refactor tagging events using list of lists (#3643) ## PR This PR introduces a fundamental breaking change to the structure of ABCI response and tx tags and the way they're processed. Namely, the SDK can support more complex and aggregated events for distribution and slashing. In addition, block responses can include duplicate keys in events. Implement new Event type. An event has a type and a list of KV pairs (ie. list-of-lists). Typical events may look like: "rewards": [{"amount": "5000uatom", "validator": "...", "recipient": "..."}] "sender": [{"address": "...", "balance": "100uatom"}] The events are indexed by {even.type}.{even.attribute[i].key}/.... In this case a client would subscribe or query for rewards.recipient='...' ABCI response types and related types now include Events []Event instead of Tags []cmn.KVPair. PubSub logic now publishes/matches against map[string][]string instead of map[string]string to support duplicate keys in response events (from #1385). A match is successful if the value is found in the slice of strings. closes: #1859 closes: #2905 ## Commits: * Implement Event ABCI type and updates responses to use events * Update messages_test.go * Update kvstore.go * Update event_bus.go * Update subscription.go * Update pubsub.go * Update kvstore.go * Update query logic to handle slice of strings in events * Update Empty#Matches and unit tests * Update pubsub logic * Update EventBus#Publish * Update kv tx indexer * Update godocs * Update ResultEvent to use slice of strings; update RPC * Update more tests * Update abci.md * Check for key in validateAndStringifyEvents * Fix KV indexer to skip empty keys * Fix linting errors * Update CHANGELOG_PENDING.md * Update docs/spec/abci/abci.md Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update abci/types/types.proto Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update docs/spec/abci/abci.md Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update libs/pubsub/query/query.go Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update match function to match if ANY value matches * Implement TestSubscribeDuplicateKeys * Update TestMatches to include multi-key test cases * Update events.go * Update Query interface godoc * Update match godoc * Add godoc for matchValue * DRY-up tx indexing * Return error from PublishWithEvents in EventBus#Publish * Update PublishEventNewBlockHeader to return an error * Fix build * Update events doc in ABCI * Update ABCI events godoc * Implement TestEventBusPublishEventTxDuplicateKeys * Update TestSubscribeDuplicateKeys to be table-driven * Remove mod file * Remove markdown from events godoc * Implement TestTxSearchDeprecatedIndexing test
6 years ago
abci: Refactor tagging events using list of lists (#3643) ## PR This PR introduces a fundamental breaking change to the structure of ABCI response and tx tags and the way they're processed. Namely, the SDK can support more complex and aggregated events for distribution and slashing. In addition, block responses can include duplicate keys in events. Implement new Event type. An event has a type and a list of KV pairs (ie. list-of-lists). Typical events may look like: "rewards": [{"amount": "5000uatom", "validator": "...", "recipient": "..."}] "sender": [{"address": "...", "balance": "100uatom"}] The events are indexed by {even.type}.{even.attribute[i].key}/.... In this case a client would subscribe or query for rewards.recipient='...' ABCI response types and related types now include Events []Event instead of Tags []cmn.KVPair. PubSub logic now publishes/matches against map[string][]string instead of map[string]string to support duplicate keys in response events (from #1385). A match is successful if the value is found in the slice of strings. closes: #1859 closes: #2905 ## Commits: * Implement Event ABCI type and updates responses to use events * Update messages_test.go * Update kvstore.go * Update event_bus.go * Update subscription.go * Update pubsub.go * Update kvstore.go * Update query logic to handle slice of strings in events * Update Empty#Matches and unit tests * Update pubsub logic * Update EventBus#Publish * Update kv tx indexer * Update godocs * Update ResultEvent to use slice of strings; update RPC * Update more tests * Update abci.md * Check for key in validateAndStringifyEvents * Fix KV indexer to skip empty keys * Fix linting errors * Update CHANGELOG_PENDING.md * Update docs/spec/abci/abci.md Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update abci/types/types.proto Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update docs/spec/abci/abci.md Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update libs/pubsub/query/query.go Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update match function to match if ANY value matches * Implement TestSubscribeDuplicateKeys * Update TestMatches to include multi-key test cases * Update events.go * Update Query interface godoc * Update match godoc * Add godoc for matchValue * DRY-up tx indexing * Return error from PublishWithEvents in EventBus#Publish * Update PublishEventNewBlockHeader to return an error * Fix build * Update events doc in ABCI * Update ABCI events godoc * Implement TestEventBusPublishEventTxDuplicateKeys * Update TestSubscribeDuplicateKeys to be table-driven * Remove mod file * Remove markdown from events godoc * Implement TestTxSearchDeprecatedIndexing test
6 years ago
7 years ago
7 years ago
abci: Refactor tagging events using list of lists (#3643) ## PR This PR introduces a fundamental breaking change to the structure of ABCI response and tx tags and the way they're processed. Namely, the SDK can support more complex and aggregated events for distribution and slashing. In addition, block responses can include duplicate keys in events. Implement new Event type. An event has a type and a list of KV pairs (ie. list-of-lists). Typical events may look like: "rewards": [{"amount": "5000uatom", "validator": "...", "recipient": "..."}] "sender": [{"address": "...", "balance": "100uatom"}] The events are indexed by {even.type}.{even.attribute[i].key}/.... In this case a client would subscribe or query for rewards.recipient='...' ABCI response types and related types now include Events []Event instead of Tags []cmn.KVPair. PubSub logic now publishes/matches against map[string][]string instead of map[string]string to support duplicate keys in response events (from #1385). A match is successful if the value is found in the slice of strings. closes: #1859 closes: #2905 ## Commits: * Implement Event ABCI type and updates responses to use events * Update messages_test.go * Update kvstore.go * Update event_bus.go * Update subscription.go * Update pubsub.go * Update kvstore.go * Update query logic to handle slice of strings in events * Update Empty#Matches and unit tests * Update pubsub logic * Update EventBus#Publish * Update kv tx indexer * Update godocs * Update ResultEvent to use slice of strings; update RPC * Update more tests * Update abci.md * Check for key in validateAndStringifyEvents * Fix KV indexer to skip empty keys * Fix linting errors * Update CHANGELOG_PENDING.md * Update docs/spec/abci/abci.md Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update abci/types/types.proto Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update docs/spec/abci/abci.md Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update libs/pubsub/query/query.go Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update match function to match if ANY value matches * Implement TestSubscribeDuplicateKeys * Update TestMatches to include multi-key test cases * Update events.go * Update Query interface godoc * Update match godoc * Add godoc for matchValue * DRY-up tx indexing * Return error from PublishWithEvents in EventBus#Publish * Update PublishEventNewBlockHeader to return an error * Fix build * Update events doc in ABCI * Update ABCI events godoc * Implement TestEventBusPublishEventTxDuplicateKeys * Update TestSubscribeDuplicateKeys to be table-driven * Remove mod file * Remove markdown from events godoc * Implement TestTxSearchDeprecatedIndexing test
6 years ago
abci: Refactor tagging events using list of lists (#3643) ## PR This PR introduces a fundamental breaking change to the structure of ABCI response and tx tags and the way they're processed. Namely, the SDK can support more complex and aggregated events for distribution and slashing. In addition, block responses can include duplicate keys in events. Implement new Event type. An event has a type and a list of KV pairs (ie. list-of-lists). Typical events may look like: "rewards": [{"amount": "5000uatom", "validator": "...", "recipient": "..."}] "sender": [{"address": "...", "balance": "100uatom"}] The events are indexed by {even.type}.{even.attribute[i].key}/.... In this case a client would subscribe or query for rewards.recipient='...' ABCI response types and related types now include Events []Event instead of Tags []cmn.KVPair. PubSub logic now publishes/matches against map[string][]string instead of map[string]string to support duplicate keys in response events (from #1385). A match is successful if the value is found in the slice of strings. closes: #1859 closes: #2905 ## Commits: * Implement Event ABCI type and updates responses to use events * Update messages_test.go * Update kvstore.go * Update event_bus.go * Update subscription.go * Update pubsub.go * Update kvstore.go * Update query logic to handle slice of strings in events * Update Empty#Matches and unit tests * Update pubsub logic * Update EventBus#Publish * Update kv tx indexer * Update godocs * Update ResultEvent to use slice of strings; update RPC * Update more tests * Update abci.md * Check for key in validateAndStringifyEvents * Fix KV indexer to skip empty keys * Fix linting errors * Update CHANGELOG_PENDING.md * Update docs/spec/abci/abci.md Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update abci/types/types.proto Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update docs/spec/abci/abci.md Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update libs/pubsub/query/query.go Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update match function to match if ANY value matches * Implement TestSubscribeDuplicateKeys * Update TestMatches to include multi-key test cases * Update events.go * Update Query interface godoc * Update match godoc * Add godoc for matchValue * DRY-up tx indexing * Return error from PublishWithEvents in EventBus#Publish * Update PublishEventNewBlockHeader to return an error * Fix build * Update events doc in ABCI * Update ABCI events godoc * Implement TestEventBusPublishEventTxDuplicateKeys * Update TestSubscribeDuplicateKeys to be table-driven * Remove mod file * Remove markdown from events godoc * Implement TestTxSearchDeprecatedIndexing test
6 years ago
abci: Refactor tagging events using list of lists (#3643) ## PR This PR introduces a fundamental breaking change to the structure of ABCI response and tx tags and the way they're processed. Namely, the SDK can support more complex and aggregated events for distribution and slashing. In addition, block responses can include duplicate keys in events. Implement new Event type. An event has a type and a list of KV pairs (ie. list-of-lists). Typical events may look like: "rewards": [{"amount": "5000uatom", "validator": "...", "recipient": "..."}] "sender": [{"address": "...", "balance": "100uatom"}] The events are indexed by {even.type}.{even.attribute[i].key}/.... In this case a client would subscribe or query for rewards.recipient='...' ABCI response types and related types now include Events []Event instead of Tags []cmn.KVPair. PubSub logic now publishes/matches against map[string][]string instead of map[string]string to support duplicate keys in response events (from #1385). A match is successful if the value is found in the slice of strings. closes: #1859 closes: #2905 ## Commits: * Implement Event ABCI type and updates responses to use events * Update messages_test.go * Update kvstore.go * Update event_bus.go * Update subscription.go * Update pubsub.go * Update kvstore.go * Update query logic to handle slice of strings in events * Update Empty#Matches and unit tests * Update pubsub logic * Update EventBus#Publish * Update kv tx indexer * Update godocs * Update ResultEvent to use slice of strings; update RPC * Update more tests * Update abci.md * Check for key in validateAndStringifyEvents * Fix KV indexer to skip empty keys * Fix linting errors * Update CHANGELOG_PENDING.md * Update docs/spec/abci/abci.md Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update abci/types/types.proto Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update docs/spec/abci/abci.md Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update libs/pubsub/query/query.go Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update match function to match if ANY value matches * Implement TestSubscribeDuplicateKeys * Update TestMatches to include multi-key test cases * Update events.go * Update Query interface godoc * Update match godoc * Add godoc for matchValue * DRY-up tx indexing * Return error from PublishWithEvents in EventBus#Publish * Update PublishEventNewBlockHeader to return an error * Fix build * Update events doc in ABCI * Update ABCI events godoc * Implement TestEventBusPublishEventTxDuplicateKeys * Update TestSubscribeDuplicateKeys to be table-driven * Remove mod file * Remove markdown from events godoc * Implement TestTxSearchDeprecatedIndexing test
6 years ago
abci: Refactor tagging events using list of lists (#3643) ## PR This PR introduces a fundamental breaking change to the structure of ABCI response and tx tags and the way they're processed. Namely, the SDK can support more complex and aggregated events for distribution and slashing. In addition, block responses can include duplicate keys in events. Implement new Event type. An event has a type and a list of KV pairs (ie. list-of-lists). Typical events may look like: "rewards": [{"amount": "5000uatom", "validator": "...", "recipient": "..."}] "sender": [{"address": "...", "balance": "100uatom"}] The events are indexed by {even.type}.{even.attribute[i].key}/.... In this case a client would subscribe or query for rewards.recipient='...' ABCI response types and related types now include Events []Event instead of Tags []cmn.KVPair. PubSub logic now publishes/matches against map[string][]string instead of map[string]string to support duplicate keys in response events (from #1385). A match is successful if the value is found in the slice of strings. closes: #1859 closes: #2905 ## Commits: * Implement Event ABCI type and updates responses to use events * Update messages_test.go * Update kvstore.go * Update event_bus.go * Update subscription.go * Update pubsub.go * Update kvstore.go * Update query logic to handle slice of strings in events * Update Empty#Matches and unit tests * Update pubsub logic * Update EventBus#Publish * Update kv tx indexer * Update godocs * Update ResultEvent to use slice of strings; update RPC * Update more tests * Update abci.md * Check for key in validateAndStringifyEvents * Fix KV indexer to skip empty keys * Fix linting errors * Update CHANGELOG_PENDING.md * Update docs/spec/abci/abci.md Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update abci/types/types.proto Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update docs/spec/abci/abci.md Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update libs/pubsub/query/query.go Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update match function to match if ANY value matches * Implement TestSubscribeDuplicateKeys * Update TestMatches to include multi-key test cases * Update events.go * Update Query interface godoc * Update match godoc * Add godoc for matchValue * DRY-up tx indexing * Return error from PublishWithEvents in EventBus#Publish * Update PublishEventNewBlockHeader to return an error * Fix build * Update events doc in ABCI * Update ABCI events godoc * Implement TestEventBusPublishEventTxDuplicateKeys * Update TestSubscribeDuplicateKeys to be table-driven * Remove mod file * Remove markdown from events godoc * Implement TestTxSearchDeprecatedIndexing test
6 years ago
libs/pubsub: relax tx querying (#4070) Some linting/cleanup missed from the initial events refactor Don't panic; instead, return false, error when matching breaks unexpectedly Strip non-numeric chars from values when attempting to match against query values Have the server log during send upon error * cleanup/lint Query#Conditions and do not panic * cleanup/lint Query#Matches and do not panic * cleanup/lint matchValue and do not panic * rever to panic in Query#Conditions * linting * strip alpha chars when attempting to match * add pending log entries * Update libs/pubsub/query/query.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * build: update variable names * update matchValue to return an error * update Query#Matches to return an error * update TestMatches * log error in send * Fix tests * Fix TestEmptyQueryMatchesAnything * fix linting * Update libs/pubsub/query/query.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * Update libs/pubsub/query/query.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * Update libs/pubsub/query/query.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * Update libs/pubsub/query/query.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * Update libs/pubsub/query/query.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * Update libs/pubsub/pubsub.go Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * add missing errors pkg import * update Query#Conditions to return an error * update query pkg unit tests * update TxIndex#Search * update pending changelog
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
abci: Refactor tagging events using list of lists (#3643) ## PR This PR introduces a fundamental breaking change to the structure of ABCI response and tx tags and the way they're processed. Namely, the SDK can support more complex and aggregated events for distribution and slashing. In addition, block responses can include duplicate keys in events. Implement new Event type. An event has a type and a list of KV pairs (ie. list-of-lists). Typical events may look like: "rewards": [{"amount": "5000uatom", "validator": "...", "recipient": "..."}] "sender": [{"address": "...", "balance": "100uatom"}] The events are indexed by {even.type}.{even.attribute[i].key}/.... In this case a client would subscribe or query for rewards.recipient='...' ABCI response types and related types now include Events []Event instead of Tags []cmn.KVPair. PubSub logic now publishes/matches against map[string][]string instead of map[string]string to support duplicate keys in response events (from #1385). A match is successful if the value is found in the slice of strings. closes: #1859 closes: #2905 ## Commits: * Implement Event ABCI type and updates responses to use events * Update messages_test.go * Update kvstore.go * Update event_bus.go * Update subscription.go * Update pubsub.go * Update kvstore.go * Update query logic to handle slice of strings in events * Update Empty#Matches and unit tests * Update pubsub logic * Update EventBus#Publish * Update kv tx indexer * Update godocs * Update ResultEvent to use slice of strings; update RPC * Update more tests * Update abci.md * Check for key in validateAndStringifyEvents * Fix KV indexer to skip empty keys * Fix linting errors * Update CHANGELOG_PENDING.md * Update docs/spec/abci/abci.md Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update abci/types/types.proto Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update docs/spec/abci/abci.md Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update libs/pubsub/query/query.go Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update match function to match if ANY value matches * Implement TestSubscribeDuplicateKeys * Update TestMatches to include multi-key test cases * Update events.go * Update Query interface godoc * Update match godoc * Add godoc for matchValue * DRY-up tx indexing * Return error from PublishWithEvents in EventBus#Publish * Update PublishEventNewBlockHeader to return an error * Fix build * Update events doc in ABCI * Update ABCI events godoc * Implement TestEventBusPublishEventTxDuplicateKeys * Update TestSubscribeDuplicateKeys to be table-driven * Remove mod file * Remove markdown from events godoc * Implement TestTxSearchDeprecatedIndexing test
6 years ago
abci: Refactor tagging events using list of lists (#3643) ## PR This PR introduces a fundamental breaking change to the structure of ABCI response and tx tags and the way they're processed. Namely, the SDK can support more complex and aggregated events for distribution and slashing. In addition, block responses can include duplicate keys in events. Implement new Event type. An event has a type and a list of KV pairs (ie. list-of-lists). Typical events may look like: "rewards": [{"amount": "5000uatom", "validator": "...", "recipient": "..."}] "sender": [{"address": "...", "balance": "100uatom"}] The events are indexed by {even.type}.{even.attribute[i].key}/.... In this case a client would subscribe or query for rewards.recipient='...' ABCI response types and related types now include Events []Event instead of Tags []cmn.KVPair. PubSub logic now publishes/matches against map[string][]string instead of map[string]string to support duplicate keys in response events (from #1385). A match is successful if the value is found in the slice of strings. closes: #1859 closes: #2905 ## Commits: * Implement Event ABCI type and updates responses to use events * Update messages_test.go * Update kvstore.go * Update event_bus.go * Update subscription.go * Update pubsub.go * Update kvstore.go * Update query logic to handle slice of strings in events * Update Empty#Matches and unit tests * Update pubsub logic * Update EventBus#Publish * Update kv tx indexer * Update godocs * Update ResultEvent to use slice of strings; update RPC * Update more tests * Update abci.md * Check for key in validateAndStringifyEvents * Fix KV indexer to skip empty keys * Fix linting errors * Update CHANGELOG_PENDING.md * Update docs/spec/abci/abci.md Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update abci/types/types.proto Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update docs/spec/abci/abci.md Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update libs/pubsub/query/query.go Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update match function to match if ANY value matches * Implement TestSubscribeDuplicateKeys * Update TestMatches to include multi-key test cases * Update events.go * Update Query interface godoc * Update match godoc * Add godoc for matchValue * DRY-up tx indexing * Return error from PublishWithEvents in EventBus#Publish * Update PublishEventNewBlockHeader to return an error * Fix build * Update events doc in ABCI * Update ABCI events godoc * Implement TestEventBusPublishEventTxDuplicateKeys * Update TestSubscribeDuplicateKeys to be table-driven * Remove mod file * Remove markdown from events godoc * Implement TestTxSearchDeprecatedIndexing test
6 years ago
abci: Refactor tagging events using list of lists (#3643) ## PR This PR introduces a fundamental breaking change to the structure of ABCI response and tx tags and the way they're processed. Namely, the SDK can support more complex and aggregated events for distribution and slashing. In addition, block responses can include duplicate keys in events. Implement new Event type. An event has a type and a list of KV pairs (ie. list-of-lists). Typical events may look like: "rewards": [{"amount": "5000uatom", "validator": "...", "recipient": "..."}] "sender": [{"address": "...", "balance": "100uatom"}] The events are indexed by {even.type}.{even.attribute[i].key}/.... In this case a client would subscribe or query for rewards.recipient='...' ABCI response types and related types now include Events []Event instead of Tags []cmn.KVPair. PubSub logic now publishes/matches against map[string][]string instead of map[string]string to support duplicate keys in response events (from #1385). A match is successful if the value is found in the slice of strings. closes: #1859 closes: #2905 ## Commits: * Implement Event ABCI type and updates responses to use events * Update messages_test.go * Update kvstore.go * Update event_bus.go * Update subscription.go * Update pubsub.go * Update kvstore.go * Update query logic to handle slice of strings in events * Update Empty#Matches and unit tests * Update pubsub logic * Update EventBus#Publish * Update kv tx indexer * Update godocs * Update ResultEvent to use slice of strings; update RPC * Update more tests * Update abci.md * Check for key in validateAndStringifyEvents * Fix KV indexer to skip empty keys * Fix linting errors * Update CHANGELOG_PENDING.md * Update docs/spec/abci/abci.md Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update abci/types/types.proto Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update docs/spec/abci/abci.md Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update libs/pubsub/query/query.go Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update match function to match if ANY value matches * Implement TestSubscribeDuplicateKeys * Update TestMatches to include multi-key test cases * Update events.go * Update Query interface godoc * Update match godoc * Add godoc for matchValue * DRY-up tx indexing * Return error from PublishWithEvents in EventBus#Publish * Update PublishEventNewBlockHeader to return an error * Fix build * Update events doc in ABCI * Update ABCI events godoc * Implement TestEventBusPublishEventTxDuplicateKeys * Update TestSubscribeDuplicateKeys to be table-driven * Remove mod file * Remove markdown from events godoc * Implement TestTxSearchDeprecatedIndexing test
6 years ago
  1. package kv
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "fmt"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/pkg/errors"
  11. dbm "github.com/tendermint/tm-db"
  12. "github.com/tendermint/tendermint/libs/pubsub/query"
  13. tmstring "github.com/tendermint/tendermint/libs/strings"
  14. "github.com/tendermint/tendermint/state/txindex"
  15. "github.com/tendermint/tendermint/types"
  16. )
  17. const (
  18. tagKeySeparator = "/"
  19. )
  20. var _ txindex.TxIndexer = (*TxIndex)(nil)
  21. // TxIndex is the simplest possible indexer, backed by key-value storage (levelDB).
  22. type TxIndex struct {
  23. store dbm.DB
  24. compositeKeysToIndex []string
  25. indexAllEvents bool
  26. }
  27. // NewTxIndex creates new KV indexer.
  28. func NewTxIndex(store dbm.DB, options ...func(*TxIndex)) *TxIndex {
  29. txi := &TxIndex{store: store, compositeKeysToIndex: make([]string, 0), indexAllEvents: false}
  30. for _, o := range options {
  31. o(txi)
  32. }
  33. return txi
  34. }
  35. // IndexEvents is an option for setting which composite keys to index.
  36. func IndexEvents(compositeKeys []string) func(*TxIndex) {
  37. return func(txi *TxIndex) {
  38. txi.compositeKeysToIndex = compositeKeys
  39. }
  40. }
  41. // IndexAllEvents is an option for indexing all events.
  42. func IndexAllEvents() func(*TxIndex) {
  43. return func(txi *TxIndex) {
  44. txi.indexAllEvents = true
  45. }
  46. }
  47. // Get gets transaction from the TxIndex storage and returns it or nil if the
  48. // transaction is not found.
  49. func (txi *TxIndex) Get(hash []byte) (*types.TxResult, error) {
  50. if len(hash) == 0 {
  51. return nil, txindex.ErrorEmptyHash
  52. }
  53. rawBytes, err := txi.store.Get(hash)
  54. if err != nil {
  55. panic(err)
  56. }
  57. if rawBytes == nil {
  58. return nil, nil
  59. }
  60. txResult := new(types.TxResult)
  61. err = cdc.UnmarshalBinaryBare(rawBytes, &txResult)
  62. if err != nil {
  63. return nil, fmt.Errorf("error reading TxResult: %v", err)
  64. }
  65. return txResult, nil
  66. }
  67. // AddBatch indexes a batch of transactions using the given list of events. Each
  68. // key that indexed from the tx's events is a composite of the event type and
  69. // the respective attribute's key delimited by a "." (eg. "account.number").
  70. // Any event with an empty type is not indexed.
  71. func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
  72. storeBatch := txi.store.NewBatch()
  73. defer storeBatch.Close()
  74. for _, result := range b.Ops {
  75. hash := result.Tx.Hash()
  76. // index tx by events
  77. txi.indexEvents(result, hash, storeBatch)
  78. // index tx by height
  79. if txi.indexAllEvents || tmstring.StringInSlice(types.TxHeightKey, txi.compositeKeysToIndex) {
  80. storeBatch.Set(keyForHeight(result), hash)
  81. }
  82. // index tx by hash
  83. rawBytes, err := cdc.MarshalBinaryBare(result)
  84. if err != nil {
  85. return err
  86. }
  87. storeBatch.Set(hash, rawBytes)
  88. }
  89. storeBatch.WriteSync()
  90. return nil
  91. }
  92. // Index indexes a single transaction using the given list of events. Each key
  93. // that indexed from the tx's events is a composite of the event type and the
  94. // respective attribute's key delimited by a "." (eg. "account.number").
  95. // Any event with an empty type is not indexed.
  96. func (txi *TxIndex) Index(result *types.TxResult) error {
  97. b := txi.store.NewBatch()
  98. defer b.Close()
  99. hash := result.Tx.Hash()
  100. // index tx by events
  101. txi.indexEvents(result, hash, b)
  102. // index tx by height
  103. if txi.indexAllEvents || tmstring.StringInSlice(types.TxHeightKey, txi.compositeKeysToIndex) {
  104. b.Set(keyForHeight(result), hash)
  105. }
  106. // index tx by hash
  107. rawBytes, err := cdc.MarshalBinaryBare(result)
  108. if err != nil {
  109. return err
  110. }
  111. b.Set(hash, rawBytes)
  112. b.WriteSync()
  113. return nil
  114. }
  115. func (txi *TxIndex) indexEvents(result *types.TxResult, hash []byte, store dbm.SetDeleter) {
  116. for _, event := range result.Result.Events {
  117. // only index events with a non-empty type
  118. if len(event.Type) == 0 {
  119. continue
  120. }
  121. for _, attr := range event.Attributes {
  122. if len(attr.Key) == 0 {
  123. continue
  124. }
  125. compositeTag := fmt.Sprintf("%s.%s", event.Type, string(attr.Key))
  126. if txi.indexAllEvents || tmstring.StringInSlice(compositeTag, txi.compositeKeysToIndex) {
  127. store.Set(keyForEvent(compositeTag, attr.Value, result), hash)
  128. }
  129. }
  130. }
  131. }
  132. // Search performs a search using the given query. It breaks the query into
  133. // conditions (like "tx.height > 5"). For each condition, it queries the DB
  134. // index. One special use cases here: (1) if "tx.hash" is found, it returns tx
  135. // result for it (2) for range queries it is better for the client to provide
  136. // both lower and upper bounds, so we are not performing a full scan. Results
  137. // from querying indexes are then intersected and returned to the caller.
  138. func (txi *TxIndex) Search(q *query.Query) ([]*types.TxResult, error) {
  139. var hashesInitialized bool
  140. filteredHashes := make(map[string][]byte)
  141. // get a list of conditions (like "tx.height > 5")
  142. conditions, err := q.Conditions()
  143. if err != nil {
  144. return nil, errors.Wrap(err, "error during parsing conditions from query")
  145. }
  146. // if there is a hash condition, return the result immediately
  147. hash, ok, err := lookForHash(conditions)
  148. if err != nil {
  149. return nil, errors.Wrap(err, "error during searching for a hash in the query")
  150. } else if ok {
  151. res, err := txi.Get(hash)
  152. switch {
  153. case err != nil:
  154. return []*types.TxResult{}, errors.Wrap(err, "error while retrieving the result")
  155. case res == nil:
  156. return []*types.TxResult{}, nil
  157. default:
  158. return []*types.TxResult{res}, nil
  159. }
  160. }
  161. // conditions to skip because they're handled before "everything else"
  162. skipIndexes := make([]int, 0)
  163. // extract ranges
  164. // if both upper and lower bounds exist, it's better to get them in order not
  165. // no iterate over kvs that are not within range.
  166. ranges, rangeIndexes := lookForRanges(conditions)
  167. if len(ranges) > 0 {
  168. skipIndexes = append(skipIndexes, rangeIndexes...)
  169. for _, r := range ranges {
  170. if !hashesInitialized {
  171. filteredHashes = txi.matchRange(r, startKey(r.key), filteredHashes, true)
  172. hashesInitialized = true
  173. // Ignore any remaining conditions if the first condition resulted
  174. // in no matches (assuming implicit AND operand).
  175. if len(filteredHashes) == 0 {
  176. break
  177. }
  178. } else {
  179. filteredHashes = txi.matchRange(r, startKey(r.key), filteredHashes, false)
  180. }
  181. }
  182. }
  183. // if there is a height condition ("tx.height=3"), extract it
  184. height := lookForHeight(conditions)
  185. // for all other conditions
  186. for i, c := range conditions {
  187. if intInSlice(i, skipIndexes) {
  188. continue
  189. }
  190. if !hashesInitialized {
  191. filteredHashes = txi.match(c, startKeyForCondition(c, height), filteredHashes, true)
  192. hashesInitialized = true
  193. // Ignore any remaining conditions if the first condition resulted
  194. // in no matches (assuming implicit AND operand).
  195. if len(filteredHashes) == 0 {
  196. break
  197. }
  198. } else {
  199. filteredHashes = txi.match(c, startKeyForCondition(c, height), filteredHashes, false)
  200. }
  201. }
  202. results := make([]*types.TxResult, 0, len(filteredHashes))
  203. for _, h := range filteredHashes {
  204. res, err := txi.Get(h)
  205. if err != nil {
  206. return nil, errors.Wrapf(err, "failed to get Tx{%X}", h)
  207. }
  208. results = append(results, res)
  209. }
  210. // sort by height & index by default
  211. sort.Slice(results, func(i, j int) bool {
  212. if results[i].Height == results[j].Height {
  213. return results[i].Index < results[j].Index
  214. }
  215. return results[i].Height < results[j].Height
  216. })
  217. return results, nil
  218. }
  219. func lookForHash(conditions []query.Condition) (hash []byte, ok bool, err error) {
  220. for _, c := range conditions {
  221. if c.CompositeKey == types.TxHashKey {
  222. decoded, err := hex.DecodeString(c.Operand.(string))
  223. return decoded, true, err
  224. }
  225. }
  226. return
  227. }
  228. // lookForHeight returns a height if there is an "height=X" condition.
  229. func lookForHeight(conditions []query.Condition) (height int64) {
  230. for _, c := range conditions {
  231. if c.CompositeKey == types.TxHeightKey && c.Op == query.OpEqual {
  232. return c.Operand.(int64)
  233. }
  234. }
  235. return 0
  236. }
  237. // special map to hold range conditions
  238. // Example: account.number => queryRange{lowerBound: 1, upperBound: 5}
  239. type queryRanges map[string]queryRange
  240. type queryRange struct {
  241. lowerBound interface{} // int || time.Time
  242. upperBound interface{} // int || time.Time
  243. key string
  244. includeLowerBound bool
  245. includeUpperBound bool
  246. }
  247. func (r queryRange) lowerBoundValue() interface{} {
  248. if r.lowerBound == nil {
  249. return nil
  250. }
  251. if r.includeLowerBound {
  252. return r.lowerBound
  253. }
  254. switch t := r.lowerBound.(type) {
  255. case int64:
  256. return t + 1
  257. case time.Time:
  258. return t.Unix() + 1
  259. default:
  260. panic("not implemented")
  261. }
  262. }
  263. func (r queryRange) AnyBound() interface{} {
  264. if r.lowerBound != nil {
  265. return r.lowerBound
  266. }
  267. return r.upperBound
  268. }
  269. func (r queryRange) upperBoundValue() interface{} {
  270. if r.upperBound == nil {
  271. return nil
  272. }
  273. if r.includeUpperBound {
  274. return r.upperBound
  275. }
  276. switch t := r.upperBound.(type) {
  277. case int64:
  278. return t - 1
  279. case time.Time:
  280. return t.Unix() - 1
  281. default:
  282. panic("not implemented")
  283. }
  284. }
  285. func lookForRanges(conditions []query.Condition) (ranges queryRanges, indexes []int) {
  286. ranges = make(queryRanges)
  287. for i, c := range conditions {
  288. if isRangeOperation(c.Op) {
  289. r, ok := ranges[c.CompositeKey]
  290. if !ok {
  291. r = queryRange{key: c.CompositeKey}
  292. }
  293. switch c.Op {
  294. case query.OpGreater:
  295. r.lowerBound = c.Operand
  296. case query.OpGreaterEqual:
  297. r.includeLowerBound = true
  298. r.lowerBound = c.Operand
  299. case query.OpLess:
  300. r.upperBound = c.Operand
  301. case query.OpLessEqual:
  302. r.includeUpperBound = true
  303. r.upperBound = c.Operand
  304. }
  305. ranges[c.CompositeKey] = r
  306. indexes = append(indexes, i)
  307. }
  308. }
  309. return ranges, indexes
  310. }
  311. func isRangeOperation(op query.Operator) bool {
  312. switch op {
  313. case query.OpGreater, query.OpGreaterEqual, query.OpLess, query.OpLessEqual:
  314. return true
  315. default:
  316. return false
  317. }
  318. }
  319. // match returns all matching txs by hash that meet a given condition and start
  320. // key. An already filtered result (filteredHashes) is provided such that any
  321. // non-intersecting matches are removed.
  322. //
  323. // NOTE: filteredHashes may be empty if no previous condition has matched.
  324. func (txi *TxIndex) match(
  325. c query.Condition,
  326. startKeyBz []byte,
  327. filteredHashes map[string][]byte,
  328. firstRun bool,
  329. ) map[string][]byte {
  330. // A previous match was attempted but resulted in no matches, so we return
  331. // no matches (assuming AND operand).
  332. if !firstRun && len(filteredHashes) == 0 {
  333. return filteredHashes
  334. }
  335. tmpHashes := make(map[string][]byte)
  336. switch {
  337. case c.Op == query.OpEqual:
  338. it, err := dbm.IteratePrefix(txi.store, startKeyBz)
  339. if err != nil {
  340. panic(err)
  341. }
  342. defer it.Close()
  343. for ; it.Valid(); it.Next() {
  344. tmpHashes[string(it.Value())] = it.Value()
  345. }
  346. case c.Op == query.OpContains:
  347. // XXX: startKey does not apply here.
  348. // For example, if startKey = "account.owner/an/" and search query = "account.owner CONTAINS an"
  349. // we can't iterate with prefix "account.owner/an/" because we might miss keys like "account.owner/Ulan/"
  350. it, err := dbm.IteratePrefix(txi.store, startKey(c.CompositeKey))
  351. if err != nil {
  352. panic(err)
  353. }
  354. defer it.Close()
  355. for ; it.Valid(); it.Next() {
  356. if !isTagKey(it.Key()) {
  357. continue
  358. }
  359. if strings.Contains(extractValueFromKey(it.Key()), c.Operand.(string)) {
  360. tmpHashes[string(it.Value())] = it.Value()
  361. }
  362. }
  363. default:
  364. panic("other operators should be handled already")
  365. }
  366. if len(tmpHashes) == 0 || firstRun {
  367. // Either:
  368. //
  369. // 1. Regardless if a previous match was attempted, which may have had
  370. // results, but no match was found for the current condition, then we
  371. // return no matches (assuming AND operand).
  372. //
  373. // 2. A previous match was not attempted, so we return all results.
  374. return tmpHashes
  375. }
  376. // Remove/reduce matches in filteredHashes that were not found in this
  377. // match (tmpHashes).
  378. for k := range filteredHashes {
  379. if tmpHashes[k] == nil {
  380. delete(filteredHashes, k)
  381. }
  382. }
  383. return filteredHashes
  384. }
  385. // matchRange returns all matching txs by hash that meet a given queryRange and
  386. // start key. An already filtered result (filteredHashes) is provided such that
  387. // any non-intersecting matches are removed.
  388. //
  389. // NOTE: filteredHashes may be empty if no previous condition has matched.
  390. func (txi *TxIndex) matchRange(
  391. r queryRange,
  392. startKey []byte,
  393. filteredHashes map[string][]byte,
  394. firstRun bool,
  395. ) map[string][]byte {
  396. // A previous match was attempted but resulted in no matches, so we return
  397. // no matches (assuming AND operand).
  398. if !firstRun && len(filteredHashes) == 0 {
  399. return filteredHashes
  400. }
  401. tmpHashes := make(map[string][]byte)
  402. lowerBound := r.lowerBoundValue()
  403. upperBound := r.upperBoundValue()
  404. it, err := dbm.IteratePrefix(txi.store, startKey)
  405. if err != nil {
  406. panic(err)
  407. }
  408. defer it.Close()
  409. LOOP:
  410. for ; it.Valid(); it.Next() {
  411. if !isTagKey(it.Key()) {
  412. continue
  413. }
  414. if _, ok := r.AnyBound().(int64); ok {
  415. v, err := strconv.ParseInt(extractValueFromKey(it.Key()), 10, 64)
  416. if err != nil {
  417. continue LOOP
  418. }
  419. include := true
  420. if lowerBound != nil && v < lowerBound.(int64) {
  421. include = false
  422. }
  423. if upperBound != nil && v > upperBound.(int64) {
  424. include = false
  425. }
  426. if include {
  427. tmpHashes[string(it.Value())] = it.Value()
  428. }
  429. // XXX: passing time in a ABCI Events is not yet implemented
  430. // case time.Time:
  431. // v := strconv.ParseInt(extractValueFromKey(it.Key()), 10, 64)
  432. // if v == r.upperBound {
  433. // break
  434. // }
  435. }
  436. }
  437. if len(tmpHashes) == 0 || firstRun {
  438. // Either:
  439. //
  440. // 1. Regardless if a previous match was attempted, which may have had
  441. // results, but no match was found for the current condition, then we
  442. // return no matches (assuming AND operand).
  443. //
  444. // 2. A previous match was not attempted, so we return all results.
  445. return tmpHashes
  446. }
  447. // Remove/reduce matches in filteredHashes that were not found in this
  448. // match (tmpHashes).
  449. for k := range filteredHashes {
  450. if tmpHashes[k] == nil {
  451. delete(filteredHashes, k)
  452. }
  453. }
  454. return filteredHashes
  455. }
  456. ///////////////////////////////////////////////////////////////////////////////
  457. // Keys
  458. func isTagKey(key []byte) bool {
  459. return strings.Count(string(key), tagKeySeparator) == 3
  460. }
  461. func extractValueFromKey(key []byte) string {
  462. parts := strings.SplitN(string(key), tagKeySeparator, 3)
  463. return parts[1]
  464. }
  465. func keyForEvent(key string, value []byte, result *types.TxResult) []byte {
  466. return []byte(fmt.Sprintf("%s/%s/%d/%d",
  467. key,
  468. value,
  469. result.Height,
  470. result.Index,
  471. ))
  472. }
  473. func keyForHeight(result *types.TxResult) []byte {
  474. return []byte(fmt.Sprintf("%s/%d/%d/%d",
  475. types.TxHeightKey,
  476. result.Height,
  477. result.Height,
  478. result.Index,
  479. ))
  480. }
  481. func startKeyForCondition(c query.Condition, height int64) []byte {
  482. if height > 0 {
  483. return startKey(c.CompositeKey, c.Operand, height)
  484. }
  485. return startKey(c.CompositeKey, c.Operand)
  486. }
  487. func startKey(fields ...interface{}) []byte {
  488. var b bytes.Buffer
  489. for _, f := range fields {
  490. b.Write([]byte(fmt.Sprintf("%v", f) + tagKeySeparator))
  491. }
  492. return b.Bytes()
  493. }