diff --git a/server/asset/eth/eth.go b/server/asset/eth/eth.go index 4f7dfcf75e..7e50a2d289 100644 --- a/server/asset/eth/eth.go +++ b/server/asset/eth/eth.go @@ -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 ) @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/server/asset/eth/eth_test.go b/server/asset/eth/eth_test.go index 531a76cc04..6dcf22618b 100644 --- a/server/asset/eth/eth_test.go +++ b/server/asset/eth/eth_test.go @@ -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) { @@ -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{}) diff --git a/server/asset/eth/rpcclient.go b/server/asset/eth/rpcclient.go index d7812f9a8a..f5ead57baa 100644 --- a/server/asset/eth/rpcclient.go +++ b/server/asset/eth/rpcclient.go @@ -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) @@ -34,7 +34,7 @@ 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() } @@ -42,7 +42,7 @@ func (c *rpcclient) Shutdown() { // 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 @@ -50,6 +50,7 @@ func (c *rpcclient) BestBlockHash(ctx context.Context) (common.Hash, error) { 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 { @@ -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 diff --git a/server/asset/eth/rpcclient_harness_test.go b/server/asset/eth/rpcclient_harness_test.go index 5404125c71..5e064cd88d 100644 --- a/server/asset/eth/rpcclient_harness_test.go +++ b/server/asset/eth/rpcclient_harness_test.go @@ -26,9 +26,9 @@ 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) } @@ -36,7 +36,7 @@ func TestMain(m *testing.M) { } func TestBestBlockHash(t *testing.T) { - _, err := ethClient.BestBlockHash(ctx) + _, err := ethClient.bestBlockHash(ctx) if err != nil { t.Fatal(err) } @@ -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) }