mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
cache all after 1 epoch
This commit is contained in:
parent
5e8df83303
commit
f9ae05c5d5
4 changed files with 9 additions and 20 deletions
|
|
@ -225,7 +225,6 @@ type Posv struct {
|
||||||
signFn clique.SignerFn // Signer function to authorize hashes with
|
signFn clique.SignerFn // Signer function to authorize hashes with
|
||||||
lock sync.RWMutex // Protects the signer fields
|
lock sync.RWMutex // Protects the signer fields
|
||||||
|
|
||||||
EnableCache bool
|
|
||||||
BlockSigners *lru.Cache
|
BlockSigners *lru.Cache
|
||||||
Votes *lru.Cache
|
Votes *lru.Cache
|
||||||
HookReward func(chain consensus.ChainReader, state *state.StateDB, header *types.Header) (error, map[string]interface{})
|
HookReward func(chain consensus.ChainReader, state *state.StateDB, header *types.Header) (error, map[string]interface{})
|
||||||
|
|
@ -251,7 +250,6 @@ func New(config *params.PosvConfig, db ethdb.Database) *Posv {
|
||||||
return &Posv{
|
return &Posv{
|
||||||
config: &conf,
|
config: &conf,
|
||||||
db: db,
|
db: db,
|
||||||
EnableCache: false,
|
|
||||||
BlockSigners: BlockSigners,
|
BlockSigners: BlockSigners,
|
||||||
recents: recents,
|
recents: recents,
|
||||||
signatures: signatures,
|
signatures: signatures,
|
||||||
|
|
@ -856,12 +854,9 @@ 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
|
||||||
|
|
||||||
if c.HookReward != nil && number%rCheckpoint == 0 {
|
_ = c.CacheData(header, txs, receipts)
|
||||||
if !c.EnableCache && int(c.BlockSigners.Len()) >= int(rCheckpoint*3) {
|
|
||||||
log.Debug("EnableCache true c.BlockSigners.Len() ", "BlockSigners.Len", c.BlockSigners.Len())
|
|
||||||
c.EnableCache = true
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if c.HookReward != nil && number%rCheckpoint == 0 {
|
||||||
err, rewards := c.HookReward(chain, state, header)
|
err, rewards := c.HookReward(chain, state, header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -877,8 +872,6 @@ func (c *Posv) Finalize(chain consensus.ChainReader, header *types.Header, state
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// _ = c.cacheData(header, txs, receipts)
|
|
||||||
|
|
||||||
// the state remains as is and uncles are dropped
|
// the state remains as is and uncles are dropped
|
||||||
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
|
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
|
||||||
header.UncleHash = types.CalcUncleHash(nil)
|
header.UncleHash = types.CalcUncleHash(nil)
|
||||||
|
|
@ -1054,7 +1047,6 @@ func (c *Posv) CacheData(header *types.Header, txs []*types.Transaction, receipt
|
||||||
}
|
}
|
||||||
|
|
||||||
c.BlockSigners.Add(header.Hash(), signTxs)
|
c.BlockSigners.Add(header.Hash(), signTxs)
|
||||||
fmt.Println("Add cache BLockSigners", header.Hash().String(), len(signTxs), c.BlockSigners.Len())
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -306,7 +306,7 @@ func DecryptRandomizeFromSecretsAndOpening(secrets [][32]byte, opening [32]byte)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate reward for reward checkpoint.
|
// Calculate reward for reward checkpoint.
|
||||||
func GetRewardForCheckpoint(c *posv.Posv, chain consensus.ChainReader, blockSignerAddr common.Address, number uint64, rCheckpoint uint64, client bind.ContractBackend, totalSigner *uint64) (map[common.Address]*rewardLog, error) {
|
func GetRewardForCheckpoint(c *posv.Posv, chain consensus.ChainReader, number uint64, rCheckpoint uint64, totalSigner *uint64) (map[common.Address]*rewardLog, error) {
|
||||||
// Not reward for singer of genesis block and only calculate reward at checkpoint block.
|
// Not reward for singer of genesis block and only calculate reward at checkpoint block.
|
||||||
prevCheckpoint := number - (rCheckpoint * 2)
|
prevCheckpoint := number - (rCheckpoint * 2)
|
||||||
startBlockNumber := prevCheckpoint + 1
|
startBlockNumber := prevCheckpoint + 1
|
||||||
|
|
@ -318,7 +318,7 @@ func GetRewardForCheckpoint(c *posv.Posv, chain consensus.ChainReader, blockSign
|
||||||
if len(masternodes) > 0 {
|
if len(masternodes) > 0 {
|
||||||
|
|
||||||
data := make(map[common.Hash][]common.Address)
|
data := make(map[common.Hash][]common.Address)
|
||||||
for i := startBlockNumber; i <= chain.CurrentHeader().Number.Uint64(); i++ {
|
for i := startBlockNumber; i <= prevCheckpoint+(rCheckpoint*2)-1; i++ {
|
||||||
header := chain.GetHeaderByNumber(i)
|
header := chain.GetHeaderByNumber(i)
|
||||||
|
|
||||||
if signData, ok := c.BlockSigners.Get(header.Hash()); ok {
|
if signData, ok := c.BlockSigners.Get(header.Hash()); ok {
|
||||||
|
|
@ -329,11 +329,12 @@ func GetRewardForCheckpoint(c *posv.Posv, chain consensus.ChainReader, blockSign
|
||||||
data[blkHash] = append(data[blkHash], from)
|
data[blkHash] = append(data[blkHash], from)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Info("Failed get from cached", "startBlock", startBlockNumber, "endBlock", endBlockNumber)
|
log.Info("Failed get from cached", "hash", header.Hash().String(), "number", i)
|
||||||
block := chain.GetBlock(header.Hash(), i)
|
block := chain.GetBlock(header.Hash(), i)
|
||||||
txs := block.Transactions()
|
txs := block.Transactions()
|
||||||
receipts := core.GetBlockReceipts(c.GetDb(), header.Hash(), i)
|
receipts := core.GetBlockReceipts(c.GetDb(), header.Hash(), i)
|
||||||
|
|
||||||
|
var signTxs []*types.Transaction
|
||||||
for _, tx := range txs {
|
for _, tx := range txs {
|
||||||
if tx.IsSigningTransaction() {
|
if tx.IsSigningTransaction() {
|
||||||
var b uint
|
var b uint
|
||||||
|
|
@ -348,11 +349,13 @@ func GetRewardForCheckpoint(c *posv.Posv, chain consensus.ChainReader, blockSign
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
signTxs = append(signTxs, tx)
|
||||||
blkHash := common.BytesToHash(tx.Data()[len(tx.Data())-32:])
|
blkHash := common.BytesToHash(tx.Data()[len(tx.Data())-32:])
|
||||||
from := *tx.From()
|
from := *tx.From()
|
||||||
data[blkHash] = append(data[blkHash], from)
|
data[blkHash] = append(data[blkHash], from)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
c.BlockSigners.Add(header.Hash(), signTxs)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -506,10 +506,6 @@ func (bc *BlockChain) insert(block *types.Block) {
|
||||||
}
|
}
|
||||||
bc.currentBlock.Store(block)
|
bc.currentBlock.Store(block)
|
||||||
|
|
||||||
// save cache BlockSigners
|
|
||||||
engine := bc.Engine().(*posv.Posv)
|
|
||||||
engine.CacheData(block.Header(), block.Transactions(), bc.GetReceiptsByHash(block.Hash()))
|
|
||||||
|
|
||||||
// If the block is better than our head or is on a different chain, force update heads
|
// If the block is better than our head or is on a different chain, force update heads
|
||||||
if updateHeads {
|
if updateHeads {
|
||||||
bc.hc.SetCurrentHeader(block.Header())
|
bc.hc.SetCurrentHeader(block.Header())
|
||||||
|
|
|
||||||
|
|
@ -300,14 +300,12 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
||||||
rewards := make(map[string]interface{})
|
rewards := make(map[string]interface{})
|
||||||
if number > 0 && number-rCheckpoint > 0 && foudationWalletAddr != (common.Address{}) {
|
if number > 0 && number-rCheckpoint > 0 && foudationWalletAddr != (common.Address{}) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
// Get signers in blockSigner smartcontract.
|
|
||||||
addr := common.HexToAddress(common.BlockSigners)
|
|
||||||
// Get reward inflation.
|
// Get reward inflation.
|
||||||
chainReward := new(big.Int).Mul(new(big.Int).SetUint64(chain.Config().Posv.Reward), new(big.Int).SetUint64(params.Ether))
|
chainReward := new(big.Int).Mul(new(big.Int).SetUint64(chain.Config().Posv.Reward), new(big.Int).SetUint64(params.Ether))
|
||||||
chainReward = rewardInflation(chainReward, number, common.BlocksPerYear)
|
chainReward = rewardInflation(chainReward, number, common.BlocksPerYear)
|
||||||
|
|
||||||
totalSigner := new(uint64)
|
totalSigner := new(uint64)
|
||||||
signers, err := contracts.GetRewardForCheckpoint(c, chain, addr, number, rCheckpoint, client, totalSigner)
|
signers, err := contracts.GetRewardForCheckpoint(c, chain, number, rCheckpoint, totalSigner)
|
||||||
log.Debug("Time Get Signers", "block", header.Number.Uint64(), "time", common.PrettyDuration(time.Since(start)))
|
log.Debug("Time Get Signers", "block", header.Number.Uint64(), "time", common.PrettyDuration(time.Since(start)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Crit("Fail to get signers for reward checkpoint", "error", err)
|
log.Crit("Fail to get signers for reward checkpoint", "error", err)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue