mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
Merge branch 'master' of github.com:thanhson1085/tomochain
This commit is contained in:
commit
401f747997
3 changed files with 66 additions and 51 deletions
|
|
@ -18,9 +18,7 @@ package posv
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
// "strings"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
// "encoding/hex"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
@ -32,7 +30,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/accounts"
|
"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"
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
"github.com/ethereum/go-ethereum/consensus"
|
"github.com/ethereum/go-ethereum/consensus"
|
||||||
|
|
@ -862,47 +859,12 @@ func (c *Posv) Finalize(chain consensus.ChainReader, header *types.Header, state
|
||||||
number := header.Number.Uint64()
|
number := header.Number.Uint64()
|
||||||
rCheckpoint := chain.Config().Posv.RewardCheckpoint
|
rCheckpoint := chain.Config().Posv.RewardCheckpoint
|
||||||
|
|
||||||
for _, tx := range txs {
|
start := time.Now()
|
||||||
if tx.IsSigningTransaction() {
|
_ = c.cacheData(txs, receipts)
|
||||||
blkHash := common.BytesToHash(tx.Data()[len(tx.Data())-32:])
|
fmt.Println("Time processed txs", len(txs), "time", common.PrettyDuration(time.Since(start)))
|
||||||
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")
|
|
||||||
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()
|
|
||||||
|
|
||||||
c.Votes.Remove(vote)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.HookReward != nil && number%rCheckpoint == 0 {
|
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())
|
fmt.Println("EnableCache true c.BlockSigners.Len()", c.BlockSigners.Len())
|
||||||
c.EnableCache = true
|
c.EnableCache = true
|
||||||
}
|
}
|
||||||
|
|
@ -1077,6 +1039,52 @@ func (c *Posv) GetMasternodesFromCheckpointHeader(preCheckpointHeader *types.Hea
|
||||||
return masternodes
|
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.
|
// Extract validators from byte array.
|
||||||
func RemovePenaltiesFromBlock(chain consensus.ChainReader, masternodes []common.Address, epochNumber uint64) []common.Address {
|
func RemovePenaltiesFromBlock(chain consensus.ChainReader, masternodes []common.Address, epochNumber uint64) []common.Address {
|
||||||
if epochNumber <= 0 {
|
if epochNumber <= 0 {
|
||||||
|
|
|
||||||
|
|
@ -212,7 +212,6 @@ func GetSignersFromContract(c *posv.Posv, addrBlockSigner common.Address, client
|
||||||
} else {
|
} else {
|
||||||
return caddrs.([]common.Address), nil
|
return caddrs.([]common.Address), nil
|
||||||
}
|
}
|
||||||
return nil, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get random from randomize contract.
|
// Get random from randomize contract.
|
||||||
|
|
@ -350,7 +349,7 @@ func GetRewardForCheckpoint(c *posv.Posv, chain consensus.ChainReader, blockSign
|
||||||
} else {
|
} else {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
squeue := make(chan []common.Address, 1)
|
squeue := make(chan []common.Address, 1)
|
||||||
wg.Add(900)
|
wg.Add(int(rCheckpoint))
|
||||||
|
|
||||||
for i := startBlockNumber; i <= endBlockNumber; i++ {
|
for i := startBlockNumber; i <= endBlockNumber; i++ {
|
||||||
go func(i uint64) {
|
go func(i uint64) {
|
||||||
|
|
@ -484,6 +483,7 @@ func GetRewardBalancesRate(c *posv.Posv, foudationWalletAddr common.Address, mas
|
||||||
vote.Masternode = masterAddr
|
vote.Masternode = masterAddr
|
||||||
vote.Voter = voteAddr
|
vote.Voter = voteAddr
|
||||||
|
|
||||||
|
if c != nil {
|
||||||
if vCap, ok := c.Votes.Get(vote); ok {
|
if vCap, ok := c.Votes.Get(vote); ok {
|
||||||
voterCap = vCap.(*big.Int)
|
voterCap = vCap.(*big.Int)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -491,8 +491,15 @@ func GetRewardBalancesRate(c *posv.Posv, foudationWalletAddr common.Address, mas
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Crit("Fail to get vote capacity", "error", err)
|
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)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
totalCap.Add(totalCap, voterCap)
|
totalCap.Add(totalCap, voterCap)
|
||||||
voterCaps[voteAddr] = voterCap
|
voterCaps[voteAddr] = voterCap
|
||||||
|
|
|
||||||
|
|
@ -144,7 +144,7 @@ func TestRewardBalance(t *testing.T) {
|
||||||
|
|
||||||
foundationAddr := common.HexToAddress(common.FoudationAddr)
|
foundationAddr := common.HexToAddress(common.FoudationAddr)
|
||||||
totalReward := new(big.Int).SetInt64(15 * 1000)
|
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 {
|
if err != nil {
|
||||||
t.Error("Fail to get reward balances rate.", err)
|
t.Error("Fail to get reward balances rate.", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue