Fix list package and some storage stuff

This commit is contained in:
mcrakhman 2022-09-12 21:25:20 +02:00 committed by Mikhail Iudin
parent e743e34849
commit c6acf63977
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
8 changed files with 53 additions and 146 deletions

View File

@ -23,9 +23,9 @@ func TestAclList_ACLState_UserInviteAndJoin(t *testing.T) {
idC := keychain.GetIdentity("C") idC := keychain.GetIdentity("C")
// checking final state // checking final state
assert.Equal(t, aclpb.ACLChange_Admin, aclList.ACLState().GetUserStates()[idA].Permissions) assert.Equal(t, aclpb.ACLUserPermissions_Admin, aclList.ACLState().GetUserStates()[idA].Permissions)
assert.Equal(t, aclpb.ACLChange_Writer, aclList.ACLState().GetUserStates()[idB].Permissions) assert.Equal(t, aclpb.ACLUserPermissions_Writer, aclList.ACLState().GetUserStates()[idB].Permissions)
assert.Equal(t, aclpb.ACLChange_Reader, aclList.ACLState().GetUserStates()[idC].Permissions) assert.Equal(t, aclpb.ACLUserPermissions_Reader, aclList.ACLState().GetUserStates()[idC].Permissions)
assert.Equal(t, aclList.Head().Content.CurrentReadKeyHash, aclList.ACLState().CurrentReadKeyHash()) assert.Equal(t, aclList.Head().Content.CurrentReadKeyHash, aclList.ACLState().CurrentReadKeyHash())
var records []*ACLRecord 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.NoError(t, err, "should have no error with permissions of B in the record 2")
assert.Equal(t, UserPermissionPair{ assert.Equal(t, UserPermissionPair{
Identity: idB, Identity: idB,
Permission: aclpb.ACLChange_Writer, Permission: aclpb.ACLUserPermissions_Writer,
}, perm) }, perm)
} }
@ -62,8 +62,8 @@ func TestAclList_ACLState_UserJoinAndRemove(t *testing.T) {
idC := keychain.GetIdentity("C") idC := keychain.GetIdentity("C")
// checking final state // checking final state
assert.Equal(t, aclpb.ACLChange_Admin, aclList.ACLState().GetUserStates()[idA].Permissions) assert.Equal(t, aclpb.ACLUserPermissions_Admin, aclList.ACLState().GetUserStates()[idA].Permissions)
assert.Equal(t, aclpb.ACLChange_Reader, aclList.ACLState().GetUserStates()[idC].Permissions) assert.Equal(t, aclpb.ACLUserPermissions_Reader, aclList.ACLState().GetUserStates()[idC].Permissions)
assert.Equal(t, aclList.Head().Content.CurrentReadKeyHash, aclList.ACLState().CurrentReadKeyHash()) assert.Equal(t, aclList.Head().Content.CurrentReadKeyHash, aclList.ACLState().CurrentReadKeyHash())
_, exists := aclList.ACLState().GetUserStates()[idB] _, 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.NoError(t, err, "should have no error with permissions of B in the record 2")
assert.Equal(t, UserPermissionPair{ assert.Equal(t, UserPermissionPair{
Identity: idB, Identity: idB,
Permission: aclpb.ACLChange_Writer, Permission: aclpb.ACLUserPermissions_Writer,
}, perm) }, perm)
_, err = aclList.ACLState().PermissionsAtRecord(records[3].Id, idB) _, err = aclList.ACLState().PermissionsAtRecord(records[3].Id, idB)

View File

@ -7,6 +7,7 @@ import (
type ListStorage interface { type ListStorage interface {
Storage Storage
Header() (*aclpb.ACLHeader, error)
Head() (*aclpb.RawACLRecord, error) Head() (*aclpb.RawACLRecord, error)
GetRawRecord(ctx context.Context, id string) (*aclpb.RawACLRecord, error) GetRawRecord(ctx context.Context, id string) (*aclpb.RawACLRecord, error)

View File

@ -1,8 +1,5 @@
package storage package storage
import "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/aclpb"
type Storage interface { type Storage interface {
ID() (string, error) ID() (string, error)
Header() (*aclpb.Header, error)
} }

View File

@ -7,6 +7,7 @@ import (
type TreeStorage interface { type TreeStorage interface {
Storage Storage
Header() (*aclpb.TreeHeader, error)
Heads() ([]string, error) Heads() ([]string, error)
SetHeads(heads []string) error SetHeads(heads []string) error

View File

@ -99,12 +99,14 @@ func (k *Keychain) AddSigningKey(key *Key) {
} }
k.SigningKeys[key.Name] = newPrivKey k.SigningKeys[key.Name] = newPrivKey
res, err := k.coder.EncodeToString(pubKey) rawPubKey, err := pubKey.Raw()
if err != nil { if err != nil {
panic(err) panic(err)
} }
k.SigningKeysByIdentity[res] = newPrivKey encoded := string(rawPubKey)
k.GeneratedIdentities[key.Name] = res
k.SigningKeysByIdentity[encoded] = newPrivKey
k.GeneratedIdentities[key.Name] = encoded
} }
func (k *Keychain) AddReadKey(key *Key) { func (k *Keychain) AddReadKey(key *Key) {

View File

@ -23,7 +23,7 @@ type ACLListStorageBuilder struct {
rawRecords []*aclpb.RawACLRecord rawRecords []*aclpb.RawACLRecord
indexes map[string]int indexes map[string]int
keychain *Keychain keychain *Keychain
header *aclpb.Header header *aclpb.ACLHeader
id string id string
} }
@ -64,7 +64,7 @@ func (t *ACLListStorageBuilder) createRaw(rec *aclpb.ACLRecord) *aclpb.RawACLRec
panic("should be able to marshal final acl message!") 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 { if err != nil {
panic("should be able to sign final acl message!") 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 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 return t.header, nil
} }
@ -134,26 +134,26 @@ func (t *ACLListStorageBuilder) Parse(tree *YMLList) {
func (t *ACLListStorageBuilder) parseRecord(rec *Record, prevId string) *aclpb.ACLRecord { func (t *ACLListStorageBuilder) parseRecord(rec *Record, prevId string) *aclpb.ACLRecord {
k := t.keychain.GetKey(rec.ReadKey).(*SymKey) k := t.keychain.GetKey(rec.ReadKey).(*SymKey)
var aclChangeContents []*aclpb.ACLChangeACLContentValue var aclChangeContents []*aclpb.ACLContentValue
for _, ch := range rec.AclChanges { for _, ch := range rec.AclChanges {
aclChangeContent := t.parseACLChange(ch) aclChangeContent := t.parseACLChange(ch)
aclChangeContents = append(aclChangeContents, aclChangeContent) aclChangeContents = append(aclChangeContents, aclChangeContent)
} }
data := &aclpb.ACLChangeACLData{ data := &aclpb.ACLData{
AclContent: aclChangeContents, AclContent: aclChangeContents,
} }
bytes, _ := data.Marshal() bytes, _ := data.Marshal()
return &aclpb.ACLRecord{ return &aclpb.ACLRecord{
PrevId: prevId, PrevId: prevId,
Identity: t.keychain.GetIdentity(rec.Identity), Identity: []byte(t.keychain.GetIdentity(rec.Identity)),
Data: bytes, Data: bytes,
CurrentReadKeyHash: k.Hash, CurrentReadKeyHash: k.Hash,
Timestamp: time.Now().Unix(), Timestamp: time.Now().Unix(),
} }
} }
func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACLChangeACLContentValue) { func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACLContentValue) {
switch { switch {
case ch.UserAdd != nil: case ch.UserAdd != nil:
add := ch.UserAdd add := ch.UserAdd
@ -161,10 +161,10 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACL
encKey := t.keychain.GetKey(add.EncryptionKey).(encryptionkey.PrivKey) encKey := t.keychain.GetKey(add.EncryptionKey).(encryptionkey.PrivKey)
rawKey, _ := encKey.GetPublic().Raw() rawKey, _ := encKey.GetPublic().Raw()
convCh = &aclpb.ACLChangeACLContentValue{ convCh = &aclpb.ACLContentValue{
Value: &aclpb.ACLChangeACLContentValueValueOfUserAdd{ Value: &aclpb.ACLContentValue_UserAdd{
UserAdd: &aclpb.ACLUserPermissionsAdd{ UserAdd: &aclpb.ACLUserAdd{
Identity: t.keychain.GetIdentity(add.Identity), Identity: []byte(t.keychain.GetIdentity(add.Identity)),
EncryptionKey: rawKey, EncryptionKey: rawKey,
EncryptedReadKeys: t.encryptReadKeys(add.EncryptedReadKeys, encKey), EncryptedReadKeys: t.encryptReadKeys(add.EncryptedReadKeys, encKey),
Permissions: t.convertPermission(add.Permission), Permissions: t.convertPermission(add.Permission),
@ -185,10 +185,10 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACL
panic(err) panic(err)
} }
convCh = &aclpb.ACLChangeACLContentValue{ convCh = &aclpb.ACLContentValue{
Value: &aclpb.ACLChangeACLContentValueValueOfUserJoin{ Value: &aclpb.ACLContentValue_UserJoin{
UserJoin: &aclpb.ACLUserPermissionsJoin{ UserJoin: &aclpb.ACLUserJoin{
Identity: t.keychain.GetIdentity(join.Identity), Identity: []byte(t.keychain.GetIdentity(join.Identity)),
EncryptionKey: rawKey, EncryptionKey: rawKey,
AcceptSignature: signature, AcceptSignature: signature,
UserInviteId: join.InviteId, UserInviteId: join.InviteId,
@ -203,9 +203,9 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACL
GetKey(invite.EncryptionKey).(encryptionkey.PrivKey) GetKey(invite.EncryptionKey).(encryptionkey.PrivKey)
rawEncKey, _ := encKey.GetPublic().Raw() rawEncKey, _ := encKey.GetPublic().Raw()
convCh = &aclpb.ACLChangeACLContentValue{ convCh = &aclpb.ACLContentValue{
Value: &aclpb.ACLChangeACLContentValueValueOfUserInvite{ Value: &aclpb.ACLContentValue_UserInvite{
UserInvite: &aclpb.ACLUserPermissionsInvite{ UserInvite: &aclpb.ACLUserInvite{
AcceptPublicKey: rawAcceptKey, AcceptPublicKey: rawAcceptKey,
EncryptPublicKey: rawEncKey, EncryptPublicKey: rawEncKey,
EncryptedReadKeys: t.encryptReadKeys(invite.EncryptedReadKeys, encKey), EncryptedReadKeys: t.encryptReadKeys(invite.EncryptedReadKeys, encKey),
@ -217,10 +217,10 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACL
case ch.UserConfirm != nil: case ch.UserConfirm != nil:
confirm := ch.UserConfirm confirm := ch.UserConfirm
convCh = &aclpb.ACLChangeACLContentValue{ convCh = &aclpb.ACLContentValue{
Value: &aclpb.ACLChangeACLContentValueValueOfUserConfirm{ Value: &aclpb.ACLContentValue_UserConfirm{
UserConfirm: &aclpb.ACLUserPermissionsConfirm{ UserConfirm: &aclpb.ACLUserConfirm{
Identity: t.keychain.GetIdentity(confirm.Identity), Identity: []byte(t.keychain.GetIdentity(confirm.Identity)),
UserAddId: confirm.UserAddId, UserAddId: confirm.UserAddId,
}, },
}, },
@ -228,10 +228,10 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACL
case ch.UserPermissionChange != nil: case ch.UserPermissionChange != nil:
permissionChange := ch.UserPermissionChange permissionChange := ch.UserPermissionChange
convCh = &aclpb.ACLChangeACLContentValue{ convCh = &aclpb.ACLContentValue{
Value: &aclpb.ACLChangeACLContentValueValueOfUserPermissionChange{ Value: &aclpb.ACLContentValue_UserPermissionChange{
UserPermissionChange: &aclpb.ACLUserPermissionsPermissionChange{ UserPermissionChange: &aclpb.ACLUserPermissionChange{
Identity: t.keychain.GetIdentity(permissionChange.Identity), Identity: []byte(t.keychain.GetIdentity(permissionChange.Identity)),
Permissions: t.convertPermission(permissionChange.Permission), 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) newReadKey := t.keychain.GetKey(remove.NewReadKey).(*SymKey)
var replaces []*aclpb.ACLChangeReadKeyReplace var replaces []*aclpb.ACLReadKeyReplace
for _, id := range remove.IdentitiesLeft { for _, id := range remove.IdentitiesLeft {
identity := t.keychain.GetIdentity(id) identity := t.keychain.GetIdentity(id)
encKey := t.keychain.EncryptionKeys[id] encKey := t.keychain.EncryptionKeys[id]
@ -250,17 +250,17 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACL
if err != nil { if err != nil {
panic(err) panic(err)
} }
replaces = append(replaces, &aclpb.ACLChangeReadKeyReplace{ replaces = append(replaces, &aclpb.ACLReadKeyReplace{
Identity: identity, Identity: []byte(identity),
EncryptionKey: rawEncKey, EncryptionKey: rawEncKey,
EncryptedReadKey: encReadKey, EncryptedReadKey: encReadKey,
}) })
} }
convCh = &aclpb.ACLChangeACLContentValue{ convCh = &aclpb.ACLContentValue{
Value: &aclpb.ACLChangeACLContentValueValueOfUserRemove{ Value: &aclpb.ACLContentValue_UserRemove{
UserRemove: &aclpb.ACLUserPermissionsRemove{ UserRemove: &aclpb.ACLUserRemove{
Identity: t.keychain.GetIdentity(remove.RemovedIdentity), Identity: []byte(t.keychain.GetIdentity(remove.RemovedIdentity)),
ReadKeyReplaces: replaces, ReadKeyReplaces: replaces,
}, },
}, },
@ -289,11 +289,11 @@ func (t *ACLListStorageBuilder) encryptReadKeys(keys []string, encKey encryption
func (t *ACLListStorageBuilder) convertPermission(perm string) aclpb.ACLUserPermissions { func (t *ACLListStorageBuilder) convertPermission(perm string) aclpb.ACLUserPermissions {
switch perm { switch perm {
case "admin": case "admin":
return aclpb.ACLChange_Admin return aclpb.ACLUserPermissions_Admin
case "writer": case "writer":
return aclpb.ACLChange_Writer return aclpb.ACLUserPermissions_Writer
case "reader": case "reader":
return aclpb.ACLChange_Reader return aclpb.ACLUserPermissions_Reader
default: default:
panic(fmt.Sprintf("incorrect permission: %s", perm)) 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() { func (t *ACLListStorageBuilder) createHeaderAndId() {
t.header = &aclpb.Header{ t.header = &aclpb.ACLHeader{
FirstId: t.rawRecords[0].Id, FirstId: t.rawRecords[0].Id,
AclListId: "",
WorkspaceId: "",
DocType: aclpb.Header_ACL,
} }
bytes, _ := t.header.Marshal() bytes, _ := t.header.Marshal()
id, _ := cid.NewCIDFromBytes(bytes) id, _ := cid.NewCIDFromBytes(bytes)

View File

@ -37,7 +37,7 @@ func (t *ACLListStorageBuilder) Graph() (string, error) {
style := "solid" style := "solid"
var chSymbs []string var chSymbs []string
aclData := &aclpb.ACLChangeACLData{} aclData := &aclpb.ACLData{}
err := proto.Unmarshal(r.GetData(), aclData) err := proto.Unmarshal(r.GetData(), aclData)
if err != nil { if err != nil {
return err return err

View File

@ -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]
}
}
})
})
}