/
address.go
282 lines (254 loc) · 9.08 KB
/
address.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package address
import (
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/qascade/dcr/lib/collaboration/address/destination"
"github.com/qascade/dcr/lib/collaboration/address/source"
"github.com/qascade/dcr/lib/collaboration/address/transformation"
"github.com/qascade/dcr/lib/collaboration/config"
)
func init() {
log.SetLevel(log.DebugLevel)
log.SetOutput(os.Stdout)
}
type Graph struct {
Count int
AdjacencyList map[AddressRef][]AddressRef
AuthorityStatus map[AddressRef]bool
CachedSources map[AddressRef]DcrAddress
CachedTransformations map[AddressRef]DcrAddress
CachedDestinations map[AddressRef]DcrAddress
}
func NewGraph(cSources map[AddressRef]DcrAddress, cTransformations map[AddressRef]DcrAddress, cDestinations map[AddressRef]DcrAddress) (*Graph, error) {
log.Info("Graph is being populated...")
count := len(cSources) + len(cTransformations) + len(cDestinations)
adjList := make(map[AddressRef][]AddressRef)
authorityStatus := make(map[AddressRef]bool)
for tRef, tAddressI := range cTransformations {
authorityStatus[tRef] = false
tAddress, ok := tAddressI.(*TransformationAddress)
if !ok {
log.Error("The address is not of type TransformationAddress")
return nil, fmt.Errorf("the address is not of type TransformationAddress for addressRef: %s", tRef)
}
sourcesInfo := tAddress.Transformation.GetSourcesInfo()
for _, sourceMetadata := range sourcesInfo {
sAddress := cSources[AddressRef(sourceMetadata.AddressRef)]
if sAddress != nil {
adjList[tRef] = append(adjList[tRef], AddressRef(sourceMetadata.AddressRef))
continue
}
// A transformation may consume another transformation output as a potential source.
if AddressRef(sourceMetadata.AddressRef).IsTransformation() {
fmt.Printf("Info::transformation %s is consuming transformation output %s as a source\n", tRef, sourceMetadata.AddressRef)
adjList[tRef] = append(adjList[tRef], AddressRef(sourceMetadata.AddressRef))
} else {
log.Error("Source Address not found")
return nil, fmt.Errorf("source Address not found for addressRef: %s", sourceMetadata.AddressRef)
}
}
}
for dRef, dAddressI := range cDestinations {
authorityStatus[dRef] = false
dAddress, ok := dAddressI.(*DestinationAddress)
if !ok {
log.Error("The address is not of type DestinationAddress")
return nil, fmt.Errorf("the address is not of type DestinationAddress for addressRef: %s", dRef)
}
adjList[dRef] = append(adjList[dRef], AddressRef(dAddress.Destination.GetTransformationRef()))
}
for sRef := range cSources {
authorityStatus[sRef] = true // Sources are always authorized.
}
graph := &Graph{
Count: count,
AdjacencyList: adjList,
AuthorityStatus: authorityStatus,
CachedSources: cSources,
CachedTransformations: cTransformations,
CachedDestinations: cDestinations,
}
return graph, nil
}
// All AddressNodeTypes must implement this interface
type DcrAddress interface {
Authorize([]AddressRef, AddressRef) (bool, error) // Are we allowed to move further down the graph?
//Deref // Function that returns the real transformation
Type() AddressType // Returns the type of Address.
}
type SourceAddress struct {
Ref AddressRef
Source source.Source
Owner AddressRef //CollaboratorName
TransformationOwnersAllowed []AddressRef
DestinationsAllowed []AddressRef
SourceNoises map[AddressRef]map[string]string
}
func NewSourceAddress(ref AddressRef, owner string, transformationOwnersAllowed []AddressRef, destAllowed []AddressRef, source source.Source, sourceNoises map[AddressRef]map[string]string) DcrAddress {
// Owner is always allowed to consume its own source.
transformationOwnersAllowed = append(transformationOwnersAllowed, NewCollaboratorRef(owner))
return &SourceAddress{
Ref: ref,
Owner: NewCollaboratorRef(owner),
TransformationOwnersAllowed: transformationOwnersAllowed,
DestinationsAllowed: destAllowed,
Source: source,
SourceNoises: sourceNoises,
}
}
// Transformaton Owner is checked against Source TransformationOwnersAllowed.
// Destination owner is checked against Source DestinationsAllowed.
func (sa *SourceAddress) Authorize(parents []AddressRef, root AddressRef) (bool, error) {
for _, ref := range parents {
if ref.IsDestination() {
isAuthorized, err := sa.AuthorizeDestination(ref)
if !isAuthorized {
return false, err
}
}
if ref.IsTransformation() {
isAuthorized, err := sa.AuthorizeTransformation(ref)
if !isAuthorized {
return false, err
}
}
}
if root.IsDestination() {
return sa.AuthorizeDestination(root)
}
if root.IsTransformation() {
return sa.AuthorizeTransformation(root)
}
// If root is a source, it is always authorized.
return true, nil
}
func (sa *SourceAddress) AuthorizeTransformation(root AddressRef) (bool, error) {
tOwner := root.Collaborator()
tAllowed := false
for _, c := range sa.TransformationOwnersAllowed {
if c == tOwner {
tAllowed = true
break
}
}
if !tAllowed {
err := fmt.Errorf("transformation: %s not allowed to consume source: %s", root, sa.Ref)
log.Error(err)
return false, err
}
log.Infof("transformation: %s allowed to consume source: %s", root, sa.Ref)
return true, nil
}
func (sa *SourceAddress) AuthorizeDestination(root AddressRef) (bool, error) {
dAllowed := false
for _, ref := range sa.DestinationsAllowed {
if root == ref {
dAllowed = true
break
}
}
if !dAllowed {
err := fmt.Errorf("destination: %s not allowed to consume source: %s", root, sa.Ref)
return false, err
}
log.Infof("destination: %s allowed to consume source: %s", root, sa.Ref)
return true, nil
}
func (sa *SourceAddress) Type() AddressType {
return ADDRESS_TYPE_SOURCE
}
type TransformationAddress struct {
Ref AddressRef
Owner AddressRef
DestinationOwnersAllowed []AddressRef
DestinationsAllowed []AddressRef
Transformation transformation.Transformation
NoiseParams []string
}
func NewTransformationAddress(ref AddressRef, owner string, destinationOwnersAllowed []AddressRef, destAllowed []AddressRef, t transformation.Transformation, noiseParams []string) DcrAddress {
// Owner is always allowed to consume its own transformation.
destinationOwnersAllowed = append(destinationOwnersAllowed, NewCollaboratorRef(owner))
destAllowed = append(destAllowed, NewCollaboratorRef(owner))
return &TransformationAddress{
Ref: ref,
Owner: NewCollaboratorRef(owner),
DestinationOwnersAllowed: destinationOwnersAllowed,
DestinationsAllowed: destAllowed,
Transformation: t,
NoiseParams: noiseParams,
}
}
// Destination Owners are to checked against transformation DestinationOwnersAllowed.
func (ta *TransformationAddress) Authorize(parents []AddressRef, root AddressRef) (bool, error) {
log.Infof("Root %s is trying to consume transformation %s, Performing authorization", root, ta.Ref)
for _, ref := range parents {
if ref.IsDestination() {
isAuthorized, err := ta.AuthorizeDestination(ref)
if !isAuthorized {
return false, err
}
}
}
if root.IsDestination() {
return ta.AuthorizeDestination(root)
}
// If root is not a destination, it must be transformation itself.
return true, nil
}
func (ta *TransformationAddress) AuthorizeDestination(root AddressRef) (bool, error) {
dAllowed := false
dOwner := root.Collaborator()
for _, ref := range ta.DestinationOwnersAllowed {
if dOwner == ref {
dAllowed = true
break
}
}
if !dAllowed {
err := fmt.Errorf("destination: %s not allowed to consume transformation: %s", root, ta.Ref)
log.Error(err)
return false, err
}
log.Infof("destination: %s allowed to consume transformation: %s", root, ta.Ref)
return true, nil
}
func (ta *TransformationAddress) Type() AddressType {
return ADDRESS_TYPE_TRANSFORMATION
}
type DestinationAddress struct {
Ref AddressRef
Owner AddressRef
Destination destination.Destination
}
func NewDestinationAddress(ref AddressRef, owner AddressRef, dest destination.Destination) DcrAddress {
return &DestinationAddress{
Ref: ref,
Owner: AddressRef(owner),
Destination: dest,
}
}
func (da *DestinationAddress) Authorize(_ []AddressRef, _ AddressRef) (bool, error) {
// Movement from destination is always authorized.
log.Infof("Performing authorization for Destination %s.", da.Ref)
return true, nil
}
// Helper functions
func (da *DestinationAddress) Type() AddressType {
return ADDRESS_TYPE_DESTINATION
}
func getAddressRefSlice(s []string) []AddressRef {
addRefS := make([]AddressRef, 0)
for _, str := range s {
addRefS = append(addRefS, NewCollaboratorRef(str))
}
return addRefS
}
// Returns a slice of AddressRef from a slice of SourceDestinationAllowedSpec
func getTransformationRefSlice(destAllowed []config.SourceDestinationAllowedSpec) []AddressRef {
addS := make([]AddressRef, 0)
for _, dest := range destAllowed {
addS = append(addS, AddressRef(dest.Ref))
}
return addS
}