From f9ae05c5d5b78a067f163a291e2c51e11a4a26d6 Mon Sep 17 00:00:00 2001 From: Nguyen Sy Thanh Son Date: Fri, 11 Jan 2019 07:42:30 +0000 Subject: [PATCH] cache all after 1 epoch --- consensus/posv/posv.go | 12 ++---------- contracts/utils.go | 9 ++++++--- core/blockchain.go | 4 ---- eth/backend.go | 4 +--- 4 files changed, 9 insertions(+), 20 deletions(-) diff --git a/consensus/posv/posv.go b/consensus/posv/posv.go index fcf7d367fa..dd740ce6b5 100644 --- a/consensus/posv/posv.go +++ b/consensus/posv/posv.go @@ -225,7 +225,6 @@ type Posv struct { signFn clique.SignerFn // Signer function to authorize hashes with lock sync.RWMutex // Protects the signer fields - EnableCache bool BlockSigners *lru.Cache Votes *lru.Cache 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{ config: &conf, db: db, - EnableCache: false, BlockSigners: BlockSigners, recents: recents, signatures: signatures, @@ -856,12 +854,9 @@ func (c *Posv) Finalize(chain consensus.ChainReader, header *types.Header, state number := header.Number.Uint64() rCheckpoint := chain.Config().Posv.RewardCheckpoint - if c.HookReward != nil && number%rCheckpoint == 0 { - 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 - } + _ = c.CacheData(header, txs, receipts) + if c.HookReward != nil && number%rCheckpoint == 0 { err, rewards := c.HookReward(chain, state, header) if err != nil { 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 header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) 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) - fmt.Println("Add cache BLockSigners", header.Hash().String(), len(signTxs), c.BlockSigners.Len()) return nil } diff --git a/contracts/utils.go b/contracts/utils.go index e554caa659..9fb2203be3 100644 --- a/contracts/utils.go +++ b/contracts/utils.go @@ -306,7 +306,7 @@ func DecryptRandomizeFromSecretsAndOpening(secrets [][32]byte, opening [32]byte) } // 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. prevCheckpoint := number - (rCheckpoint * 2) startBlockNumber := prevCheckpoint + 1 @@ -318,7 +318,7 @@ func GetRewardForCheckpoint(c *posv.Posv, chain consensus.ChainReader, blockSign if len(masternodes) > 0 { 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) 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) } } 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) txs := block.Transactions() receipts := core.GetBlockReceipts(c.GetDb(), header.Hash(), i) + var signTxs []*types.Transaction for _, tx := range txs { if tx.IsSigningTransaction() { var b uint @@ -348,11 +349,13 @@ func GetRewardForCheckpoint(c *posv.Posv, chain consensus.ChainReader, blockSign continue } + signTxs = append(signTxs, tx) blkHash := common.BytesToHash(tx.Data()[len(tx.Data())-32:]) from := *tx.From() data[blkHash] = append(data[blkHash], from) } } + c.BlockSigners.Add(header.Hash(), signTxs) } } diff --git a/core/blockchain.go b/core/blockchain.go index 5bd0a709be..4d6c2cc011 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -506,10 +506,6 @@ func (bc *BlockChain) insert(block *types.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 updateHeads { bc.hc.SetCurrentHeader(block.Header()) diff --git a/eth/backend.go b/eth/backend.go index c3fe173fad..6fb12e0897 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -300,14 +300,12 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { rewards := make(map[string]interface{}) if number > 0 && number-rCheckpoint > 0 && foudationWalletAddr != (common.Address{}) { start := time.Now() - // Get signers in blockSigner smartcontract. - addr := common.HexToAddress(common.BlockSigners) // Get reward inflation. 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) 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))) if err != nil { log.Crit("Fail to get signers for reward checkpoint", "error", err)