Skip to content

Commit f9d6591

Browse files
cli: refactor upload-bin handler to reuse SDK built-in functionality
Close #3714 Signed-off-by: Ekaterina Pavlova <ekt@morphbits.io>
1 parent 3cc6ba3 commit f9d6591

File tree

1 file changed

+5
-41
lines changed

1 file changed

+5
-41
lines changed

cli/util/upload_bin.go

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@ import (
1616
"github.com/nspcc-dev/neo-go/pkg/services/helpers/neofs"
1717
"github.com/nspcc-dev/neo-go/pkg/util"
1818
"github.com/nspcc-dev/neo-go/pkg/wallet"
19-
"github.com/nspcc-dev/neofs-sdk-go/checksum"
2019
"github.com/nspcc-dev/neofs-sdk-go/client"
2120
"github.com/nspcc-dev/neofs-sdk-go/container"
2221
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
23-
"github.com/nspcc-dev/neofs-sdk-go/netmap"
2422
"github.com/nspcc-dev/neofs-sdk-go/object"
2523
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
2624
"github.com/nspcc-dev/neofs-sdk-go/pool"
2725
"github.com/nspcc-dev/neofs-sdk-go/user"
28-
"github.com/nspcc-dev/neofs-sdk-go/version"
2926
"github.com/urfave/cli/v2"
3027
)
3128

@@ -86,17 +83,6 @@ func uploadBin(ctx *cli.Context) error {
8683
}
8784
defer p.Close()
8885

89-
var net netmap.NetworkInfo
90-
err = retry(func() error {
91-
var errNet error
92-
net, errNet = p.NetworkInfo(ctx.Context, client.PrmNetworkInfo{})
93-
return errNet
94-
}, maxRetries)
95-
if err != nil {
96-
return cli.Exit(fmt.Errorf("failed to get network info: %w", err), 1)
97-
}
98-
homomorphicHashingDisabled := net.HomomorphicHashingDisabled()
99-
10086
var containerObj container.Container
10187
err = retry(func() error {
10288
containerObj, err = p.ContainerGet(ctx.Context, containerID, client.PrmContainerGet{})
@@ -126,7 +112,7 @@ func uploadBin(ctx *cli.Context) error {
126112
return cli.Exit(fmt.Errorf("failed to find objects: %w", err), 1)
127113
}
128114

129-
err = uploadBlocksAndIndexFiles(ctx, pWrapper, rpc, signer, containerID, acc, attr, indexAttrKey, buf, i, indexFileSize, uint(currentBlockHeight), homomorphicHashingDisabled, numWorkers, maxRetries, debug)
115+
err = uploadBlocksAndIndexFiles(ctx, pWrapper, rpc, signer, containerID, acc, attr, indexAttrKey, buf, i, indexFileSize, uint(currentBlockHeight), numWorkers, maxRetries, debug)
130116
if err != nil {
131117
return cli.Exit(fmt.Errorf("failed to upload objects: %w", err), 1)
132118
}
@@ -151,7 +137,7 @@ func retry(action func() error, maxRetries uint) error {
151137
}
152138

153139
// uploadBlocksAndIndexFiles uploads the blocks and index files to the container using the pool.
154-
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, homomorphicHashingDisabled bool, numWorkers, maxRetries uint, debug bool) error {
140+
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 {
155141
if currentIndexFileID*indexFileSize >= currentBlockHeight {
156142
fmt.Fprintf(ctx.App.Writer, "No new blocks to upload. Need to upload starting from %d, current height %d\n", currentIndexFileID*indexFileSize, currentBlockHeight)
157143
return nil
@@ -218,7 +204,7 @@ func uploadBlocksAndIndexFiles(ctx *cli.Context, p poolWrapper, rpc *rpcclient.C
218204
)
219205
errRetr := retry(func() error {
220206
var errUpload error
221-
resOid, errUpload = uploadObj(ctx.Context, p, signer, acc.PrivateKey().GetScriptHash(), containerID, objBytes, attrs, homomorphicHashingDisabled)
207+
resOid, errUpload = uploadObj(ctx.Context, p, signer, acc.PrivateKey().GetScriptHash(), containerID, objBytes, attrs)
222208
if errUpload != nil {
223209
return errUpload
224210
}
@@ -265,7 +251,7 @@ func uploadBlocksAndIndexFiles(ctx *cli.Context, p poolWrapper, rpc *rpcclient.C
265251
}
266252
err := retry(func() error {
267253
var errUpload error
268-
_, errUpload = uploadObj(ctx.Context, p, signer, acc.PrivateKey().GetScriptHash(), containerID, buf, attrs, homomorphicHashingDisabled)
254+
_, errUpload = uploadObj(ctx.Context, p, signer, acc.PrivateKey().GetScriptHash(), containerID, buf, attrs)
269255
return errUpload
270256
}, maxRetries)
271257
if err != nil {
@@ -437,41 +423,19 @@ func searchObjects(ctx context.Context, p poolWrapper, containerID cid.ID, accou
437423
}
438424

439425
// uploadObj uploads object to the container using provided settings.
440-
func uploadObj(ctx context.Context, p poolWrapper, signer user.Signer, owner util.Uint160, containerID cid.ID, objData []byte, attrs []object.Attribute, homomorphicHashingDisabled bool) (oid.ID, error) {
426+
func uploadObj(ctx context.Context, p poolWrapper, signer user.Signer, owner util.Uint160, containerID cid.ID, objData []byte, attrs []object.Attribute) (oid.ID, error) {
441427
var (
442428
ownerID user.ID
443429
hdr object.Object
444-
chSHA256 checksum.Checksum
445-
chHomomorphic checksum.Checksum
446-
v = new(version.Version)
447430
prmObjectPutInit client.PrmObjectPutInit
448431
resOID = oid.ID{}
449432
)
450433

451434
ownerID.SetScriptHash(owner)
452435
hdr.SetPayload(objData)
453-
hdr.SetPayloadSize(uint64(len(objData)))
454436
hdr.SetContainerID(containerID)
455437
hdr.SetOwnerID(&ownerID)
456438
hdr.SetAttributes(attrs...)
457-
hdr.SetCreationEpoch(1)
458-
v.SetMajor(1)
459-
hdr.SetVersion(v)
460-
if !homomorphicHashingDisabled {
461-
checksum.Calculate(&chHomomorphic, checksum.TZ, objData)
462-
hdr.SetPayloadHomomorphicHash(chHomomorphic)
463-
}
464-
checksum.Calculate(&chSHA256, checksum.SHA256, objData)
465-
hdr.SetPayloadChecksum(chSHA256)
466-
467-
err := hdr.SetIDWithSignature(signer)
468-
if err != nil {
469-
return resOID, err
470-
}
471-
err = hdr.CheckHeaderVerificationFields()
472-
if err != nil {
473-
return resOID, err
474-
}
475439

476440
writer, err := p.ObjectPutInit(ctx, hdr, signer, prmObjectPutInit)
477441
if err != nil {

0 commit comments

Comments
 (0)