mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
Merge pull request #380 from nguyenbatam/fix_duplicate_hook_reward
Fix duplicate hook reward & api get reward
This commit is contained in:
commit
6645f23a80
10 changed files with 66 additions and 76 deletions
|
|
@ -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.
|
||||||
|
|
|
||||||
|
|
@ -18,3 +18,4 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var IsTestnet bool = false
|
var IsTestnet bool = false
|
||||||
|
var StoreRewardFolder string
|
||||||
|
|
|
||||||
|
|
@ -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)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,10 @@ package eth
|
||||||
|
|
||||||
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"
|
||||||
|
|
@ -236,11 +238,25 @@ 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 {
|
||||||
|
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{})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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"
|
||||||
|
|
@ -369,27 +366,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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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{})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -612,12 +612,12 @@ func (self *worker) commitNewWork() {
|
||||||
delete(self.possibleUncles, hash)
|
delete(self.possibleUncles, hash)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if atomic.LoadInt32(&self.mining) == 1 {
|
||||||
// Create the new block to seal with the consensus engine
|
// Create the new block to seal with the consensus engine
|
||||||
if work.Block, err = self.engine.Finalize(self.chain, header, work.state, work.txs, uncles, work.receipts); err != nil {
|
if work.Block, err = self.engine.Finalize(self.chain, header, work.state, work.txs, uncles, work.receipts); err != nil {
|
||||||
log.Error("Failed to finalize block for sealing", "err", err)
|
log.Error("Failed to finalize block for sealing", "err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if atomic.LoadInt32(&self.mining) == 1 {
|
|
||||||
log.Info("Committing new block", "number", work.Block.Number(), "txs", work.tcount, "special txs", len(specialTxs), "uncles", len(uncles), "elapsed", common.PrettyDuration(time.Since(tstart)))
|
log.Info("Committing new block", "number", work.Block.Number(), "txs", work.tcount, "special txs", len(specialTxs), "uncles", len(uncles), "elapsed", common.PrettyDuration(time.Since(tstart)))
|
||||||
self.unconfirmed.Shift(work.Block.NumberU64() - 1)
|
self.unconfirmed.Shift(work.Block.NumberU64() - 1)
|
||||||
self.lastParentBlockCommit = parent.Hash().Hex()
|
self.lastParentBlockCommit = parent.Hash().Hex()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue