prysm-pulse/sharding/client_test.go
Preston Van Loon 28f4b2f964 fix tests for to work properly
Former-commit-id: 6aa5738d5a16ac31e3da71f11fba5b16643a105c [formerly 837b547034da7991eaaf4be82e436b74972eccb1]
Former-commit-id: 6b8791f4ca9969f57fc89ac775e5bfda3cb7f518
2018-01-28 15:57:05 -05:00

135 lines
3.5 KiB
Go

package sharding
import (
"context"
"flag"
"fmt"
"math/big"
"math/rand"
"os"
"path"
"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"
)
// FakeEthService based on implementation of internal/ethapi.Client
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) (hexutil.Big, error) {
b := big.NewInt(1000)
return hexutil.Big(*b), nil
}
func (s *FakeEthService) EstimateGas(ctx context.Context, msg interface{}) (hexutil.Uint64, error) {
h := hexutil.Uint64(uint64(1000000))
return h, nil
}
func (s *FakeEthService) GetTransactionCount(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (hexutil.Uint64, error) {
return hexutil.Uint64(uint64(1)), 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
}
type FakeNetworkService struct{}
func (s *FakeNetworkService) Version() (string, error) {
return "100", nil
}
func newTestServer(endpoint string) (*rpc.Server, error) {
// 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
}
if err := server.RegisterName("net", new(FakeNetworkService)); err != nil {
return nil, err
}
l, err := rpc.CreateIPCListener(endpoint + "/geth.ipc")
if err != nil {
return nil, err
}
go server.ServeListener(l)
return server, nil
}
func createContext() *cli.Context {
set := flag.NewFlagSet("test", 0)
set.String(utils.DataDirFlag.Name, "", "")
return cli.NewContext(nil, set, nil)
}
func TestShardingClient(t *testing.T) {
endpoint := path.Join(os.TempDir(), fmt.Sprintf("go-ethereum-test-ipc-%d-%d", os.Getpid(), rand.Int63()))
server, err := newTestServer(endpoint)
if err != nil {
t.Fatalf("Failed to create a test server: %v", err)
}
defer server.Stop()
ctx := createContext()
if err := ctx.GlobalSet(utils.DataDirFlag.Name, endpoint); err != nil {
t.Fatalf("Failed to set global variable for flag %s. Error: %v", utils.DataDirFlag.Name, err)
}
c := MakeShardingClient(ctx)
if err := c.Start(); err != nil {
t.Errorf("Failed to start server: %v", err)
}
}