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

fetch all messages and messages by owner #536

Merged
merged 23 commits into from
Aug 9, 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
41 changes: 41 additions & 0 deletions fuel-client/assets/schema.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,46 @@ type ContractOutput {
stateRoot: Bytes32!
}

type DaMessage {
amount: U64!
sender: Address!
recipient: Address!
owner: Address!
nonce: U64!
data: [Int!]!
daHeight: U64!
fuelBlockSpend: U64
}

type DaMessageConnection {
"""
Information to aid in pagination.
"""
pageInfo: PageInfo!
"""
A list of edges.
"""
edges: [DaMessageEdge!]!
"""
A list of nodes.
"""
nodes: [DaMessage!]!
}

"""
An edge in a connection.
"""
type DaMessageEdge {
"""
A cursor for use in pagination
"""
cursor: String!
"""
The item at the end of the edge
"""
node: DaMessage!
}

"""
Implement the DateTime<Utc> scalar

Expand Down Expand Up @@ -399,6 +439,7 @@ type Query {
contractBalance(contract: ContractId!, asset: AssetId!): ContractBalance!
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): DaMessageConnection!
}

type Receipt {
Expand Down
13 changes: 13 additions & 0 deletions fuel-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,19 @@ impl FuelClient {

Ok(balances)
}

pub async fn messages(
&self,
owner: Option<&str>,
request: PaginationRequest<String>,
) -> io::Result<PaginatedResult<schema::message::DaMessage, String>> {
let owner: Option<schema::Address> = owner.map(|owner| owner.parse()).transpose()?;
let query = schema::message::OwnedDaMessageQuery::build(&(owner, request).into());

let messages = self.query(query).await?.messages.into();

Ok(messages)
}
}

#[cfg(any(test, feature = "test-helpers"))]
Expand Down
3 changes: 2 additions & 1 deletion fuel-client/src/client/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod block;
pub mod chain;
pub mod coin;
pub mod contract;
pub mod message;
pub mod node_info;
pub mod primitives;
pub mod tx;
Expand Down Expand Up @@ -221,7 +222,7 @@ pub struct OutputBreakpoint {
}

/// Generic graphql pagination query args
#[derive(cynic::FragmentArguments, Debug)]
#[derive(cynic::FragmentArguments, Debug, Default)]
pub struct ConnectionArgs {
/// Skip until cursor (forward pagination)
pub after: Option<String>,
Expand Down
107 changes: 107 additions & 0 deletions fuel-client/src/client/schema/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use super::{PageDirection, PageInfo, PaginatedResult, PaginationRequest};
use crate::client::schema::{schema, Address, U64};

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct DaMessage {
pub amount: U64,
pub sender: Address,
pub recipient: Address,
pub owner: Address,
pub nonce: U64,
pub data: Vec<i32>,
pub da_height: U64,
pub fuel_block_spend: Option<U64>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(
schema_path = "./assets/schema.sdl",
graphql_type = "Query",
argument_struct = "OwnedMessagesConnectionArgs"
)]
pub struct OwnedDaMessageQuery {
#[arguments(owner = &args.owner, after = &args.after, before = &args.before, first = &args.first, last = &args.last)]
pub messages: DaMessageConnection,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct DaMessageConnection {
pub edges: Vec<DaMessageEdge>,
pub page_info: PageInfo,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct DaMessageEdge {
pub cursor: String,
pub node: DaMessage,
}

#[derive(cynic::FragmentArguments, Debug)]
pub struct OwnedMessagesConnectionArgs {
/// Filter messages based on an owner
pub owner: Option<Address>,
/// Skip until coin id (forward pagination)
pub after: Option<String>,
/// Skip until coin id (backward pagination)
pub before: Option<String>,
/// Retrieve the first n coins in order (forward pagination)
pub first: Option<i32>,
/// Retrieve the last n coins in order (backward pagination).
/// Can't be used at the same time as `first`.
pub last: Option<i32>,
}

impl From<(Option<Address>, PaginationRequest<String>)> for OwnedMessagesConnectionArgs {
fn from(r: (Option<Address>, PaginationRequest<String>)) -> Self {
match r.1.direction {
PageDirection::Forward => OwnedMessagesConnectionArgs {
owner: r.0,
after: r.1.cursor,
before: None,
first: Some(r.1.results as i32),
last: None,
},
PageDirection::Backward => OwnedMessagesConnectionArgs {
owner: r.0,
after: None,
before: r.1.cursor,
first: None,
last: Some(r.1.results as i32),
},
}
}
}

impl From<DaMessageConnection> for PaginatedResult<DaMessage, String> {
fn from(conn: DaMessageConnection) -> Self {
PaginatedResult {
cursor: conn.page_info.end_cursor,
has_next_page: conn.page_info.has_next_page,
has_previous_page: conn.page_info.has_previous_page,
results: conn.edges.into_iter().map(|e| e.node).collect(),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn owned_message_query_gql_output() {
use cynic::QueryBuilder;

let operation = OwnedDaMessageQuery::build(OwnedMessagesConnectionArgs {
owner: Some(Address::default()),
after: None,
before: None,
first: None,
last: None,
});

insta::assert_snapshot!(operation.query)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
source: fuel-client/src/client/schema/message.rs
assertion_line: 105
expression: operation.query
---
query Query($_0: Address, $_1: Int, $_2: String, $_3: Int, $_4: String) {
messages(owner: $_0, first: $_1, after: $_2, last: $_3, before: $_4) {
edges {
cursor
node {
amount
sender
recipient
owner
nonce
data
daHeight
fuelBlockSpend
}
}
pageInfo {
endCursor
hasNextPage
hasPreviousPage
startCursor
}
}
}

11 changes: 11 additions & 0 deletions fuel-core/src/database/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use fuel_core_interfaces::{
model::DaMessage,
};
use std::borrow::Cow;
use std::ops::Deref;

impl Storage<MessageId, DaMessage> for Database {
type Error = KvStoreError;
Expand Down Expand Up @@ -76,6 +77,16 @@ impl Database {
})
})
}

pub fn all_messages(
&self,
start: Option<MessageId>,
direction: Option<IterDirection>,
) -> impl Iterator<Item = Result<DaMessage, Error>> + '_ {
let start = start.map(|v| v.deref().to_vec());
self.iter_all::<Vec<u8>, DaMessage>(columns::DA_MESSAGES, None, start, direction)
.map(|res| res.map(|(_, message)| message))
}
}

/// Get a Key by chaining Owner + MessageId
Expand Down
4 changes: 3 additions & 1 deletion fuel-core/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod coin;
pub mod contract;
pub mod dap;
pub mod health;
pub mod message;
pub mod node_info;
pub mod scalars;
pub mod tx;
Expand All @@ -23,6 +24,7 @@ pub struct Query(
contract::ContractQuery,
contract::ContractBalanceQuery,
node_info::NodeQuery,
message::MessageQuery,
);

#[derive(MergedObject, Default)]
Expand All @@ -39,6 +41,6 @@ pub fn build_schema() -> SchemaBuilder<Query, Mutation, EmptySubscription> {
Query::default(),
Mutation::default(),
EmptySubscription::default(),
["TransactionConnection"],
["TransactionConnection", "DaMessageConnection"],
)
}
Loading