accounts/abi, ethclient: Allow sending transactions from unlocked accounts via RPC

This commit is contained in:
Maxwell Koo 2017-10-20 11:51:01 -04:00
parent 479aa61f11
commit c2295074a7
4 changed files with 59 additions and 10 deletions

View file

@ -88,6 +88,8 @@ type ContractTransactor interface {
EstimateGas(ctx context.Context, call ethereum.CallMsg) (usedGas *big.Int, err error)
// SendTransaction injects the transaction into the pending pool for execution.
SendTransaction(ctx context.Context, tx *types.Transaction) error
// SendUnsignedTransaction injects an unsigned transaction into the pending pool for execution.
SendUnsignedTransaction(ctx context.Context, tx *types.Transaction, sender common.Address) error
}
// ContractBackend defines the methods needed to work with contracts on a read-write basis.

View file

@ -287,6 +287,28 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa
return nil
}
// SendUnsignedTransaction updates the pending block to include the given raw transaction.
// It panics if the transaction is invalid.
func (b *SimulatedBackend) SendUnsignedTransaction(ctx context.Context, tx *types.Transaction, sender common.Address) error {
b.mu.Lock()
defer b.mu.Unlock()
nonce := b.pendingState.GetNonce(sender)
if tx.Nonce() != nonce {
panic(fmt.Errorf("invalid transaction nonce: got %d, want %d", tx.Nonce(), nonce))
}
blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), b.database, 1, func(number int, block *core.BlockGen) {
for _, tx := range b.pendingBlock.Transactions() {
block.AddTx(tx)
}
block.AddTx(tx)
})
b.pendingBlock = blocks[0]
b.pendingState, _ = state.New(b.pendingBlock.Root(), state.NewDatabase(b.database))
return nil
}
// JumpTimeInSeconds adds skip seconds to the clock
func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error {
b.mu.Lock()

View file

@ -18,7 +18,6 @@ package bind
import (
"context"
"errors"
"fmt"
"math/big"
@ -213,16 +212,20 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i
rawTx = types.NewTransaction(nonce, c.address, value, gasLimit, gasPrice, input)
}
if opts.Signer == nil {
return nil, errors.New("no signer to authorize the transaction with")
if err := c.transactor.SendUnsignedTransaction(ensureContext(opts.Context), rawTx, opts.From); err != nil {
return nil, err
}
return rawTx, nil
} else {
signedTx, err := opts.Signer(types.HomesteadSigner{}, opts.From, rawTx)
if err != nil {
return nil, err
}
if err := c.transactor.SendTransaction(ensureContext(opts.Context), signedTx); err != nil {
return nil, err
}
return signedTx, nil
}
signedTx, err := opts.Signer(types.HomesteadSigner{}, opts.From, rawTx)
if err != nil {
return nil, err
}
if err := c.transactor.SendTransaction(ensureContext(opts.Context), signedTx); err != nil {
return nil, err
}
return signedTx, nil
}
func ensureContext(ctx context.Context) context.Context {

View file

@ -476,6 +476,12 @@ func (ec *Client) SendTransaction(ctx context.Context, tx *types.Transaction) er
return ec.c.CallContext(ctx, nil, "eth_sendRawTransaction", common.ToHex(data))
}
// SendUnsignedTransaction injects an unsigned transaction into the pending pool
// for execution, depending on the RPC server to calculate the signature.
func (ec *Client) SendUnsignedTransaction(ctx context.Context, tx *types.Transaction, sender common.Address) error {
return ec.c.CallContext(ctx, nil, "eth_sendTransaction", toTransactArg(tx, sender))
}
func toCallArg(msg ethereum.CallMsg) interface{} {
arg := map[string]interface{}{
"from": msg.From,
@ -495,3 +501,19 @@ func toCallArg(msg ethereum.CallMsg) interface{} {
}
return arg
}
func toTransactArg(tx *types.Transaction, sender common.Address) interface{} {
arg := map[string]interface{}{
"from": sender,
"to": tx.To(),
"value": (*hexutil.Big)(tx.Value()),
"gas": (*hexutil.Big)(tx.Gas()),
"gasPrice": (*hexutil.Big)(tx.GasPrice()),
}
if len(tx.Data()) > 0 {
arg["data"] = hexutil.Bytes(tx.Data())
}
return arg
}