-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregator_addr_msg.go
164 lines (146 loc) · 3.92 KB
/
aggregator_addr_msg.go
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"encoding/json"
"sort"
uuid "github.com/google/uuid"
)
type ourString string
func (s ourString) bytes() []byte {
return []byte(s)
}
// empty represents an empty map value.
type empty struct{}
// AddressSet represents a set of string-encoded IP addresses.
type AddressSet map[string]empty
// AddrsByWallet maps a wallet ID to a set of its anonymized IP addresses, all
// represented as strings.
type AddrsByWallet map[uuid.UUID]AddressSet
// WalletsByKeyID maps a key ID to a map of type addrsByWallet. Key IDs
// represent data collection epochs: whenever the key ID rotates, a new epoch
// begins, and our collection of wallet-to-address records begins afresh.
type WalletsByKeyID map[keyID]AddrsByWallet
// sorted returns the address set's addresses as a sorted string slice.
func (s AddressSet) sorted() []string {
addrs := []string{}
for a := range s {
addrs = append(addrs, a)
}
sort.Strings(addrs)
return addrs
}
// numWallets returns the total number of wallets that are currently in the
// struct. Note that this may contain duplicate wallets, i.e., wallets that
// are present for key ID x *and* for key ID y.
func (w WalletsByKeyID) numWallets() int {
if len(w) == 0 {
return 0
}
total := 0
for _, addrsByWallet := range w {
total += len(addrsByWallet)
}
return total
}
// numAddrs returns the total number of addresses (which may be different from
// the number of unique addresses!) that are currently in the struct.
func (w WalletsByKeyID) numAddrs() int {
if len(w) == 0 {
return 0
}
total := 0
for _, addrsByWallet := range w {
if len(addrsByWallet) == 0 {
continue
}
for _, addrs := range addrsByWallet {
total += len(addrs)
}
}
return total
}
// MarshalJSON marshals the given key ID-to-wallets map and turns it into the
// following JSON:
//
// {
// "keyid": {
// "024752c9-7090-4123-939e-67b08042d7d7": {
// "addrs": {
// "68a7deb0-615c-4f26-bf87-6b122732d8e9": [
// "1.1.1.1",
// "2.2.2.2",
// ...
// ],
// ...
// }
// }
// }
// }
func (w WalletsByKeyID) MarshalJSON() ([]byte, error) {
type toMarshal struct {
WalletsByKeyID map[keyID]AddrsByWallet `json:"keyid"`
}
m := &toMarshal{WalletsByKeyID: make(WalletsByKeyID)}
for keyID, wallets := range w {
m.WalletsByKeyID[keyID] = wallets
}
return json.Marshal(m)
}
// UnmarshalJSON unmarshalls the given JSON and turns it back into the given
// WalletsByKeyID.
func (w WalletsByKeyID) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &struct {
WalletsByKeyID map[keyID]AddrsByWallet `json:"keyid"`
}{w})
}
// MarshalJSON marshals the given addresses and turns it into the following
// JSON:
//
// {
// "addrs": {
// "68a7deb0-615c-4f26-bf87-6b122732d8e9": [
// "1.1.1.1",
// "2.2.2.2",
// ...
// ],
// ...
// }
// }
func (a AddrsByWallet) MarshalJSON() ([]byte, error) {
type toMarshal struct {
Addrs map[string][]string `json:"addrs"`
}
m := &toMarshal{Addrs: make(map[string][]string)}
for wallet, addrSet := range a {
addrSlice := []string{}
for addr := range addrSet {
addrSlice = append(addrSlice, addr)
}
m.Addrs[wallet.String()] = addrSlice
}
return json.Marshal(m)
}
// UnmarshalJSON unmarshalls the given JSON and turns it back into the given
// AddrsByWallet.
func (a *AddrsByWallet) UnmarshalJSON(data []byte) error {
*a = make(AddrsByWallet)
// Create an anonymous struct into which we're going to unmarshal the given
// data bytes.
s := &struct {
AddrsByWallet map[uuid.UUID][]string `json:"addrs"`
}{
AddrsByWallet: make(map[uuid.UUID][]string),
}
if err := json.Unmarshal(data, s); err != nil {
return err
}
// Now add the key/value pairs from our anonymous struct to our
// AddrsByWallet.
for walletID, addrs := range s.AddrsByWallet {
addrSet := make(AddressSet)
for _, addr := range addrs {
addrSet[addr] = empty{}
}
(*a)[walletID] = addrSet
}
return nil
}