fix error read reward

This commit is contained in:
parmarrushabh 2018-11-24 17:40:36 +05:30
parent dfbac0dcf2
commit c2414371ca
4 changed files with 30 additions and 28 deletions

View file

@ -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)
}
}
}

View file

@ -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 {

View file

@ -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

View file

@ -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{})
}
}