@@ -15,28 +15,22 @@ use self::{
15
15
TransactionCount , TransactionInputCount , TransactionOutputCount , Value , VoteOptionRange ,
16
16
} ,
17
17
} ;
18
- use crate :: db:: {
19
- self ,
20
- chain_storable:: { BlockId , CertificateTag } ,
21
- schema:: { BlockMeta , Txn } ,
22
- ExplorerDb ,
23
- } ;
24
- use async_graphql:: {
25
- connection:: { query, Connection , Edge , EmptyFields } ,
26
- FieldError ,
27
- } ;
18
+ use async_graphql:: connection:: { query, Connection , Edge , EmptyFields } ;
28
19
use async_graphql:: {
29
20
Context , EmptyMutation , FieldResult , Object , SimpleObject , Subscription , Union ,
30
21
} ;
31
- use chain_impl_mockchain:: certificate;
32
- use chain_impl_mockchain:: { block:: HeaderId as HeaderHash , transaction} ;
22
+ use chain_explorer:: chain_storable:: { self , BlockId } ;
23
+ use chain_explorer:: schema:: { BlockMeta , Txn } ;
24
+ use chain_explorer:: ExplorerDb ;
25
+ use chain_impl_mockchain:: block:: HeaderId as HeaderHash ;
26
+ use chain_impl_mockchain:: { certificate, transaction} ;
33
27
use std:: convert:: { TryFrom , TryInto } ;
34
28
use std:: str:: FromStr ;
35
29
use std:: sync:: Arc ;
36
30
use tokio:: sync:: Mutex ;
37
31
38
32
pub struct Branch {
39
- id : db :: chain_storable:: BlockId ,
33
+ id : chain_explorer :: chain_storable:: BlockId ,
40
34
txn : Arc < Txn > ,
41
35
}
42
36
@@ -181,14 +175,14 @@ impl Branch {
181
175
}
182
176
183
177
pub struct Block {
184
- hash : db :: chain_storable:: BlockId ,
178
+ hash : chain_explorer :: chain_storable:: BlockId ,
185
179
txn : Arc < Txn > ,
186
180
block_meta : Mutex < Option < BlockMeta > > ,
187
181
}
188
182
189
183
impl Block {
190
184
async fn from_string_hash ( hash : String , txn : Arc < Txn > ) -> FieldResult < Block > {
191
- let hash: db :: chain_storable:: BlockId = HeaderHash :: from_str ( & hash) ?. into ( ) ;
185
+ let hash: chain_storable:: BlockId = HeaderHash :: from_str ( & hash) ?. into ( ) ;
192
186
193
187
if let Some ( block_meta) = Self :: try_get_block_meta ( hash. clone ( ) , & txn) . await ? {
194
188
let block = Block {
@@ -202,7 +196,7 @@ impl Block {
202
196
}
203
197
}
204
198
205
- fn from_valid_hash ( hash : db :: chain_storable:: BlockId , txn : Arc < Txn > ) -> Block {
199
+ fn from_valid_hash ( hash : chain_storable:: BlockId , txn : Arc < Txn > ) -> Block {
206
200
Block {
207
201
hash,
208
202
txn,
@@ -391,8 +385,8 @@ pub struct BlockDate {
391
385
slot : Slot ,
392
386
}
393
387
394
- impl From < db :: chain_storable:: BlockDate > for BlockDate {
395
- fn from ( date : db :: chain_storable:: BlockDate ) -> BlockDate {
388
+ impl From < chain_explorer :: chain_storable:: BlockDate > for BlockDate {
389
+ fn from ( date : chain_explorer :: chain_storable:: BlockDate ) -> BlockDate {
396
390
BlockDate {
397
391
epoch : date. epoch . get ( ) . into ( ) ,
398
392
slot : Slot ( date. slot_id . get ( ) ) ,
@@ -402,7 +396,7 @@ impl From<db::chain_storable::BlockDate> for BlockDate {
402
396
403
397
#[ derive( Clone ) ]
404
398
pub struct Transaction {
405
- id : db :: chain_storable:: FragmentId ,
399
+ id : chain_explorer :: chain_storable:: FragmentId ,
406
400
block_hashes : Vec < BlockId > ,
407
401
txn : Arc < Txn > ,
408
402
}
@@ -466,7 +460,7 @@ impl Transaction {
466
460
|h, input| {
467
461
let single_account = matches ! (
468
462
input. input_type( ) ,
469
- db :: chain_storable:: InputType :: AccountSingle
463
+ chain_explorer :: chain_storable:: InputType :: AccountSingle
470
464
) ;
471
465
472
466
Edge :: new (
@@ -598,24 +592,29 @@ impl Transaction {
598
592
. get_fragment_certificate ( & id)
599
593
. map_err ( |_| ApiError :: InternalDbError ) ?;
600
594
601
- Ok ( certificate. map ( |cert| match cert. tag {
602
- CertificateTag :: VotePlan => {
603
- certificates:: Certificate :: VotePlan ( VotePlanCertificate {
604
- txn : Arc :: clone ( & txn) ,
605
- meta : Mutex :: new ( None ) ,
606
- data : cert. clone ( ) . into_vote_plan ( ) . unwrap ( ) ,
595
+ Ok ( certificate. and_then ( |cert| {
596
+ cert. as_vote_plan ( )
597
+ . map ( |data| {
598
+ certificates:: Certificate :: VotePlan ( VotePlanCertificate {
599
+ txn : Arc :: clone ( & txn) ,
600
+ meta : Mutex :: new ( None ) ,
601
+ data : * data,
602
+ } )
607
603
} )
608
- }
609
- CertificateTag :: PublicVoteCast => {
610
- certificates:: Certificate :: PublicVoteCast ( PublicVoteCastCertificate {
611
- data : cert. clone ( ) . into_public_vote_cast ( ) . unwrap ( ) ,
604
+ . or_else ( || {
605
+ cert. as_private_vote_cast ( ) . map ( |data| {
606
+ certificates:: Certificate :: PrivateVoteCast ( PrivateVoteCastCertificate {
607
+ data : * data,
608
+ } )
609
+ } )
612
610
} )
613
- }
614
- CertificateTag :: PrivateVoteCast => {
615
- certificates:: Certificate :: PrivateVoteCast ( PrivateVoteCastCertificate {
616
- data : cert. clone ( ) . into_private_vote_cast ( ) . unwrap ( ) ,
611
+ . or_else ( || {
612
+ cert. as_public_vote_cast ( ) . map ( |data| {
613
+ certificates:: Certificate :: PublicVoteCast ( PublicVoteCastCertificate {
614
+ data : * data,
615
+ } )
616
+ } )
617
617
} )
618
- }
619
618
} ) )
620
619
} )
621
620
. await
@@ -649,7 +648,7 @@ pub struct TransactionOutput {
649
648
650
649
#[ derive( Clone ) ]
651
650
pub struct Address {
652
- id : db :: chain_storable:: Address ,
651
+ id : chain_explorer :: chain_storable:: Address ,
653
652
}
654
653
655
654
impl Address {
@@ -700,8 +699,7 @@ impl Query {
700
699
let txn = Arc :: new (
701
700
extract_context ( context)
702
701
. db
703
- . get_txn ( )
704
- . await
702
+ . txn_begin ( )
705
703
. map_err ( |_| ApiError :: InternalDbError ) ?,
706
704
) ;
707
705
@@ -713,37 +711,35 @@ impl Query {
713
711
context : & Context < ' _ > ,
714
712
length : ChainLength ,
715
713
) -> FieldResult < Vec < Block > > {
716
- let txn = Arc :: new (
717
- extract_context ( context)
718
- . db
719
- . get_txn ( )
720
- . await
721
- . map_err ( |_| ApiError :: InternalDbError ) ?,
722
- ) ;
714
+ let db = extract_context ( context) . db . clone ( ) ;
715
+ tokio:: task:: spawn_blocking ( move || {
716
+ let txn = Arc :: new ( db. txn_begin ( ) . map_err ( |_| ApiError :: InternalDbError ) ?) ;
723
717
724
- let mut result = vec ! [ ] ;
725
- let chain_length = db :: chain_storable:: ChainLength :: new ( u32:: from ( length. 0 ) ) ;
718
+ let mut result = vec ! [ ] ;
719
+ let chain_length = chain_storable:: ChainLength :: new ( u32:: from ( length. 0 ) ) ;
726
720
727
- let blocks = txn
728
- . get_blocks_by_chain_length ( & chain_length)
729
- . map_err ( |_| ApiError :: InternalDbError ) ?;
721
+ let blocks = txn
722
+ . get_blocks_by_chain_length ( & chain_length)
723
+ . map_err ( |_| ApiError :: InternalDbError ) ?;
730
724
731
- for block in blocks {
732
- result. push ( Block :: from_valid_hash ( block?. clone ( ) , Arc :: clone ( & txn) ) ) ;
733
- }
725
+ for block in blocks {
726
+ result. push ( Block :: from_valid_hash ( block?. clone ( ) , Arc :: clone ( & txn) ) ) ;
727
+ }
734
728
735
- Ok ( result)
729
+ Ok ( result)
730
+ } )
731
+ . await ?
736
732
}
737
733
738
734
async fn transaction ( & self , context : & Context < ' _ > , id : String ) -> FieldResult < Transaction > {
739
- let db = & extract_context ( context) . db ;
735
+ let db = extract_context ( context) . db . clone ( ) ;
740
736
741
737
let id = chain_impl_mockchain:: fragment:: FragmentId :: from_str ( & id) ?;
742
738
743
- let txn = db. get_txn ( ) . await . map_err ( |_| ApiError :: InternalDbError ) ?;
744
-
745
739
tokio:: task:: spawn_blocking ( move || {
746
- let id = db:: chain_storable:: FragmentId :: from ( id) ;
740
+ let txn = db. txn_begin ( ) . map_err ( |_| ApiError :: InternalDbError ) ?;
741
+
742
+ let id = chain_storable:: FragmentId :: from ( id) ;
747
743
let block_hashes = txn. transaction_blocks ( & id) ?;
748
744
if block_hashes. is_empty ( ) {
749
745
Err ( ApiError :: NotFound ( format ! ( "transaction: {}" , & id, ) ) . into ( ) )
@@ -760,11 +756,11 @@ impl Query {
760
756
761
757
/// get all current branch heads, sorted (descending) by their length
762
758
pub async fn branches ( & self , context : & Context < ' _ > ) -> FieldResult < Vec < Branch > > {
763
- let db = & extract_context ( context) . db ;
764
-
765
- let txn = Arc :: new ( db. get_txn ( ) . await . map_err ( |_| ApiError :: InternalDbError ) ?) ;
759
+ let db = extract_context ( context) . db . clone ( ) ;
766
760
767
761
tokio:: task:: spawn_blocking ( move || {
762
+ let txn = Arc :: new ( db. txn_begin ( ) . map_err ( |_| ApiError :: InternalDbError ) ?) ;
763
+
768
764
let branches = txn
769
765
. get_branches ( )
770
766
. map_err ( |_| ApiError :: InternalDbError ) ?
@@ -783,10 +779,11 @@ impl Query {
783
779
784
780
/// get the state that the ledger currently considers as the main branch
785
781
async fn tip ( & self , context : & Context < ' _ > ) -> FieldResult < Branch > {
786
- let db = & extract_context ( context) . db ;
787
- let txn = Arc :: new ( db. get_txn ( ) . await . map_err ( |_| ApiError :: InternalDbError ) ?) ;
782
+ let db = extract_context ( context) . db . clone ( ) ;
788
783
789
784
tokio:: task:: spawn_blocking ( move || {
785
+ let txn = Arc :: new ( db. txn_begin ( ) . map_err ( |_| ApiError :: InternalDbError ) ?) ;
786
+
790
787
let branch = txn
791
788
. get_tip ( )
792
789
. map_err ( |_| ApiError :: InternalDbError )
@@ -826,7 +823,7 @@ impl Subscription {
826
823
let db = db. clone ( ) ;
827
824
828
825
async move {
829
- let txn = Arc :: new ( db. get_txn ( ) . await . unwrap ( ) ) ;
826
+ let txn = Arc :: new ( db. txn_begin ( ) . unwrap ( ) ) ;
830
827
tip. ok ( ) . map ( |id| Branch { id : id. into ( ) , txn } )
831
828
}
832
829
} )
@@ -856,23 +853,23 @@ pub enum EdgeOrder {
856
853
// currently the types are complex because it has both the type bounds of db::pagination and the
857
854
// ones from graphql::pagination, and some extra ones to do conversion between them
858
855
fn get_page < ' a , N , K , C , I , Item , F , M , Output > (
859
- mut edges : db :: pagination:: SanakirjaCursorIter < ' a , K , I , C , M > ,
856
+ mut edges : chain_explorer :: pagination:: SanakirjaCursorIter < ' a , K , I , C , M > ,
860
857
edge_order : EdgeOrder ,
861
858
first : Option < usize > ,
862
859
last : Option < usize > ,
863
860
before : Option < IndexCursor > ,
864
861
after : Option < IndexCursor > ,
865
862
map_item : F ,
866
863
// TODO: extract this to an enum, or better yet, to two different functions
867
- ) -> Result < Connection < IndexCursor , Item , ConnectionFields < N > > , FieldError >
864
+ ) -> Result < Connection < IndexCursor , Item , ConnectionFields < N > > , async_graphql :: FieldError >
868
865
where
869
- C : Clone + From < N > + sanakirja:: Storable + db :: pagination:: PaginationCursor ,
866
+ C : Clone + From < N > + sanakirja:: Storable + chain_explorer :: pagination:: PaginationCursor ,
870
867
K : sanakirja:: Storable + PartialEq ,
871
868
N : From < C > + Clone + TryFrom < IndexCursor > + TryFrom < u64 > + async_graphql:: OutputType ,
872
869
I : sanakirja:: Storable ,
873
870
u64 : std:: convert:: From < N > ,
874
871
F : Fn ( C , & Output ) -> Edge < IndexCursor , Item , EmptyFields > ,
875
- M : db :: pagination:: MapEntry < ' a , K , I , C , Output = & ' a Output > ,
872
+ M : chain_explorer :: pagination:: MapEntry < ' a , K , I , C , Output = & ' a Output > ,
876
873
Output : ' a ,
877
874
{
878
875
let boundaries = edges
0 commit comments