fix error read reward

This commit is contained in:
Nguyen Ba Tam 2019-01-02 13:41:31 +07:00
parent 63d6467724
commit 0eecfd8fad
9 changed files with 53 additions and 78 deletions

View file

@ -1087,9 +1087,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
cfg.EnablePreimageRecording = ctx.GlobalBool(VMEnableDebugFlag.Name) cfg.EnablePreimageRecording = ctx.GlobalBool(VMEnableDebugFlag.Name)
} }
if ctx.GlobalIsSet(StoreRewardFlag.Name) { if ctx.GlobalIsSet(StoreRewardFlag.Name) {
cfg.StoreRewardFolder = filepath.Join(stack.DataDir(), "tomo", "rewards") common.StoreRewardFolder = filepath.Join(stack.DataDir(), "tomo", "rewards")
if _, err := os.Stat(cfg.StoreRewardFolder); os.IsNotExist(err) { if _, err := os.Stat(common.StoreRewardFolder); os.IsNotExist(err) {
os.Mkdir(cfg.StoreRewardFolder, os.ModePerm) os.Mkdir(common.StoreRewardFolder, os.ModePerm)
} }
} }
// Override any default configs for hard coded networks. // Override any default configs for hard coded networks.

View file

@ -18,3 +18,4 @@ const (
) )
var IsTestnet bool = false var IsTestnet bool = false
var StoreRewardFolder string

View file

@ -18,10 +18,13 @@ package posv
import ( import (
"bytes" "bytes"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"math/big" "math/big"
"math/rand" "math/rand"
"path/filepath"
"strconv" "strconv"
"sync" "sync"
"time" "time"
@ -215,7 +218,6 @@ type Posv struct {
signatures *lru.ARCCache // Signatures of recent blocks to speed up mining signatures *lru.ARCCache // Signatures of recent blocks to speed up mining
validatorSignatures *lru.ARCCache // Signatures of recent blocks to speed up mining validatorSignatures *lru.ARCCache // Signatures of recent blocks to speed up mining
verifiedHeaders *lru.ARCCache verifiedHeaders *lru.ARCCache
rewards *lru.ARCCache
proposals map[common.Address]bool // Current list of proposals we are pushing proposals map[common.Address]bool // Current list of proposals we are pushing
signer common.Address // Ethereum address of the signing key signer common.Address // Ethereum address of the signing key
@ -241,7 +243,6 @@ func New(config *params.PosvConfig, db ethdb.Database) *Posv {
signatures, _ := lru.NewARC(inmemorySnapshots) signatures, _ := lru.NewARC(inmemorySnapshots)
validatorSignatures, _ := lru.NewARC(inmemorySnapshots) validatorSignatures, _ := lru.NewARC(inmemorySnapshots)
verifiedHeaders, _ := lru.NewARC(inmemorySnapshots) verifiedHeaders, _ := lru.NewARC(inmemorySnapshots)
rewards, _ := lru.NewARC(inmemorySnapshots)
return &Posv{ return &Posv{
config: &conf, config: &conf,
db: db, db: db,
@ -249,7 +250,6 @@ func New(config *params.PosvConfig, db ethdb.Database) *Posv {
signatures: signatures, signatures: signatures,
verifiedHeaders: verifiedHeaders, verifiedHeaders: verifiedHeaders,
validatorSignatures: validatorSignatures, validatorSignatures: validatorSignatures,
rewards: rewards,
proposals: make(map[common.Address]bool), 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 rCheckpoint := chain.Config().Posv.RewardCheckpoint
if c.HookReward != nil && number%rCheckpoint == 0 { if c.HookReward != nil && number%rCheckpoint == 0 {
err, rewardResults := c.HookReward(chain, state, header) err, rewards := c.HookReward(chain, state, header)
if err != nil { if err != nil {
return nil, err 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 // the state remains as is and uncles are dropped
@ -1084,15 +1092,3 @@ func Hop(len, pre, cur int) int {
return len - 1 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)
}

View file

@ -57,8 +57,8 @@ var (
) )
const ( const (
bodyCacheLimit = 2560 bodyCacheLimit = 256
blockCacheLimit = 2560 blockCacheLimit = 256
maxFutureBlocks = 256 maxFutureBlocks = 256
maxTimeFutureBlocks = 30 maxTimeFutureBlocks = 30
badBlockLimit = 10 badBlockLimit = 10
@ -144,7 +144,6 @@ type BlockChain struct {
badBlocks *lru.Cache // Bad block cache badBlocks *lru.Cache // Bad block cache
IPCEndpoint string IPCEndpoint string
Client *ethclient.Client // Global ipc client instance. Client *ethclient.Client // Global ipc client instance.
HookWriteRewards func(header *types.Header)
} }
// NewBlockChain returns a fully initialised block chain using information // 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 // Append a single chain head event if we've progressed the chain
if lastCanon != nil && bc.CurrentBlock().Hash() == lastCanon.Hash() { 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}) events = append(events, ChainHeadEvent{block})
log.Debug("New ChainHeadEvent from fetcher ", "number", block.NumberU64(), "hash", block.Hash()) log.Debug("New ChainHeadEvent from fetcher ", "number", block.NumberU64(), "hash", block.Hash())
} }
if bc.HookWriteRewards != nil {
bc.HookWriteRewards(block.Header())
}
return events, coalescedLogs, nil return events, coalescedLogs, nil
} }

View file

@ -19,7 +19,6 @@ package eth
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"github.com/ethereum/go-ethereum/consensus/posv"
"io/ioutil" "io/ioutil"
"math/big" "math/big"
"path/filepath" "path/filepath"
@ -239,15 +238,17 @@ func (b *EthApiBackend) GetEngine() consensus.Engine {
} }
func (s *EthApiBackend) GetRewardByHash(hash common.Hash) map[string]interface{} { func (s *EthApiBackend) GetRewardByHash(hash common.Hash) map[string]interface{} {
if c, ok := s.eth.Engine().(*posv.Posv); ok { header := s.eth.blockchain.GetHeaderByHash(hash)
rewards := c.GetRewards(hash) if header != nil {
if rewards != 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 return rewards
} }
} else { } else {
header := s.eth.blockchain.GetHeaderByHash(hash) data, err = ioutil.ReadFile(filepath.Join(common.StoreRewardFolder, header.Number.String()+"."+header.HashNoValidator().Hex()))
if header != nil {
data, err := ioutil.ReadFile(filepath.Join(s.eth.config.StoreRewardFolder, header.Number.String()+"."+header.Hash().Hex()))
if err == nil { if err == nil {
rewards := make(map[string]interface{}) rewards := make(map[string]interface{})
err = json.Unmarshal(data, &rewards) err = json.Unmarshal(data, &rewards)

View file

@ -18,12 +18,9 @@
package eth package eth
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"math/big" "math/big"
"path/filepath"
"runtime" "runtime"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -374,27 +371,6 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
} }
return false 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 return eth, nil
} }

View file

@ -114,8 +114,6 @@ type Config struct {
// Miscellaneous options // Miscellaneous options
DocRoot string `toml:"-"` DocRoot string `toml:"-"`
StoreRewardFolder string
} }
type configMarshaling struct { type configMarshaling struct {

View file

@ -494,13 +494,7 @@ func (s *PublicBlockChainAPI) BlockNumber() *big.Int {
// BlockNumber returns the block number of the chain head. // BlockNumber returns the block number of the chain head.
func (s *PublicBlockChainAPI) GetRewardByHash(hash common.Hash) map[string]interface{} { func (s *PublicBlockChainAPI) GetRewardByHash(hash common.Hash) map[string]interface{} {
if c, ok := s.b.GetEngine().(*posv.Posv); ok { return s.b.GetRewardByHash(hash)
rewards := c.GetRewards(hash)
if rewards != nil {
return rewards
}
}
return make(map[string]interface{})
} }
// GetBalance returns the amount of wei for the given address in the state of the // GetBalance returns the amount of wei for the given address in the state of the

View file

@ -18,8 +18,10 @@ package les
import ( import (
"context" "context"
"github.com/ethereum/go-ethereum/consensus/posv" "encoding/json"
"io/ioutil"
"math/big" "math/big"
"path/filepath"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -202,11 +204,25 @@ func (b *LesApiBackend) GetEngine() consensus.Engine {
return b.eth.engine return b.eth.engine
} }
func (s *LesApiBackend) GetRewardByHash(hash common.Hash) map[string]interface{} { func (s *LesApiBackend) GetRewardByHash(hash common.Hash) map[string]interface{} {
if c, ok := s.eth.Engine().(*posv.Posv); ok { header := s.eth.blockchain.GetHeaderByHash(hash)
rewards := c.GetRewards(hash) if header != nil {
if rewards != 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 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{}) return make(map[string]interface{})
} }