Skip to content

Commit

Permalink
Merge pull request #1994 from akibax/main
Browse files Browse the repository at this point in the history
Fixed the number of honor node connections and honor candidate node
  • Loading branch information
scottafk authored Jun 21, 2022
2 parents 664165b + 5671bff commit b35eaac
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/daemons/block_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func BlockGenerator(ctx context.Context, d *daemon) error {
syspar.SetRunModel(consts.CandidateNodeMode)
return BlockGeneratorCandidate(ctx, d)
}
syspar.SetRunModel(consts.HonorNodeMode)
d.sleepTime = time.Second
if node.IsNodePaused() {
return nil
Expand Down
11 changes: 6 additions & 5 deletions packages/daemons/block_generator_candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ func BlockGeneratorCandidate(ctx context.Context, d *daemon) error {
}
st := time.Now()

lastBlockInterval := time.Unix(prevBlock.Time, 0)
timeDifference := st.Sub(lastBlockInterval)
if timeDifference <= 4*time.Second {
time.Sleep(4*time.Second - timeDifference)
}
dtx := DelayedTx{
privateKey: NodePrivateKey,
publicKey: NodePublicKey,
Expand All @@ -89,6 +84,12 @@ func BlockGeneratorCandidate(ctx context.Context, d *daemon) error {
if len(trs) == 0 {
return nil
}
lastBlockInterval := time.Unix(prevBlock.Time, 0)
timeDifference := st.Sub(lastBlockInterval)
if timeDifference <= syspar.GetMaxBlockTimeDuration() {
time.Sleep(syspar.GetMaxBlockTimeDuration() - timeDifference)
}

candidateNodesByte, _ := json.Marshal(candidateNodes)
header := &types.BlockHeader{
BlockId: prevBlock.BlockID + 1,
Expand Down
21 changes: 17 additions & 4 deletions packages/daemons/candidate_node_votings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/hex"
"encoding/json"
"github.com/IBAX-io/go-ibax/packages/utils"
"sync"
"sync/atomic"
"time"
Expand All @@ -20,7 +21,7 @@ import (

type VotingRes struct {
VoteMsgInfo network.VoteMsg `json:"voteMsgInfo"`
Err error `json:"err"`
Err string `json:"err"`
}

type VotingTotal struct {
Expand All @@ -40,7 +41,7 @@ func ToUpdateMachineStatus(tcpAddress string, ch chan map[string]VotingRes, logg
voteMsg.TcpAddress = tcpAddress
voteMsg.Time = time.Now().UnixMilli()
ch <- map[string]VotingRes{
tcpAddress: {*voteMsg, err},
tcpAddress: {*voteMsg, "tcp connection error"},
}
return err
}
Expand All @@ -51,7 +52,7 @@ func ToUpdateMachineStatus(tcpAddress string, ch chan map[string]VotingRes, logg
return err
}
ch <- map[string]VotingRes{
tcpAddress: {*voteMsg, nil},
tcpAddress: {*voteMsg, ""},
}
return nil
}
Expand Down Expand Up @@ -89,6 +90,18 @@ func CandidateNodeVoting(ctx context.Context, d *daemon) error {
if len(candidateNodes) == 0 {
return nil
}
_, NodePublicKey := utils.GetNodeKeys()
NodePublicKey = "04" + NodePublicKey
var isHonorNode bool
for _, node := range candidateNodes {
if NodePublicKey == node.NodePubKey {
isHonorNode = true
break
}
}
if !isHonorNode {
return nil
}
ch := make(chan map[string]VotingRes, len(candidateNodes))
var wg sync.WaitGroup
for _, node := range candidateNodes {
Expand All @@ -107,7 +120,7 @@ func CandidateNodeVoting(ctx context.Context, d *daemon) error {
break
}
for tcpAddress, res := range serverVotingInfo {
if res.Err != nil {
if res.Err != "" {
nodeConnMap[tcpAddress] = res
continue
}
Expand Down
2 changes: 1 addition & 1 deletion packages/network/tcpserver/candidate_node_voting.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func CandidateNodeVoting(r *network.CandidateNodeVotingRequest) (*network.Candid

type VotingRes struct {
VoteMsgInfo network.VoteMsg `json:"voteMsgInfo"`
Err error `json:"err"`
Err string `json:"err"`
}
type VotingTotal struct {
Data map[string]VotingRes `json:"data"`
Expand Down
4 changes: 2 additions & 2 deletions packages/storage/sqldb/candidate_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func GetCandidateNode(numberOfNodes int) ([]CandidateNode, error) {
if err != nil {
return nil, err
}
err = GetDB(nil).Where("deleted = ? and earnest_total >= ?", 0, pledgeAmount).Order("date_reply,reply_count desc").Limit(numberOfNodes).Find(&candidateNodes).Error
err = GetDB(nil).Where("deleted = ? and earnest_total >= ?", 0, pledgeAmount).Order("referendum_total desc ,date_reply,reply_count desc").Limit(numberOfNodes).Find(&candidateNodes).Error
if err != nil {
return nil, err
}
Expand All @@ -61,7 +61,7 @@ func (c *CandidateNode) UpdateCandidateNodeInfo() error {
if err != nil {
return err
}
err = GetDB(nil).Model(&c).Where("tcp_address = ? and deleted = ? and earnest_total >= ?", c.TcpAddress, 0, pledgeAmount).Omit("candidate_nodes").Updates(CandidateNode{ReplyCount: c.ReplyCount, DateReply: time.Now().UnixMilli(), CandidateNodes: c.CandidateNodes}).Error
err = GetDB(nil).Model(&c).Where("tcp_address = ? and deleted = ? and earnest_total >= ?", c.TcpAddress, 0, pledgeAmount).Updates(CandidateNode{ReplyCount: c.ReplyCount, DateReply: time.Now().UnixMilli(), CandidateNodes: c.CandidateNodes}).Error
if err != nil {
return err
}
Expand Down

0 comments on commit b35eaac

Please sign in to comment.