@@ -3,11 +3,15 @@ mod online;
33mod reference;
44
55pub use event:: ProofOfIndexingEvent ;
6+ use graph_derive:: CheapClone ;
67pub use online:: { ProofOfIndexing , ProofOfIndexingFinisher } ;
78pub use reference:: PoICausalityRegion ;
89
910use atomic_refcell:: AtomicRefCell ;
10- use std:: sync:: Arc ;
11+ use slog:: Logger ;
12+ use std:: { ops:: Deref , sync:: Arc } ;
13+
14+ use crate :: prelude:: BlockNumber ;
1115
1216#[ derive( Copy , Clone , Debug ) ]
1317pub enum ProofOfIndexingVersion {
@@ -22,15 +26,57 @@ pub enum ProofOfIndexingVersion {
2226/// intentionally disallowed - PoI requires sequential access to the hash
2327/// function within a given causality region even if ownership is shared across
2428/// multiple mapping contexts.
25- ///
26- /// The Option<_> is because not all subgraphs support PoI until re-deployed.
27- /// Eventually this can be removed.
28- ///
29- /// This is not a great place to define this type, since the ProofOfIndexing
30- /// shouldn't "know" these details about wasmtime and subgraph re-deployments,
31- /// but the APIs that would make use of this are in graph/components so this
32- /// lives here for lack of a better choice.
33- pub type SharedProofOfIndexing = Option < Arc < AtomicRefCell < ProofOfIndexing > > > ;
29+ #[ derive( Clone , CheapClone ) ]
30+ pub struct SharedProofOfIndexing {
31+ poi : Option < Arc < AtomicRefCell < ProofOfIndexing > > > ,
32+ }
33+
34+ impl SharedProofOfIndexing {
35+ pub fn new ( block : BlockNumber , version : ProofOfIndexingVersion ) -> Self {
36+ SharedProofOfIndexing {
37+ poi : Some ( Arc :: new ( AtomicRefCell :: new ( ProofOfIndexing :: new (
38+ block, version,
39+ ) ) ) ) ,
40+ }
41+ }
42+
43+ pub fn ignored ( ) -> Self {
44+ SharedProofOfIndexing { poi : None }
45+ }
46+
47+ pub fn write_event (
48+ & self ,
49+ poi_event : & ProofOfIndexingEvent ,
50+ causality_region : & str ,
51+ logger : & Logger ,
52+ ) {
53+ if let Some ( poi) = & self . poi {
54+ let mut poi = poi. deref ( ) . borrow_mut ( ) ;
55+ poi. write ( logger, causality_region, poi_event) ;
56+ }
57+ }
58+
59+ pub fn start_handler ( & self , causality_region : & str ) {
60+ if let Some ( poi) = & self . poi {
61+ let mut poi = poi. deref ( ) . borrow_mut ( ) ;
62+ poi. start_handler ( causality_region) ;
63+ }
64+ }
65+
66+ pub fn write_deterministic_error ( & self , logger : & Logger , causality_region : & str ) {
67+ if let Some ( proof_of_indexing) = & self . poi {
68+ proof_of_indexing
69+ . deref ( )
70+ . borrow_mut ( )
71+ . write_deterministic_error ( logger, causality_region) ;
72+ }
73+ }
74+
75+ pub fn into_inner ( self ) -> Option < ProofOfIndexing > {
76+ self . poi
77+ . map ( |poi| Arc :: try_unwrap ( poi) . unwrap ( ) . into_inner ( ) )
78+ }
79+ }
3480
3581#[ cfg( test) ]
3682mod tests {
0 commit comments