diff --git a/pkg/core/object/replicate.go b/pkg/core/object/replicate.go index d1fa42ee47..5df0fc79f6 100644 --- a/pkg/core/object/replicate.go +++ b/pkg/core/object/replicate.go @@ -9,17 +9,16 @@ 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 @@ -27,25 +26,30 @@ const ( // // 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)) diff --git a/pkg/core/object/replicate_test.go b/pkg/core/object/replicate_test.go index 22ad851916..39c7521694 100644 --- a/pkg/core/object/replicate_test.go +++ b/pkg/core/object/replicate_test.go @@ -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 {