Skip to content

Commit

Permalink
martonp early review followup
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Sep 7, 2022
1 parent 5697edb commit b305f90
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 46 deletions.
70 changes: 25 additions & 45 deletions client/asset/eth/multirpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ func (p *provider) subscribeHeaders(ctx context.Context, sub ethereum.Subscripti
type receiptRecord struct {
r *types.Receipt
lastAccess time.Time
confirmed bool
}

// multiRPCClient is an ethFetcher backed by one or more public RPC providers.
Expand Down Expand Up @@ -506,37 +507,47 @@ func (m *multiRPCClient) cleanReceipts() {
}

func (m *multiRPCClient) transactionReceipt(ctx context.Context, txHash common.Hash) (r *types.Receipt, err error) {

// TODO
// TODO: Plug in to the monitoredTx system from #1638.
// TODO

// Check the cache.
const cacheExpiration = time.Minute

m.receipts.RLock()
cached := m.receipts.cache[txHash]
if cached != nil {
if cached != nil && cached.confirmed {
cached.lastAccess = time.Now()
}
if time.Since(m.receipts.lastClean) > time.Minute*20 {
m.receipts.lastClean = time.Now()
go m.cleanReceipts()
}
m.receipts.RUnlock()
if cached != nil {

// If confirmed or if it was just fetched, return it as is.
if cached != nil && (cached.confirmed || time.Since(cached.lastAccess) < cacheExpiration) {
return cached.r, nil
}

// Fetch a fresh one.
if err = m.withPreferred(func(p *provider) error {
r, err = p.ec.TransactionReceipt(ctx, txHash)
return err
}); err != nil {
return nil, err
}

var confs int64
if r.BlockNumber != nil {
tip, err := m.bestHeader(ctx)
if err != nil {
return nil, fmt.Errorf("bestHeader error: %v", err)
}
confs = new(big.Int).Sub(tip.Number, r.BlockNumber).Int64() + 1
}

m.receipts.Lock()
m.receipts.cache[txHash] = &receiptRecord{
r: r,
lastAccess: time.Now(),
confirmed: confs > txConfsNeededToConfirm,
}
m.receipts.Unlock()

Expand Down Expand Up @@ -663,7 +674,6 @@ func (m *multiRPCClient) withOne(providers []*provider, f func(*provider) error,
readyProviders = providers
}
for _, p := range readyProviders {

err := f(p)
if err == nil {
break
Expand Down Expand Up @@ -860,38 +870,19 @@ func (m *multiRPCClient) signData(data []byte) (sig, pubKey []byte, err error) {
return signData(m.creds, data)
}

// syncProgress: We're going to lie and just always say we're synced if we
// can get a header.
func (m *multiRPCClient) syncProgress(ctx context.Context) (prog *ethereum.SyncProgress, err error) {
return prog, m.withAny(func(p *provider) error {
s, err := p.ec.SyncProgress(ctx)
tip, err := p.bestHeader(ctx, m.log)
if err != nil {
return fmt.Errorf("error getting sync progress from %s: %v", p.host, err)
}
if s != nil {
prog = s
return nil
return err
}

// SyncProgress will return nil both before syncing has begun and after
// it has finished. In order to discern when syncing has begun, check
// that the best header came in under MaxBlockInterval.
bh, err := p.bestHeader(ctx, m.log)
if err != nil {
return fmt.Errorf("error getting header for nil SyncProgress resolution: %v", err)
}
// Time in the header is in seconds.
nowInSecs := time.Now().Unix() / 1000
timeDiff := nowInSecs - int64(bh.Time)

if timeDiff < dexeth.MaxBlockInterval {
// Consider this synced.
prog = &ethereum.SyncProgress{
CurrentBlock: bh.Number.Uint64(),
HighestBlock: bh.Number.Uint64(),
}
return nil
prog = &ethereum.SyncProgress{
CurrentBlock: tip.Number.Uint64(),
HighestBlock: tip.Number.Uint64(),
}
// Not synced.
prog = &ethereum.SyncProgress{}
return nil
}, allRPCErrorsAreFails)
}
Expand Down Expand Up @@ -1148,17 +1139,6 @@ func newCompatibilityTests(ctx context.Context, cb bind.ContractBackend, log slo
return nil
},
},
{
name: "SyncProgress",
f: func(p *provider) error {
s, err := p.ec.SyncProgress(ctx)
if err != nil {
return err
}
log.Infof("#### Sync progress: %+v", s)
return nil
},
},
{
name: "CodeAt",
f: func(p *provider) error {
Expand Down
2 changes: 1 addition & 1 deletion client/asset/eth/multirpc_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ var freeServers = []string{
"https://main-rpc.linkpool.io/",
"https://nodes.mewapi.io/rpc/eth",
"https://rpc.flashbots.net/",
"https://rpc.ankr.com/eth", // rpc.ankr.com "SyncProgress" error: the method eth_syncing does not exist/is not available
"https://rpc.ankr.com/eth", // Passes, but doesn't support SyncProgress, which don't use and just lie about right now.
"https://api.mycryptoapi.com/eth",
"https://ethereumnodelight.app.runonflux.io",
}
Expand Down

0 comments on commit b305f90

Please sign in to comment.