status-go/protocol/communities/community_event_message.go
Patryk Osmaczko e2cab1a8ae fix: ensure community events eventual consistency
- Extracted `community_events_factory.go`
- Introduced `eventsProcessor`
  - Improved processing logic order
  - Improved events filtering
- Introduced concept of `EventTypeID` to prevent redundant events handling
- Added sanity check before events appliance when reading community from
  database
- Removed reject&re-apply scheme (no more ping-pong issue)
- Fixed and added more variants to eventual consistency test

fixes: status-im/status-desktop#13387
fixes: status-im/status-desktop#13388
2024-02-20 21:07:01 +01:00

55 lines
1.6 KiB
Go

package communities
import (
"github.com/golang/protobuf/proto"
"github.com/status-im/status-go/protocol/protobuf"
)
type CommunityEventsMessage struct {
CommunityID []byte `json:"communityId"`
EventsBaseCommunityDescription []byte `json:"eventsBaseCommunityDescription"`
Events []CommunityEvent `json:"events,omitempty"`
}
func (m *CommunityEventsMessage) ToProtobuf() *protobuf.CommunityEventsMessage {
result := protobuf.CommunityEventsMessage{
CommunityId: m.CommunityID,
EventsBaseCommunityDescription: m.EventsBaseCommunityDescription,
SignedEvents: []*protobuf.SignedCommunityEvent{},
}
for _, event := range m.Events {
signedEvent := &protobuf.SignedCommunityEvent{
Signature: event.Signature,
Payload: event.Payload,
}
result.SignedEvents = append(result.SignedEvents, signedEvent)
}
return &result
}
func CommunityEventsMessageFromProtobuf(msg *protobuf.CommunityEventsMessage) (*CommunityEventsMessage, error) {
result := &CommunityEventsMessage{
CommunityID: msg.CommunityId,
EventsBaseCommunityDescription: msg.EventsBaseCommunityDescription,
Events: []CommunityEvent{},
}
for _, signedEvent := range msg.SignedEvents {
event, err := communityEventFromProtobuf(signedEvent)
if err != nil {
return nil, err
}
result.Events = append(result.Events, *event)
}
return result, nil
}
func (m *CommunityEventsMessage) Marshal() ([]byte, error) {
pb := m.ToProtobuf()
return proto.Marshal(pb)
}