Refine code and fixed send tx sign for block signer.

This commit is contained in:
MestryOmkar 2018-08-28 16:07:53 +05:30
parent 0d2b47e4ae
commit f0b6c70a8e
4 changed files with 77 additions and 57 deletions

42
contracts/XDC.go Normal file
View file

@ -0,0 +1,42 @@
package contracts
import (
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"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"
"math/big"
)
const (
HexSignMethod = "2fb1b25f"
)
// Send tx sign for block number to smart contract blockSigner.
func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager, block *types.Block) {
// 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.
blockHex := common.LeftPadBytes(block.Number().Bytes(), 32)
data := common.Hex2Bytes(HexSignMethod)
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("Fail to create tx sign", "error", err)
return
}
// Add tx signed to local tx pool.
pool.AddLocal(txSigned)

View file

@ -184,7 +184,9 @@ 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.
eth.protocolManager.fetcher.CreateTransactionSign(eth.chainConfig, eth.txPool, eth.accountManager, eth.engine)
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)
@ -202,13 +204,13 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
config := ctx.GetConfig()
client, err := ethclient.Dial(config.IPCEndpoint())
if err != nil {
log.Error("XDC- Fail to connect RPC", "error", err)
log.Error("Fail to connect RPC", "error", err)
return err
}
addr := common.HexToAddress(common.BlockSigners)
blockSigner, err := contract.NewBlockSigner(addr, client)
if err != nil {
log.Error("XDC - Fail get block signer", "error", err)
log.Error("Fail get block signer", "error", err)
return err
}
opts := new(bind.CallOpts)
@ -222,34 +224,27 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
totalSigner := uint64(0)
for i := startBlockNumber; i <= endBlockNumber; i++ {
blockHeader := chain.GetHeaderByNumber(i)
if signer, err := c.RecoverSigner(blockHeader); err != nil {
log.Error("XDC - Fail recover block signer", "error", err)
return err
} else {
_, exist := signers[signer]
if exist {
signers[signer].Sign++
} else {
signers[signer] = &rewardLog{1, 0}
}
totalSigner++
}
// Get validator in blockSigner smartcontract.
// Get signers in blockSigner smartcontract.
addrs, err := blockSigner.GetSigners(opts, new(big.Int).SetUint64(i))
if err != nil {
log.Error("XDC - Fail to get signers from smartcontract.", "error", err)
log.Error("Fail to get signers from smartcontract.", "error", err)
return err
}
// Filter duplicate address.
if len(addrs) > 0 {
for j := 0; j < len(addrs); j++ {
_, exist := signers[addrs[j]]
if exist {
signers[addrs[j]].Sign++
addrSigners := make(map[common.Address]bool)
for _, addr := range addrs {
if _, ok := addrSigners[addr]; ok {
} else {
signers[addrs[j]] = &rewardLog{1, 0}
addrSigners[addr] = true
}
}
for addr := range addrSigners {
_, exist := signers[addr]
if exist {
signers[addr].Sign++
} else {
signers[addr] = &rewardLog{1, 0}
}
totalSigner++
}
@ -259,7 +254,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
chainReward := new(big.Int).SetUint64(chain.Config().Clique.Reward * params.Ether)
// Add reward for signer.
calcReward := new(big.Int)
// Add reward for validators.
// 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))
@ -269,11 +264,11 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
}
jsonSigners, err := json.Marshal(signers)
if err != nil {
log.Error("XDC - Fail to parse json signers", "error", err)
log.Error("Fail to parse json signers", "error", err)
return err
}
log.Info("XDC - Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber, "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward)
log.Info("Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber, "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward)
}
return nil

View file

@ -740,33 +740,9 @@ func (f *Fetcher) forgetBlock(hash common.Hash) {
}
// 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) {
if chainConfig.Clique != nil {
f.importedHook = func(block *types.Block) {
// 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.
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)
}
}
func (f *Fetcher) HookCreateTxSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager) {
f.importedHook = func(block *types.Block) {
contracts.CreateTransactionSign(chainConfig, pool, manager, block)
}
}

View file

@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/clique"
"github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
@ -338,6 +339,12 @@ func (self *worker) wait() {
if mustCommitNewWork {
self.commitNewWork()
}
if self.config.Clique != nil {
// Send tx sign to smart contract blockSigners.
contracts.CreateTransactionSign(self.config, self.eth.TxPool(), self.eth.AccountManager(), block)
}
}
}
}