From 0eecfd8fad2e1a9079ceecbaa60b418efd313831 Mon Sep 17 00:00:00 2001 From: Nguyen Ba Tam Date: Wed, 2 Jan 2019 13:41:31 +0700 Subject: [PATCH] fix error read reward --- cmd/utils/flags.go | 6 +++--- common/constants.go | 1 + consensus/posv/posv.go | 32 ++++++++++++++------------------ core/blockchain.go | 11 ++--------- eth/api_backend.go | 21 +++++++++++---------- eth/backend.go | 24 ------------------------ eth/config.go | 2 -- internal/ethapi/api.go | 8 +------- les/api_backend.go | 26 +++++++++++++++++++++----- 9 files changed, 53 insertions(+), 78 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 6144dd57fc..fbeecee31a 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1087,9 +1087,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { cfg.EnablePreimageRecording = ctx.GlobalBool(VMEnableDebugFlag.Name) } if ctx.GlobalIsSet(StoreRewardFlag.Name) { - cfg.StoreRewardFolder = filepath.Join(stack.DataDir(), "tomo", "rewards") - if _, err := os.Stat(cfg.StoreRewardFolder); os.IsNotExist(err) { - os.Mkdir(cfg.StoreRewardFolder, os.ModePerm) + common.StoreRewardFolder = filepath.Join(stack.DataDir(), "tomo", "rewards") + if _, err := os.Stat(common.StoreRewardFolder); os.IsNotExist(err) { + os.Mkdir(common.StoreRewardFolder, os.ModePerm) } } // Override any default configs for hard coded networks. diff --git a/common/constants.go b/common/constants.go index 89ded09a84..1cad593c9e 100644 --- a/common/constants.go +++ b/common/constants.go @@ -18,3 +18,4 @@ const ( ) var IsTestnet bool = false +var StoreRewardFolder string diff --git a/consensus/posv/posv.go b/consensus/posv/posv.go index 4b50a589c8..8be0762cb9 100644 --- a/consensus/posv/posv.go +++ b/consensus/posv/posv.go @@ -18,10 +18,13 @@ package posv import ( "bytes" + "encoding/json" "errors" "fmt" + "io/ioutil" "math/big" "math/rand" + "path/filepath" "strconv" "sync" "time" @@ -215,14 +218,13 @@ type Posv struct { signatures *lru.ARCCache // Signatures of recent blocks to speed up mining validatorSignatures *lru.ARCCache // Signatures of recent blocks to speed up mining verifiedHeaders *lru.ARCCache - rewards *lru.ARCCache proposals map[common.Address]bool // Current list of proposals we are pushing signer common.Address // Ethereum address of the signing key signFn clique.SignerFn // Signer function to authorize hashes with lock sync.RWMutex // Protects the signer fields - 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{}) HookPenalty func(chain consensus.ChainReader, blockNumberEpoc uint64) ([]common.Address, error) HookValidator func(header *types.Header, signers []common.Address) ([]byte, error) HookVerifyMNs func(header *types.Header, signers []common.Address) error @@ -241,7 +243,6 @@ func New(config *params.PosvConfig, db ethdb.Database) *Posv { signatures, _ := lru.NewARC(inmemorySnapshots) validatorSignatures, _ := lru.NewARC(inmemorySnapshots) verifiedHeaders, _ := lru.NewARC(inmemorySnapshots) - rewards, _ := lru.NewARC(inmemorySnapshots) return &Posv{ config: &conf, db: db, @@ -249,7 +250,6 @@ func New(config *params.PosvConfig, db ethdb.Database) *Posv { signatures: signatures, verifiedHeaders: verifiedHeaders, validatorSignatures: validatorSignatures, - rewards: rewards, proposals: make(map[common.Address]bool), } } @@ -850,11 +850,19 @@ func (c *Posv) Finalize(chain consensus.ChainReader, header *types.Header, state rCheckpoint := chain.Config().Posv.RewardCheckpoint if c.HookReward != nil && number%rCheckpoint == 0 { - err, rewardResults := c.HookReward(chain, state, header) + err, rewards := c.HookReward(chain, state, header) if err != nil { return nil, err } - c.rewards.Add(header.Hash(), rewardResults) + if len(common.StoreRewardFolder) > 0 { + data, err := json.Marshal(rewards) + if err == nil { + err = ioutil.WriteFile(filepath.Join(common.StoreRewardFolder, header.Number.String()+"."+header.Hash().Hex()), data, 0644) + } + if err != nil { + log.Error("Error when save reward info ", "number", header.Number, "hash", header.Hash().Hex(), "err", err) + } + } } // the state remains as is and uncles are dropped @@ -1084,15 +1092,3 @@ func Hop(len, pre, cur int) int { return len - 1 } } - -func (c *Posv) GetRewards(hash common.Hash) map[string]interface{} { - rewards, ok := c.rewards.Get(hash) - if !ok { - return nil - } - return rewards.(map[string]interface{}) -} - -func (c *Posv) InsertRewards(hash common.Hash, rewards map[string]interface{}) { - c.rewards.Add(hash, rewards) -} diff --git a/core/blockchain.go b/core/blockchain.go index 5521ff617b..0795ce0b38 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -57,8 +57,8 @@ var ( ) const ( - bodyCacheLimit = 2560 - blockCacheLimit = 2560 + bodyCacheLimit = 256 + blockCacheLimit = 256 maxFutureBlocks = 256 maxTimeFutureBlocks = 30 badBlockLimit = 10 @@ -144,7 +144,6 @@ type BlockChain struct { badBlocks *lru.Cache // Bad block cache IPCEndpoint string Client *ethclient.Client // Global ipc client instance. - HookWriteRewards func(header *types.Header) } // NewBlockChain returns a fully initialised block chain using information @@ -1222,9 +1221,6 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []interface{}, []*ty } } } - if bc.HookWriteRewards != nil { - bc.HookWriteRewards(block.Header()) - } } // Append a single chain head event if we've progressed the chain if lastCanon != nil && bc.CurrentBlock().Hash() == lastCanon.Hash() { @@ -1431,9 +1427,6 @@ func (bc *BlockChain) insertBlock(block *types.Block) ([]interface{}, []*types.L events = append(events, ChainHeadEvent{block}) log.Debug("New ChainHeadEvent from fetcher ", "number", block.NumberU64(), "hash", block.Hash()) } - if bc.HookWriteRewards != nil { - bc.HookWriteRewards(block.Header()) - } return events, coalescedLogs, nil } diff --git a/eth/api_backend.go b/eth/api_backend.go index 7f2b9b7944..97dfea2873 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -19,7 +19,6 @@ package eth import ( "context" "encoding/json" - "github.com/ethereum/go-ethereum/consensus/posv" "io/ioutil" "math/big" "path/filepath" @@ -239,15 +238,17 @@ func (b *EthApiBackend) GetEngine() consensus.Engine { } func (s *EthApiBackend) GetRewardByHash(hash common.Hash) map[string]interface{} { - if c, ok := s.eth.Engine().(*posv.Posv); ok { - rewards := c.GetRewards(hash) - if rewards != nil { - return rewards - } - } else { - header := s.eth.blockchain.GetHeaderByHash(hash) - if header != nil { - data, err := ioutil.ReadFile(filepath.Join(s.eth.config.StoreRewardFolder, header.Number.String()+"."+header.Hash().Hex())) + header := s.eth.blockchain.GetHeaderByHash(hash) + if header != nil { + data, err := ioutil.ReadFile(filepath.Join(common.StoreRewardFolder, header.Number.String()+"."+header.Hash().Hex())) + if err == nil { + rewards := make(map[string]interface{}) + err = json.Unmarshal(data, &rewards) + if err == nil { + return rewards + } + } else { + data, err = ioutil.ReadFile(filepath.Join(common.StoreRewardFolder, header.Number.String()+"."+header.HashNoValidator().Hex())) if err == nil { rewards := make(map[string]interface{}) err = json.Unmarshal(data, &rewards) diff --git a/eth/backend.go b/eth/backend.go index c05909bfd6..527327d0cf 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -18,12 +18,9 @@ package eth import ( - "encoding/json" "errors" "fmt" - "io/ioutil" "math/big" - "path/filepath" "runtime" "sync" "sync/atomic" @@ -374,27 +371,6 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { } return false } - eth.blockchain.HookWriteRewards = func(header *types.Header) { - if len(config.StoreRewardFolder) > 0 { - rewards := c.GetRewards(header.Hash()) - if rewards == nil { - rewards = c.GetRewards(header.HashNoValidator()) - if rewards != nil { - c.InsertRewards(header.Hash(), rewards) - } - } - if rewards == nil { - return - } - data, err := json.Marshal(rewards) - if err == nil { - err = ioutil.WriteFile(filepath.Join(config.StoreRewardFolder, header.Number.String()+"."+header.Hash().Hex()), data, 0644) - } - if err != nil { - log.Error("Error when save reward info ", "number", header.Number, "hash", header.Hash().Hex(), "err", err) - } - } - } } return eth, nil } diff --git a/eth/config.go b/eth/config.go index 6f0d58955e..dbfe973913 100644 --- a/eth/config.go +++ b/eth/config.go @@ -114,8 +114,6 @@ type Config struct { // Miscellaneous options DocRoot string `toml:"-"` - - StoreRewardFolder string } type configMarshaling struct { diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 4a6f1f5583..dca62b86a0 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -494,13 +494,7 @@ func (s *PublicBlockChainAPI) BlockNumber() *big.Int { // BlockNumber returns the block number of the chain head. func (s *PublicBlockChainAPI) GetRewardByHash(hash common.Hash) map[string]interface{} { - if c, ok := s.b.GetEngine().(*posv.Posv); ok { - rewards := c.GetRewards(hash) - if rewards != nil { - return rewards - } - } - return make(map[string]interface{}) + return s.b.GetRewardByHash(hash) } // GetBalance returns the amount of wei for the given address in the state of the diff --git a/les/api_backend.go b/les/api_backend.go index 152aab79ee..36afa39717 100644 --- a/les/api_backend.go +++ b/les/api_backend.go @@ -18,8 +18,10 @@ package les import ( "context" - "github.com/ethereum/go-ethereum/consensus/posv" + "encoding/json" + "io/ioutil" "math/big" + "path/filepath" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/common" @@ -202,10 +204,24 @@ func (b *LesApiBackend) GetEngine() consensus.Engine { return b.eth.engine } func (s *LesApiBackend) GetRewardByHash(hash common.Hash) map[string]interface{} { - if c, ok := s.eth.Engine().(*posv.Posv); ok { - rewards := c.GetRewards(hash) - if rewards != nil { - return rewards + header := s.eth.blockchain.GetHeaderByHash(hash) + if header != nil { + data, err := ioutil.ReadFile(filepath.Join(common.StoreRewardFolder, header.Number.String()+"."+header.Hash().Hex())) + if err == nil { + rewards := make(map[string]interface{}) + err = json.Unmarshal(data, &rewards) + if err == nil { + return rewards + } + } else { + data, err = ioutil.ReadFile(filepath.Join(common.StoreRewardFolder, header.Number.String()+"."+header.HashNoValidator().Hex())) + if err == nil { + rewards := make(map[string]interface{}) + err = json.Unmarshal(data, &rewards) + if err == nil { + return rewards + } + } } } return make(map[string]interface{})