Skip to content

Commit

Permalink
Cherry-pick: jsonrpc: populate object cache with input objects (#19589)…
Browse files Browse the repository at this point in the history
… (#19640)

Cherrypick #19589 to 1.34

## Description 

Describe the changes or additions included in this PR.

## Test plan 

How did you test the new or updated feature?

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:

Co-authored-by: Brandon Williams <[email protected]>
  • Loading branch information
halfprice and bmwill authored Oct 1, 2024
1 parent 218db6e commit 5d904d3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
46 changes: 20 additions & 26 deletions crates/sui-json-rpc/src/balance_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,55 +182,49 @@ impl<P> ObjectProviderCache<P> {
}
}

pub fn new_with_cache(
provider: P,
written_objects: BTreeMap<ObjectID, (ObjectRef, Object, WriteKind)>,
) -> Self {
let mut object_cache = BTreeMap::new();
let mut last_version_cache = BTreeMap::new();
pub fn insert_objects_into_cache(&mut self, objects: Vec<Object>) {
let object_cache = self.object_cache.get_mut();
let last_version_cache = self.last_version_cache.get_mut();

for (object_id, (object_ref, object, _)) in written_objects {
let key = (object_id, object_ref.1);
for object in objects {
let object_id = object.id();
let version = object.version();

let key = (object_id, version);
object_cache.insert(key, object.clone());

match last_version_cache.get_mut(&key) {
Some(existing_seq_number) => {
if object_ref.1 > *existing_seq_number {
*existing_seq_number = object_ref.1
if version > *existing_seq_number {
*existing_seq_number = version
}
}
None => {
last_version_cache.insert(key, object_ref.1);
last_version_cache.insert(key, version);
}
}
}

Self {
object_cache: RwLock::new(object_cache),
last_version_cache: RwLock::new(last_version_cache),
provider,
}
}

pub fn new_with_output_objects(provider: P, output_objects: Vec<Object>) -> Self {
pub fn new_with_cache(
provider: P,
written_objects: BTreeMap<ObjectID, (ObjectRef, Object, WriteKind)>,
) -> Self {
let mut object_cache = BTreeMap::new();
let mut last_version_cache = BTreeMap::new();

for object in output_objects {
let object_id = object.id();
let version = object.version();

let key = (object_id, version);
for (object_id, (object_ref, object, _)) in written_objects {
let key = (object_id, object_ref.1);
object_cache.insert(key, object.clone());

match last_version_cache.get_mut(&key) {
Some(existing_seq_number) => {
if version > *existing_seq_number {
*existing_seq_number = version
if object_ref.1 > *existing_seq_number {
*existing_seq_number = object_ref.1
}
}
None => {
last_version_cache.insert(key, version);
last_version_cache.insert(key, object_ref.1);
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions crates/sui-json-rpc/src/transaction_execution_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,15 @@ impl TransactionExecutionApi {
None
};

let object_cache = response.output_objects.map(|output_objects| {
ObjectProviderCache::new_with_output_objects(self.state.clone(), output_objects)
});
let object_cache = match (response.input_objects, response.output_objects) {
(Some(input_objects), Some(output_objects)) => {
let mut object_cache = ObjectProviderCache::new(self.state.clone());
object_cache.insert_objects_into_cache(input_objects);
object_cache.insert_objects_into_cache(output_objects);
Some(object_cache)
}
_ => None,
};

let balance_changes = match &object_cache {
Some(object_cache) if opts.show_balance_changes => Some(
Expand Down

0 comments on commit 5d904d3

Please sign in to comment.