From daa9bb1df0c9436b8ee3fb00bc819e45f6423c11 Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Mon, 17 Oct 2022 14:11:55 +0300 Subject: [PATCH] move pkgs --- Makefile | 4 +- client/account/service.go | 12 +- client/badgerprovider/service.go | 4 +- client/clientspace/clientcache/treecache.go | 6 +- client/clientspace/service.go | 12 +- client/document/service.go | 2 +- client/storage/keys.go | 2 +- client/storage/liststorage.go | 4 +- client/storage/spacestorage.go | 10 +- client/storage/storageservice.go | 2 +- client/storage/treestorage.go | 20 +- cmd/consensusnode/consensusnode.go | 4 +- .../testclient/consensustestclient.go | 6 +- cmd/node/node.go | 6 +- cmd/nodesgen/gen.go | 48 +- common/account/service.go | 6 +- {app => common/app}/app.go | 2 +- {app => common/app}/app_test.go | 0 {app => common/app}/logger/log.go | 0 .../cache/mock_cache/mock_cache.go | 2 +- common/commonspace/cache/treecache.go | 4 +- common/commonspace/diffservice/diffservice.go | 2 +- .../diffservice/diffservice_test.go | 8 +- common/commonspace/diffservice/diffsyncer.go | 2 +- .../diffservice/diffsyncer_test.go | 8 +- .../diffservice/periodicsync_test.go | 2 +- common/commonspace/payloads.go | 18 +- common/commonspace/remotediff/remotediff.go | 2 +- .../commonspace/remotediff/remotediff_test.go | 2 +- common/commonspace/service.go | 10 +- common/commonspace/space.go | 32 +- .../commonspace/spacesyncproto/spacesync.go | 2 +- .../spacesyncproto/spacesync.pb.go | 4 +- .../storage/mock_storage/mock_storage.go | 16 +- common/commonspace/storage/storage.go | 12 +- .../mock_syncservice/mock_syncservice.go | 4 +- .../commonspace/syncservice/requestfactory.go | 6 +- common/commonspace/syncservice/synchandler.go | 4 +- .../syncservice/synchandler_test.go | 6 +- common/commonspace/syncservice/syncservice.go | 2 +- common/commonspace/synctree/synctree.go | 40 +- common/commonspace/synctree/synctree_test.go | 48 +- .../mock_updatelistener.go | 2 +- .../synctree/updatelistener/updatelistener.go | 4 +- {config => common/config}/account.go | 0 {config => common/config}/anytype.go | 0 {config => common/config}/api.go | 0 {config => common/config}/config.go | 4 +- {config => common/config}/grpc.go | 0 {config => common/config}/log.go | 2 +- {config => common/config}/metric.go | 0 {config => common/config}/nodes.go | 0 {config => common/config}/peer.go | 0 {config => common/config}/space.go | 0 {config => common/config}/storage.go | 0 common/metric/metric.go | 10 +- common/net/dialer/dialer.go | 6 +- common/net/pool/pool.go | 6 +- common/net/pool/pool_test.go | 2 +- common/net/rpc/server/drpcserver.go | 12 +- common/net/secure/service.go | 8 +- common/nodeconf/service.go | 24 +- common/pkg/acl/account/accountdata.go | 12 + .../acl/aclrecordproto/aclreadkeyderive.go | 4 +- .../pkg}/acl/aclrecordproto/aclrecord.pb.go | 0 .../acl/aclrecordproto/protos/aclrecord.proto | 0 common/pkg/acl/common/keychain.go | 28 + .../pkg}/acl/list/aclrecordbuilder.go | 6 +- {pkg => common/pkg}/acl/list/aclstate.go | 78 +-- .../pkg}/acl/list/aclstatebuilder.go | 6 +- {pkg => common/pkg}/acl/list/list.go | 8 +- {pkg => common/pkg}/acl/list/list_test.go | 4 +- .../pkg}/acl/list/mock_list/mock_list.go | 24 +- {pkg => common/pkg}/acl/list/record.go | 0 {pkg => common/pkg}/acl/storage/helpers.go | 0 {pkg => common/pkg}/acl/storage/inmemory.go | 4 +- .../pkg}/acl/storage/liststorage.go | 2 +- .../acl/storage/mock_storage/mock_storage.go | 4 +- {pkg => common/pkg}/acl/storage/provider.go | 2 +- {pkg => common/pkg}/acl/storage/storage.go | 0 .../pkg}/acl/storage/treestorage.go | 2 +- .../acl/testutils/acllistbuilder/keychain.go | 37 +- .../acllistbuilder/liststoragebuilder.go | 94 +-- .../acllistbuilder/liststoragebuildergraph.go | 0 .../liststoragebuildergraph_nix.go | 3 +- .../testutils/acllistbuilder/ymlentities.go | 0 .../testutils/testchanges/proto/test.pb.go | 0 .../testutils/testchanges/proto/test.proto | 0 .../pkg}/acl/testutils/yamltests/path.go | 0 .../testutils/yamltests/userjoinexample.yml | 0 .../testutils/yamltests/userremoveexample.yml | 0 {pkg => common/pkg}/acl/tree/change.go | 2 +- {pkg => common/pkg}/acl/tree/changebuilder.go | 10 +- .../pkg}/acl/tree/changevalidator.go | 18 +- .../pkg}/acl/tree/descriptionparser.go | 0 .../tree/mock_objecttree/mock_objecttree.go | 22 +- {pkg => common/pkg}/acl/tree/objecttree.go | 18 +- .../pkg}/acl/tree/objecttree_test.go | 14 +- .../pkg}/acl/tree/objecttreefactory.go | 22 +- {pkg => common/pkg}/acl/tree/rawloader.go | 4 +- common/pkg/acl/tree/rawtreevalidator.go | 17 + .../pkg}/acl/tree/signablecontent.go | 2 +- {pkg => common/pkg}/acl/tree/tree.go | 0 {pkg => common/pkg}/acl/tree/tree_test.go | 0 {pkg => common/pkg}/acl/tree/treebuilder.go | 6 +- {pkg => common/pkg}/acl/tree/treegraph.go | 0 {pkg => common/pkg}/acl/tree/treegraph_nix.go | 0 {pkg => common/pkg}/acl/tree/treeiterator.go | 0 {pkg => common/pkg}/acl/tree/treereduce.go | 0 {pkg => common/pkg}/acl/tree/util.go | 0 .../treechangeproto/protos/treechange.proto | 0 .../pkg}/acl/treechangeproto/treechange.pb.go | 0 {pkg => common/pkg}/ldiff/diff.go | 0 {pkg => common/pkg}/ldiff/diff_test.go | 0 .../pkg}/ldiff/mock_ldiff/mock_ldiff.go | 2 +- {pkg => common/pkg}/ocache/metrics.go | 0 {pkg => common/pkg}/ocache/ocache.go | 2 +- {pkg => common/pkg}/ocache/ocache_test.go | 0 {util => common/util}/cid/cid.go | 0 .../asymmetric/encryptionkey/encryptionkey.go | 4 +- .../keys/asymmetric/encryptionkey/rsa.go | 2 +- .../keys/asymmetric/signingkey/ed25519.go | 2 +- .../keys/asymmetric/signingkey/signingkey.go | 4 +- {util => common/util}/keys/decode.go | 0 {util => common/util}/keys/key.go | 0 .../util}/keys/symmetric/symmetric.go | 0 {util => common/util}/peer/peer.go | 2 +- {util => common/util}/slice/slice.go | 0 consensus/config/config.go | 20 +- consensus/consensusclient/client.go | 4 +- consensus/consensusrpc/consensrpc.go | 2 +- consensus/db/db.go | 4 +- consensus/db/db_test.go | 2 +- consensus/stream/service.go | 26 +- consensus/stream/service_test.go | 2 +- node/account/service.go | 12 +- node/nodespace/nodecache/treecache.go | 6 +- node/nodespace/service.go | 12 +- node/storage/keys.go | 2 +- node/storage/liststorage.go | 4 +- node/storage/spacestorage.go | 10 +- node/storage/storageservice.go | 4 +- node/storage/treestorage.go | 24 +- pkg/acl/account/accountdata.go | 12 - pkg/acl/acltree/aclstatebuilder.go | 186 ------ pkg/acl/acltree/change.go | 97 ---- pkg/acl/common/keychain.go | 28 - pkg/acl/example/plaintextdocument/document.go | 169 ------ .../plaintextdocument/plaintextdocstate.go | 59 -- .../treestoragebuilder/treestoragebuilder.go | 539 ------------------ .../treestoragebuildergraph_nix.go | 162 ------ pkg/acl/tree/rawtreevalidator.go | 17 - 152 files changed, 569 insertions(+), 1771 deletions(-) rename {app => common/app}/app.go (98%) rename {app => common/app}/app_test.go (100%) rename {app => common/app}/logger/log.go (100%) rename {config => common/config}/account.go (100%) rename {config => common/config}/anytype.go (100%) rename {config => common/config}/api.go (100%) rename {config => common/config}/config.go (86%) rename {config => common/config}/grpc.go (100%) rename {config => common/config}/log.go (91%) rename {config => common/config}/metric.go (100%) rename {config => common/config}/nodes.go (100%) rename {config => common/config}/peer.go (100%) rename {config => common/config}/space.go (100%) rename {config => common/config}/storage.go (100%) create mode 100644 common/pkg/acl/account/accountdata.go rename {pkg => common/pkg}/acl/aclrecordproto/aclreadkeyderive.go (73%) rename {pkg => common/pkg}/acl/aclrecordproto/aclrecord.pb.go (100%) rename {pkg => common/pkg}/acl/aclrecordproto/protos/aclrecord.proto (100%) create mode 100644 common/pkg/acl/common/keychain.go rename {pkg => common/pkg}/acl/list/aclrecordbuilder.go (89%) rename {pkg => common/pkg}/acl/list/aclstate.go (75%) rename {pkg => common/pkg}/acl/list/aclstatebuilder.go (75%) rename {pkg => common/pkg}/acl/list/list.go (92%) rename {pkg => common/pkg}/acl/list/list_test.go (94%) rename {pkg => common/pkg}/acl/list/mock_list/mock_list.go (89%) rename {pkg => common/pkg}/acl/list/record.go (100%) rename {pkg => common/pkg}/acl/storage/helpers.go (100%) rename {pkg => common/pkg}/acl/storage/inmemory.go (95%) rename {pkg => common/pkg}/acl/storage/liststorage.go (88%) rename {pkg => common/pkg}/acl/storage/mock_storage/mock_storage.go (97%) rename {pkg => common/pkg}/acl/storage/provider.go (85%) rename {pkg => common/pkg}/acl/storage/storage.go (100%) rename {pkg => common/pkg}/acl/storage/treestorage.go (84%) rename {pkg => common/pkg}/acl/testutils/acllistbuilder/keychain.go (75%) rename {pkg => common/pkg}/acl/testutils/acllistbuilder/liststoragebuilder.go (74%) rename {pkg => common/pkg}/acl/testutils/acllistbuilder/liststoragebuildergraph.go (100%) rename {pkg => common/pkg}/acl/testutils/acllistbuilder/liststoragebuildergraph_nix.go (96%) rename {pkg => common/pkg}/acl/testutils/acllistbuilder/ymlentities.go (100%) rename {pkg => common/pkg}/acl/testutils/testchanges/proto/test.pb.go (100%) rename {pkg => common/pkg}/acl/testutils/testchanges/proto/test.proto (100%) rename {pkg => common/pkg}/acl/testutils/yamltests/path.go (100%) rename {pkg => common/pkg}/acl/testutils/yamltests/userjoinexample.yml (100%) rename {pkg => common/pkg}/acl/testutils/yamltests/userremoveexample.yml (100%) rename {pkg => common/pkg}/acl/tree/change.go (93%) rename {pkg => common/pkg}/acl/tree/changebuilder.go (93%) rename {pkg => common/pkg}/acl/tree/changevalidator.go (75%) rename {pkg => common/pkg}/acl/tree/descriptionparser.go (100%) rename {pkg => common/pkg}/acl/tree/mock_objecttree/mock_objecttree.go (93%) rename {pkg => common/pkg}/acl/tree/objecttree.go (96%) rename {pkg => common/pkg}/acl/tree/objecttree_test.go (96%) rename {pkg => common/pkg}/acl/tree/objecttreefactory.go (77%) rename {pkg => common/pkg}/acl/tree/rawloader.go (96%) create mode 100644 common/pkg/acl/tree/rawtreevalidator.go rename {pkg => common/pkg}/acl/tree/signablecontent.go (59%) rename {pkg => common/pkg}/acl/tree/tree.go (100%) rename {pkg => common/pkg}/acl/tree/tree_test.go (100%) rename {pkg => common/pkg}/acl/tree/treebuilder.go (96%) rename {pkg => common/pkg}/acl/tree/treegraph.go (100%) rename {pkg => common/pkg}/acl/tree/treegraph_nix.go (100%) rename {pkg => common/pkg}/acl/tree/treeiterator.go (100%) rename {pkg => common/pkg}/acl/tree/treereduce.go (100%) rename {pkg => common/pkg}/acl/tree/util.go (100%) rename {pkg => common/pkg}/acl/treechangeproto/protos/treechange.proto (100%) rename {pkg => common/pkg}/acl/treechangeproto/treechange.pb.go (100%) rename {pkg => common/pkg}/ldiff/diff.go (100%) rename {pkg => common/pkg}/ldiff/diff_test.go (100%) rename {pkg => common/pkg}/ldiff/mock_ldiff/mock_ldiff.go (98%) rename {pkg => common/pkg}/ocache/metrics.go (100%) rename {pkg => common/pkg}/ocache/ocache.go (98%) rename {pkg => common/pkg}/ocache/ocache_test.go (100%) rename {util => common/util}/cid/cid.go (100%) rename {util => common/util}/keys/asymmetric/encryptionkey/encryptionkey.go (67%) rename {util => common/util}/keys/asymmetric/encryptionkey/rsa.go (97%) rename {util => common/util}/keys/asymmetric/signingkey/ed25519.go (97%) rename {util => common/util}/keys/asymmetric/signingkey/signingkey.go (68%) rename {util => common/util}/keys/decode.go (100%) rename {util => common/util}/keys/key.go (100%) rename {util => common/util}/keys/symmetric/symmetric.go (100%) rename {util => common/util}/peer/peer.go (79%) rename {util => common/util}/slice/slice.go (100%) delete mode 100644 pkg/acl/account/accountdata.go delete mode 100644 pkg/acl/acltree/aclstatebuilder.go delete mode 100644 pkg/acl/acltree/change.go delete mode 100644 pkg/acl/common/keychain.go delete mode 100644 pkg/acl/example/plaintextdocument/document.go delete mode 100644 pkg/acl/example/plaintextdocument/plaintextdocstate.go delete mode 100644 pkg/acl/testutils/treestoragebuilder/treestoragebuilder.go delete mode 100644 pkg/acl/testutils/treestoragebuilder/treestoragebuildergraph_nix.go delete mode 100644 pkg/acl/tree/rawtreevalidator.go diff --git a/Makefile b/Makefile index ca30b47c..0ab38d01 100644 --- a/Makefile +++ b/Makefile @@ -34,9 +34,9 @@ proto: build: - @$(eval FLAGS := $$(shell govvv -flags -pkg github.com/anytypeio/go-anytype-infrastructure-experiments/app)) + @$(eval FLAGS := $$(shell govvv -flags -pkg github.com/anytypeio/go-anytype-infrastructure-experiments/common/app)) go build -v -o bin/anytype-node -ldflags "$(FLAGS)" cmd/node/node.go build-consensus: - @$(eval FLAGS := $$(shell govvv -flags -pkg github.com/anytypeio/go-anytype-infrastructure-experiments/app)) + @$(eval FLAGS := $$(shell govvv -flags -pkg github.com/anytypeio/go-anytype-infrastructure-experiments/common/app)) go build -v -o bin/consensus-node -ldflags "$(FLAGS)" github.com/anytypeio/go-anytype-infrastructure-experiments/cmd/consensusnode diff --git a/client/account/service.go b/client/account/service.go index 3181f9bd..6f81b4fe 100644 --- a/client/account/service.go +++ b/client/account/service.go @@ -1,13 +1,13 @@ package account import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" commonaccount "github.com/anytypeio/go-anytype-infrastructure-experiments/common/account" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/encryptionkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/account" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/encryptionkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" ) type service struct { diff --git a/client/badgerprovider/service.go b/client/badgerprovider/service.go index 2fb8a9c0..ed06f91e 100644 --- a/client/badgerprovider/service.go +++ b/client/badgerprovider/service.go @@ -2,8 +2,8 @@ package badgerprovider import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" "github.com/dgraph-io/badger/v3" ) diff --git a/client/clientspace/clientcache/treecache.go b/client/clientspace/clientcache/treecache.go index 01e230b2..ad150438 100644 --- a/client/clientspace/clientcache/treecache.go +++ b/client/clientspace/clientcache/treecache.go @@ -3,11 +3,11 @@ package clientcache import ( "context" "errors" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/client/clientspace" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/cache" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ocache" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ocache" "time" ) diff --git a/client/clientspace/service.go b/client/clientspace/service.go index 4da8b2e8..2bfb425a 100644 --- a/client/clientspace/service.go +++ b/client/clientspace/service.go @@ -2,14 +2,14 @@ package clientspace import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage" + config2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/rpc/server" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ocache" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ocache" "time" ) @@ -29,14 +29,14 @@ type Service interface { } type service struct { - conf config.Space + conf config2.Space spaceCache ocache.OCache commonSpace commonspace.Service spaceStorageProvider storage.SpaceStorageProvider } func (s *service) Init(a *app.App) (err error) { - s.conf = a.MustComponent(config.CName).(*config.Config).Space + s.conf = a.MustComponent(config2.CName).(*config2.Config).Space s.commonSpace = a.MustComponent(commonspace.CName).(commonspace.Service) s.spaceStorageProvider = a.MustComponent(storage.CName).(storage.SpaceStorageProvider) s.spaceCache = ocache.New( diff --git a/client/document/service.go b/client/document/service.go index afd16366..c48a373f 100644 --- a/client/document/service.go +++ b/client/document/service.go @@ -1,6 +1,6 @@ package document -import "github.com/anytypeio/go-anytype-infrastructure-experiments/app" +import "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" type Service interface { app.Component diff --git a/client/storage/keys.go b/client/storage/keys.go index 08749a89..81ee43c6 100644 --- a/client/storage/keys.go +++ b/client/storage/keys.go @@ -1,7 +1,7 @@ package storage import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" ) type aclKeys struct { diff --git a/client/storage/liststorage.go b/client/storage/liststorage.go index bf68b602..3061eafd 100644 --- a/client/storage/liststorage.go +++ b/client/storage/liststorage.go @@ -3,8 +3,8 @@ package storage import ( "context" "errors" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" "github.com/dgraph-io/badger/v3" ) diff --git a/client/storage/spacestorage.go b/client/storage/spacestorage.go index e05efa99..c11f1846 100644 --- a/client/storage/spacestorage.go +++ b/client/storage/spacestorage.go @@ -3,7 +3,7 @@ package storage import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" spacestorage "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" + storage2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" "github.com/dgraph-io/badger/v3" "sync" ) @@ -12,7 +12,7 @@ type spaceStorage struct { spaceId string objDb *badger.DB keys spaceKeys - aclStorage storage.ListStorage + aclStorage storage2.ListStorage header *spacesyncproto.RawSpaceHeaderWithId mx sync.Mutex } @@ -81,11 +81,11 @@ func (s *spaceStorage) ID() (string, error) { return s.spaceId, nil } -func (s *spaceStorage) TreeStorage(id string) (storage.TreeStorage, error) { +func (s *spaceStorage) TreeStorage(id string) (storage2.TreeStorage, error) { return newTreeStorage(s.objDb, s.spaceId, id) } -func (s *spaceStorage) CreateTreeStorage(payload storage.TreeStorageCreatePayload) (ts storage.TreeStorage, err error) { +func (s *spaceStorage) CreateTreeStorage(payload storage2.TreeStorageCreatePayload) (ts storage2.TreeStorage, err error) { // we have mutex here, so we prevent overwriting the heads of a tree on concurrent creation s.mx.Lock() defer s.mx.Unlock() @@ -93,7 +93,7 @@ func (s *spaceStorage) CreateTreeStorage(payload storage.TreeStorageCreatePayloa return createTreeStorage(s.objDb, s.spaceId, payload) } -func (s *spaceStorage) ACLStorage() (storage.ListStorage, error) { +func (s *spaceStorage) ACLStorage() (storage2.ListStorage, error) { return s.aclStorage, nil } diff --git a/client/storage/storageservice.go b/client/storage/storageservice.go index 0cc9250e..29082c92 100644 --- a/client/storage/storageservice.go +++ b/client/storage/storageservice.go @@ -1,7 +1,7 @@ package storage import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/client/badgerprovider" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage" "github.com/dgraph-io/badger/v3" diff --git a/client/storage/treestorage.go b/client/storage/treestorage.go index 39fbe8e2..bede0bc5 100644 --- a/client/storage/treestorage.go +++ b/client/storage/treestorage.go @@ -2,8 +2,8 @@ package storage import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" + storage2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" "github.com/dgraph-io/badger/v3" ) @@ -14,7 +14,7 @@ type treeStorage struct { root *treechangeproto.RawTreeChangeWithId } -func newTreeStorage(db *badger.DB, spaceId, treeId string) (ts storage.TreeStorage, err error) { +func newTreeStorage(db *badger.DB, spaceId, treeId string) (ts storage2.TreeStorage, err error) { keys := newTreeKeys(spaceId, treeId) err = db.View(func(txn *badger.Txn) error { _, err := txn.Get(keys.RootIdKey()) @@ -43,14 +43,14 @@ func newTreeStorage(db *badger.DB, spaceId, treeId string) (ts storage.TreeStora return } -func createTreeStorage(db *badger.DB, spaceId string, payload storage.TreeStorageCreatePayload) (ts storage.TreeStorage, err error) { +func createTreeStorage(db *badger.DB, spaceId string, payload storage2.TreeStorageCreatePayload) (ts storage2.TreeStorage, err error) { keys := newTreeKeys(spaceId, payload.TreeId) if hasDB(db, keys.RootIdKey()) { - err = storage.ErrTreeExists + err = storage2.ErrTreeExists return } err = db.Update(func(txn *badger.Txn) error { - heads := storage.CreateHeadsPayload(payload.Heads) + heads := storage2.CreateHeadsPayload(payload.Heads) for _, ch := range payload.Changes { err = txn.Set(keys.RawChangeKey(ch.Id), ch.GetRawChange()) @@ -97,16 +97,16 @@ func (t *treeStorage) Heads() (heads []string, err error) { headsBytes, err := getDB(t.db, t.keys.HeadsKey()) if err != nil { if err == badger.ErrKeyNotFound { - err = storage.ErrUnknownTreeId + err = storage2.ErrUnknownTreeId } return } - heads = storage.ParseHeads(headsBytes) + heads = storage2.ParseHeads(headsBytes) return } func (t *treeStorage) SetHeads(heads []string) (err error) { - payload := storage.CreateHeadsPayload(heads) + payload := storage2.CreateHeadsPayload(heads) return putDB(t.db, t.keys.HeadsKey(), payload) } @@ -118,7 +118,7 @@ func (t *treeStorage) GetRawChange(ctx context.Context, id string) (raw *treecha res, err := getDB(t.db, t.keys.RawChangeKey(id)) if err != nil { if err == badger.ErrKeyNotFound { - err = storage.ErrUnknownTreeId + err = storage2.ErrUnknownTreeId } return } diff --git a/cmd/consensusnode/consensusnode.go b/cmd/consensusnode/consensusnode.go index 491d7ce9..b073aa6e 100644 --- a/cmd/consensusnode/consensusnode.go +++ b/cmd/consensusnode/consensusnode.go @@ -4,8 +4,8 @@ import ( "context" "flag" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/metric" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/rpc/server" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/secure" diff --git a/cmd/consensusnode/testclient/consensustestclient.go b/cmd/consensusnode/testclient/consensustestclient.go index d0dc7b69..6f8ee914 100644 --- a/cmd/consensusnode/testclient/consensustestclient.go +++ b/cmd/consensusnode/testclient/consensustestclient.go @@ -5,13 +5,13 @@ import ( "context" "flag" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/dialer" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/pool" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/secure" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/nodeconf" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/consensusclient" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/consensusproto" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/consensusproto/consensuserr" diff --git a/cmd/node/node.go b/cmd/node/node.go index bc277ebf..6ac28724 100644 --- a/cmd/node/node.go +++ b/cmd/node/node.go @@ -4,15 +4,15 @@ import ( "context" "flag" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/dialer" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/pool" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/rpc/server" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/secure" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/nodeconf" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" "github.com/anytypeio/go-anytype-infrastructure-experiments/node/account" "github.com/anytypeio/go-anytype-infrastructure-experiments/node/nodespace" "github.com/anytypeio/go-anytype-infrastructure-experiments/node/nodespace/nodecache" diff --git a/cmd/nodesgen/gen.go b/cmd/nodesgen/gen.go index 2a122295..6d69ba1e 100644 --- a/cmd/nodesgen/gen.go +++ b/cmd/nodesgen/gen.go @@ -3,12 +3,12 @@ package main import ( "flag" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" + config2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/encryptionkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/peer" cconfig "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/config" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/encryptionkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/peer" "gopkg.in/yaml.v3" "io/ioutil" "os" @@ -42,8 +42,8 @@ func main() { } flag.Parse() - var configs []config.Config - var nodes []config.Node + var configs []config2.Config + var nodes []config2.Node for _, n := range nodesMap.Nodes { cfg, err := genNodeConfig(n.Addresses, n.APIPort) if err != nil { @@ -51,7 +51,7 @@ func main() { } configs = append(configs, cfg) - node := config.Node{ + node := config2.Node{ PeerId: cfg.Account.PeerId, Address: cfg.GrpcServer.ListenAddrs[0], SigningKey: cfg.Account.SigningKey, @@ -68,7 +68,7 @@ func main() { } consConfigs = append(consConfigs, cfg) - node := config.Node{ + node := config2.Node{ PeerId: cfg.Account.PeerId, Address: cfg.GrpcServer.ListenAddrs[0], SigningKey: cfg.Account.SigningKey, @@ -124,48 +124,48 @@ func main() { } } -func genNodeConfig(addresses []string, apiPort string) (config.Config, error) { +func genNodeConfig(addresses []string, apiPort string) (config2.Config, error) { encKey, _, err := encryptionkey.GenerateRandomRSAKeyPair(2048) if err != nil { - return config.Config{}, err + return config2.Config{}, err } signKey, _, err := signingkey.GenerateRandomEd25519KeyPair() if err != nil { - return config.Config{}, err + return config2.Config{}, err } encEncKey, err := keys.EncodeKeyToString(encKey) if err != nil { - return config.Config{}, err + return config2.Config{}, err } encSignKey, err := keys.EncodeKeyToString(signKey) if err != nil { - return config.Config{}, err + return config2.Config{}, err } peerID, err := peer.IDFromSigningPubKey(signKey.GetPublic()) if err != nil { - return config.Config{}, err + return config2.Config{}, err } - return config.Config{ - Anytype: config.Anytype{SwarmKey: "/key/swarm/psk/1.0.0/base16/209992e611c27d5dce8fbd2e7389f6b51da9bee980992ef60739460b536139ec"}, - GrpcServer: config.GrpcServer{ + return config2.Config{ + Anytype: config2.Anytype{SwarmKey: "/key/swarm/psk/1.0.0/base16/209992e611c27d5dce8fbd2e7389f6b51da9bee980992ef60739460b536139ec"}, + GrpcServer: config2.GrpcServer{ ListenAddrs: addresses, TLS: false, }, - Storage: config.Storage{Path: "db"}, - Account: config.Account{ + Storage: config2.Storage{Path: "db"}, + Account: config2.Account{ PeerId: peerID.String(), SigningKey: encSignKey, EncryptionKey: encEncKey, }, - APIServer: config.APIServer{ + APIServer: config2.APIServer{ Port: apiPort, }, - Space: config.Space{ + Space: config2.Space{ GCTTL: 60, SyncPeriod: 10, }, @@ -199,11 +199,11 @@ func genConsensusConfig(addresses []string) (cconfig.Config, error) { } return cconfig.Config{ - GrpcServer: config.GrpcServer{ + GrpcServer: config2.GrpcServer{ ListenAddrs: addresses, TLS: false, }, - Account: config.Account{ + Account: config2.Account{ PeerId: peerID.String(), SigningKey: encSignKey, EncryptionKey: encEncKey, diff --git a/common/account/service.go b/common/account/service.go index 16c62bff..ab009a51 100644 --- a/common/account/service.go +++ b/common/account/service.go @@ -1,9 +1,9 @@ package account import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/account" ) const CName = "common.account" diff --git a/app/app.go b/common/app/app.go similarity index 98% rename from app/app.go rename to common/app/app.go index 718b24b6..9e2e3fa0 100644 --- a/app/app.go +++ b/common/app/app.go @@ -4,7 +4,7 @@ import ( "context" "errors" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "go.uber.org/zap" "os" "runtime" diff --git a/app/app_test.go b/common/app/app_test.go similarity index 100% rename from app/app_test.go rename to common/app/app_test.go diff --git a/app/logger/log.go b/common/app/logger/log.go similarity index 100% rename from app/logger/log.go rename to common/app/logger/log.go diff --git a/common/commonspace/cache/mock_cache/mock_cache.go b/common/commonspace/cache/mock_cache/mock_cache.go index 84822cdb..fa2ed66c 100644 --- a/common/commonspace/cache/mock_cache/mock_cache.go +++ b/common/commonspace/cache/mock_cache/mock_cache.go @@ -8,7 +8,7 @@ import ( context "context" reflect "reflect" - app "github.com/anytypeio/go-anytype-infrastructure-experiments/app" + app "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" cache "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/cache" gomock "github.com/golang/mock/gomock" ) diff --git a/common/commonspace/cache/treecache.go b/common/commonspace/cache/treecache.go index 274f044a..64c5cb28 100644 --- a/common/commonspace/cache/treecache.go +++ b/common/commonspace/cache/treecache.go @@ -4,9 +4,9 @@ package cache import ( "context" "errors" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/synctree/updatelistener" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/tree" ) const CName = "commonspace.cache" diff --git a/common/commonspace/diffservice/diffservice.go b/common/commonspace/diffservice/diffservice.go index 84b0d637..2ed2f33a 100644 --- a/common/commonspace/diffservice/diffservice.go +++ b/common/commonspace/diffservice/diffservice.go @@ -8,7 +8,7 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/nodeconf" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ldiff" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ldiff" "go.uber.org/zap" "strings" ) diff --git a/common/commonspace/diffservice/diffservice_test.go b/common/commonspace/diffservice/diffservice_test.go index 27b4da3b..9df40e46 100644 --- a/common/commonspace/diffservice/diffservice_test.go +++ b/common/commonspace/diffservice/diffservice_test.go @@ -1,12 +1,12 @@ package diffservice import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/diffservice/mock_diffservice" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage/mock_storage" - mock_storage2 "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage/mock_storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ldiff" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ldiff/mock_ldiff" + mock_storage2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage/mock_storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ldiff" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ldiff/mock_ldiff" "github.com/golang/mock/gomock" "testing" ) diff --git a/common/commonspace/diffservice/diffsyncer.go b/common/commonspace/diffservice/diffsyncer.go index d5022323..dded0e64 100644 --- a/common/commonspace/diffservice/diffsyncer.go +++ b/common/commonspace/diffservice/diffsyncer.go @@ -9,7 +9,7 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/peer" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/rpc/rpcerr" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/nodeconf" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ldiff" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ldiff" "go.uber.org/zap" "time" ) diff --git a/common/commonspace/diffservice/diffsyncer_test.go b/common/commonspace/diffservice/diffsyncer_test.go index 47fa3c2d..a83999a4 100644 --- a/common/commonspace/diffservice/diffsyncer_test.go +++ b/common/commonspace/diffservice/diffsyncer_test.go @@ -3,7 +3,7 @@ package diffservice import ( "context" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/cache" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/cache/mock_cache" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/remotediff" @@ -12,9 +12,9 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage/mock_storage" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/peer" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/nodeconf/mock_nodeconf" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - mock_aclstorage "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage/mock_storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ldiff/mock_ldiff" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + mock_aclstorage "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage/mock_storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ldiff/mock_ldiff" "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" "storj.io/drpc" diff --git a/common/commonspace/diffservice/periodicsync_test.go b/common/commonspace/diffservice/periodicsync_test.go index 56d855f2..068da366 100644 --- a/common/commonspace/diffservice/periodicsync_test.go +++ b/common/commonspace/diffservice/periodicsync_test.go @@ -1,7 +1,7 @@ package diffservice import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/diffservice/mock_diffservice" "github.com/golang/mock/gomock" "testing" diff --git a/common/commonspace/payloads.go b/common/commonspace/payloads.go index 7c25884d..66d257ec 100644 --- a/common/commonspace/payloads.go +++ b/common/commonspace/payloads.go @@ -3,9 +3,9 @@ package commonspace import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cid" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" + aclrecordproto2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/cid" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" "hash/fnv" "math/rand" "time" @@ -68,7 +68,7 @@ func storagePayloadForSpaceCreate(payload SpaceCreatePayload) (storagePayload st } // preparing acl - aclRoot := &aclrecordproto.ACLRoot{ + aclRoot := &aclrecordproto2.ACLRoot{ Identity: identity, EncryptionKey: encPubKey, SpaceId: spaceId, @@ -144,7 +144,7 @@ func storagePayloadForSpaceDerive(payload SpaceDerivePayload) (storagePayload st } // deriving and encrypting read key - readKey, err := aclrecordproto.ACLReadKeyDerive(signPrivKey, encPrivKey) + readKey, err := aclrecordproto2.ACLReadKeyDerive(signPrivKey, encPrivKey) if err != nil { return } @@ -160,7 +160,7 @@ func storagePayloadForSpaceDerive(payload SpaceDerivePayload) (storagePayload st } // preparing acl - aclRoot := &aclrecordproto.ACLRoot{ + aclRoot := &aclrecordproto2.ACLRoot{ Identity: identity, EncryptionKey: encPubKey, SpaceId: spaceId, @@ -182,7 +182,7 @@ func storagePayloadForSpaceDerive(payload SpaceDerivePayload) (storagePayload st return } -func marshalACLRoot(aclRoot *aclrecordproto.ACLRoot, key signingkey.PrivKey) (rawWithId *aclrecordproto.RawACLRecordWithId, err error) { +func marshalACLRoot(aclRoot *aclrecordproto2.ACLRoot, key signingkey.PrivKey) (rawWithId *aclrecordproto2.RawACLRecordWithId, err error) { marshalledRoot, err := aclRoot.Marshal() if err != nil { return @@ -191,7 +191,7 @@ func marshalACLRoot(aclRoot *aclrecordproto.ACLRoot, key signingkey.PrivKey) (ra if err != nil { return } - raw := &aclrecordproto.RawACLRecord{ + raw := &aclrecordproto2.RawACLRecord{ Payload: marshalledRoot, Signature: signature, } @@ -203,7 +203,7 @@ func marshalACLRoot(aclRoot *aclrecordproto.ACLRoot, key signingkey.PrivKey) (ra if err != nil { return } - rawWithId = &aclrecordproto.RawACLRecordWithId{ + rawWithId = &aclrecordproto2.RawACLRecordWithId{ Payload: marshalledRaw, Id: aclHeadId, } diff --git a/common/commonspace/remotediff/remotediff.go b/common/commonspace/remotediff/remotediff.go index 8121b1c1..e11606ec 100644 --- a/common/commonspace/remotediff/remotediff.go +++ b/common/commonspace/remotediff/remotediff.go @@ -3,7 +3,7 @@ package remotediff import ( "context" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ldiff" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ldiff" ) type Client interface { diff --git a/common/commonspace/remotediff/remotediff_test.go b/common/commonspace/remotediff/remotediff_test.go index f6b3e7e5..11473aeb 100644 --- a/common/commonspace/remotediff/remotediff_test.go +++ b/common/commonspace/remotediff/remotediff_test.go @@ -4,7 +4,7 @@ import ( "context" "fmt" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ldiff" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ldiff" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "testing" diff --git a/common/commonspace/service.go b/common/commonspace/service.go index 2b5f2404..8d94a29e 100644 --- a/common/commonspace/service.go +++ b/common/commonspace/service.go @@ -2,15 +2,15 @@ package commonspace import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/cache" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/diffservice" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/syncservice" + config2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/pool" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/nodeconf" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" ) const CName = "common.commonspace" @@ -29,7 +29,7 @@ type Service interface { } type service struct { - config config.Space + config config2.Space configurationService nodeconf.Service storageProvider storage.SpaceStorageProvider cache cache.TreeCache @@ -37,7 +37,7 @@ type service struct { } func (s *service) Init(a *app.App) (err error) { - s.config = a.MustComponent(config.CName).(*config.Config).Space + s.config = a.MustComponent(config2.CName).(*config2.Config).Space s.storageProvider = a.MustComponent(storage.CName).(storage.SpaceStorageProvider) s.configurationService = a.MustComponent(nodeconf.CName).(nodeconf.Service) s.cache = a.MustComponent(cache.CName).(cache.TreeCache) diff --git a/common/commonspace/space.go b/common/commonspace/space.go index fca752ab..d56b6e1d 100644 --- a/common/commonspace/space.go +++ b/common/commonspace/space.go @@ -10,11 +10,11 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/syncservice" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/synctree" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/synctree/updatelistener" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/list" - treestorage "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/encryptionkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/list" + storage2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" + tree2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/tree" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/encryptionkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" "sync" ) @@ -47,9 +47,9 @@ type Space interface { SpaceSyncRpc() RpcHandler - DeriveTree(ctx context.Context, payload tree.ObjectTreeCreatePayload, listener updatelistener.UpdateListener) (tree.ObjectTree, error) - CreateTree(ctx context.Context, payload tree.ObjectTreeCreatePayload, listener updatelistener.UpdateListener) (tree.ObjectTree, error) - BuildTree(ctx context.Context, id string, listener updatelistener.UpdateListener) (tree.ObjectTree, error) + DeriveTree(ctx context.Context, payload tree2.ObjectTreeCreatePayload, listener updatelistener.UpdateListener) (tree2.ObjectTree, error) + CreateTree(ctx context.Context, payload tree2.ObjectTreeCreatePayload, listener updatelistener.UpdateListener) (tree2.ObjectTree, error) + BuildTree(ctx context.Context, id string, listener updatelistener.UpdateListener) (tree2.ObjectTree, error) Close() error } @@ -94,15 +94,15 @@ func (s *space) DiffService() diffservice.DiffService { return s.diffService } -func (s *space) DeriveTree(ctx context.Context, payload tree.ObjectTreeCreatePayload, listener updatelistener.UpdateListener) (tree.ObjectTree, error) { +func (s *space) DeriveTree(ctx context.Context, payload tree2.ObjectTreeCreatePayload, listener updatelistener.UpdateListener) (tree2.ObjectTree, error) { return synctree.DeriveSyncTree(ctx, payload, s.syncService.SyncClient(), listener, s.aclList, s.storage.CreateTreeStorage) } -func (s *space) CreateTree(ctx context.Context, payload tree.ObjectTreeCreatePayload, listener updatelistener.UpdateListener) (tree.ObjectTree, error) { +func (s *space) CreateTree(ctx context.Context, payload tree2.ObjectTreeCreatePayload, listener updatelistener.UpdateListener) (tree2.ObjectTree, error) { return synctree.CreateSyncTree(ctx, payload, s.syncService.SyncClient(), listener, s.aclList, s.storage.CreateTreeStorage) } -func (s *space) BuildTree(ctx context.Context, id string, listener updatelistener.UpdateListener) (t tree.ObjectTree, err error) { +func (s *space) BuildTree(ctx context.Context, id string, listener updatelistener.UpdateListener) (t tree2.ObjectTree, err error) { getTreeRemote := func() (*spacesyncproto.ObjectSyncMessage, error) { // TODO: add empty context handling (when this is not happening due to head update) peerId, err := syncservice.GetPeerIdFromStreamContext(ctx) @@ -116,11 +116,11 @@ func (s *space) BuildTree(ctx context.Context, id string, listener updatelistene } store, err := s.storage.TreeStorage(id) - if err != nil && err != treestorage.ErrUnknownTreeId { + if err != nil && err != storage2.ErrUnknownTreeId { return } - if err == treestorage.ErrUnknownTreeId { + if err == storage2.ErrUnknownTreeId { var resp *spacesyncproto.ObjectSyncMessage resp, err = getTreeRemote() if err != nil { @@ -128,7 +128,7 @@ func (s *space) BuildTree(ctx context.Context, id string, listener updatelistene } fullSyncResp := resp.GetContent().GetFullSyncResponse() - payload := treestorage.TreeStorageCreatePayload{ + payload := storage2.TreeStorageCreatePayload{ TreeId: resp.TreeId, RootRawChange: resp.RootChange, Changes: fullSyncResp.Changes, @@ -136,7 +136,7 @@ func (s *space) BuildTree(ctx context.Context, id string, listener updatelistene } // basically building tree with inmemory storage and validating that it was without errors - err = tree.ValidateRawTree(payload, s.aclList) + err = tree2.ValidateRawTree(payload, s.aclList) if err != nil { return } @@ -146,7 +146,7 @@ func (s *space) BuildTree(ctx context.Context, id string, listener updatelistene return } } - return synctree.BuildSyncTree(ctx, s.syncService.SyncClient(), store.(treestorage.TreeStorage), listener, s.aclList) + return synctree.BuildSyncTree(ctx, s.syncService.SyncClient(), store.(storage2.TreeStorage), listener, s.aclList) } func (s *space) Close() error { diff --git a/common/commonspace/spacesyncproto/spacesync.go b/common/commonspace/spacesyncproto/spacesync.go index 068642fe..75eb3bca 100644 --- a/common/commonspace/spacesyncproto/spacesync.go +++ b/common/commonspace/spacesyncproto/spacesync.go @@ -2,7 +2,7 @@ package spacesyncproto import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" "storj.io/drpc" ) diff --git a/common/commonspace/spacesyncproto/spacesync.pb.go b/common/commonspace/spacesyncproto/spacesync.pb.go index 2fe5cbc4..4e965e55 100644 --- a/common/commonspace/spacesyncproto/spacesync.pb.go +++ b/common/commonspace/spacesyncproto/spacesync.pb.go @@ -5,8 +5,8 @@ package spacesyncproto import ( fmt "fmt" - aclrecordproto "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - treechangeproto "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" proto "github.com/gogo/protobuf/proto" io "io" math "math" diff --git a/common/commonspace/storage/mock_storage/mock_storage.go b/common/commonspace/storage/mock_storage/mock_storage.go index d1fd5934..d18cd24d 100644 --- a/common/commonspace/storage/mock_storage/mock_storage.go +++ b/common/commonspace/storage/mock_storage/mock_storage.go @@ -5,12 +5,12 @@ package mock_storage import ( + storage2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" reflect "reflect" - app "github.com/anytypeio/go-anytype-infrastructure-experiments/app" + app "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" spacesyncproto "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" storage "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage" - storage0 "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" gomock "github.com/golang/mock/gomock" ) @@ -119,10 +119,10 @@ func (m *MockSpaceStorage) EXPECT() *MockSpaceStorageMockRecorder { } // ACLStorage mocks base method. -func (m *MockSpaceStorage) ACLStorage() (storage0.ListStorage, error) { +func (m *MockSpaceStorage) ACLStorage() (storage2.ListStorage, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ACLStorage") - ret0, _ := ret[0].(storage0.ListStorage) + ret0, _ := ret[0].(storage2.ListStorage) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -148,10 +148,10 @@ func (mr *MockSpaceStorageMockRecorder) Close() *gomock.Call { } // CreateTreeStorage mocks base method. -func (m *MockSpaceStorage) CreateTreeStorage(arg0 storage0.TreeStorageCreatePayload) (storage0.TreeStorage, error) { +func (m *MockSpaceStorage) CreateTreeStorage(arg0 storage2.TreeStorageCreatePayload) (storage2.TreeStorage, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "CreateTreeStorage", arg0) - ret0, _ := ret[0].(storage0.TreeStorage) + ret0, _ := ret[0].(storage2.TreeStorage) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -208,10 +208,10 @@ func (mr *MockSpaceStorageMockRecorder) StoredIds() *gomock.Call { } // TreeStorage mocks base method. -func (m *MockSpaceStorage) TreeStorage(arg0 string) (storage0.TreeStorage, error) { +func (m *MockSpaceStorage) TreeStorage(arg0 string) (storage2.TreeStorage, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "TreeStorage", arg0) - ret0, _ := ret[0].(storage0.TreeStorage) + ret0, _ := ret[0].(storage2.TreeStorage) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/common/commonspace/storage/storage.go b/common/commonspace/storage/storage.go index 1189ede2..eeee74a2 100644 --- a/common/commonspace/storage/storage.go +++ b/common/commonspace/storage/storage.go @@ -3,10 +3,10 @@ package storage import ( "errors" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + storage2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" ) const CName = "commonspace.storage" @@ -15,9 +15,9 @@ var ErrSpaceStorageExists = errors.New("space storage exists") var ErrSpaceStorageMissing = errors.New("space storage missing") type SpaceStorage interface { - storage.Storage - storage.Provider - ACLStorage() (storage.ListStorage, error) + storage2.Storage + storage2.Provider + ACLStorage() (storage2.ListStorage, error) SpaceHeader() (*spacesyncproto.RawSpaceHeaderWithId, error) StoredIds() ([]string, error) Close() error diff --git a/common/commonspace/syncservice/mock_syncservice/mock_syncservice.go b/common/commonspace/syncservice/mock_syncservice/mock_syncservice.go index 6f1a272b..064ebf50 100644 --- a/common/commonspace/syncservice/mock_syncservice/mock_syncservice.go +++ b/common/commonspace/syncservice/mock_syncservice/mock_syncservice.go @@ -5,11 +5,11 @@ package mock_syncservice import ( + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/tree" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" reflect "reflect" spacesyncproto "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" - tree "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree" - treechangeproto "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" gomock "github.com/golang/mock/gomock" ) diff --git a/common/commonspace/syncservice/requestfactory.go b/common/commonspace/syncservice/requestfactory.go index 1237653b..08332751 100644 --- a/common/commonspace/syncservice/requestfactory.go +++ b/common/commonspace/syncservice/requestfactory.go @@ -3,9 +3,9 @@ package syncservice import ( "fmt" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/slice" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/tree" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/slice" ) type RequestFactory interface { diff --git a/common/commonspace/syncservice/synchandler.go b/common/commonspace/syncservice/synchandler.go index d74645c4..6ac153b1 100644 --- a/common/commonspace/syncservice/synchandler.go +++ b/common/commonspace/syncservice/synchandler.go @@ -4,8 +4,8 @@ import ( "context" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/cache" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/slice" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/tree" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/slice" ) type syncHandler struct { diff --git a/common/commonspace/syncservice/synchandler_test.go b/common/commonspace/syncservice/synchandler_test.go index 3b16725e..a3d7a0dc 100644 --- a/common/commonspace/syncservice/synchandler_test.go +++ b/common/commonspace/syncservice/synchandler_test.go @@ -7,9 +7,9 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/cache/mock_cache" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/syncservice/mock_syncservice" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree" - mock_tree "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree/mock_objecttree" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/tree" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/tree/mock_objecttree" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" "sync" diff --git a/common/commonspace/syncservice/syncservice.go b/common/commonspace/syncservice/syncservice.go index ae4173b2..daa065b0 100644 --- a/common/commonspace/syncservice/syncservice.go +++ b/common/commonspace/syncservice/syncservice.go @@ -3,7 +3,7 @@ package syncservice import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/cache" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/rpc/rpcerr" diff --git a/common/commonspace/synctree/synctree.go b/common/commonspace/synctree/synctree.go index 14e05a3e..2623ea89 100644 --- a/common/commonspace/synctree/synctree.go +++ b/common/commonspace/synctree/synctree.go @@ -4,30 +4,30 @@ import ( "context" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/syncservice" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/synctree/updatelistener" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/list" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/list" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" + tree2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/tree" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" ) // SyncTree sends head updates to sync service and also sends new changes to update listener type SyncTree struct { - tree.ObjectTree + tree2.ObjectTree syncClient syncservice.SyncClient listener updatelistener.UpdateListener } -var createDerivedObjectTree = tree.CreateDerivedObjectTree -var createObjectTree = tree.CreateObjectTree -var buildObjectTree = tree.BuildObjectTree +var createDerivedObjectTree = tree2.CreateDerivedObjectTree +var createObjectTree = tree2.CreateObjectTree +var buildObjectTree = tree2.BuildObjectTree func DeriveSyncTree( ctx context.Context, - payload tree.ObjectTreeCreatePayload, + payload tree2.ObjectTreeCreatePayload, syncClient syncservice.SyncClient, listener updatelistener.UpdateListener, aclList list.ACLList, - createStorage storage.TreeStorageCreatorFunc) (t tree.ObjectTree, err error) { + createStorage storage.TreeStorageCreatorFunc) (t tree2.ObjectTree, err error) { t, err = createDerivedObjectTree(payload, aclList, createStorage) if err != nil { return @@ -45,11 +45,11 @@ func DeriveSyncTree( func CreateSyncTree( ctx context.Context, - payload tree.ObjectTreeCreatePayload, + payload tree2.ObjectTreeCreatePayload, syncClient syncservice.SyncClient, listener updatelistener.UpdateListener, aclList list.ACLList, - createStorage storage.TreeStorageCreatorFunc) (t tree.ObjectTree, err error) { + createStorage storage.TreeStorageCreatorFunc) (t tree2.ObjectTree, err error) { t, err = createObjectTree(payload, aclList, createStorage) if err != nil { return @@ -70,7 +70,7 @@ func BuildSyncTree( syncClient syncservice.SyncClient, treeStorage storage.TreeStorage, listener updatelistener.UpdateListener, - aclList list.ACLList) (t tree.ObjectTree, err error) { + aclList list.ACLList) (t tree2.ObjectTree, err error) { return buildSyncTree(ctx, syncClient, treeStorage, listener, aclList) } @@ -79,7 +79,7 @@ func buildSyncTree( syncClient syncservice.SyncClient, treeStorage storage.TreeStorage, listener updatelistener.UpdateListener, - aclList list.ACLList) (t tree.ObjectTree, err error) { + aclList list.ACLList) (t tree2.ObjectTree, err error) { t, err = buildObjectTree(treeStorage, aclList) if err != nil { return @@ -96,7 +96,7 @@ func buildSyncTree( return } -func (s *SyncTree) AddContent(ctx context.Context, content tree.SignableChangeContent) (res tree.AddResult, err error) { +func (s *SyncTree) AddContent(ctx context.Context, content tree2.SignableChangeContent) (res tree2.AddResult, err error) { res, err = s.ObjectTree.AddContent(ctx, content) if err != nil { return @@ -106,17 +106,17 @@ func (s *SyncTree) AddContent(ctx context.Context, content tree.SignableChangeCo return } -func (s *SyncTree) AddRawChanges(ctx context.Context, changes ...*treechangeproto.RawTreeChangeWithId) (res tree.AddResult, err error) { +func (s *SyncTree) AddRawChanges(ctx context.Context, changes ...*treechangeproto.RawTreeChangeWithId) (res tree2.AddResult, err error) { res, err = s.ObjectTree.AddRawChanges(ctx, changes...) if err != nil { return } switch res.Mode { - case tree.Nothing: + case tree2.Nothing: return - case tree.Append: + case tree2.Append: s.listener.Update(s) - case tree.Rebuild: + case tree2.Rebuild: s.listener.Rebuild(s) } @@ -125,6 +125,6 @@ func (s *SyncTree) AddRawChanges(ctx context.Context, changes ...*treechangeprot return } -func (s *SyncTree) Tree() tree.ObjectTree { +func (s *SyncTree) Tree() tree2.ObjectTree { return s } diff --git a/common/commonspace/synctree/synctree_test.go b/common/commonspace/synctree/synctree_test.go index ef1bd99f..04ec069d 100644 --- a/common/commonspace/synctree/synctree_test.go +++ b/common/commonspace/synctree/synctree_test.go @@ -7,20 +7,20 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/syncservice/mock_syncservice" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/synctree/updatelistener" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/synctree/updatelistener/mock_updatelistener" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/list" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/list/mock_list" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage/mock_storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree" - mock_tree "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree/mock_objecttree" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/list" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/list/mock_list" + storage2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage/mock_storage" + tree2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/tree" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/tree/mock_objecttree" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" "testing" ) type syncTreeMatcher struct { - objTree tree.ObjectTree + objTree tree2.ObjectTree client syncservice.SyncClient listener updatelistener.UpdateListener } @@ -45,11 +45,11 @@ func Test_DeriveSyncTree(t *testing.T) { updateListenerMock := mock_updatelistener.NewMockUpdateListener(ctrl) syncClientMock := mock_syncservice.NewMockSyncClient(ctrl) aclListMock := mock_list.NewMockACLList(ctrl) - createStorage := storage.TreeStorageCreatorFunc(func(payload storage.TreeStorageCreatePayload) (storage.TreeStorage, error) { + createStorage := storage2.TreeStorageCreatorFunc(func(payload storage2.TreeStorageCreatePayload) (storage2.TreeStorage, error) { return nil, nil }) objTreeMock := mock_tree.NewMockObjectTree(ctrl) - createDerivedObjectTree = func(payload tree.ObjectTreeCreatePayload, l list.ACLList, create storage.TreeStorageCreatorFunc) (objTree tree.ObjectTree, err error) { + createDerivedObjectTree = func(payload tree2.ObjectTreeCreatePayload, l list.ACLList, create storage2.TreeStorageCreatorFunc) (objTree tree2.ObjectTree, err error) { require.Equal(t, l, aclListMock) return objTreeMock, nil } @@ -57,7 +57,7 @@ func Test_DeriveSyncTree(t *testing.T) { syncClientMock.EXPECT().CreateHeadUpdate(syncTreeMatcher{objTreeMock, syncClientMock, updateListenerMock}, gomock.Nil()).Return(headUpdate) syncClientMock.EXPECT().BroadcastAsync(gomock.Eq(headUpdate)).Return(nil) - _, err := DeriveSyncTree(ctx, tree.ObjectTreeCreatePayload{}, syncClientMock, updateListenerMock, aclListMock, createStorage) + _, err := DeriveSyncTree(ctx, tree2.ObjectTreeCreatePayload{}, syncClientMock, updateListenerMock, aclListMock, createStorage) require.NoError(t, err) } @@ -69,11 +69,11 @@ func Test_CreateSyncTree(t *testing.T) { updateListenerMock := mock_updatelistener.NewMockUpdateListener(ctrl) syncClientMock := mock_syncservice.NewMockSyncClient(ctrl) aclListMock := mock_list.NewMockACLList(ctrl) - createStorage := storage.TreeStorageCreatorFunc(func(payload storage.TreeStorageCreatePayload) (storage.TreeStorage, error) { + createStorage := storage2.TreeStorageCreatorFunc(func(payload storage2.TreeStorageCreatePayload) (storage2.TreeStorage, error) { return nil, nil }) objTreeMock := mock_tree.NewMockObjectTree(ctrl) - createObjectTree = func(payload tree.ObjectTreeCreatePayload, l list.ACLList, create storage.TreeStorageCreatorFunc) (objTree tree.ObjectTree, err error) { + createObjectTree = func(payload tree2.ObjectTreeCreatePayload, l list.ACLList, create storage2.TreeStorageCreatorFunc) (objTree tree2.ObjectTree, err error) { require.Equal(t, l, aclListMock) return objTreeMock, nil } @@ -81,7 +81,7 @@ func Test_CreateSyncTree(t *testing.T) { syncClientMock.EXPECT().CreateHeadUpdate(syncTreeMatcher{objTreeMock, syncClientMock, updateListenerMock}, gomock.Nil()).Return(headUpdate) syncClientMock.EXPECT().BroadcastAsync(gomock.Eq(headUpdate)).Return(nil) - _, err := CreateSyncTree(ctx, tree.ObjectTreeCreatePayload{}, syncClientMock, updateListenerMock, aclListMock, createStorage) + _, err := CreateSyncTree(ctx, tree2.ObjectTreeCreatePayload{}, syncClientMock, updateListenerMock, aclListMock, createStorage) require.NoError(t, err) } @@ -95,7 +95,7 @@ func Test_BuildSyncTree(t *testing.T) { aclListMock := mock_list.NewMockACLList(ctrl) storageMock := mock_storage.NewMockTreeStorage(ctrl) objTreeMock := mock_tree.NewMockObjectTree(ctrl) - buildObjectTree = func(store storage.TreeStorage, l list.ACLList) (objTree tree.ObjectTree, err error) { + buildObjectTree = func(store storage2.TreeStorage, l list.ACLList) (objTree tree2.ObjectTree, err error) { require.Equal(t, aclListMock, l) require.Equal(t, store, storageMock) return objTreeMock, nil @@ -109,9 +109,9 @@ func Test_BuildSyncTree(t *testing.T) { t.Run("AddRawChanges update", func(t *testing.T) { changes := []*treechangeproto.RawTreeChangeWithId{{Id: "some"}} - expectedRes := tree.AddResult{ + expectedRes := tree2.AddResult{ Added: changes, - Mode: tree.Append, + Mode: tree2.Append, } objTreeMock.EXPECT().AddRawChanges(gomock.Any(), gomock.Eq(changes)). Return(expectedRes, nil) @@ -126,9 +126,9 @@ func Test_BuildSyncTree(t *testing.T) { t.Run("AddRawChanges rebuild", func(t *testing.T) { changes := []*treechangeproto.RawTreeChangeWithId{{Id: "some"}} - expectedRes := tree.AddResult{ + expectedRes := tree2.AddResult{ Added: changes, - Mode: tree.Rebuild, + Mode: tree2.Rebuild, } objTreeMock.EXPECT().AddRawChanges(gomock.Any(), gomock.Eq(changes)). Return(expectedRes, nil) @@ -143,9 +143,9 @@ func Test_BuildSyncTree(t *testing.T) { t.Run("AddRawChanges nothing", func(t *testing.T) { changes := []*treechangeproto.RawTreeChangeWithId{{Id: "some"}} - expectedRes := tree.AddResult{ + expectedRes := tree2.AddResult{ Added: changes, - Mode: tree.Nothing, + Mode: tree2.Nothing, } objTreeMock.EXPECT().AddRawChanges(gomock.Any(), gomock.Eq(changes)). Return(expectedRes, nil) @@ -157,11 +157,11 @@ func Test_BuildSyncTree(t *testing.T) { t.Run("AddContent", func(t *testing.T) { changes := []*treechangeproto.RawTreeChangeWithId{{Id: "some"}} - content := tree.SignableChangeContent{ + content := tree2.SignableChangeContent{ Data: []byte("abcde"), } - expectedRes := tree.AddResult{ - Mode: tree.Append, + expectedRes := tree2.AddResult{ + Mode: tree2.Append, Added: changes, } objTreeMock.EXPECT().AddContent(gomock.Any(), gomock.Eq(content)). diff --git a/common/commonspace/synctree/updatelistener/mock_updatelistener/mock_updatelistener.go b/common/commonspace/synctree/updatelistener/mock_updatelistener/mock_updatelistener.go index cb88dd6a..d9c8c9b9 100644 --- a/common/commonspace/synctree/updatelistener/mock_updatelistener/mock_updatelistener.go +++ b/common/commonspace/synctree/updatelistener/mock_updatelistener/mock_updatelistener.go @@ -5,9 +5,9 @@ package mock_updatelistener import ( + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/tree" reflect "reflect" - tree "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree" gomock "github.com/golang/mock/gomock" ) diff --git a/common/commonspace/synctree/updatelistener/updatelistener.go b/common/commonspace/synctree/updatelistener/updatelistener.go index 49f6e0d5..5d8f6e2f 100644 --- a/common/commonspace/synctree/updatelistener/updatelistener.go +++ b/common/commonspace/synctree/updatelistener/updatelistener.go @@ -1,7 +1,9 @@ //go:generate mockgen -destination mock_updatelistener/mock_updatelistener.go github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/synctree/updatelistener UpdateListener package updatelistener -import "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree" +import ( + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/tree" +) type UpdateListener interface { Update(tree tree.ObjectTree) diff --git a/config/account.go b/common/config/account.go similarity index 100% rename from config/account.go rename to common/config/account.go diff --git a/config/anytype.go b/common/config/anytype.go similarity index 100% rename from config/anytype.go rename to common/config/anytype.go diff --git a/config/api.go b/common/config/api.go similarity index 100% rename from config/api.go rename to common/config/api.go diff --git a/config/config.go b/common/config/config.go similarity index 86% rename from config/config.go rename to common/config/config.go index 7bfd1746..971fbb93 100644 --- a/config/config.go +++ b/common/config/config.go @@ -2,8 +2,8 @@ package config import ( "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "gopkg.in/yaml.v3" "io/ioutil" ) diff --git a/config/grpc.go b/common/config/grpc.go similarity index 100% rename from config/grpc.go rename to common/config/grpc.go diff --git a/config/log.go b/common/config/log.go similarity index 91% rename from config/log.go rename to common/config/log.go index a09a8597..ca7bffea 100644 --- a/config/log.go +++ b/common/config/log.go @@ -1,7 +1,7 @@ package config import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "go.uber.org/zap" ) diff --git a/config/metric.go b/common/config/metric.go similarity index 100% rename from config/metric.go rename to common/config/metric.go diff --git a/config/nodes.go b/common/config/nodes.go similarity index 100% rename from config/nodes.go rename to common/config/nodes.go diff --git a/config/peer.go b/common/config/peer.go similarity index 100% rename from config/peer.go rename to common/config/peer.go diff --git a/config/space.go b/common/config/space.go similarity index 100% rename from config/space.go rename to common/config/space.go diff --git a/config/storage.go b/common/config/storage.go similarity index 100% rename from config/storage.go rename to common/config/storage.go diff --git a/common/metric/metric.go b/common/metric/metric.go index 522aaa06..64ecbd84 100644 --- a/common/metric/metric.go +++ b/common/metric/metric.go @@ -2,8 +2,8 @@ package metric import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + config2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -24,16 +24,16 @@ type Metric interface { type metric struct { registry *prometheus.Registry - config config.Metric + config config2.Metric } type configSource interface { - GetMetric() config.Metric + GetMetric() config2.Metric } func (m *metric) Init(a *app.App) (err error) { m.registry = prometheus.NewRegistry() - m.config = a.MustComponent(config.CName).(configSource).GetMetric() + m.config = a.MustComponent(config2.CName).(configSource).GetMetric() return nil } diff --git a/common/net/dialer/dialer.go b/common/net/dialer/dialer.go index 9de13e31..10de1176 100644 --- a/common/net/dialer/dialer.go +++ b/common/net/dialer/dialer.go @@ -3,11 +3,11 @@ package dialer import ( "context" "errors" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/peer" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/secure" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" "github.com/libp2p/go-libp2p-core/sec" "go.uber.org/zap" "net" diff --git a/common/net/pool/pool.go b/common/net/pool/pool.go index dfd15e5d..c0cd6305 100644 --- a/common/net/pool/pool.go +++ b/common/net/pool/pool.go @@ -3,11 +3,11 @@ package pool import ( "context" "errors" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/dialer" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/peer" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ocache" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ocache" "math/rand" "time" ) diff --git a/common/net/pool/pool_test.go b/common/net/pool/pool_test.go index a7f4a528..2a9dce82 100644 --- a/common/net/pool/pool_test.go +++ b/common/net/pool/pool_test.go @@ -4,7 +4,7 @@ import ( "context" "errors" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/dialer" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/peer" "github.com/stretchr/testify/assert" diff --git a/common/net/rpc/server/drpcserver.go b/common/net/rpc/server/drpcserver.go index de466e88..74a41c0e 100644 --- a/common/net/rpc/server/drpcserver.go +++ b/common/net/rpc/server/drpcserver.go @@ -2,11 +2,11 @@ package server import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" + config2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/metric" secure2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/secure" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" "github.com/prometheus/client_golang/prometheus" "github.com/zeebo/errs" "go.uber.org/zap" @@ -32,11 +32,11 @@ type DRPCServer interface { } type configGetter interface { - GetGRPCServer() config.GrpcServer + GetGRPCServer() config2.GrpcServer } type drpcServer struct { - config config.GrpcServer + config config2.GrpcServer drpcServer *drpcserver.Server transport secure2.Service listeners []secure2.ContextListener @@ -46,7 +46,7 @@ type drpcServer struct { } func (s *drpcServer) Init(a *app.App) (err error) { - s.config = a.MustComponent(config.CName).(configGetter).GetGRPCServer() + s.config = a.MustComponent(config2.CName).(configGetter).GetGRPCServer() s.transport = a.MustComponent(secure2.CName).(secure2.Service) s.metric = a.MustComponent(metric.CName).(metric.Metric) return nil diff --git a/common/net/secure/service.go b/common/net/secure/service.go index 3e70521d..51a92c0e 100644 --- a/common/net/secure/service.go +++ b/common/net/secure/service.go @@ -2,11 +2,11 @@ package secure import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" commonaccount "github.com/anytypeio/go-anytype-infrastructure-experiments/common/account" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys" "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/sec" libp2ptls "github.com/libp2p/go-libp2p/p2p/security/tls" diff --git a/common/nodeconf/service.go b/common/nodeconf/service.go index 66f6b6b3..6684b571 100644 --- a/common/nodeconf/service.go +++ b/common/nodeconf/service.go @@ -1,12 +1,12 @@ package nodeconf import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/encryptionkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" + config2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys" + encryptionkey2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/encryptionkey" + signingkey2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" "github.com/anytypeio/go-chash" ) @@ -36,8 +36,8 @@ type service struct { type Node struct { Address string PeerId string - SigningKey signingkey.PubKey - EncryptionKey encryptionkey.PubKey + SigningKey signingkey2.PubKey + EncryptionKey encryptionkey2.PubKey } func (n *Node) Id() string { @@ -49,7 +49,7 @@ func (n *Node) Capacity() float64 { } func (s *service) Init(a *app.App) (err error) { - conf := a.MustComponent(config.CName).(*config.Config) + conf := a.MustComponent(config2.CName).(*config2.Config) s.accountId = conf.Account.PeerId config := &configuration{ @@ -100,10 +100,10 @@ func (s *service) ConsensusPeers() []string { } func nodeFromConfigNode( - n config.Node) (*Node, error) { + n config2.Node) (*Node, error) { decodedSigningKey, err := keys.DecodeKeyFromString( n.SigningKey, - signingkey.UnmarshalEd25519PrivateKey, + signingkey2.UnmarshalEd25519PrivateKey, nil) if err != nil { return nil, err @@ -111,7 +111,7 @@ func nodeFromConfigNode( decodedEncryptionKey, err := keys.DecodeKeyFromString( n.EncryptionKey, - encryptionkey.NewEncryptionRsaPrivKeyFromBytes, + encryptionkey2.NewEncryptionRsaPrivKeyFromBytes, nil) if err != nil { return nil, err diff --git a/common/pkg/acl/account/accountdata.go b/common/pkg/acl/account/accountdata.go new file mode 100644 index 00000000..eea9d2f0 --- /dev/null +++ b/common/pkg/acl/account/accountdata.go @@ -0,0 +1,12 @@ +package account + +import ( + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/encryptionkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" +) + +type AccountData struct { // TODO: create a convenient constructor for this + Identity []byte // public key + SignKey signingkey.PrivKey + EncKey encryptionkey.PrivKey +} diff --git a/pkg/acl/aclrecordproto/aclreadkeyderive.go b/common/pkg/acl/aclrecordproto/aclreadkeyderive.go similarity index 73% rename from pkg/acl/aclrecordproto/aclreadkeyderive.go rename to common/pkg/acl/aclrecordproto/aclreadkeyderive.go index a09ba6ce..40acc8ec 100644 --- a/pkg/acl/aclrecordproto/aclreadkeyderive.go +++ b/common/pkg/acl/aclrecordproto/aclreadkeyderive.go @@ -1,6 +1,8 @@ package aclrecordproto -import "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/symmetric" +import ( + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/symmetric" +) func ACLReadKeyDerive(signKey []byte, encKey []byte) (*symmetric.Key, error) { concBuf := make([]byte, 0, len(signKey)+len(encKey)) diff --git a/pkg/acl/aclrecordproto/aclrecord.pb.go b/common/pkg/acl/aclrecordproto/aclrecord.pb.go similarity index 100% rename from pkg/acl/aclrecordproto/aclrecord.pb.go rename to common/pkg/acl/aclrecordproto/aclrecord.pb.go diff --git a/pkg/acl/aclrecordproto/protos/aclrecord.proto b/common/pkg/acl/aclrecordproto/protos/aclrecord.proto similarity index 100% rename from pkg/acl/aclrecordproto/protos/aclrecord.proto rename to common/pkg/acl/aclrecordproto/protos/aclrecord.proto diff --git a/common/pkg/acl/common/keychain.go b/common/pkg/acl/common/keychain.go new file mode 100644 index 00000000..b6de00b2 --- /dev/null +++ b/common/pkg/acl/common/keychain.go @@ -0,0 +1,28 @@ +package common + +import ( + signingkey2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" +) + +type Keychain struct { + keys map[string]signingkey2.PubKey +} + +func NewKeychain() *Keychain { + return &Keychain{ + keys: make(map[string]signingkey2.PubKey), + } +} + +func (k *Keychain) GetOrAdd(identity string) (signingkey2.PubKey, error) { + if key, exists := k.keys[identity]; exists { + return key, nil + } + res, err := signingkey2.NewSigningEd25519PubKeyFromBytes([]byte(identity)) + if err != nil { + return nil, err + } + + k.keys[identity] = res.(signingkey2.PubKey) + return res.(signingkey2.PubKey), nil +} diff --git a/pkg/acl/list/aclrecordbuilder.go b/common/pkg/acl/list/aclrecordbuilder.go similarity index 89% rename from pkg/acl/list/aclrecordbuilder.go rename to common/pkg/acl/list/aclrecordbuilder.go index 26c9e08f..64013f4a 100644 --- a/pkg/acl/list/aclrecordbuilder.go +++ b/common/pkg/acl/list/aclrecordbuilder.go @@ -1,9 +1,9 @@ package list import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/common" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cid" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/common" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/cid" "github.com/gogo/protobuf/proto" ) diff --git a/pkg/acl/list/aclstate.go b/common/pkg/acl/list/aclstate.go similarity index 75% rename from pkg/acl/list/aclstate.go rename to common/pkg/acl/list/aclstate.go index a8df8c53..8e9930db 100644 --- a/pkg/acl/list/aclstate.go +++ b/common/pkg/acl/list/aclstate.go @@ -4,12 +4,12 @@ import ( "bytes" "errors" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/common" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/encryptionkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/symmetric" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" + aclrecordproto2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/common" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/encryptionkey" + signingkey2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/symmetric" "github.com/gogo/protobuf/proto" "go.uber.org/zap" "hash/fnv" @@ -30,17 +30,17 @@ var ErrIncorrectRoot = errors.New("incorrect root") type UserPermissionPair struct { Identity string - Permission aclrecordproto.ACLUserPermissions + Permission aclrecordproto2.ACLUserPermissions } type ACLState struct { id string currentReadKeyHash uint64 userReadKeys map[uint64]*symmetric.Key - userStates map[string]*aclrecordproto.ACLUserState - userInvites map[string]*aclrecordproto.ACLUserInvite + userStates map[string]*aclrecordproto2.ACLUserState + userInvites map[string]*aclrecordproto2.ACLUserInvite encryptionKey encryptionkey.PrivKey - signingKey signingkey.PrivKey + signingKey signingkey2.PrivKey identity string permissionsAtRecord map[string][]UserPermissionPair @@ -50,7 +50,7 @@ type ACLState struct { func newACLStateWithKeys( id string, - signingKey signingkey.PrivKey, + signingKey signingkey2.PrivKey, encryptionKey encryptionkey.PrivKey) (*ACLState, error) { identity, err := signingKey.Raw() if err != nil { @@ -62,8 +62,8 @@ func newACLStateWithKeys( signingKey: signingKey, encryptionKey: encryptionKey, userReadKeys: make(map[uint64]*symmetric.Key), - userStates: make(map[string]*aclrecordproto.ACLUserState), - userInvites: make(map[string]*aclrecordproto.ACLUserInvite), + userStates: make(map[string]*aclrecordproto2.ACLUserState), + userInvites: make(map[string]*aclrecordproto2.ACLUserInvite), permissionsAtRecord: make(map[string][]UserPermissionPair), }, nil } @@ -72,8 +72,8 @@ func newACLState(id string) *ACLState { return &ACLState{ id: id, userReadKeys: make(map[uint64]*symmetric.Key), - userStates: make(map[string]*aclrecordproto.ACLUserState), - userInvites: make(map[string]*aclrecordproto.ACLUserInvite), + userStates: make(map[string]*aclrecordproto2.ACLUserState), + userInvites: make(map[string]*aclrecordproto2.ACLUserInvite), permissionsAtRecord: make(map[string][]UserPermissionPair), } } @@ -111,16 +111,16 @@ func (st *ACLState) PermissionsAtRecord(id string, identity string) (UserPermiss func (st *ACLState) applyRecord(record *ACLRecord) (err error) { if record.Id == st.id { - root, ok := record.Model.(*aclrecordproto.ACLRoot) + root, ok := record.Model.(*aclrecordproto2.ACLRoot) if !ok { return ErrIncorrectRoot } return st.applyRoot(root) } - aclData := &aclrecordproto.ACLData{} + aclData := &aclrecordproto2.ACLData{} if record.Model != nil { - aclData = record.Model.(*aclrecordproto.ACLData) + aclData = record.Model.(*aclrecordproto2.ACLData) } else { err = proto.Unmarshal(record.Data, aclData) if err != nil { @@ -148,7 +148,7 @@ func (st *ACLState) applyRecord(record *ACLRecord) (err error) { return nil } -func (st *ACLState) applyRoot(root *aclrecordproto.ACLRoot) (err error) { +func (st *ACLState) applyRoot(root *aclrecordproto2.ACLRoot) (err error) { if st.signingKey != nil && st.encryptionKey != nil { err = st.saveReadKeyFromRoot(root) if err != nil { @@ -157,16 +157,16 @@ func (st *ACLState) applyRoot(root *aclrecordproto.ACLRoot) (err error) { } // adding user to the list - userState := &aclrecordproto.ACLUserState{ + userState := &aclrecordproto2.ACLUserState{ Identity: root.Identity, EncryptionKey: root.EncryptionKey, - Permissions: aclrecordproto.ACLUserPermissions_Admin, + Permissions: aclrecordproto2.ACLUserPermissions_Admin, } st.userStates[string(root.Identity)] = userState return } -func (st *ACLState) saveReadKeyFromRoot(root *aclrecordproto.ACLRoot) (err error) { +func (st *ACLState) saveReadKeyFromRoot(root *aclrecordproto2.ACLRoot) (err error) { var readKey *symmetric.Key if len(root.GetDerivationScheme()) != 0 { var encPubKey []byte @@ -175,7 +175,7 @@ func (st *ACLState) saveReadKeyFromRoot(root *aclrecordproto.ACLRoot) (err error return } - readKey, err = aclrecordproto.ACLReadKeyDerive([]byte(st.identity), encPubKey) + readKey, err = aclrecordproto2.ACLReadKeyDerive([]byte(st.identity), encPubKey) if err != nil { return } @@ -199,7 +199,7 @@ func (st *ACLState) saveReadKeyFromRoot(root *aclrecordproto.ACLRoot) (err error return } -func (st *ACLState) applyChangeData(changeData *aclrecordproto.ACLData, hash uint64, identity []byte) (err error) { +func (st *ACLState) applyChangeData(changeData *aclrecordproto2.ACLData, hash uint64, identity []byte) (err error) { defer func() { if err != nil { return @@ -214,7 +214,7 @@ func (st *ACLState) applyChangeData(changeData *aclrecordproto.ACLData, hash uin return } - if !st.hasPermission(identity, aclrecordproto.ACLUserPermissions_Admin) { + if !st.hasPermission(identity, aclrecordproto2.ACLUserPermissions_Admin) { err = fmt.Errorf("user %s must have admin permissions", identity) return } @@ -230,7 +230,7 @@ func (st *ACLState) applyChangeData(changeData *aclrecordproto.ACLData, hash uin return nil } -func (st *ACLState) applyChangeContent(ch *aclrecordproto.ACLContentValue) error { +func (st *ACLState) applyChangeContent(ch *aclrecordproto2.ACLContentValue) error { switch { case ch.GetUserPermissionChange() != nil: return st.applyUserPermissionChange(ch.GetUserPermissionChange()) @@ -247,7 +247,7 @@ func (st *ACLState) applyChangeContent(ch *aclrecordproto.ACLContentValue) error } } -func (st *ACLState) applyUserPermissionChange(ch *aclrecordproto.ACLUserPermissionChange) error { +func (st *ACLState) applyUserPermissionChange(ch *aclrecordproto2.ACLUserPermissionChange) error { chIdentity := string(ch.Identity) state, exists := st.userStates[chIdentity] if !exists { @@ -258,12 +258,12 @@ func (st *ACLState) applyUserPermissionChange(ch *aclrecordproto.ACLUserPermissi return nil } -func (st *ACLState) applyUserInvite(ch *aclrecordproto.ACLUserInvite) error { +func (st *ACLState) applyUserInvite(ch *aclrecordproto2.ACLUserInvite) error { st.userInvites[ch.InviteId] = ch return nil } -func (st *ACLState) applyUserJoin(ch *aclrecordproto.ACLUserJoin) error { +func (st *ACLState) applyUserJoin(ch *aclrecordproto2.ACLUserJoin) error { invite, exists := st.userInvites[ch.InviteId] if !exists { return fmt.Errorf("no such invite with id %s", ch.InviteId) @@ -276,12 +276,12 @@ func (st *ACLState) applyUserJoin(ch *aclrecordproto.ACLUserJoin) error { // validating signature signature := ch.GetAcceptSignature() - verificationKey, err := signingkey.NewSigningEd25519PubKeyFromBytes(invite.AcceptPublicKey) + verificationKey, err := signingkey2.NewSigningEd25519PubKeyFromBytes(invite.AcceptPublicKey) if err != nil { return fmt.Errorf("public key verifying invite accepts is given in incorrect format: %v", err) } - res, err := verificationKey.(signingkey.PubKey).Verify(ch.Identity, signature) + res, err := verificationKey.(signingkey2.PubKey).Verify(ch.Identity, signature) if err != nil { return fmt.Errorf("verification returned error: %w", err) } @@ -302,7 +302,7 @@ func (st *ACLState) applyUserJoin(ch *aclrecordproto.ACLUserJoin) error { } // adding user to the list - userState := &aclrecordproto.ACLUserState{ + userState := &aclrecordproto2.ACLUserState{ Identity: ch.Identity, EncryptionKey: ch.EncryptionKey, Permissions: invite.Permissions, @@ -311,13 +311,13 @@ func (st *ACLState) applyUserJoin(ch *aclrecordproto.ACLUserJoin) error { return nil } -func (st *ACLState) applyUserAdd(ch *aclrecordproto.ACLUserAdd) error { +func (st *ACLState) applyUserAdd(ch *aclrecordproto2.ACLUserAdd) error { chIdentity := string(ch.Identity) if _, exists := st.userStates[chIdentity]; exists { return ErrUserAlreadyExists } - st.userStates[chIdentity] = &aclrecordproto.ACLUserState{ + st.userStates[chIdentity] = &aclrecordproto2.ACLUserState{ Identity: ch.Identity, EncryptionKey: ch.EncryptionKey, Permissions: ch.Permissions, @@ -337,7 +337,7 @@ func (st *ACLState) applyUserAdd(ch *aclrecordproto.ACLUserAdd) error { return nil } -func (st *ACLState) applyUserRemove(ch *aclrecordproto.ACLUserRemove) error { +func (st *ACLState) applyUserRemove(ch *aclrecordproto2.ACLUserRemove) error { chIdentity := string(ch.Identity) if chIdentity == st.identity { return ErrDocumentForbidden @@ -381,7 +381,7 @@ func (st *ACLState) decryptReadKeyAndHash(msg []byte) (*symmetric.Key, uint64, e return key, hasher.Sum64(), nil } -func (st *ACLState) hasPermission(identity []byte, permission aclrecordproto.ACLUserPermissions) bool { +func (st *ACLState) hasPermission(identity []byte, permission aclrecordproto2.ACLUserPermissions) bool { state, exists := st.userStates[string(identity)] if !exists { return false @@ -390,17 +390,17 @@ func (st *ACLState) hasPermission(identity []byte, permission aclrecordproto.ACL return state.Permissions == permission } -func (st *ACLState) isUserJoin(data *aclrecordproto.ACLData) bool { +func (st *ACLState) isUserJoin(data *aclrecordproto2.ACLData) bool { // if we have a UserJoin, then it should always be the first one applied return data.GetAclContent() != nil && data.GetAclContent()[0].GetUserJoin() != nil } -func (st *ACLState) isUserAdd(data *aclrecordproto.ACLData, identity []byte) bool { +func (st *ACLState) isUserAdd(data *aclrecordproto2.ACLData, identity []byte) bool { // if we have a UserAdd, then it should always be the first one applied userAdd := data.GetAclContent()[0].GetUserAdd() return data.GetAclContent() != nil && userAdd != nil && bytes.Compare(userAdd.GetIdentity(), identity) == 0 } -func (st *ACLState) GetUserStates() map[string]*aclrecordproto.ACLUserState { +func (st *ACLState) GetUserStates() map[string]*aclrecordproto2.ACLUserState { return st.userStates } diff --git a/pkg/acl/list/aclstatebuilder.go b/common/pkg/acl/list/aclstatebuilder.go similarity index 75% rename from pkg/acl/list/aclstatebuilder.go rename to common/pkg/acl/list/aclstatebuilder.go index 75b606af..1b847865 100644 --- a/pkg/acl/list/aclstatebuilder.go +++ b/common/pkg/acl/list/aclstatebuilder.go @@ -1,9 +1,9 @@ package list import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/encryptionkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/account" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/encryptionkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" ) type aclStateBuilder struct { diff --git a/pkg/acl/list/list.go b/common/pkg/acl/list/list.go similarity index 92% rename from pkg/acl/list/list.go rename to common/pkg/acl/list/list.go index 382ecf4e..3e6794ff 100644 --- a/pkg/acl/list/list.go +++ b/common/pkg/acl/list/list.go @@ -5,10 +5,10 @@ import ( "context" "errors" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/common" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/account" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/common" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" "sync" ) diff --git a/pkg/acl/list/list_test.go b/common/pkg/acl/list/list_test.go similarity index 94% rename from pkg/acl/list/list_test.go rename to common/pkg/acl/list/list_test.go index 13019b21..668c2d5f 100644 --- a/pkg/acl/list/list_test.go +++ b/common/pkg/acl/list/list_test.go @@ -1,8 +1,8 @@ package list import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/acllistbuilder" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/testutils/acllistbuilder" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "testing" diff --git a/pkg/acl/list/mock_list/mock_list.go b/common/pkg/acl/list/mock_list/mock_list.go similarity index 89% rename from pkg/acl/list/mock_list/mock_list.go rename to common/pkg/acl/list/mock_list/mock_list.go index e4c7cc6e..a28edf2b 100644 --- a/pkg/acl/list/mock_list/mock_list.go +++ b/common/pkg/acl/list/mock_list/mock_list.go @@ -5,10 +5,10 @@ package mock_list import ( + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + list2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/list" reflect "reflect" - aclrecordproto "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - list "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/list" gomock "github.com/golang/mock/gomock" ) @@ -36,10 +36,10 @@ func (m *MockACLList) EXPECT() *MockACLListMockRecorder { } // ACLState mocks base method. -func (m *MockACLList) ACLState() *list.ACLState { +func (m *MockACLList) ACLState() *list2.ACLState { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ACLState") - ret0, _ := ret[0].(*list.ACLState) + ret0, _ := ret[0].(*list2.ACLState) return ret0 } @@ -64,10 +64,10 @@ func (mr *MockACLListMockRecorder) Close() *gomock.Call { } // Get mocks base method. -func (m *MockACLList) Get(arg0 string) (*list.ACLRecord, error) { +func (m *MockACLList) Get(arg0 string) (*list2.ACLRecord, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Get", arg0) - ret0, _ := ret[0].(*list.ACLRecord) + ret0, _ := ret[0].(*list2.ACLRecord) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -79,10 +79,10 @@ func (mr *MockACLListMockRecorder) Get(arg0 interface{}) *gomock.Call { } // Head mocks base method. -func (m *MockACLList) Head() *list.ACLRecord { +func (m *MockACLList) Head() *list2.ACLRecord { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Head") - ret0, _ := ret[0].(*list.ACLRecord) + ret0, _ := ret[0].(*list2.ACLRecord) return ret0 } @@ -122,7 +122,7 @@ func (mr *MockACLListMockRecorder) IsAfter(arg0, arg1 interface{}) *gomock.Call } // Iterate mocks base method. -func (m *MockACLList) Iterate(arg0 func(*list.ACLRecord) bool) { +func (m *MockACLList) Iterate(arg0 func(*list2.ACLRecord) bool) { m.ctrl.T.Helper() m.ctrl.Call(m, "Iterate", arg0) } @@ -134,7 +134,7 @@ func (mr *MockACLListMockRecorder) Iterate(arg0 interface{}) *gomock.Call { } // IterateFrom mocks base method. -func (m *MockACLList) IterateFrom(arg0 string, arg1 func(*list.ACLRecord) bool) { +func (m *MockACLList) IterateFrom(arg0 string, arg1 func(*list2.ACLRecord) bool) { m.ctrl.T.Helper() m.ctrl.Call(m, "IterateFrom", arg0, arg1) } @@ -182,10 +182,10 @@ func (mr *MockACLListMockRecorder) RUnlock() *gomock.Call { } // Records mocks base method. -func (m *MockACLList) Records() []*list.ACLRecord { +func (m *MockACLList) Records() []*list2.ACLRecord { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Records") - ret0, _ := ret[0].([]*list.ACLRecord) + ret0, _ := ret[0].([]*list2.ACLRecord) return ret0 } diff --git a/pkg/acl/list/record.go b/common/pkg/acl/list/record.go similarity index 100% rename from pkg/acl/list/record.go rename to common/pkg/acl/list/record.go diff --git a/pkg/acl/storage/helpers.go b/common/pkg/acl/storage/helpers.go similarity index 100% rename from pkg/acl/storage/helpers.go rename to common/pkg/acl/storage/helpers.go diff --git a/pkg/acl/storage/inmemory.go b/common/pkg/acl/storage/inmemory.go similarity index 95% rename from pkg/acl/storage/inmemory.go rename to common/pkg/acl/storage/inmemory.go index a992e84a..11843de7 100644 --- a/pkg/acl/storage/inmemory.go +++ b/common/pkg/acl/storage/inmemory.go @@ -3,8 +3,8 @@ package storage import ( "context" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" "sync" ) diff --git a/pkg/acl/storage/liststorage.go b/common/pkg/acl/storage/liststorage.go similarity index 88% rename from pkg/acl/storage/liststorage.go rename to common/pkg/acl/storage/liststorage.go index 4ce5f814..02fd7ca3 100644 --- a/pkg/acl/storage/liststorage.go +++ b/common/pkg/acl/storage/liststorage.go @@ -4,7 +4,7 @@ package storage import ( "context" "errors" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" ) var ErrUnknownACLId = errors.New("acl does not exist") diff --git a/pkg/acl/storage/mock_storage/mock_storage.go b/common/pkg/acl/storage/mock_storage/mock_storage.go similarity index 97% rename from pkg/acl/storage/mock_storage/mock_storage.go rename to common/pkg/acl/storage/mock_storage/mock_storage.go index 0bfa22e8..4c1a298c 100644 --- a/pkg/acl/storage/mock_storage/mock_storage.go +++ b/common/pkg/acl/storage/mock_storage/mock_storage.go @@ -6,10 +6,10 @@ package mock_storage import ( context "context" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" reflect "reflect" - aclrecordproto "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - treechangeproto "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" gomock "github.com/golang/mock/gomock" ) diff --git a/pkg/acl/storage/provider.go b/common/pkg/acl/storage/provider.go similarity index 85% rename from pkg/acl/storage/provider.go rename to common/pkg/acl/storage/provider.go index 1d40cb30..da46ab41 100644 --- a/pkg/acl/storage/provider.go +++ b/common/pkg/acl/storage/provider.go @@ -2,7 +2,7 @@ package storage import ( "errors" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" ) var ErrUnknownTreeId = errors.New("tree does not exist") diff --git a/pkg/acl/storage/storage.go b/common/pkg/acl/storage/storage.go similarity index 100% rename from pkg/acl/storage/storage.go rename to common/pkg/acl/storage/storage.go diff --git a/pkg/acl/storage/treestorage.go b/common/pkg/acl/storage/treestorage.go similarity index 84% rename from pkg/acl/storage/treestorage.go rename to common/pkg/acl/storage/treestorage.go index 25b91e46..76332399 100644 --- a/pkg/acl/storage/treestorage.go +++ b/common/pkg/acl/storage/treestorage.go @@ -2,7 +2,7 @@ package storage import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" ) type TreeStorage interface { diff --git a/pkg/acl/testutils/acllistbuilder/keychain.go b/common/pkg/acl/testutils/acllistbuilder/keychain.go similarity index 75% rename from pkg/acl/testutils/acllistbuilder/keychain.go rename to common/pkg/acl/testutils/acllistbuilder/keychain.go index c893a665..db7fb0a6 100644 --- a/pkg/acl/testutils/acllistbuilder/keychain.go +++ b/common/pkg/acl/testutils/acllistbuilder/keychain.go @@ -1,14 +1,13 @@ package acllistbuilder import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/encryptionkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys" + encryptionkey2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/encryptionkey" + signingkey2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/symmetric" "hash/fnv" "strings" - - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/symmetric" ) type SymKey struct { @@ -17,9 +16,9 @@ type SymKey struct { } type YAMLKeychain struct { - SigningKeysByYAMLIdentity map[string]signingkey.PrivKey - SigningKeysByRealIdentity map[string]signingkey.PrivKey - EncryptionKeysByYAMLIdentity map[string]encryptionkey.PrivKey + SigningKeysByYAMLIdentity map[string]signingkey2.PrivKey + SigningKeysByRealIdentity map[string]signingkey2.PrivKey + EncryptionKeysByYAMLIdentity map[string]encryptionkey2.PrivKey ReadKeysByYAMLIdentity map[string]*SymKey ReadKeysByHash map[uint64]*SymKey GeneratedIdentities map[string]string @@ -28,9 +27,9 @@ type YAMLKeychain struct { func NewKeychain() *YAMLKeychain { return &YAMLKeychain{ - SigningKeysByYAMLIdentity: map[string]signingkey.PrivKey{}, - SigningKeysByRealIdentity: map[string]signingkey.PrivKey{}, - EncryptionKeysByYAMLIdentity: map[string]encryptionkey.PrivKey{}, + SigningKeysByYAMLIdentity: map[string]signingkey2.PrivKey{}, + SigningKeysByRealIdentity: map[string]signingkey2.PrivKey{}, + EncryptionKeysByYAMLIdentity: map[string]encryptionkey2.PrivKey{}, GeneratedIdentities: map[string]string{}, ReadKeysByYAMLIdentity: map[string]*SymKey{}, ReadKeysByHash: map[uint64]*SymKey{}, @@ -57,16 +56,16 @@ func (k *YAMLKeychain) AddEncryptionKey(key *Key) { return } var ( - newPrivKey encryptionkey.PrivKey + newPrivKey encryptionkey2.PrivKey err error ) if key.Value == "generated" { - newPrivKey, _, err = encryptionkey.GenerateRandomRSAKeyPair(2048) + newPrivKey, _, err = encryptionkey2.GenerateRandomRSAKeyPair(2048) if err != nil { panic(err) } } else { - newPrivKey, err = keys.DecodeKeyFromString(key.Value, encryptionkey.NewEncryptionRsaPrivKeyFromBytes, nil) + newPrivKey, err = keys.DecodeKeyFromString(key.Value, encryptionkey2.NewEncryptionRsaPrivKeyFromBytes, nil) if err != nil { panic(err) } @@ -79,17 +78,17 @@ func (k *YAMLKeychain) AddSigningKey(key *Key) { return } var ( - newPrivKey signingkey.PrivKey - pubKey signingkey.PubKey + newPrivKey signingkey2.PrivKey + pubKey signingkey2.PubKey err error ) if key.Value == "generated" { - newPrivKey, pubKey, err = signingkey.GenerateRandomEd25519KeyPair() + newPrivKey, pubKey, err = signingkey2.GenerateRandomEd25519KeyPair() if err != nil { panic(err) } } else { - newPrivKey, err = keys.DecodeKeyFromString(key.Value, signingkey.NewSigningEd25519PrivKeyFromBytes, nil) + newPrivKey, err = keys.DecodeKeyFromString(key.Value, signingkey2.NewSigningEd25519PrivKeyFromBytes, nil) if err != nil { panic(err) } diff --git a/pkg/acl/testutils/acllistbuilder/liststoragebuilder.go b/common/pkg/acl/testutils/acllistbuilder/liststoragebuilder.go similarity index 74% rename from pkg/acl/testutils/acllistbuilder/liststoragebuilder.go rename to common/pkg/acl/testutils/acllistbuilder/liststoragebuilder.go index 7c0f4565..cf515bfd 100644 --- a/pkg/acl/testutils/acllistbuilder/liststoragebuilder.go +++ b/common/pkg/acl/testutils/acllistbuilder/liststoragebuilder.go @@ -3,12 +3,12 @@ package acllistbuilder import ( "context" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/yamltests" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cid" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/encryptionkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" + aclrecordproto2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/testutils/yamltests" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/cid" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/encryptionkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" "hash/fnv" "io/ioutil" "path" @@ -20,18 +20,18 @@ import ( type ACLListStorageBuilder struct { aclList string - records []*aclrecordproto.ACLRecord - rawRecords []*aclrecordproto.RawACLRecordWithId + records []*aclrecordproto2.ACLRecord + rawRecords []*aclrecordproto2.RawACLRecordWithId indexes map[string]int keychain *YAMLKeychain - rawRoot *aclrecordproto.RawACLRecordWithId - root *aclrecordproto.ACLRoot + rawRoot *aclrecordproto2.RawACLRecordWithId + root *aclrecordproto2.ACLRoot id string } func NewACLListStorageBuilder(keychain *YAMLKeychain) *ACLListStorageBuilder { return &ACLListStorageBuilder{ - records: make([]*aclrecordproto.ACLRecord, 0), + records: make([]*aclrecordproto2.ACLRecord, 0), indexes: make(map[string]int), keychain: keychain, } @@ -60,7 +60,7 @@ func NewACLListStorageBuilderFromFile(file string) (*ACLListStorageBuilder, erro return tb, nil } -func (t *ACLListStorageBuilder) createRaw(rec proto.Marshaler, identity []byte) *aclrecordproto.RawACLRecordWithId { +func (t *ACLListStorageBuilder) createRaw(rec proto.Marshaler, identity []byte) *aclrecordproto2.RawACLRecordWithId { protoMarshalled, err := rec.Marshal() if err != nil { panic("should be able to marshal final acl message!") @@ -71,7 +71,7 @@ func (t *ACLListStorageBuilder) createRaw(rec proto.Marshaler, identity []byte) panic("should be able to sign final acl message!") } - rawRec := &aclrecordproto.RawACLRecord{ + rawRec := &aclrecordproto2.RawACLRecord{ Payload: protoMarshalled, Signature: signature, } @@ -83,7 +83,7 @@ func (t *ACLListStorageBuilder) createRaw(rec proto.Marshaler, identity []byte) id, _ := cid.NewCIDFromBytes(rawMarshalled) - return &aclrecordproto.RawACLRecordWithId{ + return &aclrecordproto2.RawACLRecordWithId{ Payload: rawMarshalled, Id: id, } @@ -101,11 +101,11 @@ func (t *ACLListStorageBuilder) SetHead(headId string) error { panic("SetHead is not implemented") } -func (t *ACLListStorageBuilder) Root() (*aclrecordproto.RawACLRecordWithId, error) { +func (t *ACLListStorageBuilder) Root() (*aclrecordproto2.RawACLRecordWithId, error) { return t.rawRoot, nil } -func (t *ACLListStorageBuilder) GetRawRecord(ctx context.Context, id string) (*aclrecordproto.RawACLRecordWithId, error) { +func (t *ACLListStorageBuilder) GetRawRecord(ctx context.Context, id string) (*aclrecordproto2.RawACLRecordWithId, error) { recIdx, ok := t.indexes[id] if !ok { if id == t.rawRoot.Id { @@ -116,7 +116,7 @@ func (t *ACLListStorageBuilder) GetRawRecord(ctx context.Context, id string) (*a return t.rawRecords[recIdx], nil } -func (t *ACLListStorageBuilder) AddRawRecord(ctx context.Context, rec *aclrecordproto.RawACLRecordWithId) error { +func (t *ACLListStorageBuilder) AddRawRecord(ctx context.Context, rec *aclrecordproto2.RawACLRecordWithId) error { panic("implement me") } @@ -124,7 +124,7 @@ func (t *ACLListStorageBuilder) ID() (string, error) { return t.id, nil } -func (t *ACLListStorageBuilder) GetRawRecords() []*aclrecordproto.RawACLRecordWithId { +func (t *ACLListStorageBuilder) GetRawRecords() []*aclrecordproto2.RawACLRecordWithId { return t.rawRecords } @@ -149,19 +149,19 @@ func (t *ACLListStorageBuilder) Parse(tree *YMLList) { } } -func (t *ACLListStorageBuilder) parseRecord(rec *Record, prevId string) *aclrecordproto.ACLRecord { +func (t *ACLListStorageBuilder) parseRecord(rec *Record, prevId string) *aclrecordproto2.ACLRecord { k := t.keychain.GetKey(rec.ReadKey).(*SymKey) - var aclChangeContents []*aclrecordproto.ACLContentValue + var aclChangeContents []*aclrecordproto2.ACLContentValue for _, ch := range rec.AclChanges { aclChangeContent := t.parseACLChange(ch) aclChangeContents = append(aclChangeContents, aclChangeContent) } - data := &aclrecordproto.ACLData{ + data := &aclrecordproto2.ACLData{ AclContent: aclChangeContents, } bytes, _ := data.Marshal() - return &aclrecordproto.ACLRecord{ + return &aclrecordproto2.ACLRecord{ PrevId: prevId, Identity: []byte(t.keychain.GetIdentity(rec.Identity)), Data: bytes, @@ -170,7 +170,7 @@ func (t *ACLListStorageBuilder) parseRecord(rec *Record, prevId string) *aclreco } } -func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclrecordproto.ACLContentValue) { +func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclrecordproto2.ACLContentValue) { switch { case ch.UserAdd != nil: add := ch.UserAdd @@ -178,9 +178,9 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclrecord encKey := t.keychain.GetKey(add.EncryptionKey).(encryptionkey.PrivKey) rawKey, _ := encKey.GetPublic().Raw() - convCh = &aclrecordproto.ACLContentValue{ - Value: &aclrecordproto.ACLContentValue_UserAdd{ - UserAdd: &aclrecordproto.ACLUserAdd{ + convCh = &aclrecordproto2.ACLContentValue{ + Value: &aclrecordproto2.ACLContentValue_UserAdd{ + UserAdd: &aclrecordproto2.ACLUserAdd{ Identity: []byte(t.keychain.GetIdentity(add.Identity)), EncryptionKey: rawKey, EncryptedReadKeys: t.encryptReadKeys(add.EncryptedReadKeys, encKey), @@ -202,9 +202,9 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclrecord panic(err) } - convCh = &aclrecordproto.ACLContentValue{ - Value: &aclrecordproto.ACLContentValue_UserJoin{ - UserJoin: &aclrecordproto.ACLUserJoin{ + convCh = &aclrecordproto2.ACLContentValue{ + Value: &aclrecordproto2.ACLContentValue_UserJoin{ + UserJoin: &aclrecordproto2.ACLUserJoin{ Identity: []byte(t.keychain.GetIdentity(join.Identity)), EncryptionKey: rawKey, AcceptSignature: signature, @@ -220,9 +220,9 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclrecord GetKey(invite.EncryptionKey).(encryptionkey.PrivKey) rawEncKey, _ := encKey.GetPublic().Raw() - convCh = &aclrecordproto.ACLContentValue{ - Value: &aclrecordproto.ACLContentValue_UserInvite{ - UserInvite: &aclrecordproto.ACLUserInvite{ + convCh = &aclrecordproto2.ACLContentValue{ + Value: &aclrecordproto2.ACLContentValue_UserInvite{ + UserInvite: &aclrecordproto2.ACLUserInvite{ AcceptPublicKey: rawAcceptKey, EncryptPublicKey: rawEncKey, EncryptedReadKeys: t.encryptReadKeys(invite.EncryptedReadKeys, encKey), @@ -234,9 +234,9 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclrecord case ch.UserPermissionChange != nil: permissionChange := ch.UserPermissionChange - convCh = &aclrecordproto.ACLContentValue{ - Value: &aclrecordproto.ACLContentValue_UserPermissionChange{ - UserPermissionChange: &aclrecordproto.ACLUserPermissionChange{ + convCh = &aclrecordproto2.ACLContentValue{ + Value: &aclrecordproto2.ACLContentValue_UserPermissionChange{ + UserPermissionChange: &aclrecordproto2.ACLUserPermissionChange{ Identity: []byte(t.keychain.GetIdentity(permissionChange.Identity)), Permissions: t.convertPermission(permissionChange.Permission), }, @@ -247,7 +247,7 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclrecord newReadKey := t.keychain.GetKey(remove.NewReadKey).(*SymKey) - var replaces []*aclrecordproto.ACLReadKeyReplace + var replaces []*aclrecordproto2.ACLReadKeyReplace for _, id := range remove.IdentitiesLeft { encKey := t.keychain.EncryptionKeysByYAMLIdentity[id] rawEncKey, _ := encKey.GetPublic().Raw() @@ -255,16 +255,16 @@ func (t *ACLListStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclrecord if err != nil { panic(err) } - replaces = append(replaces, &aclrecordproto.ACLReadKeyReplace{ + replaces = append(replaces, &aclrecordproto2.ACLReadKeyReplace{ Identity: []byte(t.keychain.GetIdentity(id)), EncryptionKey: rawEncKey, EncryptedReadKey: encReadKey, }) } - convCh = &aclrecordproto.ACLContentValue{ - Value: &aclrecordproto.ACLContentValue_UserRemove{ - UserRemove: &aclrecordproto.ACLUserRemove{ + convCh = &aclrecordproto2.ACLContentValue{ + Value: &aclrecordproto2.ACLContentValue_UserRemove{ + UserRemove: &aclrecordproto2.ACLUserRemove{ Identity: []byte(t.keychain.GetIdentity(remove.RemovedIdentity)), ReadKeyReplaces: replaces, }, @@ -291,20 +291,20 @@ func (t *ACLListStorageBuilder) encryptReadKeys(keys []string, encKey encryption return } -func (t *ACLListStorageBuilder) convertPermission(perm string) aclrecordproto.ACLUserPermissions { +func (t *ACLListStorageBuilder) convertPermission(perm string) aclrecordproto2.ACLUserPermissions { switch perm { case "admin": - return aclrecordproto.ACLUserPermissions_Admin + return aclrecordproto2.ACLUserPermissions_Admin case "writer": - return aclrecordproto.ACLUserPermissions_Writer + return aclrecordproto2.ACLUserPermissions_Writer case "reader": - return aclrecordproto.ACLUserPermissions_Reader + return aclrecordproto2.ACLUserPermissions_Reader default: panic(fmt.Sprintf("incorrect permission: %s", perm)) } } -func (t *ACLListStorageBuilder) traverseFromHead(f func(rec *aclrecordproto.ACLRecord, id string) error) (err error) { +func (t *ACLListStorageBuilder) traverseFromHead(f func(rec *aclrecordproto2.ACLRecord, id string) error) (err error) { for i := len(t.records) - 1; i >= 0; i-- { err = f(t.records[i], t.rawRecords[i].Id) if err != nil { @@ -317,10 +317,10 @@ func (t *ACLListStorageBuilder) traverseFromHead(f func(rec *aclrecordproto.ACLR func (t *ACLListStorageBuilder) parseRoot(root *Root) { rawSignKey, _ := t.keychain.SigningKeysByYAMLIdentity[root.Identity].GetPublic().Raw() rawEncKey, _ := t.keychain.EncryptionKeysByYAMLIdentity[root.Identity].GetPublic().Raw() - readKey, _ := aclrecordproto.ACLReadKeyDerive(rawSignKey, rawEncKey) + readKey, _ := aclrecordproto2.ACLReadKeyDerive(rawSignKey, rawEncKey) hasher := fnv.New64() hasher.Write(readKey.Bytes()) - t.root = &aclrecordproto.ACLRoot{ + t.root = &aclrecordproto2.ACLRoot{ Identity: rawSignKey, EncryptionKey: rawEncKey, SpaceId: root.SpaceId, diff --git a/pkg/acl/testutils/acllistbuilder/liststoragebuildergraph.go b/common/pkg/acl/testutils/acllistbuilder/liststoragebuildergraph.go similarity index 100% rename from pkg/acl/testutils/acllistbuilder/liststoragebuildergraph.go rename to common/pkg/acl/testutils/acllistbuilder/liststoragebuildergraph.go diff --git a/pkg/acl/testutils/acllistbuilder/liststoragebuildergraph_nix.go b/common/pkg/acl/testutils/acllistbuilder/liststoragebuildergraph_nix.go similarity index 96% rename from pkg/acl/testutils/acllistbuilder/liststoragebuildergraph_nix.go rename to common/pkg/acl/testutils/acllistbuilder/liststoragebuildergraph_nix.go index 34b6fc92..ba2679f3 100644 --- a/pkg/acl/testutils/acllistbuilder/liststoragebuildergraph_nix.go +++ b/common/pkg/acl/testutils/acllistbuilder/liststoragebuildergraph_nix.go @@ -9,8 +9,7 @@ package acllistbuilder import ( "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" "github.com/gogo/protobuf/proto" "strings" "unicode" diff --git a/pkg/acl/testutils/acllistbuilder/ymlentities.go b/common/pkg/acl/testutils/acllistbuilder/ymlentities.go similarity index 100% rename from pkg/acl/testutils/acllistbuilder/ymlentities.go rename to common/pkg/acl/testutils/acllistbuilder/ymlentities.go diff --git a/pkg/acl/testutils/testchanges/proto/test.pb.go b/common/pkg/acl/testutils/testchanges/proto/test.pb.go similarity index 100% rename from pkg/acl/testutils/testchanges/proto/test.pb.go rename to common/pkg/acl/testutils/testchanges/proto/test.pb.go diff --git a/pkg/acl/testutils/testchanges/proto/test.proto b/common/pkg/acl/testutils/testchanges/proto/test.proto similarity index 100% rename from pkg/acl/testutils/testchanges/proto/test.proto rename to common/pkg/acl/testutils/testchanges/proto/test.proto diff --git a/pkg/acl/testutils/yamltests/path.go b/common/pkg/acl/testutils/yamltests/path.go similarity index 100% rename from pkg/acl/testutils/yamltests/path.go rename to common/pkg/acl/testutils/yamltests/path.go diff --git a/pkg/acl/testutils/yamltests/userjoinexample.yml b/common/pkg/acl/testutils/yamltests/userjoinexample.yml similarity index 100% rename from pkg/acl/testutils/yamltests/userjoinexample.yml rename to common/pkg/acl/testutils/yamltests/userjoinexample.yml diff --git a/pkg/acl/testutils/yamltests/userremoveexample.yml b/common/pkg/acl/testutils/yamltests/userremoveexample.yml similarity index 100% rename from pkg/acl/testutils/yamltests/userremoveexample.yml rename to common/pkg/acl/testutils/yamltests/userremoveexample.yml diff --git a/pkg/acl/tree/change.go b/common/pkg/acl/tree/change.go similarity index 93% rename from pkg/acl/tree/change.go rename to common/pkg/acl/tree/change.go index 43e9f654..308ee958 100644 --- a/pkg/acl/tree/change.go +++ b/common/pkg/acl/tree/change.go @@ -2,7 +2,7 @@ package tree import ( "errors" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" ) var ( diff --git a/pkg/acl/tree/changebuilder.go b/common/pkg/acl/tree/changebuilder.go similarity index 93% rename from pkg/acl/tree/changebuilder.go rename to common/pkg/acl/tree/changebuilder.go index 885dff34..9fd5c106 100644 --- a/pkg/acl/tree/changebuilder.go +++ b/common/pkg/acl/tree/changebuilder.go @@ -2,11 +2,11 @@ package tree import ( "errors" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/common" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cid" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/symmetric" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/common" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/cid" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/symmetric" "github.com/gogo/protobuf/proto" "time" ) diff --git a/pkg/acl/tree/changevalidator.go b/common/pkg/acl/tree/changevalidator.go similarity index 75% rename from pkg/acl/tree/changevalidator.go rename to common/pkg/acl/tree/changevalidator.go index 220eccb3..cd0b1234 100644 --- a/pkg/acl/tree/changevalidator.go +++ b/common/pkg/acl/tree/changevalidator.go @@ -2,15 +2,15 @@ package tree import ( "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/list" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + list2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/list" ) type ObjectTreeValidator interface { // ValidateFullTree should always be entered while holding a read lock on ACLList - ValidateFullTree(tree *Tree, aclList list.ACLList) error + ValidateFullTree(tree *Tree, aclList list2.ACLList) error // ValidateNewChanges should always be entered while holding a read lock on ACLList - ValidateNewChanges(tree *Tree, aclList list.ACLList, newChanges []*Change) error + ValidateNewChanges(tree *Tree, aclList list2.ACLList, newChanges []*Change) error } type objectTreeValidator struct{} @@ -19,7 +19,7 @@ func newTreeValidator() ObjectTreeValidator { return &objectTreeValidator{} } -func (v *objectTreeValidator) ValidateFullTree(tree *Tree, aclList list.ACLList) (err error) { +func (v *objectTreeValidator) ValidateFullTree(tree *Tree, aclList list2.ACLList) (err error) { tree.Iterate(tree.RootId(), func(c *Change) (isContinue bool) { err = v.validateChange(tree, aclList, c) return err == nil @@ -27,7 +27,7 @@ func (v *objectTreeValidator) ValidateFullTree(tree *Tree, aclList list.ACLList) return err } -func (v *objectTreeValidator) ValidateNewChanges(tree *Tree, aclList list.ACLList, newChanges []*Change) (err error) { +func (v *objectTreeValidator) ValidateNewChanges(tree *Tree, aclList list2.ACLList, newChanges []*Change) (err error) { for _, c := range newChanges { err = v.validateChange(tree, aclList, c) if err != nil { @@ -37,9 +37,9 @@ func (v *objectTreeValidator) ValidateNewChanges(tree *Tree, aclList list.ACLLis return } -func (v *objectTreeValidator) validateChange(tree *Tree, aclList list.ACLList, c *Change) (err error) { +func (v *objectTreeValidator) validateChange(tree *Tree, aclList list2.ACLList, c *Change) (err error) { var ( - perm list.UserPermissionPair + perm list2.UserPermissionPair state = aclList.ACLState() ) // checking if the user could write @@ -49,7 +49,7 @@ func (v *objectTreeValidator) validateChange(tree *Tree, aclList list.ACLList, c } if perm.Permission != aclrecordproto.ACLUserPermissions_Writer && perm.Permission != aclrecordproto.ACLUserPermissions_Admin { - err = list.ErrInsufficientPermissions + err = list2.ErrInsufficientPermissions return } diff --git a/pkg/acl/tree/descriptionparser.go b/common/pkg/acl/tree/descriptionparser.go similarity index 100% rename from pkg/acl/tree/descriptionparser.go rename to common/pkg/acl/tree/descriptionparser.go diff --git a/pkg/acl/tree/mock_objecttree/mock_objecttree.go b/common/pkg/acl/tree/mock_objecttree/mock_objecttree.go similarity index 93% rename from pkg/acl/tree/mock_objecttree/mock_objecttree.go rename to common/pkg/acl/tree/mock_objecttree/mock_objecttree.go index 70afa9d4..60935b3a 100644 --- a/pkg/acl/tree/mock_objecttree/mock_objecttree.go +++ b/common/pkg/acl/tree/mock_objecttree/mock_objecttree.go @@ -6,11 +6,11 @@ package mock_tree import ( context "context" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" + tree2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/tree" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" reflect "reflect" - storage "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" - tree "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree" - treechangeproto "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" gomock "github.com/golang/mock/gomock" ) @@ -38,10 +38,10 @@ func (m *MockObjectTree) EXPECT() *MockObjectTreeMockRecorder { } // AddContent mocks base method. -func (m *MockObjectTree) AddContent(arg0 context.Context, arg1 tree.SignableChangeContent) (tree.AddResult, error) { +func (m *MockObjectTree) AddContent(arg0 context.Context, arg1 tree2.SignableChangeContent) (tree2.AddResult, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "AddContent", arg0, arg1) - ret0, _ := ret[0].(tree.AddResult) + ret0, _ := ret[0].(tree2.AddResult) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -53,14 +53,14 @@ func (mr *MockObjectTreeMockRecorder) AddContent(arg0, arg1 interface{}) *gomock } // AddRawChanges mocks base method. -func (m *MockObjectTree) AddRawChanges(arg0 context.Context, arg1 ...*treechangeproto.RawTreeChangeWithId) (tree.AddResult, error) { +func (m *MockObjectTree) AddRawChanges(arg0 context.Context, arg1 ...*treechangeproto.RawTreeChangeWithId) (tree2.AddResult, error) { m.ctrl.T.Helper() varargs := []interface{}{arg0} for _, a := range arg1 { varargs = append(varargs, a) } ret := m.ctrl.Call(m, "AddRawChanges", varargs...) - ret0, _ := ret[0].(tree.AddResult) + ret0, _ := ret[0].(tree2.AddResult) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -177,7 +177,7 @@ func (mr *MockObjectTreeMockRecorder) ID() *gomock.Call { } // Iterate mocks base method. -func (m *MockObjectTree) Iterate(arg0 func([]byte) (interface{}, error), arg1 func(*tree.Change) bool) error { +func (m *MockObjectTree) Iterate(arg0 func([]byte) (interface{}, error), arg1 func(*tree2.Change) bool) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Iterate", arg0, arg1) ret0, _ := ret[0].(error) @@ -191,7 +191,7 @@ func (mr *MockObjectTreeMockRecorder) Iterate(arg0, arg1 interface{}) *gomock.Ca } // IterateFrom mocks base method. -func (m *MockObjectTree) IterateFrom(arg0 string, arg1 func([]byte) (interface{}, error), arg2 func(*tree.Change) bool) error { +func (m *MockObjectTree) IterateFrom(arg0 string, arg1 func([]byte) (interface{}, error), arg2 func(*tree2.Change) bool) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "IterateFrom", arg0, arg1, arg2) ret0, _ := ret[0].(error) @@ -241,10 +241,10 @@ func (mr *MockObjectTreeMockRecorder) RUnlock() *gomock.Call { } // Root mocks base method. -func (m *MockObjectTree) Root() *tree.Change { +func (m *MockObjectTree) Root() *tree2.Change { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Root") - ret0, _ := ret[0].(*tree.Change) + ret0, _ := ret[0].(*tree2.Change) return ret0 } diff --git a/pkg/acl/tree/objecttree.go b/common/pkg/acl/tree/objecttree.go similarity index 96% rename from pkg/acl/tree/objecttree.go rename to common/pkg/acl/tree/objecttree.go index 9e6323aa..e11e33ac 100644 --- a/pkg/acl/tree/objecttree.go +++ b/common/pkg/acl/tree/objecttree.go @@ -4,11 +4,11 @@ package tree import ( "context" "errors" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/common" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/list" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/symmetric" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/common" + list2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/list" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/symmetric" "sync" ) @@ -66,7 +66,7 @@ type objectTree struct { validator ObjectTreeValidator rawChangeLoader *rawChangeLoader treeBuilder *treeBuilder - aclList list.ACLList + aclList list2.ACLList id string root *treechangeproto.RawTreeChangeWithId @@ -91,13 +91,13 @@ type objectTreeDeps struct { treeStorage storage.TreeStorage validator ObjectTreeValidator rawChangeLoader *rawChangeLoader - aclList list.ACLList + aclList list2.ACLList } func defaultObjectTreeDeps( rootChange *treechangeproto.RawTreeChangeWithId, treeStorage storage.TreeStorage, - aclList list.ACLList) objectTreeDeps { + aclList list2.ACLList) objectTreeDeps { keychain := common.NewKeychain() changeBuilder := newChangeBuilder(keychain, rootChange) @@ -449,7 +449,7 @@ func (ot *objectTree) IterateFrom(id string, convert ChangeConvertFunc, iterate } readKey, exists := ot.keys[c.ReadKeyHash] if !exists { - err = list.ErrNoReadKey + err = list2.ErrNoReadKey return false } diff --git a/pkg/acl/tree/objecttree_test.go b/common/pkg/acl/tree/objecttree_test.go similarity index 96% rename from pkg/acl/tree/objecttree_test.go rename to common/pkg/acl/tree/objecttree_test.go index fcfab7e7..e2a2b028 100644 --- a/pkg/acl/tree/objecttree_test.go +++ b/common/pkg/acl/tree/objecttree_test.go @@ -2,10 +2,10 @@ package tree import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/list" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/acllistbuilder" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/list" + storage2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/testutils/acllistbuilder" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "testing" @@ -53,9 +53,9 @@ func (c *mockChangeCreator) createRaw(id, aclId, snapshotId string, isSnapshot b } } -func (c *mockChangeCreator) createNewTreeStorage(treeId, aclHeadId string) storage.TreeStorage { +func (c *mockChangeCreator) createNewTreeStorage(treeId, aclHeadId string) storage2.TreeStorage { root := c.createRoot(treeId, aclHeadId) - treeStorage, _ := storage.NewInMemoryTreeStorage(treeId, root, []string{root.Id}, []*treechangeproto.RawTreeChangeWithId{root}) + treeStorage, _ := storage2.NewInMemoryTreeStorage(treeId, root, []string{root.Id}, []*treechangeproto.RawTreeChangeWithId{root}) return treeStorage } @@ -95,7 +95,7 @@ func (m *mockChangeValidator) ValidateFullTree(tree *Tree, aclList list.ACLList) type testTreeContext struct { aclList list.ACLList - treeStorage storage.TreeStorage + treeStorage storage2.TreeStorage changeBuilder *mockChangeBuilder changeCreator *mockChangeCreator objTree ObjectTree diff --git a/pkg/acl/tree/objecttreefactory.go b/common/pkg/acl/tree/objecttreefactory.go similarity index 77% rename from pkg/acl/tree/objecttreefactory.go rename to common/pkg/acl/tree/objecttreefactory.go index 1db77908..72371327 100644 --- a/pkg/acl/tree/objecttreefactory.go +++ b/common/pkg/acl/tree/objecttreefactory.go @@ -1,12 +1,12 @@ package tree import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/list" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/symmetric" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/slice" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/list" + storage2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/symmetric" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/slice" "go.uber.org/zap" "math/rand" "time" @@ -19,7 +19,7 @@ type ObjectTreeCreatePayload struct { Identity []byte } -func BuildObjectTree(treeStorage storage.TreeStorage, aclList list.ACLList) (ObjectTree, error) { +func BuildObjectTree(treeStorage storage2.TreeStorage, aclList list.ACLList) (ObjectTree, error) { rootChange, err := treeStorage.Root() if err != nil { return nil, err @@ -31,14 +31,14 @@ func BuildObjectTree(treeStorage storage.TreeStorage, aclList list.ACLList) (Obj func CreateDerivedObjectTree( payload ObjectTreeCreatePayload, aclList list.ACLList, - createStorage storage.TreeStorageCreatorFunc) (objTree ObjectTree, err error) { + createStorage storage2.TreeStorageCreatorFunc) (objTree ObjectTree, err error) { return createObjectTree(payload, 0, nil, aclList, createStorage) } func CreateObjectTree( payload ObjectTreeCreatePayload, aclList list.ACLList, - createStorage storage.TreeStorageCreatorFunc) (objTree ObjectTree, err error) { + createStorage storage2.TreeStorageCreatorFunc) (objTree ObjectTree, err error) { bytes := make([]byte, 32) _, err = rand.Read(bytes) if err != nil { @@ -52,7 +52,7 @@ func createObjectTree( timestamp int64, seed []byte, aclList list.ACLList, - createStorage storage.TreeStorageCreatorFunc) (objTree ObjectTree, err error) { + createStorage storage2.TreeStorageCreatorFunc) (objTree ObjectTree, err error) { aclList.RLock() var ( deps = defaultObjectTreeDeps(nil, nil, aclList) @@ -80,7 +80,7 @@ func createObjectTree( deps.changeBuilder.SetRootRawChange(raw) // create storage - st, err := createStorage(storage.TreeStorageCreatePayload{ + st, err := createStorage(storage2.TreeStorageCreatePayload{ TreeId: raw.Id, RootRawChange: raw, Changes: []*treechangeproto.RawTreeChangeWithId{raw}, diff --git a/pkg/acl/tree/rawloader.go b/common/pkg/acl/tree/rawloader.go similarity index 96% rename from pkg/acl/tree/rawloader.go rename to common/pkg/acl/tree/rawloader.go index 6f6c8539..bc39688c 100644 --- a/pkg/acl/tree/rawloader.go +++ b/common/pkg/acl/tree/rawloader.go @@ -2,8 +2,8 @@ package tree import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" "time" ) diff --git a/common/pkg/acl/tree/rawtreevalidator.go b/common/pkg/acl/tree/rawtreevalidator.go new file mode 100644 index 00000000..aac7dd99 --- /dev/null +++ b/common/pkg/acl/tree/rawtreevalidator.go @@ -0,0 +1,17 @@ +package tree + +import ( + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/list" + storage2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" +) + +func ValidateRawTree(payload storage2.TreeStorageCreatePayload, aclList list.ACLList) (err error) { + provider := storage2.NewInMemoryTreeStorageProvider() + treeStorage, err := provider.CreateTreeStorage(payload) + if err != nil { + return + } + + _, err = BuildObjectTree(treeStorage, aclList) + return +} diff --git a/pkg/acl/tree/signablecontent.go b/common/pkg/acl/tree/signablecontent.go similarity index 59% rename from pkg/acl/tree/signablecontent.go rename to common/pkg/acl/tree/signablecontent.go index 4774c2fb..086d1b06 100644 --- a/pkg/acl/tree/signablecontent.go +++ b/common/pkg/acl/tree/signablecontent.go @@ -1,7 +1,7 @@ package tree import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" ) type SignableChangeContent struct { diff --git a/pkg/acl/tree/tree.go b/common/pkg/acl/tree/tree.go similarity index 100% rename from pkg/acl/tree/tree.go rename to common/pkg/acl/tree/tree.go diff --git a/pkg/acl/tree/tree_test.go b/common/pkg/acl/tree/tree_test.go similarity index 100% rename from pkg/acl/tree/tree_test.go rename to common/pkg/acl/tree/tree_test.go diff --git a/pkg/acl/tree/treebuilder.go b/common/pkg/acl/tree/treebuilder.go similarity index 96% rename from pkg/acl/tree/treebuilder.go rename to common/pkg/acl/tree/treebuilder.go index d7249d28..1250bfdc 100644 --- a/pkg/acl/tree/treebuilder.go +++ b/common/pkg/acl/tree/treebuilder.go @@ -4,9 +4,9 @@ import ( "context" "errors" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/slice" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/slice" "go.uber.org/zap" "time" ) diff --git a/pkg/acl/tree/treegraph.go b/common/pkg/acl/tree/treegraph.go similarity index 100% rename from pkg/acl/tree/treegraph.go rename to common/pkg/acl/tree/treegraph.go diff --git a/pkg/acl/tree/treegraph_nix.go b/common/pkg/acl/tree/treegraph_nix.go similarity index 100% rename from pkg/acl/tree/treegraph_nix.go rename to common/pkg/acl/tree/treegraph_nix.go diff --git a/pkg/acl/tree/treeiterator.go b/common/pkg/acl/tree/treeiterator.go similarity index 100% rename from pkg/acl/tree/treeiterator.go rename to common/pkg/acl/tree/treeiterator.go diff --git a/pkg/acl/tree/treereduce.go b/common/pkg/acl/tree/treereduce.go similarity index 100% rename from pkg/acl/tree/treereduce.go rename to common/pkg/acl/tree/treereduce.go diff --git a/pkg/acl/tree/util.go b/common/pkg/acl/tree/util.go similarity index 100% rename from pkg/acl/tree/util.go rename to common/pkg/acl/tree/util.go diff --git a/pkg/acl/treechangeproto/protos/treechange.proto b/common/pkg/acl/treechangeproto/protos/treechange.proto similarity index 100% rename from pkg/acl/treechangeproto/protos/treechange.proto rename to common/pkg/acl/treechangeproto/protos/treechange.proto diff --git a/pkg/acl/treechangeproto/treechange.pb.go b/common/pkg/acl/treechangeproto/treechange.pb.go similarity index 100% rename from pkg/acl/treechangeproto/treechange.pb.go rename to common/pkg/acl/treechangeproto/treechange.pb.go diff --git a/pkg/ldiff/diff.go b/common/pkg/ldiff/diff.go similarity index 100% rename from pkg/ldiff/diff.go rename to common/pkg/ldiff/diff.go diff --git a/pkg/ldiff/diff_test.go b/common/pkg/ldiff/diff_test.go similarity index 100% rename from pkg/ldiff/diff_test.go rename to common/pkg/ldiff/diff_test.go diff --git a/pkg/ldiff/mock_ldiff/mock_ldiff.go b/common/pkg/ldiff/mock_ldiff/mock_ldiff.go similarity index 98% rename from pkg/ldiff/mock_ldiff/mock_ldiff.go rename to common/pkg/ldiff/mock_ldiff/mock_ldiff.go index 183e7bef..e90d6812 100644 --- a/pkg/ldiff/mock_ldiff/mock_ldiff.go +++ b/common/pkg/ldiff/mock_ldiff/mock_ldiff.go @@ -6,9 +6,9 @@ package mock_ldiff import ( context "context" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ldiff" reflect "reflect" - ldiff "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ldiff" gomock "github.com/golang/mock/gomock" ) diff --git a/pkg/ocache/metrics.go b/common/pkg/ocache/metrics.go similarity index 100% rename from pkg/ocache/metrics.go rename to common/pkg/ocache/metrics.go diff --git a/pkg/ocache/ocache.go b/common/pkg/ocache/ocache.go similarity index 98% rename from pkg/ocache/ocache.go rename to common/pkg/ocache/ocache.go index 03e23ba7..76eb8e6a 100644 --- a/pkg/ocache/ocache.go +++ b/common/pkg/ocache/ocache.go @@ -3,7 +3,7 @@ package ocache import ( "context" "errors" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "go.uber.org/zap" "sync" "time" diff --git a/pkg/ocache/ocache_test.go b/common/pkg/ocache/ocache_test.go similarity index 100% rename from pkg/ocache/ocache_test.go rename to common/pkg/ocache/ocache_test.go diff --git a/util/cid/cid.go b/common/util/cid/cid.go similarity index 100% rename from util/cid/cid.go rename to common/util/cid/cid.go diff --git a/util/keys/asymmetric/encryptionkey/encryptionkey.go b/common/util/keys/asymmetric/encryptionkey/encryptionkey.go similarity index 67% rename from util/keys/asymmetric/encryptionkey/encryptionkey.go rename to common/util/keys/asymmetric/encryptionkey/encryptionkey.go index 970fcfdd..76e2f21f 100644 --- a/util/keys/asymmetric/encryptionkey/encryptionkey.go +++ b/common/util/keys/asymmetric/encryptionkey/encryptionkey.go @@ -1,6 +1,8 @@ package encryptionkey -import "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" +import ( + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys" +) type PrivKey interface { keys.Key diff --git a/util/keys/asymmetric/encryptionkey/rsa.go b/common/util/keys/asymmetric/encryptionkey/rsa.go similarity index 97% rename from util/keys/asymmetric/encryptionkey/rsa.go rename to common/util/keys/asymmetric/encryptionkey/rsa.go index 6c733488..a1afc2bb 100644 --- a/util/keys/asymmetric/encryptionkey/rsa.go +++ b/common/util/keys/asymmetric/encryptionkey/rsa.go @@ -7,7 +7,7 @@ import ( "crypto/subtle" "crypto/x509" "errors" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys" "io" ) diff --git a/util/keys/asymmetric/signingkey/ed25519.go b/common/util/keys/asymmetric/signingkey/ed25519.go similarity index 97% rename from util/keys/asymmetric/signingkey/ed25519.go rename to common/util/keys/asymmetric/signingkey/ed25519.go index beb4b6ed..3df2cd3d 100644 --- a/util/keys/asymmetric/signingkey/ed25519.go +++ b/common/util/keys/asymmetric/signingkey/ed25519.go @@ -7,7 +7,7 @@ import ( "crypto/subtle" "errors" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys" "io" ) diff --git a/util/keys/asymmetric/signingkey/signingkey.go b/common/util/keys/asymmetric/signingkey/signingkey.go similarity index 68% rename from util/keys/asymmetric/signingkey/signingkey.go rename to common/util/keys/asymmetric/signingkey/signingkey.go index 07576a67..c5d09011 100644 --- a/util/keys/asymmetric/signingkey/signingkey.go +++ b/common/util/keys/asymmetric/signingkey/signingkey.go @@ -1,6 +1,8 @@ package signingkey -import "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" +import ( + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys" +) type PrivKey interface { keys.Key diff --git a/util/keys/decode.go b/common/util/keys/decode.go similarity index 100% rename from util/keys/decode.go rename to common/util/keys/decode.go diff --git a/util/keys/key.go b/common/util/keys/key.go similarity index 100% rename from util/keys/key.go rename to common/util/keys/key.go diff --git a/util/keys/symmetric/symmetric.go b/common/util/keys/symmetric/symmetric.go similarity index 100% rename from util/keys/symmetric/symmetric.go rename to common/util/keys/symmetric/symmetric.go diff --git a/util/peer/peer.go b/common/util/peer/peer.go similarity index 79% rename from util/peer/peer.go rename to common/util/peer/peer.go index dfa03282..00f5d5c3 100644 --- a/util/peer/peer.go +++ b/common/util/peer/peer.go @@ -1,7 +1,7 @@ package peer import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/peer" ) diff --git a/util/slice/slice.go b/common/util/slice/slice.go similarity index 100% rename from util/slice/slice.go rename to common/util/slice/slice.go diff --git a/consensus/config/config.go b/consensus/config/config.go index 32ce5838..e50d92d5 100644 --- a/consensus/config/config.go +++ b/consensus/config/config.go @@ -1,8 +1,8 @@ package config import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + config2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" "gopkg.in/yaml.v3" "io/ioutil" ) @@ -22,11 +22,11 @@ func NewFromFile(path string) (c *Config, err error) { } type Config struct { - GrpcServer config.GrpcServer `yaml:"grpcServer"` - Account config.Account `yaml:"account"` - Mongo Mongo `yaml:"mongo"` - Metric config.Metric `yaml:"metric"` - Log config.Log `yaml:"log"` + GrpcServer config2.GrpcServer `yaml:"grpcServer"` + Account config2.Account `yaml:"account"` + Mongo Mongo `yaml:"mongo"` + Metric config2.Metric `yaml:"metric"` + Log config2.Log `yaml:"log"` } func (c *Config) Init(a *app.App) (err error) { @@ -41,14 +41,14 @@ func (c Config) GetMongo() Mongo { return c.Mongo } -func (c Config) GetGRPCServer() config.GrpcServer { +func (c Config) GetGRPCServer() config2.GrpcServer { return c.GrpcServer } -func (c Config) GetAccount() config.Account { +func (c Config) GetAccount() config2.Account { return c.Account } -func (c Config) GetMetric() config.Metric { +func (c Config) GetMetric() config2.Metric { return c.Metric } diff --git a/consensus/consensusclient/client.go b/consensus/consensusclient/client.go index 39a1bcf4..0df9fbdb 100644 --- a/consensus/consensusclient/client.go +++ b/consensus/consensusclient/client.go @@ -2,8 +2,8 @@ package consensusclient import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/pool" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/rpc/rpcerr" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/nodeconf" diff --git a/consensus/consensusrpc/consensrpc.go b/consensus/consensusrpc/consensrpc.go index 2c5d27c9..f1cafe1e 100644 --- a/consensus/consensusrpc/consensrpc.go +++ b/consensus/consensusrpc/consensrpc.go @@ -2,7 +2,7 @@ package consensusrpc import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/rpc/server" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/consensusproto" diff --git a/consensus/db/db.go b/consensus/db/db.go index f2c4540e..ac34d6a8 100644 --- a/consensus/db/db.go +++ b/consensus/db/db.go @@ -3,8 +3,8 @@ package db import ( "context" "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/config" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/consensusproto/consensuserr" diff --git a/consensus/db/db_test.go b/consensus/db/db_test.go index edbb3cfa..58acfef7 100644 --- a/consensus/db/db_test.go +++ b/consensus/db/db_test.go @@ -2,7 +2,7 @@ package db import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/config" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/consensusproto/consensuserr" diff --git a/consensus/stream/service.go b/consensus/stream/service.go index 2322c2f9..0aa407f1 100644 --- a/consensus/stream/service.go +++ b/consensus/stream/service.go @@ -2,12 +2,12 @@ package stream import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/metric" + ocache2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ocache" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/db" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ocache" "github.com/cheggaaa/mb/v2" "github.com/mr-tron/base58" "go.uber.org/zap" @@ -42,18 +42,22 @@ type Service interface { type service struct { db db.Service - cache ocache.OCache + cache ocache2.OCache lastStreamId uint64 } func (s *service) Init(a *app.App) (err error) { s.db = a.MustComponent(db.CName).(db.Service) - s.cache = ocache.New(s.loadLog, - ocache.WithTTL(cacheTTL), - ocache.WithRefCounter(false), - ocache.WithLogger(log.Named("cache").Sugar()), - ocache.WithPrometheus(a.MustComponent(metric.CName).(metric.Metric).Registry(), "consensus", "logcache"), - ) + + cacheOpts := []ocache2.Option{ + ocache2.WithTTL(cacheTTL), + ocache2.WithRefCounter(false), + ocache2.WithLogger(log.Named("cache").Sugar()), + } + if ms := a.Component(metric.CName); ms != nil { + cacheOpts = append(cacheOpts, ocache2.WithPrometheus(ms.(metric.Metric).Registry(), "consensus", "logcache")) + } + s.cache = ocache2.New(s.loadLog, cacheOpts...) return s.db.SetChangeReceiver(s.receiveChange) } @@ -95,7 +99,7 @@ func (s *service) RemoveStream(ctx context.Context, logId []byte, streamId uint6 return } -func (s *service) loadLog(ctx context.Context, id string) (value ocache.Object, err error) { +func (s *service) loadLog(ctx context.Context, id string) (value ocache2.Object, err error) { if ctxLog := ctx.Value(ctxLogKey); ctxLog != nil { return &object{ logId: ctxLog.(consensus.Log).Id, diff --git a/consensus/stream/service_test.go b/consensus/stream/service_test.go index 80643de4..8b3498a9 100644 --- a/consensus/stream/service_test.go +++ b/consensus/stream/service_test.go @@ -2,7 +2,7 @@ package stream import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/db" "github.com/stretchr/testify/assert" diff --git a/node/account/service.go b/node/account/service.go index 3181f9bd..66b2cb0a 100644 --- a/node/account/service.go +++ b/node/account/service.go @@ -1,13 +1,13 @@ package account import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" commonaccount "github.com/anytypeio/go-anytype-infrastructure-experiments/common/account" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/encryptionkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/account" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/encryptionkey" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/asymmetric/signingkey" ) type service struct { diff --git a/node/nodespace/nodecache/treecache.go b/node/nodespace/nodecache/treecache.go index 0b9446a3..5b72d1e2 100644 --- a/node/nodespace/nodecache/treecache.go +++ b/node/nodespace/nodecache/treecache.go @@ -3,11 +3,11 @@ package nodecache import ( "context" "errors" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/cache" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ocache" "github.com/anytypeio/go-anytype-infrastructure-experiments/node/nodespace" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ocache" "time" ) diff --git a/node/nodespace/service.go b/node/nodespace/service.go index 0920ef42..69d07917 100644 --- a/node/nodespace/service.go +++ b/node/nodespace/service.go @@ -2,14 +2,14 @@ package nodespace import ( "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage" + config2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/rpc/server" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ocache" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ocache" "time" ) @@ -27,14 +27,14 @@ type Service interface { } type service struct { - conf config.Space + conf config2.Space spaceCache ocache.OCache commonSpace commonspace.Service spaceStorageProvider storage.SpaceStorageProvider } func (s *service) Init(a *app.App) (err error) { - s.conf = a.MustComponent(config.CName).(*config.Config).Space + s.conf = a.MustComponent(config2.CName).(*config2.Config).Space s.commonSpace = a.MustComponent(commonspace.CName).(commonspace.Service) s.spaceStorageProvider = a.MustComponent(storage.CName).(storage.SpaceStorageProvider) s.spaceCache = ocache.New( diff --git a/node/storage/keys.go b/node/storage/keys.go index fe12046a..8792929a 100644 --- a/node/storage/keys.go +++ b/node/storage/keys.go @@ -1,7 +1,7 @@ package storage import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" "strings" ) diff --git a/node/storage/liststorage.go b/node/storage/liststorage.go index 142e175b..c3ba4f92 100644 --- a/node/storage/liststorage.go +++ b/node/storage/liststorage.go @@ -3,8 +3,8 @@ package storage import ( "context" "github.com/akrylysov/pogreb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" ) type listStorage struct { diff --git a/node/storage/spacestorage.go b/node/storage/spacestorage.go index 3bf41085..9ac3bfbb 100644 --- a/node/storage/spacestorage.go +++ b/node/storage/spacestorage.go @@ -4,7 +4,7 @@ import ( "github.com/akrylysov/pogreb" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" spacestorage "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" + storage2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" "path" "sync" "time" @@ -18,7 +18,7 @@ type spaceStorage struct { spaceId string objDb *pogreb.DB keys spaceKeys - aclStorage storage.ListStorage + aclStorage storage2.ListStorage header *spacesyncproto.RawSpaceHeaderWithId mx sync.Mutex } @@ -125,11 +125,11 @@ func (s *spaceStorage) ID() (string, error) { return s.spaceId, nil } -func (s *spaceStorage) TreeStorage(id string) (storage.TreeStorage, error) { +func (s *spaceStorage) TreeStorage(id string) (storage2.TreeStorage, error) { return newTreeStorage(s.objDb, id) } -func (s *spaceStorage) CreateTreeStorage(payload storage.TreeStorageCreatePayload) (ts storage.TreeStorage, err error) { +func (s *spaceStorage) CreateTreeStorage(payload storage2.TreeStorageCreatePayload) (ts storage2.TreeStorage, err error) { // we have mutex here, so we prevent overwriting the heads of a tree on concurrent creation s.mx.Lock() defer s.mx.Unlock() @@ -137,7 +137,7 @@ func (s *spaceStorage) CreateTreeStorage(payload storage.TreeStorageCreatePayloa return createTreeStorage(s.objDb, payload) } -func (s *spaceStorage) ACLStorage() (storage.ListStorage, error) { +func (s *spaceStorage) ACLStorage() (storage2.ListStorage, error) { return s.aclStorage, nil } diff --git a/node/storage/storageservice.go b/node/storage/storageservice.go index 92927595..9b703cd8 100644 --- a/node/storage/storageservice.go +++ b/node/storage/storageservice.go @@ -1,9 +1,9 @@ package storage import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/config" ) type storageService struct { diff --git a/node/storage/treestorage.go b/node/storage/treestorage.go index f600b6a2..919f2140 100644 --- a/node/storage/treestorage.go +++ b/node/storage/treestorage.go @@ -3,8 +3,8 @@ package storage import ( "context" "github.com/akrylysov/pogreb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treechangeproto" + storage2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" ) type treeStorage struct { @@ -14,14 +14,14 @@ type treeStorage struct { root *treechangeproto.RawTreeChangeWithId } -func newTreeStorage(db *pogreb.DB, treeId string) (ts storage.TreeStorage, err error) { +func newTreeStorage(db *pogreb.DB, treeId string) (ts storage2.TreeStorage, err error) { keys := newTreeKeys(treeId) heads, err := db.Get(keys.HeadsKey()) if err != nil { return } if heads == nil { - err = storage.ErrUnknownTreeId + err = storage2.ErrUnknownTreeId return } @@ -30,7 +30,7 @@ func newTreeStorage(db *pogreb.DB, treeId string) (ts storage.TreeStorage, err e return } if root == nil { - err = storage.ErrUnknownTreeId + err = storage2.ErrUnknownTreeId return } @@ -47,18 +47,18 @@ func newTreeStorage(db *pogreb.DB, treeId string) (ts storage.TreeStorage, err e return } -func createTreeStorage(db *pogreb.DB, payload storage.TreeStorageCreatePayload) (ts storage.TreeStorage, err error) { +func createTreeStorage(db *pogreb.DB, payload storage2.TreeStorageCreatePayload) (ts storage2.TreeStorage, err error) { keys := newTreeKeys(payload.TreeId) has, err := db.Has(keys.HeadsKey()) if err != nil { return } if has { - err = storage.ErrTreeExists + err = storage2.ErrTreeExists return } - heads := storage.CreateHeadsPayload(payload.Heads) + heads := storage2.CreateHeadsPayload(payload.Heads) for _, ch := range payload.Changes { err = db.Put(keys.RawChangeKey(ch.Id), ch.GetRawChange()) @@ -100,15 +100,15 @@ func (t *treeStorage) Heads() (heads []string, err error) { return } if heads == nil { - err = storage.ErrUnknownTreeId + err = storage2.ErrUnknownTreeId return } - heads = storage.ParseHeads(headsBytes) + heads = storage2.ParseHeads(headsBytes) return } func (t *treeStorage) SetHeads(heads []string) (err error) { - payload := storage.CreateHeadsPayload(heads) + payload := storage2.CreateHeadsPayload(heads) return t.db.Put(t.keys.HeadsKey(), payload) } @@ -122,7 +122,7 @@ func (t *treeStorage) GetRawChange(ctx context.Context, id string) (raw *treecha return } if res == nil { - err = storage.ErrUnkownChange + err = storage2.ErrUnkownChange } raw = &treechangeproto.RawTreeChangeWithId{ diff --git a/pkg/acl/account/accountdata.go b/pkg/acl/account/accountdata.go deleted file mode 100644 index 3d3fdf28..00000000 --- a/pkg/acl/account/accountdata.go +++ /dev/null @@ -1,12 +0,0 @@ -package account - -import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/encryptionkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" -) - -type AccountData struct { // TODO: create a convenient constructor for this - Identity []byte // public key - SignKey signingkey.PrivKey - EncKey encryptionkey.PrivKey -} diff --git a/pkg/acl/acltree/aclstatebuilder.go b/pkg/acl/acltree/aclstatebuilder.go deleted file mode 100644 index 8528f9cf..00000000 --- a/pkg/acl/acltree/aclstatebuilder.go +++ /dev/null @@ -1,186 +0,0 @@ -package acltree - -import ( - "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/aclpb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/encryptionkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" -) - -type aclStateBuilder struct { - tree *Tree - identity string - key encryptionkey.PrivKey - decoder signingkey.PubKeyDecoder -} - -type decreasedPermissionsParameters struct { - users []*aclpb.ACLChange_UserPermissionChange - startChange string -} - -func newACLStateBuilder(decoder signingkey.PubKeyDecoder, accountData *account.AccountData) *aclStateBuilder { - return &aclStateBuilder{ - decoder: decoder, - identity: accountData.Identity, - key: accountData.EncKey, - } -} - -func (sb *aclStateBuilder) Init(tree *Tree) error { - sb.tree = tree - return nil -} - -func (sb *aclStateBuilder) Build() (*ACLState, error) { - state, _, err := sb.BuildBefore("") - return state, err -} - -// 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) { - var ( - err error - startChange = sb.tree.root - foundId bool - idSeenMap = make(map[string][]*Change) - decreasedPermissions *decreasedPermissionsParameters - ) - root := sb.tree.Root() - if !root.IsSnapshot { - return nil, false, fmt.Errorf("root should always be a snapshot") - } - - state, err := newACLStateFromSnapshotChange( - root.Content, - sb.identity, - sb.key, - sb.decoder) - if err != nil { - return nil, false, fmt.Errorf("could not build ACLState from snapshot: %w", err) - } - - idSeenMap[startChange.Content.Identity] = append(idSeenMap[startChange.Content.Identity], startChange) - - if startChange.Content.GetChangesData() != nil { - key, exists := state.userReadKeys[startChange.Content.CurrentReadKeyHash] - if !exists { - return nil, false, fmt.Errorf("no first snapshot") - } - - err = startChange.DecryptContents(key) - if err != nil { - return nil, false, fmt.Errorf("failed to decrypt contents of first snapshot") - } - } - - if beforeId == startChange.Id { - return state, true, nil - } - - for { - // TODO: we should optimize this method to just remember last state of iterator and not iterate from the start and skip if nothing was removed from the Tree - sb.tree.IterateSkip(sb.tree.root.Id, startChange.Id, func(c *Change) (isContinue bool) { - defer func() { - if err == nil { - startChange = c - } else if err != ErrDocumentForbidden { - //log.Errorf("marking change %s as invalid: %v", c.Id, err) - sb.tree.RemoveInvalidChange(c.Id) - } - }() - - // not applying root change - if c.Id == startChange.Id { - return true - } - - idSeenMap[c.Content.Identity] = append(idSeenMap[c.Content.Identity], c) - if c.Content.GetAclData() != nil { - err = state.applyChange(c.Content) - if err != nil { - return false - } - - // if we have some users who have less permissions now - users := state.getPermissionDecreasedUsers(c.Content) - if len(users) > 0 { - decreasedPermissions = &decreasedPermissionsParameters{ - users: users, - startChange: c.Id, - } - return false - } - } - - // the user can't make changes - if !state.hasPermission(c.Content.Identity, aclpb.ACLChange_Writer) && !state.hasPermission(c.Content.Identity, aclpb.ACLChange_Admin) { - err = fmt.Errorf("user %s cannot make changes", c.Content.Identity) - return false - } - - // decrypting contents on the fly - if c.Content.GetChangesData() != nil { - key, exists := state.userReadKeys[c.Content.CurrentReadKeyHash] - if !exists { - err = fmt.Errorf("failed to find key with hash: %d", c.Content.CurrentReadKeyHash) - return false - } - - err = c.DecryptContents(key) - if err != nil { - err = fmt.Errorf("failed to decrypt contents for hash: %d", c.Content.CurrentReadKeyHash) - return false - } - } - - if c.Id == beforeId { - foundId = true - return false - } - - return true - }) - - // if we have users with decreased permissions - if decreasedPermissions != nil { - var removed bool - validChanges := sb.tree.dfs(decreasedPermissions.startChange) - - for _, permChange := range decreasedPermissions.users { - seenChanges := idSeenMap[permChange.Identity] - - for _, seen := range seenChanges { - // if we find some invalid changes - if _, exists := validChanges[seen.Id]; !exists { - // if the user didn't have enough permission to make changes - if seen.IsACLChange() || permChange.Permissions > aclpb.ACLChange_Writer { - removed = true - sb.tree.RemoveInvalidChange(seen.Id) - } - } - } - } - - decreasedPermissions = nil - if removed { - // starting from the beginning but with updated Tree - return sb.BuildBefore(beforeId) - } - } else if err == nil { - // we can finish the acl state building process - break - } - - // the user is forbidden to access the document - if err == ErrDocumentForbidden { - return nil, foundId, err - } - - // otherwise we have to continue from the change which we had - err = nil - } - - return state, foundId, err -} diff --git a/pkg/acl/acltree/change.go b/pkg/acl/acltree/change.go deleted file mode 100644 index a615dfa9..00000000 --- a/pkg/acl/acltree/change.go +++ /dev/null @@ -1,97 +0,0 @@ -package acltree - -import ( - "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/aclpb" - "github.com/gogo/protobuf/proto" - - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/symmetric" -) - -type ChangeContent struct { - ChangesData proto.Marshaler - ACLData *aclpb.ACLChange_ACLData - Id string // TODO: this is just for testing, because id should be created automatically from content -} - -// Change is an abstract type for all types of changes -type Change struct { - Next []*Change - Unattached []*Change - PreviousIds []string - Id string - SnapshotId string - IsSnapshot bool - DecryptedDocumentChange []byte - - Content *aclpb.ACLChange - Sign []byte -} - -func (ch *Change) DecryptContents(key *symmetric.Key) error { - if ch.Content.ChangesData == nil { - return nil - } - - decrypted, err := key.Decrypt(ch.Content.ChangesData) - if err != nil { - return fmt.Errorf("failed to decrypt changes data: %w", err) - } - - ch.DecryptedDocumentChange = decrypted - return nil -} - -func (ch *Change) IsACLChange() bool { - return ch.Content.GetAclData() != nil -} - -func NewFromRawChange(rawChange *aclpb.RawChange) (*Change, error) { - unmarshalled := &aclpb.ACLChange{} - err := proto.Unmarshal(rawChange.Payload, unmarshalled) - if err != nil { - return nil, err - } - - ch := NewChange(rawChange.Id, unmarshalled) - ch.Sign = rawChange.Signature - return ch, nil -} - -func NewChange(id string, ch *aclpb.ACLChange) *Change { - return &Change{ - Next: nil, - PreviousIds: ch.TreeHeadIds, - Id: id, - Content: ch, - SnapshotId: ch.SnapshotBaseId, - IsSnapshot: ch.GetAclData().GetAclSnapshot() != nil, - } -} - -func NewACLChange(id string, ch *aclpb.ACLChange) *Change { - return &Change{ - Next: nil, - PreviousIds: ch.AclHeadIds, - Id: id, - Content: ch, - SnapshotId: ch.SnapshotBaseId, - IsSnapshot: ch.GetAclData().GetAclSnapshot() != nil, - } -} - -func (ch *Change) ProtoChange() *aclpb.ACLChange { - return ch.Content -} - -func (ch *Change) DecryptedChangeContent() []byte { - return ch.DecryptedDocumentChange -} - -func (ch *Change) Signature() []byte { - return ch.Sign -} - -func (ch *Change) CID() string { - return ch.Id -} diff --git a/pkg/acl/common/keychain.go b/pkg/acl/common/keychain.go deleted file mode 100644 index b9fc0fb6..00000000 --- a/pkg/acl/common/keychain.go +++ /dev/null @@ -1,28 +0,0 @@ -package common - -import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" -) - -type Keychain struct { - keys map[string]signingkey.PubKey -} - -func NewKeychain() *Keychain { - return &Keychain{ - keys: make(map[string]signingkey.PubKey), - } -} - -func (k *Keychain) GetOrAdd(identity string) (signingkey.PubKey, error) { - if key, exists := k.keys[identity]; exists { - return key, nil - } - res, err := signingkey.NewSigningEd25519PubKeyFromBytes([]byte(identity)) - if err != nil { - return nil, err - } - - k.keys[identity] = res.(signingkey.PubKey) - return res.(signingkey.PubKey), nil -} diff --git a/pkg/acl/example/plaintextdocument/document.go b/pkg/acl/example/plaintextdocument/document.go deleted file mode 100644 index 2c66b5b1..00000000 --- a/pkg/acl/example/plaintextdocument/document.go +++ /dev/null @@ -1,169 +0,0 @@ -package plaintextdocument - -import ( - "context" - "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account" - aclpb "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/aclpb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/acltree" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/testchanges/testchangepb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treestorage" - - "github.com/gogo/protobuf/proto" -) - -type PlainTextDocument interface { - Text() string - AddText(ctx context.Context, text string) error -} - -// TODO: this struct is not thread-safe, so use it wisely :-) -type plainTextDocument struct { - heads []string - aclTree acltree.ACLTree - state *DocumentState -} - -func (p *plainTextDocument) Text() string { - if p.state != nil { - return p.state.Text - } - return "" -} - -func (p *plainTextDocument) AddText(ctx context.Context, text string) error { - _, err := p.aclTree.AddContent(ctx, func(builder acltree.ChangeBuilder) error { - builder.AddChangeContent( - &testchangepb.PlainTextChange_Data{ - Content: []*testchangepb.PlainTextChange_Content{ - createAppendTextChangeContent(text), - }, - }) - return nil - }) - return err -} - -func (p *plainTextDocument) Update(tree acltree.ACLTree) { - p.aclTree = tree - var err error - defer func() { - if err != nil { - fmt.Println("rebuild has returned error:", err) - } - }() - - prevHeads := p.heads - p.heads = tree.Heads() - startId := prevHeads[0] - tree.IterateFrom(startId, func(change *acltree.Change) (isContinue bool) { - if change.Id == startId { - return true - } - if change.DecryptedDocumentChange != nil { - p.state, err = p.state.ApplyChange(change.DecryptedDocumentChange, change.Id) - if err != nil { - return false - } - } - return true - }) -} - -func (p *plainTextDocument) Rebuild(tree acltree.ACLTree) { - p.aclTree = tree - p.heads = tree.Heads() - var startId string - var err error - defer func() { - if err != nil { - fmt.Println("rebuild has returned error:", err) - } - }() - - rootChange := tree.Root() - - if rootChange.DecryptedDocumentChange == nil { - err = fmt.Errorf("root doesn't have decrypted change") - return - } - - state, err := BuildDocumentStateFromChange(rootChange.DecryptedDocumentChange, rootChange.Id) - if err != nil { - return - } - - startId = rootChange.Id - tree.Iterate(func(change *acltree.Change) (isContinue bool) { - if startId == change.Id { - return true - } - if change.DecryptedDocumentChange != nil { - state, err = state.ApplyChange(change.DecryptedDocumentChange, change.Id) - if err != nil { - return false - } - } - return true - }) - if err != nil { - return - } - p.state = state -} - -func NewInMemoryPlainTextDocument(acc *account.AccountData, text string) (PlainTextDocument, error) { - return NewPlainTextDocument(acc, treestorage.NewInMemoryTreeStorage, text) -} - -func NewPlainTextDocument( - acc *account.AccountData, - create treestorage.CreatorFunc, - text string) (PlainTextDocument, error) { - changeBuilder := func(builder acltree.ChangeBuilder) error { - err := builder.UserAdd(acc.Identity, acc.EncKey.GetPublic(), aclpb.ACLChange_Admin) - if err != nil { - return err - } - builder.AddChangeContent(createInitialChangeContent(text)) - return nil - } - t, err := acltree.CreateNewTreeStorageWithACL( - acc, - changeBuilder, - create) - if err != nil { - return nil, err - } - - doc := &plainTextDocument{ - heads: nil, - aclTree: nil, - state: nil, - } - tree, err := acltree.BuildACLTree(t, acc, doc) - if err != nil { - return nil, err - } - doc.aclTree = tree - return doc, nil -} - -func createInitialChangeContent(text string) proto.Marshaler { - return &testchangepb.PlainTextChange_Data{ - Content: []*testchangepb.PlainTextChange_Content{ - createAppendTextChangeContent(text), - }, - Snapshot: &testchangepb.PlainTextChange_Snapshot{Text: text}, - } -} - -func createAppendTextChangeContent(text string) *testchangepb.PlainTextChange_Content { - return &testchangepb.PlainTextChange_Content{ - Value: &testchangepb.PlainTextChange_Content_TextAppend{ - TextAppend: &testchangepb.PlainTextChange_TextAppend{ - Text: text, - }, - }, - } -} diff --git a/pkg/acl/example/plaintextdocument/plaintextdocstate.go b/pkg/acl/example/plaintextdocument/plaintextdocstate.go deleted file mode 100644 index 22ac1e4d..00000000 --- a/pkg/acl/example/plaintextdocument/plaintextdocstate.go +++ /dev/null @@ -1,59 +0,0 @@ -package plaintextdocument - -import ( - "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/testchanges/testchangepb" - - "github.com/gogo/protobuf/proto" -) - -type DocumentState struct { - LastChangeId string - Text string -} - -func NewDocumentState(text string, id string) *DocumentState { - return &DocumentState{ - LastChangeId: id, - Text: text, - } -} - -func BuildDocumentStateFromChange(change []byte, id string) (*DocumentState, error) { - var changesData testchangepb.PlainTextChange_Data - err := proto.Unmarshal(change, &changesData) - if err != nil { - return nil, err - } - - if changesData.GetSnapshot() == nil { - return nil, fmt.Errorf("could not create state from empty snapshot") - } - return NewDocumentState(changesData.GetSnapshot().GetText(), id), nil -} - -func (p *DocumentState) ApplyChange(change []byte, id string) (*DocumentState, error) { - var changesData testchangepb.PlainTextChange_Data - err := proto.Unmarshal(change, &changesData) - if err != nil { - return nil, err - } - - for _, content := range changesData.GetContent() { - err = p.applyChange(content) - if err != nil { - return nil, err - } - } - p.LastChangeId = id - return p, nil -} - -func (p *DocumentState) applyChange(ch *testchangepb.PlainTextChange_Content) error { - switch { - case ch.GetTextAppend() != nil: - text := ch.GetTextAppend().GetText() - p.Text += "|" + text - } - return nil -} diff --git a/pkg/acl/testutils/treestoragebuilder/treestoragebuilder.go b/pkg/acl/testutils/treestoragebuilder/treestoragebuilder.go deleted file mode 100644 index 162a3f5f..00000000 --- a/pkg/acl/testutils/treestoragebuilder/treestoragebuilder.go +++ /dev/null @@ -1,539 +0,0 @@ -package treestoragebuilder - -import ( - "context" - "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/aclpb" - testpb "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/testchanges/testchangepb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/yamltests" - storagepb "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treestorage/treepb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/encryptionkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/slice" - "io/ioutil" - "path" - - "github.com/gogo/protobuf/proto" - "gopkg.in/yaml.v3" -) - -const plainTextDocType uint16 = 1 - -type treeChange struct { - *aclpb.ACLChange - id string - readKey *SymKey - signKey signingkey.PrivKey - - changesDataDecrypted []byte -} - -type updateUseCase struct { - changes map[string]*treeChange -} - -type TreeStorageBuilder struct { - treeId string - allChanges map[string]*treeChange - updates map[string]*updateUseCase - heads []string - orphans []string - keychain *Keychain - header *storagepb.TreeHeader -} - -func NewTreeStorageBuilder(keychain *Keychain) *TreeStorageBuilder { - return &TreeStorageBuilder{ - allChanges: make(map[string]*treeChange), - updates: make(map[string]*updateUseCase), - keychain: keychain, - } -} - -func NewTreeStorageBuilderWithTestName(name string) (*TreeStorageBuilder, error) { - filePath := path.Join(yamltests.Path(), name) - return NewTreeStorageBuilderFromFile(filePath) -} - -func NewTreeStorageBuilderFromFile(file string) (*TreeStorageBuilder, error) { - content, err := ioutil.ReadFile(file) - if err != nil { - return nil, err - } - - ymlTree := YMLTree{} - err = yaml.Unmarshal(content, &ymlTree) - if err != nil { - return nil, err - } - - tb := NewTreeStorageBuilder(NewKeychain()) - tb.Parse(&ymlTree) - - return tb, nil -} - -func (t *TreeStorageBuilder) TreeID() (string, error) { - return t.treeId, nil -} - -func (t *TreeStorageBuilder) GetKeychain() *Keychain { - return t.keychain -} - -func (t *TreeStorageBuilder) Heads() ([]string, error) { - return t.heads, nil -} - -func (t *TreeStorageBuilder) AddRawChange(change *aclpb.RawChange) error { - aclChange := new(aclpb.ACLChange) - var err error - - if err = proto.Unmarshal(change.Payload, aclChange); err != nil { - return fmt.Errorf("could not unmarshall changes") - } - var changesData []byte - - // get correct readkey - readKey := t.keychain.ReadKeysByHash[aclChange.CurrentReadKeyHash] - if aclChange.ChangesData != nil { - changesData, err = readKey.Key.Decrypt(aclChange.ChangesData) - if err != nil { - return fmt.Errorf("failed to decrypt changes data: %w", err) - } - } - - // get correct signing key - signKey := t.keychain.SigningKeysByIdentity[aclChange.Identity] - - t.allChanges[change.Id] = &treeChange{ - ACLChange: aclChange, - id: change.Id, - readKey: readKey, - signKey: signKey, - changesDataDecrypted: changesData, - } - return nil -} - -func (t *TreeStorageBuilder) AddOrphans(orphans ...string) error { - t.orphans = append(t.orphans, orphans...) - return nil -} - -func (t *TreeStorageBuilder) AddChange(change aclchanges.Change) error { - aclChange := change.ProtoChange() - var err error - var changesData []byte - - // get correct readkey - readKey := t.keychain.ReadKeysByHash[aclChange.CurrentReadKeyHash] - if aclChange.ChangesData != nil { - changesData, err = readKey.Key.Decrypt(aclChange.ChangesData) - if err != nil { - return fmt.Errorf("failed to decrypt changes data: %w", err) - } - } - - // get correct signing key - signKey := t.keychain.SigningKeysByIdentity[aclChange.Identity] - - t.allChanges[change.CID()] = &treeChange{ - ACLChange: aclChange, - id: change.CID(), - readKey: readKey, - signKey: signKey, - changesDataDecrypted: changesData, - } - return nil -} - -func (t *TreeStorageBuilder) Orphans() ([]string, error) { - return t.orphans, nil -} - -func (t *TreeStorageBuilder) SetHeads(heads []string) error { - // we should copy here instead of just setting the value - t.heads = heads - return nil -} - -func (t *TreeStorageBuilder) RemoveOrphans(orphans ...string) error { - t.orphans = slice.Difference(t.orphans, orphans) - return nil -} - -func (t *TreeStorageBuilder) GetChange(ctx context.Context, recordID string) (*aclpb.RawChange, error) { - return t.getChange(recordID, t.allChanges), nil -} - -func (t *TreeStorageBuilder) GetUpdates(useCase string) []*aclpb.RawChange { - var res []*aclpb.RawChange - update := t.updates[useCase] - for _, ch := range update.changes { - rawCh := t.getChange(ch.id, update.changes) - res = append(res, rawCh) - } - return res -} - -func (t *TreeStorageBuilder) Header() (*storagepb.TreeHeader, error) { - return t.header, nil -} - -func (t *TreeStorageBuilder) getChange(changeId string, m map[string]*treeChange) *aclpb.RawChange { - rec := m[changeId] - - if rec.changesDataDecrypted != nil { - encrypted, err := rec.readKey.Key.Encrypt(rec.changesDataDecrypted) - if err != nil { - panic("should be able to encrypt data with read key!") - } - - rec.ChangesData = encrypted - } - - aclMarshaled, err := proto.Marshal(rec.ACLChange) - if err != nil { - panic("should be able to marshal final acl message!") - } - - signature, err := rec.signKey.Sign(aclMarshaled) - if err != nil { - panic("should be able to sign final acl message!") - } - - transformedRec := &aclpb.RawChange{ - Payload: aclMarshaled, - Signature: signature, - Id: changeId, - } - return transformedRec -} - -func (t *TreeStorageBuilder) Parse(tree *YMLTree) { - // Just to clarify - we are generating new identities for the ones that - // are specified in the yml file, because our identities should be Ed25519 - // the same thing is happening for the encryption keys - t.keychain.ParseKeys(&tree.Keys) - t.treeId = t.parseTreeId(tree.Description) - for _, ch := range tree.Changes { - newChange := t.parseChange(ch) - t.allChanges[newChange.id] = newChange - } - - t.parseGraph(tree) - t.parseOrphans(tree) - t.parseHeader(tree) - t.parseUpdates(tree.Updates) -} - -func (t *TreeStorageBuilder) parseChange(ch *Change) *treeChange { - newChange := &treeChange{ - id: ch.Id, - } - k := t.keychain.GetKey(ch.ReadKey).(*SymKey) - newChange.readKey = k - newChange.signKey = t.keychain.SigningKeys[ch.Identity] - aclChange := &aclpb.ACLChange{} - aclChange.Identity = t.keychain.GetIdentity(ch.Identity) - if len(ch.AclChanges) > 0 || ch.AclSnapshot != nil { - aclChange.AclData = &aclpb.ACLChange_ACLData{} - if ch.AclSnapshot != nil { - aclChange.AclData.AclSnapshot = t.parseACLSnapshot(ch.AclSnapshot) - } - if ch.AclChanges != nil { - var aclChangeContents []*aclpb.ACLChange_ACLContentValue - for _, ch := range ch.AclChanges { - aclChangeContent := t.parseACLChange(ch) - aclChangeContents = append(aclChangeContents, aclChangeContent) - } - aclChange.AclData.AclContent = aclChangeContents - } - } - if len(ch.Changes) > 0 || ch.Snapshot != nil { - changesData := &testpb.PlainTextChange_Data{} - if ch.Snapshot != nil { - changesData.Snapshot = t.parseChangeSnapshot(ch.Snapshot) - } - if len(ch.Changes) > 0 { - var changeContents []*testpb.PlainTextChange_Content - for _, ch := range ch.Changes { - aclChangeContent := t.parseDocumentChange(ch) - changeContents = append(changeContents, aclChangeContent) - } - changesData.Content = changeContents - } - m, err := proto.Marshal(changesData) - if err != nil { - return nil - } - newChange.changesDataDecrypted = m - } - aclChange.CurrentReadKeyHash = k.Hash - newChange.ACLChange = aclChange - return newChange -} - -func (t *TreeStorageBuilder) parseTreeId(description *TreeDescription) string { - if description == nil { - panic("no author in tree") - } - return description.Author + ".tree.id" -} - -func (t *TreeStorageBuilder) parseChangeSnapshot(s *PlainTextSnapshot) *testpb.PlainTextChange_Snapshot { - return &testpb.PlainTextChange_Snapshot{ - Text: s.Text, - } -} - -func (t *TreeStorageBuilder) parseACLSnapshot(s *ACLSnapshot) *aclpb.ACLChange_ACLSnapshot { - newState := &aclpb.ACLChange_ACLState{} - for _, state := range s.UserStates { - aclUserState := &aclpb.ACLChange_UserState{} - aclUserState.Identity = t.keychain.GetIdentity(state.Identity) - - encKey := t.keychain. - GetKey(state.EncryptionKey).(encryptionkey.PrivKey) - rawKey, _ := encKey.GetPublic().Raw() - aclUserState.EncryptionKey = rawKey - - aclUserState.EncryptedReadKeys = t.encryptReadKeys(state.EncryptedReadKeys, encKey) - aclUserState.Permissions = t.convertPermission(state.Permissions) - newState.UserStates = append(newState.UserStates, aclUserState) - } - return &aclpb.ACLChange_ACLSnapshot{ - AclState: newState, - } -} - -func (t *TreeStorageBuilder) parseDocumentChange(ch *PlainTextChange) (convCh *testpb.PlainTextChange_Content) { - switch { - case ch.TextAppend != nil: - convCh = &testpb.PlainTextChange_Content{ - Value: &testpb.PlainTextChange_Content_TextAppend{ - TextAppend: &testpb.PlainTextChange_TextAppend{ - Text: ch.TextAppend.Text, - }, - }, - } - } - if convCh == nil { - panic("cannot have empty document change") - } - - return convCh -} - -func (t *TreeStorageBuilder) parseACLChange(ch *ACLChange) (convCh *aclpb.ACLChange_ACLContentValue) { - switch { - case ch.UserAdd != nil: - add := ch.UserAdd - - encKey := t.keychain. - GetKey(add.EncryptionKey).(encryptionkey.PrivKey) - rawKey, _ := encKey.GetPublic().Raw() - - convCh = &aclpb.ACLChange_ACLContentValue{ - Value: &aclpb.ACLChange_ACLContent_Value_UserAdd{ - UserAdd: &aclpb.ACLChange_UserAdd{ - Identity: t.keychain.GetIdentity(add.Identity), - EncryptionKey: rawKey, - EncryptedReadKeys: t.encryptReadKeys(add.EncryptedReadKeys, encKey), - Permissions: t.convertPermission(add.Permission), - }, - }, - } - case ch.UserJoin != nil: - join := ch.UserJoin - - encKey := t.keychain. - GetKey(join.EncryptionKey).(encryptionkey.PrivKey) - rawKey, _ := encKey.GetPublic().Raw() - - idKey, _ := t.keychain.SigningKeys[join.Identity].GetPublic().Raw() - signKey := t.keychain.GetKey(join.AcceptSignature).(signingkey.PrivKey) - signature, err := signKey.Sign(idKey) - if err != nil { - panic(err) - } - - convCh = &aclpb.ACLChange_ACLContentValue{ - Value: &aclpb.ACLChange_ACLContent_Value_UserJoin{ - UserJoin: &aclpb.ACLChange_UserJoin{ - Identity: t.keychain.GetIdentity(join.Identity), - EncryptionKey: rawKey, - AcceptSignature: signature, - UserInviteId: join.InviteId, - EncryptedReadKeys: t.encryptReadKeys(join.EncryptedReadKeys, encKey), - }, - }, - } - case ch.UserInvite != nil: - invite := ch.UserInvite - rawAcceptKey, _ := t.keychain.GetKey(invite.AcceptKey).(signingkey.PrivKey).GetPublic().Raw() - encKey := t.keychain. - GetKey(invite.EncryptionKey).(encryptionkey.PrivKey) - rawEncKey, _ := encKey.GetPublic().Raw() - - convCh = &aclpb.ACLChange_ACLContentValue{ - Value: &aclpb.ACLChange_ACLContent_Value_UserInvite{ - UserInvite: &aclpb.ACLChange_UserInvite{ - AcceptPublicKey: rawAcceptKey, - EncryptPublicKey: rawEncKey, - EncryptedReadKeys: t.encryptReadKeys(invite.EncryptedReadKeys, encKey), - Permissions: t.convertPermission(invite.Permissions), - InviteId: invite.InviteId, - }, - }, - } - case ch.UserConfirm != nil: - confirm := ch.UserConfirm - - convCh = &aclpb.ACLChange_ACLContentValue{ - Value: &aclpb.ACLChange_ACLContent_Value_UserConfirm{ - UserConfirm: &aclpb.ACLChange_UserConfirm{ - Identity: t.keychain.GetIdentity(confirm.Identity), - UserAddId: confirm.UserAddId, - }, - }, - } - case ch.UserPermissionChange != nil: - permissionChange := ch.UserPermissionChange - - convCh = &aclpb.ACLChange_ACLContentValue{ - Value: &aclpb.ACLChange_ACLContent_Value_UserPermissionChange{ - UserPermissionChange: &aclpb.ACLChange_UserPermissionChange{ - Identity: t.keychain.GetIdentity(permissionChange.Identity), - Permissions: t.convertPermission(permissionChange.Permission), - }, - }, - } - case ch.UserRemove != nil: - remove := ch.UserRemove - - newReadKey := t.keychain.GetKey(remove.NewReadKey).(*SymKey) - - var replaces []*aclpb.ACLChange_ReadKeyReplace - for _, id := range remove.IdentitiesLeft { - identity := t.keychain.GetIdentity(id) - encKey := t.keychain.EncryptionKeys[id] - rawEncKey, _ := encKey.GetPublic().Raw() - encReadKey, err := encKey.GetPublic().Encrypt(newReadKey.Key.Bytes()) - if err != nil { - panic(err) - } - replaces = append(replaces, &aclpb.ACLChange_ReadKeyReplace{ - Identity: identity, - EncryptionKey: rawEncKey, - EncryptedReadKey: encReadKey, - }) - } - - convCh = &aclpb.ACLChange_ACLContentValue{ - Value: &aclpb.ACLChange_ACLContent_Value_UserRemove{ - UserRemove: &aclpb.ACLChange_UserRemove{ - Identity: t.keychain.GetIdentity(remove.RemovedIdentity), - ReadKeyReplaces: replaces, - }, - }, - } - } - if convCh == nil { - panic("cannot have empty acl change") - } - - return convCh -} - -func (t *TreeStorageBuilder) encryptReadKeys(keys []string, encKey encryptionkey.PrivKey) (enc [][]byte) { - for _, k := range keys { - realKey := t.keychain.GetKey(k).(*SymKey).Key.Bytes() - res, err := encKey.GetPublic().Encrypt(realKey) - if err != nil { - panic(err) - } - - enc = append(enc, res) - } - return -} - -func (t *TreeStorageBuilder) convertPermission(perm string) aclpb.ACLChange_UserPermissions { - switch perm { - case "admin": - return aclpb.ACLChange_Admin - case "writer": - return aclpb.ACLChange_Writer - case "reader": - return aclpb.ACLChange_Reader - default: - panic(fmt.Sprintf("incorrect permission: %s", perm)) - } -} - -func (t *TreeStorageBuilder) traverseFromHeads(f func(t *treeChange) error) error { - uniqMap := map[string]struct{}{} - stack := make([]string, len(t.orphans), 10) - copy(stack, t.orphans) - for len(stack) > 0 { - id := stack[len(stack)-1] - stack = stack[:len(stack)-1] - if _, exists := uniqMap[id]; exists { - continue - } - - ch := t.allChanges[id] - uniqMap[id] = struct{}{} - if err := f(ch); err != nil { - return err - } - - for _, prev := range ch.ACLChange.TreeHeadIds { - stack = append(stack, prev) - } - } - return nil -} - -func (t *TreeStorageBuilder) parseUpdates(updates []*Update) { - for _, update := range updates { - useCase := &updateUseCase{ - changes: map[string]*treeChange{}, - } - for _, ch := range update.Changes { - newChange := t.parseChange(ch) - useCase.changes[newChange.id] = newChange - } - for _, node := range update.Graph { - rec := useCase.changes[node.Id] - rec.AclHeadIds = node.ACLHeads - rec.TreeHeadIds = node.TreeHeads - rec.SnapshotBaseId = node.BaseSnapshot - } - - t.updates[update.UseCase] = useCase - } -} - -func (t *TreeStorageBuilder) parseGraph(tree *YMLTree) { - for _, node := range tree.Graph { - rec := t.allChanges[node.Id] - rec.AclHeadIds = node.ACLHeads - rec.TreeHeadIds = node.TreeHeads - rec.SnapshotBaseId = node.BaseSnapshot - } -} - -func (t *TreeStorageBuilder) parseOrphans(tree *YMLTree) { - t.orphans = tree.Orphans -} - -func (t *TreeStorageBuilder) parseHeader(tree *YMLTree) { - t.header = &storagepb.TreeHeader{ - FirstChangeId: tree.Header.FirstChangeId, - IsWorkspace: tree.Header.IsWorkspace, - } -} diff --git a/pkg/acl/testutils/treestoragebuilder/treestoragebuildergraph_nix.go b/pkg/acl/testutils/treestoragebuilder/treestoragebuildergraph_nix.go deleted file mode 100644 index a68cb0a1..00000000 --- a/pkg/acl/testutils/treestoragebuilder/treestoragebuildergraph_nix.go +++ /dev/null @@ -1,162 +0,0 @@ -//go:build (linux || darwin) && !android && !ios && !nographviz && (amd64 || arm64) -// +build linux darwin -// +build !android -// +build !ios -// +build !nographviz -// +build amd64 arm64 - -package treestoragebuilder - -import ( - "fmt" - testpb "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/testchanges/testchangepb" - - "github.com/gogo/protobuf/proto" - "strings" - "unicode" - - "github.com/awalterschulze/gographviz" -) - -// To quickly look at visualized string you can use https://dreampuf.github.io/GraphvizOnline - -type EdgeParameters struct { - style string - color string - label string -} - -func (t *TreeStorageBuilder) Graph() (string, error) { - // TODO: check updates on https://github.com/goccy/go-graphviz/issues/52 or make a fix yourself to use better library here - graph := gographviz.NewGraph() - graph.SetName("G") - graph.SetDir(true) - var nodes = make(map[string]struct{}) - - var addNodes = func(r *treeChange) error { - // TODO: revisit function after checking - - style := "solid" - if r.GetAclData() != nil { - style = "filled" - } else if r.changesDataDecrypted != nil { - style = "dashed" - } - - var chSymbs []string - if r.changesDataDecrypted != nil { - res := &testpb.PlainTextChange_Data{} - err := proto.Unmarshal(r.changesDataDecrypted, res) - if err != nil { - return err - } - - for _, chc := range res.Content { - tp := fmt.Sprintf("%T", chc.Value) - tp = strings.Replace(tp, "ChangeContent_Value_", "", 1) - res := "" - for _, ts := range tp { - if unicode.IsUpper(ts) { - res += string(ts) - } - } - chSymbs = append(chSymbs, res) - } - } - if r.GetAclData() != nil { - for _, chc := range r.GetAclData().AclContent { - tp := fmt.Sprintf("%T", chc.Value) - tp = strings.Replace(tp, "ACLChange_ACLContent_Value_", "", 1) - res := "" - for _, ts := range tp { - if unicode.IsUpper(ts) { - res += string(ts) - } - } - chSymbs = append(chSymbs, res) - } - } - - shortId := r.id - label := fmt.Sprintf("Id: %s\nChanges: %s\n", - shortId, - strings.Join(chSymbs, ","), - ) - e := graph.AddNode("G", "\""+r.id+"\"", map[string]string{ - "label": "\"" + label + "\"", - "style": "\"" + style + "\"", - }) - if e != nil { - return e - } - nodes[r.id] = struct{}{} - return nil - } - - var createEdge = func(firstId, secondId string, params EdgeParameters) error { - _, exists := nodes[firstId] - if !exists { - return fmt.Errorf("no such node") - } - _, exists = nodes[secondId] - if !exists { - return fmt.Errorf("no previous node") - } - - err := graph.AddEdge("\""+firstId+"\"", "\""+secondId+"\"", true, map[string]string{ - "color": params.color, - "style": params.style, - }) - if err != nil { - return err - } - - return nil - } - - var addLinks = func(t *treeChange) error { - for _, prevId := range t.AclHeadIds { - err := createEdge(t.id, prevId, EdgeParameters{ - style: "dashed", - color: "red", - }) - if err != nil { - return err - } - } - - for _, prevId := range t.TreeHeadIds { - err := createEdge(t.id, prevId, EdgeParameters{ - style: "dashed", - color: "blue", - }) - if err != nil { - return err - } - } - - if t.SnapshotBaseId != "" { - err := createEdge(t.id, t.SnapshotBaseId, EdgeParameters{ - style: "bold", - color: "blue", - }) - if err != nil { - return err - } - } - - return nil - } - - err := t.traverseFromHeads(addNodes) - if err != nil { - return "", err - } - - err = t.traverseFromHeads(addLinks) - if err != nil { - return "", err - } - - return graph.String(), nil -} diff --git a/pkg/acl/tree/rawtreevalidator.go b/pkg/acl/tree/rawtreevalidator.go deleted file mode 100644 index 01cb1105..00000000 --- a/pkg/acl/tree/rawtreevalidator.go +++ /dev/null @@ -1,17 +0,0 @@ -package tree - -import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/list" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" -) - -func ValidateRawTree(payload storage.TreeStorageCreatePayload, aclList list.ACLList) (err error) { - provider := storage.NewInMemoryTreeStorageProvider() - treeStorage, err := provider.CreateTreeStorage(payload) - if err != nil { - return - } - - _, err = BuildObjectTree(treeStorage, aclList) - return -}