Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
feat(api)!: Allowed to return None (#315)
Browse files Browse the repository at this point in the history
* feat(api): Allow returns None

* add e2e test

* test: update testcase with new sdk

* fix

Co-authored-by: homura <[email protected]>
  • Loading branch information
yejiayu and homura authored Jul 11, 2020
1 parent f946c19 commit b69d1eb
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 177 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
# dev
devtools/chain/data

tests/e2e/node_modules/
tests/e2e/node_modules
tests/e2e/yarn-error.log

# rocksdb
**/rocksdb/
Expand Down
59 changes: 30 additions & 29 deletions core/api/src/adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,46 +86,44 @@ impl<
&self,
ctx: Context,
height: Option<u64>,
) -> ProtocolResult<Block> {
let block = match height {
Some(id) => self
.storage
.get_block(ctx.clone(), id)
.await?
.ok_or_else(|| APIError::NotFound)?,
None => self.storage.get_latest_block(ctx).await?,
};

Ok(block)
) -> ProtocolResult<Option<Block>> {
match height {
Some(id) => self.storage.get_block(ctx.clone(), id).await,
None => Ok(Some(self.storage.get_latest_block(ctx).await?)),
}
}

async fn get_receipt_by_tx_hash(&self, ctx: Context, tx_hash: Hash) -> ProtocolResult<Receipt> {
let receipt = self
async fn get_receipt_by_tx_hash(
&self,
ctx: Context,
tx_hash: Hash,
) -> ProtocolResult<Option<Receipt>> {
let opt_receipt = self
.storage
.get_receipt_by_hash(ctx.clone(), tx_hash)
.await?
.ok_or_else(|| APIError::NotFound)?;
.await?;

let exec_height = self.storage.get_latest_block(ctx).await?.header.exec_height;
let height = receipt.height;
if exec_height >= height {
return Ok(receipt);
}
Err(APIError::UnExecedError {
real: exec_height,
expect: height,

match opt_receipt {
Some(receipt) => {
let height = receipt.height;
if exec_height >= height {
Ok(Some(receipt))
} else {
Ok(None)
}
}
None => Ok(None),
}
.into())
}

async fn get_transaction_by_hash(
&self,
ctx: Context,
tx_hash: Hash,
) -> ProtocolResult<SignedTransaction> {
self.storage
.get_transaction_by_hash(ctx, tx_hash)
.await?
.ok_or_else(|| APIError::NotFound.into())
) -> ProtocolResult<Option<SignedTransaction>> {
self.storage.get_transaction_by_hash(ctx, tx_hash).await
}

async fn query_service(
Expand All @@ -139,7 +137,10 @@ impl<
method: String,
payload: String,
) -> ProtocolResult<ServiceResponse<String>> {
let block = self.get_block_by_height(ctx.clone(), Some(height)).await?;
let block = self
.get_block_by_height(ctx.clone(), Some(height))
.await?
.ok_or_else(|| APIError::NotFound)?;

let executor = EF::from_root(
block.header.state_root.clone(),
Expand Down
24 changes: 14 additions & 10 deletions core/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct Query;
#[juniper::graphql_object(Context = State)]
impl Query {
#[graphql(name = "getBlock", description = "Get the block")]
async fn get_block(state_ctx: &State, height: Option<Uint64>) -> FieldResult<Block> {
async fn get_block(state_ctx: &State, height: Option<Uint64>) -> FieldResult<Option<Block>> {
let ctx = Context::new();
let inst = Instant::now();
common_apm::metrics::api::API_REQUEST_COUNTER_VEC_STATIC
Expand All @@ -68,12 +68,12 @@ impl Query {
None => None,
};

let block = match state_ctx
let opt_block = match state_ctx
.adapter
.get_block_by_height(ctx.clone(), height)
.await
{
Ok(block) => block,
Ok(opt_block) => opt_block,
Err(err) => {
common_apm::metrics::api::API_REQUEST_RESULT_COUNTER_VEC_STATIC
.get_block
Expand All @@ -92,38 +92,41 @@ impl Query {
.get_block
.observe(common_apm::metrics::duration_to_sec(inst.elapsed()));

Ok(Block::from(block))
Ok(opt_block.map(Block::from))
}

#[graphql(name = "getTransaction", description = "Get the transaction by hash")]
async fn get_transaction(state_ctx: &State, tx_hash: Hash) -> FieldResult<SignedTransaction> {
async fn get_transaction(
state_ctx: &State,
tx_hash: Hash,
) -> FieldResult<Option<SignedTransaction>> {
let ctx = Context::new();

let hash = protocol::types::Hash::from_hex(&tx_hash.as_hex())?;

let stx = state_ctx
let opt_stx = state_ctx
.adapter
.get_transaction_by_hash(ctx.clone(), hash)
.await?;

Ok(SignedTransaction::from(stx))
Ok(opt_stx.map(SignedTransaction::from))
}

#[graphql(
name = "getReceipt",
description = "Get the receipt by transaction hash"
)]
async fn get_receipt(state_ctx: &State, tx_hash: Hash) -> FieldResult<Receipt> {
async fn get_receipt(state_ctx: &State, tx_hash: Hash) -> FieldResult<Option<Receipt>> {
let ctx = Context::new();

let hash = protocol::types::Hash::from_hex(&tx_hash.as_hex())?;

let receipt = state_ctx
let opt_receipt = state_ctx
.adapter
.get_receipt_by_tx_hash(ctx.clone(), hash)
.await?;

Ok(Receipt::from(receipt))
Ok(opt_receipt.map(Receipt::from))
}

#[graphql(name = "queryService", description = "query service")]
Expand All @@ -143,6 +146,7 @@ impl Query {
Some(id) => id.try_into_u64()?,
None => {
block_on(state_ctx.adapter.get_block_by_height(Context::new(), None))?
.expect("Always not none")
.header
.height
}
Expand Down
15 changes: 11 additions & 4 deletions protocol/src/traits/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,23 @@ pub trait APIAdapter: Send + Sync {
signed_tx: SignedTransaction,
) -> ProtocolResult<()>;

async fn get_block_by_height(&self, ctx: Context, height: Option<u64>)
-> ProtocolResult<Block>;
async fn get_block_by_height(
&self,
ctx: Context,
height: Option<u64>,
) -> ProtocolResult<Option<Block>>;

async fn get_receipt_by_tx_hash(&self, ctx: Context, tx_hash: Hash) -> ProtocolResult<Receipt>;
async fn get_receipt_by_tx_hash(
&self,
ctx: Context,
tx_hash: Hash,
) -> ProtocolResult<Option<Receipt>>;

async fn get_transaction_by_hash(
&self,
ctx: Context,
tx_hash: Hash,
) -> ProtocolResult<SignedTransaction>;
) -> ProtocolResult<Option<SignedTransaction>>;

async fn query_service(
&self,
Expand Down
39 changes: 0 additions & 39 deletions tests/e2e/docker-compose-e2e-test.yaml

This file was deleted.

15 changes: 9 additions & 6 deletions tests/e2e/multisig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address } from '@mutajs/types';
import { createBindingClass, write, Write } from '@mutajs/service';
import { Address } from '@mutadev/types';
import { createServiceBindingClass, write, Write } from '@mutadev/service';

type u8 = number;
type u32 = number;
Expand All @@ -21,9 +21,12 @@ interface GenerateMultiSigAccountResponse {
}

export interface MultiSigServiceModel {
generate_account: Write<GenerateMultiSigAccountPayload, GenerateMultiSigAccountResponse>;
generate_account: Write<GenerateMultiSigAccountPayload, GenerateMultiSigAccountResponse>;
}

export const MultiSigService = createBindingClass<MultiSigServiceModel>('multi_signature', {
generate_account: write(),
});
export const MultiSigService = createServiceBindingClass({
serviceName: 'multi_signature',
write: {
generate_account: write<GenerateMultiSigAccountPayload, GenerateMultiSigAccountResponse>(),
}
})
10 changes: 5 additions & 5 deletions tests/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
"prettier": "prettier --write **/*.{js,ts,graphql}"
},
"dependencies": {
"@mutajs/account": "^0.11.2-dev.0",
"@mutajs/client": "^0.11.2-dev.0",
"@mutajs/service": "^0.11.2-dev.0",
"@mutajs/utils": "^0.11.2-dev.0",
"@mutadev/account": "0.2.0-dev+pr315.0",
"@mutadev/client": "0.2.0-dev+pr315.0",
"@mutadev/muta-sdk": "0.2.0-dev+pr315.0",
"@mutadev/service": "0.2.0-dev+pr315.0",
"@mutadev/utils": "0.2.0-dev+pr315.0",
"@types/node": "^14.0.14",
"apollo-boost": "^0.4.4",
"graphql": "^15.2.0",
"graphql-tag": "^2.10.1",
"muta-sdk": "^0.11.2-dev.0",
"node-fetch": "^2.6.0",
"toml": "^3.0.0",
"ts-node": "^8.3.0",
Expand Down
Loading

0 comments on commit b69d1eb

Please sign in to comment.