Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ra0x3 committed Dec 1, 2023
1 parent 769096e commit c72b68e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 90 deletions.
2 changes: 1 addition & 1 deletion packages/fuel-indexer-lib/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ lazy_static! {
pub static ref INTERNAL_INDEXER_ENTITIES: HashSet<&'static str> = HashSet::from([
"IndexMetadataEntity",
"IndexerPredicateEntity",
"PredicateCoinOutputEntity",
]);

/// Set of types that implement `AsRef<[u8]>`.
Expand Down Expand Up @@ -180,7 +181,6 @@ lazy_static! {
"Witnesses",
]);


/// ABI types not allowed in the contract ABI.
pub static ref UNSUPPORTED_ABI_JSON_TYPES: HashSet<&'static str> = HashSet::from(["Vec"]);

Expand Down
8 changes: 2 additions & 6 deletions packages/fuel-indexer-macros/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ pub fn decode_snippet(
let ty_id = typ.type_id;

if is_fuel_primitive(typ) {
// Since predicates are contained in the container `Predicates`, their decoding works a bit
// differently than the typical Fuel primitive.
if is_predicate_primitive(typ) {
quote! {
#ty_id => {
Expand Down Expand Up @@ -1267,9 +1265,7 @@ pub fn configurable_fn_type_name(configurable: &Configurable) -> Option<String>
}

/// Derive a mapping of input type IDs to their corresponding names for a given set of predicates.
pub fn predicate_predicate_inputs_names_map(
manifest: &Manifest,
) -> HashMap<usize, String> {
pub fn predicate_inputs_names_map(manifest: &Manifest) -> HashMap<usize, String> {
let mut output = HashMap::new();
if let Some(predicate) = manifest.predicates() {
if predicate.is_empty() {
Expand Down Expand Up @@ -1301,6 +1297,6 @@ pub fn predicate_inputs_name(template_name: &str) -> String {
}

/// Derive the name of the set of configurables for this predicate template.
pub fn sdk_configurables_name(template_name: &str) -> String {
pub fn configurables_name(template_name: &str) -> String {
format!("{}Configurables", to_pascal_case(template_name))
}
65 changes: 7 additions & 58 deletions packages/fuel-indexer-macros/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,32 +197,7 @@ fn process_fn_items(
})
.collect::<Vec<TypeDeclaration>>();

let configurable_abi_decoders = configurable_abi_types
.iter()
.filter_map(|typ| {
if is_non_decodable_type(typ) {
return None;
}

// NOTE: Not sure if we should handle generics and cache type IDs here (i.e., same
// as we do for standard contract TypeDeclarations).

let type_tokens = typ.rust_tokens();
let name = typ.decoder_field_ident();
let ty_id = typ.type_id;

// NOTE: This decoding is used to derive the indexer configurable so that we can push the configurable
// to the `Decoder` (as we do for all other types passed to indexer handlers).
Some(quote! {
#ty_id => {
let obj = #type_tokens::new(data);
self.#name.push(obj);
}
})
})
.collect::<Vec<proc_macro2::TokenStream>>();

let conract_abi_decoders = contract_abi_types
let contract_abi_decoders = contract_abi_types
.iter()
.filter_map(|typ| {
if is_non_decodable_type(typ) {
Expand Down Expand Up @@ -312,31 +287,7 @@ fn process_fn_items(
})
.collect::<Vec<proc_macro2::TokenStream>>();

let decoders = [
fuel_type_decoders,
conract_abi_decoders,
configurable_abi_decoders,
]
.concat();

let configurable_struct_fields = configurable_abi_types
.iter()
.filter_map(|typ| {
if is_non_decodable_type(typ) {
return None;
}

// NOTE: Not sure if we should handle generics and cache type IDs here (i.e., same
// as we do for standard contract TypeDeclarations).

let name = typ.decoder_field_ident();
let ty = typ.rust_tokens();

Some(quote! {
#name: Vec<#ty>
})
})
.collect::<Vec<proc_macro2::TokenStream>>();
let decoders = [fuel_type_decoders, contract_abi_decoders].concat();

let contract_struct_fields = contract_abi_types
.iter()
Expand Down Expand Up @@ -427,8 +378,6 @@ fn process_fn_items(
contract_abi_type_ids.insert(ty.to_string(), typ.type_id);
decoded_type_snippets.insert(typ.type_id);

// Since predicates are contained in the container `Predicates`, their decoding works a bit
// differently than the typical Fuel primitive.
if typ.type_id == Predicates::type_id() {
Some(quote! {
#name: Predicates
Expand All @@ -449,12 +398,13 @@ fn process_fn_items(
t.iter()
.map(|t| {
let name = predicate_inputs_name(&t.name());
let name = format_ident! { "{}", name };
let ident = format_ident! { "{}", name };
let ty = quote! { #ident };
let ident =
format_ident! { "{}_decoded", name.to_lowercase() };
let ty = format_ident! { "{}", name };
let ty = quote! { #ty };

quote! {
#name: Vec<#ty>
#ident: Vec<#ty>
}
})
.collect::<Vec<proc_macro2::TokenStream>>()
Expand All @@ -466,7 +416,6 @@ fn process_fn_items(
let decoder_fields = [
contract_struct_fields,
fuel_struct_fields,
configurable_struct_fields,
predicate_inputs_fields,
]
.concat();
Expand Down
41 changes: 16 additions & 25 deletions packages/fuel-indexer-macros/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ pub fn process_transaction_tokens(
}
});


decoder.decode_messagedata(type_id, data.clone())?;

let ty_id = MessageOut::type_id();
Expand Down Expand Up @@ -341,7 +340,8 @@ pub fn process_transaction_tokens(
}
}

/// `TokenStream` used to determine which decoder function on `Decoders` to call
/// `TokenStream` used to determine which decoder function on `Decoders` to trigger
/// for a given predicate and its associated `predicate_data` inputs.
fn decode_inputs_tokens(manifest: &Manifest) -> Vec<TokenStream> {
let mut inputs_decoder_match = Vec::new();
manifest.predicates().map(|p| {
Expand All @@ -350,8 +350,7 @@ fn decode_inputs_tokens(manifest: &Manifest) -> Vec<TokenStream> {
let name = predicate_inputs_name(&t.name());
let template_id = t.id().to_string();
let ident = format_ident! { "{}", name };
let name = name.to_lowercase();
let fn_name = format_ident! { "decode_{}", name };
let fn_name = format_ident! { "decode_{}", name.to_lowercase() };

inputs_decoder_match.push(quote! {
#template_id => {
Expand All @@ -370,7 +369,7 @@ fn decode_inputs_tokens(manifest: &Manifest) -> Vec<TokenStream> {
///
/// If predicates are not enabled, the AST will not be augmented with these tokens.
pub fn transaction_predicate_tokens(manifest: &Manifest) -> TokenStream {
let configurables_match = configurables_match_tokens(manifest);
let configurables_match = inputs_match_tokens(manifest);
let verification_tokens = predicate_verification_tokens(manifest);
let decode_inputs_tokens = decode_inputs_tokens(manifest);

Expand Down Expand Up @@ -426,7 +425,6 @@ pub fn transaction_predicate_tokens(manifest: &Manifest) -> TokenStream {
})
.collect::<Vec<_>>();


submitted_predicates.iter().for_each(|indexer_predicate| {
let template_id = indexer_predicate.template_id().to_string();
let configurables = indexer_predicate.configurables().to_owned();
Expand Down Expand Up @@ -454,14 +452,13 @@ pub fn transaction_predicate_tokens(manifest: &Manifest) -> TokenStream {
..
} = coin;


let utxos = PredicateCoinOutputEntity::find_many(PredicateCoinOutputEntity::owner().eq(owner.to_owned()));
utxos.iter().for_each(|utxo| {
let predicate = IndexerPredicateEntity::find(IndexerPredicateEntity::coin_output().eq(utxo.id.clone()))
let predicate_entity = IndexerPredicateEntity::find(IndexerPredicateEntity::coin_output().eq(utxo.id.clone()))
.expect("Could not find predicate entity for coin output.");
let predicate = IndexerPredicate::from(predicate);
let template_id = predicate.template_id().to_string();
let data = bincode::serialize(&predicate).expect("Could not serialize predicate.");
let indexer_predicate = IndexerPredicate::from(predicate_entity);
let template_id = indexer_predicate.template_id().to_string();
let data = bincode::serialize(&indexer_predicate).expect("Could not serialize predicate.");
decoder.decode_predicate(data, template_id.as_str()).expect("Could not decode predicate.");

match template_id.as_str() {
Expand All @@ -474,20 +471,21 @@ pub fn transaction_predicate_tokens(manifest: &Manifest) -> TokenStream {
});
}
_ => {
debug!("Input type ignored for predicates.");
debug!("Unsupported predicate input type.");
}
}
});
}
_ => {
debug!("Transaction type ignored for predicates.");
debug!("Unsupported predicate transaction type.");
}
}
}
}

/// `TokenStream` used to build the match statements to derive indexer configurables.
pub fn configurables_match_tokens(manifest: &Manifest) -> Vec<TokenStream> {
/// `TokenStream` used to build the match statement to derive the predicate inputs object. This predicates
/// input object is passed to the `fuels-rs` predicate encoder for predicate verification.
pub fn inputs_match_tokens(manifest: &Manifest) -> Vec<TokenStream> {
manifest
.predicates()
.map(|p| {
Expand All @@ -498,9 +496,6 @@ pub fn configurables_match_tokens(manifest: &Manifest) -> Vec<TokenStream> {
let template_id = t.id().to_string();
let name = predicate_inputs_name(&t.name());
let ident = format_ident! { "{}", name };

// NOTE: This decoding is used to derive the indexer configurable so that we can further use it
// before we move to the predicate verification process.
quote! {
#template_id => {
let obj = #ident::new(configurables);
Expand All @@ -524,14 +519,14 @@ pub fn predicate_verification_tokens(manifest: &Manifest) -> Vec<TokenStream> {
return output;
}

let configurable_names = predicate_predicate_inputs_names_map(manifest);
let configurable_names = predicate_inputs_names_map(manifest);

if let Some(templates) = predicates.templates() {
for template in templates {
let indexer_variant_name =
format_ident! { "{}", predicate_inputs_name(&template.name()) };
let sdk_variant_name =
format_ident! { "{}", sdk_configurables_name(&template.name()) };
format_ident! { "{}", configurables_name(&template.name()) };

let abi = get_json_abi(Some(template.abi()))
.expect("Could not derive predicate JSON ABI.");
Expand Down Expand Up @@ -620,7 +615,7 @@ pub fn predicate_inputs_tokens(manifest: &Manifest) -> TokenStream {
return output;
}

let configurable_namings = predicate_predicate_inputs_names_map(manifest);
let configurable_namings = predicate_inputs_names_map(manifest);

let mut configurable_variants = Vec::new();

Expand Down Expand Up @@ -676,7 +671,6 @@ pub fn predicate_inputs_tokens(manifest: &Manifest) -> TokenStream {
}
}).collect::<Vec<_>>();


main.inputs.iter().for_each(|input| {
let ty_id = input.type_id;
let typ = predicate_types
Expand Down Expand Up @@ -749,9 +743,6 @@ pub fn predicate_inputs_tokens(manifest: &Manifest) -> TokenStream {

/// Generate a set of tokens for converting predicate types between their ABI-like type
/// (e.g., `IndexerPredicate`) and their Entity-like type (e.g., `IndexerPredicateEntity`).
///
/// Since predicates are stored in the DB as `IndexerPredicateEntity` types, we need to convert to/from
/// these `Entity` types and vice versa.
pub fn predicate_entity_impl_tokens() -> TokenStream {
quote! {
impl From<PredicateCoinOutput> for PredicateCoinOutputEntity {
Expand Down

0 comments on commit c72b68e

Please sign in to comment.