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

Update for ABI #90

Merged
merged 15 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1,397 changes: 1,213 additions & 184 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
# Use the new resolver to prevent dev-deps and build-deps from enabling debugging or test features in production.
resolver = "2"
members = [
"examples/simple-wasm",
"fuel-client",
Expand Down
3 changes: 2 additions & 1 deletion examples/simple-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ publish = false
crate-type = ['cdylib']

[dependencies]
fuels-core = { version = "0.1", features=["no-std"] }
fuel-indexer = { path = "../../fuel-indexer/lib" }
fuel-indexer-derive = { path = "../../fuel-indexer/derive" }
# TODO: abigen
fuels-abigen-macro = { git = "https://github.com/fuellabs/fuels-rs.git", package="fuels-abigen-macro" }
serde = { version = "1.0.128", default-features = false, features = ["derive"] }
41 changes: 41 additions & 0 deletions examples/simple-wasm/contracts/my_struct.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[
{
"type":"contract",
"inputs":[
{
"name":"SomeEvent",
"type":"struct SomeEvent",
"components": [
{
"name": "id",
"type": "u64"
},
{
"name": "account",
"type": "b256"
}
]
},
{
"name":"AnotherEvent",
"type":"struct AnotherEvent",
"components": [
{
"name": "id",
"type": "u64"
},
{
"name": "hash",
"type": "b256"
},
{
"name": "bar",
"type": "bool"
}
]
}
],
"name":"takes_struct",
"outputs":[]
}
]
16 changes: 12 additions & 4 deletions examples/simple-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#![no_std]
extern crate alloc;
use fuel_indexer_derive::{graphql_schema, handler};
mod test_data;
use test_data::*;
use fuels_abigen_macro::wasm_abigen;

graphql_schema!("test_namespace", "schema/schema.graphql");
wasm_abigen!(no_name, "examples/simple-wasm/contracts/my_struct.json");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is much cleaner than a build script. Nice!


#[handler]
fn function_one(event: SomeEvent) {
let SomeEvent { id, account } = event;

let t1 = Thing1 { id, account };
let t1 = Thing1 {
id,
account: Address::from(account),
};
t1.save();
}

Expand All @@ -19,7 +23,11 @@ fn function_two(event: AnotherEvent) {

let Thing1 { account, .. } = Thing1::load(id).expect("No object with that ID");

let t2 = Thing2 { id, account, hash };
let t2 = Thing2 {
id,
account,
hash: Bytes32::from(hash),
};

t2.save();
}
16 changes: 0 additions & 16 deletions examples/simple-wasm/src/test_data.rs

This file was deleted.

1 change: 1 addition & 0 deletions fuel-indexer/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ quote = "1.0"
syn = { version = "1.0", features = ["full"] }

[dev-dependencies]
fuels-core = { version = "0.1" }
fuel-indexer = { path = "../lib" }
serde = { version = "1.0", default-features = false, features = ["derive"] }
trybuild = "1.0"
6 changes: 5 additions & 1 deletion fuel-indexer/derive/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub fn process_handler_attr(attrs: TokenStream, item: TokenStream) -> TokenStrea
let mut block: Block = parse_quote! {
{
use fuel_indexer::types::{serialize, deserialize};
use fuels_core::abi_decoder::ABIDecoder;

let mut decoder = ABIDecoder::new();
}
};

Expand All @@ -37,7 +40,8 @@ pub fn process_handler_attr(attrs: TokenStream, item: TokenStream) -> TokenStrea

let stmts: Vec<_> = parse_quote! {
let vec = unsafe { Vec::from_raw_parts(#ptr, #len, #len) };
let #pat: #ty = deserialize(&vec);
let tokens = decoder.decode(&#ty::param_types(), &vec).expect("Type decoding failed");
let #pat = #ty::new_from_tokens(&tokens);
};

block.stmts.extend(stmts);
Expand Down
11 changes: 7 additions & 4 deletions fuel-indexer/derive/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ fn process_field<'a>(

fn process_type_def<'a>(
query_root: &str,
namespace: &str,
types: &HashSet<String>,
typ: &TypeDefinition<'a, String>,
) -> Option<proc_macro2::TokenStream> {
Expand All @@ -89,7 +90,7 @@ fn process_type_def<'a>(
return None;
}
let name = &obj.name;
let type_id = type_id(name);
let type_id = type_id(namespace, name);
// TODO: ignore directives for now, could do some useful things with them though.
let mut block = quote! {};
let mut row_extractors = quote! {};
Expand Down Expand Up @@ -152,11 +153,12 @@ fn process_type_def<'a>(

fn process_definition<'a>(
query_root: &str,
namespace: &str,
types: &HashSet<String>,
definition: &Definition<'a, String>,
) -> Option<proc_macro2::TokenStream> {
match definition {
Definition::TypeDefinition(def) => process_type_def(query_root, types, def),
Definition::TypeDefinition(def) => process_type_def(query_root, namespace, types, def),
Definition::SchemaDefinition(_def) => None,
def => {
panic!("Unhandled definition type: {:?}", def);
Expand Down Expand Up @@ -291,7 +293,6 @@ pub(crate) fn process_graphql_schema(inputs: TokenStream) -> TokenStream {
let version = const_item("VERSION", &schema_version(&text));

let mut output = quote! {
extern crate alloc;
use alloc::{vec, vec::Vec};
use fuel_indexer::Entity;
use fuel_indexer::types::*;
Expand All @@ -302,7 +303,9 @@ pub(crate) fn process_graphql_schema(inputs: TokenStream) -> TokenStream {
let query_root = get_query_root(&types, &ast);

for definition in ast.definitions.iter() {
if let Some(def) = process_definition(&query_root, &types, definition) {
if let Some(def) =
process_definition(&query_root, &schema.namespace.value(), &types, definition)
{
output = quote! {
#output
#def
Expand Down
21 changes: 18 additions & 3 deletions fuel-indexer/derive/test_data/fail_noschema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,30 @@ use core::convert::TryFrom;
use fuel_indexer_derive::{graphql_schema, handler};
use alloc::vec::Vec;
use fuel_indexer::types::*;
use serde::{Deserialize, Serialize};
use fuels_core::{ParamType, Token};

graphql_schema!("namespace", "doesnt_exist.graphql");

#[derive(Deserialize, Serialize)]
struct SomeEvent {
id: u64,
account: Address,
}

impl SomeEvent {
fn param_types() -> Vec<ParamType> {
Vec::new()
}
pub fn into_token(self) -> Token {
Token::Struct(Vec::new())
}
pub fn new_from_tokens(tokens: &[Token]) -> SomeEvent {
SomeEvent {
id: 4,
account: Address::default(),
}
}
}

#[handler]
fn function_one(event: SomeEvent) {
let SomeEvent { id, account } = event;
Expand All @@ -23,12 +37,13 @@ fn function_one(event: SomeEvent) {
}

fn main() {
use fuels_core::abi_encoder::ABIEncoder;
let s = SomeEvent {
id: 0,
account: Address::try_from([0; 32]).expect("failed"),
};

let mut bytes = serialize(&s);
let mut bytes = ABIEncoder::new().encode(&[s.into_token()]).expect("Failed compile test");

let ptr = bytes.as_mut_ptr();
let len = bytes.len();
Expand Down
37 changes: 34 additions & 3 deletions fuel-indexer/derive/test_data/success.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,43 @@ use core::convert::TryFrom;
use fuel_indexer_derive::handler;
use alloc::vec::Vec;
use fuel_indexer::types::*;
use serde::{Deserialize, Serialize};
use fuels_core::{ParamType, Token};

#[derive(Deserialize, Serialize)]
struct SomeEvent {
id: u64,
account: Address,
}

impl SomeEvent {
fn param_types() -> Vec<ParamType> {
let mut t = Vec::new();
t.push(ParamType::U64);
t.push(ParamType::B256);
t
}

fn into_token(self) -> Token {
let mut t = Vec::new();
t.push(Token::U64(self.id));
t.push(Token::B256(self.account.into()));
Token::Struct(t)
}

fn new_from_tokens(tokens: &[Token]) -> SomeEvent {
let id = match tokens[0] {
Token::U64(i) => i,
_ => panic!("Should be a U64"),
};

let addr = match tokens[1] {
Token::B256(b) => b,
_ => panic!("Should be a U256"),
};

SomeEvent { id, account: Address::from(addr) }
}
}

#[handler]
fn function_one(event: SomeEvent) {
let SomeEvent { id, account } = event;
Expand All @@ -21,12 +50,14 @@ fn function_one(event: SomeEvent) {
}

fn main() {
use fuels_core::abi_encoder::ABIEncoder;

let s = SomeEvent {
id: 0,
account: Address::try_from([0; 32]).expect("failed"),
};

let mut bytes = serialize(&s);
let mut bytes = ABIEncoder::new().encode(&[s.into_token()]).expect("Failed compile test");

let ptr = bytes.as_mut_ptr();
let len = bytes.len();
Expand Down
3 changes: 2 additions & 1 deletion fuel-indexer/indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ wasmer-engine-universal = "2.0"

[dev-dependencies]
fuel-indexer = { path = "../lib" }
fuel-tx = { version = "0.1", features = ["serde-types"] }
fuels-core = { version = "0.1" }
fuel-types = { version = "0.1", features = ["serde-types"] }
serde_yaml = "0.8.19"

[features]
Expand Down
27 changes: 20 additions & 7 deletions fuel-indexer/indexer/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,27 +202,40 @@ impl Database {
mod tests {
use super::*;
use crate::IndexEnv;
use fuel_tx::Address;
use fuel_types::Address;
use wasmer::{imports, Instance, Module, Store, WasmerEnv};
use wasmer_compiler_llvm::LLVM;
use wasmer_engine_universal::Universal;

cfg_if::cfg_if! {
if #[cfg(feature = "llvm")] {
use wasmer_compiler_llvm::LLVM;
fn compiler() -> LLVM {
LLVM::default()
}
} else {
use wasmer_compiler_cranelift::Cranelift;
fn compiler() -> Cranelift {
Cranelift::default()
}
}
}

const DATABASE_URL: &'static str = "postgres://postgres:my-secret@127.0.0.1:5432";
const GRAPHQL_SCHEMA: &'static str = include_str!("test_data/schema.graphql");
const WASM_BYTES: &'static [u8] = include_bytes!("test_data/simple_wasm.wasm");
const THING1_TYPE: u64 = 0x89F35D3CD458C71E;
const THING1_TYPE: u64 = 0xA21A262A00405632;
const TEST_COLUMNS: [(&'static str, i32, &'static str); 7] = [
("thing1", 0, "id"),
("thing1", 1, "account"),
("thing1", 2, "object"),
("thing2", 0, "id"),
("thing2", 1, "account"),
("thing2", 2, "hash"),
("thing2", 3, "object"),
("thing1", 0, "id"),
("thing1", 1, "account"),
("thing1", 2, "object"),
];

fn wasm_instance() -> IndexerResult<Instance> {
let compiler = LLVM::default();
let compiler = compiler();
let store = Store::new(&Universal::new(compiler).engine());
let module = Module::new(&store, WASM_BYTES)?;

Expand Down
Binary file modified fuel-indexer/indexer/src/test_data/simple_wasm.wasm
Binary file not shown.
Loading