@@ -11,12 +11,9 @@ import (
1111 "github.com/0gfoundation/0g-storage-client/common/blockchain"
1212 "github.com/0gfoundation/0g-storage-client/core"
1313 "github.com/0gfoundation/0g-storage-client/indexer"
14- "github.com/0gfoundation/0g-storage-client/node"
1514 "github.com/0gfoundation/0g-storage-client/transfer"
1615 "github.com/ethereum/go-ethereum/common"
1716 "github.com/ethereum/go-ethereum/common/hexutil"
18- "github.com/openweb3/web3go"
19- "github.com/pkg/errors"
2017 "github.com/sirupsen/logrus"
2118 "github.com/spf13/cobra"
2219)
@@ -170,94 +167,55 @@ func upload(*cobra.Command, []string) {
170167 }
171168 defer file .Close ()
172169
173- uploader , closer , err := newUploader (ctx , file .NumSegments (), uploadArgs , w3client , opt )
174- if err != nil {
175- logrus .WithError (err ).Fatal ("Failed to initialize uploader" )
176- }
177- defer closer ()
178- uploader .WithRoutines (uploadArgs .routines )
179-
180- _ , roots , err := uploader .SplitableUpload (ctx , file , uploadArgs .fragmentSize , opt )
181- if err != nil && opt .FullTrusted {
182- logrus .WithError (err ).Fatal ("Failed to upload file with full trusted nodes" )
183- }
184-
185- if err != nil && ! opt .FullTrusted {
186- logrus .WithError (err ).Warn ("Upload with full trusted nodes failed, retrying with all full trusted nodes" )
187- opt .FullTrusted = true
188- fullUploader , fullCloser , err := newUploader (ctx , file .NumSegments (), uploadArgs , w3client , opt )
170+ var roots []common.Hash
171+ if uploadArgs .indexer != "" {
172+ indexerClient , err := indexer .NewClient (uploadArgs .indexer , indexer.IndexerClientOption {
173+ ProviderOption : providerOption ,
174+ LogOption : zg_common.LogOption {Logger : logrus .StandardLogger ()},
175+ Routines : uploadArgs .routines ,
176+ Contract : & transfer.ContractAddress {
177+ FlowAddress : uploadArgs .flowAddress ,
178+ MarketAddress : uploadArgs .marketAddress ,
179+ },
180+ })
189181 if err != nil {
190- logrus .WithError (err ).Fatal ("Failed to initialize uploader " )
182+ logrus .WithError (err ).Fatal ("Failed to initialize indexer client " )
191183 }
192- defer fullCloser ()
193- fullUploader . WithRoutines ( uploadArgs . routines )
194- _ , roots , err = fullUploader .SplitableUpload (ctx , file , uploadArgs .fragmentSize , opt )
184+ defer indexerClient . Close ()
185+
186+ _ , roots , err = indexerClient .SplitableUpload (ctx , w3client , file , uploadArgs .fragmentSize , opt )
195187 if err != nil {
196- logrus .WithError (err ).Fatal ("Failed to upload file with full trusted nodes " )
188+ logrus .WithError (err ).Fatal ("Failed to upload file" )
197189 }
198- }
199-
200- if len (roots ) == 1 {
201- logrus .Infof ("file uploaded, root = %v" , roots [0 ])
202190 } else {
203- s := make ([]string , len (roots ))
204- for i , root := range roots {
205- s [i ] = root .String ()
206- }
207- logrus .Infof ("file uploaded in %v fragments, roots = %v" , len (roots ), strings .Join (s , "," ))
208- }
209- }
210-
211- func newUploader (ctx context.Context , segNum uint64 , args uploadArgument , w3client * web3go.Client , opt transfer.UploadOption ) (* transfer.Uploader , func (), error ) {
212- var contractConfig * transfer.UploaderContractConfig
213- if args .flowAddress != "" {
214- if ! common .IsHexAddress (args .flowAddress ) {
215- return nil , nil , errors .Errorf ("invalid flow address: %s" , args .flowAddress )
216- }
217- flowAddr := common .HexToAddress (args .flowAddress )
218- contractConfig = & transfer.UploaderContractConfig {
219- FlowAddress : & flowAddr ,
220- }
221- if args .marketAddress != "" {
222- if ! common .IsHexAddress (args .marketAddress ) {
223- return nil , nil , errors .Errorf ("invalid market address: %s" , args .marketAddress )
224- }
225- marketAddr := common .HexToAddress (args .marketAddress )
226- contractConfig .MarketAddress = & marketAddr
227- }
228- } else if args .marketAddress != "" {
229- return nil , nil , errors .New ("market-address requires flow-address" )
230- }
231-
232- if args .indexer != "" {
233- indexerClient , err := indexer .NewClient (args .indexer , indexer.IndexerClientOption {
191+ uploader , closer , err := transfer .NewUploaderFromConfig (ctx , w3client , transfer.UploaderConfig {
192+ Nodes : uploadArgs .node ,
234193 ProviderOption : providerOption ,
235194 LogOption : zg_common.LogOption {Logger : logrus .StandardLogger ()},
195+ Contact : & transfer.ContractAddress {
196+ FlowAddress : uploadArgs .flowAddress ,
197+ MarketAddress : uploadArgs .marketAddress ,
198+ },
199+ Routines : uploadArgs .routines ,
236200 })
237201 if err != nil {
238- return nil , nil , errors . WithMessage (err , "failed to initialize indexer client " )
202+ logrus . WithError (err ). Fatal ( "Failed to initialize uploader " )
239203 }
204+ defer closer ()
240205
241- up , err := indexerClient . NewUploaderFromIndexerNodesWithContractConfig (ctx , segNum , w3client , opt . ExpectedReplica , nil , opt . Method , opt . FullTrusted , contractConfig )
206+ _ , roots , err = uploader . SplitableUpload (ctx , file , uploadArgs . fragmentSize , opt )
242207 if err != nil {
243- return nil , nil , err
208+ logrus . WithError ( err ). Fatal ( "Failed to upload file" )
244209 }
245-
246- return up , indexerClient .Close , nil
247210 }
248211
249- clients := node .MustNewZgsClients (args .node , nil , providerOption )
250- closer := func () {
251- for _ , client := range clients {
252- client .Close ()
212+ if len (roots ) == 1 {
213+ logrus .Infof ("file uploaded, root = %v" , roots [0 ])
214+ } else {
215+ s := make ([]string , len (roots ))
216+ for i , root := range roots {
217+ s [i ] = root .String ()
253218 }
219+ logrus .Infof ("file uploaded in %v fragments, roots = %v" , len (roots ), strings .Join (s , "," ))
254220 }
255-
256- up , err := transfer .NewUploaderWithContractConfig (ctx , w3client , & transfer.SelectedNodes {Trusted : clients }, contractConfig , zg_common.LogOption {Logger : logrus .StandardLogger ()})
257- if err != nil {
258- closer ()
259- return nil , nil , err
260- }
261-
262- return up , closer , nil
263221}
0 commit comments