44 "context"
55 "fmt"
66 "scroll-tech/common/types"
7- "scroll-tech/common/utils"
87 "scroll-tech/rollup/internal/config"
98 "scroll-tech/rollup/internal/orm"
109 "time"
@@ -23,23 +22,28 @@ type ArweaveUploader struct {
2322 txTag string
2423 confirmations int
2524 blobUploadOrm * orm.BlobUpload
25+ batchOrm * orm.Batch
26+
27+ onReuploadNeeded func (batch * orm.Batch , platform types.BlobStoragePlatform , speedFactor int64 ) (string , error )
2628}
2729
28- func NewArweaveUploader (ctx context.Context , cfg * config.ArweaveConfig , db * gorm.DB ) (* ArweaveUploader , error ) {
29- wallet , err := goar .NewWallet ([] byte ( cfg .PrivateKey ) , cfg .Endpoint )
30+ func NewArweaveUploader (ctx context.Context , cfg * config.ArweaveConfig , db * gorm.DB , onReuploadNeeded func ( batch * orm. Batch , platform types. BlobStoragePlatform , speedFactor int64 ) ( string , error ) ) (* ArweaveUploader , error ) {
31+ wallet , err := goar .NewWalletFromPath ( cfg .PrivateKeyPath , cfg .Endpoint )
3032 if err != nil {
3133 return nil , fmt .Errorf ("failed to new arweave uploader: %w" , err )
3234 }
3335
3436 client := goar .NewClient (cfg .Endpoint )
3537
3638 arweaveUploader := ArweaveUploader {
37- ctx : ctx ,
38- wallet : wallet ,
39- client : client ,
40- txTag : cfg .TxTag ,
41- confirmations : int (cfg .Confirmations ),
42- blobUploadOrm : orm .NewBlobUpload (db ),
39+ ctx : ctx ,
40+ wallet : wallet ,
41+ client : client ,
42+ txTag : cfg .TxTag ,
43+ confirmations : int (cfg .Confirmations ),
44+ blobUploadOrm : orm .NewBlobUpload (db ),
45+ batchOrm : orm .NewBatch (db ),
46+ onReuploadNeeded : onReuploadNeeded ,
4347 }
4448
4549 go arweaveUploader .loop (ctx )
@@ -48,35 +52,32 @@ func NewArweaveUploader(ctx context.Context, cfg *config.ArweaveConfig, db *gorm
4852}
4953
5054// UploadData uploads data to arweave
51- func (u * ArweaveUploader ) UploadData (ctx context.Context , data []byte , objectKey string ) (* schema.Transaction , error ) {
52- tx , err := u .wallet .SendData (
53- data ,
54- []schema.Tag {
55- schema.Tag {
56- Name : u .txTag ,
57- Value : objectKey ,
55+ func (u * ArweaveUploader ) UploadData (ctx context.Context , data []byte , objectKey string , speedFactor int64 ) (* schema.Transaction , error ) {
56+ var tx schema.Transaction
57+ var err error
58+ if speedFactor == 0 {
59+ tx , err = u .wallet .SendData (
60+ data ,
61+ []schema.Tag {
62+ schema.Tag {
63+ Name : u .txTag ,
64+ Value : objectKey ,
65+ },
5866 },
59- },
60- )
61- if err != nil {
62- return nil , err
67+ )
68+ } else {
69+ tx , err = u .wallet .SendDataSpeedUp (
70+ data ,
71+ []schema.Tag {
72+ schema.Tag {
73+ Name : u .txTag ,
74+ Value : objectKey ,
75+ },
76+ },
77+ speedFactor ,
78+ )
6379 }
6480
65- return & tx , nil
66- }
67-
68- // UploadDataSpeedUp uploads data to arweave with higher gas price
69- func (u * ArweaveUploader ) UploadDataSpeedUp (ctx context.Context , data []byte , objectKey string , speedFactor int64 ) (* schema.Transaction , error ) {
70- tx , err := u .wallet .SendDataSpeedUp (
71- data ,
72- []schema.Tag {
73- schema.Tag {
74- Name : u .txTag ,
75- Value : objectKey ,
76- },
77- },
78- speedFactor ,
79- )
8081 if err != nil {
8182 return nil , err
8283 }
@@ -99,15 +100,45 @@ func (u *ArweaveUploader) checkPendingBlobUploads(ctx context.Context) {
99100 if err == schema .ErrPendingTx {
100101 if time .Since (blobUpload .UpdatedAt ) > 10 * time .Minute {
101102 // transaction pending too long, we need to bump the gas price
102- (blobUpload.BatchHash , blobUpload .BatchIndex )
103-
104-
103+ if u .onReuploadNeeded != nil {
104+ // get batch from database
105+ dbBatch , err := u .batchOrm .GetBatchByIndex (u .ctx , blobUpload .BatchIndex )
106+ if err != nil {
107+ log .Error ("failed to get batch by index %d: %w" , blobUpload .BatchIndex , err )
108+ continue
109+ }
110+ if dbBatch .Hash != blobUpload .BatchHash {
111+ log .Error ("found unmatched batch hash when reupload blob data" , "batch index" , blobUpload .BatchIndex , "dbBatch hash" , dbBatch .Hash , "blobUpload batch hash" , blobUpload .BatchHash , "err" , err )
112+ continue
113+ }
114+ if _ , err := u .onReuploadNeeded (dbBatch , types .BlobStoragePlatformArweave , 50 ); err != nil {
115+ log .Error ("failed to reupload blob" , "batch index" , blobUpload .BatchIndex , "batch hash" , blobUpload .BatchHash , "err" , err )
116+ } else {
117+ log .Info ("successfully reuploaded blob with higher gas price" , "batch index" , blobUpload .BatchIndex , "batch hash" , blobUpload .BatchHash )
118+ }
119+ }
105120 }
106- u .client .GetTransactionByID (blobUpload .TxHash )
107- log .Debug ("got pending arweave transaction, waitting for confirmation" )
121+ log .Debug ("got pending arweave transaction, waiting for confirmation" )
108122 }
109123 if err == schema .ErrNotFound || err == schema .ErrInvalidId {
110-
124+ // resend transaction if it's dropped
125+ if u .onReuploadNeeded != nil {
126+ // get batch from database
127+ dbBatch , err := u .batchOrm .GetBatchByIndex (u .ctx , blobUpload .BatchIndex )
128+ if err != nil {
129+ log .Error ("failed to get batch by index %d: %w" , blobUpload .BatchIndex , err )
130+ continue
131+ }
132+ if dbBatch .Hash != blobUpload .BatchHash {
133+ log .Error ("found unmatched batch hash when reupload blob data" , "batch index" , blobUpload .BatchIndex , "dbBatch hash" , dbBatch .Hash , "blobUpload batch hash" , blobUpload .BatchHash , "err" , err )
134+ continue
135+ }
136+ if _ , err := u .onReuploadNeeded (dbBatch , types .BlobStoragePlatformArweave , 0 ); err != nil {
137+ log .Error ("failed to reupload blob" , "batch index" , blobUpload .BatchIndex , "batch hash" , blobUpload .BatchHash , "err" , err )
138+ } else {
139+ log .Info ("successfully reuploaded blob" , "batch index" , blobUpload .BatchIndex , "batch hash" , blobUpload .BatchHash )
140+ }
141+ }
111142 }
112143 log .Error ("failed to get arweave transaction status" , "err" , err )
113144 return
0 commit comments