@@ -3,19 +3,17 @@ package util
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "slices"
7
6
"strconv"
8
7
"sync"
9
8
"time"
10
9
11
10
"github.com/nspcc-dev/neo-go/cli/cmdargs"
12
11
"github.com/nspcc-dev/neo-go/cli/options"
13
12
"github.com/nspcc-dev/neo-go/pkg/core/block"
13
+ "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
14
14
"github.com/nspcc-dev/neo-go/pkg/io"
15
15
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
16
16
"github.com/nspcc-dev/neo-go/pkg/services/helpers/neofs"
17
- "github.com/nspcc-dev/neo-go/pkg/util"
18
- "github.com/nspcc-dev/neo-go/pkg/wallet"
19
17
"github.com/nspcc-dev/neofs-sdk-go/client"
20
18
"github.com/nspcc-dev/neofs-sdk-go/container"
21
19
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
@@ -107,12 +105,12 @@ func uploadBin(ctx *cli.Context) error {
107
105
return cli .Exit (fmt .Sprintf ("failed to get current block height from RPC: %v" , err ), 1 )
108
106
}
109
107
fmt .Fprintln (ctx .App .Writer , "Chain block height:" , currentBlockHeight )
110
- i , buf , err := searchIndexFile (ctx , pWrapper , containerID , acc , signer , indexFileSize , attr , indexAttrKey , maxParallelSearches , maxRetries , debug )
108
+ i , buf , err := searchIndexFile (ctx , pWrapper , containerID , acc . PrivateKey () , signer , indexFileSize , attr , indexAttrKey , maxParallelSearches , maxRetries , debug )
111
109
if err != nil {
112
110
return cli .Exit (fmt .Errorf ("failed to find objects: %w" , err ), 1 )
113
111
}
114
112
115
- err = uploadBlocksAndIndexFiles (ctx , pWrapper , rpc , signer , containerID , acc , attr , indexAttrKey , buf , i , indexFileSize , uint (currentBlockHeight ), numWorkers , maxRetries , debug )
113
+ err = uploadBlocksAndIndexFiles (ctx , pWrapper , rpc , signer , containerID , user . NewFromScriptHash ( acc . ScriptHash ()) , attr , indexAttrKey , buf , i , indexFileSize , uint (currentBlockHeight ), numWorkers , maxRetries , debug )
116
114
if err != nil {
117
115
return cli .Exit (fmt .Errorf ("failed to upload objects: %w" , err ), 1 )
118
116
}
@@ -140,7 +138,7 @@ func retry(action func() error, maxRetries uint, debug bool) error {
140
138
}
141
139
142
140
// uploadBlocksAndIndexFiles uploads the blocks and index files to the container using the pool.
143
- func uploadBlocksAndIndexFiles (ctx * cli.Context , p poolWrapper , rpc * rpcclient.Client , signer user.Signer , containerID cid.ID , acc * wallet. Account , attr , indexAttributeKey string , buf []byte , currentIndexFileID , indexFileSize , currentBlockHeight uint , numWorkers , maxRetries uint , debug bool ) error {
141
+ func uploadBlocksAndIndexFiles (ctx * cli.Context , p poolWrapper , rpc * rpcclient.Client , signer user.Signer , containerID cid.ID , ownerID user. ID , attr , indexAttributeKey string , buf []byte , currentIndexFileID , indexFileSize , currentBlockHeight uint , numWorkers , maxRetries uint , debug bool ) error {
144
142
if currentIndexFileID * indexFileSize >= currentBlockHeight {
145
143
fmt .Fprintf (ctx .App .Writer , "No new blocks to upload. Need to upload starting from %d, current height %d\n " , currentIndexFileID * indexFileSize , currentBlockHeight )
146
144
return nil
@@ -152,15 +150,14 @@ func uploadBlocksAndIndexFiles(ctx *cli.Context, p poolWrapper, rpc *rpcclient.C
152
150
errCh = make (chan error )
153
151
doneCh = make (chan struct {})
154
152
wg sync.WaitGroup
155
- emptyOID = make ([]byte , neofs .OIDSize )
156
153
)
157
154
fmt .Fprintf (ctx .App .Writer , "Processing batch from %d to %d\n " , indexFileStart , indexFileEnd - 1 )
158
155
wg .Add (int (numWorkers ))
159
156
for i := range numWorkers {
160
157
go func (i uint ) {
161
158
defer wg .Done ()
162
159
for blockIndex := indexFileStart + i ; blockIndex < indexFileEnd ; blockIndex += numWorkers {
163
- if slices . Compare (buf [blockIndex % indexFileSize * neofs . OIDSize : blockIndex % indexFileSize * neofs . OIDSize + neofs . OIDSize ], emptyOID ) != 0 {
160
+ if ! oid . ID (buf [blockIndex % indexFileSize * oid . Size : blockIndex % indexFileSize * oid . Size + oid . Size ]). IsZero () {
164
161
if debug {
165
162
fmt .Fprintf (ctx .App .Writer , "Block %d is already uploaded\n " , blockIndex )
166
163
}
@@ -207,7 +204,7 @@ func uploadBlocksAndIndexFiles(ctx *cli.Context, p poolWrapper, rpc *rpcclient.C
207
204
)
208
205
errRetr := retry (func () error {
209
206
var errUpload error
210
- resOid , errUpload = uploadObj (ctx .Context , p , signer , acc . PrivateKey (). GetScriptHash () , containerID , objBytes , attrs )
207
+ resOid , errUpload = uploadObj (ctx .Context , p , signer , ownerID , containerID , objBytes , attrs )
211
208
if errUpload != nil {
212
209
return errUpload
213
210
}
@@ -223,7 +220,7 @@ func uploadBlocksAndIndexFiles(ctx *cli.Context, p poolWrapper, rpc *rpcclient.C
223
220
}
224
221
return
225
222
}
226
- resOid . Encode (buf [blockIndex % indexFileSize * neofs . OIDSize :])
223
+ copy (buf [blockIndex % indexFileSize * oid . Size :], resOid [ :])
227
224
}
228
225
}(i )
229
226
}
@@ -241,9 +238,9 @@ func uploadBlocksAndIndexFiles(ctx *cli.Context, p poolWrapper, rpc *rpcclient.C
241
238
fmt .Fprintf (ctx .App .Writer , "Successfully processed batch of blocks: from %d to %d\n " , indexFileStart , indexFileEnd - 1 )
242
239
243
240
// Additional check for empty OIDs in the buffer.
244
- for k := uint (0 ); k < (indexFileEnd - indexFileStart )* neofs . OIDSize ; k += neofs . OIDSize {
245
- if slices . Compare (buf [k : k + neofs . OIDSize ], emptyOID ) == 0 {
246
- return fmt .Errorf ("empty OID found in index file %d at position %d (block index %d)" , indexFileStart / indexFileSize , k / neofs . OIDSize , indexFileStart / indexFileSize * indexFileSize + k / neofs . OIDSize )
241
+ for k := uint (0 ); k < (indexFileEnd - indexFileStart )* oid . Size ; k += oid . Size {
242
+ if oid . ID (buf [k : k + oid . Size ]). IsZero () {
243
+ return fmt .Errorf ("empty OID found in index file %d at position %d (block index %d)" , indexFileStart / indexFileSize , k / oid . Size , indexFileStart / indexFileSize * indexFileSize + k / oid . Size )
247
244
}
248
245
}
249
246
if indexFileEnd - indexFileStart == indexFileSize {
@@ -254,7 +251,7 @@ func uploadBlocksAndIndexFiles(ctx *cli.Context, p poolWrapper, rpc *rpcclient.C
254
251
}
255
252
err := retry (func () error {
256
253
var errUpload error
257
- _ , errUpload = uploadObj (ctx .Context , p , signer , acc . PrivateKey (). GetScriptHash () , containerID , buf , attrs )
254
+ _ , errUpload = uploadObj (ctx .Context , p , signer , ownerID , containerID , buf , attrs )
258
255
return errUpload
259
256
}, maxRetries , debug )
260
257
if err != nil {
@@ -268,10 +265,10 @@ func uploadBlocksAndIndexFiles(ctx *cli.Context, p poolWrapper, rpc *rpcclient.C
268
265
}
269
266
270
267
// searchIndexFile returns the ID and buffer for the next index file to be uploaded.
271
- func searchIndexFile (ctx * cli.Context , p poolWrapper , containerID cid.ID , account * wallet. Account , signer user.Signer , indexFileSize uint , blockAttributeKey , attributeKey string , maxParallelSearches , maxRetries uint , debug bool ) (uint , []byte , error ) {
268
+ func searchIndexFile (ctx * cli.Context , p poolWrapper , containerID cid.ID , privKeys * keys. PrivateKey , signer user.Signer , indexFileSize uint , blockAttributeKey , attributeKey string , maxParallelSearches , maxRetries uint , debug bool ) (uint , []byte , error ) {
272
269
var (
273
270
// buf is used to store OIDs of the uploaded blocks.
274
- buf = make ([]byte , indexFileSize * neofs . OIDSize )
271
+ buf = make ([]byte , indexFileSize * oid . Size )
275
272
doneCh = make (chan struct {})
276
273
errCh = make (chan error )
277
274
@@ -283,7 +280,7 @@ func searchIndexFile(ctx *cli.Context, p poolWrapper, containerID cid.ID, accoun
283
280
// Search for existing index files.
284
281
filters .AddFilter ("IndexSize" , fmt .Sprintf ("%d" , indexFileSize ), object .MatchStringEqual )
285
282
for i := 0 ; ; i ++ {
286
- indexIDs := searchObjects (ctx .Context , p , containerID , account , attributeKey , uint (i ), uint (i + 1 ), 1 , maxRetries , debug , errCh , filters )
283
+ indexIDs := searchObjects (ctx .Context , p , containerID , privKeys , attributeKey , uint (i ), uint (i + 1 ), 1 , maxRetries , debug , errCh , filters )
287
284
count := 0
288
285
for range indexIDs {
289
286
count ++
@@ -338,14 +335,14 @@ func searchIndexFile(ctx *cli.Context, p poolWrapper, containerID cid.ID, accoun
338
335
}
339
336
pos := uint (blockIndex ) % indexFileSize
340
337
if _ , ok := processedIndices .LoadOrStore (pos , blockIndex ); ! ok {
341
- id . Encode (buf [pos * neofs . OIDSize :])
338
+ copy (buf [pos * oid . Size :], id [ :])
342
339
}
343
340
}
344
341
}()
345
342
}
346
343
347
344
// Search for blocks within the index file range.
348
- objIDs := searchObjects (ctx .Context , p , containerID , account , blockAttributeKey , existingIndexCount * indexFileSize , (existingIndexCount + 1 )* indexFileSize , maxParallelSearches , maxRetries , debug , errCh )
345
+ objIDs := searchObjects (ctx .Context , p , containerID , privKeys , blockAttributeKey , existingIndexCount * indexFileSize , (existingIndexCount + 1 )* indexFileSize , maxParallelSearches , maxRetries , debug , errCh )
349
346
for id := range objIDs {
350
347
oidCh <- id
351
348
}
@@ -364,7 +361,7 @@ func searchIndexFile(ctx *cli.Context, p poolWrapper, containerID cid.ID, accoun
364
361
// searchObjects searches in parallel for objects with attribute GE startIndex and LT
365
362
// endIndex. It returns a buffered channel of resulting object IDs and closes it once
366
363
// OID search is finished. Errors are sent to errCh in a non-blocking way.
367
- func searchObjects (ctx context.Context , p poolWrapper , containerID cid.ID , account * wallet. Account , blockAttributeKey string , startIndex , endIndex , maxParallelSearches , maxRetries uint , debug bool , errCh chan error , additionalFilters ... object.SearchFilters ) chan oid.ID {
364
+ func searchObjects (ctx context.Context , p poolWrapper , containerID cid.ID , privKeys * keys. PrivateKey , blockAttributeKey string , startIndex , endIndex , maxParallelSearches , maxRetries uint , debug bool , errCh chan error , additionalFilters ... object.SearchFilters ) chan oid.ID {
368
365
var res = make (chan oid.ID , 2 * neofs .DefaultSearchBatchSize )
369
366
go func () {
370
367
var wg sync.WaitGroup
@@ -402,7 +399,7 @@ func searchObjects(ctx context.Context, p poolWrapper, containerID cid.ID, accou
402
399
var objIDs []oid.ID
403
400
err := retry (func () error {
404
401
var errBlockSearch error
405
- objIDs , errBlockSearch = neofs .ObjectSearch (ctx , p , account . PrivateKey () , containerID .String (), prm )
402
+ objIDs , errBlockSearch = neofs .ObjectSearch (ctx , p , privKeys , containerID .String (), prm )
406
403
return errBlockSearch
407
404
}, maxRetries , debug )
408
405
if err != nil {
@@ -426,18 +423,16 @@ func searchObjects(ctx context.Context, p poolWrapper, containerID cid.ID, accou
426
423
}
427
424
428
425
// uploadObj uploads object to the container using provided settings.
429
- func uploadObj (ctx context.Context , p poolWrapper , signer user.Signer , owner util. Uint160 , containerID cid.ID , objData []byte , attrs []object.Attribute ) (oid.ID , error ) {
426
+ func uploadObj (ctx context.Context , p poolWrapper , signer user.Signer , ownerID user. ID , containerID cid.ID , objData []byte , attrs []object.Attribute ) (oid.ID , error ) {
430
427
var (
431
- ownerID user.ID
432
428
hdr object.Object
433
429
prmObjectPutInit client.PrmObjectPutInit
434
430
resOID = oid.ID {}
435
431
)
436
432
437
- ownerID .SetScriptHash (owner )
438
433
hdr .SetPayload (objData )
439
434
hdr .SetContainerID (containerID )
440
- hdr .SetOwnerID ( & ownerID )
435
+ hdr .SetOwner ( ownerID )
441
436
hdr .SetAttributes (attrs ... )
442
437
443
438
writer , err := p .ObjectPutInit (ctx , hdr , signer , prmObjectPutInit )
@@ -455,7 +450,7 @@ func uploadObj(ctx context.Context, p poolWrapper, signer user.Signer, owner uti
455
450
}
456
451
res := writer .GetResult ()
457
452
resOID = res .StoredObjectID ()
458
- if resOID .Equals (oid. ID {} ) {
453
+ if resOID .IsZero ( ) {
459
454
return resOID , fmt .Errorf ("object ID is empty" )
460
455
}
461
456
return resOID , nil
0 commit comments