mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-19 19:30:44 +00:00
Add feature send tx sign (validator block) from coinbase to smart con…
This commit is contained in:
parent
d7f57b92e3
commit
3f1c3839c9
4 changed files with 57 additions and 15 deletions
|
|
@ -30,6 +30,7 @@ import (
|
||||||
const (
|
const (
|
||||||
HashLength = 32
|
HashLength = 32
|
||||||
AddressLength = 20
|
AddressLength = 20
|
||||||
|
BlockSigners = "0x0000000000000000000000000000000000000089"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
||||||
|
|
@ -586,13 +586,17 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
|
||||||
if pool.currentState.GetBalance(from).Cmp(tx.Cost()) < 0 {
|
if pool.currentState.GetBalance(from).Cmp(tx.Cost()) < 0 {
|
||||||
return ErrInsufficientFunds
|
return ErrInsufficientFunds
|
||||||
}
|
}
|
||||||
|
if tx.To().String() != common.BlockSigners {
|
||||||
intrGas, err := IntrinsicGas(tx.Data(), tx.To() == nil, pool.homestead)
|
intrGas, err := IntrinsicGas(tx.Data(), tx.To() == nil, pool.homestead)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// Exclude check smart contract sign address.
|
||||||
if tx.Gas() < intrGas {
|
if tx.Gas() < intrGas {
|
||||||
return ErrIntrinsicGas
|
return ErrIntrinsicGas
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
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)
|
log.Trace("Discarding already known transaction", "hash", hash)
|
||||||
return false, fmt.Errorf("known transaction: %x", hash)
|
return false, fmt.Errorf("known transaction: %x", hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the transaction fails basic validation, discard it
|
// If the transaction fails basic validation, discard it
|
||||||
if err := pool.validateTx(tx, local); err != nil {
|
if err := pool.validateTx(tx, local); err != nil {
|
||||||
log.Trace("Discarding invalid transaction", "hash", hash, "err", err)
|
log.Trace("Discarding invalid transaction", "hash", hash, "err", err)
|
||||||
|
|
|
||||||
|
|
@ -178,6 +178,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.
|
||||||
|
eth.protocolManager.fetcher.CreateTransactionSign(eth.chainConfig, eth.txPool, eth.accountManager)
|
||||||
|
|
||||||
return eth, nil
|
return eth, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,10 @@ import (
|
||||||
"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"
|
||||||
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
|
"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 (
|
const (
|
||||||
|
|
@ -734,3 +738,32 @@ func (f *Fetcher) forgetBlock(hash common.Hash) {
|
||||||
delete(f.queued, 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue