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

enhancement: add support for unindexable types #943

Merged
merged 10 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
3 changes: 2 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ fuel-tx = { version = "0.26", default-features = false }
fuel-types = { version = "0.26", default-features = false }
fuels = { version = "0.40", default-features = false }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = { version = "1.0", default-features = false }
thiserror = "1.0"
tokio = "1.17"
tracing = "0.1"
2 changes: 1 addition & 1 deletion examples/block-explorer/explorer-indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ fuel-indexer-plugin = { workspace = true }
fuel-indexer-schema = { workspace = true, default-features = false }
fuels = { workspace = true }
serde = { workspace = true }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
serde_json = { workspace = true, features = ["alloc"] }

2 changes: 1 addition & 1 deletion packages/fuel-indexer-api-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ hyper = { version = "0.14", features = ["client", "http2", "http1", "runtime" ]
hyper-rustls = { version = "0.23", features = ["http2"] }
jsonwebtoken = "8"
serde = { features = ["derive"], workspace = true }
serde_json = { version = "1.0", features = ["raw_value"] }
serde_json = { workspace = true, features = ["raw_value"] }
sqlx = { version = "0.6", features = ["postgres", "runtime-tokio-rustls", "bigdecimal"] }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ impl Index {
}
}
}

/// Directive specifying to not build SQL tables for object.
pub struct NoRelation(pub bool);
5 changes: 5 additions & 0 deletions packages/fuel-indexer-database/database-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ impl NewColumn {
ColumnType::Enum => "varchar(255)",
ColumnType::Int1 => "integer",
ColumnType::UInt1 => "integer",
ColumnType::NoRelation => "Json",
}
}
}
Expand Down Expand Up @@ -209,6 +210,7 @@ pub enum ColumnType {
Enum = 30,
Int1 = 31,
UInt1 = 32,
NoRelation = 33,
}

impl From<ColumnType> for i32 {
Expand Down Expand Up @@ -247,6 +249,7 @@ impl From<ColumnType> for i32 {
ColumnType::Enum => 30,
ColumnType::Int1 => 31,
ColumnType::UInt1 => 32,
ColumnType::NoRelation => 33,
}
}
}
Expand Down Expand Up @@ -293,6 +296,7 @@ impl From<i32> for ColumnType {
30 => ColumnType::Enum,
31 => ColumnType::Int1,
32 => ColumnType::UInt1,
33 => ColumnType::NoRelation,
_ => panic!("Invalid column type."),
}
}
Expand Down Expand Up @@ -334,6 +338,7 @@ impl From<&str> for ColumnType {
"Enum" => ColumnType::Enum,
"Int1" => ColumnType::Int1,
"UInt1" => ColumnType::UInt1,
"NoRelation" => ColumnType::NoRelation,
_ => panic!("Invalid column type: '{name}'"),
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/fuel-indexer-graphql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fuel-indexer-database-types = { workspace = true }
fuel-indexer-schema = { workspace = true, features = ["db-models"] }
fuel-indexer-types = { workspace = true }
lazy_static = "1.4"
serde_json = "1.0"
serde_json = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions packages/fuel-indexer-graphql/src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ lazy_static! {
"Json",
"MessageId",
"Nonce",
"NoRelation",
"Salt",
"Signature",
"Tai64Timestamp",
Expand Down
5 changes: 3 additions & 2 deletions packages/fuel-indexer-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ description = "Fuel Indexer Library"

[dependencies]
anyhow = "1.0"
bincode = { workspace = true }
clap = { features = ["cargo", "derive", "env"], workspace = true }
fuel-indexer-types = { workspace = true }
http = { version = "0.2", default-features = false }
serde = { features = ["derive"], workspace = true }
serde_json = "1.0"
serde = { workspace = true }
serde_json = { workspace = true }
serde_yaml = "0.8"
sha2 = "0.9"
strum = { version = "0.24", default-features = false, features = ["derive"] }
Expand Down
13 changes: 13 additions & 0 deletions packages/fuel-indexer-lib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ const HUMAN_LOGGING: &str = "HUMAN_LOGGING";

const ROOT_DIRECTORY_NAME: &str = "fuel-indexer";

/// Serialize a generic item.
pub fn serialize(obj: &impl Serialize) -> Vec<u8> {
bincode::serialize(obj).expect("Serialize failed")
}

/// Deserialize a generic item.
pub fn deserialize<'a, T: Deserialize<'a>>(bytes: &'a [u8]) -> Result<T, String> {
match bincode::deserialize(bytes) {
Ok(obj) => Ok(obj),
Err(e) => Err(format!("Bincode serde error {e:?}")),
}
}

// Testing assets use relative paths, while production assets will use absolute paths
//
// If we can successfully find the local project root, then we're in the repository,
Expand Down
2 changes: 1 addition & 1 deletion packages/fuel-indexer-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ lazy_static = "1.4"
proc-macro-error = "1.0"
proc-macro2 = "1.0"
quote = "1.0"
serde_json = "1.0.64"
serde_json = { workspace = true }
sha2 = "0.9.5"
syn = { version = "2.0", features = ["full"] }

Expand Down
20 changes: 10 additions & 10 deletions packages/fuel-indexer-macros/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ fn process_fn_items(
let mut decoder = Decoders::default();

let ty_id = abi::BlockData::type_id();
let data = bincode::serialize(&block).expect("Bad serialization.");
let data = serialize(&block);
decoder.decode_type(ty_id, data);

for tx in block.transactions {
Expand All @@ -491,14 +491,14 @@ fn process_fn_items(
return_types.push(param1);
callees.insert(id);

let data = bincode::serialize(&abi::Call { contract_id, to: id, amount, asset_id, gas, fn_name }).expect("Bad encoding");
let data = serialize(&abi::Call { contract_id, to: id, amount, asset_id, gas, fn_name });
let ty_id = abi::Call::type_id();
decoder.decode_type(ty_id, data);
}
Receipt::Log { id, ra, rb, .. } => {
#check_if_subscribed_to_contract
let ty_id = abi::Log::type_id();
let data = bincode::serialize(&abi::Log{ contract_id: id, ra, rb }).expect("Bad encoding,");
let data = serialize(&abi::Log{ contract_id: id, ra, rb });
decoder.decode_type(ty_id, data);
}
Receipt::LogData { rb, data, ptr, len, id, .. } => {
Expand All @@ -510,7 +510,7 @@ fn process_fn_items(
#check_if_subscribed_to_contract
if callees.contains(&id) {
let ty_id = abi::Return::type_id();
let data = bincode::serialize(&abi::Return{ contract_id: id, val, pc, is }).expect("Bad encoding,");
let data = serialize(&abi::Return{ contract_id: id, val, pc, is });
decoder.decode_type(ty_id, data);
}
}
Expand Down Expand Up @@ -545,36 +545,36 @@ fn process_fn_items(
decoder.decode_messagedata(type_id, data.clone());

let ty_id = abi::MessageOut::type_id();
let data = bincode::serialize(&abi::MessageOut{ message_id, sender, recipient, amount, nonce, len, digest, data }).expect("Bad encoding");
let data = serialize(&abi::MessageOut{ message_id, sender, recipient, amount, nonce, len, digest, data });
decoder.decode_type(ty_id, data);
}
Receipt::ScriptResult { result, gas_used } => {
let ty_id = abi::ScriptResult::type_id();
let data = bincode::serialize(&abi::ScriptResult{ result: u64::from(result), gas_used }).expect("Bad encoding,");
let data = serialize(&abi::ScriptResult{ result: u64::from(result), gas_used });
decoder.decode_type(ty_id, data);
}
Receipt::Transfer { id, to, asset_id, amount, pc, is, .. } => {
#check_if_subscribed_to_contract
let ty_id = abi::Transfer::type_id();
let data = bincode::serialize(&abi::Transfer{ contract_id: id, to, asset_id, amount, pc, is }).expect("Bad encoding,");
let data = serialize(&abi::Transfer{ contract_id: id, to, asset_id, amount, pc, is });
decoder.decode_type(ty_id, data);
}
Receipt::TransferOut { id, to, asset_id, amount, pc, is, .. } => {
#check_if_subscribed_to_contract
let ty_id = abi::TransferOut::type_id();
let data = bincode::serialize(&abi::TransferOut{ contract_id: id, to, asset_id, amount, pc, is }).expect("Bad encoding,");
let data = serialize(&abi::TransferOut{ contract_id: id, to, asset_id, amount, pc, is });
decoder.decode_type(ty_id, data);
}
Receipt::Panic { id, reason, .. } => {
#check_if_subscribed_to_contract
let ty_id = abi::Panic::type_id();
let data = bincode::serialize(&abi::Panic{ contract_id: id, reason: *reason.reason() as u32 }).expect("Bad encoding,");
let data = serialize(&abi::Panic{ contract_id: id, reason: *reason.reason() as u32 });
decoder.decode_type(ty_id, data);
}
Receipt::Revert { id, ra, .. } => {
#check_if_subscribed_to_contract
let ty_id = abi::Revert::type_id();
let data = bincode::serialize(&abi::Revert{ contract_id: id, error_val: u64::from(ra & 0xF) }).expect("Bad encoding,");
let data = serialize(&abi::Revert{ contract_id: id, error_val: u64::from(ra & 0xF) });
decoder.decode_type(ty_id, data);
}
_ => {
Expand Down
2 changes: 1 addition & 1 deletion packages/fuel-indexer-macros/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn handler_block_native(
fn native_prelude() -> proc_macro2::TokenStream {
quote! {
use fuel_indexer_plugin::native::*;
use fuel_indexer_plugin::{{serialize, deserialize}, types::*, serde::{Deserialize, Serialize}};
use fuel_indexer_plugin::{{serialize, deserialize}, types::*, serde::{Deserialize, Serialize}, serde_json};

// TODO: Eventually prevent these types of implicity imports and have users import
// all dependencies explicity (preferably through a single crate).
Expand Down
Loading
Loading