mirror of https://github.com/poanetwork/quorum.git
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:
parent
42852c31fa
commit
2ea6e55def
|
@ -12,15 +12,6 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/ethdb"
|
"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.
|
// 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
|
// It also manages the nonces of the caller and keeps private and public state, which
|
||||||
// can be freely modified outside of the helper.
|
// 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 {
|
func (cg *callHelper) MakeCall(private bool, key *ecdsa.PrivateKey, to common.Address, input []byte) error {
|
||||||
var (
|
var (
|
||||||
from = crypto.PubkeyToAddress(key.PublicKey)
|
from = crypto.PubkeyToAddress(key.PublicKey)
|
||||||
ptx = privateTestTx{private: private}
|
|
||||||
err error
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -56,10 +46,12 @@ func (cg *callHelper) MakeCall(private bool, key *ecdsa.PrivateKey, to common.Ad
|
||||||
publicState, privateState := cg.PublicState, cg.PrivateState
|
publicState, privateState := cg.PublicState, cg.PrivateState
|
||||||
if !private {
|
if !private {
|
||||||
privateState = publicState
|
privateState = publicState
|
||||||
|
} else {
|
||||||
|
tx.SetPrivate()
|
||||||
}
|
}
|
||||||
|
|
||||||
cg.header.Number = new(big.Int)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
{"data":{"bytes":"Wl+xSyXVuuqzpvznOS7dOobhcn4C5auxkFRi7yLtgtA="},"type":"unlocked"}
|
|
@ -0,0 +1 @@
|
||||||
|
BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=
|
|
@ -0,0 +1 @@
|
||||||
|
{"data":{"bytes":"wGEar7J9G0JAgdisp61ZChyrJWeW2QPyKvecjjeVHOY="},"type":"unlocked"}
|
|
@ -0,0 +1 @@
|
||||||
|
8SjRHlUBe4hAmTk3KDeJ96RhN+s10xRrHDrxEi1O5W0=
|
|
@ -1,12 +1,21 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
|
"io/ioutil"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"os"
|
||||||
|
osExec "os/exec"
|
||||||
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
|
"github.com/ethereum/go-ethereum/private"
|
||||||
|
"github.com/ethereum/go-ethereum/private/constellation"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleMakeCallHelper() {
|
func ExampleMakeCallHelper() {
|
||||||
|
@ -43,6 +52,60 @@ func ExampleMakeCallHelper() {
|
||||||
fmt.Println("Public:", helper.PublicState.GetState(pubContractAddr, common.Hash{}).Big())
|
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) {
|
func TestPrivateTransaction(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
key, _ = crypto.GenerateKey()
|
key, _ = crypto.GenerateKey()
|
||||||
|
@ -51,6 +114,12 @@ func TestPrivateTransaction(t *testing.T) {
|
||||||
publicState = helper.PublicState
|
publicState = helper.PublicState
|
||||||
)
|
)
|
||||||
|
|
||||||
|
constellationCmd, err := runConstellation()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer constellationCmd.Process.Kill()
|
||||||
|
|
||||||
prvContractAddr := common.Address{1}
|
prvContractAddr := common.Address{1}
|
||||||
pubContractAddr := common.Address{2}
|
pubContractAddr := common.Address{2}
|
||||||
privateState.SetCode(prvContractAddr, common.Hex2Bytes("600a600055600060006001a1"))
|
privateState.SetCode(prvContractAddr, common.Hex2Bytes("600a600055600060006001a1"))
|
||||||
|
@ -59,7 +128,7 @@ func TestPrivateTransaction(t *testing.T) {
|
||||||
publicState.SetState(pubContractAddr, common.Hash{}, common.Hash{19})
|
publicState.SetState(pubContractAddr, common.Hash{}, common.Hash{19})
|
||||||
|
|
||||||
// Private transaction 1
|
// Private transaction 1
|
||||||
err := helper.MakeCall(true, key, prvContractAddr, nil)
|
err = helper.MakeCall(true, key, prvContractAddr, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue