mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-10 10:00:49 +09:00
Fix list package and some storage stuff
This commit is contained in:
parent
e743e34849
commit
c6acf63977
8 changed files with 53 additions and 146 deletions
|
@ -23,9 +23,9 @@ func TestAclList_ACLState_UserInviteAndJoin(t *testing.T) {
|
|||
idC := keychain.GetIdentity("C")
|
||||
|
||||
// checking final state
|
||||
assert.Equal(t, aclpb.ACLChange_Admin, aclList.ACLState().GetUserStates()[idA].Permissions)
|
||||
assert.Equal(t, aclpb.ACLChange_Writer, aclList.ACLState().GetUserStates()[idB].Permissions)
|
||||
assert.Equal(t, aclpb.ACLChange_Reader, aclList.ACLState().GetUserStates()[idC].Permissions)
|
||||
assert.Equal(t, aclpb.ACLUserPermissions_Admin, aclList.ACLState().GetUserStates()[idA].Permissions)
|
||||
assert.Equal(t, aclpb.ACLUserPermissions_Writer, aclList.ACLState().GetUserStates()[idB].Permissions)
|
||||
assert.Equal(t, aclpb.ACLUserPermissions_Reader, aclList.ACLState().GetUserStates()[idC].Permissions)
|
||||
assert.Equal(t, aclList.Head().Content.CurrentReadKeyHash, aclList.ACLState().CurrentReadKeyHash())
|
||||
|
||||
var records []*ACLRecord
|
||||
|
@ -44,7 +44,7 @@ func TestAclList_ACLState_UserInviteAndJoin(t *testing.T) {
|
|||
assert.NoError(t, err, "should have no error with permissions of B in the record 2")
|
||||
assert.Equal(t, UserPermissionPair{
|
||||
Identity: idB,
|
||||
Permission: aclpb.ACLChange_Writer,
|
||||
Permission: aclpb.ACLUserPermissions_Writer,
|
||||
}, perm)
|
||||
}
|
||||
|
||||
|
@ -62,8 +62,8 @@ func TestAclList_ACLState_UserJoinAndRemove(t *testing.T) {
|
|||
idC := keychain.GetIdentity("C")
|
||||
|
||||
// checking final state
|
||||
assert.Equal(t, aclpb.ACLChange_Admin, aclList.ACLState().GetUserStates()[idA].Permissions)
|
||||
assert.Equal(t, aclpb.ACLChange_Reader, aclList.ACLState().GetUserStates()[idC].Permissions)
|
||||
assert.Equal(t, aclpb.ACLUserPermissions_Admin, aclList.ACLState().GetUserStates()[idA].Permissions)
|
||||
assert.Equal(t, aclpb.ACLUserPermissions_Reader, aclList.ACLState().GetUserStates()[idC].Permissions)
|
||||
assert.Equal(t, aclList.Head().Content.CurrentReadKeyHash, aclList.ACLState().CurrentReadKeyHash())
|
||||
|
||||
_, exists := aclList.ACLState().GetUserStates()[idB]
|
||||
|
@ -84,7 +84,7 @@ func TestAclList_ACLState_UserJoinAndRemove(t *testing.T) {
|
|||
assert.NoError(t, err, "should have no error with permissions of B in the record 2")
|
||||
assert.Equal(t, UserPermissionPair{
|
||||
Identity: idB,
|
||||
Permission: aclpb.ACLChange_Writer,
|
||||
Permission: aclpb.ACLUserPermissions_Writer,
|
||||
}, perm)
|
||||
|
||||
_, err = aclList.ACLState().PermissionsAtRecord(records[3].Id, idB)
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
type ListStorage interface {
|
||||
Storage
|
||||
Header() (*aclpb.ACLHeader, error)
|
||||
Head() (*aclpb.RawACLRecord, error)
|
||||
|
||||
GetRawRecord(ctx context.Context, id string) (*aclpb.RawACLRecord, error)
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
package storage
|
||||
|
||||
import "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/aclpb"
|
||||
|
||||
type Storage interface {
|
||||
ID() (string, error)
|
||||
Header() (*aclpb.Header, error)
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
type TreeStorage interface {
|
||||
Storage
|
||||
Header() (*aclpb.TreeHeader, error)
|
||||
Heads() ([]string, error)
|
||||
SetHeads(heads []string) error
|
||||
|
||||
|
|
|
@ -99,12 +99,14 @@ func (k *Keychain) AddSigningKey(key *Key) {
|
|||
}
|
||||
|
||||
k.SigningKeys[key.Name] = newPrivKey
|
||||
res, err := k.coder.EncodeToString(pubKey)
|
||||
rawPubKey, err := pubKey.Raw()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
k.SigningKeysByIdentity[res] = newPrivKey
|
||||
k.GeneratedIdentities[key.Name] = res
|
||||
encoded := string(rawPubKey)
|
||||
|
||||
k.SigningKeysByIdentity[encoded] = newPrivKey
|
||||
k.GeneratedIdentities[key.Name] = encoded
|
||||
}
|
||||
|
||||
func (k *Keychain) AddReadKey(key *Key) {
|
||||
|
|
|
@ -23,7 +23,7 @@ type ACLListStorageBuilder struct {
|
|||
rawRecords []*aclpb.RawACLRecord
|
||||
indexes map[string]int
|
||||
keychain *Keychain
|
||||
header *aclpb.Header
|
||||
header *aclpb.ACLHeader
|
||||
id string
|
||||
}
|
||||
|
||||
|
@ -64,7 +64,7 @@ func (t *ACLListStorageBuilder) createRaw(rec *aclpb.ACLRecord) *aclpb.RawACLRec
|
|||
panic("should be able to marshal final acl message!")
|
||||
}
|
||||
|
||||
signature, err := t.keychain.SigningKeysByIdentity[rec.Identity].Sign(aclMarshaled)
|
||||
signature, err := t.keychain.SigningKeysByIdentity[string(rec.Identity)].Sign(aclMarshaled)
|
||||
if err != nil {
|
||||
panic("should be able to sign final acl message!")
|
||||
}
|
||||
|
@ -86,7 +86,7 @@ func (t *ACLListStorageBuilder) Head() (*aclpb.RawACLRecord, error) {
|
|||
return t.getRecord(len(t.records) - 1), nil
|
||||
}
|
||||
|
||||
func (t *ACLListStorageBuilder) Header() (*aclpb.Header, error) {
|
||||
func (t *ACLListStorageBuilder) Header() (*aclpb.ACLHeader, error) {
|
||||
return t.header, nil
|
||||
}
|
||||
|
||||
|
@ -134,26 +134,26 @@ func (t *ACLListStorageBuilder) Parse(tree *YMLList) {
|
|||
|
||||
func (t *ACLListStorageBuilder) parseRecord(rec *Record, prevId string) *aclpb.ACLRecord {
|
||||
k := t.keychain.GetKey(rec.ReadKey).(*SymKey)
|
||||
var aclChangeContents []*aclpb.ACLChangeACLContentValue
|
||||
var aclChangeContents []*aclpb.ACLContentValue
|
||||
for _, ch := range rec.AclChanges {
|
||||
aclChangeContent := t.parseACLChange(ch)
|
||||
aclChangeContents = append(aclChangeContents, aclChangeContent)
|
||||
}
|
||||
data := &aclpb.ACLChangeACLData{
|
||||
data := &aclpb.ACLData{
|
||||
AclContent: aclChangeContents,
|
||||
}
|
||||
bytes, _ := data.Marshal()
|
||||
|
||||
return &aclpb.ACLRecord{
|
||||
PrevId: prevId,
|
||||
Identity: t.keychain.GetIdentity(rec.Identity),
|
||||
Identity: []byte(t.keychain.GetIdentity(rec.Identity)),
|
||||
Data: bytes,
|
||||
CurrentReadKeyHash: k.Hash,
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACLChangeACLContentValue) {
|
||||
func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACLContentValue) {
|
||||
switch {
|
||||
case ch.UserAdd != nil:
|
||||
add := ch.UserAdd
|
||||
|
@ -161,10 +161,10 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACL
|
|||
encKey := t.keychain.GetKey(add.EncryptionKey).(encryptionkey.PrivKey)
|
||||
rawKey, _ := encKey.GetPublic().Raw()
|
||||
|
||||
convCh = &aclpb.ACLChangeACLContentValue{
|
||||
Value: &aclpb.ACLChangeACLContentValueValueOfUserAdd{
|
||||
UserAdd: &aclpb.ACLUserPermissionsAdd{
|
||||
Identity: t.keychain.GetIdentity(add.Identity),
|
||||
convCh = &aclpb.ACLContentValue{
|
||||
Value: &aclpb.ACLContentValue_UserAdd{
|
||||
UserAdd: &aclpb.ACLUserAdd{
|
||||
Identity: []byte(t.keychain.GetIdentity(add.Identity)),
|
||||
EncryptionKey: rawKey,
|
||||
EncryptedReadKeys: t.encryptReadKeys(add.EncryptedReadKeys, encKey),
|
||||
Permissions: t.convertPermission(add.Permission),
|
||||
|
@ -185,10 +185,10 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACL
|
|||
panic(err)
|
||||
}
|
||||
|
||||
convCh = &aclpb.ACLChangeACLContentValue{
|
||||
Value: &aclpb.ACLChangeACLContentValueValueOfUserJoin{
|
||||
UserJoin: &aclpb.ACLUserPermissionsJoin{
|
||||
Identity: t.keychain.GetIdentity(join.Identity),
|
||||
convCh = &aclpb.ACLContentValue{
|
||||
Value: &aclpb.ACLContentValue_UserJoin{
|
||||
UserJoin: &aclpb.ACLUserJoin{
|
||||
Identity: []byte(t.keychain.GetIdentity(join.Identity)),
|
||||
EncryptionKey: rawKey,
|
||||
AcceptSignature: signature,
|
||||
UserInviteId: join.InviteId,
|
||||
|
@ -203,9 +203,9 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACL
|
|||
GetKey(invite.EncryptionKey).(encryptionkey.PrivKey)
|
||||
rawEncKey, _ := encKey.GetPublic().Raw()
|
||||
|
||||
convCh = &aclpb.ACLChangeACLContentValue{
|
||||
Value: &aclpb.ACLChangeACLContentValueValueOfUserInvite{
|
||||
UserInvite: &aclpb.ACLUserPermissionsInvite{
|
||||
convCh = &aclpb.ACLContentValue{
|
||||
Value: &aclpb.ACLContentValue_UserInvite{
|
||||
UserInvite: &aclpb.ACLUserInvite{
|
||||
AcceptPublicKey: rawAcceptKey,
|
||||
EncryptPublicKey: rawEncKey,
|
||||
EncryptedReadKeys: t.encryptReadKeys(invite.EncryptedReadKeys, encKey),
|
||||
|
@ -217,10 +217,10 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACL
|
|||
case ch.UserConfirm != nil:
|
||||
confirm := ch.UserConfirm
|
||||
|
||||
convCh = &aclpb.ACLChangeACLContentValue{
|
||||
Value: &aclpb.ACLChangeACLContentValueValueOfUserConfirm{
|
||||
UserConfirm: &aclpb.ACLUserPermissionsConfirm{
|
||||
Identity: t.keychain.GetIdentity(confirm.Identity),
|
||||
convCh = &aclpb.ACLContentValue{
|
||||
Value: &aclpb.ACLContentValue_UserConfirm{
|
||||
UserConfirm: &aclpb.ACLUserConfirm{
|
||||
Identity: []byte(t.keychain.GetIdentity(confirm.Identity)),
|
||||
UserAddId: confirm.UserAddId,
|
||||
},
|
||||
},
|
||||
|
@ -228,10 +228,10 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACL
|
|||
case ch.UserPermissionChange != nil:
|
||||
permissionChange := ch.UserPermissionChange
|
||||
|
||||
convCh = &aclpb.ACLChangeACLContentValue{
|
||||
Value: &aclpb.ACLChangeACLContentValueValueOfUserPermissionChange{
|
||||
UserPermissionChange: &aclpb.ACLUserPermissionsPermissionChange{
|
||||
Identity: t.keychain.GetIdentity(permissionChange.Identity),
|
||||
convCh = &aclpb.ACLContentValue{
|
||||
Value: &aclpb.ACLContentValue_UserPermissionChange{
|
||||
UserPermissionChange: &aclpb.ACLUserPermissionChange{
|
||||
Identity: []byte(t.keychain.GetIdentity(permissionChange.Identity)),
|
||||
Permissions: t.convertPermission(permissionChange.Permission),
|
||||
},
|
||||
},
|
||||
|
@ -241,7 +241,7 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACL
|
|||
|
||||
newReadKey := t.keychain.GetKey(remove.NewReadKey).(*SymKey)
|
||||
|
||||
var replaces []*aclpb.ACLChangeReadKeyReplace
|
||||
var replaces []*aclpb.ACLReadKeyReplace
|
||||
for _, id := range remove.IdentitiesLeft {
|
||||
identity := t.keychain.GetIdentity(id)
|
||||
encKey := t.keychain.EncryptionKeys[id]
|
||||
|
@ -250,17 +250,17 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACL
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
replaces = append(replaces, &aclpb.ACLChangeReadKeyReplace{
|
||||
Identity: identity,
|
||||
replaces = append(replaces, &aclpb.ACLReadKeyReplace{
|
||||
Identity: []byte(identity),
|
||||
EncryptionKey: rawEncKey,
|
||||
EncryptedReadKey: encReadKey,
|
||||
})
|
||||
}
|
||||
|
||||
convCh = &aclpb.ACLChangeACLContentValue{
|
||||
Value: &aclpb.ACLChangeACLContentValueValueOfUserRemove{
|
||||
UserRemove: &aclpb.ACLUserPermissionsRemove{
|
||||
Identity: t.keychain.GetIdentity(remove.RemovedIdentity),
|
||||
convCh = &aclpb.ACLContentValue{
|
||||
Value: &aclpb.ACLContentValue_UserRemove{
|
||||
UserRemove: &aclpb.ACLUserRemove{
|
||||
Identity: []byte(t.keychain.GetIdentity(remove.RemovedIdentity)),
|
||||
ReadKeyReplaces: replaces,
|
||||
},
|
||||
},
|
||||
|
@ -289,11 +289,11 @@ func (t *ACLListStorageBuilder) encryptReadKeys(keys []string, encKey encryption
|
|||
func (t *ACLListStorageBuilder) convertPermission(perm string) aclpb.ACLUserPermissions {
|
||||
switch perm {
|
||||
case "admin":
|
||||
return aclpb.ACLChange_Admin
|
||||
return aclpb.ACLUserPermissions_Admin
|
||||
case "writer":
|
||||
return aclpb.ACLChange_Writer
|
||||
return aclpb.ACLUserPermissions_Writer
|
||||
case "reader":
|
||||
return aclpb.ACLChange_Reader
|
||||
return aclpb.ACLUserPermissions_Reader
|
||||
default:
|
||||
panic(fmt.Sprintf("incorrect permission: %s", perm))
|
||||
}
|
||||
|
@ -310,11 +310,8 @@ func (t *ACLListStorageBuilder) traverseFromHead(f func(rec *aclpb.ACLRecord, id
|
|||
}
|
||||
|
||||
func (t *ACLListStorageBuilder) createHeaderAndId() {
|
||||
t.header = &aclpb.Header{
|
||||
FirstId: t.rawRecords[0].Id,
|
||||
AclListId: "",
|
||||
WorkspaceId: "",
|
||||
DocType: aclpb.Header_ACL,
|
||||
t.header = &aclpb.ACLHeader{
|
||||
FirstId: t.rawRecords[0].Id,
|
||||
}
|
||||
bytes, _ := t.header.Marshal()
|
||||
id, _ := cid.NewCIDFromBytes(bytes)
|
||||
|
|
|
@ -37,7 +37,7 @@ func (t *ACLListStorageBuilder) Graph() (string, error) {
|
|||
style := "solid"
|
||||
|
||||
var chSymbs []string
|
||||
aclData := &aclpb.ACLChangeACLData{}
|
||||
aclData := &aclpb.ACLData{}
|
||||
err := proto.Unmarshal(r.GetData(), aclData)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -1,91 +0,0 @@
|
|||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func BenchmarkHashes(b *testing.B) {
|
||||
genRandomBytes := func() [][]byte {
|
||||
var res [][]byte
|
||||
s := rand.NewSource(time.Now().Unix())
|
||||
r := rand.New(s)
|
||||
for i := 0; i < 10000; i++ {
|
||||
var newBytes []byte
|
||||
for j := 0; j < 64; j++ {
|
||||
newBytes = append(newBytes, byte(r.Intn(256)))
|
||||
}
|
||||
res = append(res, newBytes)
|
||||
}
|
||||
return res
|
||||
}
|
||||
makeStrings := func(input [][]byte) []string {
|
||||
var res []string
|
||||
for _, bytes := range input {
|
||||
res = append(res, string(bytes))
|
||||
}
|
||||
return res
|
||||
}
|
||||
res := genRandomBytes()
|
||||
stringRes := makeStrings(res)
|
||||
stringMap := map[string]struct{}{}
|
||||
b.Run("string bytes hash map write", func(b *testing.B) {
|
||||
stringMap = map[string]struct{}{}
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, bytes := range res {
|
||||
stringMap[string(bytes)] = struct{}{}
|
||||
}
|
||||
}
|
||||
b.Run("hash map read", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, bytes := range res {
|
||||
_, _ = stringMap[string(bytes)]
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
b.Run("compare byte slices as strings", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, bytes := range res {
|
||||
if string(bytes) == string(bytes) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
b.Run("compare byte slices", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, bt := range res {
|
||||
if bytes.Compare(bt, bt) == 0 {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
b.Run("compare strings", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, st := range stringRes {
|
||||
if st == st {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
b.Run("string hash map write", func(b *testing.B) {
|
||||
stringMap = map[string]struct{}{}
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, str := range stringRes {
|
||||
stringMap[str] = struct{}{}
|
||||
}
|
||||
}
|
||||
b.Run("hash map read", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, str := range stringRes {
|
||||
_, _ = stringMap[str]
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue