@@ -30,11 +30,8 @@ const (
3030)
3131
3232// SnapshotUploadRequest holds the parameters for the snapshot upload task.
33- type SnapshotUploadRequest struct {
34- Bucket string `json:"bucket"`
35- Prefix string `json:"prefix"`
36- Region string `json:"region"`
37- }
33+ // S3 bucket, region, and prefix are derived from the sidecar's environment.
34+ type SnapshotUploadRequest struct {}
3835
3936// uploadState tracks the last successfully uploaded snapshot height.
4037type uploadState struct {
@@ -46,12 +43,16 @@ type uploadState struct {
4643// at the configured interval until the context is cancelled.
4744type SnapshotUploader struct {
4845 homeDir string
46+ bucket string
47+ region string
48+ chainID string
4949 uploadInterval time.Duration
5050 s3UploaderFactory seis3.UploaderFactory
5151}
5252
5353// NewSnapshotUploader creates an uploader targeting the given home directory.
54- func NewSnapshotUploader (homeDir string , uploadInterval time.Duration , factory seis3.UploaderFactory ) * SnapshotUploader {
54+ // Bucket, region, and chainID are read from environment at construction time.
55+ func NewSnapshotUploader (homeDir , bucket , region , chainID string , uploadInterval time.Duration , factory seis3.UploaderFactory ) * SnapshotUploader {
5556 if factory == nil {
5657 factory = seis3 .DefaultUploaderFactory
5758 }
@@ -60,6 +61,9 @@ func NewSnapshotUploader(homeDir string, uploadInterval time.Duration, factory s
6061 }
6162 return & SnapshotUploader {
6263 homeDir : homeDir ,
64+ bucket : bucket ,
65+ region : region ,
66+ chainID : chainID ,
6367 uploadInterval : uploadInterval ,
6468 s3UploaderFactory : factory ,
6569 }
@@ -70,21 +74,15 @@ func NewSnapshotUploader(homeDir string, uploadInterval time.Duration, factory s
7074// sleeping for the configured interval between attempts. It stays
7175// running until the context is cancelled.
7276func (u * SnapshotUploader ) Handler () engine.TaskHandler {
73- return engine .TypedHandler (func (ctx context.Context , cfg SnapshotUploadRequest ) error {
74- if cfg .Bucket == "" {
75- return fmt .Errorf ("snapshot-upload: missing required param 'bucket'" )
76- }
77- if cfg .Region == "" {
78- return fmt .Errorf ("snapshot-upload: missing required param 'region'" )
79- }
80- return u .runLoop (ctx , cfg )
77+ return engine .TypedHandler (func (ctx context.Context , _ SnapshotUploadRequest ) error {
78+ return u .runLoop (ctx )
8179 })
8280}
8381
84- func (u * SnapshotUploader ) runLoop (ctx context.Context , cfg SnapshotUploadRequest ) error {
85- uploadLog .Info ("starting snapshot upload loop" , "interval" , u .uploadInterval , "bucket" , cfg . Bucket )
82+ func (u * SnapshotUploader ) runLoop (ctx context.Context ) error {
83+ uploadLog .Info ("starting snapshot upload loop" , "interval" , u .uploadInterval , "bucket" , u . bucket )
8684 for {
87- if err := u .Upload (ctx , cfg ); err != nil {
85+ if err := u .Upload (ctx ); err != nil {
8886 uploadLog .Warn ("upload attempt failed, will retry next interval" , "error" , err )
8987 }
9088
@@ -103,7 +101,7 @@ func (u *SnapshotUploader) runLoop(ctx context.Context, cfg SnapshotUploadReques
103101//
104102// The archive is streamed through an io.Pipe so it never needs to be buffered
105103// entirely in memory; the transfermanager handles multipart upload automatically.
106- func (u * SnapshotUploader ) Upload (ctx context.Context , cfg SnapshotUploadRequest ) error {
104+ func (u * SnapshotUploader ) Upload (ctx context.Context ) error {
107105 snapshotsDir := filepath .Join (u .homeDir , "data" , "snapshots" )
108106
109107 height , err := pickUploadCandidate (snapshotsDir )
@@ -121,25 +119,25 @@ func (u *SnapshotUploader) Upload(ctx context.Context, cfg SnapshotUploadRequest
121119 return nil
122120 }
123121
124- uploadLog .Info ("uploading snapshot" , "height" , height , "bucket" , cfg . Bucket , "region" , cfg . Region )
122+ uploadLog .Info ("uploading snapshot" , "height" , height , "bucket" , u . bucket , "region" , u . region )
125123
126- uploader , err := u .s3UploaderFactory (ctx , cfg . Region )
124+ uploader , err := u .s3UploaderFactory (ctx , u . region )
127125 if err != nil {
128126 return fmt .Errorf ("building S3 uploader: %w" , err )
129127 }
130128
131- prefix := normalizePrefix ( cfg . Prefix )
129+ prefix := u . chainID + "/"
132130
133131 archiveKey := fmt .Sprintf ("%s%d.tar.gz" , prefix , height )
134132 uploadLog .Info ("streaming archive to S3" , "key" , archiveKey )
135- if err := u .streamUpload (ctx , uploader , cfg . Bucket , archiveKey , snapshotsDir , height ); err != nil {
133+ if err := u .streamUpload (ctx , uploader , u . bucket , archiveKey , snapshotsDir , height ); err != nil {
136134 return fmt .Errorf ("uploading %s: %w" , archiveKey , err )
137135 }
138136
139137 latestKey := prefix + "latest.txt"
140138 latestBody := []byte (strconv .FormatInt (height , 10 ))
141139 _ , err = uploader .UploadObject (ctx , & transfermanager.UploadObjectInput {
142- Bucket : aws .String (cfg . Bucket ),
140+ Bucket : aws .String (u . bucket ),
143141 Key : aws .String (latestKey ),
144142 Body : bytes .NewReader (latestBody ),
145143 })
0 commit comments