Merge pull request #62 from dinhln89/reward

Add unit test for calculate reward for signers at reward checkpoint.
This commit is contained in:
Tuna 2018-06-28 17:59:47 +07:00 committed by GitHub
commit c4147006cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 200 additions and 114 deletions

View file

@ -1,15 +1,14 @@
package contracts package contracts
import ( import (
"encoding/json"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/contracts/blocksigner/contract" "github.com/ethereum/go-ethereum/contracts/blocksigner/contract"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"math/big" "math/big"
) )
@ -18,20 +17,14 @@ const (
HexSignMethod = "2fb1b25f" HexSignMethod = "2fb1b25f"
) )
// Get ethClient over IPC of current node. type rewardLog struct {
func GetEthClient(ctx *node.ServiceContext) (*ethclient.Client, error) { Sign uint64 `json:"sign"`
conf := ctx.GetConfig() Reward *big.Int `json:"reward"`
client, err := ethclient.Dial(conf.IPCEndpoint())
if err != nil {
log.Error("Fail to connect RPC", "error", err)
return nil, err
}
return client, nil
} }
// Send tx sign for block number to smart contract blockSigner. // 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 { func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager, block *types.Block) error {
if chainConfig.Clique != nil {
// Find active account. // Find active account.
account := accounts.Account{} account := accounts.Account{}
var wallet accounts.Wallet var wallet accounts.Wallet
@ -53,6 +46,7 @@ func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, m
// Add tx signed to local tx pool. // Add tx signed to local tx pool.
pool.AddLocal(txSigned) pool.AddLocal(txSigned)
}
return nil return nil
} }
@ -68,9 +62,8 @@ func CreateTxSign(blockNumber *big.Int, nonce uint64, blockSigner common.Address
} }
// Get signers signed for blockNumber from blockSigner contract. // Get signers signed for blockNumber from blockSigner contract.
func GetSignersFromContract(client bind.ContractBackend, blockNumber uint64) ([]common.Address, error) { func GetSignersFromContract(addrBlockSigner common.Address, client bind.ContractBackend, blockNumber uint64) ([]common.Address, error) {
addr := common.HexToAddress(common.BlockSigners) blockSigner, err := contract.NewBlockSigner(addrBlockSigner, client)
blockSigner, err := contract.NewBlockSigner(addr, client)
if err != nil { if err != nil {
log.Error("Fail get instance of blockSigner", "error", err) log.Error("Fail get instance of blockSigner", "error", err)
return nil, err return nil, err
@ -84,3 +77,64 @@ func GetSignersFromContract(client bind.ContractBackend, blockNumber uint64) ([]
return addrs, nil return addrs, nil
} }
// Calculate reward for reward checkpoint.
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)
for i := startBlockNumber; i <= endBlockNumber; i++ {
addrs, err := GetSignersFromContract(blockSignerAddr, client, i)
if err != nil {
log.Error("Fail to get signers from smartcontract.", "error", err, "blockNumber", i)
return nil, err
}
// Filter duplicate address.
if len(addrs) > 0 {
addrSigners := make(map[common.Address]bool)
for _, addr := range addrs {
if _, ok := addrSigners[addr]; !ok {
addrSigners[addr] = true
}
}
for addr := range addrSigners {
_, exist := signers[addr]
if exist {
signers[addr].Sign++
} else {
signers[addr] = &rewardLog{1, new(big.Int)}
}
*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 signers.
for signer, rLog := range signers {
// 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("Signers data", "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward)
return resultSigners, nil
}

View file

@ -10,6 +10,7 @@ import (
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"math/big" "math/big"
"math/rand" "math/rand"
"testing" "testing"
@ -19,11 +20,13 @@ func TestSendTxSign(t *testing.T) {
acc1Key, _ := crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") acc1Key, _ := crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
acc2Key, _ := crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") acc2Key, _ := crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
acc3Key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") acc3Key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
acc4Key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee04aefe388d1e14474d32c45c72ce7b7a")
acc1Addr := crypto.PubkeyToAddress(acc1Key.PublicKey) acc1Addr := crypto.PubkeyToAddress(acc1Key.PublicKey)
acc2Addr := crypto.PubkeyToAddress(acc2Key.PublicKey) acc2Addr := crypto.PubkeyToAddress(acc2Key.PublicKey)
acc3Addr := crypto.PubkeyToAddress(acc3Key.PublicKey) acc3Addr := crypto.PubkeyToAddress(acc3Key.PublicKey)
accounts := []common.Address{acc2Addr, acc3Addr} acc4Addr := crypto.PubkeyToAddress(acc4Key.PublicKey)
keys := []*ecdsa.PrivateKey{acc2Key, acc3Key} accounts := []common.Address{acc2Addr, acc3Addr, acc4Addr}
keys := []*ecdsa.PrivateKey{acc2Key, acc3Key, acc4Key}
signer := types.HomesteadSigner{} signer := types.HomesteadSigner{}
genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000)}} genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000)}}
@ -49,16 +52,19 @@ func TestSendTxSign(t *testing.T) {
} }
// Tx sign for signer. // Tx sign for signer.
signCount := uint64(0)
for i := uint64(0); i < 100; i++ { for i := uint64(0); i < 100; i++ {
randIndex := rand.Intn(len(keys)) randIndex := rand.Intn(len(keys))
accKey := keys[randIndex] accKey := keys[randIndex]
signTx(ctx, backend, signer, nonces, accKey, i) signTx(ctx, backend, signer, nonces, accKey, i)
oldBlock[i] = accounts[randIndex] oldBlock[i] = accounts[randIndex]
signCount++
// Tx sign for validators. // Tx sign for validators.
for _, key := range keys { for _, key := range keys {
if key != accKey { if key != accKey {
signTx(ctx, backend, signer, nonces, key, i) signTx(ctx, backend, signer, nonces, key, i)
signCount++
} }
} }
} }
@ -77,4 +83,35 @@ func TestSendTxSign(t *testing.T) {
t.Error("Tx sign for block validators not match") t.Error("Tx sign for block validators not match")
} }
} }
// Unit test for reward checkpoint.
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 {
_, err := GetRewardForCheckpoint(blockSignerAddr, i, rCheckpoint, backend, total)
if err != nil {
t.Errorf("Fail to get signers for reward checkpoint: %v", err)
}
}
}
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)
}
} }

View file

@ -25,7 +25,6 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"encoding/json"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
@ -41,6 +40,7 @@ import (
"github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/eth/filters" "github.com/ethereum/go-ethereum/eth/filters"
"github.com/ethereum/go-ethereum/eth/gasprice" "github.com/ethereum/go-ethereum/eth/gasprice"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/internal/ethapi"
@ -95,6 +95,8 @@ type Ethereum struct {
netRPCService *ethapi.PublicNetAPI netRPCService *ethapi.PublicNetAPI
lock sync.RWMutex // Protects the variadic fields (e.g. gas price and etherbase) lock sync.RWMutex // Protects the variadic fields (e.g. gas price and etherbase)
IPCEndpoint string
Client *ethclient.Client // Global ipc client instance.
} }
func (s *Ethereum) AddLesServer(ls LesServer) { func (s *Ethereum) AddLesServer(ls LesServer) {
@ -181,82 +183,58 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
} }
eth.ApiBackend.gpo = gasprice.NewOracle(eth.ApiBackend, gpoParams) 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 { if eth.chainConfig.Clique != nil {
c := eth.engine.(*clique.Clique) c := eth.engine.(*clique.Clique)
// Set global ipc endpoint.
eth.IPCEndpoint = ctx.GetConfig().IPCEndpoint()
// 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. // Hook reward for clique validator.
c.HookReward = func(chain consensus.ChainReader, state *state.StateDB, header *types.Header) error { c.HookReward = func(chain consensus.ChainReader, state *state.StateDB, header *types.Header) error {
type rewardLog struct { client, err := eth.GetClient()
Sign uint64 `json:"sign"` if err != nil {
Reward float64 `json:"reward"` log.Error("Fail to connect IPC client for blockSigner", "error", err)
return err
} }
number := header.Number.Uint64() number := header.Number.Uint64()
rCheckpoint := chain.Config().Clique.RewardCheckpoint rCheckpoint := chain.Config().Clique.RewardCheckpoint
prevCheckpoint := number - rCheckpoint if number > 0 && number-rCheckpoint > 0 {
if number > 0 && prevCheckpoint > 0 {
// 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)
// Get signers in blockSigner smartcontract. // Get signers in blockSigner smartcontract.
client, err := contracts.GetEthClient(ctx) addr := common.HexToAddress(common.BlockSigners)
if err != nil {
log.Error("Fail to connect IPC from blockSigner", "error", err)
return err
}
for i := startBlockNumber; i <= endBlockNumber; i++ {
addrs, err := contracts.GetSignersFromContract(client, i)
if err != nil {
log.Error("Fail to get signers from smartcontract.", "error", err, "blockNumber", i)
return err
}
// Filter duplicate address.
if len(addrs) > 0 {
addrSigners := make(map[common.Address]bool)
for _, addr := range addrs {
if _, ok := addrSigners[addr]; ok {
} else {
addrSigners[addr] = true
}
}
for addr := range addrSigners {
_, exist := signers[addr]
if exist {
signers[addr].Sign++
} else {
signers[addr] = &rewardLog{1, 0}
}
totalSigner++
}
}
}
chainReward := new(big.Int).SetUint64(chain.Config().Clique.Reward * params.Ether) chainReward := new(big.Int).SetUint64(chain.Config().Clique.Reward * params.Ether)
// Add reward for signer. totalSigner := new(uint64)
calcReward := new(big.Int) 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. // Add reward for signers.
for signer, rLog := range signers { if len(signers) > 0 {
calcReward.Mul(chainReward, new(big.Int).SetUint64(rLog.Sign)) for signer, calcReward := range rewardSigners {
calcReward.Div(calcReward, new(big.Int).SetUint64(totalSigner))
rLog.Reward = float64(calcReward.Int64())
state.AddBalance(signer, calcReward) state.AddBalance(signer, calcReward)
} }
jsonSigners, err := json.Marshal(signers)
if err != nil {
log.Error("Fail to parse json signers", "error", err)
return err
} }
log.Info("Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber, "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward)
} }
return nil return nil
@ -537,3 +515,18 @@ func (s *Ethereum) Stop() error {
return nil return nil
} }
// Get current IPC Client.
func (s *Ethereum) GetClient() (*ethclient.Client, error) {
if s.Client == nil {
// Inject ipc client global instance.
client, err := ethclient.Dial(s.IPCEndpoint)
if err != nil {
log.Error("Fail to connect RPC", "error", err)
return nil, err
}
s.Client = client
}
return s.Client, nil
}

View file

@ -22,14 +22,10 @@ import (
"math/rand" "math/rand"
"time" "time"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"gopkg.in/karalabe/cookiejar.v2/collections/prque" "gopkg.in/karalabe/cookiejar.v2/collections/prque"
) )
@ -739,11 +735,7 @@ func (f *Fetcher) forgetBlock(hash common.Hash) {
} }
} }
// Create tx for sign to smartcontract after import block into chain. // Bind import hook when block imported into chain.
func (f *Fetcher) HookCreateTxSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager) { func (f *Fetcher) SetImportedHook(importedHook func(*types.Block)) {
f.importedHook = func(block *types.Block) { f.importedHook = importedHook
if err := contracts.CreateTransactionSign(chainConfig, pool, manager, block); err != nil {
log.Error("Fail to create tx sign for imported block", "error", err)
}
}
} }

View file

@ -341,6 +341,16 @@ func (self *worker) wait() {
} }
if self.config.Clique != nil { 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. // Send tx sign to smart contract blockSigners.
if err := contracts.CreateTransactionSign(self.config, self.eth.TxPool(), self.eth.AccountManager(), block); err != nil { 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") log.Error("Fail to create tx sign for signer", "error", "err")