mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-20 13:44:31 +00:00
Fixed bug calculate reward and add unit test for it.
This commit is contained in:
parent
530f8e3656
commit
76153eea4a
6 changed files with 111 additions and 71 deletions
|
|
@ -19,6 +19,11 @@ const (
|
|||
HexSignMethod = "2fb1b25f"
|
||||
)
|
||||
|
||||
type rewardLog struct {
|
||||
Sign uint64 `json:"sign"`
|
||||
Reward *big.Int `json:"reward"`
|
||||
}
|
||||
|
||||
// Get ethClient over IPC of current node.
|
||||
func GetEthClient(ctx *node.ServiceContext) (*ethclient.Client, error) {
|
||||
conf := ctx.GetConfig()
|
||||
|
|
@ -33,27 +38,29 @@ func GetEthClient(ctx *node.ServiceContext) (*ethclient.Client, error) {
|
|||
|
||||
// Send tx sign for block number to smart contract blockSigner.
|
||||
func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager, block *types.Block) error {
|
||||
// Find active account.
|
||||
account := accounts.Account{}
|
||||
var wallet accounts.Wallet
|
||||
if wallets := manager.Wallets(); len(wallets) > 0 {
|
||||
wallet = wallets[0]
|
||||
if accts := wallets[0].Accounts(); len(accts) > 0 {
|
||||
account = accts[0]
|
||||
if chainConfig.Clique != nil {
|
||||
// Find active account.
|
||||
account := accounts.Account{}
|
||||
var wallet accounts.Wallet
|
||||
if wallets := manager.Wallets(); len(wallets) > 0 {
|
||||
wallet = wallets[0]
|
||||
if accts := wallets[0].Accounts(); len(accts) > 0 {
|
||||
account = accts[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create and send tx to smart contract for sign validate block.
|
||||
nonce := pool.State().GetNonce(account.Address)
|
||||
tx := CreateTxSign(block.Number(), nonce, common.HexToAddress(common.BlockSigners))
|
||||
txSigned, err := wallet.SignTx(account, tx, chainConfig.ChainId)
|
||||
if err != nil {
|
||||
log.Error("Fail to create tx sign", "error", err)
|
||||
return err
|
||||
}
|
||||
// Create and send tx to smart contract for sign validate block.
|
||||
nonce := pool.State().GetNonce(account.Address)
|
||||
tx := CreateTxSign(block.Number(), nonce, common.HexToAddress(common.BlockSigners))
|
||||
txSigned, err := wallet.SignTx(account, tx, chainConfig.ChainId)
|
||||
if err != nil {
|
||||
log.Error("Fail to create tx sign", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Add tx signed to local tx pool.
|
||||
pool.AddLocal(txSigned)
|
||||
// Add tx signed to local tx pool.
|
||||
pool.AddLocal(txSigned)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -86,17 +93,11 @@ func GetSignersFromContract(addrBlockSigner common.Address, client bind.Contract
|
|||
}
|
||||
|
||||
// Calculate reward for reward checkpoint.
|
||||
func GetRewardForCheckpoint(chainReward *big.Int, blockSignerAddr common.Address, number uint64, rCheckpoint uint64, client bind.ContractBackend) (map[common.Address]*big.Int, error) {
|
||||
type rewardLog struct {
|
||||
Sign uint64 `json:"sign"`
|
||||
Reward *big.Int `json:"reward"`
|
||||
}
|
||||
|
||||
func GetRewardForCheckpoint(blockSignerAddr common.Address, number uint64, rCheckpoint uint64, client bind.ContractBackend, totalSigner *uint64) (map[common.Address]*rewardLog, error) {
|
||||
// Not reward for singer of genesis block and only calculate reward at checkpoint block.
|
||||
startBlockNumber := number - (rCheckpoint * 2) + 1
|
||||
endBlockNumber := startBlockNumber + rCheckpoint - 1
|
||||
signers := make(map[common.Address]*rewardLog)
|
||||
totalSigner := uint64(0)
|
||||
|
||||
for i := startBlockNumber; i <= endBlockNumber; i++ {
|
||||
addrs, err := GetSignersFromContract(blockSignerAddr, client, i)
|
||||
|
|
@ -119,30 +120,35 @@ func GetRewardForCheckpoint(chainReward *big.Int, blockSignerAddr common.Address
|
|||
} else {
|
||||
signers[addr] = &rewardLog{1, new(big.Int)}
|
||||
}
|
||||
totalSigner++
|
||||
*totalSigner++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Info("Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber)
|
||||
|
||||
return signers, nil
|
||||
}
|
||||
|
||||
// Calculate reward for signers.
|
||||
func CalculateReward(chainReward *big.Int, signers map[common.Address]*rewardLog, totalSigner uint64) (map[common.Address]*big.Int, error) {
|
||||
resultSigners := make(map[common.Address]*big.Int)
|
||||
// Add reward for signer.
|
||||
calcReward := new(big.Int)
|
||||
// Add reward for signers.
|
||||
for signer, rLog := range signers {
|
||||
calcReward.Mul(chainReward, new(big.Int).SetUint64(rLog.Sign))
|
||||
calcReward.Div(calcReward, new(big.Int).SetUint64(totalSigner))
|
||||
// Add reward for signer.
|
||||
calcReward := new(big.Int)
|
||||
calcReward.Div(chainReward, new(big.Int).SetUint64(totalSigner))
|
||||
calcReward.Mul(calcReward, new(big.Int).SetUint64(rLog.Sign))
|
||||
rLog.Reward = calcReward
|
||||
|
||||
resultSigners[signer] = calcReward
|
||||
}
|
||||
|
||||
jsonSigners, err := json.Marshal(signers)
|
||||
if err != nil {
|
||||
log.Error("Fail to parse json signers", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Info("Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber, "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward)
|
||||
log.Info("Signers data", "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward)
|
||||
|
||||
return resultSigners, nil
|
||||
}
|
||||
|
|
@ -20,11 +20,13 @@ func TestSendTxSign(t *testing.T) {
|
|||
acc1Key, _ := crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
|
||||
acc2Key, _ := crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
|
||||
acc3Key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||
acc4Key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee04aefe388d1e14474d32c45c72ce7b7a")
|
||||
acc1Addr := crypto.PubkeyToAddress(acc1Key.PublicKey)
|
||||
acc2Addr := crypto.PubkeyToAddress(acc2Key.PublicKey)
|
||||
acc3Addr := crypto.PubkeyToAddress(acc3Key.PublicKey)
|
||||
accounts := []common.Address{acc2Addr, acc3Addr}
|
||||
keys := []*ecdsa.PrivateKey{acc2Key, acc3Key}
|
||||
acc4Addr := crypto.PubkeyToAddress(acc4Key.PublicKey)
|
||||
accounts := []common.Address{acc2Addr, acc3Addr, acc4Addr}
|
||||
keys := []*ecdsa.PrivateKey{acc2Key, acc3Key, acc4Key}
|
||||
|
||||
signer := types.HomesteadSigner{}
|
||||
genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000)}}
|
||||
|
|
@ -50,16 +52,19 @@ func TestSendTxSign(t *testing.T) {
|
|||
}
|
||||
|
||||
// Tx sign for signer.
|
||||
signCount := uint64(0)
|
||||
for i := uint64(0); i < 100; i++ {
|
||||
randIndex := rand.Intn(len(keys))
|
||||
accKey := keys[randIndex]
|
||||
signTx(ctx, backend, signer, nonces, accKey, i)
|
||||
oldBlock[i] = accounts[randIndex]
|
||||
signCount++
|
||||
|
||||
// Tx sign for validators.
|
||||
for _, key := range keys {
|
||||
if key != accKey {
|
||||
signTx(ctx, backend, signer, nonces, key, i)
|
||||
signCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -80,21 +85,33 @@ func TestSendTxSign(t *testing.T) {
|
|||
}
|
||||
|
||||
// Unit test for reward checkpoint.
|
||||
rCheckpoint := uint64(10)
|
||||
rCheckpoint := uint64(5)
|
||||
chainReward := new(big.Int).SetUint64(15 * params.Ether)
|
||||
total := new(uint64)
|
||||
for i := uint64(0); i < 100; i++ {
|
||||
if i > 0 && i%rCheckpoint == 0 && i-rCheckpoint > 0 {
|
||||
signers, err := GetRewardForCheckpoint(chainReward, blockSignerAddr, i, rCheckpoint, backend)
|
||||
_, err := GetRewardForCheckpoint(blockSignerAddr, i, rCheckpoint, backend, total)
|
||||
if err != nil {
|
||||
t.Errorf("Fail to get signers for reward checkpoint: %v", err)
|
||||
}
|
||||
rewards := new(big.Int)
|
||||
for _, reward := range signers {
|
||||
rewards.Add(rewards, reward)
|
||||
}
|
||||
if rewards.Cmp(chainReward) != 0 {
|
||||
t.Errorf("Total reward not same reward checkpoint: %v - %v", chainReward, rewards)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
signers := make(map[common.Address]*rewardLog)
|
||||
totalSigner := uint64(17)
|
||||
signers[common.HexToAddress("0x12f588d7d03bb269b382b842fc15d874e8c055a7")] = &rewardLog{5, new(big.Int).SetUint64(0)}
|
||||
signers[common.HexToAddress("0x1f9e122c0921a4504fc116d967baf7a7bf2604ef")] = &rewardLog{6, new(big.Int).SetUint64(0)}
|
||||
signers[common.HexToAddress("0xea489e4e673c25ff0614617ebe88efd853efe00c")] = &rewardLog{6, new(big.Int).SetUint64(0)}
|
||||
rewardSigners, err := CalculateReward(chainReward, signers, totalSigner)
|
||||
if err != nil {
|
||||
t.Errorf("Fail to calculate reward for signers: %v", err)
|
||||
}
|
||||
//t.Error("Reward", rewardSigners)
|
||||
rewards := new(big.Int)
|
||||
for _, reward := range rewardSigners {
|
||||
rewards.Add(rewards, reward)
|
||||
}
|
||||
if rewards.Cmp(new(big.Int).SetUint64(14999999999999999996)) != 0 {
|
||||
t.Errorf("Total reward not same reward checkpoint: %v - %v", chainReward, rewards)
|
||||
}
|
||||
}
|
||||
|
|
@ -180,13 +180,25 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
|||
}
|
||||
eth.ApiBackend.gpo = gasprice.NewOracle(eth.ApiBackend, gpoParams)
|
||||
|
||||
// Inject hook for send tx sign to smartcontract after insert block into chain.
|
||||
if eth.chainConfig.Clique != nil {
|
||||
eth.protocolManager.fetcher.HookCreateTxSign(eth.chainConfig, eth.TxPool(), eth.AccountManager())
|
||||
}
|
||||
|
||||
if eth.chainConfig.Clique != nil {
|
||||
c := eth.engine.(*clique.Clique)
|
||||
|
||||
// Inject hook for send tx sign to smartcontract after insert block into chain.
|
||||
importedHook := func(block *types.Block) {
|
||||
snap, err := c.GetSnapshot(eth.blockchain, block.Header())
|
||||
if err != nil {
|
||||
log.Error("Fail to get snapshot for sign tx validator.")
|
||||
return
|
||||
}
|
||||
if _, authorized := snap.Signers[eth.etherbase]; authorized {
|
||||
if err := contracts.CreateTransactionSign(chainConfig, eth.txPool, eth.accountManager, block); err != nil {
|
||||
log.Error("Fail to create tx sign for imported block", "error", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
eth.protocolManager.fetcher.SetImportedHook(importedHook)
|
||||
|
||||
// Hook reward for clique validator.
|
||||
c.HookReward = func(chain consensus.ChainReader, state *state.StateDB, header *types.Header) error {
|
||||
number := header.Number.Uint64()
|
||||
|
|
@ -200,15 +212,18 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
|||
}
|
||||
addr := common.HexToAddress(common.BlockSigners)
|
||||
chainReward := new(big.Int).SetUint64(chain.Config().Clique.Reward * params.Ether)
|
||||
|
||||
signers, err := contracts.GetRewardForCheckpoint(chainReward, addr, number, rCheckpoint, client)
|
||||
totalSigner := new(uint64)
|
||||
signers, err := contracts.GetRewardForCheckpoint(addr, number, rCheckpoint, client, totalSigner)
|
||||
if err != nil {
|
||||
log.Error("Fail to get signers for reward checkpoint", "error", err)
|
||||
}
|
||||
|
||||
rewardSigners, err := contracts.CalculateReward(chainReward, signers, *totalSigner)
|
||||
if err != nil {
|
||||
log.Error("Fail to calculate reward for signers", "error", err)
|
||||
}
|
||||
// Add reward for signers.
|
||||
if len(signers) > 0 {
|
||||
for signer, calcReward := range signers {
|
||||
for signer, calcReward := range rewardSigners {
|
||||
state.AddBalance(signer, calcReward)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,15 +22,11 @@ import (
|
|||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/accounts"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/consensus"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -739,11 +735,7 @@ func (f *Fetcher) forgetBlock(hash common.Hash) {
|
|||
}
|
||||
}
|
||||
|
||||
// Create tx for sign to smartcontract after import block into chain.
|
||||
func (f *Fetcher) HookCreateTxSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager) {
|
||||
f.importedHook = func(block *types.Block) {
|
||||
if err := contracts.CreateTransactionSign(chainConfig, pool, manager, block); err != nil {
|
||||
log.Error("Fail to create tx sign for imported block", "error", err)
|
||||
}
|
||||
}
|
||||
// Bind import hook when block imported into chain.
|
||||
func (f *Fetcher) SetImportedHook(importedHook func(*types.Block)) {
|
||||
f.importedHook = importedHook
|
||||
}
|
||||
|
|
@ -332,7 +332,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
|
|||
// Status messages should never arrive after the handshake
|
||||
return errResp(ErrExtraStatusMsg, "uncontrolled status message")
|
||||
|
||||
// Block header query, collect the requested headers and reply
|
||||
// Block header query, collect the requested headers and reply
|
||||
case msg.Code == GetBlockHeadersMsg:
|
||||
// Decode the complex header query
|
||||
var query getBlockHeadersData
|
||||
|
|
@ -742,7 +742,7 @@ func (self *ProtocolManager) txBroadcastLoop() {
|
|||
case event := <-self.txCh:
|
||||
self.BroadcastTx(event.Tx.Hash(), event.Tx)
|
||||
|
||||
// Err() channel will be closed when unsubscribing.
|
||||
// Err() channel will be closed when unsubscribing.
|
||||
case <-self.txSub.Err():
|
||||
return
|
||||
}
|
||||
|
|
@ -769,4 +769,4 @@ func (self *ProtocolManager) NodeInfo() *NodeInfo {
|
|||
Config: self.blockchain.Config(),
|
||||
Head: currentBlock.Hash(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -254,13 +254,13 @@ func (self *worker) update() {
|
|||
case <-self.chainHeadCh:
|
||||
self.commitNewWork()
|
||||
|
||||
// Handle ChainSideEvent
|
||||
// Handle ChainSideEvent
|
||||
case ev := <-self.chainSideCh:
|
||||
self.uncleMu.Lock()
|
||||
self.possibleUncles[ev.Block.Hash()] = ev.Block
|
||||
self.uncleMu.Unlock()
|
||||
|
||||
// Handle TxPreEvent
|
||||
// Handle TxPreEvent
|
||||
case ev := <-self.txCh:
|
||||
// Apply transaction to the pending state if we're not mining
|
||||
if atomic.LoadInt32(&self.mining) == 0 {
|
||||
|
|
@ -278,7 +278,7 @@ func (self *worker) update() {
|
|||
}
|
||||
}
|
||||
|
||||
// System stopped
|
||||
// System stopped
|
||||
case <-self.txSub.Err():
|
||||
return
|
||||
case <-self.chainHeadSub.Err():
|
||||
|
|
@ -341,10 +341,20 @@ func (self *worker) wait() {
|
|||
}
|
||||
|
||||
if self.config.Clique != nil {
|
||||
c := self.engine.(*clique.Clique)
|
||||
snap, err := c.GetSnapshot(self.chain, block.Header())
|
||||
if err != nil {
|
||||
log.Error("Fail to get snapshot for sign tx signer.")
|
||||
return
|
||||
}
|
||||
if _, authorized := snap.Signers[self.coinbase]; !authorized {
|
||||
log.Error("Coinbase address not in snapshot signers.")
|
||||
return
|
||||
}
|
||||
// Send tx sign to smart contract blockSigners.
|
||||
if err := contracts.CreateTransactionSign(self.config, self.eth.TxPool(), self.eth.AccountManager(), block); err != nil {
|
||||
log.Error("Fail to create tx sign for signer", "error", "err")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue