Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

geyser: remove duplicated account updates for confirmed/finalized #152

Merged
merged 3 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The minor version will be incremented upon a breaking change and the patch versi
### Fixes

- client: set max message size for decode ([#151](https://github.com/rpcpool/yellowstone-grpc/pull/151)).
- geyser: remove duplicated account updates for confirmed/finalized ([#152](https://github.com/rpcpool/yellowstone-grpc/pull/152)).

### Breaking

Expand Down
38 changes: 31 additions & 7 deletions yellowstone-grpc-geyser/src/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,11 @@ impl GrpcService {
u64,
(Option<MessageBlockMeta>, Vec<MessageTransactionInfo>),
> = BTreeMap::new();
let mut messages: HashMap<u64, Vec<Message>> = HashMap::new();
#[allow(clippy::type_complexity)]
let mut messages: HashMap<
u64,
(Vec<Option<Message>>, HashMap<Pubkey, (u64, usize)>),
> = HashMap::new();
let mut processed_messages = Vec::with_capacity(PROCESSED_MESSAGES_MAX);
let processed_sleep = sleep(PROCESSED_MESSAGES_SLEEP);
tokio::pin!(processed_sleep);
Expand All @@ -527,12 +531,18 @@ impl GrpcService {
(Vec::with_capacity(1), Vec::with_capacity(1))
}
CommitmentLevel::Confirmed => {
let messages = messages.get(&slot.slot).cloned().unwrap_or_default();
let messages = messages
.get(&slot.slot)
.map(|entry| entry.0.iter().filter_map(|x| x.clone()).collect())
.unwrap_or_default();
(messages, Vec::with_capacity(1))
}
CommitmentLevel::Finalized => {
messages.retain(|msg_slot, _messages| *msg_slot >= slot.slot);
let messages = messages.remove(&slot.slot).unwrap_or_default();
let messages = messages
.remove(&slot.slot)
.map(|entry| entry.0.into_iter().filter_map(|x| x).collect())
.unwrap_or_default();
(Vec::with_capacity(1), messages)
}
};
Expand Down Expand Up @@ -565,10 +575,24 @@ impl GrpcService {
.as_mut()
.reset(Instant::now() + PROCESSED_MESSAGES_SLEEP);
}
messages
.entry($message.get_slot())
.or_default()
.push($message);
let (vec, map) = messages.entry($message.get_slot()).or_default();
if let Message::Account(message) = &$message {
let write_version = message.account.write_version;
let index = vec.len();
if let Some(entry) = map.get_mut(&message.account.pubkey) {
if entry.0 < write_version {
vec[entry.1] = None; // We would able to make replace but then we will lose message order
vec.push(Some($message));
entry.0 = write_version;
entry.1 = index;
}
} else {
map.insert(message.account.pubkey, (write_version, index));
vec.push(Some($message));
}
} else {
vec.push(Some($message));
}
}
};
}
Expand Down