diff --git a/channels/adminListener.go b/channels/adminListener.go index c64819bf4e0eb597fe63649e90bb723081573353..2d2a72ca0425f0e9893fb786649dcee599428ea4 100644 --- a/channels/adminListener.go +++ b/channels/adminListener.go @@ -10,8 +10,8 @@ import ( "gitlab.com/xx_network/primitives/id" ) -// the adminListener adheres to the broadcast listener interface and is used -// when admin messages are received on the channel +// adminListener adheres to the broadcast listener interface and is used when +// admin messages are received on the channel. type adminListener struct { chID *id.ID trigger triggerAdminEventFunc @@ -20,28 +20,28 @@ type adminListener struct { func (al *adminListener) Listen(payload []byte, receptionID receptionID.EphemeralIdentity, round rounds.Round) { - //Remove the padding + // Remove the padding payloadUnpadded, err := broadcast.DecodeSizedBroadcast(payload) if err != nil { - jww.WARN.Printf("Failed to strip the padding on User Message "+ - "on channel %s", al.chID) + jww.WARN.Printf( + "Failed to strip the padding on User Message on channel %s", al.chID) return } - //get the message ID + // Get the message ID msgID := channel.MakeMessageID(payloadUnpadded) - //Decode the message as a channel message + // Decode the message as a channel message cm := &ChannelMessage{} if err = proto.Unmarshal(payloadUnpadded, cm); err != nil { - jww.WARN.Printf("Failed to unmarshal Channel Message from Admin"+ - " on channel %s", al.chID) + jww.WARN.Printf("Failed to unmarshal Channel Message from Admin on "+ + "channel %s", al.chID) return } - /*CRYPTOGRAPHICALLY RELEVANT CHECKS*/ + /* CRYPTOGRAPHICALLY RELEVANT CHECKS */ - // check the round to ensure the message is not a replay + // Check the round to ensure that the message is not a replay if id.Round(cm.RoundID) != round.ID { jww.WARN.Printf("The round message %s send on %s referenced "+ "(%d) was not the same as the round the message was found on (%d)", @@ -49,7 +49,7 @@ func (al *adminListener) Listen(payload []byte, return } - //Submit the message to the event model for listening + // Submit the message to the event model for listening al.trigger(al.chID, cm, msgID, receptionID, round) return diff --git a/channels/adminListener_test.go b/channels/adminListener_test.go index d2746007fc08586b710eb1ce5a989b466e8f50ae..928acf11e9c1b5d62279409e473a4dc7ee7e5808 100644 --- a/channels/adminListener_test.go +++ b/channels/adminListener_test.go @@ -25,9 +25,9 @@ type triggerAdminEventDummy struct { round rounds.Round } -func (taed *triggerAdminEventDummy) triggerAdminEvent(chID *id.ID, cm *ChannelMessage, - messageID cryptoChannel.MessageID, receptionID receptionID.EphemeralIdentity, - round rounds.Round) { +func (taed *triggerAdminEventDummy) triggerAdminEvent(chID *id.ID, + cm *ChannelMessage, messageID cryptoChannel.MessageID, + receptionID receptionID.EphemeralIdentity, round rounds.Round) { taed.gotData = true taed.chID = chID @@ -37,10 +37,10 @@ func (taed *triggerAdminEventDummy) triggerAdminEvent(chID *id.ID, cm *ChannelMe taed.round = round } -// Tests the happy path +// Tests the happy path. func TestAdminListener_Listen(t *testing.T) { - //build inputs + // Build inputs chID := &id.ID{} chID[0] = 1 @@ -67,7 +67,7 @@ func TestAdminListener_Listen(t *testing.T) { msgID := cryptoChannel.MakeMessageID(cmSerial) - //build the listener + // Build the listener dummy := &triggerAdminEventDummy{} al := adminListener{ @@ -75,10 +75,10 @@ func TestAdminListener_Listen(t *testing.T) { trigger: dummy.triggerAdminEvent, } - //call the listener + // Call the listener al.Listen(chMsgSerialSized, receptionID.EphemeralIdentity{}, r) - //check the results + // Check the results if !dummy.gotData { t.Fatalf("No data returned after valid listen") } @@ -103,11 +103,11 @@ func TestAdminListener_Listen(t *testing.T) { } } -// Tests that the message is rejected when the round it came on doesnt -// match the round in the channel message +// Tests that the message is rejected when the round it came on doesn't match +// the round in the channel message. func TestAdminListener_Listen_BadRound(t *testing.T) { - //build inputs + // build inputs chID := &id.ID{} chID[0] = 1 @@ -116,7 +116,7 @@ func TestAdminListener_Listen_BadRound(t *testing.T) { cm := &ChannelMessage{ Lease: int64(time.Hour), - // different from the round above + // Different from the round above RoundID: 69, PayloadType: 42, Payload: []byte("blarg"), @@ -133,7 +133,7 @@ func TestAdminListener_Listen_BadRound(t *testing.T) { t.Fatalf("Failed to size channel message: %+v", err) } - //build the listener + // Build the listener dummy := &triggerAdminEventDummy{} al := adminListener{ @@ -141,10 +141,10 @@ func TestAdminListener_Listen_BadRound(t *testing.T) { trigger: dummy.triggerAdminEvent, } - //call the listener + // Call the listener al.Listen(chMsgSerialSized, receptionID.EphemeralIdentity{}, r) - //check the results + // check the results if dummy.gotData { t.Fatalf("payload handled when it should have failed due to " + "a round issue") @@ -152,10 +152,10 @@ func TestAdminListener_Listen_BadRound(t *testing.T) { } -// Tests that the message is rejected when the channel message is malformed +// Tests that the message is rejected when the channel message is malformed. func TestAdminListener_Listen_BadChannelMessage(t *testing.T) { - //build inputs + // Build inputs chID := &id.ID{} chID[0] = 1 @@ -170,7 +170,7 @@ func TestAdminListener_Listen_BadChannelMessage(t *testing.T) { t.Fatalf("Failed to size channel message: %+v", err) } - //build the listener + // Build the listener dummy := &triggerAdminEventDummy{} al := adminListener{ @@ -178,10 +178,10 @@ func TestAdminListener_Listen_BadChannelMessage(t *testing.T) { trigger: dummy.triggerAdminEvent, } - //call the listener + // Call the listener al.Listen(chMsgSerialSized, receptionID.EphemeralIdentity{}, r) - //check the results + // Check the results if dummy.gotData { t.Fatalf("payload handled when it should have failed due to " + "a malformed channel message") @@ -190,10 +190,10 @@ func TestAdminListener_Listen_BadChannelMessage(t *testing.T) { } // Tests that the message is rejected when the sized broadcast message is -//malformed +// malformed. func TestAdminListener_Listen_BadSizedBroadcast(t *testing.T) { - //build inputs + // build inputs chID := &id.ID{} chID[0] = 1 @@ -202,7 +202,7 @@ func TestAdminListener_Listen_BadSizedBroadcast(t *testing.T) { cm := &ChannelMessage{ Lease: int64(time.Hour), - // different from the round above + // Different from the round above RoundID: 69, PayloadType: 42, Payload: []byte("blarg"), @@ -219,10 +219,10 @@ func TestAdminListener_Listen_BadSizedBroadcast(t *testing.T) { t.Fatalf("Failed to size channel message: %+v", err) } - //remove half the sized broadcast to make it malformed + // Remove half the sized broadcast to make it malformed chMsgSerialSized = chMsgSerialSized[:broadcast.GetSizedBroadcastSize(chMsgSerialSized)/2] - //build the listener + // Build the listener dummy := &triggerAdminEventDummy{} al := adminListener{ @@ -230,10 +230,10 @@ func TestAdminListener_Listen_BadSizedBroadcast(t *testing.T) { trigger: dummy.triggerAdminEvent, } - //call the listener + // Call the listener al.Listen(chMsgSerialSized, receptionID.EphemeralIdentity{}, r) - //check the results + // Check the results if dummy.gotData { t.Fatalf("payload handled when it should have failed due to " + "a malformed sized broadcast") diff --git a/channels/channelMessages.proto b/channels/channelMessages.proto index 171f359e971094e8ba63cc1685fa184c4b2498ea..eb39899459c3fa06972682cea9ae480fe2207ba2 100644 --- a/channels/channelMessages.proto +++ b/channels/channelMessages.proto @@ -10,51 +10,54 @@ syntax = "proto3"; package parse; option go_package = "/channels"; -// ChannelMessage is transmitted by the channel. Effectively it is -// a command for the channel sent by a user with admin access of the channel. +// ChannelMessage is transmitted by the channel. Effectively it is a command for +// the channel sent by a user with admin access of the channel. message ChannelMessage{ // Lease is the length that this channel message will take effect. int64 Lease = 1; - // The round this message was sent on + // The round this message was sent on. uint64 RoundID = 2; // The type the below payload is. This may be some form of channel command, // such as BAN<username1>. uint32 PayloadType = 3; - // Payload is the actual message payload. It will be processed differently based - // on the PayloadType + // Payload is the actual message payload. It will be processed differently + // based on the PayloadType. bytes Payload = 4; } // UserMessage is a message sent by a user who is a member within the channel. message UserMessage { - // Message contains the contents of the message. This is typically what - // the end-user has submitted to the channel. This is a serialization of the + // Message contains the contents of the message. This is typically what the + // end-user has submitted to the channel. This is a serialization of the // ChannelMessage. bytes Message = 1; - // ValidationSignature is the signature validating this user owns - // their username and may send messages to the channel under this username. - // This signature is provided by UD and may be validated by all members of - // the channel. - // ValidationSignature = Sig(UD_ECCPrivKey,Username|ECCPublicKey|UsernameLease) + // ValidationSignature is the signature validating this user owns their + // username and may send messages to the channel under this username. This + // signature is provided by UD and may be validated by all members of the + // channel. + // + // ValidationSignature = Sig(UD_ECCPrivKey, Username | ECCPublicKey | UsernameLease) bytes ValidationSignature = 2; - // Signature is the signature proving this message has been - // sent by the owner of this user's public key. - // Signature = Sig(User_ECCPublicKey,Message) + // Signature is the signature proving this message has been sent by the + // owner of this user's public key. + // + // Signature = Sig(User_ECCPublicKey, Message) bytes Signature = 3; // Username is the username the user has registered with the channel and // with UD. string Username = 4; - // ECCPublicKey is the user's EC Public key. This is provided by the network. + // ECCPublicKey is the user's EC Public key. This is provided by the + // network. bytes ECCPublicKey = 5; - // UsernameLease is the lease that has been provided to the username. - // This value is provide by UD. + // UsernameLease is the lease that has been provided to the username. This + // value is provide by UD. int64 UsernameLease = 6; } diff --git a/channels/emoji.go b/channels/emoji.go index ee62782550f16b0708ad7de94068e1db273fadc3..c640264563cec55f6d42d2ab224facbd3a035270 100644 --- a/channels/emoji.go +++ b/channels/emoji.go @@ -7,10 +7,10 @@ import ( const findEmoji = "(\\\\u00a9|\\\\u00ae|[\\\\u2000-\\\\u3300]|\\\\ud83c[\\\\ud000-\\\\udfff]|\\\\ud83d[\\\\ud000-\\\\udfff]|\\\\ud83e[\\\\ud000-\\\\udfff])" -var InvalidReaction = errors.New("The reaction is not valid, " + - "it must be a single emoji") +var InvalidReaction = errors.New( + "The reaction is not valid, it must be a single emoji") -// ValidateReaction checks that the reaction only contains a single Emoji +// ValidateReaction checks that the reaction only contains a single emoji. func ValidateReaction(reaction string) error { reactRunes := []rune(reaction) if len(reactRunes) > 1 { diff --git a/channels/manager.go b/channels/manager.go index 51fb0bbc37af670133b4e791ba727d066117ceb6..2d4b958de5ea3fa28a17ff25d63df38ffab6424d 100644 --- a/channels/manager.go +++ b/channels/manager.go @@ -10,31 +10,30 @@ import ( ) type manager struct { - //List of all channels + // List of all channels channels map[*id.ID]*joinedChannel mux sync.RWMutex - //External references + // External references kv *versioned.KV client broadcast.Client rng *fastRNG.StreamGenerator name NameService - //Events model + // Events model *events - // make the function used to create broadcasts be a pointer so it - // can be replaced in tests + // Makes the function that is used to create broadcasts be a pointer so that + // it can be replaced in tests broadcastMaker broadcast.NewBroadcastChannelFunc } -// NewManager Creates a new channel.Manager. -// Prefix's teh KV with the username so multiple instances for multiple -// users will not error. +// NewManager creates a new channel.Manager. It prefixes the KV with the +// username so that multiple instances for multiple users will not error. func NewManager(kv *versioned.KV, client broadcast.Client, rng *fastRNG.StreamGenerator, name NameService, model EventModel) Manager { - //prefix the kv with the username so multiple can be run + // Prefix the kv with the username so multiple can be run kv = kv.Prefix(name.GetUsername()) m := manager{ @@ -52,24 +51,28 @@ func NewManager(kv *versioned.KV, client broadcast.Client, return &m } -// JoinChannel joins the given channel. Will fail if the channel -// has already been joined. +// JoinChannel joins the given channel. It will fail if the channel has already +// been joined. func (m *manager) JoinChannel(channel cryptoBroadcast.Channel) error { err := m.addChannel(channel) if err != nil { return err } + go m.events.model.JoinChannel(channel) + return nil } -// LeaveChannel leaves the given channel. It will return an error -// if the channel was not previously joined. -func (m *manager) LeaveChannel(channelId *id.ID) error { - err := m.removeChannel(channelId) +// LeaveChannel leaves the given channel. It will return an error if the channel +// was not previously joined. +func (m *manager) LeaveChannel(channelID *id.ID) error { + err := m.removeChannel(channelID) if err != nil { return err } - go m.events.model.LeaveChannel(channelId) + + go m.events.model.LeaveChannel(channelID) + return nil } diff --git a/channels/manager_test.go b/channels/manager_test.go index 39911b2fccef6679c692e7e4bff5cd182728d7b9..25a1e11ee7c74cd16b06b8bd8fd82112bd120eee 100644 --- a/channels/manager_test.go +++ b/channels/manager_test.go @@ -8,7 +8,7 @@ import ( ) func TestMain(m *testing.M) { - // many tests trigger warn prints, set the out threshold so the warns + // Many tests trigger WARN prints;, set the out threshold so the WARN prints // can be seen in the logs jww.SetStdoutThreshold(jww.LevelWarn) os.Exit(m.Run())