Skip to content

Commit

Permalink
fix_: mitigate permission stuck in pending state
Browse files Browse the repository at this point in the history
This PR mitigates permission stuck in pending state upon making device a
control node. It fixes
[#14023](status-im/status-desktop#14023)
  • Loading branch information
kounkou committed Apr 25, 2024
1 parent 9e5462e commit 6fe326b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 13 deletions.
26 changes: 14 additions & 12 deletions protocol/communities/community_events_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ import (

var ErrInvalidCommunityEventClock = errors.New("clock for admin event message is outdated")

func (o *Community) processEvents(message *CommunityEventsMessage, lastlyAppliedEvents map[string]uint64) error {
func (o *Community) processEvents(events []CommunityEvent, eventsBaseCommunityDescription []byte, lastlyAppliedEvents map[string]uint64) error {
processor := &eventsProcessor{
community: o,
message: message,
logger: o.config.Logger.Named("eventsProcessor"),
lastlyAppliedEvents: lastlyAppliedEvents,
community: o,
events: events,
logger: o.config.Logger.Named("eventsProcessor"),
lastlyAppliedEvents: lastlyAppliedEvents,
eventsBaseCommunityDescription: eventsBaseCommunityDescription,
}
return processor.exec()
}

type eventsProcessor struct {
community *Community
message *CommunityEventsMessage
logger *zap.Logger
lastlyAppliedEvents map[string]uint64
community *Community
events []CommunityEvent
logger *zap.Logger
lastlyAppliedEvents map[string]uint64
eventsBaseCommunityDescription []byte

eventsToApply []CommunityEvent
}
Expand All @@ -53,7 +55,7 @@ func (e *eventsProcessor) exec() error {
}

func (e *eventsProcessor) validateDescription() error {
description, err := validateAndGetEventsMessageCommunityDescription(e.message.EventsBaseCommunityDescription, e.community.ControlNode())
description, err := validateAndGetEventsMessageCommunityDescription(e.eventsBaseCommunityDescription, e.community.ControlNode())
if err != nil {
return err
}
Expand Down Expand Up @@ -88,7 +90,7 @@ func (e *eventsProcessor) validateEvent(event *CommunityEvent) error {

// Filter invalid and outdated events.
func (e *eventsProcessor) filterEvents() {
for _, ev := range e.message.Events {
for _, ev := range e.events {
event := ev
if err := e.validateEvent(&event); err == nil {
e.eventsToApply = append(e.eventsToApply, event)
Expand Down Expand Up @@ -145,7 +147,7 @@ func (e *eventsProcessor) sortEvents() {
func (e *eventsProcessor) applyEvents() {
if e.community.config.EventsData == nil {
e.community.config.EventsData = &EventsData{
EventsBaseCommunityDescription: e.message.EventsBaseCommunityDescription,
EventsBaseCommunityDescription: e.eventsBaseCommunityDescription,
}
}
e.community.config.EventsData.Events = e.eventsToApply
Expand Down
60 changes: 59 additions & 1 deletion protocol/communities/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2035,7 +2035,8 @@ func (m *Manager) HandleCommunityEventsMessage(signer *ecdsa.PublicKey, message
return nil, err
}
}
err = community.processEvents(eventsMessage, lastlyAppliedEvents)

err = community.processEvents(eventsMessage.Events, eventsMessage.EventsBaseCommunityDescription, lastlyAppliedEvents)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -5188,11 +5189,68 @@ func (m *Manager) promoteSelfToControlNode(community *Community, clock uint64) (
return false, err
}

if community.IsControlNode() {
err = m.handleCommunityEvents(community)
if err != nil {
return false, err
}
}

community.increaseClock()

return ownerChanged, nil
}

func (m *Manager) handleCommunityEvents(community *Community) error {
if community.config.EventsData == nil {
return nil
}

lastlyAppliedEvents, err := m.persistence.GetAppliedCommunityEvents(community.ID())
if err != nil {
return err
}

err = community.processEvents(community.config.EventsData.Events, community.config.EventsData.EventsBaseCommunityDescription,
lastlyAppliedEvents)
if err != nil {
return err
}

_, err = m.handleAdditionalAdminChanges(community)
if err != nil {
return err
}

if err = m.handleCommunityTokensMetadata(community); err != nil {
return err
}

appliedEvents := map[string]uint64{}
if community.config.EventsData != nil {
for _, event := range community.config.EventsData.Events {
appliedEvents[event.EventTypeID()] = event.CommunityEventClock
}
}

community.config.EventsData = nil // clear events, they are already applied
community.increaseClock()

err = m.persistence.SaveCommunity(community)
if err != nil {
return err
}

err = m.persistence.UpsertAppliedCommunityEvents(community.ID(), appliedEvents)
if err != nil {
return err
}

m.publish(&Subscription{Community: community})

return nil
}

func (m *Manager) shareRequestsToJoinWithNewPrivilegedMembers(community *Community, newPrivilegedMembers map[protobuf.CommunityMember_Roles][]*ecdsa.PublicKey) error {
requestsToJoin, err := m.GetCommunityRequestsToJoinWithRevealedAddresses(community.ID())
if err != nil {
Expand Down

0 comments on commit 6fe326b

Please sign in to comment.