Skip to content

Commit

Permalink
[API] WriteSetChange::Delete/WriteTableItem
Browse files Browse the repository at this point in the history
  • Loading branch information
msmouse authored and aptos-bot committed Apr 27, 2022
1 parent 301c2e6 commit 7ca61f5
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 7 deletions.
55 changes: 55 additions & 0 deletions api/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1374,8 +1374,10 @@ components:
oneOf:
- $ref: '#/components/schemas/DeleteModule'
- $ref: '#/components/schemas/DeleteResource'
- $ref: '#/components/schemas/DeleteTableItem'
- $ref: '#/components/schemas/WriteModule'
- $ref: '#/components/schemas/WriteResource'
- $ref: '#/components/schemas/WriteTableItem'
discriminator:
propertyName: type
DeleteModule:
Expand Down Expand Up @@ -1415,6 +1417,31 @@ components:
$ref: '#/components/schemas/Address'
resource:
$ref: '#/components/schemas/MoveStructTagId'
DeleteTableItem:
title: Delete Table Item
type: object
description: Delete table item change.
required:
- type
- state_key_hash
- data
properties:
type:
type: string
example: "delete_table_item"
state_key_hash:
$ref: '#/components/schemas/HexEncodedBytes'
data:
title: Table item deletion
type: object
required:
- handle
- key
properties:
handle:
$ref: '#/components/schemas/HexEncodedBytes'
key:
$ref: '#/components/schemas/HexEncodedBytes'
WriteModule:
title: Write Module
type: object
Expand Down Expand Up @@ -1453,6 +1480,34 @@ components:
$ref: '#/components/schemas/Address'
data:
$ref: '#/components/schemas/AccountResource'
WriteTableItem:
title: Write Table Item
type: object
description: Write table item
required:
- type
- state_key_hash
- data
properties:
type:
type: string
example: "write_table_item"
state_key_hash:
$ref: '#/components/schemas/HexEncodedBytes'
data:
title: Table item write
type: object
required:
- handle
- key
- value
properties:
handle:
$ref: '#/components/schemas/HexEncodedBytes'
key:
$ref: '#/components/schemas/HexEncodedBytes'
value:
$ref: '#/components/schemas/HexEncodedBytes'
Script:
title: Script
type: object
Expand Down
43 changes: 36 additions & 7 deletions api/types/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,24 +204,28 @@ impl<'a, R: MoveResolverExt + ?Sized> MoveConverter<'a, R> {
state_key: StateKey,
op: WriteOp,
) -> Result<WriteSetChange> {
let hash = state_key.hash().to_hex_literal();

match state_key {
StateKey::AccessPath(access_path) => {
self.try_access_path_into_write_set_change(access_path, op)
self.try_access_path_into_write_set_change(hash, access_path, op)
}
StateKey::TableItem { handle, key } => {
self.try_table_item_into_write_set_change(hash, handle, key, op)
}
StateKey::AccountAddressKey(_) | StateKey::Raw(_) | StateKey::TableItem { .. } => Err(
format_err!("Can't convert state key {:?} to WriteSetChange", state_key),
),
StateKey::AccountAddressKey(_) | StateKey::Raw(_) => Err(format_err!(
"Can't convert state key {:?} to WriteSetChange",
state_key
)),
}
}

pub fn try_access_path_into_write_set_change(
&self,
state_key_hash: String,
access_path: AccessPath,
op: WriteOp,
) -> Result<WriteSetChange> {
let state_key_hash = StateKey::AccessPath(access_path.clone())
.hash()
.to_hex_literal();
let ret = match op {
WriteOp::Deletion => match access_path.get_path() {
Path::Code(module_id) => WriteSetChange::DeleteModule {
Expand Down Expand Up @@ -251,6 +255,31 @@ impl<'a, R: MoveResolverExt + ?Sized> MoveConverter<'a, R> {
Ok(ret)
}

pub fn try_table_item_into_write_set_change(
&self,
state_key_hash: String,
handle: u128,
key: Vec<u8>,
op: WriteOp,
) -> Result<WriteSetChange> {
let handle = handle.to_be_bytes().to_vec().into();
let key = key.into();
let ret = match op {
WriteOp::Deletion => WriteSetChange::DeleteTableItem {
state_key_hash,
handle,
key,
},
WriteOp::Value(value) => WriteSetChange::WriteTableItem {
state_key_hash,
handle,
key,
value: value.into(),
},
};
Ok(ret)
}

pub fn try_into_events(&self, events: &[ContractEvent]) -> Result<Vec<Event>> {
let mut ret = vec![];
for event in events {
Expand Down
13 changes: 13 additions & 0 deletions api/types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,11 @@ pub enum WriteSetChange {
state_key_hash: String,
resource: MoveStructTag,
},
DeleteTableItem {
state_key_hash: String,
handle: HexEncodedBytes,
key: HexEncodedBytes,
},
WriteModule {
address: Address,
state_key_hash: String,
Expand All @@ -470,15 +475,23 @@ pub enum WriteSetChange {
state_key_hash: String,
data: MoveResource,
},
WriteTableItem {
state_key_hash: String,
handle: HexEncodedBytes,
key: HexEncodedBytes,
value: HexEncodedBytes,
},
}

impl WriteSetChange {
pub fn type_str(&self) -> &'static str {
match self {
WriteSetChange::DeleteModule { .. } => "delete_module",
WriteSetChange::DeleteResource { .. } => "delete_resource",
WriteSetChange::DeleteTableItem { .. } => "delete_table_item",
WriteSetChange::WriteModule { .. } => "write_module",
WriteSetChange::WriteResource { .. } => "write_resource",
WriteSetChange::WriteTableItem { .. } => "write_table_item",
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions ecosystem/indexer/src/models/write_set_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::{models::transactions::Transaction, schema::write_set_changes};
use aptos_rest_client::aptos_api_types::WriteSetChange as APIWriteSetChange;
use serde::Serialize;
use serde_json::json;

#[derive(AsChangeset, Associations, Debug, Identifiable, Insertable, Queryable, Serialize)]
#[diesel(table_name = "write_set_changes")]
Expand Down Expand Up @@ -57,6 +58,23 @@ impl WriteSetChange {
data: Default::default(),
inserted_at: chrono::Utc::now().naive_utc(),
},
APIWriteSetChange::DeleteTableItem {
state_key_hash,
handle,
key,
} => WriteSetChange {
transaction_hash,
hash: state_key_hash.clone(),
type_: write_set_change.type_str().to_string(),
address: "".to_owned(),
module: Default::default(),
resource: Default::default(),
data: json!({
"handle": handle,
"key": key,
}),
inserted_at: chrono::Utc::now().naive_utc(),
},
APIWriteSetChange::WriteModule {
address,
state_key_hash,
Expand Down Expand Up @@ -85,6 +103,25 @@ impl WriteSetChange {
data: serde_json::to_value(data).unwrap(),
inserted_at: chrono::Utc::now().naive_utc(),
},
APIWriteSetChange::WriteTableItem {
state_key_hash,
handle,
key,
value,
} => WriteSetChange {
transaction_hash,
hash: state_key_hash.clone(),
type_: write_set_change.type_str().to_string(),
address: "".to_owned(),
module: Default::default(),
resource: Default::default(),
data: json!({
"handle": handle,
"key": key,
"value": value,
}),
inserted_at: chrono::Utc::now().naive_utc(),
},
}
}

Expand Down

0 comments on commit 7ca61f5

Please sign in to comment.