2020-09-26 07:54:03 +00:00
|
|
|
package getsvc
|
|
|
|
|
|
|
|
import (
|
2021-04-29 12:18:29 +00:00
|
|
|
"context"
|
2020-12-08 16:18:24 +00:00
|
|
|
"crypto/sha256"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-12-08 16:18:24 +00:00
|
|
|
"hash"
|
2021-04-29 12:18:29 +00:00
|
|
|
"io"
|
|
|
|
"sync"
|
2020-12-08 16:18:24 +00:00
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-04-29 12:18:29 +00:00
|
|
|
rpcclient "github.com/nspcc-dev/neofs-api-go/rpc/client"
|
|
|
|
signature2 "github.com/nspcc-dev/neofs-api-go/util/signature"
|
2020-09-26 07:54:03 +00:00
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2020-12-08 16:18:24 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2021-04-29 12:18:29 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/rpc"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/signature"
|
2021-06-21 14:13:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/client"
|
2020-12-09 10:32:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2021-06-21 14:13:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2020-12-02 23:45:25 +00:00
|
|
|
objectSvc "github.com/nspcc-dev/neofs-node/pkg/services/object"
|
2020-09-26 07:54:03 +00:00
|
|
|
getsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/get"
|
2021-09-28 06:02:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/internal"
|
2020-12-08 16:26:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
2020-12-08 16:18:24 +00:00
|
|
|
"github.com/nspcc-dev/tzhash/tz"
|
2020-09-26 07:54:03 +00:00
|
|
|
)
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
var errWrongMessageSeq = errors.New("incorrect message sequence")
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *Service) toPrm(req *objectV2.GetRequest, stream objectSvc.GetObjectStream) (*getsvc.Prm, error) {
|
|
|
|
meta := req.GetMetaHeader()
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
commonPrm, err := util.CommonPrmFromV2(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
p := new(getsvc.Prm)
|
2021-01-12 14:55:02 +00:00
|
|
|
p.SetCommonParameters(commonPrm.
|
2021-10-26 12:07:28 +00:00
|
|
|
WithKeyStorage(s.keyStorage),
|
2020-12-11 11:59:16 +00:00
|
|
|
)
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
body := req.GetBody()
|
2020-12-09 10:32:33 +00:00
|
|
|
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
|
2020-12-07 17:49:47 +00:00
|
|
|
p.WithRawFlag(body.GetRaw())
|
2020-12-02 23:45:25 +00:00
|
|
|
p.SetObjectWriter(&streamObjectWriter{stream})
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
if !commonPrm.LocalOnly() {
|
|
|
|
var onceResign sync.Once
|
|
|
|
|
2021-09-28 06:02:02 +00:00
|
|
|
p.SetRequestForwarder(groupAddressRequestForwarder(func(addr network.Address, c client.Client, pubkey []byte) (*objectSDK.Object, error) {
|
2021-04-29 12:18:29 +00:00
|
|
|
var err error
|
|
|
|
|
2021-10-26 12:07:28 +00:00
|
|
|
key, err := s.keyStorage.GetKey(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
// once compose and resign forwarding request
|
|
|
|
onceResign.Do(func() {
|
2021-10-26 12:07:28 +00:00
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
// compose meta header of the local server
|
|
|
|
metaHdr := new(session.RequestMetaHeader)
|
|
|
|
metaHdr.SetTTL(meta.GetTTL() - 1)
|
|
|
|
// TODO: think how to set the other fields
|
|
|
|
metaHdr.SetOrigin(meta)
|
|
|
|
|
|
|
|
req.SetMetaHeader(metaHdr)
|
|
|
|
|
|
|
|
err = signature.SignServiceMessage(key, req)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// code below is copy-pasted from c.GetObject implementation,
|
|
|
|
// perhaps it is worth highlighting the utility function in neofs-api-go
|
|
|
|
|
|
|
|
// open stream
|
2021-06-21 14:13:08 +00:00
|
|
|
stream, err := rpc.GetObject(c.RawForAddress(addr), req, rpcclient.WithContext(stream.Context()))
|
2021-04-29 12:18:29 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("stream opening failed: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
headWas bool
|
|
|
|
payload []byte
|
|
|
|
obj = new(objectV2.Object)
|
|
|
|
resp = new(objectV2.GetResponse)
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// receive message from server stream
|
|
|
|
err := stream.Read(resp)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2021-04-29 12:18:29 +00:00
|
|
|
if !headWas {
|
|
|
|
return nil, io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("reading the response failed: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 06:02:02 +00:00
|
|
|
// verify response key
|
|
|
|
if err = internal.VerifyResponseKeyV2(pubkey, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
// verify response structure
|
|
|
|
if err := signature.VerifyServiceMessage(resp); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("response verification failed: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch v := resp.GetBody().GetObjectPart().(type) {
|
|
|
|
default:
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("unexpected object part %T", v)
|
2021-04-29 12:18:29 +00:00
|
|
|
case *objectV2.GetObjectPartInit:
|
|
|
|
if headWas {
|
|
|
|
return nil, errWrongMessageSeq
|
|
|
|
}
|
|
|
|
|
|
|
|
headWas = true
|
|
|
|
|
|
|
|
obj.SetObjectID(v.GetObjectID())
|
|
|
|
obj.SetSignature(v.GetSignature())
|
|
|
|
|
|
|
|
hdr := v.GetHeader()
|
|
|
|
obj.SetHeader(hdr)
|
|
|
|
|
|
|
|
payload = make([]byte, 0, hdr.GetPayloadLength())
|
|
|
|
case *objectV2.GetObjectPartChunk:
|
|
|
|
if !headWas {
|
|
|
|
return nil, errWrongMessageSeq
|
|
|
|
}
|
|
|
|
|
|
|
|
payload = append(payload, v.GetChunk()...)
|
|
|
|
case *objectV2.SplitInfo:
|
|
|
|
si := objectSDK.NewSplitInfoFromV2(v)
|
|
|
|
return nil, objectSDK.NewSplitInfoError(si)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.SetPayload(payload)
|
|
|
|
|
|
|
|
// convert the object
|
|
|
|
return objectSDK.NewFromV2(obj), nil
|
2021-06-22 12:08:17 +00:00
|
|
|
}))
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
return p, nil
|
2020-09-26 07:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
func (s *Service) toRangePrm(req *objectV2.GetRangeRequest, stream objectSvc.GetObjectRangeStream) (*getsvc.RangePrm, error) {
|
|
|
|
meta := req.GetMetaHeader()
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
commonPrm, err := util.CommonPrmFromV2(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
p := new(getsvc.RangePrm)
|
2021-01-12 14:55:02 +00:00
|
|
|
p.SetCommonParameters(commonPrm.
|
2021-10-26 12:07:28 +00:00
|
|
|
WithKeyStorage(s.keyStorage),
|
2020-12-11 11:59:16 +00:00
|
|
|
)
|
2020-12-07 17:49:47 +00:00
|
|
|
|
|
|
|
body := req.GetBody()
|
2020-12-09 10:32:33 +00:00
|
|
|
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
|
2020-12-07 17:49:47 +00:00
|
|
|
p.WithRawFlag(body.GetRaw())
|
|
|
|
p.SetChunkWriter(&streamObjectRangeWriter{stream})
|
2020-12-09 10:32:33 +00:00
|
|
|
p.SetRange(objectSDK.NewRangeFromV2(body.GetRange()))
|
2020-12-07 17:49:47 +00:00
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
if !commonPrm.LocalOnly() {
|
|
|
|
var onceResign sync.Once
|
|
|
|
|
2021-10-26 12:07:28 +00:00
|
|
|
key, err := s.keyStorage.GetKey(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-28 06:02:02 +00:00
|
|
|
p.SetRequestForwarder(groupAddressRequestForwarder(func(addr network.Address, c client.Client, pubkey []byte) (*objectSDK.Object, error) {
|
2021-04-29 12:18:29 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// once compose and resign forwarding request
|
|
|
|
onceResign.Do(func() {
|
|
|
|
// compose meta header of the local server
|
|
|
|
metaHdr := new(session.RequestMetaHeader)
|
|
|
|
metaHdr.SetTTL(meta.GetTTL() - 1)
|
|
|
|
// TODO: think how to set the other fields
|
|
|
|
metaHdr.SetOrigin(meta)
|
|
|
|
|
|
|
|
req.SetMetaHeader(metaHdr)
|
|
|
|
|
|
|
|
err = signature.SignServiceMessage(key, req)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// code below is copy-pasted from c.ObjectPayloadRangeData implementation,
|
|
|
|
// perhaps it is worth highlighting the utility function in neofs-api-go
|
|
|
|
|
|
|
|
// open stream
|
2021-06-21 14:13:08 +00:00
|
|
|
stream, err := rpc.GetObjectRange(c.RawForAddress(addr), req, rpcclient.WithContext(stream.Context()))
|
2021-04-29 12:18:29 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not create Get payload range stream: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 07:41:26 +00:00
|
|
|
payload := make([]byte, 0, body.GetRange().GetLength())
|
2021-04-29 12:18:29 +00:00
|
|
|
|
|
|
|
resp := new(objectV2.GetRangeResponse)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// receive message from server stream
|
|
|
|
err := stream.Read(resp)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2021-04-29 12:18:29 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("reading the response failed: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 06:02:02 +00:00
|
|
|
// verify response key
|
|
|
|
if err = internal.VerifyResponseKeyV2(pubkey, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
// verify response structure
|
|
|
|
if err := signature.VerifyServiceMessage(resp); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not verify %T: %w", resp, err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch v := resp.GetBody().GetRangePart().(type) {
|
|
|
|
case nil:
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("unexpected range type %T", v)
|
2021-04-29 12:18:29 +00:00
|
|
|
case *objectV2.GetRangePartChunk:
|
|
|
|
payload = append(payload, v.GetChunk()...)
|
|
|
|
case *objectV2.SplitInfo:
|
|
|
|
si := objectSDK.NewSplitInfoFromV2(v)
|
|
|
|
|
|
|
|
return nil, objectSDK.NewSplitInfoError(si)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := objectSDK.NewRaw()
|
|
|
|
obj.SetPayload(payload)
|
|
|
|
|
|
|
|
return obj.Object(), nil
|
2021-06-22 12:08:17 +00:00
|
|
|
}))
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:18:24 +00:00
|
|
|
func (s *Service) toHashRangePrm(req *objectV2.GetRangeHashRequest) (*getsvc.RangeHashPrm, error) {
|
2021-01-12 14:55:02 +00:00
|
|
|
commonPrm, err := util.CommonPrmFromV2(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:18:24 +00:00
|
|
|
p := new(getsvc.RangeHashPrm)
|
2021-01-12 14:55:02 +00:00
|
|
|
p.SetCommonParameters(commonPrm.
|
2021-10-26 12:07:28 +00:00
|
|
|
WithKeyStorage(s.keyStorage),
|
2020-12-11 11:59:16 +00:00
|
|
|
)
|
2020-12-08 16:18:24 +00:00
|
|
|
|
|
|
|
body := req.GetBody()
|
2020-12-09 10:32:33 +00:00
|
|
|
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
|
2020-12-08 16:18:24 +00:00
|
|
|
|
|
|
|
rngsV2 := body.GetRanges()
|
2020-12-09 10:32:33 +00:00
|
|
|
rngs := make([]*objectSDK.Range, 0, len(rngsV2))
|
2020-12-08 16:18:24 +00:00
|
|
|
|
|
|
|
for i := range rngsV2 {
|
2020-12-09 10:32:33 +00:00
|
|
|
rngs = append(rngs, objectSDK.NewRangeFromV2(rngsV2[i]))
|
2020-12-08 16:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.SetRangeList(rngs)
|
2021-01-11 13:50:04 +00:00
|
|
|
p.SetSalt(body.GetSalt())
|
2020-12-08 16:18:24 +00:00
|
|
|
|
|
|
|
switch t := body.GetType(); t {
|
|
|
|
default:
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("unknown checksum type %v", t)
|
2020-12-08 16:18:24 +00:00
|
|
|
case refs.SHA256:
|
|
|
|
p.SetHashGenerator(func() hash.Hash {
|
|
|
|
return sha256.New()
|
|
|
|
})
|
|
|
|
case refs.TillichZemor:
|
|
|
|
p.SetHashGenerator(func() hash.Hash {
|
|
|
|
return tz.New()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
type headResponseWriter struct {
|
|
|
|
mainOnly bool
|
|
|
|
|
|
|
|
body *objectV2.HeadResponseBody
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *headResponseWriter) WriteHeader(hdr *object.Object) error {
|
|
|
|
if w.mainOnly {
|
|
|
|
w.body.SetHeaderPart(toShortObjectHeader(hdr))
|
|
|
|
} else {
|
|
|
|
w.body.SetHeaderPart(toFullObjectHeader(hdr))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
func (s *Service) toHeadPrm(ctx context.Context, req *objectV2.HeadRequest, resp *objectV2.HeadResponse) (*getsvc.HeadPrm, error) {
|
2020-12-09 10:32:33 +00:00
|
|
|
meta := req.GetMetaHeader()
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
commonPrm, err := util.CommonPrmFromV2(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
p := new(getsvc.HeadPrm)
|
2021-01-12 14:55:02 +00:00
|
|
|
p.SetCommonParameters(commonPrm.
|
2021-10-26 12:07:28 +00:00
|
|
|
WithKeyStorage(s.keyStorage),
|
2020-12-11 11:59:16 +00:00
|
|
|
)
|
2020-12-09 10:32:33 +00:00
|
|
|
|
|
|
|
body := req.GetBody()
|
|
|
|
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
|
|
|
|
p.WithRawFlag(body.GetRaw())
|
|
|
|
p.SetHeaderWriter(&headResponseWriter{
|
|
|
|
mainOnly: body.GetMainOnly(),
|
|
|
|
body: resp.GetBody(),
|
|
|
|
})
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
if !commonPrm.LocalOnly() {
|
|
|
|
var onceResign sync.Once
|
|
|
|
|
2021-09-28 06:02:02 +00:00
|
|
|
p.SetRequestForwarder(groupAddressRequestForwarder(func(addr network.Address, c client.Client, pubkey []byte) (*objectSDK.Object, error) {
|
2021-04-29 12:18:29 +00:00
|
|
|
var err error
|
|
|
|
|
2021-10-26 12:07:28 +00:00
|
|
|
key, err := s.keyStorage.GetKey(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
// once compose and resign forwarding request
|
|
|
|
onceResign.Do(func() {
|
|
|
|
// compose meta header of the local server
|
|
|
|
metaHdr := new(session.RequestMetaHeader)
|
|
|
|
metaHdr.SetTTL(meta.GetTTL() - 1)
|
|
|
|
// TODO: think how to set the other fields
|
|
|
|
metaHdr.SetOrigin(meta)
|
|
|
|
|
|
|
|
req.SetMetaHeader(metaHdr)
|
|
|
|
|
|
|
|
err = signature.SignServiceMessage(key, req)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// code below is copy-pasted from c.GetObjectHeader implementation,
|
|
|
|
// perhaps it is worth highlighting the utility function in neofs-api-go
|
|
|
|
|
|
|
|
// send Head request
|
2021-06-21 14:13:08 +00:00
|
|
|
resp, err := rpc.HeadObject(c.RawForAddress(addr), req, rpcclient.WithContext(ctx))
|
2021-04-29 12:18:29 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("sending the request failed: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 06:02:02 +00:00
|
|
|
// verify response key
|
|
|
|
if err = internal.VerifyResponseKeyV2(pubkey, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
// verify response structure
|
|
|
|
if err := signature.VerifyServiceMessage(resp); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("response verification failed: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
hdr *objectV2.Header
|
|
|
|
idSig *refs.Signature
|
|
|
|
)
|
|
|
|
|
|
|
|
switch v := resp.GetBody().GetHeaderPart().(type) {
|
|
|
|
case nil:
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("unexpected header type %T", v)
|
2021-04-29 12:18:29 +00:00
|
|
|
case *objectV2.ShortHeader:
|
|
|
|
if !body.GetMainOnly() {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("wrong header part type: expected %T, received %T",
|
2021-04-29 12:18:29 +00:00
|
|
|
(*objectV2.ShortHeader)(nil), (*objectV2.HeaderWithSignature)(nil),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
h := v
|
|
|
|
|
|
|
|
hdr = new(objectV2.Header)
|
|
|
|
hdr.SetPayloadLength(h.GetPayloadLength())
|
|
|
|
hdr.SetVersion(h.GetVersion())
|
|
|
|
hdr.SetOwnerID(h.GetOwnerID())
|
|
|
|
hdr.SetObjectType(h.GetObjectType())
|
|
|
|
hdr.SetCreationEpoch(h.GetCreationEpoch())
|
|
|
|
hdr.SetPayloadHash(h.GetPayloadHash())
|
|
|
|
hdr.SetHomomorphicHash(h.GetHomomorphicHash())
|
|
|
|
case *objectV2.HeaderWithSignature:
|
|
|
|
if body.GetMainOnly() {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("wrong header part type: expected %T, received %T",
|
2021-04-29 12:18:29 +00:00
|
|
|
(*objectV2.HeaderWithSignature)(nil), (*objectV2.ShortHeader)(nil),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
hdrWithSig := v
|
|
|
|
if hdrWithSig == nil {
|
|
|
|
return nil, errors.New("nil object part")
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr = hdrWithSig.GetHeader()
|
|
|
|
idSig = hdrWithSig.GetSignature()
|
|
|
|
|
|
|
|
if err := signature2.VerifyDataWithSource(
|
|
|
|
signature.StableMarshalerWrapper{
|
|
|
|
SM: p.Address().ObjectID().ToV2(),
|
|
|
|
},
|
|
|
|
func() (key, sig []byte) {
|
|
|
|
return idSig.GetKey(), idSig.GetSign()
|
|
|
|
},
|
|
|
|
); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("incorrect object header signature: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
case *objectV2.SplitInfo:
|
|
|
|
si := objectSDK.NewSplitInfoFromV2(v)
|
|
|
|
|
|
|
|
return nil, objectSDK.NewSplitInfoError(si)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := new(objectV2.Object)
|
|
|
|
obj.SetHeader(hdr)
|
|
|
|
obj.SetSignature(idSig)
|
|
|
|
|
|
|
|
raw := object.NewRawFromV2(obj)
|
|
|
|
raw.SetID(p.Address().ObjectID())
|
|
|
|
|
|
|
|
// convert the object
|
|
|
|
return raw.Object().SDK(), nil
|
2021-06-22 12:08:17 +00:00
|
|
|
}))
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitInfoResponse(info *objectSDK.SplitInfo) *objectV2.GetResponse {
|
2020-12-02 23:45:25 +00:00
|
|
|
resp := new(objectV2.GetResponse)
|
|
|
|
|
|
|
|
body := new(objectV2.GetResponseBody)
|
|
|
|
resp.SetBody(body)
|
|
|
|
|
|
|
|
body.SetObjectPart(info.ToV2())
|
|
|
|
|
|
|
|
return resp
|
2020-09-26 07:54:03 +00:00
|
|
|
}
|
2020-12-07 17:49:47 +00:00
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func splitInfoRangeResponse(info *objectSDK.SplitInfo) *objectV2.GetRangeResponse {
|
2020-12-07 17:49:47 +00:00
|
|
|
resp := new(objectV2.GetRangeResponse)
|
|
|
|
|
|
|
|
body := new(objectV2.GetRangeResponseBody)
|
|
|
|
resp.SetBody(body)
|
|
|
|
|
|
|
|
body.SetRangePart(info.ToV2())
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
2020-12-08 16:18:24 +00:00
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func setSplitInfoHeadResponse(info *objectSDK.SplitInfo, resp *objectV2.HeadResponse) {
|
|
|
|
resp.GetBody().SetHeaderPart(info.ToV2())
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:18:24 +00:00
|
|
|
func toHashResponse(typ refs.ChecksumType, res *getsvc.RangeHashRes) *objectV2.GetRangeHashResponse {
|
|
|
|
resp := new(objectV2.GetRangeHashResponse)
|
|
|
|
|
|
|
|
body := new(objectV2.GetRangeHashResponseBody)
|
|
|
|
resp.SetBody(body)
|
|
|
|
|
|
|
|
body.SetType(typ)
|
|
|
|
body.SetHashList(res.Hashes())
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
2020-12-09 10:32:33 +00:00
|
|
|
|
|
|
|
func toFullObjectHeader(hdr *object.Object) objectV2.GetHeaderPart {
|
|
|
|
obj := hdr.ToV2()
|
|
|
|
|
|
|
|
hs := new(objectV2.HeaderWithSignature)
|
|
|
|
hs.SetHeader(obj.GetHeader())
|
|
|
|
hs.SetSignature(obj.GetSignature())
|
|
|
|
|
|
|
|
return hs
|
|
|
|
}
|
|
|
|
|
|
|
|
func toShortObjectHeader(hdr *object.Object) objectV2.GetHeaderPart {
|
|
|
|
hdrV2 := hdr.ToV2().GetHeader()
|
|
|
|
|
|
|
|
sh := new(objectV2.ShortHeader)
|
|
|
|
sh.SetOwnerID(hdrV2.GetOwnerID())
|
|
|
|
sh.SetCreationEpoch(hdrV2.GetCreationEpoch())
|
|
|
|
sh.SetPayloadLength(hdrV2.GetPayloadLength())
|
|
|
|
sh.SetVersion(hdrV2.GetVersion())
|
|
|
|
sh.SetObjectType(hdrV2.GetObjectType())
|
2020-12-23 09:35:36 +00:00
|
|
|
sh.SetHomomorphicHash(hdrV2.GetHomomorphicHash())
|
|
|
|
sh.SetPayloadHash(hdrV2.GetPayloadHash())
|
2020-12-09 10:32:33 +00:00
|
|
|
|
|
|
|
return sh
|
|
|
|
}
|
2021-06-22 12:08:17 +00:00
|
|
|
|
2021-09-28 06:02:02 +00:00
|
|
|
func groupAddressRequestForwarder(f func(network.Address, client.Client, []byte) (*objectSDK.Object, error)) getsvc.RequestForwarder {
|
2021-09-28 04:46:10 +00:00
|
|
|
return func(info client.NodeInfo, c client.Client) (*objectSDK.Object, error) {
|
2021-06-22 12:08:17 +00:00
|
|
|
var (
|
|
|
|
firstErr error
|
|
|
|
res *objectSDK.Object
|
2021-09-28 06:02:02 +00:00
|
|
|
|
|
|
|
key = info.PublicKey()
|
2021-06-22 12:08:17 +00:00
|
|
|
)
|
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
info.AddressGroup().IterateAddresses(func(addr network.Address) (stop bool) {
|
2021-06-22 12:08:17 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
stop = err == nil
|
|
|
|
|
|
|
|
if stop || firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
|
|
|
|
// would be nice to log otherwise
|
|
|
|
}()
|
|
|
|
|
2021-09-28 06:02:02 +00:00
|
|
|
res, err = f(addr, c, key)
|
2021-06-22 12:08:17 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
return res, firstErr
|
|
|
|
}
|
|
|
|
}
|