summaryrefslogtreecommitdiff
path: root/txnsync/interfaces.go
blob: 77ac07163f6b58871384f1f1fa52b5152f679eca (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
// Copyright (C) 2019-2021 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 txnsync

import (
	"time"

	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/data/pooldata"
	"github.com/algorand/go-algorand/util/timers"
)

//msgp:ignore eventType
type eventType int

const (
	transactionPoolChangedEvent eventType = 1
	newRoundEvent               eventType = 2
)

// RoundSettings is used to communicate the transaction syncer setting for a specific round
type RoundSettings struct {
	Round             basics.Round
	FetchTransactions bool // for non-relays that has no participation keys, there is no need to request transactions
}

// Event is an external triggering event
type Event struct {
	eventType

	transactionPoolSize           int
	roundSettings                 RoundSettings
	transactionHandlerBacklogFull bool
}

// IncomingMessageHandler is the signature of the incoming message handler used by the transaction sync to receive network messages
type IncomingMessageHandler func(networkPeer interface{}, peer *Peer, message []byte, sequenceNumber uint64, receivedTimestamp int64) error

// SendMessageCallback define a message sent feedback for performing message tracking
type SendMessageCallback func(enqueued bool, sequenceNumber uint64) error

// PeerInfo describes a single peer returned by GetPeers or GetPeer
type PeerInfo struct {
	TxnSyncPeer *Peer
	NetworkPeer interface{}
	IsOutgoing  bool
}

// networkPeerAddress is a subset of the network package HTTPPeer and UnicastPeer interface that
// provides feedback for the destination address. It's used for logging out packet's destination addresses.
type networkPeerAddress interface {
	GetAddress() string
}

// NodeConnector is used by the transaction sync for communicating with components external to the txnsync package.
type NodeConnector interface {
	Events() <-chan Event
	GetCurrentRoundSettings() RoundSettings // return the current round settings from the node
	Clock() timers.WallClock
	Random(uint64) uint64
	GetPeers() []PeerInfo
	GetPeer(interface{}) PeerInfo // get a single peer given a network peer opaque interface
	// UpdatePeers call is being made to inform the node that either a link need to be established
	// between the set of the txsyncPeers peers and the set of netPeers, or that the peersAverageDataExchangeRate
	// was recalculated and could potentially be updated.
	// The peersAverageDataExchangeRate passed in here is the average communication rate ( measured in bytes per second )
	// across all the connected peers.
	UpdatePeers(txsyncPeers []*Peer, netPeers []interface{}, peersAverageDataExchangeRate uint64)
	SendPeerMessage(netPeer interface{}, msg []byte, callback SendMessageCallback)
	GetPeerLatency(netPeer interface{}) time.Duration
	// GetPendingTransactionGroups is called by the transaction sync when it needs to look into the transaction
	// pool and get the updated set of pending transactions. The second returned argument is the latest locally originated
	// group counter within the given transaction groups list. If there is no group that is locally originated, the expected
	// value is InvalidSignedTxGroupCounter.
	GetPendingTransactionGroups() (txGroups []pooldata.SignedTxGroup, latestLocallyOriginatedGroupCounter uint64)
	// IncomingTransactionGroups is called by the transaction sync when transactions have been received and need
	// to be stored in the transaction pool. The method returns the number of transactions in the transaction
	// pool before the txGroups is applied. A negative value is returned if the provided txGroups could not be applied
	// to the transaction pool.
	IncomingTransactionGroups(peer *Peer, messageSeq uint64, txGroups []pooldata.SignedTxGroup) (transactionPoolSize int)
	NotifyMonitor() chan struct{}
}

// MakeTransactionPoolChangeEvent creates an event for when a txn pool size has changed.
func MakeTransactionPoolChangeEvent(transactionPoolSize int, transactionHandlerBacklogFull bool) Event {
	return Event{
		eventType:                     transactionPoolChangedEvent,
		transactionPoolSize:           transactionPoolSize,
		transactionHandlerBacklogFull: transactionHandlerBacklogFull,
	}
}

// MakeNewRoundEvent creates an event for when a new round starts
func MakeNewRoundEvent(roundNumber basics.Round, fetchTransactions bool) Event {
	return Event{
		eventType: newRoundEvent,
		roundSettings: RoundSettings{
			Round:             roundNumber,
			FetchTransactions: fetchTransactions,
		},
	}
}