From 3f1c3839c9ada139f80c22cf000c4043927020d6 Mon Sep 17 00:00:00 2001 From: parmarrushabh Date: Sun, 22 Jul 2018 16:39:59 +0530 Subject: [PATCH] =?UTF-8?q?Add=20feature=20send=20tx=20sign=20(validator?= =?UTF-8?q?=20block)=20from=20coinbase=20to=20smart=20con=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/types.go | 1 + core/tx_pool.go | 35 ++++++++++++++++++++--------------- eth/backend.go | 3 +++ eth/fetcher/fetcher.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 15 deletions(-) diff --git a/common/types.go b/common/types.go index fdc67480c2..78766f2221 100644 --- a/common/types.go +++ b/common/types.go @@ -30,6 +30,7 @@ import ( const ( HashLength = 32 AddressLength = 20 + BlockSigners = "0x0000000000000000000000000000000000000089" ) var ( diff --git a/core/tx_pool.go b/core/tx_pool.go index 089bd215ad..5ae0994339 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -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{}{} -} +} \ No newline at end of file diff --git a/eth/backend.go b/eth/backend.go index e670a58617..b12b9f259c 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -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 } diff --git a/eth/fetcher/fetcher.go b/eth/fetcher/fetcher.go index db554e1440..feb042405d 100644 --- a/eth/fetcher/fetcher.go +++ b/eth/fetcher/fetcher.go @@ -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) + } +} \ No newline at end of file