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

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ra0x3 committed Nov 14, 2023
1 parent 7d5a917 commit 77508c4
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 114 deletions.
5 changes: 5 additions & 0 deletions packages/fuel-indexer-lib/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ impl Predicates {
pub fn templates(&self) -> Option<&[PredicateTemplate]> {
self.templates.as_deref()
}

/// Check if this predicate set is empty.
pub fn is_empty(&self) -> bool {
self.templates.is_none()
}
}

/// Represents a predicate template.
Expand Down
247 changes: 135 additions & 112 deletions packages/fuel-indexer-macros/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ fn additional_declarations() -> proc_macro2::TokenStream {
}
}

fn predicate_handler_block(
manifest: &Manifest,
block: proc_macro2::TokenStream,
) -> proc_macro2::TokenStream {
let should_run = manifest
.predicates()
.map(|p| !p.is_empty())
.unwrap_or(false);
if should_run {
quote! {
#block
}
} else {
quote! {}
}
}

fn process_fn_items(
manifest: &Manifest,
contract_abi_path: Option<String>,
Expand Down Expand Up @@ -850,134 +867,140 @@ fn process_fn_items(
}
}
};
(
quote! {
#subscribed_contract_ids

let mut process_transaction = |decoder: &mut Decoders, tx: fuel::TransactionData| -> anyhow::Result<()> {
let tx_id = tx.id;
let predicate_tokens = quote! {
match &tx.transaction {
fuel::Transaction::Script(script) => {
let fuel::Script {
inputs,
outputs,
witnesses,
..
} = script;

let signaled_predicates = witnesses
.iter()
.map(|w| PredicateWitnessData::try_from(w.to_owned()))
.filter_map(Result::ok)
.map(|data| {
Predicate::from_signaled_witness_data(
data.to_owned(),
tx_id,
outputs[data.output_index() as usize].to_owned(),
)
})
.collect::<Vec<_>>();

// Cache signaled predicates
signaled_predicates.iter().for_each(|p| {
let _p = PredicateEntity::from(p.to_owned()).get_or_create();
});

match &tx.transaction {
fuel::Transaction::Script(script) => {
let fuel::Script {
inputs,
outputs,
witnesses,
..
} = script;

let signaled_predicates = witnesses
.iter()
.map(|w| PredicateWitnessData::try_from(w.to_owned()))
.filter_map(Result::ok)
.map(|data| {
Predicate::from_signaled_witness_data(
data.to_owned(),
tx_id,
outputs[data.output_index() as usize].to_owned(),
)
})
.collect::<Vec<_>>();

// Cache signaled predicates
signaled_predicates.iter().for_each(|p| {
let _p = PredicateEntity::from(p.to_owned()).get_or_create();
});

// in order to verify predicates you need compare this input to _saved_ outputs, not outputs in
// this same transaction
let results = inputs.iter().map(|i| {
match i {
fuel::Input::Coin(coin) => {
let fuel::InputCoin {
utxo_id,
owner,
amount,
asset_id,
predicate,
predicate_data,
..
} = coin;

// This could potentially be an InputCoin with no predicate data
if predicate.is_empty() || predicate_data.is_empty() {
return None;
}
let results = inputs.iter().map(|i| {
match i {
fuel::Input::Coin(coin) => {
let fuel::InputCoin {
utxo_id,
owner,
amount,
asset_id,
predicate,
predicate_data,
..
} = coin;

// This could potentially be an InputCoin with no predicate data
if predicate.is_empty() || predicate_data.is_empty() {
return None;
}

// FIXME: hard-coding chain ID for now
let chain_id = 0;

// FIXME: After https://github.com/FuelLabs/fuel-indexer/pull/1446 is merged, use the new .find()
// functionality to look in the DB for the predicate
let pred = signaled_predicates.iter().find(|p| {
let utxo = p.coin_output();
utxo.to == *owner
});


let configurable = match pred {
Some(p) => {
let template_id = p.template_id().to_string();
let predicate_data = p.configurables().to_owned();
let configurable = match template_id.as_str() {
#(#configurables_match)*
_ => panic!("Unknown predicate template ID; check ABI to make sure that predicate IDs are correct.")
};
Some(configurable)
}
None => None,
};
// FIXME: hard-coding chain ID for now
let chain_id = 0;

/**
if let Some(configurable) = configurable {
match configurable => {
ConfigurablesContainer::TestPredicate1IndexerConfigurables { field1, field2, .. } => {
let configurables = TestPredicate1Configurables::new()
.with_STRUCT(field1)
.with_enum(field2);
// FIXME: After https://github.com/FuelLabs/fuel-indexer/pull/1446 is merged, use the new .find()
// functionality to look in the DB for the predicate
let pred = signaled_predicates.iter().find(|p| {
let utxo = p.coin_output();
utxo.to == *owner
});

let predicate_data = TestPredicate1Encoder::encode_data(field1, field2, ..);

let mut predicate: Predicate = Predicate::from_code(chain_id, predicate_code)?
.with_data(predicate_data)
.with_configurables(configurables);
let configurable = match pred {
Some(p) => {
let template_id = p.template_id().to_string();
let predicate_data = p.configurables().to_owned();
let configurable = match template_id.as_str() {
#(#configurables_match)*
_ => panic!("Unknown predicate template ID; check ABI to make sure that predicate IDs are correct.")
};

if *predicate.address() == coin_output.owner {
decoder.decode_type(predicate.type_id(), predicate);
}
}
ConfigurablesContainer::TestPredicate2IndexerConfigurables { field2, field2, .. } => {
let configurables = TestPredicate2Configurables::new()
.with_STRUCT(field1)
.with_enum(field2);
Some(configurable)
}
None => None,
};

let predicate_data = TestPredicate2Encoder::encode_data(field1, field2, .. );
/**
if let Some(configurable) = configurable {
match configurable => {
ConfigurablesContainer::TestPredicate1IndexerConfigurables { field1, field2, .. } => {
let configurables = TestPredicate1Configurables::new()
.with_STRUCT(field1)
.with_enum(field2);
let mut predicate: Predicate = Predicate::from_code(chain_id, predicate_code)?
.with_data(predicate_data)
.with_configurables(configurables);
let predicate_data = TestPredicate1Encoder::encode_data(field1, field2, ..);
if *predicate.address() == coin_output.owner {
decoder.decode_type(predicate.type_id(), predicate);
}
}
let mut predicate: Predicate = Predicate::from_code(chain_id, predicate_code)?
.with_data(predicate_data)
.with_configurables(configurables);
if *predicate.address() == coin_output.owner {
decoder.decode_type(predicate.type_id(), predicate);
}
}
*/
ConfigurablesContainer::TestPredicate2IndexerConfigurables { field2, field2, .. } => {
let configurables = TestPredicate2Configurables::new()
.with_STRUCT(field1)
.with_enum(field2);
None
}
_ => {
debug!("Input type ignored for predicates.");
None
let predicate_data = TestPredicate2Encoder::encode_data(field1, field2, .. );
let mut predicate: Predicate = Predicate::from_code(chain_id, predicate_code)?
.with_data(predicate_data)
.with_configurables(configurables);
if *predicate.address() == coin_output.owner {
decoder.decode_type(predicate.type_id(), predicate);
}
}
}
}
}).collect::<Vec<Option<bool>>>();
}
_ => {
debug!("Transaction type ignored for predicates.");
*/

None
}
_ => {
debug!("Input type ignored for predicates.");
None
}
}
}
}).collect::<Vec<Option<bool>>>();
}
_ => {
debug!("Transaction type ignored for predicates.");
}
}
};

let predicate_block = predicate_handler_block(manifest, predicate_tokens);

(
quote! {
#subscribed_contract_ids

let mut process_transaction = |decoder: &mut Decoders, tx: fuel::TransactionData| -> anyhow::Result<()> {
let tx_id = tx.id;

#predicate_block

let mut return_types = Vec::new();
let mut callees = HashSet::new();
Expand Down
4 changes: 4 additions & 0 deletions packages/fuel-indexer-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ pub(crate) fn indexer_configurables_tokens(
};

if let Some(p) = predicates {
if p.is_empty() {
return output;
}

let predicate_types = predicate_abi
.iter()
.flat_map(|abi| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,8 @@ mod fuel_indexer_test {

#[predicate]
fn fuel_indexer_test_predicates(
predicates: PredicateIndex,
configurables: TestPredicate1InnerConfigurables,
#[allow(unused)] predicates: PredicateIndex,
#[allow(unused)] configurables: TestPredicate1InnerConfigurables,
) {
info!("fuel_indexer_test_predicates handling trigger_predicates event");
}
Expand Down

0 comments on commit 77508c4

Please sign in to comment.