Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value Transfer in Private Transactions #538

Merged
merged 2 commits into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,8 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
return ErrNonceTooLow
}
// Ether value is not currently supported on private transactions
if tx.IsPrivate() && (tx.Value().Sign() != 0) {
return ErrEtherValueUnsupported;
if tx.IsPrivate() && (len(tx.Data()) == 0 || tx.Value().Sign() != 0) {
return ErrEtherValueUnsupported
}
// Transactor should have enough funds to cover the costs
// cost == V + GP * GL
Expand Down
66 changes: 57 additions & 9 deletions core/tx_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ package core
import (
"crypto/ecdsa"
"fmt"
"io/ioutil"
"math/big"
"math/rand"
"os"
"reflect"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/params"
"math/rand"
"time"
"io/ioutil"
"os"
"reflect"
)

// testTxPoolConfig is a transaction pool configuration without stateful disk
Expand Down Expand Up @@ -281,6 +282,54 @@ func TestQuorumInvalidTransactions(t *testing.T) {

}

func TestValidateTx_whenValueZeroTransferForPrivateTransaction(t *testing.T) {
pool, key := setupQuorumTxPool()
defer pool.Stop()
zeroValue := common.Big0
zeroGasPrice := common.Big0
defaultTxPoolGasLimit := big.NewInt(1000000)
arbitraryTx, _ := types.SignTx(types.NewTransaction(0, common.Address{}, zeroValue, defaultTxPoolGasLimit, zeroGasPrice, nil), types.HomesteadSigner{}, key)
arbitraryTx.SetPrivate()

if err := pool.AddRemote(arbitraryTx); err != ErrEtherValueUnsupported {
t.Error("expected:", ErrEtherValueUnsupported, "; got:", err)
}
}

func TestValidateTx_whenValueNonZeroTransferForPrivateTransaction(t *testing.T) {
pool, key := setupQuorumTxPool()
defer pool.Stop()
arbitraryValue := common.Big3
arbitraryTx, balance, from := newPrivateTransaction(arbitraryValue, nil, key)
pool.currentState.AddBalance(from, balance)

if err := pool.AddRemote(arbitraryTx); err != ErrEtherValueUnsupported {
t.Error("expected: ", ErrEtherValueUnsupported, "; got:", err)
}
}

func newPrivateTransaction(value *big.Int, data []byte, key *ecdsa.PrivateKey) (*types.Transaction, *big.Int, common.Address) {
zeroGasPrice := common.Big0
defaultTxPoolGasLimit := big.NewInt(1000000)
arbitraryTx, _ := types.SignTx(types.NewTransaction(0, common.Address{}, value, defaultTxPoolGasLimit, zeroGasPrice, data), types.HomesteadSigner{}, key)
arbitraryTx.SetPrivate()
balance := new(big.Int).Add(arbitraryTx.Value(), new(big.Int).Mul(arbitraryTx.Gas(), arbitraryTx.GasPrice()))
from, _ := deriveSender(arbitraryTx)
return arbitraryTx, balance, from
}

func TestValidateTx_whenValueNonZeroWithSmartContractForPrivateTransaction(t *testing.T) {
pool, key := setupQuorumTxPool()
defer pool.Stop()
arbitraryValue := common.Big3
arbitraryTx, balance, from := newPrivateTransaction(arbitraryValue, []byte("arbitrary bytecode"), key)
pool.currentState.AddBalance(from, balance)

if err := pool.AddRemote(arbitraryTx); err != ErrEtherValueUnsupported {
t.Error("expected: ", ErrEtherValueUnsupported, "; got:", err)
}
}

func TestTransactionQueue(t *testing.T) {
pool, key := setupTxPool()
defer pool.Stop()
Expand Down Expand Up @@ -1527,9 +1576,9 @@ func benchmarkPoolBatchInsert(b *testing.B, size int) {
//Checks that the EIP155 signer is assigned to the TxPool no matter the configuration, even invalid config
func TestEIP155SignerOnTxPool(t *testing.T) {
var flagtests = []struct {
name string
homesteadBlock *big.Int
eip155Block *big.Int
name string
homesteadBlock *big.Int
eip155Block *big.Int
}{
{"hsnileip155nil", nil, nil},
{"hsnileip1550", nil, big.NewInt(0)},
Expand Down Expand Up @@ -1567,4 +1616,3 @@ func TestEIP155SignerOnTxPool(t *testing.T) {
}

}

33 changes: 20 additions & 13 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,14 @@ func (s *PrivateAccountAPI) SendTransaction(ctx context.Context, args SendTxArgs
data := []byte(args.Data)
isPrivate := args.PrivateFor != nil
if isPrivate {
log.Info("sending private tx", "data", fmt.Sprintf("%x", data), "privatefrom", args.PrivateFrom, "privatefor", args.PrivateFor)
data, err = private.P.Send(data, args.PrivateFrom, args.PrivateFor)
log.Info("sent private tx", "data", fmt.Sprintf("%x", data), "privatefrom", args.PrivateFrom, "privatefor", args.PrivateFor)
if err != nil {
return common.Hash{}, err
}
if len(data) > 0 {
log.Info("sending private tx", "data", fmt.Sprintf("%x", data), "privatefrom", args.PrivateFrom, "privatefor", args.PrivateFor)
data, err = private.P.Send(data, args.PrivateFrom, args.PrivateFor)
log.Info("sent private tx", "data", fmt.Sprintf("%x", data), "privatefrom", args.PrivateFrom, "privatefor", args.PrivateFor)
if err != nil {
return common.Hash{}, err
}
} // else tx_pool.go#validateTx will capture and throw error
args.Data = data
}

Expand Down Expand Up @@ -1152,13 +1154,15 @@ func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args Sen
isPrivate := args.PrivateFor != nil

if isPrivate {
//Send private transaction to local Constellation node
log.Info("sending private tx", "data", fmt.Sprintf("%x", data), "privatefrom", args.PrivateFrom, "privatefor", args.PrivateFor)
data, err = private.P.Send(data, args.PrivateFrom, args.PrivateFor)
log.Info("sent private tx", "data", fmt.Sprintf("%x", data), "privatefrom", args.PrivateFrom, "privatefor", args.PrivateFor)
if err != nil {
return common.Hash{}, err
}
if len(data) > 0 {
//Send private transaction to local Constellation node
log.Info("sending private tx", "data", fmt.Sprintf("%x", data), "privatefrom", args.PrivateFrom, "privatefor", args.PrivateFor)
data, err = private.P.Send(data, args.PrivateFrom, args.PrivateFor)
log.Info("sent private tx", "data", fmt.Sprintf("%x", data), "privatefrom", args.PrivateFrom, "privatefor", args.PrivateFor)
if err != nil {
return common.Hash{}, err
}
} // else tx_pool.go#validateTx will capture and throw error
args.Data = data
}

Expand Down Expand Up @@ -1239,6 +1243,9 @@ func (s *PublicTransactionPoolAPI) SignTransaction(ctx context.Context, args Sen
if err != nil {
return nil, err
}
if args.PrivateFor != nil {
tx.SetPrivate()
}
data, err := rlp.EncodeToBytes(tx)
if err != nil {
return nil, err
Expand Down