Skip to content

Commit

Permalink
eth: RegFeeConfirmations
Browse files Browse the repository at this point in the history
Implement RegFeeConfirmations. Add transactionConfirmations method
to nodeClient. Check local tx pool first, since non-zero confs will
always request from peers.
  • Loading branch information
buck54321 authored and chappjc committed Dec 1, 2021
1 parent 70df246 commit 74abdca
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
7 changes: 6 additions & 1 deletion client/asset/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ type ethFetcher interface {
unlock(ctx context.Context, pw string) error
signData(addr common.Address, data []byte) ([]byte, error)
sendToAddr(ctx context.Context, addr common.Address, val uint64) (*types.Transaction, error)
transactionConfirmations(context.Context, common.Hash) (uint32, error)
}

// Check that ExchangeWallet satisfies the asset.Wallet interface.
Expand Down Expand Up @@ -838,8 +839,12 @@ func (eth *ExchangeWallet) SyncStatus() (bool, float32, error) {
return progress == 1, progress, nil
}

// RegFeeConfirmations gets the number of confirmations for the specified
// transaction.
func (eth *ExchangeWallet) RegFeeConfirmations(ctx context.Context, coinID dex.Bytes) (confs uint32, err error) {
return 0, asset.ErrNotImplemented
var txHash common.Hash
copy(txHash[:], coinID)
return eth.node.transactionConfirmations(ctx, txHash)
}

// monitorBlocks pings for new blocks and runs the tipChange callback function
Expand Down
4 changes: 4 additions & 0 deletions client/asset/eth/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ func (n *testNode) sendToAddr(ctx context.Context, addr common.Address, val uint
return nil, nil
}

func (n *testNode) transactionConfirmations(context.Context, common.Hash) (uint32, error) {
return 0, nil
}

func TestLoadConfig(t *testing.T) {
tests := []struct {
name string
Expand Down
26 changes: 26 additions & 0 deletions client/asset/eth/nodeclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,32 @@ func (n *nodeClient) isRedeemable(secretHash [32]byte, secret [32]byte, contract
})
}

// transactionConfirmations gets the number of confirmations for the specified
// transaction.
func (n *nodeClient) transactionConfirmations(ctx context.Context, txHash common.Hash) (uint32, error) {
// We'll check the local tx pool first, since from what I can tell, a light
// client always requests tx data from the network for anything else.
if tx := n.leth.ApiBackend.GetPoolTransaction(txHash); tx != nil {
return 0, nil
}
hdr, err := n.bestHeader(ctx)
if err != nil {
return 0, err
}
tx, _, blockHeight, _, err := n.leth.ApiBackend.GetTransaction(ctx, txHash)
if err != nil {
return 0, err
}
if tx != nil {
return uint32(hdr.Number.Uint64() - blockHeight + 1), nil
}
// TODO: There may be a race between when the tx is removed from our local
// tx pool, and when our peers are ready to supply the info. I saw a
// CoinNotFoundError in TestAccount/testSendTransaction, but haven't
// reproduced.
return 0, asset.CoinNotFoundError
}

// newTxOpts is a constructor for a TransactOpts.
func newTxOpts(ctx context.Context, from common.Address, val, maxGas uint64, maxFeeRate, gasTipCap *big.Int) *bind.TransactOpts {
if gasTipCap.Cmp(maxFeeRate) > 0 {
Expand Down
26 changes: 26 additions & 0 deletions client/asset/eth/nodeclient_harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,31 @@ func testSendTransaction(t *testing.T) {
t.Fatal(err)
}

// Checking confirmations for a random hash should result in not found error.
var txHash common.Hash
copy(txHash[:], encode.RandomBytes(32))
_, err = ethClient.transactionConfirmations(ctx, txHash)
if !errors.Is(err, asset.CoinNotFoundError) {
t.Fatalf("no CoinNotFoundError")
}

txOpts, _ := ethClient.txOpts(ctx, 1, dexeth.InitGas(1, 0), nil)

tx, err := ethClient.sendTransaction(ctx, txOpts, simnetAddr, nil)
if err != nil {
t.Fatal(err)
}

txHash = tx.Hash()

confs, err := ethClient.transactionConfirmations(ctx, txHash)
if err != nil {
t.Fatalf("transactionConfirmations error: %v", err)
}
if confs != 0 {
t.Fatalf("%d confs reported for unmined transaction", confs)
}

bal, _ := ethClient.balance(ctx)
if bal.PendingIn.Cmp(dexeth.GweiToWei(1)) != 0 { // We sent it to ourselves.
t.Fatalf("pending in not showing")
Expand All @@ -389,6 +407,14 @@ func testSendTransaction(t *testing.T) {
if err := waitForMined(t, time.Second*10, false); err != nil {
t.Fatal(err)
}

confs, err = ethClient.transactionConfirmations(ctx, txHash)
if err != nil {
t.Fatalf("transactionConfirmations error after mining: %v", err)
}
if confs == 0 {
t.Fatalf("zero confs after mining")
}
}

func testTransactionReceipt(t *testing.T) {
Expand Down

0 comments on commit 74abdca

Please sign in to comment.