summaryrefslogtreecommitdiff
path: root/agreement/fuzzer/messageDuplicationFilter_test.go
blob: 4a7d4e88c9ecd4b4743fcd7f50681e94372a0d1b (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
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
// 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 (
	"container/heap"
	"encoding/json"

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

// Duplicate message with delay
type MessageDuplicationFilter struct {
	NetworkFilter
	NetworkFilterFactory

	upstream   UpstreamFilter
	downstream DownstreamFilter

	nodeID                int
	upStreamDuplication   map[int]map[string]int
	downStreamDuplication map[int]map[string]int

	upMutex                deadlock.Mutex
	downMutex              deadlock.Mutex
	upStreamMessageQueue   PriorityQueue
	downStreamMessageQueue PriorityQueue
	currentTick            int
}

func (n *MessageDuplicationFilter) Init() {
	heap.Init(&n.upStreamMessageQueue)
	heap.Init(&n.downStreamMessageQueue)
}

func MakeMessageDuplicationFilter(upStreamDuplication map[int]map[string]int, downStreamDuplication map[int]map[string]int) *MessageDuplicationFilter {
	return &MessageDuplicationFilter{
		upStreamDuplication:   upStreamDuplication,
		downStreamDuplication: downStreamDuplication,
	}
}

// Return a Queue Item for pushing to the queue
func (n *MessageDuplicationFilter) MakeQueueItem(tic int, sourceNode, targetNode int, tag protocol.Tag, data []byte) (mqi *QueueItem) {

	return &QueueItem{
		priority: tic,
		message: AlgoMessage{
			sourceNode: sourceNode,
			targetNode: targetNode,
			tag:        tag,
			data:       data,
		},
	}
}

// If there is a downstream duplicate for the node, then compute the release tick for the duplicate and add to downstream message queue
func (n *MessageDuplicationFilter) SendMessage(sourceNode, targetNode int, tag protocol.Tag, data []byte) {

	tickDelay := 0
	if ticDelayTagMap, hasNode := n.downStreamDuplication[n.nodeID]; hasNode {
		if delay, hasTag := ticDelayTagMap[string(tag)]; hasTag {
			tickDelay = delay
		} else if delay, hasAny := ticDelayTagMap["*"]; hasAny {
			tickDelay = delay
		}
	}
	if tickDelay != 0 {
		newTick := n.currentTick + tickDelay
		//fmt.Printf("currentTick: %d node: %d tag: %s duplicating/delaying downstream message by %d, new tick %d\n", n.currentTick, n.nodeID, tag, tickDelay, newTick)
		n.downMutex.Lock()
		heap.Push(&n.downStreamMessageQueue, n.MakeQueueItem(newTick, sourceNode, targetNode, tag, data))
		n.downMutex.Unlock()
	}
	n.downstream.SendMessage(sourceNode, targetNode, tag, data)
}

func (n *MessageDuplicationFilter) GetDownstreamFilter() DownstreamFilter {
	return n.downstream
}

// if there is an upstream delay for the node, then compute the release tick for the duplicate and add to upstream message queue
func (n *MessageDuplicationFilter) ReceiveMessage(sourceNode int, tag protocol.Tag, data []byte) {
	tickDelay := 0
	if ticDelayTagMap, hasNode := n.upStreamDuplication[n.nodeID]; hasNode {
		if delay, hasTag := ticDelayTagMap[string(tag)]; hasTag {
			tickDelay = delay
		} else if delay, hasAny := ticDelayTagMap["*"]; hasAny {
			tickDelay = delay
		}
	}
	if tickDelay != 0 {
		newTick := n.currentTick + tickDelay
		//fmt.Printf("currentTick: %d node: %d tag: %s duplicating/delaying upstream message by %d, new tick %d\n", n.currentTick, n.nodeID, tag, tickDelay, newTick)
		n.upMutex.Lock()
		heap.Push(&n.upStreamMessageQueue, n.MakeQueueItem(newTick, sourceNode, 0, tag, data))
		n.upMutex.Unlock()
	}
	n.upstream.ReceiveMessage(sourceNode, tag, data)
}

func (n *MessageDuplicationFilter) SetDownstreamFilter(f DownstreamFilter) {
	n.downstream = f
}

func (n *MessageDuplicationFilter) SetUpstreamFilter(f UpstreamFilter) {
	n.upstream = f
}

func (n *MessageDuplicationFilter) CreateFilter(nodeID int, fuzzer *Fuzzer) NetworkFilter {
	networkFilter := MessageDuplicationFilter{
		nodeID:                nodeID,
		upStreamDuplication:   n.upStreamDuplication,
		downStreamDuplication: n.downStreamDuplication,
	}
	networkFilter.Init()
	return &networkFilter
}

func (n *MessageDuplicationFilter) Tick(newClockTime int) bool {
	// update the current tick
	n.currentTick = newClockTime
	// process messages in buffer
	sent := n.processDownstreamBuffer()
	received := n.processUpstreamBuffer()
	// forward the tick
	return n.upstream.Tick(newClockTime) || received || sent
}

// check for messages ready to send on downstream and upstream queues
func (n *MessageDuplicationFilter) processDownstreamBuffer() bool {
	n.downMutex.Lock()
	defer n.downMutex.Unlock()
	sent := false
	for n.downStreamMessageQueue.Len() > 0 {
		item := heap.Pop(&n.downStreamMessageQueue).(*QueueItem)
		if item.priority <= n.currentTick {
			// time on queue has expired, send the message
			n.downstream.SendMessage(item.message.sourceNode, item.message.targetNode, item.message.tag, item.message.data)
			//fmt.Printf("currentTick: %d node: %d releasing downstream message with tag %s and tick %d\n", n.currentTick, n.nodeID, item.message.tag, item.priority)
			sent = true
		} else {
			// we have gone beyond current time, push the message back to the queue and break
			heap.Push(&n.downStreamMessageQueue, item)
			//fmt.Printf("currentTick: %d node: %d pushing back downstream message with tick %d\n", n.currentTick, n.nodeID, item.priority)
			break
		}
	}
	return sent
}

func (n *MessageDuplicationFilter) processUpstreamBuffer() bool {
	n.upMutex.Lock()
	defer n.upMutex.Unlock()
	received := false
	for n.upStreamMessageQueue.Len() > 0 {
		item := heap.Pop(&n.upStreamMessageQueue).(*QueueItem)
		if item.priority <= n.currentTick {
			// time on queue has expired, receive the message
			n.upstream.ReceiveMessage(item.message.sourceNode, item.message.tag, item.message.data)
			//fmt.Printf("currentTick: %d node: %d releasing upstream message with tag %s and tick %d\n", n.currentTick, n.nodeID, item.message.tag, item.priority)
			received = true
		} else {
			// we have gone beyond current time, push the message back to the queue and break
			heap.Push(&n.upStreamMessageQueue, item)
			//fmt.Printf("currentTick: %d node: %d pushing back upstream message with tick %d\n", n.currentTick, n.nodeID, item.priority)
			break
		}
	}
	return received
}

// Unmarshall MessageDuplicationFilter
func (n *MessageDuplicationFilter) Unmarshal(b []byte) NetworkFilterFactory {
	type messageDuplicationFilterJSON struct {
		Name                  string
		UpStreamDuplication   map[int]map[string]int
		DownStreamDuplication map[int]map[string]int
	}

	var jsonConfig messageDuplicationFilterJSON
	if err := json.Unmarshal(b, &jsonConfig); err != nil {
		return nil
	}
	if jsonConfig.Name != "MessageDuplicationFilter" {
		return nil
	}
	return &MessageDuplicationFilter{
		upStreamDuplication:   jsonConfig.UpStreamDuplication,
		downStreamDuplication: jsonConfig.DownStreamDuplication,
	}
}

// register MessageDuplicationFilter
func init() {
	registeredFilterFactories = append(registeredFilterFactories, &MessageDuplicationFilter{})
}