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

Expose signature of the block, if any in GraphQL #770

Merged
merged 11 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
326 changes: 291 additions & 35 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
Expand Up @@ -19,6 +19,7 @@ members = [
"fuel-tests",
"fuel-txpool",
"xtask",
"version-compatibility",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Voxelot wrote in the slack:

Im thinking we should test if client will still work against 0.14.1. Since it's a new field it'd be nice if an older version of the server just ignored the request, but we should double check that this won't suddenly break the SDK etc

So I added tests to verify version compatibility between 0.14.1 and 0.14.2

]

[profile.release]
Expand Down
10 changes: 10 additions & 0 deletions fuel-client/assets/schema.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ input BalanceFilterInput {
type Block {
id: BlockId!
header: Header!
consensus: Consensus!
transactions: [Transaction!]!
}

Expand Down Expand Up @@ -164,6 +165,8 @@ enum CoinStatus {
SPENT
}

union Consensus = PoAConsensus

type ConsensusParameters {
contractMaxSize: U64!
maxInputs: U64!
Expand Down Expand Up @@ -462,6 +465,13 @@ type PageInfo {
endCursor: String
}

type PoAConsensus {
"""
Gets the signature of the block produced by `PoA` consensus.
"""
signature: Signature!
}

type ProgramState {
returnType: ReturnType!
data: HexString!
Expand Down
28 changes: 28 additions & 0 deletions fuel-client/src/client/schema/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::client::{
BlockId,
ConnectionArgs,
PageInfo,
Signature,
Tai64Timestamp,
U64,
},
Expand Down Expand Up @@ -72,6 +73,7 @@ pub struct BlockEdge {
pub struct Block {
pub id: BlockId,
pub header: Header,
pub consensus: Consensus,
pub transactions: Vec<TransactionIdFragment>,
}

Expand Down Expand Up @@ -120,6 +122,32 @@ pub struct Header {
pub application_hash: Bytes32,
}

#[derive(cynic::InlineFragments, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub enum Consensus {
PoAConsensus(PoAConsensus),
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct PoAConsensus {
pub signature: Signature,
}

impl Block {
/// Returns the block producer public key, if any.
pub fn block_producer(&self) -> Option<fuel_vm::fuel_crypto::PublicKey> {
ControlCplusControlV marked this conversation as resolved.
Show resolved Hide resolved
let message = self.header.id.clone().into_message();
match &self.consensus {
Consensus::PoAConsensus(poa) => {
let signature = poa.signature.clone().into_signature();
let producer_pub_key = signature.recover(&message);
producer_pub_key.ok()
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
16 changes: 16 additions & 0 deletions fuel-client/src/client/schema/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,19 @@ impl<'de> Deserialize<'de> for Tai64Timestamp {
Ok(Self(Tai64(s.parse().map_err(D::Error::custom)?)))
}
}

impl BlockId {
/// Converts the hash into a message having the same bytes.
pub fn into_message(self) -> fuel_vm::fuel_crypto::Message {
let bytes: fuel_vm::fuel_types::Bytes32 = self.into();
// This is safe because BlockId is a cryptographically secure hash.
unsafe { fuel_vm::fuel_crypto::Message::from_bytes_unchecked(bytes.into()) }
}
}

impl Signature {
pub fn into_signature(self) -> fuel_vm::fuel_crypto::Signature {
let bytes: fuel_vm::fuel_types::Bytes64 = self.into();
bytes.into()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ query Query($_0: BlockId) {
time
applicationHash
}
consensus {
__typename
... on PoAConsensus {
signature
}
}
transactions {
id
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ query Query($_0: Int, $_1: String, $_2: Int, $_3: String) {
time
applicationHash
}
consensus {
__typename
... on PoAConsensus {
signature
}
}
transactions {
id
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ query Query {
time
applicationHash
}
consensus {
__typename
... on PoAConsensus {
signature
}
}
transactions {
id
}
Expand Down
47 changes: 46 additions & 1 deletion fuel-core/src/schema/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use super::scalars::{
};
use crate::{
database::{
storage::FuelBlocks,
storage::{
FuelBlocks,
SealedBlockConsensus,
},
Database,
KvStoreError,
},
Expand All @@ -16,6 +19,7 @@ use crate::{
schema::{
scalars::{
BlockId,
Signature,
U64,
},
tx::types::Transaction,
Expand All @@ -34,6 +38,7 @@ use async_graphql::{
Context,
InputObject,
Object,
Union,
};
use fuel_core_interfaces::{
common::{
Expand All @@ -48,6 +53,7 @@ use fuel_core_interfaces::{
},
model::{
FuelApplicationHeader,
FuelBlockConsensus,
FuelBlockHeader,
FuelConsensusHeader,
PartialFuelBlock,
Expand All @@ -67,6 +73,15 @@ pub struct Block {

pub struct Header(pub(crate) FuelBlockHeader);

#[derive(Union)]
pub enum Consensus {
PoA(PoAConsensus),
}

pub struct PoAConsensus {
signature: Signature,
}

#[Object]
impl Block {
async fn id(&self) -> BlockId {
Expand All @@ -79,6 +94,18 @@ impl Block {
&self.header
}

async fn consensus(&self, ctx: &Context<'_>) -> async_graphql::Result<Consensus> {
let db = ctx.data_unchecked::<Database>().clone();
let id = self.header.0.id().into();
let consensus = db
.storage::<SealedBlockConsensus>()
.get(&id)
.map(|c| c.map(|c| c.into_owned().into()))?
.ok_or(KvStoreError::NotFound)?;

Ok(consensus)
}

async fn transactions(
&self,
ctx: &Context<'_>,
Expand Down Expand Up @@ -152,6 +179,14 @@ impl Header {
}
}

#[Object]
impl PoAConsensus {
/// Gets the signature of the block produced by `PoA` consensus.
async fn signature(&self) -> Signature {
self.signature
}
}

#[derive(Default)]
pub struct BlockQuery;

Expand Down Expand Up @@ -477,3 +512,13 @@ impl From<FuelBlockDb> for Header {
Header(block.header)
}
}

impl From<FuelBlockConsensus> for Consensus {
fn from(consensus: FuelBlockConsensus) -> Self {
match consensus {
FuelBlockConsensus::PoA(poa) => Consensus::PoA(PoAConsensus {
signature: poa.signature.into(),
}),
}
}
}
36 changes: 29 additions & 7 deletions fuel-tests/tests/blocks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use fuel_core::{
database::{
storage::FuelBlocks,
storage::{
FuelBlocks,
SealedBlockConsensus,
},
Database,
},
model::{
Expand All @@ -18,9 +21,13 @@ use fuel_core_interfaces::{
fuel_storage::StorageAsMut,
fuel_tx,
fuel_tx::UniqueIdentifier,
secrecy::ExposeSecret,
tai64::Tai64,
},
model::FuelConsensusHeader,
model::{
FuelBlockConsensus,
FuelConsensusHeader,
},
};
use fuel_gql_client::{
client::{
Expand All @@ -40,6 +47,7 @@ use itertools::{
Itertools,
};
use rstest::rstest;
use std::ops::Deref;

#[tokio::test]
async fn block() {
Expand All @@ -50,6 +58,9 @@ async fn block() {
db.storage::<FuelBlocks>()
.insert(&id.into(), &block)
.unwrap();
db.storage::<SealedBlockConsensus>()
.insert(&id.into(), &FuelBlockConsensus::PoA(Default::default()))
.unwrap();

// setup server & client
let srv = FuelService::from_database(db, Config::local_node())
Expand All @@ -74,7 +85,9 @@ async fn produce_block() {

config.manual_blocks_enabled = true;

let srv = FuelService::from_database(db, config).await.unwrap();
let srv = FuelService::from_database(db, config.clone())
.await
.unwrap();

let client = FuelClient::from(srv.bound_address);

Expand All @@ -93,17 +106,23 @@ async fn produce_block() {
if let TransactionStatus::Success { block_id, .. } =
transaction_response.unwrap().status
{
let block_height: u64 = client
let block = client
.block(block_id.to_string().as_str())
.await
.unwrap()
.unwrap();
let actual_pub_key = block.block_producer().unwrap();
let block_height: u64 = block.header.height.into();
let expected_pub_key = config
.consensus_key
.unwrap()
.header
.height
.into();
.expose_secret()
.deref()
.public_key();

// Block height is now 6 after being advance 5
assert!(6 == block_height);
assert_eq!(actual_pub_key, expected_pub_key);
} else {
panic!("Wrong tx status");
};
Expand Down Expand Up @@ -272,6 +291,9 @@ async fn block_connection_5(
db.storage::<FuelBlocks>()
.insert(&id.into(), &block)
.unwrap();
db.storage::<SealedBlockConsensus>()
.insert(&id.into(), &FuelBlockConsensus::PoA(Default::default()))
.unwrap();
}

// setup server & client
Expand Down
9 changes: 8 additions & 1 deletion fuel-tests/tests/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ use fuel_core::service::{
Config,
FuelService,
};
use fuel_gql_client::client::FuelClient;
use fuel_gql_client::{
client::FuelClient,
fuel_tx::Transaction,
};

#[tokio::test]
async fn chain_info() {
let node_config = Config::local_node();
let srv = FuelService::new_node(node_config.clone()).await.unwrap();
let client = FuelClient::from(srv.bound_address);
client
.submit_and_await_commit(&Transaction::default())
.await
.unwrap();

let chain_info = client.chain_info().await.unwrap();

Expand Down
13 changes: 13 additions & 0 deletions version-compatibility/0.14.1-0.14.2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "test_0_14_1-0_14_2"
version = "0.0.0"
edition = "2021"
license = "BUSL-1.1"
publish = false

[lib]
path = "dummy.rs"

[dev-dependencies]
fuel-core-0_14_1-client-0_14_2 = { path = "fuel-core:0.14.1-client:0.14.2" }
fuel-core-0_14_2-client-0_14_1 = { path = "fuel-core:0.14.2-client:0.14.1" }
1 change: 1 addition & 0 deletions version-compatibility/0.14.1-0.14.2/dummy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "fuel-core-0_14_1-client-0_14_2"
version = "0.0.0"
edition = "2021"
license = "BUSL-1.1"
publish = false

[lib]
path = "test.rs"

[dependencies]
fuel-core = { version = "0.14.1" }
# TODO: Use `0.14.2` when it is released
fuel-gql-client = { path = "../../../fuel-client", features = ["test-helpers"] }
tokio = { version = "1.21", features = ["macros", "rt-multi-thread"] }
Loading