|
| 1 | +package sharding |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "math/big" |
| 8 | + "math/rand" |
| 9 | + "os" |
| 10 | + "sync" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/ethereum/go-ethereum/accounts/keystore" |
| 14 | + "github.com/ethereum/go-ethereum/core/types" |
| 15 | + |
| 16 | + "github.com/ethereum/go-ethereum/cmd/utils" |
| 17 | + "github.com/ethereum/go-ethereum/common" |
| 18 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 19 | + "github.com/ethereum/go-ethereum/node" |
| 20 | + "github.com/ethereum/go-ethereum/rpc" |
| 21 | + cli "gopkg.in/urfave/cli.v1" |
| 22 | +) |
| 23 | + |
| 24 | +// FakeEthService based on implementation of internal/ethapi.Client |
| 25 | +type FakeEthService struct { |
| 26 | + mu sync.Mutex |
| 27 | + |
| 28 | + getCodeResp hexutil.Bytes |
| 29 | + getCodeErr error |
| 30 | +} |
| 31 | + |
| 32 | +// eth_getCode |
| 33 | +func (s *FakeEthService) GetCode(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (hexutil.Bytes, error) { |
| 34 | + s.mu.Lock() |
| 35 | + defer s.mu.Unlock() |
| 36 | + return s.getCodeResp, s.getCodeErr |
| 37 | +} |
| 38 | + |
| 39 | +// Set return values for eth_getCode |
| 40 | +func (s *FakeEthService) SetGetCode(resp hexutil.Bytes, err error) { |
| 41 | + s.mu.Lock() |
| 42 | + s.getCodeResp = resp |
| 43 | + s.getCodeErr = err |
| 44 | + s.mu.Unlock() |
| 45 | +} |
| 46 | + |
| 47 | +func (s *FakeEthService) GasPrice(ctx context.Context) (*big.Int, error) { |
| 48 | + return big.NewInt(10000), nil |
| 49 | +} |
| 50 | + |
| 51 | +func (s *FakeEthService) GetTransactionCount(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (*hexutil.Uint64, error) { |
| 52 | + return nil, nil |
| 53 | +} |
| 54 | + |
| 55 | +func (s *FakeEthService) SendRawTransaction(ctx context.Context, encodedTx hexutil.Bytes) (common.Hash, error) { |
| 56 | + return common.Hash{}, nil |
| 57 | +} |
| 58 | + |
| 59 | +func (s *FakeEthService) GetTransactionReceipt(hash common.Hash) (*types.Receipt, error) { |
| 60 | + return &types.Receipt{ |
| 61 | + ContractAddress: common.StringToAddress("0x1"), |
| 62 | + Logs: []*types.Log{}, |
| 63 | + }, nil |
| 64 | +} |
| 65 | + |
| 66 | +func (s *FakeEthService) GetTransactionByHash(hash common.Hash) (tx *types.Transaction, isPending bool, err error) { |
| 67 | + return nil, false, nil |
| 68 | +} |
| 69 | + |
| 70 | +type FakeNetworkService struct{} |
| 71 | + |
| 72 | +func (s *FakeNetworkService) Version() (string, error) { |
| 73 | + return "100", nil |
| 74 | +} |
| 75 | + |
| 76 | +func newTestServer(endpoint string) (*rpc.Server, error) { |
| 77 | + // Create datadir. |
| 78 | + if err := os.Mkdir(endpoint, 0777); err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + // Create a default account without password. |
| 83 | + scryptN, scryptP, keydir, err := (&node.Config{DataDir: endpoint}).AccountConfig() |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + if _, err := keystore.StoreKey(keydir, "" /*password*/, scryptN, scryptP); err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + // Create server and register eth service with FakeEthService |
| 92 | + server := rpc.NewServer() |
| 93 | + if err := server.RegisterName("eth", new(FakeEthService)); err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + if err := server.RegisterName("net", new(FakeNetworkService)); err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + l, err := rpc.CreateIPCListener(endpoint + "/geth.ipc") |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + go server.ServeListener(l) |
| 104 | + |
| 105 | + return server, nil |
| 106 | +} |
| 107 | + |
| 108 | +func createContext() *cli.Context { |
| 109 | + set := flag.NewFlagSet("test", 0) |
| 110 | + set.String(utils.DataDirFlag.Name, "", "") |
| 111 | + return cli.NewContext(nil, set, nil) |
| 112 | +} |
| 113 | + |
| 114 | +func TestShardingClient(t *testing.T) { |
| 115 | + endpoint := fmt.Sprintf("%s/go-ethereum-test-ipc-%d-%d", os.TempDir(), os.Getpid(), rand.Int63()) |
| 116 | + server, err := newTestServer(endpoint) |
| 117 | + if err != nil { |
| 118 | + t.Fatalf("Failed to create a test server: %v", err) |
| 119 | + } |
| 120 | + defer server.Stop() |
| 121 | + |
| 122 | + ctx := createContext() |
| 123 | + if err := ctx.GlobalSet(utils.DataDirFlag.Name, endpoint); err != nil { |
| 124 | + t.Fatalf("Failed to set global variable for flag %s. Error: %v", utils.DataDirFlag.Name, err) |
| 125 | + } |
| 126 | + |
| 127 | + c := MakeShardingClient(ctx) |
| 128 | + |
| 129 | + if err := c.Start(); err != nil { |
| 130 | + t.Errorf("Failed to start server: %v", err) |
| 131 | + } |
| 132 | +} |
0 commit comments