From 1061ade1f82d1873cda396a72d6613898da39419 Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Sat, 20 Jan 2018 12:01:05 -0500 Subject: [PATCH] Working on tests for sharding client Former-commit-id: 71665407b28bbedc5d0fdf245d157ccdb373202f [formerly e37561747705f94c22f812528eb93abffe398b1e] Former-commit-id: d805b464fdbee525e935543dc6d252962d634ecf --- sharding/client.go | 2 +- sharding/client_test.go | 81 ++++++++++++++++++++++++++++++++++++++--- sharding/vmc.go | 4 +- 3 files changed, 78 insertions(+), 9 deletions(-) diff --git a/sharding/client.go b/sharding/client.go index 3763ad21f..ef919cf84 100644 --- a/sharding/client.go +++ b/sharding/client.go @@ -34,7 +34,7 @@ func MakeShardingClient(ctx *cli.Context) *Client { endpoint := fmt.Sprintf("%s/geth.ipc", path) config := &node.Config{ - DataDir: "/tmp/ethereum", + DataDir: path, } scryptN, scryptP, keydir, err := config.AccountConfig() if err != nil { diff --git a/sharding/client_test.go b/sharding/client_test.go index 10badf244..85f67db82 100644 --- a/sharding/client_test.go +++ b/sharding/client_test.go @@ -1,25 +1,93 @@ package sharding import ( + "context" "flag" "fmt" + "math/big" "math/rand" "os" + "sync" "testing" + "github.com/ethereum/go-ethereum/accounts/keystore" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/cmd/utils" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/rpc" cli "gopkg.in/urfave/cli.v1" ) -func randomEndpoint() string { - return fmt.Sprintf("/tmp/go-ethereum-test-ipc-%d-%d", os.Getpid(), rand.Int63()) +// FakeEthService based on implementation of internal/ethapi +type FakeEthService struct { + mu sync.Mutex + + getCodeResp hexutil.Bytes + getCodeErr error +} + +// eth_getCode +func (s *FakeEthService) GetCode(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (hexutil.Bytes, error) { + s.mu.Lock() + defer s.mu.Unlock() + return s.getCodeResp, s.getCodeErr +} + +// Set return values for eth_getCode +func (s *FakeEthService) SetGetCode(resp hexutil.Bytes, err error) { + s.mu.Lock() + s.getCodeResp = resp + s.getCodeErr = err + s.mu.Unlock() +} + +func (s *FakeEthService) GasPrice(ctx context.Context) (*big.Int, error) { + return big.NewInt(10000), nil +} + +func (s *FakeEthService) GetTransactionCount(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (*hexutil.Uint64, error) { + return nil, nil +} + +func (s *FakeEthService) SendRawTransaction(ctx context.Context, encodedTx hexutil.Bytes) (common.Hash, error) { + return common.Hash{}, nil +} + +func (s *FakeEthService) GetTransactionReceipt(hash common.Hash) (*types.Receipt, error) { + return &types.Receipt{ + ContractAddress: common.StringToAddress("0x1"), + Logs: []*types.Log{}, + }, nil +} + +func (s *FakeEthService) GetTransactionByHash(hash common.Hash) (tx *types.Transaction, isPending bool, err error) { + return nil, false, nil } func newTestServer(endpoint string) (*rpc.Server, error) { - server := rpc.NewServer() + // Create datadir. + if err := os.Mkdir(endpoint, 0777); err != nil { + return nil, err + } - l, err := rpc.CreateIPCListener(endpoint) + // Create a default account without password. + scryptN, scryptP, keydir, err := (&node.Config{DataDir: endpoint}).AccountConfig() + if err != nil { + return nil, err + } + if _, err := keystore.StoreKey(keydir, "" /*password*/, scryptN, scryptP); err != nil { + return nil, err + } + + // Create server and register eth service with FakeEthService + server := rpc.NewServer() + if err := server.RegisterName("eth", new(FakeEthService)); err != nil { + return nil, err + } + l, err := rpc.CreateIPCListener(endpoint + "/geth.ipc") if err != nil { return nil, err } @@ -34,8 +102,8 @@ func createContext() *cli.Context { return cli.NewContext(nil, set, nil) } -func TestStart(t *testing.T) { - endpoint := randomEndpoint() +func TestShardingClient(t *testing.T) { + endpoint := fmt.Sprintf("%s/go-ethereum-test-ipc-%d-%d", os.TempDir(), os.Getpid(), rand.Int63()) server, err := newTestServer(endpoint) if err != nil { t.Fatalf("Failed to create a test server: %v", err) @@ -48,6 +116,7 @@ func TestStart(t *testing.T) { } c := MakeShardingClient(ctx) + if err := c.Start(); err != nil { t.Errorf("Failed to start server: %v", err) } diff --git a/sharding/vmc.go b/sharding/vmc.go index 7ff2f0c77..5425a0904 100644 --- a/sharding/vmc.go +++ b/sharding/vmc.go @@ -80,8 +80,8 @@ func (c *Client) deployVMC() (*common.Address, error) { return nil, fmt.Errorf("unable to get nonce for %s: %v", accounts[0].Address, err) } - tx := types.NewContractCreation(nonce, new(big.Int).SetInt64(0), contractGasLimit, suggestedGasPrice, abiBytecode) - signed, err := c.keystore.SignTx(accounts[0], tx, new(big.Int).SetInt64(1000)) + tx := types.NewContractCreation(nonce, new(big.Int).SetInt64(0) /*amount*/, contractGasLimit, suggestedGasPrice, abiBytecode) + signed, err := c.keystore.SignTx(accounts[0], tx, new(big.Int).SetInt64(1000) /*chainId*/) // TODO(prestonvanloon): reference chain ID from flag if err != nil { return nil, fmt.Errorf("unable to sign transaction: %v", err) }