Skip to content

Commit

Permalink
node/object/put: allow optional fields
Browse files Browse the repository at this point in the history
Map type allows to be flexible and not rely on positional data interpretation,
so there is no need to put empty `deleted` or `locked` arrays for objects that
do not delete or lock anything.

Signed-off-by: Pavel Karpy <[email protected]>
  • Loading branch information
carpawell committed Dec 25, 2024
1 parent b194cc2 commit 5f01f0e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
30 changes: 17 additions & 13 deletions pkg/core/object/replicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,47 @@ import (
)

const (
currentVersion = 7 // it is also a number of fields
)

const (
networkMagicKey = "network"
// required
cidKey = "cid"
oidKey = "oid"
sizeKey = "size"
deletedKey = "deleted"
lockedKey = "locked"
validUntilKey = "validUntil"
networkMagicKey = "network"

// optional
deletedKey = "deleted"
lockedKey = "locked"
)

// EncodeReplicationMetaInfo uses NEO's map (strict order) serialized format as a raw
// representation of object's meta information.
//
// This (ordered) format is used (keys are strings):
//
// "network": network magic
// "cid": _raw_ container ID (32 bytes)
// "oid": _raw_ object ID (32 bytes)
// "size": payload size
// "deleted": array of _raw_ object IDs
// "locked": array of _raw_ object IDs
// "validUntil": last valid block number for meta information
// "network": network magic
// "deleted": [OPTIONAL] array of _raw_ object IDs
// "locked": [OPTIONAL] array of _raw_ object IDs
//
// Last valid epoch is object's creation epoch + 10.
func EncodeReplicationMetaInfo(cID cid.ID, oID oid.ID, pSize uint64,
deleted, locked []oid.ID, vub uint64, magicNumber uint32) []byte {
kvs := []stackitem.MapElement{
kv(networkMagicKey, magicNumber),
kv(cidKey, cID[:]),
kv(oidKey, oID[:]),
kv(sizeKey, pSize),
oidsKV(deletedKey, deleted),
oidsKV(lockedKey, locked),
kv(validUntilKey, vub),
kv(networkMagicKey, magicNumber),
}

if len(deleted) > 0 {
kvs = append(kvs, oidsKV(deletedKey, deleted))
}
if len(locked) > 0 {
kvs = append(kvs, oidsKV(lockedKey, locked))
}

result, err := stackitem.Serialize(stackitem.NewMapWithValue(kvs))
Expand Down
30 changes: 14 additions & 16 deletions pkg/core/object/replicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,26 @@ func TestMetaInfo(t *testing.T) {
mm, ok := item.Value().([]stackitem.MapElement)
require.True(t, ok)

require.Len(t, mm, currentVersion)
require.Equal(t, cidKey, string(mm[0].Key.Value().([]byte)))
require.Equal(t, cID[:], mm[0].Value.Value().([]byte))

require.Equal(t, networkMagicKey, string(mm[0].Key.Value().([]byte)))
require.Equal(t, network, uint32(mm[0].Value.Value().(*big.Int).Uint64()))
require.Equal(t, oidKey, string(mm[1].Key.Value().([]byte)))
require.Equal(t, oID[:], mm[1].Value.Value().([]byte))

require.Equal(t, cidKey, string(mm[1].Key.Value().([]byte)))
require.Equal(t, cID[:], mm[1].Value.Value().([]byte))
require.Equal(t, sizeKey, string(mm[2].Key.Value().([]byte)))
require.Equal(t, size, mm[2].Value.Value().(*big.Int).Uint64())

require.Equal(t, oidKey, string(mm[2].Key.Value().([]byte)))
require.Equal(t, oID[:], mm[2].Value.Value().([]byte))
require.Equal(t, validUntilKey, string(mm[3].Key.Value().([]byte)))
require.Equal(t, validUntil, mm[3].Value.Value().(*big.Int).Uint64())

require.Equal(t, sizeKey, string(mm[3].Key.Value().([]byte)))
require.Equal(t, size, mm[3].Value.Value().(*big.Int).Uint64())
require.Equal(t, networkMagicKey, string(mm[4].Key.Value().([]byte)))
require.Equal(t, network, uint32(mm[4].Value.Value().(*big.Int).Uint64()))

require.Equal(t, deletedKey, string(mm[4].Key.Value().([]byte)))
require.Equal(t, deleted, stackItemToOIDs(t, mm[4].Value))
require.Equal(t, deletedKey, string(mm[5].Key.Value().([]byte)))
require.Equal(t, deleted, stackItemToOIDs(t, mm[5].Value))

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

require.Equal(t, validUntilKey, string(mm[6].Key.Value().([]byte)))
require.Equal(t, validUntil, mm[6].Value.Value().(*big.Int).Uint64())
require.Equal(t, lockedKey, string(mm[6].Key.Value().([]byte)))
require.Equal(t, locked, stackItemToOIDs(t, mm[6].Value))
}

func stackItemToOIDs(t *testing.T, value stackitem.Item) []oid.ID {
Expand Down

0 comments on commit 5f01f0e

Please sign in to comment.