fix(CommunityMember)_: update LastUpdateClock

This commit is contained in:
Mykhailo Prakhov 2024-07-05 15:22:22 +02:00
parent fa05248ad3
commit 6333e2a053
6 changed files with 28 additions and 56 deletions

View File

@ -2384,7 +2384,7 @@ func (o *Community) RequestsToJoin() []*RequestToJoin {
return o.config.RequestsToJoin return o.config.RequestsToJoin
} }
func (o *Community) AddMember(publicKey *ecdsa.PublicKey, roles []protobuf.CommunityMember_Roles) (*CommunityChanges, error) { func (o *Community) AddMember(publicKey *ecdsa.PublicKey, roles []protobuf.CommunityMember_Roles, lastUpdateClock uint64) (*CommunityChanges, error) {
if !o.IsControlNode() { if !o.IsControlNode() {
return nil, ErrNotControlNode return nil, ErrNotControlNode
} }
@ -2397,11 +2397,12 @@ func (o *Community) AddMember(publicKey *ecdsa.PublicKey, roles []protobuf.Commu
} }
if _, ok := o.config.CommunityDescription.Members[memberKey]; !ok { if _, ok := o.config.CommunityDescription.Members[memberKey]; !ok {
o.config.CommunityDescription.Members[memberKey] = &protobuf.CommunityMember{Roles: roles} o.config.CommunityDescription.Members[memberKey] = &protobuf.CommunityMember{Roles: roles, LastUpdateClock: lastUpdateClock}
changes.MembersAdded[memberKey] = o.config.CommunityDescription.Members[memberKey] changes.MembersAdded[memberKey] = o.config.CommunityDescription.Members[memberKey]
} }
o.increaseClock() o.increaseClock()
return changes, nil return changes, nil
} }
@ -2506,21 +2507,6 @@ func (o *Community) AllowsAllMembersToPinMessage() bool {
return o.config.CommunityDescription.AdminSettings != nil && o.config.CommunityDescription.AdminSettings.PinMessageAllMembersEnabled return o.config.CommunityDescription.AdminSettings != nil && o.config.CommunityDescription.AdminSettings.PinMessageAllMembersEnabled
} }
func (o *Community) AddMemberWithRevealedAccounts(dbRequest *RequestToJoin, roles []protobuf.CommunityMember_Roles, accounts []*protobuf.RevealedAccount) (*CommunityChanges, error) {
o.mutex.Lock()
defer o.mutex.Unlock()
if !o.IsControlNode() {
return nil, ErrNotControlNode
}
changes := o.addMemberWithRevealedAccounts(dbRequest.PublicKey, roles, accounts, dbRequest.Clock)
o.increaseClock()
return changes, nil
}
func (o *Community) CreateDeepCopy() *Community { func (o *Community) CreateDeepCopy() *Community {
return &Community{ return &Community{
encryptor: o.encryptor, encryptor: o.encryptor,
@ -2753,25 +2739,6 @@ func (o *Community) deleteTokenPermission(permissionID string) (*CommunityChange
return changes, nil return changes, nil
} }
func (o *Community) addMemberWithRevealedAccounts(memberKey string, roles []protobuf.CommunityMember_Roles, accounts []*protobuf.RevealedAccount, clock uint64) *CommunityChanges {
changes := o.emptyCommunityChanges()
if o.config.CommunityDescription.Members == nil {
o.config.CommunityDescription.Members = make(map[string]*protobuf.CommunityMember)
}
if _, ok := o.config.CommunityDescription.Members[memberKey]; !ok {
o.config.CommunityDescription.Members[memberKey] = &protobuf.CommunityMember{Roles: roles}
changes.MembersAdded[memberKey] = o.config.CommunityDescription.Members[memberKey]
}
o.config.CommunityDescription.Members[memberKey].RevealedAccounts = accounts
o.config.CommunityDescription.Members[memberKey].LastUpdateClock = clock
changes.MemberWalletsAdded[memberKey] = o.config.CommunityDescription.Members[memberKey].RevealedAccounts
return changes
}
func (o *Community) DeclineRequestToJoin(dbRequest *RequestToJoin) (adminEventCreated bool, err error) { func (o *Community) DeclineRequestToJoin(dbRequest *RequestToJoin) (adminEventCreated bool, err error) {
o.mutex.Lock() o.mutex.Lock()
defer o.mutex.Unlock() defer o.mutex.Unlock()

View File

@ -4,6 +4,7 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"reflect" "reflect"
"testing" "testing"
"time"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
@ -506,12 +507,12 @@ func (s *CommunityEncryptionKeyActionSuite) TestCommunityLevelKeyActions_Members
modified := origin.CreateDeepCopy() modified := origin.CreateDeepCopy()
for _, member := range tc.originMembers { for _, member := range tc.originMembers {
_, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}) _, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}, origin.Clock())
s.Require().NoError(err) s.Require().NoError(err)
} }
for _, member := range tc.modifiedMembers { for _, member := range tc.modifiedMembers {
_, err := modified.AddMember(member, []protobuf.CommunityMember_Roles{}) _, err := modified.AddMember(member, []protobuf.CommunityMember_Roles{}, origin.Clock())
s.Require().NoError(err) s.Require().NoError(err)
} }
@ -608,7 +609,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestCommunityLevelKeyActions_Permiss
s.Require().NoError(err) s.Require().NoError(err)
} }
for _, member := range tc.originMembers { for _, member := range tc.originMembers {
_, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}) _, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}, origin.Clock())
s.Require().NoError(err) s.Require().NoError(err)
} }
@ -617,7 +618,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestCommunityLevelKeyActions_Permiss
s.Require().NoError(err) s.Require().NoError(err)
} }
for _, member := range tc.modifiedMembers { for _, member := range tc.modifiedMembers {
_, err := modified.AddMember(member, []protobuf.CommunityMember_Roles{}) _, err := modified.AddMember(member, []protobuf.CommunityMember_Roles{}, origin.Clock())
s.Require().NoError(err) s.Require().NoError(err)
} }
@ -759,7 +760,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestChannelLevelKeyActions() {
s.Require().NoError(err) s.Require().NoError(err)
} }
for _, member := range tc.originMembers { for _, member := range tc.originMembers {
_, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}) _, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}, origin.Clock())
s.Require().NoError(err) s.Require().NoError(err)
_, err = origin.AddMemberToChat(channelID, member, []protobuf.CommunityMember_Roles{}, protobuf.CommunityMember_CHANNEL_ROLE_POSTER) _, err = origin.AddMemberToChat(channelID, member, []protobuf.CommunityMember_Roles{}, protobuf.CommunityMember_CHANNEL_ROLE_POSTER)
s.Require().NoError(err) s.Require().NoError(err)
@ -770,7 +771,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestChannelLevelKeyActions() {
s.Require().NoError(err) s.Require().NoError(err)
} }
for _, member := range tc.modifiedMembers { for _, member := range tc.modifiedMembers {
_, err := modified.AddMember(member, []protobuf.CommunityMember_Roles{}) _, err := modified.AddMember(member, []protobuf.CommunityMember_Roles{}, origin.Clock())
s.Require().NoError(err) s.Require().NoError(err)
_, err = modified.AddMemberToChat(channelID, member, []protobuf.CommunityMember_Roles{}, protobuf.CommunityMember_CHANNEL_ROLE_POSTER) _, err = modified.AddMemberToChat(channelID, member, []protobuf.CommunityMember_Roles{}, protobuf.CommunityMember_CHANNEL_ROLE_POSTER)
s.Require().NoError(err) s.Require().NoError(err)
@ -832,7 +833,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestNilOrigin() {
func (s *CommunityEncryptionKeyActionSuite) TestControlNodeChange() { func (s *CommunityEncryptionKeyActionSuite) TestControlNodeChange() {
channelID := "1234" channelID := "1234"
chatID := types.EncodeHex(crypto.CompressPubkey(&s.identity.PublicKey)) + channelID chatID := types.EncodeHex(crypto.CompressPubkey(&s.identity.PublicKey)) + channelID
clock := uint64(time.Now().Unix())
testCases := []struct { testCases := []struct {
name string name string
permissions []*protobuf.CommunityTokenPermission permissions []*protobuf.CommunityTokenPermission
@ -874,8 +875,8 @@ func (s *CommunityEncryptionKeyActionSuite) TestControlNodeChange() {
CommunityKeyAction: EncryptionKeyAction{ CommunityKeyAction: EncryptionKeyAction{
ActionType: EncryptionKeyRekey, ActionType: EncryptionKeyRekey,
Members: map[string]*protobuf.CommunityMember{ Members: map[string]*protobuf.CommunityMember{
s.member1Key: &protobuf.CommunityMember{}, s.member1Key: &protobuf.CommunityMember{LastUpdateClock: clock},
s.member2Key: &protobuf.CommunityMember{}, s.member2Key: &protobuf.CommunityMember{LastUpdateClock: clock},
}, },
}, },
ChannelKeysActions: map[string]EncryptionKeyAction{ ChannelKeysActions: map[string]EncryptionKeyAction{
@ -935,8 +936,12 @@ func (s *CommunityEncryptionKeyActionSuite) TestControlNodeChange() {
CommunityKeyAction: EncryptionKeyAction{ CommunityKeyAction: EncryptionKeyAction{
ActionType: EncryptionKeyRekey, ActionType: EncryptionKeyRekey,
Members: map[string]*protobuf.CommunityMember{ Members: map[string]*protobuf.CommunityMember{
s.member1Key: &protobuf.CommunityMember{}, s.member1Key: &protobuf.CommunityMember{
s.member2Key: &protobuf.CommunityMember{}, LastUpdateClock: clock,
},
s.member2Key: &protobuf.CommunityMember{
LastUpdateClock: clock,
},
}, },
}, },
ChannelKeysActions: map[string]EncryptionKeyAction{ ChannelKeysActions: map[string]EncryptionKeyAction{
@ -968,7 +973,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestControlNodeChange() {
s.Require().NoError(err) s.Require().NoError(err)
} }
for _, member := range tc.members { for _, member := range tc.members {
_, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}) _, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}, clock)
s.Require().NoError(err) s.Require().NoError(err)
} }
for _, member := range tc.channelMembers { for _, member := range tc.channelMembers {

View File

@ -2807,7 +2807,7 @@ func (m *Manager) AcceptRequestToJoin(dbRequest *RequestToJoin) (*Community, err
memberRoles = []protobuf.CommunityMember_Roles{role} memberRoles = []protobuf.CommunityMember_Roles{role}
} }
_, err = community.AddMember(pk, memberRoles) _, err = community.AddMember(pk, memberRoles, dbRequest.Clock)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3693,7 +3693,7 @@ func (m *Manager) AddMemberOwnerToCommunity(communityID types.HexBytes, pk *ecds
return nil, err return nil, err
} }
_, err = community.AddMember(pk, []protobuf.CommunityMember_Roles{protobuf.CommunityMember_ROLE_OWNER}) _, err = community.AddMember(pk, []protobuf.CommunityMember_Roles{protobuf.CommunityMember_ROLE_OWNER}, community.Clock())
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -4677,7 +4677,7 @@ func (m *Manager) promoteSelfToControlNode(community *Community, clock uint64) (
if exists := community.HasMember(&m.identity.PublicKey); !exists { if exists := community.HasMember(&m.identity.PublicKey); !exists {
ownerRole := []protobuf.CommunityMember_Roles{protobuf.CommunityMember_ROLE_OWNER} ownerRole := []protobuf.CommunityMember_Roles{protobuf.CommunityMember_ROLE_OWNER}
_, err = community.AddMember(&m.identity.PublicKey, ownerRole) _, err = community.AddMember(&m.identity.PublicKey, ownerRole, community.Clock())
if err != nil { if err != nil {
return false, err return false, err
} }

View File

@ -2283,7 +2283,7 @@ func (s *MessengerCommunitiesSuite) TestShareCommunityWithPreviousMember() {
communityChat := response.Chats()[0] communityChat := response.Chats()[0]
// Add Alice to the community before sharing it // Add Alice to the community before sharing it
_, err = community.AddMember(&s.alice.identity.PublicKey, []protobuf.CommunityMember_Roles{}) _, err = community.AddMember(&s.alice.identity.PublicKey, []protobuf.CommunityMember_Roles{}, community.Clock())
s.Require().NoError(err) s.Require().NoError(err)
err = s.bob.communitiesManager.SaveCommunity(community) err = s.bob.communitiesManager.SaveCommunity(community)
@ -4778,14 +4778,14 @@ func (s *MessengerCommunitiesSuite) TestIgnoreOutdatedCommunityDescription() {
signer, description1, err := communities.UnwrapCommunityDescriptionMessage(wrappedDescription1) signer, description1, err := communities.UnwrapCommunityDescriptionMessage(wrappedDescription1)
s.Require().NoError(err) s.Require().NoError(err)
_, err = community.AddMember(&s.alice.identity.PublicKey, []protobuf.CommunityMember_Roles{}) _, err = community.AddMember(&s.alice.identity.PublicKey, []protobuf.CommunityMember_Roles{}, community.Clock())
s.Require().NoError(err) s.Require().NoError(err)
wrappedDescription2, err := community.ToProtocolMessageBytes() wrappedDescription2, err := community.ToProtocolMessageBytes()
s.Require().NoError(err) s.Require().NoError(err)
_, description2, err := communities.UnwrapCommunityDescriptionMessage(wrappedDescription2) _, description2, err := communities.UnwrapCommunityDescriptionMessage(wrappedDescription2)
s.Require().NoError(err) s.Require().NoError(err)
_, err = community.AddMember(&s.bob.identity.PublicKey, []protobuf.CommunityMember_Roles{}) _, err = community.AddMember(&s.bob.identity.PublicKey, []protobuf.CommunityMember_Roles{}, community.Clock())
s.Require().NoError(err) s.Require().NoError(err)
wrappedDescription3, err := community.ToProtocolMessageBytes() wrappedDescription3, err := community.ToProtocolMessageBytes()
s.Require().NoError(err) s.Require().NoError(err)

View File

@ -2186,7 +2186,7 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberPermissi
s.Require().NoError(err) s.Require().NoError(err)
err = s.owner.communitiesManager.SaveRequestToJoinRevealedAddresses(requestId, requestToJoin.RevealedAccounts) err = s.owner.communitiesManager.SaveRequestToJoinRevealedAddresses(requestId, requestToJoin.RevealedAccounts)
s.Require().NoError(err) s.Require().NoError(err)
_, err = community.AddMember(&privateKey.PublicKey, communityRole) _, err = community.AddMember(&privateKey.PublicKey, communityRole, requestToJoin.Clock)
s.Require().NoError(err) s.Require().NoError(err)
_, err = community.AddMemberToChat(chat.CommunityChatID(), &privateKey.PublicKey, communityRole, protobuf.CommunityMember_CHANNEL_ROLE_POSTER) _, err = community.AddMemberToChat(chat.CommunityChatID(), &privateKey.PublicKey, communityRole, protobuf.CommunityMember_CHANNEL_ROLE_POSTER)
s.Require().NoError(err) s.Require().NoError(err)

View File

@ -303,7 +303,7 @@ func (s *MessengerSuite) TestMarkMessageWithNotificationAsUnreadInCommunityChatS
community := response.Communities()[0] community := response.Communities()[0]
communityChat := response.Chats()[0] communityChat := response.Chats()[0]
_, err = community.AddMember(&s.m.identity.PublicKey, []protobuf.CommunityMember_Roles{}) _, err = community.AddMember(&s.m.identity.PublicKey, []protobuf.CommunityMember_Roles{}, community.Clock())
s.Require().NoError(err) s.Require().NoError(err)
err = other.communitiesManager.SaveCommunity(community) err = other.communitiesManager.SaveCommunity(community)