Fix TestPrivateTransaction to actually create private transactions (#144)

It now creates private transactions, but that means it now needs to run
constellation in a subprocess, so do that as well
This commit is contained in:
Guilherme Salgado 2017-08-01 13:54:29 +01:00 committed by Patrick Mylund Nielsen
parent 42852c31fa
commit 2ea6e55def
6 changed files with 78 additions and 13 deletions

View File

@ -12,15 +12,6 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
)
// privateTestTx stubs transaction so that it can be flagged as private or not
type privateTestTx struct {
*types.Transaction
private bool
}
// IsPrivate returns whether the transaction should be considered privat.
func (ptx *privateTestTx) IsPrivate() bool { return ptx.private }
// callHelper makes it easier to do proper calls and use the state transition object.
// It also manages the nonces of the caller and keeps private and public state, which
// can be freely modified outside of the helper.
@ -44,10 +35,9 @@ func (cg *callHelper) TxNonce(addr common.Address) uint64 {
func (cg *callHelper) MakeCall(private bool, key *ecdsa.PrivateKey, to common.Address, input []byte) error {
var (
from = crypto.PubkeyToAddress(key.PublicKey)
ptx = privateTestTx{private: private}
err error
)
ptx.Transaction, err = types.NewTransaction(cg.TxNonce(from), to, new(big.Int), big.NewInt(1000000), new(big.Int), input).SignECDSA(key)
tx, err := types.NewTransaction(cg.TxNonce(from), to, new(big.Int), big.NewInt(1000000), new(big.Int), input).SignECDSA(key)
if err != nil {
return err
}
@ -56,10 +46,12 @@ func (cg *callHelper) MakeCall(private bool, key *ecdsa.PrivateKey, to common.Ad
publicState, privateState := cg.PublicState, cg.PrivateState
if !private {
privateState = publicState
} else {
tx.SetPrivate()
}
cg.header.Number = new(big.Int)
_, _, err = ApplyMessage(NewEnv(publicState, privateState, &ChainConfig{}, nil, ptx.Transaction, &cg.header, vm.Config{}), ptx, cg.gp)
_, _, err = ApplyMessage(NewEnv(publicState, privateState, &ChainConfig{}, nil, tx, &cg.header, vm.Config{}), tx, cg.gp)
if err != nil {
return err
}

View File

@ -0,0 +1 @@
{"data":{"bytes":"Wl+xSyXVuuqzpvznOS7dOobhcn4C5auxkFRi7yLtgtA="},"type":"unlocked"}

View File

@ -0,0 +1 @@
BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=

View File

@ -0,0 +1 @@
{"data":{"bytes":"wGEar7J9G0JAgdisp61ZChyrJWeW2QPyKvecjjeVHOY="},"type":"unlocked"}

View File

@ -0,0 +1 @@
8SjRHlUBe4hAmTk3KDeJ96RhN+s10xRrHDrxEi1O5W0=

View File

@ -1,12 +1,21 @@
package core
import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"math/big"
"os"
osExec "os/exec"
"path"
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/private"
"github.com/ethereum/go-ethereum/private/constellation"
)
func ExampleMakeCallHelper() {
@ -43,6 +52,60 @@ func ExampleMakeCallHelper() {
fmt.Println("Public:", helper.PublicState.GetState(pubContractAddr, common.Hash{}).Big())
}
var constellationCfgTemplate = template.Must(template.New("t").Parse(`
url = "http://127.0.0.1:9000/"
port = 9000
socketPath = "{{.RootDir}}/qdata/tm1.ipc"
otherNodeUrls = []
publicKeyPath = "{{.RootDir}}/keys/tm1.pub"
privateKeyPath = "{{.RootDir}}/keys/tm1.key"
archivalPublicKeyPath = "{{.RootDir}}/keys/tm1a.pub"
archivalPrivateKeyPath = "{{.RootDir}}/keys/tm1a.key"
storagePath = "{{.RootDir}}/qdata/constellation1"
`))
func runConstellation() (*osExec.Cmd, error) {
dir, err := ioutil.TempDir("", "TestPrivateTxConstellationData")
if err != nil {
return nil, err
}
defer os.RemoveAll(dir)
here, err := os.Getwd()
if err != nil {
return nil, err
}
if err = os.MkdirAll(path.Join(dir, "qdata"), 0755); err != nil {
return nil, err
}
if err = os.Symlink(path.Join(here, "constellation-test-keys"), path.Join(dir, "keys")); err != nil {
return nil, err
}
cfgFile, err := os.Create(path.Join(dir, "constellation.cfg"))
if err != nil {
return nil, err
}
err = constellationCfgTemplate.Execute(cfgFile, map[string]string{"RootDir": dir})
if err != nil {
return nil, err
}
constellationCmd := osExec.Command("constellation-node", cfgFile.Name())
var stdout, stderr bytes.Buffer
constellationCmd.Stdout = &stdout
constellationCmd.Stderr = &stderr
var constellationErr error
go func() {
constellationErr = constellationCmd.Start()
}()
// Give the constellation subprocess some time to start.
time.Sleep(1 * time.Second)
if constellationErr != nil {
fmt.Println(stdout.String() + stderr.String())
return nil, constellationErr
}
private.P = constellation.MustNew(cfgFile.Name())
return constellationCmd, nil
}
func TestPrivateTransaction(t *testing.T) {
var (
key, _ = crypto.GenerateKey()
@ -51,6 +114,12 @@ func TestPrivateTransaction(t *testing.T) {
publicState = helper.PublicState
)
constellationCmd, err := runConstellation()
if err != nil {
t.Fatal(err)
}
defer constellationCmd.Process.Kill()
prvContractAddr := common.Address{1}
pubContractAddr := common.Address{2}
privateState.SetCode(prvContractAddr, common.Hex2Bytes("600a600055600060006001a1"))
@ -59,7 +128,7 @@ func TestPrivateTransaction(t *testing.T) {
publicState.SetState(pubContractAddr, common.Hash{}, common.Hash{19})
// Private transaction 1
err := helper.MakeCall(true, key, prvContractAddr, nil)
err = helper.MakeCall(true, key, prvContractAddr, nil)
if err != nil {
t.Fatal(err)
}