Skip to content

Commit

Permalink
Unexport interface methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed Apr 27, 2021
1 parent 9484b23 commit f0ae5f1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
24 changes: 12 additions & 12 deletions server/asset/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func init() {
const (
assetName = "eth"
coinIDSize = 52
// The blockPollInterval is the delay between calls to BestBlockHash to
// The blockPollInterval is the delay between calls to bestBlockHash to
// check for new blocks.
blockPollInterval = time.Second
)
Expand All @@ -49,10 +49,10 @@ func (d *Driver) DecodeCoinID(coinID []byte) (string, error) {
// ethFetcher represents a blockchain information fetcher. In practice, it is
// satisfied by rpcclient. For testing, it can be satisfied by a stub.
type ethFetcher interface {
Shutdown()
Connect(ctx context.Context, IPC string) error
BestBlockHash(ctx context.Context) (common.Hash, error)
Block(ctx context.Context, hash common.Hash) (*types.Block, error)
shutdown()
connect(ctx context.Context, IPC string) error
bestBlockHash(ctx context.Context) (common.Hash, error)
block(ctx context.Context, hash common.Hash) (*types.Block, error)
}

// Backend is an asset backend for Ethereum. It has methods for fetching output
Expand Down Expand Up @@ -99,24 +99,24 @@ func NewBackend(ipc string, logger dex.Logger, network dex.Network) (*Backend, e
}

func (eth *Backend) shutdown() {
eth.node.Shutdown()
eth.node.shutdown()
}

// Connect connects to the node RPC server. A dex.Connector.
func (eth *Backend) Connect(ctx context.Context) (*sync.WaitGroup, error) {
c := rpcclient{}
if err := c.Connect(ctx, eth.cfg.IPC); err != nil {
if err := c.connect(ctx, eth.cfg.IPC); err != nil {
return nil, err
}
eth.node = &c

// Prime the cache with the best block.
bestHash, err := eth.node.BestBlockHash(ctx)
bestHash, err := eth.node.bestBlockHash(ctx)
if err != nil {
eth.shutdown()
return nil, fmt.Errorf("error getting best block hash from geth: %w", err)
}
block, err := eth.node.Block(ctx, bestHash)
block, err := eth.node.block(ctx, bestHash)
if err != nil {
eth.shutdown()
return nil, fmt.Errorf("error getting best block from geth: %w", err)
Expand Down Expand Up @@ -279,7 +279,7 @@ out:
select {
case <-blockPoll.C:
tip := eth.blockCache.tip()
bestHash, err := eth.node.BestBlockHash(ctx)
bestHash, err := eth.node.bestBlockHash(ctx)
if err != nil {
sendErr(asset.NewConnectionError("error retrieving best block: %w", err))
continue
Expand All @@ -288,7 +288,7 @@ out:
continue
}

block, err := eth.node.Block(ctx, bestHash)
block, err := eth.node.block(ctx, bestHash)
if err != nil {
sendErrFmt("error retrieving block %x: %w", bestHash, err)
continue
Expand All @@ -313,7 +313,7 @@ out:
if iHash == zeroHash {
break
}
iBlock, err := eth.node.Block(ctx, iHash)
iBlock, err := eth.node.block(ctx, iHash)
if err != nil {
sendErrFmt("error retrieving block %s: %w", iHash, err)
break
Expand Down
26 changes: 13 additions & 13 deletions server/asset/eth/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ var (
)

type testNode struct {
connectErr error
bestBlockHash common.Hash
bestBlockHashErr error
block *types.Block
blockErr error
connectErr error
bestBlkHash common.Hash
bestBlkHashErr error
blk *types.Block
blkErr error
}

func (n *testNode) Connect(ctx context.Context, IPC string) error {
func (n *testNode) connect(ctx context.Context, IPC string) error {
return n.connectErr
}

func (n *testNode) Shutdown() {}
func (n *testNode) shutdown() {}

func (n *testNode) BestBlockHash(ctx context.Context) (common.Hash, error) {
return n.bestBlockHash, n.bestBlockHashErr
func (n *testNode) bestBlockHash(ctx context.Context) (common.Hash, error) {
return n.bestBlkHash, n.bestBlkHashErr
}

func (n *testNode) Block(ctx context.Context, hash common.Hash) (*types.Block, error) {
return n.block, n.blockErr
func (n *testNode) block(ctx context.Context, hash common.Hash) (*types.Block, error) {
return n.blk, n.blkErr
}

func TestLoad(t *testing.T) {
Expand Down Expand Up @@ -204,8 +204,8 @@ func TestRun(t *testing.T) {
block1 := types.NewBlockWithHeader(header1)
blockHash1 := block1.Hash()
node := &testNode{}
node.bestBlockHash = blockHash1
node.block = block1
node.bestBlkHash = blockHash1
node.blk = block1
backend := unconnectedETH(tLogger, nil)
ch := backend.BlockChannel(1)
blocker := make(chan struct{})
Expand Down
9 changes: 5 additions & 4 deletions server/asset/eth/rpcclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type rpcclient struct {

// Connect connects to an ipc socket. It then wraps ethclient's client and
// bundles commands in a form we can easil use.
func (c *rpcclient) Connect(ctx context.Context, IPC string) error {
func (c *rpcclient) connect(ctx context.Context, IPC string) error {
client, err := rpc.DialIPC(ctx, IPC)
if err != nil {
return fmt.Errorf("unable to dial rpc: %v", err)
Expand All @@ -34,22 +34,23 @@ func (c *rpcclient) Connect(ctx context.Context, IPC string) error {
}

// Shutdown shuts down the client.
func (c *rpcclient) Shutdown() {
func (c *rpcclient) shutdown() {
if c.ec != nil {
c.ec.Close()
}
}

// BestBlockHash gets the best blocks hash at the time of calling. Due to the
// speed of Ethereum blocks, this changes often.
func (c *rpcclient) BestBlockHash(ctx context.Context) (common.Hash, error) {
func (c *rpcclient) bestBlockHash(ctx context.Context) (common.Hash, error) {
header, err := c.bestHeader(ctx)
if err != nil {
return common.Hash{}, err
}
return header.Hash(), nil
}

// bestHeader gets the best header at the time of calling.
func (c *rpcclient) bestHeader(ctx context.Context) (*types.Header, error) {
bn, err := c.ec.BlockNumber(ctx)
if err != nil {
Expand All @@ -63,7 +64,7 @@ func (c *rpcclient) bestHeader(ctx context.Context) (*types.Header, error) {
}

// Block gets the block identified by hash.
func (c *rpcclient) Block(ctx context.Context, hash common.Hash) (*types.Block, error) {
func (c *rpcclient) block(ctx context.Context, hash common.Hash) (*types.Block, error) {
block, err := c.ec.BlockByHash(ctx, hash)
if err != nil {
return nil, err
Expand Down
10 changes: 5 additions & 5 deletions server/asset/eth/rpcclient_harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ func TestMain(m *testing.M) {
ctx, cancel = context.WithCancel(context.Background())
defer func() {
cancel()
ethClient.Shutdown()
ethClient.shutdown()
}()
if err := ethClient.Connect(ctx, ipc); err != nil {
if err := ethClient.connect(ctx, ipc); err != nil {
fmt.Printf("Connect error: %v\n", err)
os.Exit(1)
}
os.Exit(m.Run())
}

func TestBestBlockHash(t *testing.T) {
_, err := ethClient.BestBlockHash(ctx)
_, err := ethClient.bestBlockHash(ctx)
if err != nil {
t.Fatal(err)
}
Expand All @@ -50,11 +50,11 @@ func TestBestHeader(t *testing.T) {
}

func TestBlock(t *testing.T) {
h, err := ethClient.BestBlockHash(ctx)
h, err := ethClient.bestBlockHash(ctx)
if err != nil {
t.Fatal(err)
}
_, err = ethClient.Block(ctx, h)
_, err = ethClient.block(ctx, h)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit f0ae5f1

Please sign in to comment.