summaryrefslogtreecommitdiff
path: root/network/msgOfInterest.go
blob: 727405c8a8db60cbf7af9011e9be9e35b33787e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package network

import (
	"errors"
	"strings"

	"github.com/algorand/go-algorand/protocol"
)

var errUnableUnmarshallMessage = errors.New("unmarshalMessageOfInterest: could not unmarshall message")
var errInvalidMessageOfInterest = errors.New("unmarshalMessageOfInterest: message missing the tags key")
var errInvalidMessageOfInterestLength = errors.New("unmarshalMessageOfInterest: message length is too long")
var errInvalidMessageOfInterestInvalidTag = errors.New("unmarshalMessageOfInterest: invalid tag")

const maxMessageOfInterestTags = 1024
const topicsEncodingSeparator = ","

func unmarshallMessageOfInterest(data []byte) (map[protocol.Tag]bool, error) {
	// decode the message, and ensure it's a valid message.
	topics, err := UnmarshallTopics(data)
	if err != nil {
		return nil, errUnableUnmarshallMessage
	}
	tags, found := topics.GetValue("tags")
	if !found {
		return nil, errInvalidMessageOfInterest
	}
	if len(tags) > maxMessageOfInterestTags {
		return nil, errInvalidMessageOfInterestLength
	}
	// convert the tags into a tags map.
	msgTagsMap := make(map[protocol.Tag]bool, len(tags))
	for _, tag := range strings.Split(string(tags), topicsEncodingSeparator) {
		if len(tag) != protocol.TagLength {
			return nil, errInvalidMessageOfInterestInvalidTag
		}
		if _, ok := protocol.TagMap[protocol.Tag(tag)]; !ok {
			return nil, errInvalidMessageOfInterestInvalidTag
		}
		msgTagsMap[protocol.Tag(tag)] = true
	}
	return msgTagsMap, nil
}

// MarshallMessageOfInterest generate a message of interest message body for a given set of message tags.
func MarshallMessageOfInterest(messageTags []protocol.Tag) []byte {
	// create a long string with all these messages.
	tags := ""
	for _, tag := range messageTags {
		tags += topicsEncodingSeparator + string(tag)
	}
	if len(tags) > 0 {
		tags = tags[len(topicsEncodingSeparator):]
	}
	topics := Topics{Topic{key: "tags", data: []byte(tags)}}
	return topics.MarshallTopics()
}

// MarshallMessageOfInterestMap generates a message of interest message body
// for the message tags that map to "true" in the map argument.
func MarshallMessageOfInterestMap(tagmap map[protocol.Tag]bool) []byte {
	tags := ""
	for tag, flag := range tagmap {
		if flag {
			tags += topicsEncodingSeparator + string(tag)
		}
	}
	if len(tags) > 0 {
		tags = tags[len(topicsEncodingSeparator):]
	}
	topics := Topics{Topic{key: "tags", data: []byte(tags)}}
	return topics.MarshallTopics()
}

// MessageOfInterestMaxSize returns the maximum size of a MI message sent over the network
// by encoding all of the tags currenttly in use.
func MessageOfInterestMaxSize() int {
	allTags := make(map[protocol.Tag]bool, len(protocol.TagList))
	for _, tag := range protocol.TagList {
		allTags[tag] = true
	}
	return len(MarshallMessageOfInterest(protocol.TagList))
}