Skip to content

Commit

Permalink
fix(CLI): implement TableResolver with a hacky way (#25)
Browse files Browse the repository at this point in the history
* fix(CLI): implement TableResolver with a hack way

* fmt
  • Loading branch information
Poytr1 authored Nov 24, 2022
1 parent fd2f291 commit 78d3cae
Show file tree
Hide file tree
Showing 7 changed files with 866 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ uuid = { version = "1.2.1", features = [
"fast-rng", # Use a faster (but still sufficiently random) RNG
"macro-diagnostics", # Enable better diagnostics for compile-time UUIDs
]}
better_any = "0.1.1"
sha3 = "0.9.1"
smallvec = "1.7.0"
reqwest = { version = "0.11.12", features = ["blocking", "json"] }

[dev-dependencies]
ctor = "0.1.26"
10 changes: 10 additions & 0 deletions src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ pub fn get_function_module(
Ok((None, abi))
}

// pub fn get_table_item(
// client: Client,
// table_handle: &AccountAddress,
// network: String,
// ledger_version: u64
// ) {
// let aptos_account = AptosAccountAddress::from_bytes(table_handle.into_bytes()).unwrap();
// client.get_table_item_bcs(aptos_account, "", "", ())
// }

pub fn construct_struct_type_tag_from_str(raw: &str) -> TypeTag {
let mut splitted = raw.split("::");
let address = AccountAddress::from_hex_literal(splitted.next().unwrap()).unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod config;
mod helper;
mod storage;
mod table;
23 changes: 18 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod config;
mod helper;
mod storage;
mod table;

extern crate core;
extern crate log;

Expand All @@ -20,7 +22,7 @@ use clap::{arg, command};
use log::{debug, error, LevelFilter};
use move_core_types::account_address::AccountAddress;
use move_core_types::identifier::{IdentStr, Identifier};
use move_core_types::language_storage::{ModuleId, TypeTag};
use move_core_types::language_storage::{ModuleId, TypeTag, CORE_CODE_ADDRESS};
use move_core_types::value::MoveValue;
use move_stdlib;
use move_vm_runtime::move_vm::MoveVM;
Expand All @@ -29,15 +31,17 @@ use serde::{Deserialize, Serialize};
use uuid::Uuid;

use aptos_vm::natives;
use move_table_extension::NativeTableContext;
use move_vm_runtime::native_extensions::NativeContextExtensions;
use move_vm_runtime::native_functions::NativeFunctionTable;
use toml::value::Table;

use crate::config::{ConfigData, ToolConfig};
use crate::helper::{
absolute_path, construct_struct_type_tag_from_str, get_function_module, get_node_url,
serialize_input_params,
};
use crate::storage::InMemoryLazyStorage;
use crate::table::NativeTableContext;

const STD_ADDR: AccountAddress = AccountAddress::ONE;

Expand Down Expand Up @@ -236,12 +240,21 @@ fn exec_func_internal(
type_args: Vec<TypeTag>,
args: Vec<Vec<u8>>,
) -> Option<Vec<String>> {
let vm = MoveVM::new(natives::aptos_natives(
let natives = natives::aptos_natives(
NativeGasParameters::zeros(),
AbstractValueSizeGasParameters::zeros(),
LATEST_GAS_FEATURE_VERSION,
))
.unwrap();
);
let extended_natives: NativeFunctionTable = natives
.into_iter()
.filter(|(_, name, _, _)| name.as_str() != "table")
.chain(table::table_natives(
CORE_CODE_ADDRESS,
table::GasParameters::zeros(),
))
.collect();

let vm = MoveVM::new(extended_natives).unwrap();

let mut extensions = NativeContextExtensions::default();
extensions.add(NativeTableContext::new([0u8; 32], &storage));
Expand Down
46 changes: 43 additions & 3 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ use aptos_sdk::move_types::account_address::AccountAddress as AptosAccountAddres
use aptos_sdk::move_types::language_storage::StructTag as AptosStructTag;

use crate::helper::get_function_module;
use anyhow::{bail, Result};
use crate::table::{TableHandle, TableResolver};
use anyhow::{bail, Error, Result};
use aptos_sdk::bcs;
use aptos_sdk::rest_client::aptos_api_types::mime_types::BCS;
use aptos_sdk::rest_client::Client;
use log::{debug, error};
use move_core_types::account_address::AccountAddress;
use move_core_types::effects::{AccountChangeSet, ChangeSet, Op};
use move_core_types::identifier::Identifier;
use move_core_types::language_storage::{ModuleId, StructTag};
use move_core_types::language_storage::{ModuleId, StructTag, TypeTag};
use move_core_types::resolver::{ModuleResolver, ResourceResolver};
use move_table_extension::{TableHandle, TableResolver};
use move_vm_types::loaded_data::runtime_types::Type;
use reqwest::header::ACCEPT;
use serde_json::json;
use std::collections::HashMap;
use std::str::FromStr;
use std::{
collections::{btree_map, BTreeMap},
Expand Down Expand Up @@ -208,4 +214,38 @@ impl TableResolver for InMemoryLazyStorage {
) -> std::result::Result<Option<Vec<u8>>, anyhow::Error> {
Ok(None)
}

fn resolve_table_entry_with_key_value_ty(
&self,
handle: &TableHandle,
key_ty: &TypeTag,
value_ty: &TypeTag,
key: &[u8],
) -> std::result::Result<Option<Vec<u8>>, Error> {
let url_string = if self.ledger_version > 0 {
format!(
"https://fullnode.{}.aptoslabs.com/v1/tables/0x{}/item?ledger_version={}",
self.network, handle.0, self.ledger_version
)
} else {
format!(
"https://fullnode.{}.aptoslabs.com/v1/tables/0x{}/item",
self.network, handle.0
)
};
let c = reqwest::blocking::Client::new();
let mut map = HashMap::new();
map.insert("key_type", key_ty.to_string());
map.insert("value_type", value_ty.to_string());
map.insert("key", hex::encode(key));
let resp = c
.post(url_string)
.header(ACCEPT, BCS)
.json(&map)
.send()
.unwrap()
.bytes()
.unwrap();
Ok(Some(resp.to_vec()))
}
}
Loading

0 comments on commit 78d3cae

Please sign in to comment.