mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
Merge pull request #46 from dinhln89/master
Fixed send tx sign to smart contract for block signer.
This commit is contained in:
commit
20237ddf69
4 changed files with 81 additions and 84 deletions
43
contracts/tomo.go
Normal file
43
contracts/tomo.go
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
@ -184,7 +184,9 @@ 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.
|
// 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 {
|
if eth.chainConfig.Clique != nil {
|
||||||
c := eth.engine.(*clique.Clique)
|
c := eth.engine.(*clique.Clique)
|
||||||
|
|
@ -202,13 +204,13 @@ 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("TOMO - Fail to connect RPC", "error", err)
|
log.Error("Fail to connect RPC", "error", err)
|
||||||
return 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("TOMO - Fail get block signer", "error", err)
|
log.Error("Fail get block signer", "error", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
opts := new(bind.CallOpts)
|
opts := new(bind.CallOpts)
|
||||||
|
|
@ -219,34 +221,30 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
||||||
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)
|
// Get signers in blockSigner smartcontract.
|
||||||
if signer, err := c.RecoverSigner(blockHeader); err != nil {
|
addrs, err := blockSigner.GetSigners(opts, new(big.Int).SetUint64(i))
|
||||||
log.Error("TOMO - Fail recover block signer", "error", err)
|
if err != nil {
|
||||||
|
log.Error("Fail to get signers from smartcontract.", "error", err)
|
||||||
return err
|
return err
|
||||||
} else {
|
|
||||||
_, exist := signers[signer]
|
|
||||||
if exist {
|
|
||||||
signers[signer].Sign++
|
|
||||||
} else {
|
|
||||||
signers[signer] = &rewardLog{1, 0}
|
|
||||||
}
|
}
|
||||||
totalSigner++
|
// Filter duplicate address.
|
||||||
}
|
|
||||||
|
|
||||||
// Get validator in blockSigner smartcontract.
|
|
||||||
addrs, _ := blockSigner.GetSigners(opts, new(big.Int).SetUint64(i))
|
|
||||||
if len(addrs) > 0 {
|
if len(addrs) > 0 {
|
||||||
for j := 0; j < len(addrs); j++ {
|
addrSigners := make(map[common.Address]bool)
|
||||||
_, exist := validators[addrs[j]]
|
for _, addr := range addrs {
|
||||||
if exist {
|
if _, ok := addrSigners[addr]; ok {
|
||||||
validators[addrs[j]].Sign++
|
|
||||||
} else {
|
} else {
|
||||||
validators[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++
|
totalSigner++
|
||||||
}
|
}
|
||||||
|
|
@ -256,6 +254,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 signers.
|
||||||
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))
|
||||||
|
|
@ -263,26 +262,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 {
|
||||||
log.Error("TOMO - Fail to parse json signers", "error", err)
|
log.Error("Fail to parse json signers", "error", err)
|
||||||
return err
|
|
||||||
}
|
|
||||||
jsonValidators, err := json.Marshal(validators)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("TOMO - Fail to parse json validators", "error", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("TOMO - Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber, "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward, "validators", string(jsonValidators))
|
log.Info("Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber, "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -25,13 +25,12 @@ 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/contracts"
|
||||||
"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"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
|
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
|
||||||
"math/big"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -741,45 +740,8 @@ 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) HookCreateTxSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager) {
|
||||||
if chainConfig.Clique != nil {
|
|
||||||
c := engine.(*clique.Clique)
|
|
||||||
|
|
||||||
f.importedHook = func(block *types.Block) {
|
f.importedHook = func(block *types.Block) {
|
||||||
// Find active account.
|
contracts.CreateTransactionSign(chainConfig, pool, manager, block)
|
||||||
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]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get block signer.
|
|
||||||
signer, err := c.RecoverSigner(block.Header())
|
|
||||||
if err != nil {
|
|
||||||
log.Error("TOMO - Fail to get signer", "error", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Not send tx sign when this node is current block miner.
|
|
||||||
if signer != account.Address {
|
|
||||||
// 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("TOMO - Fail to create tx sign", "error", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add tx signed to local tx pool.
|
|
||||||
pool.AddLocal(txSigned)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/consensus"
|
"github.com/ethereum/go-ethereum/consensus"
|
||||||
"github.com/ethereum/go-ethereum/consensus/clique"
|
"github.com/ethereum/go-ethereum/consensus/clique"
|
||||||
"github.com/ethereum/go-ethereum/consensus/misc"
|
"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"
|
||||||
"github.com/ethereum/go-ethereum/core/state"
|
"github.com/ethereum/go-ethereum/core/state"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
|
@ -338,6 +339,11 @@ func (self *worker) wait() {
|
||||||
if mustCommitNewWork {
|
if mustCommitNewWork {
|
||||||
self.commitNewWork()
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue