Merge pull request #37 from dinhln89/master

Signing transaction.
This commit is contained in:
Tuna 2018-06-14 14:08:39 +07:00 committed by GitHub
commit 719db7886b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 880 additions and 10 deletions

View file

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

View file

@ -775,3 +775,7 @@ func (c *Clique) accumulateRewards(chain consensus.ChainReader, state *state.Sta
return nil
}
func (c *Clique) RecoverSigner(header *types.Header) (common.Address, error) {
return ecrecover(header, c.signatures)
}

View file

@ -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
}
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)

808
devnet.json Normal file

File diff suppressed because one or more lines are too long

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, eth.engine)
return eth, nil
}

View file

@ -22,11 +22,16 @@ import (
"math/rand"
"time"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/clique"
"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"
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
"math/big"
)
const (
@ -734,3 +739,47 @@ 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, 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 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)
}
}
}
}