Commit 9bceceab authored by caopingcp's avatar caopingcp

code cleanup

parent 88a82e81
...@@ -137,11 +137,6 @@ func (cs *ConsensusState) IsRunning() bool { ...@@ -137,11 +137,6 @@ func (cs *ConsensusState) IsRunning() bool {
} }
//---------------------------------------- //----------------------------------------
// String returns a string.
func (cs *ConsensusState) String() string {
// better not to access shared variables
return fmt.Sprintf("ConsensusState") //(H:%v R:%v S:%v", cs.Height, cs.Round, cs.Step)
}
// GetState returns a copy of the chain state. // GetState returns a copy of the chain state.
func (cs *ConsensusState) GetState() State { func (cs *ConsensusState) GetState() State {
...@@ -443,7 +438,7 @@ func (cs *ConsensusState) handleMsg(mi MsgInfo) { ...@@ -443,7 +438,7 @@ func (cs *ConsensusState) handleMsg(mi MsgInfo) {
} }
func (cs *ConsensusState) handleTimeout(ti timeoutInfo, rs ttypes.RoundState) { func (cs *ConsensusState) handleTimeout(ti timeoutInfo, rs ttypes.RoundState) {
tendermintlog.Debug("Received tock", "timeout", ti.Duration, "height", ti.Height, "round", ti.Round, "step", ti.Step) tendermintlog.Debug("Received tock", "timeout", ti.String())
// timeouts must be for current height, round, step // timeouts must be for current height, round, step
if ti.Height != rs.Height || ti.Round < rs.Round || (ti.Round == rs.Round && ti.Step < rs.Step) { if ti.Height != rs.Height || ti.Round < rs.Round || (ti.Round == rs.Round && ti.Step < rs.Step) {
......
...@@ -414,11 +414,6 @@ func (node *Node) addPeer(pc *peerConn) error { ...@@ -414,11 +414,6 @@ func (node *Node) addPeer(pc *peerConn) error {
return fmt.Errorf("get remote ip failed:%v", rErr) return fmt.Errorf("get remote ip failed:%v", rErr)
} }
// Filter peer against ID white list
//if err := node.FilterConnByID(peerID); err != nil {
//return err
//}
// Check version, chain id // Check version, chain id
if err := node.CompatibleWith(peerNodeInfo); err != nil { if err := node.CompatibleWith(peerNodeInfo); err != nil {
return err return err
...@@ -440,7 +435,6 @@ func (node *Node) addPeer(pc *peerConn) error { ...@@ -440,7 +435,6 @@ func (node *Node) addPeer(pc *peerConn) error {
if err := node.peerSet.Add(pc); err != nil { if err := node.peerSet.Add(pc); err != nil {
return err return err
} }
//node.metrics.Peers.Add(float64(1))
tendermintlog.Info("Added peer", "peer", pc.ip) tendermintlog.Info("Added peer", "peer", pc.ip)
return nil return nil
......
...@@ -698,12 +698,6 @@ OUTER_LOOP: ...@@ -698,12 +698,6 @@ OUTER_LOOP:
// If the peer is on a previous height, help catch up. // If the peer is on a previous height, help catch up.
if (0 < prs.Height) && (prs.Height < rs.Height) { if (0 < prs.Height) && (prs.Height < rs.Height) {
//if prs.Height+1 == rs.Height && prs.Round == rs.LastCommit.Round() && prs.Step == ttypes.RoundStepCommit && prs.ProposalBlock {
// tendermintlog.Debug("Peer is waiting for finalizeCommit finish", "peerip", pc.ip.String(),
// "state", fmt.Sprintf("%v/%v/%v", prs.Height, prs.Round, prs.Step))
// time.Sleep(10 * pc.myState.PeerGossipSleep())
// continue OUTER_LOOP
//}
if prs.ProposalBlockHash == nil || prs.ProposalBlock { if prs.ProposalBlockHash == nil || prs.ProposalBlock {
time.Sleep(pc.myState.PeerGossipSleep()) time.Sleep(pc.myState.PeerGossipSleep())
continue OUTER_LOOP continue OUTER_LOOP
...@@ -803,9 +797,6 @@ OUTER_LOOP: ...@@ -803,9 +797,6 @@ OUTER_LOOP:
rs := pc.myState.GetRoundState() rs := pc.myState.GetRoundState()
prs := pc.state prs := pc.state
//tendermintlog.Debug("gossipVotesRoutine", "rs(H/R/S)", fmt.Sprintf("%v/%v/%v", rs.Height, rs.Round, rs.Step.String()),
// "prs(H/R/S)", fmt.Sprintf("%v/%v/%v", prs.Height, prs.Round, prs.Step.String()),
// "precommits", rs.Votes.Precommits(prs.Round).BitArray().String(), "peerip", pc.ip.String())
switch sleeping { switch sleeping {
case 1: // First sleep case 1: // First sleep
......
...@@ -133,7 +133,6 @@ func (sc *SecretConnection) Write(data []byte) (n int, err error) { ...@@ -133,7 +133,6 @@ func (sc *SecretConnection) Write(data []byte) (n int, err error) {
// encrypt the frame // encrypt the frame
var sealedFrame = make([]byte, sealedFrameSize) var sealedFrame = make([]byte, sealedFrameSize)
secretbox.Seal(sealedFrame[:0], frame, sc.sendNonce, sc.shrSecret) secretbox.Seal(sealedFrame[:0], frame, sc.sendNonce, sc.shrSecret)
// fmt.Printf("secretbox.Seal(sealed:%X,sendNonce:%X,shrSecret:%X\n", sealedFrame, sc.sendNonce, sc.shrSecret)
incr2Nonce(sc.sendNonce) incr2Nonce(sc.sendNonce)
// end encryption // end encryption
...@@ -162,7 +161,6 @@ func (sc *SecretConnection) Read(data []byte) (n int, err error) { ...@@ -162,7 +161,6 @@ func (sc *SecretConnection) Read(data []byte) (n int, err error) {
// decrypt the frame // decrypt the frame
var frame = make([]byte, totalFrameSize) var frame = make([]byte, totalFrameSize)
// fmt.Printf("secretbox.Open(sealed:%X,recvNonce:%X,shrSecret:%X\n", sealedFrame, sc.recvNonce, sc.shrSecret)
_, ok := secretbox.Open(frame[:0], sealedFrame, sc.recvNonce, sc.shrSecret) _, ok := secretbox.Open(frame[:0], sealedFrame, sc.recvNonce, sc.shrSecret)
if !ok { if !ok {
return n, errors.New("Failed to decrypt SecretConnection") return n, errors.New("Failed to decrypt SecretConnection")
...@@ -298,9 +296,7 @@ func shareAuthSignature(sc io.ReadWriter, pubKey crypto.PubKey, signature crypto ...@@ -298,9 +296,7 @@ func shareAuthSignature(sc io.ReadWriter, pubKey crypto.PubKey, signature crypto
if err2 != nil { if err2 != nil {
return return
} }
//n := int(0) // not used.
//recvMsg = wire.ReadBinary(authSigMessage{}, bytes.NewBuffer(readBuffer), authSigMsgSize, &n, &err2).(authSigMessage)
//secret.Info("shareAuthSignature", "readBuffer", readBuffer)
recvMsg.Key, err2 = types.ConsensusCrypto.PubKeyFromBytes(readBuffer[:32]) recvMsg.Key, err2 = types.ConsensusCrypto.PubKeyFromBytes(readBuffer[:32])
if err2 != nil { if err2 != nil {
return return
......
...@@ -407,17 +407,30 @@ func (client *Client) CreateBlock() { ...@@ -407,17 +407,30 @@ func (client *Client) CreateBlock() {
if issleep { if issleep {
time.Sleep(time.Second) time.Sleep(time.Second)
} }
if !client.CheckTxsAvailable() { height, err := client.getLastHeight()
if err != nil {
issleep = true
continue
}
if !client.CheckTxsAvailable(height) {
issleep = true issleep = true
continue continue
} }
issleep = false issleep = false
client.txsAvailable <- client.GetCurrentHeight() + 1 client.txsAvailable <- height + 1
time.Sleep(time.Duration(timeoutTxAvail) * time.Millisecond) 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 // TxsAvailable check available channel
func (client *Client) TxsAvailable() <-chan int64 { func (client *Client) TxsAvailable() <-chan int64 {
return client.txsAvailable return client.txsAvailable
...@@ -429,9 +442,9 @@ func (client *Client) StopC() <-chan struct{} { ...@@ -429,9 +442,9 @@ func (client *Client) StopC() <-chan struct{} {
} }
// CheckTxsAvailable check whether some new transactions arriving // CheckTxsAvailable check whether some new transactions arriving
func (client *Client) CheckTxsAvailable() bool { func (client *Client) CheckTxsAvailable(height int64) bool {
txs := client.RequestTx(10, nil) txs := client.RequestTx(10, nil)
txs = client.CheckTxDup(txs, client.GetCurrentHeight()) txs = client.CheckTxDup(txs, height)
return len(txs) != 0 return len(txs) != 0
} }
...@@ -495,10 +508,9 @@ func (client *Client) CommitBlock(block *types.Block) error { ...@@ -495,10 +508,9 @@ func (client *Client) CommitBlock(block *types.Block) error {
// WaitBlock by height // WaitBlock by height
func (client *Client) WaitBlock(height int64) bool { func (client *Client) WaitBlock(height int64) bool {
retry := 0 retry := 0
var newHeight int64
for { for {
newHeight = client.GetCurrentHeight() newHeight, err := client.getLastHeight()
if newHeight >= height { if err == nil && newHeight >= height {
return true return true
} }
retry++ retry++
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"github.com/stretchr/testify/assert"
"math/rand" "math/rand"
"os" "os"
"testing" "testing"
...@@ -52,12 +53,12 @@ func init() { ...@@ -52,12 +53,12 @@ func init() {
log.SetLogLevel("info") log.SetLogLevel("info")
} }
func TestTendermintPerf(t *testing.T) { func TestTendermintPerf(t *testing.T) {
TendermintPerf() TendermintPerf(t)
fmt.Println("=======start clear test data!=======") fmt.Println("=======start clear test data!=======")
clearTestData() clearTestData()
} }
func TendermintPerf() { func TendermintPerf(t *testing.T) {
q, chain, s, mem, exec, cs, p2p := initEnvTendermint() q, chain, s, mem, exec, cs, p2p := initEnvTendermint()
defer chain.Close() defer chain.Close()
defer mem.Close() defer mem.Close()
...@@ -75,6 +76,7 @@ func TendermintPerf() { ...@@ -75,6 +76,7 @@ func TendermintPerf() {
NormPut() NormPut()
time.Sleep(time.Second) time.Sleep(time.Second)
} }
CheckState(t, cs.(*Client))
AddNode() AddNode()
for i := 0; i < loopCount*3; i++ { for i := 0; i < loopCount*3; i++ {
NormPut() NormPut()
...@@ -196,7 +198,7 @@ func AddNode() { ...@@ -196,7 +198,7 @@ func AddNode() {
action := &ty.ValNodeAction{Value: nput, Ty: ty.ValNodeActionUpdate} action := &ty.ValNodeAction{Value: nput, Ty: ty.ValNodeActionUpdate}
tx := &types.Transaction{Execer: []byte("valnode"), Payload: types.Encode(action), Fee: fee} tx := &types.Transaction{Execer: []byte("valnode"), Payload: types.Encode(action), Fee: fee}
tx.To = address.ExecAddress("valnode") tx.To = address.ExecAddress("valnode")
tx.Nonce = r.Int63() tx.Nonce = random.Int63()
tx.Sign(types.SECP256K1, getprivkey("CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944")) tx.Sign(types.SECP256K1, getprivkey("CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944"))
reply, err := c.SendTransaction(context.Background(), tx) reply, err := c.SendTransaction(context.Background(), tx)
...@@ -209,3 +211,13 @@ func AddNode() { ...@@ -209,3 +211,13 @@ func AddNode() {
return return
} }
} }
func CheckState(t *testing.T, client *Client) {
msg1, err := client.Query_IsHealthy(&types.ReqNil{})
assert.Nil(t, err)
flag := msg1.(*ty.IsHealthy).IsHealthy
assert.Equal(t, true, flag)
_, err = client.Query_NodeInfo(&types.ReqNil{})
assert.Nil(t, err)
}
...@@ -163,10 +163,8 @@ func (b *TendermintBlock) StringIndented(indent string) string { ...@@ -163,10 +163,8 @@ func (b *TendermintBlock) StringIndented(indent string) string {
return Fmt(`Block{ return Fmt(`Block{
%s %v %s %v
%s %v %s %v
%s %v
%s}#%v`, %s}#%v`,
indent, header.StringIndented(indent+" "), indent, header.StringIndented(indent+" "),
// indent, b.Evidence.StringIndented(indent+" "),
indent, lastCommit.StringIndented(indent+" "), indent, lastCommit.StringIndented(indent+" "),
indent, b.Hash()) indent, b.Hash())
} }
......
...@@ -100,9 +100,8 @@ func (hvs *HeightVoteSet) SetRound(round int) { ...@@ -100,9 +100,8 @@ func (hvs *HeightVoteSet) SetRound(round int) {
func (hvs *HeightVoteSet) addRound(round int) { func (hvs *HeightVoteSet) addRound(round int) {
if _, ok := hvs.roundVoteSets[round]; ok { if _, ok := hvs.roundVoteSets[round]; ok {
panic(Fmt("Panicked on a Sanity Check: %v", "addRound() for an existing round")) panic("addRound() for an existing round")
} }
// log.Debug("addRound(round)", "round", round)
prevotes := NewVoteSet(hvs.chainID, hvs.height, round, VoteTypePrevote, hvs.valSet) prevotes := NewVoteSet(hvs.chainID, hvs.height, round, VoteTypePrevote, hvs.valSet)
precommits := NewVoteSet(hvs.chainID, hvs.height, round, VoteTypePrecommit, hvs.valSet) precommits := NewVoteSet(hvs.chainID, hvs.height, round, VoteTypePrecommit, hvs.valSet)
hvs.roundVoteSets[round] = RoundVoteSet{ hvs.roundVoteSets[round] = RoundVoteSet{
...@@ -179,7 +178,7 @@ func (hvs *HeightVoteSet) getVoteSet(round int, voteType byte) *VoteSet { ...@@ -179,7 +178,7 @@ func (hvs *HeightVoteSet) getVoteSet(round int, voteType byte) *VoteSet {
case VoteTypePrecommit: case VoteTypePrecommit:
return rvs.Precommits return rvs.Precommits
default: default:
panic(Fmt("Panicked on a Sanity Check: %v", Fmt("Unexpected vote type %X", voteType))) panic(Fmt("Unexpected vote type %X", voteType))
} }
} }
......
...@@ -213,7 +213,6 @@ func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error { ...@@ -213,7 +213,6 @@ func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error {
// Hash ... // Hash ...
func (vote *Vote) Hash() []byte { func (vote *Vote) Hash() []byte {
if vote == nil { if vote == nil {
//votelog.Error("vote hash is nil")
return nil return nil
} }
bytes, err := json.Marshal(vote) bytes, err := json.Marshal(vote)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment