11use apollo_gateway_config:: config:: ProofArchiveWriterConfig ;
22use async_trait:: async_trait;
3+ use google_cloud_storage:: client:: { Client , ClientConfig } ;
4+ use google_cloud_storage:: http:: objects:: upload:: { Media , UploadObjectRequest , UploadType } ;
35#[ cfg( any( feature = "testing" , test) ) ]
46use mockall:: automock;
57use starknet_api:: transaction:: fields:: { Proof , ProofFacts } ;
@@ -22,27 +24,61 @@ pub enum ProofArchiveError {
2224 WriteError ( String ) ,
2325}
2426
25- #[ derive( Clone , Default ) ]
27+ #[ derive( Clone ) ]
2628pub struct GcsProofArchiveWriter {
27- // TODO(Einat): remove #[allow(dead_code)] once implemented.
28- #[ allow( dead_code) ]
2929 config : ProofArchiveWriterConfig ,
30+ client : Client ,
3031}
3132
3233impl GcsProofArchiveWriter {
3334 pub fn new ( config : ProofArchiveWriterConfig ) -> Self {
34- Self { config }
35+ let client = Client :: new ( ClientConfig :: default ( ) ) ;
36+ Self { config, client }
3537 }
3638}
3739
3840#[ async_trait]
3941impl ProofArchiveWriterTrait for GcsProofArchiveWriter {
42+ async fn set_proof (
43+ & self ,
44+ proof_facts : ProofFacts ,
45+ proof : Proof ,
46+ ) -> Result < ( ) , ProofArchiveError > {
47+ let facts_hash = proof_facts. hash ( ) ;
48+ let proof_bytes: Vec < u8 > = proof. 0 . iter ( ) . flat_map ( |& val| val. to_be_bytes ( ) ) . collect ( ) ;
49+ let object_name = format ! ( "proofs/{}" , facts_hash) ;
50+
51+ self . client
52+ . upload_object (
53+ & UploadObjectRequest {
54+ bucket : self . config . bucket_name . clone ( ) ,
55+ ..Default :: default ( )
56+ } ,
57+ proof_bytes,
58+ & UploadType :: Simple ( Media :: new ( object_name) ) ,
59+ )
60+ . await
61+ . map_err ( |e| {
62+ ProofArchiveError :: WriteError ( format ! ( "Failed to upload to GCS: {}" , e) )
63+ } ) ?;
64+
65+ Ok ( ( ) )
66+ }
67+ }
68+
69+ /// No-op proof archive writer that does nothing.
70+ /// Used in tests and when proof archiving is disabled.
71+ #[ derive( Clone , Default ) ]
72+ pub struct NoOpProofArchiveWriter ;
73+
74+ #[ async_trait]
75+ impl ProofArchiveWriterTrait for NoOpProofArchiveWriter {
4076 async fn set_proof (
4177 & self ,
4278 _proof_facts : ProofFacts ,
4379 _proof : Proof ,
4480 ) -> Result < ( ) , ProofArchiveError > {
45- // TODO(Einat): Write proof to GCS .
81+ // No-op: do nothing in test environments .
4682 Ok ( ( ) )
4783 }
4884}
0 commit comments