From 00b49e91cf8243207c3d2ce0b3f6cde7f9dc4e0d Mon Sep 17 00:00:00 2001 From: Nguyen Sy Thanh Son Date: Mon, 7 Jan 2019 02:50:25 +0000 Subject: [PATCH 1/4] add more logs to debug --- consensus/posv/posv.go | 3 ++- contracts/utils.go | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/consensus/posv/posv.go b/consensus/posv/posv.go index 7fd611fb85..cff6bf6c9c 100644 --- a/consensus/posv/posv.go +++ b/consensus/posv/posv.go @@ -876,7 +876,7 @@ func (c *Posv) Finalize(chain consensus.ChainReader, header *types.Header, state } if b == types.ReceiptStatusFailed { - fmt.Println("Tx receipt status false") + fmt.Println("Tx receipt status false", tx.Hash().Hex()) continue } @@ -896,6 +896,7 @@ func (c *Posv) Finalize(chain consensus.ChainReader, header *types.Header, state vote.Masternode = *addr vote.Voter = *tx.From() + fmt.Println("Remove from Votes cache", vote.Masternode.String(), vote.Voter.String()) c.Votes.Remove(vote) } } diff --git a/contracts/utils.go b/contracts/utils.go index b42df94207..7a1a40389d 100644 --- a/contracts/utils.go +++ b/contracts/utils.go @@ -488,12 +488,12 @@ func GetRewardBalancesRate(c *posv.Posv, foudationWalletAddr common.Address, mas voterCap = vCap.(*big.Int) } else { voterCap, err = validator.GetVoterCap(opts, masterAddr, voteAddr) + if err != nil { + log.Crit("Fail to get vote capacity", "error", err) + } + fmt.Println("Add to Votes cache", vote.Masternode.String(), vote.Voter.String(), voterCap.String()) c.Votes.Add(vote, voterCap) } - if err != nil { - log.Error("Fail to get vote capacity", "error", err) - return nil, err - } totalCap.Add(totalCap, voterCap) voterCaps[voteAddr] = voterCap From 3aba8f4c26c7351cb435c3e5fc7c7d7246c2a895 Mon Sep 17 00:00:00 2001 From: Nguyen Sy Thanh Son Date: Mon, 7 Jan 2019 07:22:41 +0000 Subject: [PATCH 2/4] fix unit test --- consensus/posv/posv.go | 2 +- contracts/utils.go | 17 ++++++++++++----- contracts/validator/validator_test.go | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/consensus/posv/posv.go b/consensus/posv/posv.go index cff6bf6c9c..d981050555 100644 --- a/consensus/posv/posv.go +++ b/consensus/posv/posv.go @@ -903,7 +903,7 @@ func (c *Posv) Finalize(chain consensus.ChainReader, header *types.Header, state } if c.HookReward != nil && number%rCheckpoint == 0 { - if !c.EnableCache && c.BlockSigners.Len() >= 2700 { + if !c.EnableCache && uint64(c.BlockSigners.Len()) >= (rCheckpoint*3) { fmt.Println("EnableCache true c.BlockSigners.Len()", c.BlockSigners.Len()) c.EnableCache = true } diff --git a/contracts/utils.go b/contracts/utils.go index a91f6c2fc1..991f71bd6a 100644 --- a/contracts/utils.go +++ b/contracts/utils.go @@ -350,7 +350,7 @@ func GetRewardForCheckpoint(c *posv.Posv, chain consensus.ChainReader, blockSign } else { var wg sync.WaitGroup squeue := make(chan []common.Address, 1) - wg.Add(900) + wg.Add(int(rCheckpoint)) for i := startBlockNumber; i <= endBlockNumber; i++ { go func(i uint64) { @@ -484,15 +484,22 @@ func GetRewardBalancesRate(c *posv.Posv, foudationWalletAddr common.Address, mas vote.Masternode = masterAddr vote.Voter = voteAddr - if vCap, ok := c.Votes.Get(vote); ok { - voterCap = vCap.(*big.Int) + if c != nil { + if vCap, ok := c.Votes.Get(vote); ok { + voterCap = vCap.(*big.Int) + } else { + voterCap, err = validator.GetVoterCap(opts, masterAddr, voteAddr) + if err != nil { + log.Crit("Fail to get vote capacity", "error", err) + } + fmt.Println("Add to Votes cache", vote.Masternode.String(), vote.Voter.String(), voterCap.String()) + c.Votes.Add(vote, voterCap) + } } else { voterCap, err = validator.GetVoterCap(opts, masterAddr, voteAddr) if err != nil { log.Crit("Fail to get vote capacity", "error", err) } - fmt.Println("Add to Votes cache", vote.Masternode.String(), vote.Voter.String(), voterCap.String()) - c.Votes.Add(vote, voterCap) } totalCap.Add(totalCap, voterCap) diff --git a/contracts/validator/validator_test.go b/contracts/validator/validator_test.go index f3896db4b8..e5df78d42f 100644 --- a/contracts/validator/validator_test.go +++ b/contracts/validator/validator_test.go @@ -144,7 +144,7 @@ func TestRewardBalance(t *testing.T) { foundationAddr := common.HexToAddress(common.FoudationAddr) totalReward := new(big.Int).SetInt64(15 * 1000) - rewards, err := contracts.GetRewardBalancesRate(foundationAddr, acc3Addr, totalReward, baseValidator) + rewards, err := contracts.GetRewardBalancesRate(nil, foundationAddr, acc3Addr, totalReward, baseValidator) if err != nil { t.Error("Fail to get reward balances rate.", err) } From 65ff031f16062ab582a77735e99d1287d7623960 Mon Sep 17 00:00:00 2001 From: Nguyen Sy Thanh Son Date: Mon, 7 Jan 2019 07:30:47 +0000 Subject: [PATCH 3/4] fix tests unreachable code --- contracts/utils.go | 1 - 1 file changed, 1 deletion(-) diff --git a/contracts/utils.go b/contracts/utils.go index 991f71bd6a..5e353e2616 100644 --- a/contracts/utils.go +++ b/contracts/utils.go @@ -212,7 +212,6 @@ func GetSignersFromContract(c *posv.Posv, addrBlockSigner common.Address, client } else { return caddrs.([]common.Address), nil } - return nil, nil } // Get random from randomize contract. From b19694afe333222707a0d6cce4c4850fcd0bc2ad Mon Sep 17 00:00:00 2001 From: Nguyen Sy Thanh Son Date: Mon, 7 Jan 2019 10:29:43 +0000 Subject: [PATCH 4/4] concurrent process txs --- consensus/posv/posv.go | 91 +++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/consensus/posv/posv.go b/consensus/posv/posv.go index d981050555..c0cd72b3a6 100644 --- a/consensus/posv/posv.go +++ b/consensus/posv/posv.go @@ -18,9 +18,7 @@ package posv import ( "bytes" - // "strings" "encoding/json" - // "encoding/hex" "errors" "fmt" "io/ioutil" @@ -32,7 +30,6 @@ import ( "time" "github.com/ethereum/go-ethereum/accounts" - // "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/consensus" @@ -862,45 +859,9 @@ func (c *Posv) Finalize(chain consensus.ChainReader, header *types.Header, state number := header.Number.Uint64() rCheckpoint := chain.Config().Posv.RewardCheckpoint - for _, tx := range txs { - if tx.IsSigningTransaction() { - blkHash := common.BytesToHash(tx.Data()[len(tx.Data())-32:]) - from := *tx.From() - - var b uint - for _, r := range receipts { - if r.TxHash == tx.Hash() { - b = r.Status - break - } - } - - if b == types.ReceiptStatusFailed { - fmt.Println("Tx receipt status false", tx.Hash().Hex()) - continue - } - - var lAddr []common.Address - if cached, ok := c.BlockSigners.Get(blkHash); ok { - lAddr = cached.([]common.Address) - lAddr = append(lAddr, from) - } else { - lAddr = []common.Address{from} - } - c.BlockSigners.Add(blkHash, lAddr) - } else { - - b, addr := tx.IsVotingTransaction() - if b && addr != nil { - var vote common.Vote - vote.Masternode = *addr - vote.Voter = *tx.From() - - fmt.Println("Remove from Votes cache", vote.Masternode.String(), vote.Voter.String()) - c.Votes.Remove(vote) - } - } - } + start := time.Now() + _ = c.cacheData(txs, receipts) + fmt.Println("Time processed txs", len(txs), "time", common.PrettyDuration(time.Since(start))) if c.HookReward != nil && number%rCheckpoint == 0 { if !c.EnableCache && uint64(c.BlockSigners.Len()) >= (rCheckpoint*3) { @@ -1078,6 +1039,52 @@ func (c *Posv) GetMasternodesFromCheckpointHeader(preCheckpointHeader *types.Hea return masternodes } +func (c *Posv) cacheData(txs []*types.Transaction, receipts []*types.Receipt) error { + for _, tx := range txs { + go func(tx *types.Transaction) error { + if tx.IsSigningTransaction() { + blkHash := common.BytesToHash(tx.Data()[len(tx.Data())-32:]) + from := *tx.From() + + var b uint + for _, r := range receipts { + if r.TxHash == tx.Hash() { + b = r.Status + return nil + } + } + + if b == types.ReceiptStatusFailed { + fmt.Println("Tx receipt status false", tx.Hash().Hex()) + return nil + } + + var lAddr []common.Address + if cached, ok := c.BlockSigners.Get(blkHash); ok { + lAddr = cached.([]common.Address) + lAddr = append(lAddr, from) + } else { + lAddr = []common.Address{from} + } + c.BlockSigners.Add(blkHash, lAddr) + } else { + + b, addr := tx.IsVotingTransaction() + if b && addr != nil { + var vote common.Vote + vote.Masternode = *addr + vote.Voter = *tx.From() + + fmt.Println("Remove from Votes cache", vote.Masternode.String(), vote.Voter.String()) + c.Votes.Remove(vote) + } + } + return nil + }(tx) + } + return nil +} + // Extract validators from byte array. func RemovePenaltiesFromBlock(chain consensus.ChainReader, masternodes []common.Address, epochNumber uint64) []common.Address { if epochNumber <= 0 {