/
rpcserver.go
executable file
·4125 lines (4005 loc) · 148 KB
/
rpcserver.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package node
import (
"bytes"
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/hex"
js "encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"math/big"
"math/rand"
"net"
"net/http"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"git.parallelcoin.io/dev/9/cmd/node/mempool"
blockchain "git.parallelcoin.io/dev/9/pkg/chain"
chaincfg "git.parallelcoin.io/dev/9/pkg/chain/config"
"git.parallelcoin.io/dev/9/pkg/chain/fork"
chainhash "git.parallelcoin.io/dev/9/pkg/chain/hash"
indexers "git.parallelcoin.io/dev/9/pkg/chain/index"
"git.parallelcoin.io/dev/9/pkg/chain/mining"
cpuminer "git.parallelcoin.io/dev/9/pkg/chain/mining/cpu"
txscript "git.parallelcoin.io/dev/9/pkg/chain/tx/script"
"git.parallelcoin.io/dev/9/pkg/chain/wire"
database "git.parallelcoin.io/dev/9/pkg/db"
p "git.parallelcoin.io/dev/9/pkg/peer"
"git.parallelcoin.io/dev/9/pkg/rpc/json"
"git.parallelcoin.io/dev/9/pkg/util"
cl "git.parallelcoin.io/dev/9/pkg/util/cl"
ec "git.parallelcoin.io/dev/9/pkg/util/elliptic"
"github.com/btcsuite/websocket"
)
type commandHandler func(*rpcServer, interface{}, <-chan struct{}) (interface{}, error)
// gbtWorkState houses state that is used in between multiple RPC invocations to getblocktemplate.
type gbtWorkState struct {
sync.Mutex
lastTxUpdate time.Time
lastGenerated time.Time
prevHash *chainhash.Hash
minTimestamp time.Time
template *mining.BlockTemplate
notifyMap map[chainhash.Hash]map[int64]chan struct{}
timeSource blockchain.MedianTimeSource
algo string
}
// parsedRPCCmd represents a JSON-RPC request object that has been parsed into a known concrete command along with any error that might have happened while parsing it.
type parsedRPCCmd struct {
id interface{}
method string
cmd interface{}
err *json.RPCError
}
// retrievedTx represents a transaction that was either loaded from the transaction memory pool or from the database. When a transaction is loaded from the database, it is loaded with the raw serialized bytes while the mempool has the fully deserialized structure. This structure therefore will have one of the two fields set depending on where is was retrieved from. This is mainly done for efficiency to avoid extra serialization steps when possible.
type retrievedTx struct {
txBytes []byte
blkHash *chainhash.Hash // Only set when transaction is in a block.
tx *util.Tx
}
// rpcServer provides a concurrent safe RPC server to a chain server.
type rpcServer struct {
started int32
shutdown int32
Cfg rpcserverConfig
authsha [sha256.Size]byte
limitauthsha [sha256.Size]byte
ntfnMgr *wsNotificationManager
numClients int32
statusLines map[int]string
statusLock sync.RWMutex
wg sync.WaitGroup
gbtWorkState *gbtWorkState
helpCacher *helpCacher
requestProcessShutdown chan struct{}
quit chan int
}
// rpcserverConfig is a descriptor containing the RPC server configuration.
type rpcserverConfig struct {
// Listeners defines a slice of listeners for which the RPC server will take ownership of and accept connections. Since the RPC server takes ownership of these listeners, they will be closed when the RPC server is stopped.
Listeners []net.Listener
// StartupTime is the unix timestamp for when the server that is hosting the RPC server started.
StartupTime int64
// ConnMgr defines the connection manager for the RPC server to use. It provides the RPC server with a means to do things such as add, remove, connect, disconnect, and query peers as well as other connection-related data and tasks.
ConnMgr rpcserverConnManager
// SyncMgr defines the sync manager for the RPC server to use.
SyncMgr rpcserverSyncManager
// These fields allow the RPC server to interface with the local block chain data and state.
TimeSource blockchain.MedianTimeSource
Chain *blockchain.BlockChain
ChainParams *chaincfg.Params
DB database.DB
// TxMemPool defines the transaction memory pool to interact with.
TxMemPool *mempool.TxPool
// These fields allow the RPC server to interface with mining. Generator produces block templates and the CPUMiner solves them using the CPU. CPU mining is typically only useful for test purposes when doing regression or simulation testing.
Generator *mining.BlkTmplGenerator
CPUMiner *cpuminer.CPUMiner
// These fields define any optional indexes the RPC server can make use of to provide additional data when queried.
TxIndex *indexers.TxIndex
AddrIndex *indexers.AddrIndex
CfIndex *indexers.CfIndex
// The fee estimator keeps track of how long transactions are left in the mempool before they are mined into blocks.
FeeEstimator *mempool.FeeEstimator
// Algo sets the algorithm expected from the RPC endpoint. This allows multiple ports to serve multiple types of miners with one main node per algorithm. Currently 514 for scrypt and anything else passes for sha256d. After hard fork 1 there is 9, and may be expanded in the future (equihash, cuckoo and cryptonight all require substantial block header/tx formatting changes)
Algo string
}
// rpcserverConnManager represents a connection manager for use with the RPC server. The interface contract requires that all of these methods are safe for concurrent access.
type rpcserverConnManager interface {
// Connect adds the provided address as a new outbound peer. The permanent flag indicates whether or not to make the peer persistent and reconnect if the connection is lost. Attempting to connect to an already existing peer will return an error.
Connect(addr string, permanent bool) error
// RemoveByID removes the peer associated with the provided id from the list of persistent peers. Attempting to remove an id that does not exist will return an error.
RemoveByID(id int32) error
// RemoveByAddr removes the peer associated with the provided address from the list of persistent peers. Attempting to remove an address that does not exist will return an error.
RemoveByAddr(addr string) error
// DisconnectByID disconnects the peer associated with the provided id. This applies to both inbound and outbound peers. Attempting to remove an id that does not exist will return an error.
DisconnectByID(id int32) error
// DisconnectByAddr disconnects the peer associated with the provided address. This applies to both inbound and outbound peers. Attempting to remove an address that does not exist will return an error.
DisconnectByAddr(addr string) error
// ConnectedCount returns the number of currently connected peers.
ConnectedCount() int32
// NetTotals returns the sum of all bytes received and sent across the network for all peers.
NetTotals() (uint64, uint64)
// ConnectedPeers returns an array consisting of all connected peers.
ConnectedPeers() []rpcserverPeer
// PersistentPeers returns an array consisting of all the persistent peers.
PersistentPeers() []rpcserverPeer
// BroadcastMessage sends the provided message to all currently connected peers.
BroadcastMessage(msg wire.Message)
// AddRebroadcastInventory adds the provided inventory to the list of inventories to be rebroadcast at random intervals until they show up in a block.
AddRebroadcastInventory(iv *wire.InvVect, data interface{})
// RelayTransactions generates and relays inventory vectors for all of the passed transactions to all connected peers.
RelayTransactions(txns []*mempool.TxDesc)
}
// rpcserverPeer represents a peer for use with the RPC server. The interface contract requires that all of these methods are safe for concurrent access.
type rpcserverPeer interface {
// ToPeer returns the underlying peer instance.
ToPeer() *p.Peer
// IsTxRelayDisabled returns whether or not the peer has disabled transaction relay.
IsTxRelayDisabled() bool
// BanScore returns the current integer value that represents how close the peer is to being banned.
BanScore() uint32
// FeeFilter returns the requested current minimum fee rate for which transactions should be announced.
FeeFilter() int64
}
// rpcserverSyncManager represents a sync manager for use with the RPC server. The interface contract requires that all of these methods are safe for concurrent access.
type rpcserverSyncManager interface {
// IsCurrent returns whether or not the sync manager believes the chain is current as compared to the rest of the network.
IsCurrent() bool
// SubmitBlock submits the provided block to the network after processing it locally.
SubmitBlock(block *util.Block, flags blockchain.BehaviorFlags) (bool, error)
// Pause pauses the sync manager until the returned channel is closed.
Pause() chan<- struct{}
// SyncPeerID returns the ID of the peer that is currently the peer being used to sync from or 0 if there is none.
SyncPeerID() int32
// LocateHeaders returns the headers of the blocks after the first known block in the provided locators until the provided stop hash or the current tip is reached, up to a max of wire.MaxBlockHeadersPerMsg hashes.
LocateHeaders(locators []*chainhash.Hash, hashStop *chainhash.Hash) []wire.BlockHeader
}
// API version constants
const (
jsonrpcSemverString = "1.3.0"
jsonrpcSemverMajor = 1
jsonrpcSemverMinor = 3
jsonrpcSemverPatch = 0
)
const (
// rpcAuthTimeoutSeconds is the number of seconds a connection to the RPC server is allowed to stay open without authenticating before it is closed.
rpcAuthTimeoutSeconds = 10
// uint256Size is the number of bytes needed to represent an unsigned 256-bit integer.
uint256Size = 32
// gbtNonceRange is two 32-bit big-endian hexadecimal integers which represent the valid ranges of nonces returned by the getblocktemplate RPC.
gbtNonceRange = "00000000ffffffff"
// gbtRegenerateSeconds is the number of seconds that must pass before a new template is generated when the previous block hash has not changed and there have been changes to the available transactions in the memory pool.
gbtRegenerateSeconds = 60
// maxProtocolVersion is the max protocol version the server supports.
maxProtocolVersion = 70002
)
// Errors
var (
// ErrRPCNoWallet is an error returned to RPC clients when the provided command is recognized as a wallet command.
ErrRPCNoWallet = &json.RPCError{
Code: json.ErrRPCNoWallet,
Message: "This implementation does not implement wallet commands",
}
)
// Errors
var (
// ErrRPCUnimplemented is an error returned to RPC clients when the provided command is recognized, but not implemented.
ErrRPCUnimplemented = &json.RPCError{
Code: json.ErrRPCUnimplemented,
Message: "Command unimplemented",
}
)
var (
// gbtCapabilities describes additional capabilities returned with a block template generated by the getblocktemplate RPC. It is declared here to avoid the overhead of creating the slice on every invocation for constant data.
gbtCapabilities = []string{"proposal"}
)
var (
// gbtCoinbaseAux describes additional data that miners should include in the coinbase signature script. It is declared here to avoid the overhead of creating a new object on every invocation for constant data.
gbtCoinbaseAux = &json.GetBlockTemplateResultAux{
Flags: hex.EncodeToString(builderScript(txscript.
NewScriptBuilder().
AddData([]byte(mining.CoinbaseFlags)))),
}
)
var (
// gbtMutableFields are the manipulations the server allows to be made to block templates generated by the getblocktemplate RPC. It is declared here to avoid the overhead of creating the slice on every invocation for constant data.
gbtMutableFields = []string{
"time", "transactions/add", "prevblock", "coinbase/append",
}
)
// list of commands that we recognize, but for which pod has no support because it lacks support for wallet functionality. For these commands the user should ask a connected instance of btcwallet.
var rpcAskWallet = map[string]struct{}{
"addmultisigaddress": {},
"backupwallet": {},
"createencryptedwallet": {},
"createmultisig": {},
"dumpprivkey": {},
"dumpwallet": {},
"encryptwallet": {},
"getaccount": {},
"getaccountaddress": {},
"getaddressesbyaccount": {},
"getbalance": {},
"getnewaddress": {},
"getrawchangeaddress": {},
"getreceivedbyaccount": {},
"getreceivedbyaddress": {},
"gettransaction": {},
"gettxoutsetinfo": {},
"getunconfirmedbalance": {},
"getwalletinfo": {},
"importprivkey": {},
"importwallet": {},
"keypoolrefill": {},
"listaccounts": {},
"listaddressgroupings": {},
"listlockunspent": {},
"listreceivedbyaccount": {},
"listreceivedbyaddress": {},
"listsinceblock": {},
"listtransactions": {},
"listunspent": {},
"lockunspent": {},
"move": {},
"sendfrom": {},
"sendmany": {},
"sendtoaddress": {},
"setaccount": {},
"settxfee": {},
"signmessage": {},
"signrawtransaction": {},
"walletlock": {},
"walletpassphrase": {},
"walletpassphrasechange": {},
}
// rpcHandlers maps RPC command strings to appropriate handler functions. This is set by init because help references rpcHandlers and thus causes a dependency loop.
var rpcHandlers map[string]commandHandler
var rpcHandlersBeforeInit = map[string]commandHandler{
"addnode": handleAddNode,
"createrawtransaction": handleCreateRawTransaction,
// "debuglevel": handleDebugLevel,
"decoderawtransaction": handleDecodeRawTransaction,
"decodescript": handleDecodeScript,
"estimatefee": handleEstimateFee,
"generate": handleGenerate,
"getaddednodeinfo": handleGetAddedNodeInfo,
"getbestblock": handleGetBestBlock,
"getbestblockhash": handleGetBestBlockHash,
"getblock": handleGetBlock,
"getblockchaininfo": handleGetBlockChainInfo,
"getblockcount": handleGetBlockCount,
"getblockhash": handleGetBlockHash,
"getblockheader": handleGetBlockHeader,
"getblocktemplate": handleGetBlockTemplate,
"getcfilter": handleGetCFilter,
"getcfilterheader": handleGetCFilterHeader,
"getconnectioncount": handleGetConnectionCount,
"getcurrentnet": handleGetCurrentNet,
"getdifficulty": handleGetDifficulty,
"getgenerate": handleGetGenerate,
"gethashespersec": handleGetHashesPerSec,
"getheaders": handleGetHeaders,
"getinfo": handleGetInfo,
"getmempoolinfo": handleGetMempoolInfo,
"getmininginfo": handleGetMiningInfo,
"getnettotals": handleGetNetTotals,
"getnetworkhashps": handleGetNetworkHashPS,
"getpeerinfo": handleGetPeerInfo,
"getrawmempool": handleGetRawMempool,
"getrawtransaction": handleGetRawTransaction,
"gettxout": handleGetTxOut,
"getwork": handleGetWork,
"help": handleHelp,
"node": handleNode,
"ping": handlePing,
"searchrawtransactions": handleSearchRawTransactions,
"sendrawtransaction": handleSendRawTransaction,
"setgenerate": handleSetGenerate,
"stop": handleStop,
"submitblock": handleSubmitBlock,
"uptime": handleUptime,
"validateaddress": handleValidateAddress,
"verifychain": handleVerifyChain,
"verifymessage": handleVerifyMessage,
"version": handleVersion,
}
// Commands that are available to a limited user
var rpcLimited = map[string]struct{}{
// Websockets commands
"loadtxfilter": {},
"notifyblocks": {},
"notifynewtransactions": {},
"notifyreceived": {},
"notifyspent": {},
"rescan": {},
"rescanblocks": {},
"session": {},
// Websockets AND HTTP/S commands
"help": {},
// HTTP/S-only commands
"createrawtransaction": {},
"decoderawtransaction": {},
"decodescript": {},
"estimatefee": {},
"getbestblock": {},
"getbestblockhash": {},
"getblock": {},
"getblockcount": {},
"getblockhash": {},
"getblockheader": {},
"getcfilter": {},
"getcfilterheader": {},
"getcurrentnet": {},
"getdifficulty": {},
"getheaders": {},
"getinfo": {},
"getnettotals": {},
"getnetworkhashps": {},
"getrawmempool": {},
"getrawtransaction": {},
"gettxout": {},
"searchrawtransactions": {},
"sendrawtransaction": {},
"submitblock": {},
"uptime": {},
"validateaddress": {},
"verifymessage": {},
"version": {},
}
// Commands that are currently unimplemented, but should ultimately be.
var rpcUnimplemented = map[string]struct{}{
"estimatepriority": {},
"getchaintips": {},
"getmempoolentry": {},
"getnetworkinfo": {},
"getwork": {},
"invalidateblock": {},
"preciousblock": {},
"reconsiderblock": {},
}
// NotifyBlockConnected uses the newly-connected block to notify any long poll clients with a new block template when their existing block template is stale due to the newly connected block.
func (
state *gbtWorkState,
) NotifyBlockConnected(
blockHash *chainhash.Hash,
) {
go func() {
state.Lock()
statelasttxupdate := state.lastTxUpdate
state.Unlock()
state.notifyLongPollers(blockHash, statelasttxupdate)
}()
}
// NotifyMempoolTx uses the new last updated time for the transaction memory pool to notify any long poll clients with a new block template when their existing block template is stale due to enough time passing and the contents of the memory pool changing.
func (
state *gbtWorkState,
) NotifyMempoolTx(
lastUpdated time.Time,
) {
go func() {
state.Lock()
defer state.Unlock()
// No need to notify anything if no block templates have been generated yet.
if state.prevHash == nil || state.lastGenerated.IsZero() {
return
}
if time.Now().After(state.lastGenerated.Add(time.Second * gbtRegenerateSeconds)) {
state.notifyLongPollers(state.prevHash, lastUpdated)
}
}()
}
// blockTemplateResult returns the current block template associated with the state as a json.GetBlockTemplateResult that is ready to be encoded to JSON and returned to the caller. This function MUST be called with the state locked.
func (
state *gbtWorkState,
) blockTemplateResult(
useCoinbaseValue bool,
submitOld *bool,
) (
*json.GetBlockTemplateResult,
error,
) {
// Ensure the timestamps are still in valid range for the template. This should really only ever happen if the local clock is changed after the template is generated, but it's important to avoid serving invalid block templates.
template := state.template
msgBlock := template.Block
header := &msgBlock.Header
adjustedTime := state.timeSource.AdjustedTime()
maxTime := adjustedTime.Add(time.Second * blockchain.MaxTimeOffsetSeconds)
if header.Timestamp.After(maxTime) {
return nil, &json.RPCError{
Code: json.ErrRPCOutOfRange,
Message: fmt.Sprintf("The template time is after the "+
"maximum allowed time for a block - template "+
"time %v, maximum time %v", adjustedTime,
maxTime),
}
}
// Convert each transaction in the block template to a template result transaction. The result does not include the coinbase, so notice the adjustments to the various lengths and indices.
numTx := len(msgBlock.Transactions)
transactions := make([]json.GetBlockTemplateResultTx, 0, numTx-1)
txIndex := make(map[chainhash.Hash]int64, numTx)
for i, tx := range msgBlock.Transactions {
txHash := tx.TxHash()
txIndex[txHash] = int64(i)
// Skip the coinbase transaction.
if i == 0 {
continue
}
// Create an array of 1-based indices to transactions that come before this one in the transactions list which this one depends on. This is necessary since the created block must ensure proper ordering of the dependencies. A map is used before creating the final array to prevent duplicate entries when multiple inputs reference the same transaction.
dependsMap := make(map[int64]struct{})
for _, txIn := range tx.TxIn {
if idx, ok := txIndex[txIn.PreviousOutPoint.Hash]; ok {
dependsMap[idx] = struct{}{}
}
}
depends := make([]int64, 0, len(dependsMap))
for idx := range dependsMap {
depends = append(depends, idx)
}
// Serialize the transaction for later conversion to hex.
txBuf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
if err := tx.Serialize(txBuf); err != nil {
context := "Failed to serialize transaction"
return nil, internalRPCError(err.Error(), context)
}
bTx := util.NewTx(tx)
resultTx := json.GetBlockTemplateResultTx{
Data: hex.EncodeToString(txBuf.Bytes()),
Hash: txHash.String(),
Depends: depends,
Fee: template.Fees[i],
SigOps: template.SigOpCosts[i],
Weight: blockchain.GetTransactionWeight(bTx),
}
transactions = append(transactions, resultTx)
}
// Generate the block template reply. Note that following mutations are implied by the included or omission of fields: Including MinTime -> time/decrement Omitting CoinbaseTxn -> coinbase, generation
targetDifficulty := fmt.Sprintf("%064x", blockchain.CompactToBig(header.Bits))
templateID := encodeTemplateID(state.prevHash, state.lastGenerated)
reply := json.GetBlockTemplateResult{
Bits: strconv.FormatInt(int64(header.Bits), 16),
CurTime: header.Timestamp.Unix(),
Height: int64(template.Height),
PreviousHash: header.PrevBlock.String(),
WeightLimit: blockchain.MaxBlockWeight,
SigOpLimit: blockchain.MaxBlockSigOpsCost,
SizeLimit: wire.MaxBlockPayload,
Transactions: transactions,
Version: header.Version,
LongPollID: templateID,
SubmitOld: submitOld,
Target: targetDifficulty,
MinTime: state.minTimestamp.Unix(),
MaxTime: maxTime.Unix(),
Mutable: gbtMutableFields,
NonceRange: gbtNonceRange,
Capabilities: gbtCapabilities,
}
// If the generated block template includes transactions with witness data, then include the witness commitment in the GBT result.
if template.WitnessCommitment != nil {
reply.DefaultWitnessCommitment = hex.EncodeToString(template.WitnessCommitment)
}
if useCoinbaseValue {
reply.CoinbaseAux = gbtCoinbaseAux
reply.CoinbaseValue = &msgBlock.Transactions[0].TxOut[0].Value
} else {
// Ensure the template has a valid payment address associated with it when a full coinbase is requested.
if !template.ValidPayAddress {
return nil, &json.RPCError{
Code: json.ErrRPCInternal.Code,
Message: "A coinbase transaction has been " +
"requested, but the server has not " +
"been configured with any payment " +
"addresses via --miningaddr",
}
}
// Serialize the transaction for conversion to hex.
tx := msgBlock.Transactions[0]
txBuf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
if err := tx.Serialize(txBuf); err != nil {
context := "Failed to serialize transaction"
return nil, internalRPCError(err.Error(), context)
}
resultTx := json.GetBlockTemplateResultTx{
Data: hex.EncodeToString(txBuf.Bytes()),
Hash: tx.TxHash().String(),
Depends: []int64{},
Fee: template.Fees[0],
SigOps: template.SigOpCosts[0],
}
reply.CoinbaseTxn = &resultTx
}
return &reply, nil
}
// notifyLongPollers notifies any channels that have been registered to be notified when block templates are stale. This function MUST be called with the state locked.
func (
state *gbtWorkState,
) notifyLongPollers(
latestHash *chainhash.Hash,
lastGenerated time.Time,
) {
// Notify anything that is waiting for a block template update from a hash which is not the hash of the tip of the best chain since their work is now invalid.
for hash, channels := range state.notifyMap {
if !hash.IsEqual(latestHash) {
for _, c := range channels {
close(c)
}
delete(state.notifyMap, hash)
}
}
// Return now if the provided last generated timestamp has not been initialized.
if lastGenerated.IsZero() {
return
}
// Return now if there is nothing registered for updates to the current best block hash.
channels, ok := state.notifyMap[*latestHash]
if !ok {
return
}
// Notify anything that is waiting for a block template update from a block template generated before the most recently generated block template.
lastGeneratedUnix := lastGenerated.Unix()
for lastGen, c := range channels {
if lastGen < lastGeneratedUnix {
close(c)
delete(channels, lastGen)
}
}
// Remove the entry altogether if there are no more registered channels.
if len(channels) == 0 {
delete(state.notifyMap, *latestHash)
}
}
// templateUpdateChan returns a channel that will be closed once the block template associated with the passed previous hash and last generated time is stale. The function will return existing channels for duplicate parameters which allows to wait for the same block template without requiring a different channel for each client. This function MUST be called with the state locked.
func (
state *gbtWorkState,
) templateUpdateChan(
prevHash *chainhash.Hash,
lastGenerated int64,
) chan struct{} {
// Either get the current list of channels waiting for updates about changes to block template for the previous hash or create a new one.
channels, ok := state.notifyMap[*prevHash]
if !ok {
m := make(map[int64]chan struct{})
state.notifyMap[*prevHash] = m
channels = m
}
// Get the current channel associated with the time the block template was last generated or create a new one.
c, ok := channels[lastGenerated]
if !ok {
c = make(chan struct{})
channels[lastGenerated] = c
}
return c
}
// updateBlockTemplate creates or updates a block template for the work state. A new block template will be generated when the current best block has changed or the transactions in the memory pool have been updated and it has been long enough since the last template was generated. Otherwise, the timestamp for the existing block template is updated (and possibly the difficulty on testnet per the consesus rules). Finally, if the useCoinbaseValue flag is false and the existing block template does not already contain a valid payment address, the block template will be updated with a randomly selected payment address from the list of configured addresses. This function MUST be called with the state locked.
func (
state *gbtWorkState,
) updateBlockTemplate(
s *rpcServer,
useCoinbaseValue bool,
) error {
generator := s.Cfg.Generator
lastTxUpdate := generator.TxSource().LastUpdated()
if lastTxUpdate.IsZero() {
lastTxUpdate = time.Now()
}
// Generate a new block template when the current best block has changed or the transactions in the memory pool have been updated and it has been at least gbtRegenerateSecond since the last template was generated.
var msgBlock *wire.MsgBlock
var targetDifficulty string
latestHash := &s.Cfg.Chain.BestSnapshot().Hash
template := state.template
if template == nil || state.prevHash == nil ||
!state.prevHash.IsEqual(latestHash) ||
(state.lastTxUpdate != lastTxUpdate &&
time.Now().After(state.lastGenerated.Add(time.Second*
gbtRegenerateSeconds))) {
// Reset the previous best hash the block template was generated against so any errors below cause the next invocation to try again.
state.prevHash = nil
// Choose a payment address at random if the caller requests a full coinbase as opposed to only the pertinent details needed to create their own coinbase.
var payAddr util.Address
if !useCoinbaseValue {
payAddr = StateCfg.ActiveMiningAddrs[rand.Intn(len(StateCfg.ActiveMiningAddrs))]
}
// Create a new block template that has a coinbase which anyone can redeem. This is only acceptable because the returned block template doesn't include the coinbase, so the caller will ultimately create their own coinbase which pays to the appropriate address(es).
blkTemplate, err := generator.NewBlockTemplate(payAddr, state.algo)
if err != nil {
return internalRPCError("(rpcserver.go) Failed to create new block "+
"template: "+err.Error(), "")
}
template = blkTemplate
msgBlock = template.Block
targetDifficulty = fmt.Sprintf("%064x",
blockchain.CompactToBig(msgBlock.Header.Bits))
// Get the minimum allowed timestamp for the block based on the median timestamp of the last several blocks per the chain consensus rules.
best := s.Cfg.Chain.BestSnapshot()
minTimestamp := mining.MinimumMedianTime(best)
// Update work state to ensure another block template isn't generated until needed.
state.template = template
state.lastGenerated = time.Now()
state.lastTxUpdate = lastTxUpdate
state.prevHash = latestHash
state.minTimestamp = minTimestamp
log <- cl.Debugf{
"generated block template (timestamp %v, target %s, merkle root %s)",
msgBlock.Header.Timestamp,
targetDifficulty,
msgBlock.Header.MerkleRoot,
}
// Notify any clients that are long polling about the new template.
state.notifyLongPollers(latestHash, lastTxUpdate)
} else {
// At this point, there is a saved block template and another request for a template was made, but either the available transactions haven't change or it hasn't been long enough to trigger a new block template to be generated. So, update the existing block template. When the caller requires a full coinbase as opposed to only the pertinent details needed to create their own coinbase, add a payment address to the output of the coinbase of the template if it doesn't already have one. Since this requires mining addresses to be specified via the config, an error is returned if none have been specified.
if !useCoinbaseValue && !template.ValidPayAddress {
// Choose a payment address at random.
payToAddr := StateCfg.ActiveMiningAddrs[rand.Intn(len(StateCfg.ActiveMiningAddrs))]
// Update the block coinbase output of the template to pay to the randomly selected payment address.
pkScript, err := txscript.PayToAddrScript(payToAddr)
if err != nil {
context := "Failed to create pay-to-addr script"
return internalRPCError(err.Error(), context)
}
template.Block.Transactions[0].TxOut[0].PkScript = pkScript
template.ValidPayAddress = true
// Update the merkle root.
block := util.NewBlock(template.Block)
merkles := blockchain.BuildMerkleTreeStore(block.Transactions(), false)
template.Block.Header.MerkleRoot = *merkles[len(merkles)-1]
}
// Set locals for convenience.
msgBlock = template.Block
targetDifficulty = fmt.Sprintf("%064x",
blockchain.CompactToBig(msgBlock.Header.Bits))
// Update the time of the block template to the current time while accounting for the median time of the past several blocks per the chain consensus rules.
generator.UpdateBlockTime(msgBlock)
msgBlock.Header.Nonce = 0
log <- cl.Debugf{
"updated block template (timestamp %v, target %s)",
msgBlock.Header.Timestamp,
targetDifficulty,
}
}
return nil
}
// NotifyNewTransactions notifies both websocket and getblocktemplate long poll clients of the passed transactions. This function should be called whenever new transactions are added to the mempool.
func (
s *rpcServer,
) NotifyNewTransactions(
txns []*mempool.TxDesc,
) {
for _, txD := range txns {
// Notify websocket clients about mempool transactions.
s.ntfnMgr.NotifyMempoolTx(txD.Tx, true)
// Potentially notify any getblocktemplate long poll clients about stale block templates due to the new transaction.
s.gbtWorkState.NotifyMempoolTx(s.Cfg.TxMemPool.LastUpdated())
}
}
// RequestedProcessShutdown returns a channel that is sent to when an authorized RPC client requests the process to shutdown. If the request can not be read immediately, it is dropped.
func (
s *rpcServer,
) RequestedProcessShutdown() <-chan struct{} {
return s.requestProcessShutdown
}
// Start is used by server.go to start the rpc listener.
func (
s *rpcServer,
) Start() {
if atomic.AddInt32(&s.started, 1) != 1 {
return
}
rpcServeMux := http.NewServeMux()
httpServer := &http.Server{
Handler: rpcServeMux,
// Timeout connections which don't complete the initial handshake within the allowed timeframe.
ReadTimeout: time.Second * rpcAuthTimeoutSeconds,
}
rpcServeMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Connection", "close")
w.Header().Set("Content-Type", "application/json")
r.Close = true
// Limit the number of connections to max allowed.
if s.limitConnections(w, r.RemoteAddr) {
return
}
// Keep track of the number of connected clients.
s.incrementClients()
defer s.decrementClients()
_, isAdmin, err := s.checkAuth(r, true)
if err != nil {
jsonAuthFail(w)
return
}
// Read and respond to the request.
s.jsonRPCRead(w, r, isAdmin)
})
// Websocket endpoint.
rpcServeMux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
authenticated, isAdmin, err := s.checkAuth(r, false)
if err != nil {
jsonAuthFail(w)
return
}
// Attempt to upgrade the connection to a websocket connection using the default size for read/write buffers.
ws, err := websocket.Upgrade(w, r, nil, 0, 0)
if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok {
log <- cl.Error{"unexpected websocket error:", err}
}
http.Error(w, "400 Bad Request.", http.StatusBadRequest)
return
}
s.WebsocketHandler(ws, r.RemoteAddr, authenticated, isAdmin)
})
for _, listener := range s.Cfg.Listeners {
s.wg.Add(1)
go func(listener net.Listener) {
log <- cl.Info{"RPC server listening on", listener.Addr()}
httpServer.Serve(listener)
log <- cl.Trace{"RPC listener done for", listener.Addr()}
s.wg.Done()
}(listener)
}
s.ntfnMgr.wg.Add(2)
s.ntfnMgr.Start()
}
// Stop is used by server.go to stop the rpc listener.
func (
s *rpcServer,
) Stop() error {
if atomic.AddInt32(&s.shutdown, 1) != 1 {
log <- cl.Inf("RPC server is already in the process of shutting down")
return nil
}
log <- cl.Wrn("RPC server shutting down")
for _, listener := range s.Cfg.Listeners {
err := listener.Close()
if err != nil {
log <- cl.Error{"problem shutting down RPC:", err}
return err
}
}
s.ntfnMgr.Shutdown()
s.ntfnMgr.WaitForShutdown()
close(s.quit)
s.wg.Wait()
log <- cl.Inf("RPC server shutdown complete")
return nil
}
// checkAuth checks the HTTP Basic authentication supplied by a wallet or RPC client in the HTTP request r. If the supplied authentication does not match the username and password expected, a non-nil error is returned. This check is time-constant. The first bool return value signifies auth success (true if successful) and the second bool return value specifies whether the user can change the state of the server (true) or whether the user is limited (false). The second is always false if the first is.
func (
s *rpcServer,
) checkAuth(
r *http.Request,
require bool,
) (
bool,
bool, error,
) {
authhdr := r.Header["Authorization"]
if len(authhdr) <= 0 {
if require {
log <- cl.Warn{"RPC authentication failure from", r.RemoteAddr}
return false, false, errors.New("auth failure")
}
return false, false, nil
}
authsha := sha256.Sum256([]byte(authhdr[0]))
// Check for limited auth first as in environments with limited users, those are probably expected to have a higher volume of calls
limitcmp := subtle.ConstantTimeCompare(authsha[:], s.limitauthsha[:])
if limitcmp == 1 {
return true, false, nil
}
// Check for admin-level auth
cmp := subtle.ConstantTimeCompare(authsha[:], s.authsha[:])
if cmp == 1 {
return true, true, nil
}
// Request's auth doesn't match either user
log <- cl.Warn{"RPC authentication failure from", r.RemoteAddr}
return false, false, errors.New("auth failure")
}
// decrementClients subtracts one from the number of connected RPC clients. Note this only applies to standard clients. Websocket clients have their own limits and are tracked separately. This function is safe for concurrent access.
func (
s *rpcServer,
) decrementClients() {
atomic.AddInt32(&s.numClients, -1)
}
// Callback for notifications from blockchain. It notifies clients that are long polling for changes or subscribed to websockets notifications.
func (
s *rpcServer,
) handleBlockchainNotification(
notification *blockchain.Notification,
) {
switch notification.Type {
case blockchain.NTBlockAccepted:
block, ok := notification.Data.(*util.Block)
if !ok {
log <- cl.Wrn("chain accepted notification is not a block")
break
}
// Allow any clients performing long polling via the getblocktemplate RPC to be notified when the new block causes their old block template to become stale.
s.gbtWorkState.NotifyBlockConnected(block.Hash())
case blockchain.NTBlockConnected:
block, ok := notification.Data.(*util.Block)
if !ok {
log <- cl.Wrn("chain connected notification is not a block")
break
}
// Notify registered websocket clients of incoming block.
s.ntfnMgr.NotifyBlockConnected(block)
case blockchain.NTBlockDisconnected:
block, ok := notification.Data.(*util.Block)
if !ok {
log <- cl.Wrn("chain disconnected notification is not a block.")
break
}
// Notify registered websocket clients.
s.ntfnMgr.NotifyBlockDisconnected(block)
}
}
// httpStatusLine returns a response Status-Line (RFC 2616 Section 6.1) for the given request and response status code. This function was lifted and adapted from the standard library HTTP server code since it's not exported.
func (
s *rpcServer,
) httpStatusLine(
req *http.Request,
code int,
) string {
// Fast path:
key := code
proto11 := req.ProtoAtLeast(1, 1)
if !proto11 {
key = -key
}
s.statusLock.RLock()
line, ok := s.statusLines[key]
s.statusLock.RUnlock()
if ok {
return line
}
// Slow path:
proto := "HTTP/1.0"
if proto11 {
proto = "HTTP/1.1"
}
codeStr := strconv.Itoa(code)
text := http.StatusText(code)
if text != "" {
line = proto + " " + codeStr + " " + text + "\r\n"
s.statusLock.Lock()
s.statusLines[key] = line
s.statusLock.Unlock()
} else {
text = "status code " + codeStr
line = proto + " " + codeStr + " " + text + "\r\n"
}
return line
}
// incrementClients adds one to the number of connected RPC clients. Note this only applies to standard clients. Websocket clients have their own limits and are tracked separately. This function is safe for concurrent access.
func (
s *rpcServer,
) incrementClients() {
atomic.AddInt32(&s.numClients, 1)
}
// jsonRPCRead handles reading and responding to RPC messages.
func (
s *rpcServer,
) jsonRPCRead(
w http.ResponseWriter,
r *http.Request,
isAdmin bool,
) {
if atomic.LoadInt32(&s.shutdown) != 0 {
return
}
// Read and close the JSON-RPC request body from the caller.
body, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
errCode := http.StatusBadRequest
http.Error(w, fmt.Sprintf("%d error reading JSON message: %v",
errCode, err), errCode)
return
}
// Unfortunately, the http server doesn't provide the ability to change the read deadline for the new connection and having one breaks long polling. However, not having a read deadline on the initial connection would mean clients can connect and idle forever. Thus, hijack the connecton from the HTTP server, clear the read deadline, and handle writing the response manually.
hj, ok := w.(http.Hijacker)
if !ok {
errMsg := "webserver doesn't support hijacking"
log <- cl.Warnf{errMsg}
errCode := http.StatusInternalServerError
http.Error(w, strconv.Itoa(errCode)+" "+errMsg, errCode)
return
}
conn, buf, err := hj.Hijack()
if err != nil {
log <- cl.Warn{"failed to hijack HTTP connection:", err}
errCode := http.StatusInternalServerError
http.Error(w, strconv.Itoa(errCode)+" "+err.Error(), errCode)
return
}
defer conn.Close()
defer buf.Flush()
conn.SetReadDeadline(timeZeroVal)
// Attempt to parse the raw body into a JSON-RPC request.
var responseID interface{}
var jsonErr error
var result interface{}
var request json.Request
if err := js.Unmarshal(body, &request); err != nil {
jsonErr = &json.RPCError{
Code: json.ErrRPCParse.Code,
Message: "Failed to parse request: " + err.Error(),
}
}
if jsonErr == nil {
/* The JSON-RPC 1.0 spec defines that notifications must have their "id" set to null and states that notifications do not have a response. A JSON-RPC 2.0 notification is a request with "json-rpc":"2.0", and without an "id" member.
The specification states that notifications must not be responded to. JSON-RPC 2.0 permits the null value as a valid request id, therefore such requests are not notifications.
Bitcoin Core serves requests with "id":null or even an absent "id", and responds to such requests with "id":null in the response. Pod does not respond to any request without and "id" or "id":null, regardless the indicated JSON-RPC protocol version unless RPC quirks are enabled.
With RPC quirks enabled, such requests will be responded to if the reqeust does not indicate JSON-RPC version. RPC quirks can be enabled by the user to avoid compatibility issues with software relying on Core's behavior.
*/
if request.ID == nil && !(*Cfg.RPCQuirks && request.Jsonrpc == "") {
return
}
// The parse was at least successful enough to have an ID so set it for the response.
responseID = request.ID
// Setup a close notifier. Since the connection is hijacked, the CloseNotifer on the ResponseWriter is not available.
closeChan := make(chan struct{}, 1)
go func() {
_, err := conn.Read(make([]byte, 1))
if err != nil {
close(closeChan)
}
}()
// Check if the user is limited and set error if method unauthorized
if !isAdmin {
if _, ok := rpcLimited[request.Method]; !ok {
jsonErr = &json.RPCError{
Code: json.ErrRPCInvalidParams.Code,
Message: "limited user not authorized for this method",
}
}
}
if jsonErr == nil {