Code cleanup
License: MIT Signed-off-by: Dirk McCormick <dirkmdev@gmail.com>
Dirk McCormick committed
Jan 30, 2018 at 22:44 UTC
dafa140e1f4692b79e383930b62b023e9c6a01d9
3 files changed
+19
-14
namesys/ipns_validate_test.go
+1
-1
@@ -64,7 +64,7 @@ func TestValidation(t *testing.T) {
64
t.Fatal(err)
65
}
66
if resp != p {
67
- t.Fatal("Mismatch between published path %s and resolved path %s", p, resp)
67
+ t.Fatalf("Mismatch between published path %s and resolved path %s", p, resp)
68
}
69
70
// Create expired entry
namesys/selector.go
+15
-13
@@ -10,6 +10,8 @@ import (
10
proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
11
)
12
13
+// Selects best record by checking which has the highest sequence number
14
+// and latest EOL
15
func IpnsSelectorFunc(k string, vals [][]byte) (int, error) {
16
var recs []*pb.IpnsEntry
17
for _, v := range vals {
@@ -26,40 +28,40 @@ func IpnsSelectorFunc(k string, vals [][]byte) (int, error) {
28
}
29
30
func selectRecord(recs []*pb.IpnsEntry, vals [][]byte) (int, error) {
29
- var best_seq uint64
30
- best_i := -1
31
+ var bestSeq uint64
32
+ besti := -1
33
34
for i, r := range recs {
33
- if r == nil || r.GetSequence() < best_seq {
35
+ if r == nil || r.GetSequence() < bestSeq {
36
continue
37
}
38
37
- if best_i == -1 || r.GetSequence() > best_seq {
38
- best_seq = r.GetSequence()
39
- best_i = i
40
- } else if r.GetSequence() == best_seq {
39
+ if besti == -1 || r.GetSequence() > bestSeq {
40
+ bestSeq = r.GetSequence()
41
+ besti = i
42
+ } else if r.GetSequence() == bestSeq {
43
rt, err := u.ParseRFC3339(string(r.GetValidity()))
44
if err != nil {
45
continue
46
}
47
46
- bestt, err := u.ParseRFC3339(string(recs[best_i].GetValidity()))
48
+ bestt, err := u.ParseRFC3339(string(recs[besti].GetValidity()))
49
if err != nil {
50
continue
51
}
52
53
if rt.After(bestt) {
52
- best_i = i
54
+ besti = i
55
} else if rt == bestt {
54
- if bytes.Compare(vals[i], vals[best_i]) > 0 {
55
- best_i = i
56
+ if bytes.Compare(vals[i], vals[besti]) > 0 {
57
+ besti = i
58
}
59
}
60
}
61
}
60
- if best_i == -1 {
62
+ if besti == -1 {
63
return 0, errors.New("no usable records in given set")
64
}
65
64
- return best_i, nil
66
+ return besti, nil
67
}
namesys/validator.go
+3
@@ -29,6 +29,9 @@ var ErrInvalidPath = errors.New("record path invalid")
29
// signature verification
30
var ErrSignature = errors.New("record signature verification failed")
31
32
+// Returns a ValidChecker for IPNS records
33
+// The validator function will get a public key from the KeyBook
34
+// to verify the record's signature
35
func NewIpnsRecordValidator(kbook pstore.KeyBook) *record.ValidChecker {
36
// ValidateIpnsRecord implements ValidatorFunc and verifies that the
37
// given 'val' is an IpnsEntry and that that entry is valid.