Fixed send tx sign to smart contract for block signer.

This commit is contained in:
parmarrushabh 2018-08-19 15:29:11 +05:30
parent a710ab8c68
commit 8b71594e11
2 changed files with 31 additions and 49 deletions

View file

@ -202,29 +202,30 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
config := ctx.GetConfig() config := ctx.GetConfig()
client, err := ethclient.Dial(config.IPCEndpoint()) client, err := ethclient.Dial(config.IPCEndpoint())
if err != nil { if err != nil {
log.Error("XDC - Fail to connect RPC", "error", err) log.Error("XDC- Fail to connect RPC", "error", err)
return err
} }
addr := common.HexToAddress(common.BlockSigners) addr := common.HexToAddress(common.BlockSigners)
blockSigner, err := contract.NewBlockSigner(addr, client) blockSigner, err := contract.NewBlockSigner(addr, client)
if err != nil { if err != nil {
log.Error("XDC - Fail get block signer", "error", err) log.Error("XDC - Fail get block signer", "error", err)
return err
} }
opts := new(bind.CallOpts) opts := new(bind.CallOpts)
prevCheckpoint := number - rCheckpoint prevCheckpoint := number - rCheckpoint
if number > 0 && prevCheckpoint > 0 { if number > 0 && prevCheckpoint > 0 {
// Not reward for singer of genesis block and only calculate reward at checkpoint block. // Not reward for singer of genesis block and only calculate reward at checkpoint block.
startBlockNumber := number - (rCheckpoint * 2) + 1 startBlockNumber := number - (rCheckpoint * 2) + 1
endBlockNumber := startBlockNumber + rCheckpoint - 1 endBlockNumber := startBlockNumber + rCheckpoint - 1
signers := make(map[common.Address]*rewardLog) signers := make(map[common.Address]*rewardLog)
validators := make(map[common.Address]*rewardLog)
totalSigner := uint64(0) totalSigner := uint64(0)
for i := startBlockNumber; i <= endBlockNumber; i++ { for i := startBlockNumber; i <= endBlockNumber; i++ {
blockHeader := chain.GetHeaderByNumber(i) blockHeader := chain.GetHeaderByNumber(i)
if signer, err := c.RecoverSigner(blockHeader); err != nil { if signer, err := c.RecoverSigner(blockHeader); err != nil {
log.Error("XDC - Fail recover block signer", "error", err)
return err return err
} else { } else {
_, exist := signers[signer] _, exist := signers[signer]
@ -237,14 +238,18 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
} }
// Get validator in blockSigner smartcontract. // Get validator in blockSigner smartcontract.
addrs, _ := blockSigner.GetSigners(opts, new(big.Int).SetUint64(i)) addrs, err := blockSigner.GetSigners(opts, new(big.Int).SetUint64(i))
if err != nil {
log.Error("XDC - Fail to get signers from smartcontract.", "error", err)
return err
}
if len(addrs) > 0 { if len(addrs) > 0 {
for j := 0; j < len(addrs); j++ { for j := 0; j < len(addrs); j++ {
_, exist := validators[addrs[j]] _, exist := signers[addrs[j]]
if exist { if exist {
validators[addrs[j]].Sign++ signers[addrs[j]].Sign++
} else { } else {
validators[addrs[j]] = &rewardLog{1, 0} signers[addrs[j]] = &rewardLog{1, 0}
} }
totalSigner++ totalSigner++
} }
@ -254,6 +259,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
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. // Add reward for signer.
calcReward := new(big.Int) calcReward := new(big.Int)
// Add reward for validators.
for signer, rLog := range signers { for signer, rLog := range signers {
calcReward.Mul(chainReward, new(big.Int).SetUint64(rLog.Sign)) calcReward.Mul(chainReward, new(big.Int).SetUint64(rLog.Sign))
calcReward.Div(calcReward, new(big.Int).SetUint64(totalSigner)) calcReward.Div(calcReward, new(big.Int).SetUint64(totalSigner))
@ -261,24 +267,13 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
state.AddBalance(signer, calcReward) state.AddBalance(signer, calcReward)
} }
// Add reward for validators.
for validator, rLog := range validators {
calcReward.Mul(chainReward, new(big.Int).SetUint64(rLog.Sign))
calcReward.Div(calcReward, new(big.Int).SetUint64(totalSigner))
rLog.Reward = float64(calcReward.Int64())
state.AddBalance(validator, calcReward)
}
jsonSigners, err := json.Marshal(signers) jsonSigners, err := json.Marshal(signers)
if err != nil { if err != nil {
return err log.Error("XDC - Fail to parse json signers", "error", err)
}
jsonValidators, err := json.Marshal(validators)
if err != nil {
return err return err
} }
log.Info("XDC - Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber, "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward, "validators", string(jsonValidators)) log.Info("XDC - Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber, "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward)
} }
return nil return nil
@ -441,8 +436,8 @@ func (self *Ethereum) SetEtherbase(etherbase common.Address) {
self.miner.SetEtherbase(etherbase) self.miner.SetEtherbase(etherbase)
} }
// ValidateStaker checks if node's address is in set of validators // ValidateMiner checks if node's address is in set of validators
func (s *Ethereum) ValidateStaker() (bool, error) { func (s *Ethereum) ValidateMiner() (bool, error) {
eb, err := s.Etherbase() eb, err := s.Etherbase()
if err != nil { if err != nil {
return false, err return false, err
@ -464,7 +459,7 @@ func (s *Ethereum) ValidateStaker() (bool, error) {
return true, nil return true, nil
} }
func (s *Ethereum) StartStaking(local bool) error { func (s *Ethereum) StartMining(local bool) error {
eb, err := s.Etherbase() eb, err := s.Etherbase()
if err != nil { if err != nil {
log.Error("Cannot start mining without etherbase", "err", err) log.Error("Cannot start mining without etherbase", "err", err)

View file

@ -25,7 +25,6 @@ import (
"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/consensus" "github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/clique"
"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/log" "github.com/ethereum/go-ethereum/log"
@ -743,8 +742,6 @@ func (f *Fetcher) forgetBlock(hash common.Hash) {
// Create tx for sign to smartcontract after import block into chain. // Create tx for sign to smartcontract after import block into chain.
func (f *Fetcher) CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager, engine consensus.Engine) { func (f *Fetcher) CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager, engine consensus.Engine) {
if chainConfig.Clique != nil { if chainConfig.Clique != nil {
c := engine.(*clique.Clique)
f.importedHook = func(block *types.Block) { f.importedHook = func(block *types.Block) {
// Find active account. // Find active account.
account := accounts.Account{} account := accounts.Account{}
@ -756,30 +753,20 @@ func (f *Fetcher) CreateTransactionSign(chainConfig *params.ChainConfig, pool *c
} }
} }
// Get block signer. // Create and send tx to smart contract for sign validate block.
signer, err := c.RecoverSigner(block.Header()) blockHex := common.LeftPadBytes(block.Number().Bytes(), 32)
data := common.Hex2Bytes("2fb1b25f")
inputData := append(data, blockHex...)
nonce := pool.State().GetNonce(account.Address)
tx := types.NewTransaction(nonce, common.HexToAddress(common.BlockSigners), big.NewInt(0), 100000, big.NewInt(0), inputData)
txSigned, err := wallet.SignTx(account, tx, chainConfig.ChainId)
if err != nil { if err != nil {
log.Error("XDC - Fail to get signer", "error", err) log.Error("XDC - Fail to create tx sign", "error", err)
return return
} }
// Not send tx sign when this node is current block miner. // Add tx signed to local tx pool.
if signer != account.Address { pool.AddLocal(txSigned)
// Create and send tx to smartcontract for sign validate block.
blockHex := common.LeftPadBytes(block.Number().Bytes(), 32)
data := common.Hex2Bytes("2fb1b25f")
inputData := append(data, blockHex...)
nonce := pool.State().GetNonce(account.Address)
tx := types.NewTransaction(nonce, common.HexToAddress(common.BlockSigners), big.NewInt(0), 100000, big.NewInt(0), inputData)
txSigned, err := wallet.SignTx(account, tx, chainConfig.ChainId)
if err != nil {
log.Error("XDC - Fail to create tx sign", "error", err)
return
}
// Add tx signed to local tx pool.
pool.AddLocal(txSigned)
}
} }
} }
} }