Skip to content

Commit

Permalink
node/object/put: add object type to meta-on-chain
Browse files Browse the repository at this point in the history
It is optional since one of the types (REGULAR) is the most expected, and its
missing can be recognizable.

Signed-off-by: Pavel Karpy <[email protected]>
  • Loading branch information
carpawell committed Dec 28, 2024
1 parent 0fa7ad9 commit ce1096b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
8 changes: 7 additions & 1 deletion pkg/core/object/replicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
objectsdk "github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
)

Expand All @@ -21,6 +22,7 @@ const (
previousPartKey = "previousPart"
deletedKey = "deleted"
lockedKey = "locked"
typeKey = "type"
)

// EncodeReplicationMetaInfo uses NEO's map (strict order) serialized format as a raw
Expand All @@ -37,7 +39,8 @@ const (
// "previousPart": [OPTIONAL] _raw_ object ID (32 bytes)
// "deleted": [OPTIONAL] array of _raw_ object IDs
// "locked": [OPTIONAL] array of _raw_ object IDs
func EncodeReplicationMetaInfo(cID cid.ID, oID, firstPart, previousPart oid.ID, pSize uint64,
// "type": [OPTIONAL] object type enumeration
func EncodeReplicationMetaInfo(cID cid.ID, oID, firstPart, previousPart oid.ID, pSize uint64, typ objectsdk.Type,
deleted, locked []oid.ID, vub uint64, magicNumber uint32) []byte {
kvs := []stackitem.MapElement{
kv(cidKey, cID[:]),
Expand All @@ -59,6 +62,9 @@ func EncodeReplicationMetaInfo(cID cid.ID, oID, firstPart, previousPart oid.ID,
if len(locked) > 0 {
kvs = append(kvs, oidsKV(lockedKey, locked))
}
if typ != objectsdk.TypeRegular {
kvs = append(kvs, kv(typeKey, uint32(typ)))
}

result, err := stackitem.Serialize(stackitem.NewMapWithValue(kvs))
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion pkg/core/object/replicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
"github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
"github.com/stretchr/testify/require"
Expand All @@ -24,6 +25,7 @@ type m struct {
prev oid.ID
deleted []oid.ID
locked []oid.ID
typ object.Type
}

func TestMetaInfo(t *testing.T) {
Expand All @@ -37,6 +39,7 @@ func TestMetaInfo(t *testing.T) {
prev: oidtest.ID(),
deleted: oidtest.IDs(10),
locked: oidtest.IDs(10),
typ: object.TypeTombstone,
}

t.Run("full", func(t *testing.T) {
Expand All @@ -49,13 +52,14 @@ func TestMetaInfo(t *testing.T) {
meta.deleted = nil
meta.deleted = nil
meta.locked = nil
meta.typ = object.TypeRegular

testMeta(t, meta, false)
})
}

func testMeta(t *testing.T, m m, full bool) {
raw := EncodeReplicationMetaInfo(m.cID, m.oID, m.first, m.prev, m.size, m.deleted, m.locked, m.vub, m.magic)
raw := EncodeReplicationMetaInfo(m.cID, m.oID, m.first, m.prev, m.size, m.typ, m.deleted, m.locked, m.vub, m.magic)
item, err := stackitem.Deserialize(raw)
require.NoError(t, err)

Expand Down Expand Up @@ -94,6 +98,9 @@ func testMeta(t *testing.T, m m, full bool) {

require.Equal(t, lockedKey, string(mm[8].Key.Value().([]byte)))
require.Equal(t, m.locked, stackItemToOIDs(t, mm[8].Value))

require.Equal(t, typeKey, string(mm[9].Key.Value().([]byte)))
require.Equal(t, int(m.typ), int(mm[9].Value.Value().(*big.Int).Uint64()))
}

func stackItemToOIDs(t *testing.T, value stackitem.Item) []oid.ID {
Expand Down
9 changes: 5 additions & 4 deletions pkg/network/transport/object/grpc/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ func (s *Server) metaInfoSignature(o object.Object) ([]byte, error) {

var deleted []oid.ID
var locked []oid.ID
switch o.Type() {
typ := o.Type()
switch typ {
case object.TypeTombstone:
var t object.Tombstone
err := t.Unmarshal(o.Payload())
Expand All @@ -242,9 +243,9 @@ func (s *Server) metaInfoSignature(o object.Object) ([]byte, error) {
secondBlock := firstBlock + currentEpochDuration
thirdBlock := secondBlock + currentEpochDuration

firstMeta := objectcore.EncodeReplicationMetaInfo(o.GetContainerID(), o.GetID(), firstObj, prevObj, o.PayloadSize(), deleted, locked, firstBlock, s.mNumber)
secondMeta := objectcore.EncodeReplicationMetaInfo(o.GetContainerID(), o.GetID(), firstObj, prevObj, o.PayloadSize(), deleted, locked, secondBlock, s.mNumber)
thirdMeta := objectcore.EncodeReplicationMetaInfo(o.GetContainerID(), o.GetID(), firstObj, prevObj, o.PayloadSize(), deleted, locked, thirdBlock, s.mNumber)
firstMeta := objectcore.EncodeReplicationMetaInfo(o.GetContainerID(), o.GetID(), firstObj, prevObj, o.PayloadSize(), typ, deleted, locked, firstBlock, s.mNumber)
secondMeta := objectcore.EncodeReplicationMetaInfo(o.GetContainerID(), o.GetID(), firstObj, prevObj, o.PayloadSize(), typ, deleted, locked, secondBlock, s.mNumber)
thirdMeta := objectcore.EncodeReplicationMetaInfo(o.GetContainerID(), o.GetID(), firstObj, prevObj, o.PayloadSize(), typ, deleted, locked, thirdBlock, s.mNumber)

var firstSig neofscrypto.Signature
var secondSig neofscrypto.Signature
Expand Down
2 changes: 1 addition & 1 deletion pkg/network/transport/object/grpc/replication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func TestServer_Replicate(t *testing.T) {

require.Equal(t, signer.PublicKeyBytes, sig.PublicKeyBytes())
require.True(t, sig.Verify(objectcore.EncodeReplicationMetaInfo(
o.GetContainerID(), o.GetID(), o.GetFirstID(), o.GetPreviousID(), o.PayloadSize(), nil, nil,
o.GetContainerID(), o.GetID(), o.GetFirstID(), o.GetPreviousID(), o.PayloadSize(), o.Type(), nil, nil,
uint64((123+1+i)*240), mNumber)))

sigsRaw = sigsRaw[:4+l]
Expand Down
5 changes: 3 additions & 2 deletions pkg/services/object/put/distributed.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ func (t *distributedTarget) Close() (oid.ID, error) {

var deletedObjs []oid.ID
var lockedObjs []oid.ID
switch t.objMeta.Type() {
typ := t.objMeta.Type()
switch typ {
case objectSDK.TypeTombstone:
deletedObjs = t.objMeta.Objects()
case objectSDK.TypeLock:
Expand All @@ -159,7 +160,7 @@ func (t *distributedTarget) Close() (oid.ID, error) {
}

expectedVUB := (uint64(t.currentBlock)/t.currentEpochDuration + 2) * t.currentEpochDuration
t.objSharedMeta = object.EncodeReplicationMetaInfo(t.obj.GetContainerID(), t.obj.GetID(), firstObj, prevObj, t.obj.PayloadSize(), deletedObjs,
t.objSharedMeta = object.EncodeReplicationMetaInfo(t.obj.GetContainerID(), t.obj.GetID(), firstObj, prevObj, t.obj.PayloadSize(), typ, deletedObjs,
lockedObjs, expectedVUB, t.networkMagicNumber)
id := t.obj.GetID()
err := t.placementIterator.iterateNodesForObject(id, t.sendObject)
Expand Down

0 comments on commit ce1096b

Please sign in to comment.