Skip to content

Commit

Permalink
Fix Transaction Status API & Fix Remove TX Bug (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
ControlCplusControlV authored Apr 27, 2022
1 parent 0e78e21 commit 0b78de5
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 27 deletions.
2 changes: 2 additions & 0 deletions fuel-core-interfaces/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod block;
mod block_height;
mod coin;
mod txpool;

pub use block::{FuelBlock, FuelBlockDb, FuelBlockHeader};
pub use block_height::BlockHeight;
pub use coin::{Coin, CoinStatus};
pub use txpool::{ArcTx, TxInfo};
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::ops::Deref;

use crate::types::ArcTx;
use chrono::{DateTime, Utc};
use fuel_tx::Transaction;
use std::sync::Arc;

pub type ArcTx = Arc<Transaction>;

#[derive(Debug, Clone)]
pub struct TxInfo {
Expand Down
5 changes: 3 additions & 2 deletions fuel-core-interfaces/src/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use thiserror::Error;
use crate::{
db::{Error as DbStateError, KvStoreError},
model::Coin,
model::TxInfo,
};
use fuel_storage::Storage;
use fuel_vm::prelude::Contract;
Expand Down Expand Up @@ -50,10 +51,10 @@ pub trait TxPool: Send + Sync {
-> Vec<anyhow::Result<Vec<Arc<Transaction>>>>;

/// find all tx by their hash
async fn find(&self, hashes: &[TxId]) -> Vec<Option<Arc<Transaction>>>;
async fn find(&self, hashes: &[TxId]) -> Vec<Option<TxInfo>>;

/// find one tx by its hash
async fn find_one(&self, hash: &TxId) -> Option<Arc<Transaction>>;
async fn find_one(&self, hash: &TxId) -> Option<TxInfo>;

/// find all dependent tx and return them with requsted dependencies in one list sorted by Price.
async fn find_dependent(&self, hashes: &[TxId]) -> Vec<Arc<Transaction>>;
Expand Down
2 changes: 1 addition & 1 deletion fuel-core/src/schema/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl TxQuery {
let found_tx = tx_pool.pool().find(&[key]).await;

if let Some(Some(transaction)) = found_tx.get(0) {
Ok(Some(Transaction((transaction.deref()).clone())))
Ok(Some(Transaction((transaction.tx().deref()).clone())))
} else {
Ok(Storage::<fuel_types::Bytes32, FuelTx>::get(db, &key)?
.map(|tx| Transaction(tx.into_owned())))
Expand Down
16 changes: 14 additions & 2 deletions fuel-core/src/schema/tx/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use crate::model::FuelBlockDb;
use crate::schema::contract::Contract;
use crate::schema::scalars::{AssetId, Bytes32, HexString, Salt, TransactionId, U64};
use crate::tx_pool::TransactionStatus as TxStatus;
use crate::tx_pool::TxPool;
use crate::{database::Database, schema::block::Block};
use async_graphql::{Context, Enum, Object, Union};
use chrono::{DateTime, Utc};
use fuel_core_interfaces::db::KvStoreError;
use fuel_storage::Storage;
use fuel_types::bytes::SerializableVec;
use fuel_vm::prelude::ProgramState as VmProgramState;
use std::sync::Arc;

pub struct ProgramState {
return_type: ReturnType,
Expand Down Expand Up @@ -214,8 +216,18 @@ impl Transaction {

async fn status(&self, ctx: &Context<'_>) -> async_graphql::Result<Option<TransactionStatus>> {
let db = ctx.data_unchecked::<Database>();
let status = db.get_tx_status(&self.0.id())?;
Ok(status.map(Into::into))

let txpool = ctx.data::<Arc<TxPool>>().unwrap().pool();

let transaction_in_pool = txpool.find_one(&self.0.id()).await;

if transaction_in_pool.is_some() {
let time = transaction_in_pool.unwrap().submited_time();
Ok(Some(TransactionStatus::Submitted(SubmittedStatus(time))))
} else {
let status = db.get_tx_status(&self.0.id())?;
Ok(status.map(Into::into))
}
}

async fn receipts(&self, ctx: &Context<'_>) -> async_graphql::Result<Option<Vec<Receipt>>> {
Expand Down
12 changes: 12 additions & 0 deletions fuel-tests/tests/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,25 @@ async fn submit_utxo_verified_tx() {
assert!(block_exists.is_some());
}

// Once https://github.com/FuelLabs/fuel-core/issues/50 is resolved this should rely on the Submitted Status rather than Success
assert!(matches!(
transaction_result,
TransactionStatus::Success { .. }
));
}
}

#[ignore]
#[tokio::test]
async fn transaction_status_submitted() {
// This test should ensure a transaction's status is Submitted while it is in the mempool
// This test should also ensure a transaction's time of submission is correct in the returned status
// Currently blocked until https://github.com/FuelLabs/fuel-core/issues/50 is resolved
// as execution must be seperate from submission for a tx to persist inside of the txpool
// Merge with the submit_utxo_verified_tx test once utxo_verification is the default
todo!();
}

#[tokio::test]
async fn receipts() {
let transaction = fuel_tx::Transaction::default();
Expand Down
1 change: 0 additions & 1 deletion fuel-txpool/src/containers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod dependency;
pub mod info;
pub mod price_sort;
4 changes: 1 addition & 3 deletions fuel-txpool/src/containers/dependency.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use crate::{types::*, Error};
use anyhow::anyhow;
use fuel_core_interfaces::{
model::{Coin, CoinStatus},
model::{ArcTx, Coin, CoinStatus, TxInfo},
txpool::TxPoolDb,
};
use fuel_tx::{Input, Output, UtxoId};
use std::collections::{HashMap, HashSet};

use super::info::TxInfo;

/// Check and hold dependency between inputs and outputs. Be mindful
/// about depth of connection
#[derive(Debug, Clone)]
Expand Down
1 change: 1 addition & 0 deletions fuel-txpool/src/containers/price_sort.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::types::*;
use fuel_core_interfaces::model::ArcTx;
use std::{cmp, collections::BTreeMap};

#[derive(Debug, Clone)]
Expand Down
17 changes: 6 additions & 11 deletions fuel-txpool/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::sync::Arc;

use crate::Error;
use crate::{subscribers::MultiSubscriber, types::*, Config, TxPool as TxPoolImpl};
use fuel_core_interfaces::txpool::{Subscriber, TxPool, TxPoolDb};

use async_trait::async_trait;
use fuel_core_interfaces::model::{ArcTx, TxInfo};
use fuel_core_interfaces::txpool::{Subscriber, TxPool, TxPoolDb};
use std::collections::HashMap;
use tokio::sync::RwLock;

Expand Down Expand Up @@ -56,22 +56,17 @@ impl TxPool for TxPoolService {
}

/// find all tx by its hash
async fn find(&self, hashes: &[TxId]) -> Vec<Option<ArcTx>> {
async fn find(&self, hashes: &[TxId]) -> Vec<Option<TxInfo>> {
let mut res = Vec::with_capacity(hashes.len());
let pool = self.txpool.read().await;
for hash in hashes {
res.push(pool.txs().get(hash).map(|info| info.tx().clone()));
res.push(pool.txs().get(hash).cloned());
}
res
}

async fn find_one(&self, hash: &TxId) -> Option<ArcTx> {
self.txpool
.read()
.await
.txs()
.get(hash)
.map(|info| info.tx().clone())
async fn find_one(&self, hash: &TxId) -> Option<TxInfo> {
self.txpool.read().await.txs().get(hash).cloned()
}

/// find all dependent tx and return them with requsted dependencies in one list sorted by Price.
Expand Down
3 changes: 2 additions & 1 deletion fuel-txpool/src/subscribers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::sync::Arc;

use crate::{types::ArcTx, Error};
use crate::Error;
use async_trait::async_trait;
use fuel_core_interfaces::model::ArcTx;
use fuel_core_interfaces::txpool::Subscriber;
use parking_lot::RwLock;

Expand Down
6 changes: 3 additions & 3 deletions fuel-txpool/src/txpool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::containers::dependency::Dependency;
use crate::containers::info::TxInfo;
use crate::Error;
use crate::{containers::price_sort::PriceSort, types::*, Config};
use fuel_core_interfaces::model::{ArcTx, TxInfo};
use fuel_core_interfaces::txpool::TxPoolDb;
use std::collections::HashMap;
use std::sync::Arc;
Expand Down Expand Up @@ -98,8 +98,8 @@ impl TxPool {

/// remove transaction from pool needed on user demand. Low priority
pub fn remove_by_tx_id(&mut self, tx_id: &TxId) -> Vec<ArcTx> {
if let Some(tx) = self.by_hash.get(tx_id) {
self.by_gas_price.remove(tx);
if let Some(tx) = self.by_hash.remove(tx_id) {
self.by_gas_price.remove(tx.tx());
return self
.by_dependency
.recursively_remove_all_dependencies(&self.by_hash, tx.tx().clone());
Expand Down
2 changes: 0 additions & 2 deletions fuel-txpool/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
pub use fuel_tx::ContractId;
pub use fuel_tx::{Transaction, TxId};
use fuel_types::Word;
use std::sync::Arc;

pub type ArcTx = Arc<Transaction>;
pub type GasPrice = Word;

0 comments on commit 0b78de5

Please sign in to comment.