-
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.
node/object/put: exchange meta signatures during replication
Initial replication requires nodes to sign object's main meta information and respond with it. Meta information is not sent on wire and treated as a fixed ordered NEO's map. Signatures are verified, not stored/send anywhere yet. It follows the recent API extension: nspcc-dev/neofs-api#299. Further, this extension is planned to have a contract adoption: nspcc-dev/neofs-contract#413 and nspcc-dev/neofs-contract#414. Closes #2876. Signed-off-by: Pavel Karpy <[email protected]>
- Loading branch information
Showing
13 changed files
with
350 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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 = 6 // it is also a number of fields | ||
) | ||
|
||
const ( | ||
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): | ||
// | ||
// "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) []byte { | ||
kvs := []stackitem.MapElement{ | ||
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,65 @@ | ||
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) { | ||
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) | ||
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, cidKey, string(mm[0].Key.Value().([]byte))) | ||
require.Equal(t, cID[:], mm[0].Value.Value().([]byte)) | ||
|
||
require.Equal(t, oidKey, string(mm[1].Key.Value().([]byte))) | ||
require.Equal(t, oID[:], 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, deletedKey, string(mm[3].Key.Value().([]byte))) | ||
require.Equal(t, deleted, stackItemToOIDs(t, mm[3].Value)) | ||
|
||
require.Equal(t, lockedKey, string(mm[4].Key.Value().([]byte))) | ||
require.Equal(t, locked, stackItemToOIDs(t, mm[4].Value)) | ||
|
||
require.Equal(t, validUntilKey, string(mm[5].Key.Value().([]byte))) | ||
require.Equal(t, validUntil+validInterval, mm[5].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
Oops, something went wrong.