forked from cruzbit/cruzbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
211 lines (180 loc) · 7.47 KB
/
protocol.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
// Copyright 2019 cruzbit developers
// Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
package cruzbit
import "golang.org/x/crypto/ed25519"
// Protocol is the name of this version of the cruzbit peer protocol.
const Protocol = "cruzbit.1"
// Message is a message frame for all messages in the cruzbit.1 protocol.
type Message struct {
Type string `json:"type"`
Body interface{} `json:"body,omitempty"`
}
// InvBlockMessage is used to communicate blocks available for download.
// Type: "inv_block".
type InvBlockMessage struct {
BlockIDs []BlockID `json:"block_ids"`
}
// GetBlockMessage is used to request a block for download.
// Type: "get_block".
type GetBlockMessage struct {
BlockID BlockID `json:"block_id"`
}
// GetBlockByHeightMessage is used to request a block for download.
// Type: "get_block_by_height".
type GetBlockByHeightMessage struct {
Height int64 `json:"height"`
}
// BlockMessage is used to send a peer a complete block.
// Type: "block".
type BlockMessage struct {
BlockID *BlockID `json:"block_id,omitempty"`
Block *Block `json:"block,omitempty"`
}
// GetBlockHeaderMessage is used to request a block header.
// Type: "get_block_header".
type GetBlockHeaderMessage struct {
BlockID BlockID `json:"block_id"`
}
// GetBlockHeaderByHeghtMessage is used to request a block header.
// Type: "get_block_header_by_height".
type GetBlockHeaderByHeightMessage struct {
Height int64 `json:"height"`
}
// BlockHeaderMessage is used to send a peer a block's heder.
// Type: "block_header".
type BlockHeaderMessage struct {
BlockID *BlockID `json:"block_id,omitempty"`
BlockHeader *BlockHeader `json:"header,omitempty"`
}
// FindCommonAncestorMessage is used to find a common ancestor with a peer.
// Type: "find_common_ancestor".
type FindCommonAncestorMessage struct {
BlockIDs []BlockID `json:"block_ids"`
}
// GetBalanceMessage requests a public key's balance.
// Type: "get_balance".
type GetBalanceMessage struct {
PublicKey ed25519.PublicKey `json:"public_key"`
}
// BalanceMessage is used to send a public key's balance to a peer.
// Type: "balance".
type BalanceMessage struct {
BlockID *BlockID `json:"block_id,omitempty"`
Height int64 `json:"height,omitempty"`
PublicKey ed25519.PublicKey `json:"public_key"`
Balance int64 `json:"balance"`
Error string `json:"error,omitempty"`
}
// GetBalancesMessage requests a set of public key balances.
// Type: "get_balances".
type GetBalancesMessage struct {
PublicKeys []ed25519.PublicKey `json:"public_keys"`
}
// BalancesMessage is used to send a public key balances to a peer.
// Type: "balances".
type BalancesMessage struct {
BlockID *BlockID `json:"block_id,omitempty"`
Height int64 `json:"height,omitempty"`
Balances []PublicKeyBalance `json:"balances,omitempty"`
Error string `json:"error,omitempty"`
}
// PublicKeyBalance is an entry in the BalancesMessage's Balances field.
type PublicKeyBalance struct {
PublicKey ed25519.PublicKey `json:"public_key"`
Balance int64 `json:"balance"`
}
// GetTransactioMessage is used to request a confirmed transaction.
// Type: "get_transaction".
type GetTransactionMessage struct {
TransactionID TransactionID `json:"transaction_id"`
}
// TransactionMessage us used to send a peer a confirmed transaction.
// Type: "transaction"
type TransactionMessage struct {
BlockID *BlockID `json:"block_id,omitempty"`
Height int64 `json:"height,omitempty"`
TransactionID TransactionID `json:"transaction_id"`
Transaction *Transaction `json:"transaction,omitempty"`
}
// TipHeaderMessage is used to send a peer the header for the tip block in the block chain.
// Type: "tip_header". It is sent in response to the empty "get_tip_header" message type.
type TipHeaderMessage struct {
BlockID *BlockID `json:"block_id,omitempty"`
BlockHeader *BlockHeader `json:"header,omitempty"`
TimeSeen int64 `json:"time_seen,omitempty"`
}
// PushTransactionMessage is used to push a newly processed unconfirmed transaction to peers.
// Type: "push_transaction".
type PushTransactionMessage struct {
Transaction *Transaction `json:"transaction"`
}
// PushTransactionResultMessage is sent in response to a PushTransactionMessage.
// Type: "push_transaction_result".
type PushTransactionResultMessage struct {
TransactionID TransactionID `json:"transaction_id"`
Error string `json:"error,omitempty"`
}
// FilterLoadMessage is used to request that we load a filter which is used to
// filter transactions returned to the peer based on interest.
// Type: "filter_load"
type FilterLoadMessage struct {
Type string `json:"type"`
Filter []byte `json:"filter"`
}
// FilterAddMessage is used to request the addition of the given public keys to the current filter.
// The filter is created if it's not set.
// Type: "filter_add".
type FilterAddMessage struct {
PublicKeys []ed25519.PublicKey `json:"public_keys"`
}
// FilterResultMessage indicates whether or not the filter request was successful.
// Type: "filter_result".
type FilterResultMessage struct {
Error string `json:"error,omitempty"`
}
// FilterBlockMessage represents a pared down block containing only transactions relevant to the peer given their filter.
// Type: "filter_block".
type FilterBlockMessage struct {
BlockID BlockID `json:"block_id"`
Header *BlockHeader `json:"header"`
Transactions []*Transaction `json:"transactions"`
}
// FilterTransactionQueueMessage returns a pared down view of the unconfirmed transaction queue containing only
// transactions relevant to the peer given their filter.
// Type: "filter_transaction_queue".
type FilterTransactionQueueMessage struct {
Transactions []*Transaction `json:"transactions"`
Error string `json:"error,omitempty"`
}
// GetPublicKeyTransactionsMessage requests transactions associated with a given public key over a given
// height range of the block chain.
// Type: "get_public_key_transactions".
type GetPublicKeyTransactionsMessage struct {
PublicKey ed25519.PublicKey `json:"public_key"`
StartHeight int64 `json:"start_height"`
StartIndex int `json:"start_index"`
EndHeight int64 `json:"end_height"`
Limit int `json:"limit"`
}
// PublicKeyTransactionsMessage is used to return a list of block headers and the transactions relevant to
// the public key over a given height range of the block chain.
// Type: "public_key_transactions".
type PublicKeyTransactionsMessage struct {
PublicKey ed25519.PublicKey `json:"public_key"`
StartHeight int64 `json:"start_height"`
StopHeight int64 `json:"stop_height"`
StopIndex int `json:"stop_index"`
FilterBlocks []*FilterBlockMessage `json:"filter_blocks"`
Error string `json:"error,omitempty"`
}
// PeerAddressesMessage is used to communicate a list of potential peer addresses known by a peer.
// Type: "peer_addresses". Sent in response to the empty "get_peer_addresses" message type.
type PeerAddressesMessage struct {
Addresses []string `json:"addresses"`
}
// TransactionRelayPolicyMessage is used to communicate this node's current settings for min fee and min amount.
// Type: "transaction_relay_policy". Sent in response to the empty "get_transaction_relay_policy" message type.
type TransactionRelayPolicyMessage struct {
MinFee int64 `json:"min_fee"`
MinAmount int64 `json:"min_amount"`
}