2020-02-05 10:09:33 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-02-10 11:22:37 +00:00
|
|
|
// maxRetries is the maximum number of attempts we do before giving up
|
2020-02-05 10:09:33 +00:00
|
|
|
const maxRetries uint64 = 11
|
|
|
|
|
2020-04-09 12:48:55 +00:00
|
|
|
// ENSBackoffTimeSec is the step of the exponential backoff
|
|
|
|
// we retry roughly for 17 hours after receiving the message 2^11 * 30
|
|
|
|
const ENSBackoffTimeSec uint64 = 30
|
2020-02-05 10:09:33 +00:00
|
|
|
|
|
|
|
// We calculate if it's too early to retry, by exponentially backing off
|
|
|
|
func verifiedENSRecentlyEnough(now, verifiedAt, retries uint64) bool {
|
2020-04-09 12:48:55 +00:00
|
|
|
return now < verifiedAt+ENSBackoffTimeSec*retries*uint64(math.Exp2(float64(retries)))
|
2020-02-05 10:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func shouldENSBeVerified(c *Contact, now uint64) bool {
|
|
|
|
if c.Name == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ENSVerified {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ENSVerificationRetries >= maxRetries {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if verifiedENSRecentlyEnough(now, c.ENSVerifiedAt, c.ENSVerificationRetries) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(c.Name, ".eth") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should trigger re-verification of the ENS name for this contact
|
|
|
|
func hasENSNameChanged(c *Contact, newName string, clockValue uint64) bool {
|
|
|
|
if c.LastENSClockValue > clockValue {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if newName == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(newName, ".eth") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if newName == c.Name {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|