From c2414371caa8cc49ef50f7f67cc2918dd82d6751 Mon Sep 17 00:00:00 2001 From: parmarrushabh Date: Sat, 24 Nov 2018 17:40:36 +0530 Subject: [PATCH] fix error read reward --- cmd/utils/flags.go | 10 ++++------ core/blockchain.go | 11 +++-------- internal/ethapi/api.go | 9 +-------- les/api_backend.go | 28 ++++++++++++++++++++++------ 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index dfdc170977..1385ae1e46 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -74,10 +74,8 @@ SUBCOMMANDS: func init() { cli.AppHelpTemplate = `{{.Name}} {{if .Flags}}[global options] {{end}}command{{if .Flags}} [command options]{{end}} [arguments...] - VERSION: {{.Version}} - COMMANDS: {{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}} {{end}}{{if .Flags}} @@ -1087,9 +1085,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(), "XDC", "rewards") - if _, err := os.Stat(cfg.StoreRewardFolder); os.IsNotExist(err) { - os.Mkdir(cfg.StoreRewardFolder, os.ModePerm) + common.StoreRewardFolder = filepath.Join(stack.DataDir(), "XDC", "rewards") + if _, err := os.Stat(common.StoreRewardFolder); os.IsNotExist(err) { + os.Mkdir(common.StoreRewardFolder, os.ModePerm) } } // Override any default configs for hard coded networks. @@ -1315,4 +1313,4 @@ func MigrateFlags(action func(ctx *cli.Context) error) func(*cli.Context) error } return action(ctx) } -} +} \ No newline at end of file diff --git a/core/blockchain.go b/core/blockchain.go index 9b6be16ccc..81be81a666 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -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,7 @@ 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,11 +1428,9 @@ 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 -} +} // insertStats tracks and reports on block insertion. type insertStats struct { diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 35fd1bc813..379bc5f215 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -493,14 +493,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().(*XDPoS.XDPoS); 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 7bbe47abfa..c90e287920 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/XDPoS" + "encoding/json" + "io/ioutil" "math/big" + "path/filepath" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/common" @@ -202,11 +204,25 @@ 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().(*XDPoS.XDPoS); 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{}) -} \ No newline at end of file +} \ No newline at end of file