Skip to content

Commit

Permalink
Enable withdrawals (#691)
Browse files Browse the repository at this point in the history
* building query

* add merkle proof

* Add root and proof set to OutputProof

* added mocks

* assert message id

* added signature and block

* finish integration test and wire up graph api

* handle results

* add comments

* move to mod

* fix bug

Co-authored-by: Brandon Vrooman <brandon.vrooman@gmail.com>
Co-authored-by: ControlCplusControlV <44706811+ControlCplusControlV@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 26, 2022
1 parent d693450 commit b4a613d
Show file tree
Hide file tree
Showing 24 changed files with 1,315 additions and 120 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.terraform
*.tfstate*
*.terraform.lock.hcl*
.vscode
1 change: 1 addition & 0 deletions Cargo.lock

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

63 changes: 60 additions & 3 deletions fuel-client/assets/schema.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ input BalanceFilterInput {

type Block {
id: BlockId!
height: U64!
header: Header!
transactions: [Transaction!]!
time: DateTime!
}

type BlockConnection {
Expand Down Expand Up @@ -268,6 +267,49 @@ type FailureStatus {
}


type Header {
"""
Hash of the header
"""
id: BlockId!
"""
The layer 1 height of messages and events to include since the last layer 1 block number.
"""
daHeight: U64!
"""
Number of transactions in this block.
"""
transactionsCount: U64!
"""
Number of output messages in this block.
"""
outputMessagesCount: U64!
"""
Merkle root of transactions.
"""
transactionsRoot: Bytes32!
"""
Merkle root of messages in this block.
"""
outputMessagesRoot: Bytes32!
"""
Fuel block height.
"""
height: U64!
"""
Merkle root of all previous block header hashes.
"""
prevRoot: Bytes32!
"""
The block producer time.
"""
time: DateTime!
"""
Hash of the application header.
"""
applicationHash: Bytes32!
}

scalar HexString


Expand Down Expand Up @@ -312,7 +354,7 @@ type Message {
sender: Address!
recipient: Address!
nonce: U64!
data: [Int!]!
data: HexString!
daHeight: U64!
fuelBlockSpend: U64
}
Expand Down Expand Up @@ -353,6 +395,18 @@ type MessageOutput {
amount: U64!
}

type MessageProof {
proofSet: [Bytes32!]!
proofIndex: U64!
sender: Address!
recipient: Address!
nonce: Bytes32!
amount: U64!
data: HexString!
signature: Signature!
header: Header!
}

type Mutation {
startSession: ID!
endSession(id: ID!): Boolean!
Expand Down Expand Up @@ -442,6 +496,7 @@ type Query {
contractBalances(filter: ContractBalanceFilterInput!, first: Int, after: String, last: Int, before: String): ContractBalanceConnection!
nodeInfo: NodeInfo!
messages(owner: Address, first: Int, after: String, last: Int, before: String): MessageConnection!
messageProof(transactionId: TransactionId!, messageId: MessageId!): MessageProof
"""
For each `query_per_asset`, get some spendable resources(of asset specified by the query) owned by
`owner` that add up at least the query amount. The returned resources are actual resources
Expand Down Expand Up @@ -526,6 +581,8 @@ enum RunState {

scalar Salt

scalar Signature

input SpendQueryElementInput {
"""
Identifier of the asset to spend.
Expand Down
23 changes: 22 additions & 1 deletion fuel-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ pub use schema::{
PaginationRequest,
};

use self::schema::block::ProduceBlockArgs;
use self::schema::{
block::ProduceBlockArgs,
message::MessageProofArgs,
};

pub mod schema;
pub mod types;
Expand Down Expand Up @@ -582,6 +585,24 @@ impl FuelClient {

Ok(messages)
}

/// Request a merkle proof of an output message.
pub async fn message_proof(
&self,
transaction_id: &str,
message_id: &str,
) -> io::Result<Option<schema::message::MessageProof>> {
let transaction_id: schema::TransactionId = transaction_id.parse()?;
let message_id: schema::MessageId = message_id.parse()?;
let query = schema::message::MessageProofQuery::build(&MessageProofArgs {
transaction_id,
message_id,
});

let proof = self.query(query).await?.message_proof;

Ok(proof)
}
}

#[cfg(any(test, feature = "test-helpers"))]
Expand Down
23 changes: 20 additions & 3 deletions fuel-client/src/client/schema/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use crate::client::{
PaginatedResult,
};

use super::tx::TransactionIdFragment;
use super::{
tx::TransactionIdFragment,
Bytes32,
};

#[derive(cynic::FragmentArguments, Debug)]
pub struct BlockByIdArgs {
Expand Down Expand Up @@ -67,9 +70,8 @@ pub struct BlockEdge {
#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct Block {
pub height: U64,
pub id: BlockId,
pub time: DateTime,
pub header: Header,
pub transactions: Vec<TransactionIdFragment>,
}

Expand All @@ -95,6 +97,21 @@ pub struct BlockMutation {
pub produce_blocks: U64,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct Header {
pub id: BlockId,
pub da_height: U64,
pub transactions_count: U64,
pub output_messages_count: U64,
pub transactions_root: Bytes32,
pub output_messages_root: Bytes32,
pub height: U64,
pub prev_root: Bytes32,
pub time: DateTime,
pub application_hash: Bytes32,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
49 changes: 48 additions & 1 deletion fuel-client/src/client/schema/message.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use super::{
block::Header,
Bytes32,
HexString,
PageDirection,
PageInfo,
PaginatedResult,
PaginationRequest,
Signature,
TransactionId,
};
use crate::client::schema::{
schema,
Expand All @@ -19,7 +24,7 @@ pub struct Message {
pub sender: Address,
pub recipient: Address,
pub nonce: U64,
pub data: Vec<i32>,
pub data: HexString,
pub da_height: U64,
pub fuel_block_spend: Option<U64>,
}
Expand Down Expand Up @@ -64,6 +69,48 @@ pub struct OwnedMessagesConnectionArgs {
pub last: Option<i32>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(
schema_path = "./assets/schema.sdl",
graphql_type = "Query",
argument_struct = "MessageProofArgs"
)]
pub struct MessageProofQuery {
#[arguments(transaction_id = &args.transaction_id, message_id = &args.message_id)]
pub message_proof: Option<MessageProof>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct MessageProof {
/// The proof set of the message proof.
pub proof_set: Vec<Bytes32>,
/// The index that was used to produce this proof.
pub proof_index: U64,
/// The signature of the fuel block.
pub signature: Signature,
/// The fuel block that contains the message.
pub header: Header,
/// The messages sender address.
pub sender: Address,
/// The messages recipient address.
pub recipient: Address,
/// The nonce from the message.
pub nonce: Bytes32,
/// The amount from the message.
pub amount: U64,
/// The data from the message.
pub data: HexString,
}

#[derive(cynic::FragmentArguments, Debug)]
pub struct MessageProofArgs {
/// Transaction id that contains the output message.
pub transaction_id: TransactionId,
/// Message id of the output message that requires a proof.
pub message_id: MessageId,
}

impl From<(Option<Address>, PaginationRequest<String>)> for OwnedMessagesConnectionArgs {
fn from(r: (Option<Address>, PaginationRequest<String>)) -> Self {
match r.1.direction {
Expand Down
18 changes: 18 additions & 0 deletions fuel-client/src/client/schema/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::{
Formatter,
LowerHex,
},
ops::Deref,
str::FromStr,
};

Expand Down Expand Up @@ -110,6 +111,7 @@ fuel_type_scalar!(ContractId, ContractId);
fuel_type_scalar!(Salt, Salt);
fuel_type_scalar!(TransactionId, Bytes32);
fuel_type_scalar!(MessageId, MessageId);
fuel_type_scalar!(Signature, Bytes64);

impl LowerHex for MessageId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -174,6 +176,14 @@ impl From<HexString> for Vec<u8> {
}
}

impl Deref for HexString {
type Target = Bytes;

fn deref(&self) -> &Self::Target {
&self.0
}
}

#[derive(Debug, Clone)]
pub struct Bytes(pub Vec<u8>);

Expand Down Expand Up @@ -214,6 +224,14 @@ impl Display for Bytes {
}
}

impl Deref for Bytes {
type Target = [u8];

fn deref(&self) -> &Self::Target {
&self.0
}
}

#[derive(
Debug, Clone, derive_more::Into, derive_more::From, PartialOrd, Eq, PartialEq,
)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
---
source: fuel-client/src/client/schema/block.rs
assertion_line: 108
expression: operation.query
---
query Query($_0: BlockId) {
block(id: $_0) {
height
id
time
header {
id
daHeight
transactionsCount
outputMessagesCount
transactionsRoot
outputMessagesRoot
height
prevRoot
time
applicationHash
}
transactions {
id
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
---
source: fuel-client/src/client/schema/block.rs
assertion_line: 129
expression: operation.query
---
query Query($_0: Int, $_1: String, $_2: Int, $_3: String) {
blocks(first: $_0, after: $_1, last: $_2, before: $_3) {
edges {
cursor
node {
height
id
time
header {
id
daHeight
transactionsCount
outputMessagesCount
transactionsRoot
outputMessagesRoot
height
prevRoot
time
applicationHash
}
transactions {
id
}
Expand Down
Loading

0 comments on commit b4a613d

Please sign in to comment.