|
1 | 1 | package pubsub |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "crypto/sha256" |
| 6 | + "fmt" |
| 7 | + pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb" |
| 8 | + "github.com/libp2p/go-libp2p/core/peer" |
5 | 9 | "sort" |
6 | 10 | "strings" |
7 | 11 | ) |
8 | 12 |
|
| 13 | +type TopicBundleHash [4]byte |
| 14 | + |
| 15 | +func newTopicBundleHash(bytes []byte) (*TopicBundleHash, error) { |
| 16 | + if len(bytes) != 4 { |
| 17 | + return nil, fmt.Errorf("expected 4 bytes for TopicBundleHash found: %d", len(bytes)) |
| 18 | + } |
| 19 | + var hash TopicBundleHash |
| 20 | + copy(hash[:], bytes) |
| 21 | + |
| 22 | + return &hash, nil |
| 23 | +} |
| 24 | + |
9 | 25 | type topicTableExtension struct { |
| 26 | + bundleHashes []TopicBundleHash |
| 27 | + intersectedHashes map[peer.ID][]TopicBundleHash |
| 28 | + |
| 29 | + indexToName map[TopicBundleHash][]string // bundle hash -> list of topics |
| 30 | + nameToIndex map[TopicBundleHash]map[string]int // bundle hash -> topic -> 0-based index in bundle |
10 | 31 | } |
11 | 32 |
|
12 | | -type TopicBundleHash [4]byte |
| 33 | +func newTopicTableExtension(myBundles [][]string) (*topicTableExtension, error) { |
| 34 | + bundleHashes := make([]TopicBundleHash, 0, len(myBundles)) |
13 | 35 |
|
14 | | -func computeTopicBundleHash(topics []string) TopicBundleHash { |
15 | | - sortedTopics := make([]string, len(topics)) |
16 | | - copy(sortedTopics, topics) |
| 36 | + indexToName := make(map[TopicBundleHash][]string) |
| 37 | + nameToIndex := make(map[TopicBundleHash]map[string]int) |
17 | 38 |
|
18 | | - sort.Strings(sortedTopics) |
| 39 | + for _, topics := range myBundles { |
| 40 | + sort.Strings(topics) |
19 | 41 |
|
20 | | - concatenated := strings.Join(sortedTopics, "") |
| 42 | + hash := computeTopicBundleHash(topics) |
| 43 | + bundleHashes = append(bundleHashes, hash) |
| 44 | + |
| 45 | + indexToName[hash] = topics |
| 46 | + nameToIndex[hash] = make(map[string]int) |
| 47 | + for idx, topic := range topics { |
| 48 | + nameToIndex[hash][topic] = idx |
| 49 | + } |
| 50 | + } |
| 51 | + if err := validateBundles(bundleHashes); err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + e := &topicTableExtension{ |
| 55 | + bundleHashes: bundleHashes, |
| 56 | + intersectedHashes: make(map[peer.ID][]TopicBundleHash), |
| 57 | + indexToName: indexToName, |
| 58 | + nameToIndex: nameToIndex, |
| 59 | + } |
| 60 | + return e, nil |
| 61 | +} |
| 62 | + |
| 63 | +func (e *topicTableExtension) GetControlExtension() *pubsub_pb.ExtTopicTable { |
| 64 | + hashSlices := make([][]byte, 0, len(e.bundleHashes)) |
| 65 | + |
| 66 | + for _, hash := range e.bundleHashes { |
| 67 | + hashSlices = append(hashSlices, hash[:]) |
| 68 | + } |
| 69 | + return &pubsub_pb.ExtTopicTable{ |
| 70 | + TopicBundleHashes: hashSlices, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func (e *topicTableExtension) AddPeer(id peer.ID, bundles []TopicBundleHash) error { |
| 75 | + if err := validateBundles(bundles); err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + e.intersectedHashes[id] = computeBundleIntersection(e.bundleHashes, bundles) |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +// Note that topicIndex is 1-based |
| 83 | +func (e *topicTableExtension) GetTopicName(id peer.ID, topicIndex int) (string, error) { |
| 84 | + if topicIndex < 1 { |
| 85 | + return "", fmt.Errorf("Invalid topic index: %d", topicIndex) |
| 86 | + } |
| 87 | + |
| 88 | + // Turn the index to 0-based |
| 89 | + idx := topicIndex - 1 |
21 | 90 |
|
| 91 | + for _, hash := range e.intersectedHashes[id] { |
| 92 | + if idx < len(e.indexToName[hash]) { |
| 93 | + return e.indexToName[hash][idx], nil |
| 94 | + } else { |
| 95 | + idx -= len(e.indexToName[hash]) |
| 96 | + } |
| 97 | + } |
| 98 | + return "", fmt.Errorf("Invalid topic index: %d", topicIndex) |
| 99 | +} |
| 100 | + |
| 101 | +// It returns a 1-based index |
| 102 | +func (e *topicTableExtension) GetTopicIndex(id peer.ID, topicName string) (int, error) { |
| 103 | + topicIndex := 0 |
| 104 | + |
| 105 | + for _, hash := range e.intersectedHashes[id] { |
| 106 | + if idx, ok := e.nameToIndex[hash][topicName]; ok { |
| 107 | + topicIndex += idx |
| 108 | + // Turn the index to 1-based |
| 109 | + topicIndex += 1 |
| 110 | + return topicIndex, nil |
| 111 | + } else { |
| 112 | + topicIndex += len(e.nameToIndex[hash]) |
| 113 | + } |
| 114 | + } |
| 115 | + return 0, fmt.Errorf("The topic not found: %s", topicName) |
| 116 | +} |
| 117 | + |
| 118 | +func validateBundles(bundles []TopicBundleHash) error { |
| 119 | + seen := make(map[TopicBundleHash]struct{}, len(bundles)) |
| 120 | + for _, bundle := range bundles { |
| 121 | + if _, ok := seen[bundle]; ok { |
| 122 | + return fmt.Errorf("duplicates found") |
| 123 | + } |
| 124 | + seen[bundle] = struct{}{} |
| 125 | + } |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +// Assume that the topics have been sorted |
| 130 | +func computeTopicBundleHash(sortedTopics []string) TopicBundleHash { |
| 131 | + concatenated := strings.Join(sortedTopics, "") |
22 | 132 | hash := sha256.Sum256([]byte(concatenated)) |
23 | 133 |
|
24 | 134 | var result TopicBundleHash |
25 | 135 | copy(result[:], hash[len(hash)-4:]) |
26 | 136 | return result |
27 | 137 | } |
| 138 | + |
| 139 | +func computeBundleIntersection(first, second []TopicBundleHash) []TopicBundleHash { |
| 140 | + var result []TopicBundleHash |
| 141 | + |
| 142 | + // Find common prefix where elements at each index are equal in both slices. |
| 143 | + for i := 0; i < min(len(first), len(second)) && bytes.Equal(first[i][:], second[i][:]); i++ { |
| 144 | + result = append(result, first[i]) |
| 145 | + } |
| 146 | + |
| 147 | + // Store the length of the matching prefix. This is our marker. |
| 148 | + prefixLen := len(result) |
| 149 | + |
| 150 | + // Build a set of the remaining elements in the first slice after the prefix. |
| 151 | + // For each remaining element in the second slice, if it exists in the set, |
| 152 | + // add it to the result. (Duplicates possible if not validated up front.) |
| 153 | + seen := make(map[TopicBundleHash]struct{}) |
| 154 | + for _, v := range first[prefixLen:] { |
| 155 | + seen[v] = struct{}{} |
| 156 | + } |
| 157 | + for _, v := range second[prefixLen:] { |
| 158 | + if _, ok := seen[v]; ok { |
| 159 | + result = append(result, v) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + // Sort the unordered tail lexicographically. |
| 164 | + unordered := result[prefixLen:] |
| 165 | + sort.Slice(unordered, func(i, j int) bool { |
| 166 | + return bytes.Compare(unordered[i][:], unordered[j][:]) < 0 |
| 167 | + }) |
| 168 | + |
| 169 | + return result |
| 170 | +} |
0 commit comments