Skip to content

Commit fe58a30

Browse files
committed
feat(blob-uploader): add arweave sender
1 parent 9780bcb commit fe58a30

5 files changed

Lines changed: 154 additions & 124 deletions

File tree

rollup/conf/config.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@
112112
"region": "us-west-2",
113113
"access_key": "ACCESSKEY",
114114
"secret_key": "SECRETKEY"
115+
},
116+
"arweave_config": {
117+
"endpoint": "",
118+
"private_key_path": "",
119+
"tx_tag": "",
120+
"confirmations": ""
115121
}
116122
}
117123
},
@@ -121,4 +127,4 @@
121127
"maxOpenNum": 200,
122128
"maxIdleNum": 20
123129
}
124-
}
130+
}

rollup/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ require (
1414
github.com/go-resty/resty/v2 v2.7.0
1515
github.com/holiman/uint256 v1.3.2
1616
github.com/mitchellh/mapstructure v1.5.0
17-
github.com/permadao/goar v1.1.1
1817
github.com/prometheus/client_golang v1.16.0
1918
github.com/scroll-tech/da-codec v0.1.3-0.20250519114140-bfa7133d4ad1
2019
github.com/scroll-tech/go-ethereum v1.10.14-0.20250609212349-f30b2b1832a2
@@ -43,6 +42,7 @@ require (
4342
github.com/aws/smithy-go v1.22.2 // indirect
4443
github.com/beorn7/perks v1.0.1 // indirect
4544
github.com/bits-and-blooms/bitset v1.20.0 // indirect
45+
github.com/btcsuite/btcd v0.23.5-0.20231215221805-96c9fd8078fd // indirect
4646
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
4747
github.com/btcsuite/btcd/btcutil v1.1.5 // indirect
4848
github.com/bytedance/sonic v1.10.1 // indirect
@@ -107,6 +107,7 @@ require (
107107
github.com/onsi/gomega v1.27.1 // indirect
108108
github.com/panjf2000/ants/v2 v2.10.0 // indirect
109109
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
110+
github.com/permadao/goar v1.1.1 // indirect
110111
github.com/pkg/errors v0.9.1 // indirect
111112
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
112113
github.com/prometheus/client_model v0.4.0 // indirect

rollup/internal/config/l2.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ type AWSS3Config struct {
6666

6767
// ArweaveConfig loads arweave_uploader configuration items.
6868
type ArweaveConfig struct {
69-
Endpoint string `json:"endpoint"`
70-
PrivateKey string `json:"private_key"`
71-
TxTag string `json:"tx_tag"`
72-
Confirmations uint64 `json:"confirmations"`
69+
Endpoint string `json:"endpoint"`
70+
PrivateKeyPath string `json:"private_key_path"`
71+
TxTag string `json:"tx_tag"`
72+
Confirmations uint64 `json:"confirmations"`
7373
}

rollup/internal/controller/blob_uploader/arweave_sender.go

Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
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

Comments
 (0)