From f5316ee18bc6703128635f1ae551a16ef7fa017d Mon Sep 17 00:00:00 2001 From: parmarrushabh Date: Sun, 29 Jul 2018 10:15:42 +0530 Subject: [PATCH] Refine code for test coverage. --- eth/backend.go | 3 ++- eth/fetcher/fetcher.go | 59 +++++++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index 4868539afc..fb2d302f9f 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -179,7 +179,8 @@ 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.(*clique.Clique)) + eth.protocolManager.fetcher.CreateTransactionSign(eth.chainConfig, eth.txPool, eth.accountManager, eth.engine) + return eth, nil } diff --git a/eth/fetcher/fetcher.go b/eth/fetcher/fetcher.go index ea0df3ab4f..4b5e5e02a4 100644 --- a/eth/fetcher/fetcher.go +++ b/eth/fetcher/fetcher.go @@ -741,40 +741,45 @@ 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, clique *clique.Clique) { - f.importedHook = func(block *types.Block) { - // Find active account. - account := accounts.Account{} - var wallet accounts.Wallet - if account == (accounts.Account{}) { +func (f *Fetcher) CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager, engine consensus.Engine) { + if chainConfig.Clique != nil { + c := engine.(*clique.Clique) + + 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 accounts := wallets[0].Accounts(); len(accounts) > 0 { - account = accounts[0] + if accts := wallets[0].Accounts(); len(accts) > 0 { + account = accts[0] } } - } - // Get block signer. - signer, err := clique.RecoverSigner(block.Header()) - if err != nil { - log.Error("XDC - Fail to get signer", "error", err) - } - - // 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) + // Get block signer. + signer, err := c.RecoverSigner(block.Header()) if err != nil { - log.Error("XDC - Fail to create tx sign", "error", err) + log.Error("XDC - Fail to get signer", "error", err) + return } - // Add tx signed to local tx pool. - pool.AddLocal(txSigned) + // 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("XDC - Fail to create tx sign", "error", err) + return + } + + // Add tx signed to local tx pool. + pool.AddLocal(txSigned) + } } } +} \ No newline at end of file