Skip to content

Commit

Permalink
client/eth: Check provider header times.
Browse files Browse the repository at this point in the history
When fetching a new or cached header with a provider, do a basic check
on the header's time to determine if the header, and so the provider,
are up to date.
  • Loading branch information
JoeGruffins committed Feb 16, 2023
1 parent 692a175 commit 843c7ae
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 152 deletions.
12 changes: 4 additions & 8 deletions client/asset/eth/eth.go
Expand Up @@ -299,7 +299,7 @@ type ethFetcher interface {
sendSignedTransaction(ctx context.Context, tx *types.Transaction) error
sendTransaction(ctx context.Context, txOpts *bind.TransactOpts, to common.Address, data []byte) (*types.Transaction, error)
signData(data []byte) (sig, pubKey []byte, err error)
syncProgress(context.Context) (*ethereum.SyncProgress, error)
syncProgress(context.Context) (progress *ethereum.SyncProgress, tipTime uint64, err error)
transactionConfirmations(context.Context, common.Hash) (uint32, error)
getTransaction(context.Context, common.Hash) (*types.Transaction, int64, error)
txOpts(ctx context.Context, val, maxGas uint64, maxFeeRate, nonce *big.Int) (*bind.TransactOpts, error)
Expand Down Expand Up @@ -595,7 +595,7 @@ func createWallet(createWalletParams *asset.CreateWalletParams, skipConnect bool
}

if len(unknownEndpoints) > 0 && createWalletParams.Net == dex.Mainnet {
providers, err := connectProviders(ctx, unknownEndpoints, createWalletParams.Logger, big.NewInt(chainIDs[createWalletParams.Net]))
providers, err := connectProviders(ctx, unknownEndpoints, createWalletParams.Logger, big.NewInt(chainIDs[createWalletParams.Net]), createWalletParams.Net)
if err != nil {
return err
}
Expand Down Expand Up @@ -2799,17 +2799,13 @@ func (*baseWallet) ValidateSecret(secret, secretHash []byte) bool {
// more, requesting the best block header starts to fail after a few tries
// during initial sync. Investigate how to get correct sync progress.
func (eth *baseWallet) SyncStatus() (bool, float32, error) {
prog, err := eth.node.syncProgress(eth.ctx)
prog, tipTime, err := eth.node.syncProgress(eth.ctx)
if err != nil {
return false, 0, err
}
checkHeaderTime := func() (bool, error) {
bh, err := eth.node.bestHeader(eth.ctx)
if err != nil {
return false, err
}
// Time in the header is in seconds.
timeDiff := time.Now().Unix() - int64(bh.Time)
timeDiff := time.Now().Unix() - int64(tipTime)
if timeDiff > dexeth.MaxBlockInterval && eth.net != dex.Simnet {
eth.log.Infof("Time since last eth block (%d sec) exceeds %d sec."+
"Assuming not in sync. Ensure your computer's system clock "+
Expand Down
20 changes: 11 additions & 9 deletions client/asset/eth/eth_test.go
Expand Up @@ -97,6 +97,8 @@ type testNode struct {
bestHdr *types.Header
bestHdrErr error
syncProg ethereum.SyncProgress
syncProgT uint64
syncProgErr error
bal *big.Int
balErr error
signDataErr error
Expand Down Expand Up @@ -206,8 +208,8 @@ func (n *testNode) lock() error {
func (n *testNode) locked() bool {
return false
}
func (n *testNode) syncProgress(context.Context) (*ethereum.SyncProgress, error) {
return &n.syncProg, nil
func (n *testNode) syncProgress(context.Context) (prog *ethereum.SyncProgress, bestBlockUNIXTime uint64, err error) {
return &n.syncProg, n.syncProgT, n.syncProgErr
}
func (n *testNode) peerCount() uint32 {
return 1
Expand Down Expand Up @@ -464,8 +466,8 @@ func TestSyncStatus(t *testing.T) {
tests := []struct {
name string
syncProg ethereum.SyncProgress
syncProgErr error
subSecs uint64
bestHdrErr error
wantErr, wantSynced bool
wantRatio float32
}{{
Expand All @@ -491,22 +493,22 @@ func TestSyncStatus(t *testing.T) {
},
subSecs: dexeth.MaxBlockInterval + 1,
}, {
name: "best header error",
bestHdrErr: errors.New(""),
name: "sync progress error",
syncProg: ethereum.SyncProgress{
CurrentBlock: 25,
HighestBlock: 0,
},
wantErr: true,
syncProgErr: errors.New(""),
wantErr: true,
}}

for _, test := range tests {
nowInSecs := uint64(time.Now().Unix())
ctx, cancel := context.WithCancel(context.Background())
node := &testNode{
syncProg: test.syncProg,
bestHdr: &types.Header{Time: nowInSecs - test.subSecs},
bestHdrErr: test.bestHdrErr,
syncProg: test.syncProg,
syncProgT: nowInSecs - test.subSecs,
syncProgErr: test.syncProgErr,
}
eth := &baseWallet{
node: node,
Expand Down

0 comments on commit 843c7ae

Please sign in to comment.