Commit 9bceceab authored by caopingcp's avatar caopingcp

code cleanup

parent 88a82e81
......@@ -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.
func (cs *ConsensusState) GetState() State {
......@@ -443,7 +438,7 @@ func (cs *ConsensusState) handleMsg(mi MsgInfo) {
}
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
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 {
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
if err := node.CompatibleWith(peerNodeInfo); err != nil {
return err
......@@ -440,7 +435,6 @@ func (node *Node) addPeer(pc *peerConn) error {
if err := node.peerSet.Add(pc); err != nil {
return err
}
//node.metrics.Peers.Add(float64(1))
tendermintlog.Info("Added peer", "peer", pc.ip)
return nil
......
......@@ -698,12 +698,6 @@ OUTER_LOOP:
// If the peer is on a previous height, help catch up.
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 {
time.Sleep(pc.myState.PeerGossipSleep())
continue OUTER_LOOP
......@@ -803,9 +797,6 @@ OUTER_LOOP:
rs := pc.myState.GetRoundState()
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 {
case 1: // First sleep
......
......@@ -133,7 +133,6 @@ func (sc *SecretConnection) Write(data []byte) (n int, err error) {
// encrypt the frame
var sealedFrame = make([]byte, sealedFrameSize)
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)
// end encryption
......@@ -162,7 +161,6 @@ func (sc *SecretConnection) Read(data []byte) (n int, err error) {
// decrypt the frame
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)
if !ok {
return n, errors.New("Failed to decrypt SecretConnection")
......@@ -298,9 +296,7 @@ func shareAuthSignature(sc io.ReadWriter, pubKey crypto.PubKey, signature crypto
if err2 != nil {
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])
if err2 != nil {
return
......
......@@ -407,17 +407,30 @@ func (client *Client) CreateBlock() {
if issleep {
time.Sleep(time.Second)
}
if !client.CheckTxsAvailable() {
height, err := client.getLastHeight()
if err != nil {
issleep = true
continue
}
if !client.CheckTxsAvailable(height) {
issleep = true
continue
}
issleep = false
client.txsAvailable <- client.GetCurrentHeight() + 1
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
......@@ -429,9 +442,9 @@ func (client *Client) StopC() <-chan struct{} {
}
// 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.CheckTxDup(txs, client.GetCurrentHeight())
txs = client.CheckTxDup(txs, height)
return len(txs) != 0
}
......@@ -495,10 +508,9 @@ func (client *Client) CommitBlock(block *types.Block) error {
// WaitBlock by height
func (client *Client) WaitBlock(height int64) bool {
retry := 0
var newHeight int64
for {
newHeight = client.GetCurrentHeight()
if newHeight >= height {
newHeight, err := client.getLastHeight()
if err == nil && newHeight >= height {
return true
}
retry++
......
......@@ -11,6 +11,7 @@ import (
"errors"
"flag"
"fmt"
"github.com/stretchr/testify/assert"
"math/rand"
"os"
"testing"
......@@ -52,12 +53,12 @@ func init() {
log.SetLogLevel("info")
}
func TestTendermintPerf(t *testing.T) {
TendermintPerf()
TendermintPerf(t)
fmt.Println("=======start clear test data!=======")
clearTestData()
}
func TendermintPerf() {
func TendermintPerf(t *testing.T) {
q, chain, s, mem, exec, cs, p2p := initEnvTendermint()
defer chain.Close()
defer mem.Close()
......@@ -75,6 +76,7 @@ func TendermintPerf() {
NormPut()
time.Sleep(time.Second)
}
CheckState(t, cs.(*Client))
AddNode()
for i := 0; i < loopCount*3; i++ {
NormPut()
......@@ -196,7 +198,7 @@ func AddNode() {
action := &ty.ValNodeAction{Value: nput, Ty: ty.ValNodeActionUpdate}
tx := &types.Transaction{Execer: []byte("valnode"), Payload: types.Encode(action), Fee: fee}
tx.To = address.ExecAddress("valnode")
tx.Nonce = r.Int63()
tx.Nonce = random.Int63()
tx.Sign(types.SECP256K1, getprivkey("CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944"))
reply, err := c.SendTransaction(context.Background(), tx)
......@@ -209,3 +211,13 @@ func AddNode() {
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 {
return Fmt(`Block{
%s %v
%s %v
%s %v
%s}#%v`,
indent, header.StringIndented(indent+" "),
// indent, b.Evidence.StringIndented(indent+" "),
indent, lastCommit.StringIndented(indent+" "),
indent, b.Hash())
}
......
......@@ -100,9 +100,8 @@ func (hvs *HeightVoteSet) SetRound(round int) {
func (hvs *HeightVoteSet) addRound(round int) {
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)
precommits := NewVoteSet(hvs.chainID, hvs.height, round, VoteTypePrecommit, hvs.valSet)
hvs.roundVoteSets[round] = RoundVoteSet{
......@@ -179,7 +178,7 @@ func (hvs *HeightVoteSet) getVoteSet(round int, voteType byte) *VoteSet {
case VoteTypePrecommit:
return rvs.Precommits
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 {
// Hash ...
func (vote *Vote) Hash() []byte {
if vote == nil {
//votelog.Error("vote hash is nil")
return nil
}
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