-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/exchange object meta's signatures (#2928)
- Loading branch information
Showing
18 changed files
with
384 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package object | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem" | ||
cid "github.com/nspcc-dev/neofs-sdk-go/container/id" | ||
oid "github.com/nspcc-dev/neofs-sdk-go/object/id" | ||
) | ||
|
||
const ( | ||
validInterval = 10 // in epochs | ||
currentVersion = 7 // it is also a number of fields | ||
) | ||
|
||
const ( | ||
networkMagicKey = "network" | ||
cidKey = "cid" | ||
oidKey = "oid" | ||
sizeKey = "size" | ||
deletedKey = "deleted" | ||
lockedKey = "locked" | ||
validUntilKey = "validuntil" | ||
) | ||
|
||
// 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 epoch number for meta information | ||
// | ||
// Last valid epoch is object's creation epoch + 10. | ||
func EncodeReplicationMetaInfo(cID cid.ID, oID oid.ID, pSize uint64, | ||
deleted, locked []oid.ID, createdAt 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, createdAt+validInterval), | ||
} | ||
|
||
result, err := stackitem.Serialize(stackitem.NewMapWithValue(kvs)) | ||
if err != nil { | ||
// all the errors in the stackitem relate only cases when it is | ||
// impossible to use serialized values (too many values, unsupported | ||
// types, etc.), unexpected errors at all | ||
panic(fmt.Errorf("unexpected stackitem map serialization failure: %v", err)) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func kv(k string, value any) stackitem.MapElement { | ||
return stackitem.MapElement{ | ||
Key: stackitem.Make(k), | ||
Value: stackitem.Make(value), | ||
} | ||
} | ||
|
||
func oidsKV(fieldKey string, oIDs []oid.ID) stackitem.MapElement { | ||
res := make([]stackitem.Item, 0, len(oIDs)) | ||
for _, oID := range oIDs { | ||
res = append(res, stackitem.NewByteArray(oID[:])) | ||
} | ||
|
||
return kv(fieldKey, res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package object | ||
|
||
import ( | ||
"math/big" | ||
"math/rand/v2" | ||
"testing" | ||
|
||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem" | ||
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" | ||
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" | ||
) | ||
|
||
func TestMetaInfo(t *testing.T) { | ||
network := rand.Uint32() | ||
oID := oidtest.ID() | ||
cID := cidtest.ID() | ||
size := rand.Uint64() | ||
deleted := oidtest.IDs(10) | ||
locked := oidtest.IDs(10) | ||
validUntil := rand.Uint64() | ||
|
||
raw := EncodeReplicationMetaInfo(cID, oID, size, deleted, locked, validUntil, network) | ||
item, err := stackitem.Deserialize(raw) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, stackitem.MapT, item.Type()) | ||
mm, ok := item.Value().([]stackitem.MapElement) | ||
require.True(t, ok) | ||
|
||
require.Len(t, mm, currentVersion) | ||
|
||
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, cidKey, string(mm[1].Key.Value().([]byte))) | ||
require.Equal(t, cID[:], mm[1].Value.Value().([]byte)) | ||
|
||
require.Equal(t, oidKey, string(mm[2].Key.Value().([]byte))) | ||
require.Equal(t, oID[:], mm[2].Value.Value().([]byte)) | ||
|
||
require.Equal(t, sizeKey, string(mm[3].Key.Value().([]byte))) | ||
require.Equal(t, size, mm[3].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, 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+validInterval, mm[6].Value.Value().(*big.Int).Uint64()) | ||
} | ||
|
||
func stackItemToOIDs(t *testing.T, value stackitem.Item) []oid.ID { | ||
value, ok := value.(*stackitem.Array) | ||
require.True(t, ok) | ||
|
||
vv := value.Value().([]stackitem.Item) | ||
res := make([]oid.ID, 0, len(vv)) | ||
|
||
for _, v := range vv { | ||
raw := v.Value().([]byte) | ||
res = append(res, oid.ID(raw)) | ||
} | ||
|
||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.