From dafa140e1f4692b79e383930b62b023e9c6a01d9 Mon Sep 17 00:00:00 2001 From: Dirk McCormick Date: Tue, 30 Jan 2018 22:44:46 -0500 Subject: [PATCH] Code cleanup License: MIT Signed-off-by: Dirk McCormick --- namesys/ipns_validate_test.go | 2 +- namesys/selector.go | 28 +++++++++++++++------------- namesys/validator.go | 3 +++ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/namesys/ipns_validate_test.go b/namesys/ipns_validate_test.go index cb7d80954..b5df33a00 100644 --- a/namesys/ipns_validate_test.go +++ b/namesys/ipns_validate_test.go @@ -64,7 +64,7 @@ func TestValidation(t *testing.T) { t.Fatal(err) } if resp != p { - t.Fatal("Mismatch between published path %s and resolved path %s", p, resp) + t.Fatalf("Mismatch between published path %s and resolved path %s", p, resp) } // Create expired entry diff --git a/namesys/selector.go b/namesys/selector.go index 6114bfcce..c93ddce89 100644 --- a/namesys/selector.go +++ b/namesys/selector.go @@ -10,6 +10,8 @@ import ( proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto" ) +// Selects best record by checking which has the highest sequence number +// and latest EOL func IpnsSelectorFunc(k string, vals [][]byte) (int, error) { var recs []*pb.IpnsEntry for _, v := range vals { @@ -26,40 +28,40 @@ func IpnsSelectorFunc(k string, vals [][]byte) (int, error) { } func selectRecord(recs []*pb.IpnsEntry, vals [][]byte) (int, error) { - var best_seq uint64 - best_i := -1 + var bestSeq uint64 + besti := -1 for i, r := range recs { - if r == nil || r.GetSequence() < best_seq { + if r == nil || r.GetSequence() < bestSeq { continue } - if best_i == -1 || r.GetSequence() > best_seq { - best_seq = r.GetSequence() - best_i = i - } else if r.GetSequence() == best_seq { + if besti == -1 || r.GetSequence() > bestSeq { + bestSeq = r.GetSequence() + besti = i + } else if r.GetSequence() == bestSeq { rt, err := u.ParseRFC3339(string(r.GetValidity())) if err != nil { continue } - bestt, err := u.ParseRFC3339(string(recs[best_i].GetValidity())) + bestt, err := u.ParseRFC3339(string(recs[besti].GetValidity())) if err != nil { continue } if rt.After(bestt) { - best_i = i + besti = i } else if rt == bestt { - if bytes.Compare(vals[i], vals[best_i]) > 0 { - best_i = i + if bytes.Compare(vals[i], vals[besti]) > 0 { + besti = i } } } } - if best_i == -1 { + if besti == -1 { return 0, errors.New("no usable records in given set") } - return best_i, nil + return besti, nil } diff --git a/namesys/validator.go b/namesys/validator.go index 3eebce7f9..57924535a 100644 --- a/namesys/validator.go +++ b/namesys/validator.go @@ -29,6 +29,9 @@ var ErrInvalidPath = errors.New("record path invalid") // signature verification var ErrSignature = errors.New("record signature verification failed") +// Returns a ValidChecker for IPNS records +// The validator function will get a public key from the KeyBook +// to verify the record's signature func NewIpnsRecordValidator(kbook pstore.KeyBook) *record.ValidChecker { // ValidateIpnsRecord implements ValidatorFunc and verifies that the // given 'val' is an IpnsEntry and that that entry is valid.