/
tendermint.go
702 lines (641 loc) · 20.1 KB
/
tendermint.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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tendermint
import (
"bytes"
"fmt"
"math/rand"
"os"
"sync/atomic"
"time"
"github.com/33cn/chain33/common/crypto"
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/queue"
drivers "github.com/33cn/chain33/system/consensus"
cty "github.com/33cn/chain33/system/dapp/coins/types"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/util"
ttypes "github.com/33cn/plugin/plugin/consensus/tendermint/types"
tmtypes "github.com/33cn/plugin/plugin/dapp/valnode/types"
"github.com/golang/protobuf/proto"
)
const (
tendermintVersion = "0.1.0"
)
var (
tendermintlog = log15.New("module", "tendermint")
genesis string
genesisAmount int64 = 1e8
genesisBlockTime int64
timeoutTxAvail int32 = 1000
timeoutPropose int32 = 3000 // millisecond
timeoutProposeDelta int32 = 500
timeoutPrevote int32 = 1000
timeoutPrevoteDelta int32 = 500
timeoutPrecommit int32 = 1000
timeoutPrecommitDelta int32 = 500
timeoutCommit int32 = 1000
skipTimeoutCommit = false
createEmptyBlocks = false
fastSync = false
preExec = false
createEmptyBlocksInterval int32 // second
validatorNodes = []string{"127.0.0.1:46656"}
peerGossipSleepDuration int32 = 100
peerQueryMaj23SleepDuration int32 = 2000
zeroHash [32]byte
random *rand.Rand
signName = "ed25519"
useAggSig = false
gossipVotes atomic.Value
)
func init() {
drivers.Reg("tendermint", New)
drivers.QueryData.Register("tendermint", &Client{})
random = rand.New(rand.NewSource(time.Now().UnixNano()))
}
// Client Tendermint implementation
type Client struct {
//config
*drivers.BaseClient
genesisDoc *ttypes.GenesisDoc // initial validator set
privValidator ttypes.PrivValidator
privKey crypto.PrivKey // local node's p2p key
pubKey string
csState *ConsensusState
csStore *ConsensusStore // save consensus state
node *Node
txsAvailable chan int64
stopC chan struct{}
}
type subConfig struct {
Genesis string `json:"genesis"`
GenesisAmount int64 `json:"genesisAmount"`
GenesisBlockTime int64 `json:"genesisBlockTime"`
TimeoutTxAvail int32 `json:"timeoutTxAvail"`
TimeoutPropose int32 `json:"timeoutPropose"`
TimeoutProposeDelta int32 `json:"timeoutProposeDelta"`
TimeoutPrevote int32 `json:"timeoutPrevote"`
TimeoutPrevoteDelta int32 `json:"timeoutPrevoteDelta"`
TimeoutPrecommit int32 `json:"timeoutPrecommit"`
TimeoutPrecommitDelta int32 `json:"timeoutPrecommitDelta"`
TimeoutCommit int32 `json:"timeoutCommit"`
SkipTimeoutCommit bool `json:"skipTimeoutCommit"`
CreateEmptyBlocks bool `json:"createEmptyBlocks"`
CreateEmptyBlocksInterval int32 `json:"createEmptyBlocksInterval"`
ValidatorNodes []string `json:"validatorNodes"`
FastSync bool `json:"fastSync"`
PreExec bool `json:"preExec"`
SignName string `json:"signName"`
UseAggregateSignature bool `json:"useAggregateSignature"`
}
func applyConfig(sub []byte) {
var subcfg subConfig
if sub != nil {
types.MustDecode(sub, &subcfg)
}
if subcfg.Genesis != "" {
genesis = subcfg.Genesis
}
if subcfg.GenesisAmount > 0 {
genesisAmount = subcfg.GenesisAmount
}
if subcfg.GenesisBlockTime > 0 {
genesisBlockTime = subcfg.GenesisBlockTime
}
if subcfg.TimeoutTxAvail > 0 {
timeoutTxAvail = subcfg.TimeoutTxAvail
}
if subcfg.TimeoutPropose > 0 {
timeoutPropose = subcfg.TimeoutPropose
}
if subcfg.TimeoutProposeDelta > 0 {
timeoutProposeDelta = subcfg.TimeoutProposeDelta
}
if subcfg.TimeoutPrevote > 0 {
timeoutPrevote = subcfg.TimeoutPrevote
}
if subcfg.TimeoutPrevoteDelta > 0 {
timeoutPrevoteDelta = subcfg.TimeoutPrevoteDelta
}
if subcfg.TimeoutPrecommit > 0 {
timeoutPrecommit = subcfg.TimeoutPrecommit
}
if subcfg.TimeoutPrecommitDelta > 0 {
timeoutPrecommitDelta = subcfg.TimeoutPrecommitDelta
}
if subcfg.TimeoutCommit > 0 {
timeoutCommit = subcfg.TimeoutCommit
}
skipTimeoutCommit = subcfg.SkipTimeoutCommit
createEmptyBlocks = subcfg.CreateEmptyBlocks
if subcfg.CreateEmptyBlocksInterval > 0 {
createEmptyBlocksInterval = subcfg.CreateEmptyBlocksInterval
}
if len(subcfg.ValidatorNodes) > 0 {
validatorNodes = subcfg.ValidatorNodes
}
fastSync = subcfg.FastSync
preExec = subcfg.PreExec
if subcfg.SignName != "" {
signName = subcfg.SignName
}
useAggSig = subcfg.UseAggregateSignature
gossipVotes.Store(true)
}
// DefaultDBProvider returns a database using the DBBackend and DBDir
// specified in the ctx.Config.
func DefaultDBProvider(name string) dbm.DB {
return dbm.NewDB(name, "leveldb", fmt.Sprintf("datadir%stendermint", string(os.PathSeparator)), 0)
}
// New ...
func New(cfg *types.Consensus, sub []byte) queue.Module {
tendermintlog.Info("Start to create tendermint client")
applyConfig(sub)
//init rand
ttypes.Init()
signType, ok := ttypes.SignMap[signName]
if !ok {
tendermintlog.Error("Invalid sign name")
return nil
}
ttypes.CryptoName = types.GetSignName("", signType)
cr, err := crypto.Load(ttypes.CryptoName, -1)
if err != nil {
tendermintlog.Error("NewTendermintClient", "err", err)
return nil
}
ttypes.ConsensusCrypto = cr
if useAggSig {
_, err = crypto.ToAggregate(ttypes.ConsensusCrypto)
if err != nil {
tendermintlog.Error("ConsensusCrypto not support aggregate signature", "name", ttypes.CryptoName)
return nil
}
}
genDoc, err := ttypes.GenesisDocFromFile("genesis.json")
if err != nil {
tendermintlog.Error("NewTendermintClient", "msg", "GenesisDocFromFile fail", "error", err)
return nil
}
privValidator := ttypes.LoadOrGenPrivValidatorFS("priv_validator.json")
if privValidator == nil {
tendermintlog.Error("NewTendermintClient create priv_validator file fail")
return nil
}
ttypes.InitMessageMap()
priv := privValidator.PrivKey
pubkey := privValidator.GetPubKey().KeyString()
c := drivers.NewBaseClient(cfg)
client := &Client{
BaseClient: c,
genesisDoc: genDoc,
privValidator: privValidator,
privKey: priv,
pubKey: pubkey,
csStore: NewConsensusStore(),
txsAvailable: make(chan int64, 1),
stopC: make(chan struct{}, 1),
}
c.SetChild(client)
return client
}
// GenesisDoc returns the Node's GenesisDoc.
func (client *Client) GenesisDoc() *ttypes.GenesisDoc {
return client.genesisDoc
}
// GenesisState returns the Node's GenesisState.
func (client *Client) GenesisState() *State {
state, err := MakeGenesisState(client.genesisDoc)
if err != nil {
tendermintlog.Error("GenesisState", "err", err)
return nil
}
return &state
}
// Close TODO:may need optimize
func (client *Client) Close() {
client.BaseClient.Close()
if client.node != nil {
client.node.Stop()
}
client.stopC <- struct{}{}
tendermintlog.Info("consensus tendermint closed")
}
// SetQueueClient ...
func (client *Client) SetQueueClient(q queue.Client) {
client.InitClient(q, func() {
//call init block
client.InitBlock()
})
go client.EventLoop()
go client.StartConsensus()
}
// StartConsensus a routine that make the consensus start
func (client *Client) StartConsensus() {
//进入共识前先同步到最大高度
hint := time.NewTicker(5 * time.Second)
beg := time.Now()
OuterLoop:
for fastSync {
select {
case <-hint.C:
tendermintlog.Info("Still catching up max height......", "Height", client.GetCurrentHeight(), "cost", time.Since(beg))
default:
if client.IsCaughtUp() {
tendermintlog.Info("This node has caught up max height")
break OuterLoop
}
time.Sleep(time.Second)
}
}
hint.Stop()
// load state
var state State
if client.GetCurrentHeight() == 0 {
genState := client.GenesisState()
if genState == nil {
panic("StartConsensus GenesisState fail")
}
state = genState.Copy()
} else if client.GetCurrentHeight() <= client.csStore.LoadStateHeight() {
stoState := client.csStore.LoadStateFromStore()
if stoState == nil {
panic("StartConsensus LoadStateFromStore fail")
}
state = LoadState(stoState)
tendermintlog.Info("Load state from store")
} else {
height := client.GetCurrentHeight()
blkState := client.LoadBlockState(height)
if blkState == nil {
panic("StartConsensus LoadBlockState fail")
}
state = LoadState(blkState)
tendermintlog.Info("Load state from block")
//save initial state in store
blkCommit := client.LoadBlockCommit(height)
if blkCommit == nil {
panic("StartConsensus LoadBlockCommit fail")
}
err := client.csStore.SaveConsensusState(height-1, blkState, blkCommit)
if err != nil {
panic(fmt.Sprintf("StartConsensus SaveConsensusState fail: %v", err))
}
tendermintlog.Info("Save state from block")
}
// start
tendermintlog.Info("StartConsensus",
"privValidator", fmt.Sprintf("%X", ttypes.Fingerprint(client.privValidator.GetAddress())),
"state", state)
// Log whether this node is a validator or an observer
if state.Validators.HasAddress(client.privValidator.GetAddress()) {
tendermintlog.Info("This node is a validator")
} else {
tendermintlog.Info("This node is not a validator")
}
stateDB := NewStateDB(client, state)
// make block executor for consensus and blockchain reactors to execute blocks
blockExec := NewBlockExecutor(stateDB)
// Make ConsensusReactor
csState := NewConsensusState(client, state, blockExec)
// reset height, round, state begin at newheigt,0,0
client.privValidator.ResetLastHeight(state.LastBlockHeight)
csState.SetPrivValidator(client.privValidator)
client.csState = csState
// Create & add listener
protocol, listeningAddress := "tcp", "0.0.0.0:46656"
node := NewNode(validatorNodes, protocol, listeningAddress, client.privKey, state.ChainID, tendermintVersion, csState)
client.node = node
node.Start()
go client.CreateBlock()
}
// GetGenesisBlockTime ...
func (client *Client) GetGenesisBlockTime() int64 {
return genesisBlockTime
}
// CreateGenesisTx ...
func (client *Client) CreateGenesisTx() (ret []*types.Transaction) {
var tx types.Transaction
tx.Execer = []byte(client.GetAPI().GetConfig().GetCoinExec())
tx.To = genesis
//gen payload
g := &cty.CoinsAction_Genesis{}
g.Genesis = &types.AssetsGenesis{}
g.Genesis.Amount = genesisAmount * client.GetAPI().GetConfig().GetCoinPrecision()
tx.Payload = types.Encode(&cty.CoinsAction{Value: g, Ty: cty.CoinsActionGenesis})
ret = append(ret, &tx)
return
}
func (client *Client) getBlockInfoTx(current *types.Block) (*tmtypes.ValNodeAction, error) {
//检查第一笔交易
if len(current.Txs) == 0 {
return nil, types.ErrEmptyTx
}
baseTx := current.Txs[0]
var valAction tmtypes.ValNodeAction
err := types.Decode(baseTx.GetPayload(), &valAction)
if err != nil {
return nil, err
}
//检查交易类型
if valAction.GetTy() != tmtypes.ValNodeActionBlockInfo {
return nil, ttypes.ErrBaseTxType
}
//检查交易内容
if valAction.GetBlockInfo() == nil {
return nil, ttypes.ErrBlockInfoTx
}
return &valAction, nil
}
// CheckBlock 检查区块
func (client *Client) CheckBlock(parent *types.Block, current *types.BlockDetail) error {
cfg := client.GetAPI().GetConfig()
if current.Block.Difficulty != cfg.GetP(0).PowLimitBits {
return types.ErrBlockHeaderDifficulty
}
valAction, err := client.getBlockInfoTx(current.Block)
if err != nil {
return err
}
if parent.Height+1 != current.Block.Height {
return types.ErrBlockHeight
}
//判断exec 是否成功
if current.Receipts[0].Ty != types.ExecOk {
return ttypes.ErrBaseExecErr
}
info := valAction.GetBlockInfo()
if current.Block.Height > 1 {
lastValAction, err := client.getBlockInfoTx(parent)
if err != nil {
return err
}
lastInfo := lastValAction.GetBlockInfo()
lastProposalBlock := &ttypes.TendermintBlock{TendermintBlock: lastInfo.GetBlock()}
if !lastProposalBlock.HashesTo(info.Block.Header.LastBlockID.Hash) {
return ttypes.ErrLastBlockID
}
}
return nil
}
// ProcEvent reply not support action err
func (client *Client) ProcEvent(msg *queue.Message) bool {
msg.ReplyErr("Client", types.ErrActionNotSupport)
return true
}
// CreateBlock a routine monitor whether some transactions available and tell client by available channel
func (client *Client) CreateBlock() {
for {
if client.IsClosed() {
tendermintlog.Info("CreateBlock quit")
break
}
if !client.csState.IsRunning() {
tendermintlog.Info("consensus not running")
time.Sleep(500 * time.Millisecond)
continue
}
height, err := client.getLastHeight()
if err != nil {
continue
}
if !client.CheckTxsAvailable(height) {
time.Sleep(1000 * time.Millisecond)
continue
}
if height+1 == client.csState.GetRoundState().Height {
client.txsAvailable <- height + 1
}
time.Sleep(time.Duration(timeoutTxAvail) * time.Millisecond)
}
}
func (client *Client) getLastHeight() (int64, error) {
lastBlock, err := client.RequestLastBlock()
if err != nil {
return -1, err
}
return lastBlock.Height, nil
}
// TxsAvailable check available channel
func (client *Client) TxsAvailable() <-chan int64 {
return client.txsAvailable
}
// StopC stop client
func (client *Client) StopC() <-chan struct{} {
return client.stopC
}
// CheckTxsAvailable check whether some new transactions arriving
func (client *Client) CheckTxsAvailable(height int64) bool {
txs := client.RequestTx(1, nil)
txs = client.CheckTxDup(txs, height)
return len(txs) != 0
}
// CheckTxDup check transactions that duplicate
func (client *Client) CheckTxDup(txs []*types.Transaction, height int64) (transactions []*types.Transaction) {
cacheTxs := types.TxsToCache(txs)
var err error
cacheTxs, err = util.CheckTxDup(client.GetQueueClient(), cacheTxs, height)
if err != nil {
return txs
}
return types.CacheToTxs(cacheTxs)
}
// BuildBlock build a new block
func (client *Client) BuildBlock() *types.Block {
lastBlock, err := client.RequestLastBlock()
if err != nil {
tendermintlog.Error("BuildBlock fail", "err", err)
return nil
}
cfg := client.GetAPI().GetConfig()
txs := client.RequestTx(int(cfg.GetP(lastBlock.Height+1).MaxTxNumber)-1, nil)
// placeholder
tx0 := &types.Transaction{}
txs = append([]*types.Transaction{tx0}, txs...)
var newblock types.Block
newblock.ParentHash = lastBlock.Hash(cfg)
newblock.Height = lastBlock.Height + 1
client.AddTxsToBlock(&newblock, txs)
//固定难度
newblock.Difficulty = cfg.GetP(0).PowLimitBits
newblock.BlockTime = types.Now().Unix()
if lastBlock.BlockTime >= newblock.BlockTime {
newblock.BlockTime = lastBlock.BlockTime + 1
}
return &newblock
}
// CommitBlock call WriteBlock to commit to chain
func (client *Client) CommitBlock(block *types.Block) error {
cfg := client.GetAPI().GetConfig()
retErr := client.WriteBlock(nil, block)
if retErr != nil {
tendermintlog.Info("CommitBlock fail", "err", retErr)
if client.WaitBlock(block.Height) {
if !preExec {
return nil
}
curBlock, err := client.RequestBlock(block.Height)
if err == nil {
if bytes.Equal(curBlock.Hash(cfg), block.Hash(cfg)) {
tendermintlog.Info("already has block")
return nil
}
tendermintlog.Info("block is different", "block", block, "curBlock", curBlock)
if bytes.Equal(curBlock.Txs[0].Hash(), block.Txs[0].Hash()) {
tendermintlog.Warn("base tx is same, origin maybe same")
return nil
}
}
}
return retErr
}
return nil
}
// WaitBlock by height
func (client *Client) WaitBlock(height int64) bool {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
beg := time.Now()
for {
select {
case <-ticker.C:
tendermintlog.Info("Still waiting block......", "height", height, "cost", time.Since(beg))
default:
newHeight, err := client.getLastHeight()
if err == nil && newHeight >= height {
return true
}
time.Sleep(100 * time.Millisecond)
}
}
}
// QueryValidatorsByHeight ...
func (client *Client) QueryValidatorsByHeight(height int64) (*tmtypes.ValNodes, error) {
if height < 1 {
return nil, ttypes.ErrHeightLessThanOne
}
req := &tmtypes.ReqValNodes{Height: height}
param, err := proto.Marshal(req)
if err != nil {
tendermintlog.Error("QueryValidatorsByHeight marshal", "err", err)
return nil, types.ErrInvalidParam
}
msg := client.GetQueueClient().NewMessage("execs", types.EventBlockChainQuery,
&types.ChainExecutor{Driver: "valnode", FuncName: "GetValNodeByHeight", StateHash: zeroHash[:], Param: param})
err = client.GetQueueClient().Send(msg, true)
if err != nil {
tendermintlog.Error("QueryValidatorsByHeight send", "err", err)
return nil, err
}
msg, err = client.GetQueueClient().Wait(msg)
if err != nil {
tendermintlog.Info("QueryValidatorsByHeight result", "err", err)
return nil, err
}
return msg.GetData().(types.Message).(*tmtypes.ValNodes), nil
}
// QueryBlockInfoByHeight get blockInfo and block by height
func (client *Client) QueryBlockInfoByHeight(height int64) (*tmtypes.TendermintBlockInfo, *types.Block, error) {
if height < 1 {
return nil, nil, ttypes.ErrHeightLessThanOne
}
block, err := client.RequestBlock(height)
if err != nil {
return nil, nil, err
}
valAction, err := client.getBlockInfoTx(block)
if err != nil {
return nil, nil, err
}
return valAction.GetBlockInfo(), block, nil
}
// LoadBlockCommit by height
func (client *Client) LoadBlockCommit(height int64) *tmtypes.TendermintCommit {
blockInfo, _, err := client.QueryBlockInfoByHeight(height)
if err != nil {
tendermintlog.Error("LoadBlockCommit GetBlockInfo fail", "err", err)
return nil
}
return blockInfo.GetBlock().GetLastCommit()
}
// LoadBlockState by height
func (client *Client) LoadBlockState(height int64) *tmtypes.State {
blockInfo, _, err := client.QueryBlockInfoByHeight(height)
if err != nil {
tendermintlog.Error("LoadBlockState GetBlockInfo fail", "err", err)
return nil
}
return blockInfo.GetState()
}
// LoadProposalBlock by height
func (client *Client) LoadProposalBlock(height int64) *tmtypes.TendermintBlock {
blockInfo, block, err := client.QueryBlockInfoByHeight(height)
if err != nil {
tendermintlog.Error("LoadProposal GetBlockInfo fail", "err", err)
return nil
}
proposalBlock := blockInfo.GetBlock()
proposalBlock.Data = block
return proposalBlock
}
// Query_IsHealthy query whether consensus is sync
func (client *Client) Query_IsHealthy(req *types.ReqNil) (types.Message, error) {
isHealthy := false
if client.IsCaughtUp() {
rs := client.csState.QueryRoundState()
if rs != nil && client.GetCurrentHeight() <= rs.Height+1 {
isHealthy = true
}
}
return &tmtypes.IsHealthy{IsHealthy: isHealthy}, nil
}
// Query_NodeInfo query validator node info
func (client *Client) Query_NodeInfo(req *types.ReqNil) (types.Message, error) {
rs := client.csState.QueryRoundState()
if rs == nil {
return nil, ttypes.ErrConsensusQuery
}
vals := rs.Validators.Validators
nodes := make([]*tmtypes.ValNodeInfo, 0)
for _, val := range vals {
if val == nil {
nodes = append(nodes, &tmtypes.ValNodeInfo{})
} else {
ipstr, idstr := "UNKOWN", "UNKOWN"
pub, err := ttypes.ConsensusCrypto.PubKeyFromBytes(val.PubKey)
if err != nil {
tendermintlog.Error("Query_NodeInfo invalid pubkey", "err", err)
} else {
id := GenIDByPubKey(pub)
idstr = string(id)
if id == client.node.ID {
ipstr = client.node.IP
} else {
ip := client.node.peerSet.GetIP(id)
if ip == nil {
tendermintlog.Error("Query_NodeInfo nil ip", "id", idstr)
} else {
ipstr = ip.String()
}
}
}
item := &tmtypes.ValNodeInfo{
NodeIP: ipstr,
NodeID: idstr,
Address: fmt.Sprintf("%X", val.Address),
PubKey: fmt.Sprintf("%X", val.PubKey),
VotingPower: val.VotingPower,
Accum: val.Accum,
}
nodes = append(nodes, item)
}
}
return &tmtypes.ValNodeInfoSet{Nodes: nodes}, nil
}
// CmpBestBlock 比较newBlock是不是最优区块
func (client *Client) CmpBestBlock(newBlock *types.Block, cmpBlock *types.Block) bool {
return false
}