1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-10 18:10:49 +09:00

Add timestamp in create object methods

This commit is contained in:
mcrakhman 2023-04-25 18:17:59 +02:00 committed by Mikhail Iudin
parent 66f8dacdfe
commit 0b5336ed71
No known key found for this signature in database
GPG key ID: FAAAA8BAABDFF1C0
4 changed files with 106 additions and 18 deletions

View file

@ -2,8 +2,10 @@ package block
import (
"context"
"crypto/rand"
"errors"
"fmt"
"github.com/anytypeio/any-sync/util/crypto"
"time"
"github.com/anytypeio/any-sync/app/ocache"
@ -221,14 +223,11 @@ func (s *Service) CreateTreePayloadWithSpace(ctx context.Context, space commonsp
if err != nil {
return treestorage.TreeStorageCreatePayload{}, err
}
payload := objecttree.ObjectTreeCreatePayload{
PrivKey: s.commonAccount.Account().SignKey,
ChangeType: spaceservice.ChangeType,
ChangePayload: changePayload,
SpaceId: space.Id(),
IsEncrypted: true,
treePayload, err := createPayload(space.Id(), s.commonAccount.Account().SignKey, changePayload, time.Now().Unix())
if err != nil {
return treestorage.TreeStorageCreatePayload{}, err
}
return space.CreateTree(ctx, payload)
return space.CreateTree(ctx, treePayload)
}
func (s *Service) CreateTreeObjectWithPayload(ctx context.Context, payload treestorage.TreeStorageCreatePayload, initFunc InitFunc) (sb smartblock.SmartBlock, err error) {
@ -286,14 +285,8 @@ func (s *Service) DeriveTreeCreatePayload(
if err != nil {
return nil, err
}
payload := objecttree.ObjectTreeCreatePayload{
PrivKey: s.commonAccount.Account().SignKey,
ChangeType: spaceservice.ChangeType,
ChangePayload: changePayload,
SpaceId: space.Id(),
IsEncrypted: true,
}
create, err := space.DeriveTree(context.Background(), payload)
treePayload := derivePayload(space.Id(), s.commonAccount.Account().SignKey, changePayload)
create, err := space.CreateTree(context.Background(), treePayload)
return &create, err
}
@ -393,3 +386,29 @@ func createChangePayload(sbType coresb.SmartBlockType) (data []byte, err error)
payload := &model.ObjectChangePayload{SmartBlockType: model.SmartBlockType(sbType)}
return payload.Marshal()
}
func derivePayload(spaceId string, signKey crypto.PrivKey, changePayload []byte) objecttree.ObjectTreeCreatePayload {
return objecttree.ObjectTreeCreatePayload{
PrivKey: signKey,
ChangeType: spaceservice.ChangeType,
ChangePayload: changePayload,
SpaceId: spaceId,
IsEncrypted: true,
}
}
func createPayload(spaceId string, signKey crypto.PrivKey, changePayload []byte, timestamp int64) (objecttree.ObjectTreeCreatePayload, error) {
seed := make([]byte, 32)
if _, err := rand.Read(seed); err != nil {
return objecttree.ObjectTreeCreatePayload{}, err
}
return objecttree.ObjectTreeCreatePayload{
PrivKey: signKey,
ChangeType: spaceservice.ChangeType,
ChangePayload: changePayload,
SpaceId: spaceId,
IsEncrypted: true,
Timestamp: timestamp,
Seed: seed,
}, nil
}

69
core/block/cache_test.go Normal file
View file

@ -0,0 +1,69 @@
package block
import (
"github.com/anytypeio/any-sync/commonspace/object/accountdata"
"github.com/anytypeio/any-sync/commonspace/object/acl/list"
"github.com/anytypeio/any-sync/commonspace/object/tree/objecttree"
"github.com/anytypeio/any-sync/commonspace/object/tree/treechangeproto"
"github.com/anytypeio/any-sync/util/crypto"
spaceservice "github.com/anytypeio/go-anytype-middleware/space"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/require"
"testing"
"time"
)
func Test_Payloads(t *testing.T) {
// doing some any-sync preparations
changePayload := []byte("some")
keys, err := accountdata.NewRandom()
require.NoError(t, err)
aclList, err := list.NewTestDerivedAcl("spaceId", keys)
require.NoError(t, err)
timestamp := time.Now().Add(time.Hour).Unix()
checkRoot := func(root *treechangeproto.RawTreeChangeWithId, changePayload []byte, changeType string, timestamp int64) {
builder := objecttree.NewChangeBuilder(crypto.NewKeyStorage(), root)
ch, err := builder.Unmarshall(root, true)
require.NoError(t, err)
rootModel := &treechangeproto.TreeChangeInfo{}
err = proto.Unmarshal(ch.Data, rootModel)
require.NoError(t, err)
require.Equal(t, rootModel.ChangePayload, changePayload)
require.Equal(t, rootModel.ChangeType, spaceservice.ChangeType)
require.Equal(t, ch.Timestamp, timestamp)
}
t.Run("test create payload", func(t *testing.T) {
firstPayload, err := createPayload("spaceId", keys.SignKey, changePayload, timestamp)
require.NoError(t, err)
firstRoot, err := objecttree.CreateObjectTreeRoot(firstPayload, aclList)
require.NoError(t, err)
secondPayload, err := createPayload("spaceId", keys.SignKey, changePayload, timestamp)
require.NoError(t, err)
secondRoot, err := objecttree.CreateObjectTreeRoot(secondPayload, aclList)
require.NoError(t, err)
// checking that created roots are not equal
require.NotEqual(t, firstRoot, secondRoot)
checkRoot(firstRoot, changePayload, spaceservice.ChangeType, timestamp)
checkRoot(secondRoot, changePayload, spaceservice.ChangeType, timestamp)
})
t.Run("test derive payload", func(t *testing.T) {
firstPayload := derivePayload("spaceId", keys.SignKey, changePayload)
firstRoot, err := objecttree.CreateObjectTreeRoot(firstPayload, aclList)
require.NoError(t, err)
secondPayload := derivePayload("spaceId", keys.SignKey, changePayload)
secondRoot, err := objecttree.CreateObjectTreeRoot(secondPayload, aclList)
require.NoError(t, err)
// checking that derived roots are equal
require.Equal(t, firstRoot, secondRoot)
checkRoot(firstRoot, changePayload, spaceservice.ChangeType, 0)
})
}

2
go.mod
View file

@ -7,7 +7,7 @@ require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/VividCortex/ewma v1.2.0
github.com/adrium/goheif v0.0.0-20230113233934-ca402e77a786
github.com/anytypeio/any-sync v0.0.39
github.com/anytypeio/any-sync v0.0.41
github.com/anytypeio/go-naturaldate/v2 v2.0.1
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/blevesearch/bleve/v2 v2.3.6

4
go.sum
View file

@ -42,8 +42,8 @@ github.com/andybalholm/cascadia v1.2.0/go.mod h1:YCyR8vOZT9aZ1CHEd8ap0gMVm2aFgxB
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/anytypeio/any-sync v0.0.39 h1:4A4CZxCK5AYZSI50Pi+XoOSjVmggxAls8ZkVb3voGCQ=
github.com/anytypeio/any-sync v0.0.39/go.mod h1:G8ScbRRMkdlvbWmBC6lYCw5e5I7FWD5hQG8mpNrrSDQ=
github.com/anytypeio/any-sync v0.0.41 h1:S5vrsxQxU1KC6nlSr83scHmqlR2MPfDl5OLyvBTRsII=
github.com/anytypeio/any-sync v0.0.41/go.mod h1:G8ScbRRMkdlvbWmBC6lYCw5e5I7FWD5hQG8mpNrrSDQ=
github.com/anytypeio/go-chash v0.1.0 h1:nYCMh13SEai/7cXRUoKfU27uASj7XEF6NhvY6hFMKY8=
github.com/anytypeio/go-chash v0.1.0/go.mod h1:Q7XiggkMrThRFAwYSItzLOT9OPC8a497SLZtgmJtC/I=
github.com/anytypeio/go-ds-badger3 v0.3.1-0.20221103102622-3233d4e13cb8 h1:LC9w0M0SbA5VuZeBtUdq+uR4mdjbJhxurNtovmRiOrU=