-
Notifications
You must be signed in to change notification settings - Fork 13
/
node.go
262 lines (215 loc) · 6.42 KB
/
node.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
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package node
import (
"encoding/hex"
"time"
"github.com/bitmark-inc/bitmarkd/announce"
"github.com/bitmark-inc/bitmarkd/announce/rpc"
"github.com/bitmark-inc/bitmarkd/block"
"github.com/bitmark-inc/bitmarkd/blockdump"
"github.com/bitmark-inc/bitmarkd/blockheader"
"github.com/bitmark-inc/bitmarkd/counter"
"github.com/bitmark-inc/bitmarkd/difficulty"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/mode"
"github.com/bitmark-inc/bitmarkd/peer"
"github.com/bitmark-inc/bitmarkd/proof"
"github.com/bitmark-inc/bitmarkd/reservoir"
"github.com/bitmark-inc/bitmarkd/rpc/ratelimit"
"github.com/bitmark-inc/bitmarkd/storage"
"github.com/bitmark-inc/logger"
"golang.org/x/time/rate"
)
const (
rateLimitNode = 200
rateBurstNode = 100
)
// Node - type for RPC calls
type Node struct {
Log *logger.L
Limiter *rate.Limiter
Start time.Time
Version string
Announce announce.Announce
Pool storage.Handle
counter *counter.Counter
}
// limit for count
const maximumNodeList = 100
// ---
// Arguments - arguments for RPC
type Arguments struct {
Start uint64 `json:"start,string"`
Count int `json:"count"`
}
// Reply - result from RPC
type Reply struct {
Nodes []rpc.Entry `json:"nodes"`
NextStart uint64 `json:"nextStart,string"`
}
func New(log *logger.L, pools reservoir.Handles, start time.Time, version string, counter *counter.Counter, ann announce.Announce) *Node {
return &Node{
Log: log,
Limiter: rate.NewLimiter(rateLimitNode, rateBurstNode),
Start: start,
Version: version,
Announce: ann,
Pool: pools.Blocks,
counter: counter,
}
}
// List - list all node offering RPC functionality
func (node *Node) List(arguments *Arguments, reply *Reply) error {
if err := ratelimit.LimitN(node.Limiter, arguments.Count, maximumNodeList); nil != err {
return err
}
nodes, nextStart, err := node.Announce.Fetch(arguments.Start, arguments.Count)
if nil != err {
return err
}
reply.Nodes = nodes
reply.NextStart = nextStart
return nil
}
// ---
// InfoArguments - empty arguments for info request
type InfoArguments struct{}
// InfoReply - results from info request
type InfoReply struct {
Chain string `json:"chain"`
Mode string `json:"mode"`
Block BlockInfo `json:"block"`
Miner MinerInfo `json:"miner"`
RPCs uint64 `json:"rpcs"`
Peers uint64 `json:"peers"`
TransactionCounters Counters `json:"transactionCounters"`
Difficulty float64 `json:"difficulty"`
Hashrate float64 `json:"hashrate"`
Version string `json:"version"`
Uptime string `json:"uptime"`
PublicKey string `json:"publicKey"`
}
// BlockInfo - the highest block held by the node
type BlockInfo struct {
Height uint64 `json:"height"`
Hash string `json:"hash"`
}
// Counters - transaction counters
type Counters struct {
Pending int `json:"pending"`
Verified int `json:"verified"`
}
// MinerInfo - miner info, include success / failed mined block count
type MinerInfo struct {
Success uint64 `json:"success"`
Failed uint64 `json:"failed"`
}
// Info - return some information about this node
// only enough for clients to determine node state
// for more detail information use HTTP GET requests
func (node *Node) Info(_ *InfoArguments, reply *InfoReply) error {
if err := ratelimit.Limit(node.Limiter); nil != err {
return err
}
if node.Pool == nil {
return fault.DatabaseIsNotSet
}
in, out := peer.GetCounts()
reply.Chain = mode.ChainName()
reply.Mode = mode.String()
reply.Block = BlockInfo{
Height: blockheader.Height(),
Hash: block.LastBlockHash(node.Pool),
}
reply.Miner = MinerInfo{
Success: uint64(proof.MinedBlocks()),
Failed: uint64(proof.FailMinedBlocks()),
}
reply.RPCs = node.counter.Uint64()
reply.Peers = in + out
reply.TransactionCounters.Pending, reply.TransactionCounters.Verified = reservoir.ReadCounters()
reply.Difficulty = difficulty.Current.Value()
reply.Hashrate = difficulty.Hashrate()
reply.Version = node.Version
reply.Uptime = time.Since(node.Start).String()
reply.PublicKey = hex.EncodeToString(peer.PublicKey())
return nil
}
// BlockDumpArguments - the block to be dumped
type BlockDumpArguments struct {
Height uint64 `json:"height,string"`
Binary bool `json:"binary"`
}
// BlockDumpReply - BlockDump header and transactions
type BlockDumpReply struct {
Block interface{} `json:"block"`
}
// BlockDump - return a dump of the block
func (node *Node) BlockDump(arguments *BlockDumpArguments, reply *BlockDumpReply) error {
if err := ratelimit.Limit(node.Limiter); nil != err {
return err
}
block, err := blockdump.BlockDump(arguments.Height, arguments.Binary)
if nil == err {
reply.Block = block
}
return err
}
// BlockDecodeArguments - the block to be decodeed
type BlockDecodeArguments struct {
Packed []byte `json:"packed"`
}
// BlockDecodeReply - BlockDecode header and transactions
type BlockDecodeReply struct {
Block interface{} `json:"block"`
}
// BlockDecode - return a decoded version of the block
func (node *Node) BlockDecode(arguments *BlockDecodeArguments, reply *BlockDecodeReply) error {
if err := ratelimit.Limit(node.Limiter); nil != err {
return err
}
block, err := blockdump.BlockDecode(arguments.Packed, 0, true)
if nil == err {
reply.Block = block
}
return err
}
// BlockDumpRangeArguments - the block to be dumped
type BlockDumpRangeArguments struct {
Height uint64 `json:"height,string"`
Count int `json:"count"`
Txs bool `json:"txs"`
}
// BlockDumpRangeReply - BlockDumpRange header and transactions
type BlockDumpRangeReply struct {
Blocks []interface{} `json:"blocks"`
}
// BlockDumpRange - return a dump of the block
func (node *Node) BlockDumpRange(arguments *BlockDumpRangeArguments, reply *BlockDumpRangeReply) error {
if err := ratelimit.Limit(node.Limiter); nil != err {
return err
}
height := arguments.Height
count := arguments.Count
if count < 1 {
count = 1
}
if count > 100 {
count = 100
}
decodeTxs := arguments.Txs
blocks := make([]interface{}, count)
for i := 0; i < count; i += 1 {
block, err := blockdump.BlockDump(height, decodeTxs)
if nil != err {
return err
}
blocks[i] = block
height += 1
}
reply.Blocks = blocks
return nil
}