Skip to content

Commit 12b75f1

Browse files
authored
Merge pull request #345 from input-output-hk/lowhung/introduce-query-error-into-state-modules
feat: Integrate `QueryError` into state query responses and refactor REST to match on new NotFound enum
2 parents be4c807 + a77ac5a commit 12b75f1

File tree

35 files changed

+579
-345
lines changed

35 files changed

+579
-345
lines changed

common/src/address.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,12 @@ pub struct ShelleyAddressPointer {
148148
serde::Deserialize,
149149
minicbor::Encode,
150150
minicbor::Decode,
151+
Default,
151152
)]
152153
pub enum ShelleyAddressDelegationPart {
153154
/// No delegation (enterprise addresses)
154155
#[n(0)]
156+
#[default]
155157
None,
156158

157159
/// Delegation to stake key
@@ -167,12 +169,6 @@ pub enum ShelleyAddressDelegationPart {
167169
Pointer(#[n(0)] ShelleyAddressPointer),
168170
}
169171

170-
impl Default for ShelleyAddressDelegationPart {
171-
fn default() -> Self {
172-
Self::None
173-
}
174-
}
175-
176172
/// A Shelley-era address
177173
#[derive(
178174
Debug,
@@ -619,20 +615,15 @@ impl Default for StakeAddress {
619615
}
620616

621617
/// A Cardano address
622-
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
618+
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize, Default)]
623619
pub enum Address {
620+
#[default]
624621
None,
625622
Byron(ByronAddress),
626623
Shelley(ShelleyAddress),
627624
Stake(StakeAddress),
628625
}
629626

630-
impl Default for Address {
631-
fn default() -> Self {
632-
Self::None
633-
}
634-
}
635-
636627
impl Address {
637628
/// Get the stake pointer if there is one
638629
pub fn get_pointer(&self) -> Option<ShelleyAddressPointer> {

common/src/queries/accounts.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::HashMap;
22

3+
use crate::queries::errors::QueryError;
34
use crate::{
45
DRepChoice, PoolId, PoolLiveStakeInfo, RewardType, ShelleyAddress, StakeAddress, TxIdentifier,
56
};
@@ -80,9 +81,7 @@ pub enum AccountsStateQueryResponse {
8081
// DReps-related responses
8182
DrepDelegators(DrepDelegators),
8283
AccountsDrepDelegationsMap(HashMap<StakeAddress, Option<DRepChoice>>),
83-
84-
NotFound,
85-
Error(String),
84+
Error(QueryError),
8685
}
8786

8887
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]

common/src/queries/addresses.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::queries::errors::QueryError;
12
use crate::{Address, AddressTotals, TxIdentifier, UTxOIdentifier};
23

34
pub const DEFAULT_ADDRESS_QUERY_TOPIC: (&str, &str) =
@@ -15,6 +16,5 @@ pub enum AddressStateQueryResponse {
1516
AddressTotals(AddressTotals),
1617
AddressUTxOs(Vec<UTxOIdentifier>),
1718
AddressTransactions(Vec<TxIdentifier>),
18-
NotFound,
19-
Error(String),
19+
Error(QueryError),
2020
}

common/src/queries/assets.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::queries::errors::QueryError;
12
use crate::{
23
AssetAddressEntry, AssetInfoRecord, AssetMintRecord, AssetName, PolicyAsset, PolicyId,
34
TxIdentifier,
@@ -36,6 +37,5 @@ pub enum AssetsStateQueryResponse {
3637
AssetAddresses(AssetAddresses),
3738
AssetTransactions(AssetTransactions),
3839
PolicyIdAssets(PolicyAssets),
39-
NotFound,
40-
Error(String),
40+
Error(QueryError),
4141
}

common/src/queries/blocks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::queries::errors::QueryError;
12
use crate::{
23
queries::misc::Order,
34
serialization::{Bech32Conversion, Bech32WithHrp},
@@ -90,8 +91,7 @@ pub enum BlocksStateQueryResponse {
9091
BlockInvolvedAddresses(BlockInvolvedAddresses),
9192
BlockHashes(BlockHashes),
9293
TransactionHashes(TransactionHashes),
93-
NotFound,
94-
Error(String),
94+
Error(QueryError),
9595
}
9696

9797
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]

common/src/queries/epochs.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::queries::errors::QueryError;
12
use crate::{messages::EpochActivityMessage, protocol_params::ProtocolParams, PoolId};
23

34
pub const DEFAULT_EPOCHS_QUERY_TOPIC: (&str, &str) =
@@ -23,9 +24,7 @@ pub enum EpochsStateQueryResponse {
2324
EpochStakeDistribution(EpochStakeDistribution),
2425
EpochStakeDistributionByPool(EpochStakeDistributionByPool),
2526
LatestEpochBlocksMintedByPool(u64),
26-
27-
NotFound,
28-
Error(String),
27+
Error(QueryError),
2928
}
3029

3130
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]

common/src/queries/governance.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::HashMap;
22

3+
use crate::queries::errors::QueryError;
34
use crate::{
45
Anchor, DRepCredential, GovActionId, Lovelace, ProposalProcedure, StakeAddress, TxHash,
56
TxIdentifier, Vote, Voter, VotingProcedure,
@@ -41,8 +42,7 @@ pub enum GovernanceStateQueryResponse {
4142
ProposalWithdrawals(ProposalWithdrawals),
4243
ProposalVotes(ProposalVotes),
4344
ProposalMetadata(ProposalMetadata),
44-
NotFound,
45-
Error(String),
45+
Error(QueryError),
4646
}
4747

4848
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
@@ -124,14 +124,3 @@ pub struct ProposalVotes {
124124

125125
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
126126
pub struct ProposalMetadata {}
127-
128-
pub fn handle_governance_query_result<T>(
129-
result: anyhow::Result<Option<T>>,
130-
mapper: impl FnOnce(T) -> GovernanceStateQueryResponse,
131-
) -> GovernanceStateQueryResponse {
132-
match result {
133-
Ok(Some(val)) => mapper(val),
134-
Ok(None) => GovernanceStateQueryResponse::NotFound,
135-
Err(e) => GovernanceStateQueryResponse::Error(e.to_string()),
136-
}
137-
}

common/src/queries/mempool.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::queries::errors::QueryError;
2+
13
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
24
pub enum MempoolStateQuery {
35
GetMempoolList,
@@ -10,8 +12,7 @@ pub enum MempoolStateQueryResponse {
1012
MempoolList(MempoolList),
1113
MempoolTransaction(MempoolTransaction),
1214
MempoolTransactionByAddress(MempoolTransactionByAddress),
13-
NotFound,
14-
Error(String),
15+
Error(QueryError),
1516
}
1617

1718
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]

common/src/queries/metadata.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::queries::errors::QueryError;
2+
13
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
24
pub enum MetadataStateQuery {
35
GetMetadataLabels,
@@ -10,8 +12,7 @@ pub enum MetadataStateQueryResponse {
1012
MetadataLabels(MetadataLabels),
1113
TransactionMetadataJSON(TransactionMetadataJSON),
1214
TransactionMetadataCBOR(TransactionMetadataCBOR),
13-
NotFound,
14-
Error(String),
15+
Error(QueryError),
1516
}
1617

1718
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]

common/src/queries/network.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::queries::errors::QueryError;
2+
13
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
24
pub enum NetworkStateQuery {
35
GetNetworkInformation,
@@ -8,8 +10,7 @@ pub enum NetworkStateQuery {
810
pub enum NetworkStateQueryResponse {
911
NetworkInformation(NetworkInformation),
1012
EraSummary(EraSummary),
11-
NotFound,
12-
Error(String),
13+
Error(QueryError),
1314
}
1415

1516
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]

0 commit comments

Comments
 (0)