Add feature send tx sign (validator block) from coinbase to smart con…

This commit is contained in:
parmarrushabh 2018-07-22 16:39:59 +05:30
parent d7f57b92e3
commit 3f1c3839c9
4 changed files with 57 additions and 15 deletions

View file

@ -30,6 +30,7 @@ import (
const (
HashLength = 32
AddressLength = 20
BlockSigners = "0x0000000000000000000000000000000000000089"
)
var (

View file

@ -107,10 +107,10 @@ var (
type TxStatus uint
const (
TxStatusUnknown TxStatus = iota
TxStatusQueued
TxStatusPending
TxStatusIncluded
TxStatusUnknown TxStatus = iota
TxStatusQueued
TxStatusPending
TxStatusIncluded
)
// blockChain provides the state of blockchain and current gas limit to do
@ -293,11 +293,11 @@ func (pool *TxPool) loop() {
pool.mu.Unlock()
}
// Be unsubscribed due to system stopped
// Be unsubscribed due to system stopped
case <-pool.chainHeadSub.Err():
return
// Handle stats reporting ticks
// Handle stats reporting ticks
case <-report.C:
pool.mu.RLock()
pending, queued := pool.stats()
@ -309,7 +309,7 @@ func (pool *TxPool) loop() {
prevPending, prevQueued, prevStales = pending, queued, stales
}
// Handle inactive account transaction eviction
// Handle inactive account transaction eviction
case <-evict.C:
pool.mu.Lock()
for addr := range pool.queue {
@ -326,7 +326,7 @@ func (pool *TxPool) loop() {
}
pool.mu.Unlock()
// Handle local transaction journal rotation
// Handle local transaction journal rotation
case <-journal.C:
if pool.journal != nil {
pool.mu.Lock()
@ -586,13 +586,17 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
if pool.currentState.GetBalance(from).Cmp(tx.Cost()) < 0 {
return ErrInsufficientFunds
}
intrGas, err := IntrinsicGas(tx.Data(), tx.To() == nil, pool.homestead)
if err != nil {
return err
}
if tx.Gas() < intrGas {
return ErrIntrinsicGas
if tx.To().String() != common.BlockSigners {
intrGas, err := IntrinsicGas(tx.Data(), tx.To() == nil, pool.homestead)
if err != nil {
return err
}
// Exclude check smart contract sign address.
if tx.Gas() < intrGas {
return ErrIntrinsicGas
}
}
return nil
}
@ -611,6 +615,7 @@ func (pool *TxPool) add(tx *types.Transaction, local bool) (bool, error) {
log.Trace("Discarding already known transaction", "hash", hash)
return false, fmt.Errorf("known transaction: %x", hash)
}
// If the transaction fails basic validation, discard it
if err := pool.validateTx(tx, local); err != nil {
log.Trace("Discarding invalid transaction", "hash", hash, "err", err)
@ -1160,4 +1165,4 @@ func (as *accountSet) containsTx(tx *types.Transaction) bool {
// add inserts a new address into the set to track.
func (as *accountSet) add(addr common.Address) {
as.accounts[addr] = struct{}{}
}
}

View file

@ -178,6 +178,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)
return eth, nil
}

View file

@ -27,6 +27,10 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/accounts"
"math/big"
"github.com/ethereum/go-ethereum/core"
)
const (
@ -734,3 +738,32 @@ func (f *Fetcher) forgetBlock(hash common.Hash) {
delete(f.queued, 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) {
f.importedHook = func(block *types.Block) {
// Find active account.
account := accounts.Account{}
var wallet accounts.Wallet
if account == (accounts.Account{}) {
if wallets := manager.Wallets(); len(wallets) > 0 {
wallet = wallets[0]
if accounts := wallets[0].Accounts(); len(accounts) > 0 {
account = accounts[0]
}
}
}
// Create and send tx to smartcontract for sign validate block.
data := common.Hex2Bytes("2fb1b25f")
nonce := pool.State().GetNonce(account.Address)
tx := types.NewTransaction(nonce, common.HexToAddress(common.BlockSigners), big.NewInt(0), 100000, big.NewInt(0), data)
txSigned, err := wallet.SignTx(account, tx, chainConfig.ChainId)
if err != nil {
log.Error("TOMO - Fail to create tx sign", "error", err)
}
// Add tx signed to local tx pool.
pool.AddLocal(txSigned)
}
}