2020-09-16 14:46:31 +00:00
|
|
|
package transformer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-09-16 14:46:31 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-05-31 11:03:17 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/session"
|
2020-10-21 15:11:19 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2020-09-16 14:46:31 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
)
|
|
|
|
|
|
|
|
type formatter struct {
|
2020-10-21 15:10:22 +00:00
|
|
|
prm *FormatterParams
|
2020-09-16 14:46:31 +00:00
|
|
|
|
|
|
|
obj *object.RawObject
|
|
|
|
|
|
|
|
sz uint64
|
2020-10-21 15:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FormatterParams groups NewFormatTarget parameters.
|
|
|
|
type FormatterParams struct {
|
|
|
|
Key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
NextTarget ObjectTarget
|
2020-09-29 12:41:29 +00:00
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
SessionToken *session.Token
|
2020-10-21 15:11:19 +00:00
|
|
|
|
|
|
|
NetworkState netmap.State
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFormatTarget returns ObjectTarget instance that finalizes object structure
|
|
|
|
// and writes it to the next target.
|
|
|
|
//
|
|
|
|
// Chunks must be written before the WriteHeader call.
|
|
|
|
//
|
|
|
|
// Object changes:
|
|
|
|
// - sets version to current SDK version;
|
|
|
|
// - sets payload size to the total length of all written chunks;
|
2020-09-29 12:41:29 +00:00
|
|
|
// - sets session token;
|
2020-10-21 15:11:19 +00:00
|
|
|
// - sets number of creation epoch;
|
2020-09-16 14:46:31 +00:00
|
|
|
// - calculates and sets verification fields (ID, Signature).
|
2020-10-21 15:10:22 +00:00
|
|
|
func NewFormatTarget(p *FormatterParams) ObjectTarget {
|
2020-09-16 14:46:31 +00:00
|
|
|
return &formatter{
|
2020-10-21 15:10:22 +00:00
|
|
|
prm: p,
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *formatter) WriteHeader(obj *object.RawObject) error {
|
|
|
|
f.obj = obj
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *formatter) Write(p []byte) (n int, err error) {
|
2020-10-21 15:10:22 +00:00
|
|
|
n, err = f.prm.NextTarget.Write(p)
|
2020-09-16 14:46:31 +00:00
|
|
|
|
|
|
|
f.sz += uint64(n)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *formatter) Close() (*AccessIdentifiers, error) {
|
2020-10-21 15:11:19 +00:00
|
|
|
curEpoch := f.prm.NetworkState.CurrentEpoch()
|
|
|
|
|
2020-09-16 14:46:31 +00:00
|
|
|
f.obj.SetVersion(pkg.SDKVersion())
|
|
|
|
f.obj.SetPayloadSize(f.sz)
|
2020-10-21 15:10:22 +00:00
|
|
|
f.obj.SetSessionToken(f.prm.SessionToken)
|
2020-10-21 15:11:19 +00:00
|
|
|
f.obj.SetCreationEpoch(curEpoch)
|
2020-09-16 14:46:31 +00:00
|
|
|
|
2020-12-18 10:39:44 +00:00
|
|
|
var (
|
|
|
|
parID *objectSDK.ID
|
|
|
|
parHdr *objectSDK.Object
|
|
|
|
)
|
2020-09-16 14:46:31 +00:00
|
|
|
|
2020-12-18 10:39:44 +00:00
|
|
|
if par := f.obj.Parent(); par != nil && par.Signature() == nil {
|
2020-09-16 14:46:31 +00:00
|
|
|
rawPar := objectSDK.NewRawFromV2(par.ToV2())
|
|
|
|
|
2020-10-21 15:10:22 +00:00
|
|
|
rawPar.SetSessionToken(f.prm.SessionToken)
|
2020-10-21 15:11:19 +00:00
|
|
|
rawPar.SetCreationEpoch(curEpoch)
|
2020-09-29 12:41:29 +00:00
|
|
|
|
2020-10-21 15:10:22 +00:00
|
|
|
if err := objectSDK.SetIDWithSignature(f.prm.Key, rawPar); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not finalize parent object: %w", err)
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 09:43:52 +00:00
|
|
|
parID = rawPar.ID()
|
2020-12-18 10:39:44 +00:00
|
|
|
parHdr = rawPar.Object()
|
2020-09-16 14:46:31 +00:00
|
|
|
|
2020-12-18 10:39:44 +00:00
|
|
|
f.obj.SetParent(parHdr)
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 15:10:22 +00:00
|
|
|
if err := objectSDK.SetIDWithSignature(f.prm.Key, f.obj.SDK()); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not finalize object: %w", err)
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 15:10:22 +00:00
|
|
|
if err := f.prm.NextTarget.WriteHeader(f.obj); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not write header to next target: %w", err)
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 15:10:22 +00:00
|
|
|
if _, err := f.prm.NextTarget.Close(); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not close next target: %w", err)
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new(AccessIdentifiers).
|
2020-11-16 09:43:52 +00:00
|
|
|
WithSelfID(f.obj.ID()).
|
2020-12-18 10:39:44 +00:00
|
|
|
WithParentID(parID).
|
|
|
|
WithParent(parHdr), nil
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|