summaryrefslogtreecommitdiff
path: root/agreement/fuzzer/router_test.go
blob: 74e39c1ec11008f3ac760c04a5b73d35b10692bb (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
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
// Copyright (C) 2019-2023 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 fuzzer

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

// Router is the core router which delivers all messages between the nodes.
type Router struct {
	DownstreamFilter
	fuzzer   *Fuzzer
	q        []routedMessage
	activity int32
}

type routedMessage struct {
	tag      protocol.Tag
	data     []byte
	source   int
	target   int
	upstream UpstreamFilter
}

// MakeRouter creates a router
func MakeRouter(fuzzer *Fuzzer) *Router {
	r := &Router{
		fuzzer: fuzzer,
		q:      make([]routedMessage, 0),
	}
	return r
}

func (r *Router) getUpstreamFilter(targetNode int) (upstreamFilter UpstreamFilter) {
	currentFilter := DownstreamFilter(r.fuzzer.facades[targetNode])
	// go down the stream until we get to the router, and
	for {
		downstreamFilter := currentFilter.GetDownstreamFilter()

		if downstreamFilter == DownstreamFilter(r) {
			// we've reached the router. we need to go one step back.
			upstreamFilter, _ = currentFilter.(UpstreamFilter)
			return
		}
		currentFilter = downstreamFilter
	}
}

// SendMessage routes messages sent to message received.
func (r *Router) SendMessage(sourceNode, targetNode int, tag protocol.Tag, data []byte) {
	if targetNode >= 0 {
		r.sendMessageToNode(sourceNode, targetNode, tag, data)
		return
	}
	for i := 0; i < r.fuzzer.nodesCount; i++ {
		if i != sourceNode {
			r.sendMessageToNode(sourceNode, i, tag, data)
		}
	}
}

// GetDownstreamFilter implementation.
func (r *Router) GetDownstreamFilter() DownstreamFilter {
	return nil
}

// SendMessage routes messages sent to message received.
func (r *Router) sendMessageToNode(sourceNode, targetNode int, tag protocol.Tag, data []byte) {
	upstreamFilter := r.getUpstreamFilter(targetNode)
	if upstreamFilter == nil {
		return
	}
	if r.fuzzer.IsDisconnected(sourceNode, targetNode) {
		return
	}

	r.q = append(r.q, routedMessage{
		tag:      tag,
		data:     data,
		source:   sourceNode,
		target:   targetNode,
		upstream: upstreamFilter,
	})
}

func (r *Router) Start() {
}

func (r *Router) Shutdown() {
}

func (r *Router) sendMessage(targetNode int, tag protocol.Tag) bool {
	if len(r.q) == 0 {
		return false
	}
	for i, msg := range r.q {
		if msg.target == targetNode {
			if tag != "" && msg.tag != tag {
				continue
			}
			r.q = append(r.q[:i], r.q[i+1:]...)
			msg.upstream.ReceiveMessage(msg.source, msg.tag, msg.data)
			return true
		}
	}

	return false
}

func (r *Router) hasPendingMessage(targetNode int, tag protocol.Tag) bool {
	if len(r.q) == 0 {
		return false
	}
	for _, msg := range r.q {
		if msg.target == targetNode {
			if tag != "" && msg.tag != tag {
				continue
			}
			return true
		}
	}

	return false
}

// Tick send clock updates across all the filters.
func (r *Router) Tick(newClockTime int) bool {
	changed := false
	for i := 0; i < r.fuzzer.nodesCount; i++ {
		up := r.getUpstreamFilter(i)
		changed = up.Tick(newClockTime) || changed
	}
	return changed
}