From ee123efd7ded2f29ab8e1d48f5f10c1a22302f0a Mon Sep 17 00:00:00 2001 From: mcrakhman Date: Fri, 8 Jul 2022 10:19:43 +0200 Subject: [PATCH] WIP change package structure --- {data => acltree}/aclstate.go | 46 +++++++++---------- {data => acltree}/aclstatebuilder.go | 18 ++++---- {data => acltree}/aclstatebuilder_test.go | 22 +++++---- {data => acltree}/acltreebuilder.go | 22 +++++---- {data => acltree}/change.go | 4 +- {data => acltree}/changeloader.go | 24 +++++----- {data => acltree}/docstate.go | 2 +- {data => acltree}/document.go | 13 ++++-- {data => acltree}/document_test.go | 5 +- {data => acltree}/documentcontext.go | 4 +- {data => acltree}/documentstatebuilder.go | 6 +-- .../documentstatebuilder_test.go | 5 +- {data => acltree}/pb/aclchanges.pb.go | 0 {data => acltree}/pb/protos/aclchanges.proto | 0 {data => acltree}/plaintextdocstate.go | 6 ++- {data => acltree}/snapshotvalidator.go | 3 +- {data => acltree}/threadhelpers.go | 8 +--- {data => acltree}/tree.go | 2 +- {data => acltree}/treebuilder.go | 22 +++++---- {data => acltree}/treebuilder_test.go | 2 +- {data => acltree}/treegraph.go | 2 +- {data => acltree}/treegraph_nix.go | 2 +- {data => acltree}/treeiterator.go | 2 +- .../threadbuilder/invalidsnapshotexample.yml | 0 {data => testutils}/threadbuilder/keychain.go | 1 - .../pb/protos/testdocumentchanges.proto | 0 .../pb/testdocumentchanges.pb.go | 0 .../threadbuilder/threadbuilder.go | 3 +- .../threadbuilder/threadbuildergraph.go | 0 .../threadbuilder/threadbuildergraph_nix.go | 0 .../threadbuilder/userjoinexample.yml | 0 .../threadbuilder/userjoinexampleupdate.yml | 0 .../threadbuilder/userremovebeforeexample.yml | 0 .../threadbuilder/userremoveexample.yml | 0 .../threadbuilder/validsnapshotexample.yml | 0 .../threadbuilder/ymlentities.go | 0 .../threadbuilder/ymlentities_test.go | 2 +- {data/threadmodels => thread}/models.go | 2 +- {data/threadmodels => thread}/threadid.go | 7 +-- .../threadmodels => thread}/threadid_test.go | 5 +- {data/threadmodels => util/keys}/keys.go | 2 +- 41 files changed, 127 insertions(+), 115 deletions(-) rename {data => acltree}/aclstate.go (86%) rename {data => acltree}/aclstatebuilder.go (90%) rename {data => acltree}/aclstatebuilder_test.go (92%) rename {data => acltree}/acltreebuilder.go (84%) rename {data => acltree}/change.go (93%) rename {data => acltree}/changeloader.go (75%) rename {data => acltree}/docstate.go (92%) rename {data => acltree}/document.go (96%) rename {data => acltree}/document_test.go (93%) rename {data => acltree}/documentcontext.go (78%) rename {data => acltree}/documentstatebuilder.go (92%) rename {data => acltree}/documentstatebuilder_test.go (87%) rename {data => acltree}/pb/aclchanges.pb.go (100%) rename {data => acltree}/pb/protos/aclchanges.proto (100%) rename {data => acltree}/plaintextdocstate.go (93%) rename {data => acltree}/snapshotvalidator.go (92%) rename {data => acltree}/threadhelpers.go (94%) rename {data => acltree}/tree.go (99%) rename {data => acltree}/treebuilder.go (92%) rename {data => acltree}/treebuilder_test.go (99%) rename {data => acltree}/treegraph.go (93%) rename {data => acltree}/treegraph_nix.go (99%) rename {data => acltree}/treeiterator.go (99%) rename {data => testutils}/threadbuilder/invalidsnapshotexample.yml (100%) rename {data => testutils}/threadbuilder/keychain.go (97%) rename data/pb/protos/plaintextchanges.proto => testutils/threadbuilder/pb/protos/testdocumentchanges.proto (100%) rename data/pb/plaintextchanges.pb.go => testutils/threadbuilder/pb/testdocumentchanges.pb.go (100%) rename {data => testutils}/threadbuilder/threadbuilder.go (99%) rename {data => testutils}/threadbuilder/threadbuildergraph.go (100%) rename {data => testutils}/threadbuilder/threadbuildergraph_nix.go (100%) rename {data => testutils}/threadbuilder/userjoinexample.yml (100%) rename {data => testutils}/threadbuilder/userjoinexampleupdate.yml (100%) rename {data => testutils}/threadbuilder/userremovebeforeexample.yml (100%) rename {data => testutils}/threadbuilder/userremoveexample.yml (100%) rename {data => testutils}/threadbuilder/validsnapshotexample.yml (100%) rename {data => testutils}/threadbuilder/ymlentities.go (100%) rename {data => testutils}/threadbuilder/ymlentities_test.go (67%) rename {data/threadmodels => thread}/models.go (94%) rename {data/threadmodels => thread}/threadid.go (84%) rename {data/threadmodels => thread}/threadid_test.go (77%) rename {data/threadmodels => util/keys}/keys.go (99%) diff --git a/data/aclstate.go b/acltree/aclstate.go similarity index 86% rename from data/aclstate.go rename to acltree/aclstate.go index 5a7a5cf8..ad64dad4 100644 --- a/data/aclstate.go +++ b/acltree/aclstate.go @@ -1,4 +1,4 @@ -package data +package acltree import ( "bytes" @@ -6,8 +6,8 @@ import ( "fmt" "hash/fnv" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/pb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/threadmodels" + "github.com/anytypeio/go-anytype-infrastructure-experiments/acltree/pb" + "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" "github.com/textileio/go-threads/crypto/symmetric" ) @@ -17,22 +17,22 @@ var ErrUserRemoved = errors.New("user was removed from the document") var ErrDocumentForbidden = errors.New("your user was forbidden access to the document") var ErrUserAlreadyExists = errors.New("user already exists") -type ACLState struct { +type aclState struct { currentReadKeyHash uint64 userReadKeys map[uint64]*symmetric.Key userStates map[string]*pb.ACLChangeUserState userInvites map[string]*pb.ACLChangeUserInvite - signingPubKeyDecoder threadmodels.SigningPubKeyDecoder - encryptionKey threadmodels.EncryptionPrivKey + signingPubKeyDecoder keys.SigningPubKeyDecoder + encryptionKey keys.EncryptionPrivKey identity string } func NewACLStateFromSnapshot( snapshot *pb.ACLChangeACLSnapshot, identity string, - encryptionKey threadmodels.EncryptionPrivKey, - signingPubKeyDecoder threadmodels.SigningPubKeyDecoder) (*ACLState, error) { - st := &ACLState{ + encryptionKey keys.EncryptionPrivKey, + signingPubKeyDecoder keys.SigningPubKeyDecoder) (*aclState, error) { + st := &aclState{ identity: identity, encryptionKey: encryptionKey, userReadKeys: make(map[uint64]*symmetric.Key), @@ -47,7 +47,7 @@ func NewACLStateFromSnapshot( return st, nil } -func (st *ACLState) recreateFromSnapshot(snapshot *pb.ACLChangeACLSnapshot) error { +func (st *aclState) recreateFromSnapshot(snapshot *pb.ACLChangeACLSnapshot) error { state := snapshot.AclState for _, userState := range state.UserStates { st.userStates[userState.Identity] = userState @@ -74,7 +74,7 @@ func (st *ACLState) recreateFromSnapshot(snapshot *pb.ACLChangeACLSnapshot) erro return nil } -func (st *ACLState) ApplyChange(changeId string, change *pb.ACLChange) error { +func (st *aclState) ApplyChange(changeId string, change *pb.ACLChange) error { // we can't check this for the user which is joining, because it will not be in our list if !st.isUserJoin(change) { // we check signature when we add this to the tree, so no need to do it here @@ -98,7 +98,7 @@ func (st *ACLState) ApplyChange(changeId string, change *pb.ACLChange) error { } // TODO: remove changeId, because it is not needed -func (st *ACLState) applyChange(changeId string, ch *pb.ACLChangeACLContentValue) error { +func (st *aclState) applyChange(changeId string, ch *pb.ACLChangeACLContentValue) error { switch { case ch.GetUserPermissionChange() != nil: return st.applyUserPermissionChange(ch.GetUserPermissionChange()) @@ -117,7 +117,7 @@ func (st *ACLState) applyChange(changeId string, ch *pb.ACLChangeACLContentValue } } -func (st *ACLState) applyUserPermissionChange(ch *pb.ACLChangeUserPermissionChange) error { +func (st *aclState) applyUserPermissionChange(ch *pb.ACLChangeUserPermissionChange) error { if _, exists := st.userStates[ch.Identity]; !exists { return ErrNoSuchUser } @@ -126,12 +126,12 @@ func (st *ACLState) applyUserPermissionChange(ch *pb.ACLChangeUserPermissionChan return nil } -func (st *ACLState) applyUserInvite(changeId string, ch *pb.ACLChangeUserInvite) error { +func (st *aclState) applyUserInvite(changeId string, ch *pb.ACLChangeUserInvite) error { st.userInvites[changeId] = ch return nil } -func (st *ACLState) applyUserJoin(ch *pb.ACLChangeUserJoin) error { +func (st *aclState) applyUserJoin(ch *pb.ACLChangeUserJoin) error { invite, exists := st.userInvites[ch.UserInviteChangeId] if !exists { return fmt.Errorf("no such invite with id %s", ch.UserInviteChangeId) @@ -188,7 +188,7 @@ func (st *ACLState) applyUserJoin(ch *pb.ACLChangeUserJoin) error { return nil } -func (st *ACLState) applyUserAdd(ch *pb.ACLChangeUserAdd) error { +func (st *aclState) applyUserAdd(ch *pb.ACLChangeUserAdd) error { if _, exists := st.userStates[ch.Identity]; exists { return ErrUserAlreadyExists } @@ -203,7 +203,7 @@ func (st *ACLState) applyUserAdd(ch *pb.ACLChangeUserAdd) error { return nil } -func (st *ACLState) applyUserRemove(ch *pb.ACLChangeUserRemove) error { +func (st *aclState) applyUserRemove(ch *pb.ACLChangeUserRemove) error { if ch.Identity == st.identity { return ErrDocumentForbidden } @@ -235,7 +235,7 @@ func (st *ACLState) applyUserRemove(ch *pb.ACLChangeUserRemove) error { return nil } -func (st *ACLState) applyUserConfirm(ch *pb.ACLChangeUserConfirm) error { +func (st *aclState) applyUserConfirm(ch *pb.ACLChangeUserConfirm) error { if _, exists := st.userStates[ch.Identity]; !exists { return ErrNoSuchUser } @@ -245,7 +245,7 @@ func (st *ACLState) applyUserConfirm(ch *pb.ACLChangeUserConfirm) error { return nil } -func (st *ACLState) decryptReadKeyAndHash(msg []byte) (*symmetric.Key, uint64, error) { +func (st *aclState) decryptReadKeyAndHash(msg []byte) (*symmetric.Key, uint64, error) { decrypted, err := st.encryptionKey.Decrypt(msg) if err != nil { return nil, 0, ErrFailedToDecrypt @@ -261,7 +261,7 @@ func (st *ACLState) decryptReadKeyAndHash(msg []byte) (*symmetric.Key, uint64, e return key, hasher.Sum64(), nil } -func (st *ACLState) HasPermission(identity string, permission pb.ACLChangeUserPermissions) bool { +func (st *aclState) HasPermission(identity string, permission pb.ACLChangeUserPermissions) bool { state, exists := st.userStates[identity] if !exists { return false @@ -270,12 +270,12 @@ func (st *ACLState) HasPermission(identity string, permission pb.ACLChangeUserPe return state.Permissions == permission } -func (st *ACLState) isUserJoin(ch *pb.ACLChange) bool { +func (st *aclState) isUserJoin(ch *pb.ACLChange) bool { // if we have a UserJoin, then it should always be the first one applied return ch.AclData.GetAclContent() != nil && ch.AclData.GetAclContent()[0].GetUserJoin() != nil } -func (st *ACLState) GetPermissionDecreasedUsers(ch *pb.ACLChange) (identities []*pb.ACLChangeUserPermissionChange) { +func (st *aclState) GetPermissionDecreasedUsers(ch *pb.ACLChange) (identities []*pb.ACLChangeUserPermissionChange) { // this should be called after general checks are completed if ch.GetAclData().GetAclContent() == nil { return nil @@ -307,7 +307,7 @@ func (st *ACLState) GetPermissionDecreasedUsers(ch *pb.ACLChange) (identities [] return identities } -func (st *ACLState) Equal(other *ACLState) bool { +func (st *aclState) Equal(other *aclState) bool { if st == nil && other == nil { return true } diff --git a/data/aclstatebuilder.go b/acltree/aclstatebuilder.go similarity index 90% rename from data/aclstatebuilder.go rename to acltree/aclstatebuilder.go index 2dbe7e5d..2bb52301 100644 --- a/data/aclstatebuilder.go +++ b/acltree/aclstatebuilder.go @@ -1,18 +1,18 @@ -package data +package acltree import ( "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/pb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/threadmodels" + "github.com/anytypeio/go-anytype-infrastructure-experiments/acltree/pb" + "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" ) type ACLStateBuilder struct { tree *Tree - aclState *ACLState + aclState *aclState identity string - key threadmodels.EncryptionPrivKey - decoder threadmodels.SigningPubKeyDecoder + key keys.EncryptionPrivKey + decoder keys.SigningPubKeyDecoder } type decreasedPermissionsParameters struct { @@ -20,7 +20,7 @@ type decreasedPermissionsParameters struct { startChange string } -func NewACLStateBuilder(decoder threadmodels.SigningPubKeyDecoder, accountData *AccountData) *ACLStateBuilder { +func NewACLStateBuilder(decoder keys.SigningPubKeyDecoder, accountData *AccountData) *ACLStateBuilder { return &ACLStateBuilder{ decoder: decoder, identity: accountData.Identity, @@ -28,7 +28,7 @@ func NewACLStateBuilder(decoder threadmodels.SigningPubKeyDecoder, accountData * } } -func (sb *ACLStateBuilder) Build() (*ACLState, error) { +func (sb *ACLStateBuilder) Build() (*aclState, error) { state, _, err := sb.BuildBefore("") return state, err } @@ -55,7 +55,7 @@ func (sb *ACLStateBuilder) Init(tree *Tree) error { } // TODO: we can probably have only one state builder, because we can build both at the same time -func (sb *ACLStateBuilder) BuildBefore(beforeId string) (*ACLState, bool, error) { +func (sb *ACLStateBuilder) BuildBefore(beforeId string) (*aclState, bool, error) { var ( err error startChange = sb.tree.root diff --git a/data/aclstatebuilder_test.go b/acltree/aclstatebuilder_test.go similarity index 92% rename from data/aclstatebuilder_test.go rename to acltree/aclstatebuilder_test.go index ea6a6cfb..518e1547 100644 --- a/data/aclstatebuilder_test.go +++ b/acltree/aclstatebuilder_test.go @@ -1,11 +1,13 @@ -package data +package acltree import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/pb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/threadbuilder" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/threadmodels" - "github.com/stretchr/testify/assert" "testing" + + "github.com/anytypeio/go-anytype-infrastructure-experiments/acltree/pb" + "github.com/anytypeio/go-anytype-infrastructure-experiments/testutils/threadbuilder" + "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" + + "github.com/stretchr/testify/assert" ) func TestACLStateBuilder_UserJoinBuild(t *testing.T) { @@ -18,7 +20,7 @@ func TestACLStateBuilder_UserJoinBuild(t *testing.T) { thread, keychain.GetIdentity("A"), keychain.EncryptionKeys["A"], - threadmodels.NewEd25519Decoder(), + keys.NewEd25519Decoder(), NewPlainTextDocumentStateProvider(), false) if err != nil { @@ -53,7 +55,7 @@ func TestACLStateBuilder_UserRemoveBuild(t *testing.T) { thread, keychain.GetIdentity("A"), keychain.EncryptionKeys["A"], - threadmodels.NewEd25519Decoder(), + keys.NewEd25519Decoder(), NewPlainTextDocumentStateProvider(), false) if err != nil { @@ -84,7 +86,7 @@ func TestACLStateBuilder_UserRemoveBeforeBuild(t *testing.T) { thread, keychain.GetIdentity("A"), keychain.EncryptionKeys["A"], - threadmodels.NewEd25519Decoder(), + keys.NewEd25519Decoder(), NewPlainTextDocumentStateProvider(), false) if err != nil { @@ -116,7 +118,7 @@ func TestACLStateBuilder_InvalidSnapshotBuild(t *testing.T) { thread, keychain.GetIdentity("A"), keychain.EncryptionKeys["A"], - threadmodels.NewEd25519Decoder(), + keys.NewEd25519Decoder(), NewPlainTextDocumentStateProvider(), false) if err != nil { @@ -147,7 +149,7 @@ func TestACLStateBuilder_ValidSnapshotBuild(t *testing.T) { thread, keychain.GetIdentity("A"), keychain.EncryptionKeys["A"], - threadmodels.NewEd25519Decoder(), + keys.NewEd25519Decoder(), NewPlainTextDocumentStateProvider(), false) if err != nil { diff --git a/data/acltreebuilder.go b/acltree/acltreebuilder.go similarity index 84% rename from data/acltreebuilder.go rename to acltree/acltreebuilder.go index a5f994e7..680c9c79 100644 --- a/data/acltreebuilder.go +++ b/acltree/acltreebuilder.go @@ -1,23 +1,25 @@ -package data +package acltree import ( "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/threadmodels" + "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" + + "github.com/anytypeio/go-anytype-infrastructure-experiments/thread" "github.com/anytypeio/go-anytype-infrastructure-experiments/util/slice" - "github.com/textileio/go-threads/core/thread" + gothread "github.com/textileio/go-threads/core/thread" ) type ACLTreeBuilder struct { cache map[string]*Change - identityKeys map[string]threadmodels.SigningPubKey - signingPubKeyDecoder threadmodels.SigningPubKeyDecoder + identityKeys map[string]keys.SigningPubKey + signingPubKeyDecoder keys.SigningPubKeyDecoder tree *Tree - thread threadmodels.Thread + thread thread.Thread *changeLoader } -func NewACLTreeBuilder(t threadmodels.Thread, decoder threadmodels.SigningPubKeyDecoder) *ACLTreeBuilder { +func NewACLTreeBuilder(t thread.Thread, decoder keys.SigningPubKeyDecoder) *ACLTreeBuilder { return &ACLTreeBuilder{ signingPubKeyDecoder: decoder, thread: t, @@ -30,7 +32,7 @@ func NewACLTreeBuilder(t threadmodels.Thread, decoder threadmodels.SigningPubKey func (tb *ACLTreeBuilder) Init() { tb.cache = make(map[string]*Change) - tb.identityKeys = make(map[string]threadmodels.SigningPubKey) + tb.identityKeys = make(map[string]keys.SigningPubKey) tb.tree = &Tree{} tb.changeLoader.init(tb.cache, tb.identityKeys) } @@ -97,7 +99,7 @@ func (tb *ACLTreeBuilder) dfsFromStart(heads []string) (buf []*Change, possibleR } func (tb *ACLTreeBuilder) getRoot(possibleRoots []*Change) (*Change, error) { - threadId, err := thread.Decode(tb.thread.ID()) + threadId, err := gothread.Decode(tb.thread.ID()) if err != nil { return nil, err } @@ -109,7 +111,7 @@ func (tb *ACLTreeBuilder) getRoot(possibleRoots []*Change) (*Change, error) { continue } - res, err := threadmodels.VerifyACLThreadID(sk, threadId) + res, err := thread.VerifyACLThreadID(sk, threadId) if err != nil { continue } diff --git a/data/change.go b/acltree/change.go similarity index 93% rename from data/change.go rename to acltree/change.go index 906eb7c8..f07f19b1 100644 --- a/data/change.go +++ b/acltree/change.go @@ -1,9 +1,9 @@ -package data +package acltree import ( "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/pb" + "github.com/anytypeio/go-anytype-infrastructure-experiments/acltree/pb" "github.com/textileio/go-threads/crypto/symmetric" ) diff --git a/data/changeloader.go b/acltree/changeloader.go similarity index 75% rename from data/changeloader.go rename to acltree/changeloader.go index 5382aada..3947cee7 100644 --- a/data/changeloader.go +++ b/acltree/changeloader.go @@ -1,25 +1,27 @@ -package data +package acltree import ( "context" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/pb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/threadmodels" - "github.com/gogo/protobuf/proto" "time" + + "github.com/anytypeio/go-anytype-infrastructure-experiments/acltree/pb" + "github.com/anytypeio/go-anytype-infrastructure-experiments/thread" + "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" + "github.com/gogo/protobuf/proto" ) type changeLoader struct { cache map[string]*Change - identityKeys map[string]threadmodels.SigningPubKey - signingPubKeyDecoder threadmodels.SigningPubKeyDecoder - thread threadmodels.Thread + identityKeys map[string]keys.SigningPubKey + signingPubKeyDecoder keys.SigningPubKeyDecoder + thread thread.Thread changeCreator func(id string, ch *pb.ACLChange) *Change } func newChangeLoader( - thread threadmodels.Thread, - signingPubKeyDecoder threadmodels.SigningPubKeyDecoder, + thread thread.Thread, + signingPubKeyDecoder keys.SigningPubKeyDecoder, changeCreator func(id string, ch *pb.ACLChange) *Change) *changeLoader { return &changeLoader{ signingPubKeyDecoder: signingPubKeyDecoder, @@ -29,7 +31,7 @@ func newChangeLoader( } func (c *changeLoader) init(cache map[string]*Change, - identityKeys map[string]threadmodels.SigningPubKey) { + identityKeys map[string]keys.SigningPubKey) { c.cache = cache c.identityKeys = identityKeys } @@ -71,7 +73,7 @@ func (c *changeLoader) verify(identity string, payload, signature []byte) (isVer return identityKey.Verify(payload, signature) } -func (c *changeLoader) makeVerifiedACLChange(change *threadmodels.RawChange) (aclChange *pb.ACLChange, err error) { +func (c *changeLoader) makeVerifiedACLChange(change *thread.RawChange) (aclChange *pb.ACLChange, err error) { aclChange = new(pb.ACLChange) // TODO: think what should we do with such cases, because this can be used by attacker to break our tree diff --git a/data/docstate.go b/acltree/docstate.go similarity index 92% rename from data/docstate.go rename to acltree/docstate.go index 693319d8..dd6f69d0 100644 --- a/data/docstate.go +++ b/acltree/docstate.go @@ -1,4 +1,4 @@ -package data +package acltree type DocumentState interface { ApplyChange(change []byte, id string) (DocumentState, error) diff --git a/data/document.go b/acltree/document.go similarity index 96% rename from data/document.go rename to acltree/document.go index 44706213..8a89792a 100644 --- a/data/document.go +++ b/acltree/document.go @@ -1,9 +1,8 @@ -package data +package acltree import ( "fmt" "github.com/anytypeio/go-anytype-infrastructure-experiments/data/pb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/threadmodels" "github.com/anytypeio/go-anytype-infrastructure-experiments/util/slice" "github.com/gogo/protobuf/proto" ) @@ -64,7 +63,13 @@ func NewDocument( } } -func (d *Document) Create(payload *CreateChangePayload) error { +//sync layer -> Object cache -> document -> Update (..raw changes) +//client layer -> Object cache -> document -> CreateChange(...) +// + +// smartblock -> CreateChange(payload) +// SmartTree iterate etc +func (d *Document) CreateChange(payload *CreateChangePayload) error { // TODO: add snapshot creation logic marshalled, err := payload.ChangesData.Marshal() if err != nil { @@ -275,6 +280,8 @@ func (d *Document) build(fromStart bool) (DocumentState, error) { return nil, err } + // tree should be exposed + d.docStateBuilder.init(d.docContext.aclState, d.docContext.fullTree) d.docContext.docState, err = d.docStateBuilder.build() if err != nil { diff --git a/data/document_test.go b/acltree/document_test.go similarity index 93% rename from data/document_test.go rename to acltree/document_test.go index 6cd128a6..f6c868ff 100644 --- a/data/document_test.go +++ b/acltree/document_test.go @@ -1,10 +1,9 @@ -package data +package acltree import ( + "github.com/anytypeio/go-anytype-infrastructure-experiments/testutils/threadbuilder" "github.com/stretchr/testify/assert" "testing" - - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/threadbuilder" ) func TestDocument_Build(t *testing.T) { diff --git a/data/documentcontext.go b/acltree/documentcontext.go similarity index 78% rename from data/documentcontext.go rename to acltree/documentcontext.go index 4acfb76f..b1b29e1a 100644 --- a/data/documentcontext.go +++ b/acltree/documentcontext.go @@ -1,8 +1,8 @@ -package data +package acltree type documentContext struct { aclTree *Tree // TODO: remove it, because we don't use it fullTree *Tree - aclState *ACLState + aclState *aclState docState DocumentState } diff --git a/data/documentstatebuilder.go b/acltree/documentstatebuilder.go similarity index 92% rename from data/documentstatebuilder.go rename to acltree/documentstatebuilder.go index 3c9b0ea9..227999a4 100644 --- a/data/documentstatebuilder.go +++ b/acltree/documentstatebuilder.go @@ -1,4 +1,4 @@ -package data +package acltree import ( "fmt" @@ -8,7 +8,7 @@ import ( type documentStateBuilder struct { tree *Tree - aclState *ACLState // TODO: decide if this is needed or not + aclState *aclState // TODO: decide if this is needed or not stateProvider InitialStateProvider } @@ -18,7 +18,7 @@ func newDocumentStateBuilder(stateProvider InitialStateProvider) *documentStateB } } -func (d *documentStateBuilder) init(aclState *ACLState, tree *Tree) { +func (d *documentStateBuilder) init(aclState *aclState, tree *Tree) { d.tree = tree d.aclState = aclState } diff --git a/data/documentstatebuilder_test.go b/acltree/documentstatebuilder_test.go similarity index 87% rename from data/documentstatebuilder_test.go rename to acltree/documentstatebuilder_test.go index b691b72e..dd91de24 100644 --- a/data/documentstatebuilder_test.go +++ b/acltree/documentstatebuilder_test.go @@ -1,8 +1,7 @@ -package data +package acltree import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/threadbuilder" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/threadmodels" + "github.com/anytypeio/go-anytype-infrastructure-experiments/testutils/threadbuilder" "github.com/stretchr/testify/assert" "testing" ) diff --git a/data/pb/aclchanges.pb.go b/acltree/pb/aclchanges.pb.go similarity index 100% rename from data/pb/aclchanges.pb.go rename to acltree/pb/aclchanges.pb.go diff --git a/data/pb/protos/aclchanges.proto b/acltree/pb/protos/aclchanges.proto similarity index 100% rename from data/pb/protos/aclchanges.proto rename to acltree/pb/protos/aclchanges.proto diff --git a/data/plaintextdocstate.go b/acltree/plaintextdocstate.go similarity index 93% rename from data/plaintextdocstate.go rename to acltree/plaintextdocstate.go index 3357c02e..91ba2c3b 100644 --- a/data/plaintextdocstate.go +++ b/acltree/plaintextdocstate.go @@ -1,4 +1,4 @@ -package data +package acltree import ( "fmt" @@ -7,6 +7,10 @@ import ( "github.com/gogo/protobuf/proto" ) +// TestDocumentState -> testutils +// ThreadBuilder -> testutils +// move protos to test utils + type PlainTextDocumentState struct { LastChangeId string Text string diff --git a/data/snapshotvalidator.go b/acltree/snapshotvalidator.go similarity index 92% rename from data/snapshotvalidator.go rename to acltree/snapshotvalidator.go index 58de1b03..8b40ec97 100644 --- a/data/snapshotvalidator.go +++ b/acltree/snapshotvalidator.go @@ -1,8 +1,7 @@ -package data +package acltree import ( "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/threadmodels" ) type SnapshotValidator struct { diff --git a/data/threadhelpers.go b/acltree/threadhelpers.go similarity index 94% rename from data/threadhelpers.go rename to acltree/threadhelpers.go index 4841c065..7e46b697 100644 --- a/data/threadhelpers.go +++ b/acltree/threadhelpers.go @@ -1,12 +1,8 @@ -package data - -import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/threadmodels" -) +package acltree type ACLContext struct { Tree *Tree - ACLState *ACLState + ACLState *aclState DocState DocumentState } diff --git a/data/tree.go b/acltree/tree.go similarity index 99% rename from data/tree.go rename to acltree/tree.go index 0c4bfb6f..a437d42d 100644 --- a/data/tree.go +++ b/acltree/tree.go @@ -1,4 +1,4 @@ -package data +package acltree import ( "bytes" diff --git a/data/treebuilder.go b/acltree/treebuilder.go similarity index 92% rename from data/treebuilder.go rename to acltree/treebuilder.go index 3bcdda49..05800980 100644 --- a/data/treebuilder.go +++ b/acltree/treebuilder.go @@ -1,13 +1,15 @@ -package data +package acltree import ( "errors" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/threadmodels" + "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" + + "github.com/anytypeio/go-anytype-infrastructure-experiments/thread" //"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/lib/logging" "github.com/anytypeio/go-anytype-infrastructure-experiments/util/slice" "github.com/prometheus/common/log" - "github.com/textileio/go-threads/core/thread" + gothread "github.com/textileio/go-threads/core/thread" ) var ( @@ -17,15 +19,15 @@ var ( type TreeBuilder struct { cache map[string]*Change - identityKeys map[string]threadmodels.SigningPubKey - signingPubKeyDecoder threadmodels.SigningPubKeyDecoder + identityKeys map[string]keys.SigningPubKey + signingPubKeyDecoder keys.SigningPubKeyDecoder tree *Tree - thread threadmodels.Thread + thread thread.Thread *changeLoader } -func NewTreeBuilder(t threadmodels.Thread, decoder threadmodels.SigningPubKeyDecoder) *TreeBuilder { +func NewTreeBuilder(t thread.Thread, decoder keys.SigningPubKeyDecoder) *TreeBuilder { return &TreeBuilder{ signingPubKeyDecoder: decoder, thread: t, @@ -38,7 +40,7 @@ func NewTreeBuilder(t threadmodels.Thread, decoder threadmodels.SigningPubKeyDec func (tb *TreeBuilder) Init() { tb.cache = make(map[string]*Change) - tb.identityKeys = make(map[string]threadmodels.SigningPubKey) + tb.identityKeys = make(map[string]keys.SigningPubKey) tb.tree = &Tree{} tb.changeLoader.init(tb.cache, tb.identityKeys) } @@ -285,7 +287,7 @@ func (tb *TreeBuilder) findCommonForTwoSnapshots(s1, s2 string) (s string, err e } func (tb *TreeBuilder) getRoot(possibleRoots []*Change) (*Change, error) { - threadId, err := thread.Decode(tb.thread.ID()) + threadId, err := gothread.Decode(tb.thread.ID()) if err != nil { return nil, err } @@ -297,7 +299,7 @@ func (tb *TreeBuilder) getRoot(possibleRoots []*Change) (*Change, error) { continue } - res, err := threadmodels.VerifyACLThreadID(sk, threadId) + res, err := thread.VerifyACLThreadID(sk, threadId) if err != nil { continue } diff --git a/data/treebuilder_test.go b/acltree/treebuilder_test.go similarity index 99% rename from data/treebuilder_test.go rename to acltree/treebuilder_test.go index fa4a8971..311fbbc4 100644 --- a/data/treebuilder_test.go +++ b/acltree/treebuilder_test.go @@ -1,4 +1,4 @@ -package data +package acltree //func TestACLTreeBuilder_UserJoinCorrectHeadsAndLen(t *testing.T) { // thread, err := threadbuilder.NewThreadBuilderFromFile("threadbuilder/userjoinexample.yml") diff --git a/data/treegraph.go b/acltree/treegraph.go similarity index 93% rename from data/treegraph.go rename to acltree/treegraph.go index 60dc9967..3b37fa2e 100644 --- a/data/treegraph.go +++ b/acltree/treegraph.go @@ -2,7 +2,7 @@ // +build !linux,!darwin android ios nographviz // +build !amd64 -package data +package acltree import "fmt" diff --git a/data/treegraph_nix.go b/acltree/treegraph_nix.go similarity index 99% rename from data/treegraph_nix.go rename to acltree/treegraph_nix.go index 0adbb58f..175007aa 100644 --- a/data/treegraph_nix.go +++ b/acltree/treegraph_nix.go @@ -5,7 +5,7 @@ // +build !nographviz // +build amd64 arm64 -package data +package acltree import ( "bytes" diff --git a/data/treeiterator.go b/acltree/treeiterator.go similarity index 99% rename from data/treeiterator.go rename to acltree/treeiterator.go index dd08b6df..addcf307 100644 --- a/data/treeiterator.go +++ b/acltree/treeiterator.go @@ -1,4 +1,4 @@ -package data +package acltree import "sync" diff --git a/data/threadbuilder/invalidsnapshotexample.yml b/testutils/threadbuilder/invalidsnapshotexample.yml similarity index 100% rename from data/threadbuilder/invalidsnapshotexample.yml rename to testutils/threadbuilder/invalidsnapshotexample.yml diff --git a/data/threadbuilder/keychain.go b/testutils/threadbuilder/keychain.go similarity index 97% rename from data/threadbuilder/keychain.go rename to testutils/threadbuilder/keychain.go index 501fb404..74ecf649 100644 --- a/data/threadbuilder/keychain.go +++ b/testutils/threadbuilder/keychain.go @@ -4,7 +4,6 @@ import ( "hash/fnv" "strings" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/threadmodels" "github.com/textileio/go-threads/crypto/symmetric" ) diff --git a/data/pb/protos/plaintextchanges.proto b/testutils/threadbuilder/pb/protos/testdocumentchanges.proto similarity index 100% rename from data/pb/protos/plaintextchanges.proto rename to testutils/threadbuilder/pb/protos/testdocumentchanges.proto diff --git a/data/pb/plaintextchanges.pb.go b/testutils/threadbuilder/pb/testdocumentchanges.pb.go similarity index 100% rename from data/pb/plaintextchanges.pb.go rename to testutils/threadbuilder/pb/testdocumentchanges.pb.go diff --git a/data/threadbuilder/threadbuilder.go b/testutils/threadbuilder/threadbuilder.go similarity index 99% rename from data/threadbuilder/threadbuilder.go rename to testutils/threadbuilder/threadbuilder.go index 411204a7..6663281f 100644 --- a/data/threadbuilder/threadbuilder.go +++ b/testutils/threadbuilder/threadbuilder.go @@ -9,7 +9,6 @@ import ( "gopkg.in/yaml.v3" "github.com/anytypeio/go-anytype-infrastructure-experiments/data/pb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/data/threadmodels" ) const plainTextDocType uint16 = 1 @@ -46,7 +45,7 @@ func NewThreadBuilderFromFile(file string) (*ThreadBuilder, error) { return nil, err } - thread := YMLThread{} + thread := YMLThread{Some: &Super{}} err = yaml.Unmarshal(content, &thread) if err != nil { return nil, err diff --git a/data/threadbuilder/threadbuildergraph.go b/testutils/threadbuilder/threadbuildergraph.go similarity index 100% rename from data/threadbuilder/threadbuildergraph.go rename to testutils/threadbuilder/threadbuildergraph.go diff --git a/data/threadbuilder/threadbuildergraph_nix.go b/testutils/threadbuilder/threadbuildergraph_nix.go similarity index 100% rename from data/threadbuilder/threadbuildergraph_nix.go rename to testutils/threadbuilder/threadbuildergraph_nix.go diff --git a/data/threadbuilder/userjoinexample.yml b/testutils/threadbuilder/userjoinexample.yml similarity index 100% rename from data/threadbuilder/userjoinexample.yml rename to testutils/threadbuilder/userjoinexample.yml diff --git a/data/threadbuilder/userjoinexampleupdate.yml b/testutils/threadbuilder/userjoinexampleupdate.yml similarity index 100% rename from data/threadbuilder/userjoinexampleupdate.yml rename to testutils/threadbuilder/userjoinexampleupdate.yml diff --git a/data/threadbuilder/userremovebeforeexample.yml b/testutils/threadbuilder/userremovebeforeexample.yml similarity index 100% rename from data/threadbuilder/userremovebeforeexample.yml rename to testutils/threadbuilder/userremovebeforeexample.yml diff --git a/data/threadbuilder/userremoveexample.yml b/testutils/threadbuilder/userremoveexample.yml similarity index 100% rename from data/threadbuilder/userremoveexample.yml rename to testutils/threadbuilder/userremoveexample.yml diff --git a/data/threadbuilder/validsnapshotexample.yml b/testutils/threadbuilder/validsnapshotexample.yml similarity index 100% rename from data/threadbuilder/validsnapshotexample.yml rename to testutils/threadbuilder/validsnapshotexample.yml diff --git a/data/threadbuilder/ymlentities.go b/testutils/threadbuilder/ymlentities.go similarity index 100% rename from data/threadbuilder/ymlentities.go rename to testutils/threadbuilder/ymlentities.go diff --git a/data/threadbuilder/ymlentities_test.go b/testutils/threadbuilder/ymlentities_test.go similarity index 67% rename from data/threadbuilder/ymlentities_test.go rename to testutils/threadbuilder/ymlentities_test.go index bd066301..d2420eae 100644 --- a/data/threadbuilder/ymlentities_test.go +++ b/testutils/threadbuilder/ymlentities_test.go @@ -6,7 +6,7 @@ import ( ) func Test_YamlParse(t *testing.T) { - tb, _ := NewThreadBuilderFromFile("validsnapshotexample.yml") + tb, _ := NewThreadBuilderFromFile("userjoinexample.yml") gr, _ := tb.Graph() fmt.Println(gr) } diff --git a/data/threadmodels/models.go b/thread/models.go similarity index 94% rename from data/threadmodels/models.go rename to thread/models.go index 71b6d433..3dee82e1 100644 --- a/data/threadmodels/models.go +++ b/thread/models.go @@ -1,4 +1,4 @@ -package threadmodels +package thread import ( "context" diff --git a/data/threadmodels/threadid.go b/thread/threadid.go similarity index 84% rename from data/threadmodels/threadid.go rename to thread/threadid.go index c2bfe178..406f9eea 100644 --- a/data/threadmodels/threadid.go +++ b/thread/threadid.go @@ -1,15 +1,16 @@ -package threadmodels +package thread import ( "crypto/rand" "encoding/binary" "fmt" + "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" "hash/fnv" "github.com/textileio/go-threads/core/thread" ) -func CreateACLThreadID(k SigningPubKey, docType uint16) (thread.ID, error) { +func CreateACLThreadID(k keys.SigningPubKey, docType uint16) (thread.ID, error) { rndlen := 32 buf := make([]byte, 8+rndlen) @@ -34,7 +35,7 @@ func CreateACLThreadID(k SigningPubKey, docType uint16) (thread.ID, error) { return threadIDFromBytes(docType, buf) } -func VerifyACLThreadID(k SigningPubKey, threadId thread.ID) (bool, error) { +func VerifyACLThreadID(k keys.SigningPubKey, threadId thread.ID) (bool, error) { bytes := threadId.Bytes() pubKeyBytes := threadId.Bytes()[len(bytes)-40 : len(bytes)-32] hash := binary.LittleEndian.Uint64(pubKeyBytes) diff --git a/data/threadmodels/threadid_test.go b/thread/threadid_test.go similarity index 77% rename from data/threadmodels/threadid_test.go rename to thread/threadid_test.go index df562a77..bddef215 100644 --- a/data/threadmodels/threadid_test.go +++ b/thread/threadid_test.go @@ -1,11 +1,12 @@ -package threadmodels +package thread import ( + "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" "testing" ) func TestCreateACLThreadIDVerify(t *testing.T) { - _, pubKey, err := GenerateRandomEd25519KeyPair() + _, pubKey, err := keys.GenerateRandomEd25519KeyPair() if err != nil { t.Fatalf("should not return error after generating key pair: %v", err) } diff --git a/data/threadmodels/keys.go b/util/keys/keys.go similarity index 99% rename from data/threadmodels/keys.go rename to util/keys/keys.go index deb7967b..796b6ab3 100644 --- a/data/threadmodels/keys.go +++ b/util/keys/keys.go @@ -1,4 +1,4 @@ -package threadmodels +package keys import ( "crypto/rand"