-
Notifications
You must be signed in to change notification settings - Fork 0
/
repub.go
154 lines (128 loc) · 3.73 KB
/
repub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package republisher
import (
"context"
"errors"
"time"
keystore "github.com/ipfs/go-ipfs/keystore"
namesys "github.com/ipfs/go-ipfs/namesys"
pb "github.com/ipfs/go-ipfs/namesys/pb"
path "github.com/ipfs/go-ipfs/path"
dshelp "github.com/ipfs/go-ipfs/thirdparty/ds-help"
routing "gx/ipfs/QmPR2JzfKd9poHx9XBhzoFeBBC31ZM3W5iUPKJZWyaoZZm/go-libp2p-routing"
goprocess "gx/ipfs/QmSF8fPo3jgVBAy8fpdjjYqgG87dkJgUprRBHRd2tmfgpP/goprocess"
gpctx "gx/ipfs/QmSF8fPo3jgVBAy8fpdjjYqgG87dkJgUprRBHRd2tmfgpP/goprocess/context"
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
ds "gx/ipfs/QmVSase1JP7cq9QkPT46oNwdp9pT6kBkG3oqS14y3QcZjG/go-datastore"
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
ic "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
recpb "gx/ipfs/QmbxkgUceEcuSZ4ZdBA3x74VUDSSYjHYmmeEqkjxbtZ6Jg/go-libp2p-record/pb"
)
var errNoEntry = errors.New("no previous entry")
var log = logging.Logger("ipns-repub")
var DefaultRebroadcastInterval = time.Hour * 4
const DefaultRecordLifetime = time.Hour * 24
type Republisher struct {
r routing.ValueStore
ds ds.Datastore
self ic.PrivKey
ks keystore.Keystore
Interval time.Duration
// how long records that are republished should be valid for
RecordLifetime time.Duration
}
// NewRepublisher creates a new Republisher
func NewRepublisher(r routing.ValueStore, ds ds.Datastore, self ic.PrivKey, ks keystore.Keystore) *Republisher {
return &Republisher{
r: r,
ds: ds,
self: self,
ks: ks,
Interval: DefaultRebroadcastInterval,
RecordLifetime: DefaultRecordLifetime,
}
}
func (rp *Republisher) Run(proc goprocess.Process) {
tick := time.NewTicker(rp.Interval)
defer tick.Stop()
for {
select {
case <-tick.C:
err := rp.republishEntries(proc)
if err != nil {
log.Error("Republisher failed to republish: ", err)
}
case <-proc.Closing():
return
}
}
}
func (rp *Republisher) republishEntries(p goprocess.Process) error {
ctx, cancel := context.WithCancel(gpctx.OnClosingContext(p))
defer cancel()
err := rp.republishEntry(ctx, rp.self)
if err != nil {
return err
}
if rp.ks != nil {
keyNames, err := rp.ks.List()
if err != nil {
return err
}
for _, name := range keyNames {
priv, err := rp.ks.Get(name)
if err != nil {
return err
}
err = rp.republishEntry(ctx, priv)
if err != nil {
return err
}
}
}
return nil
}
func (rp *Republisher) republishEntry(ctx context.Context, priv ic.PrivKey) error {
id, err := peer.IDFromPrivateKey(priv)
if err != nil {
return err
}
log.Debugf("republishing ipns entry for %s", id)
// Look for it locally only
_, ipnskey := namesys.IpnsKeysForID(id)
p, seq, err := rp.getLastVal(ipnskey)
if err != nil {
if err == errNoEntry {
return nil
}
return err
}
// update record with same sequence number
eol := time.Now().Add(rp.RecordLifetime)
err = namesys.PutRecordToRouting(ctx, priv, p, seq, eol, rp.r, id)
if err != nil {
println("put record to routing error: " + err.Error())
return err
}
return nil
}
func (rp *Republisher) getLastVal(k string) (path.Path, uint64, error) {
ival, err := rp.ds.Get(dshelp.NewKeyFromBinary([]byte(k)))
if err != nil {
// not found means we dont have a previously published entry
return "", 0, errNoEntry
}
val := ival.([]byte)
dhtrec := new(recpb.Record)
err = proto.Unmarshal(val, dhtrec)
if err != nil {
return "", 0, err
}
// extract published data from record
e := new(pb.IpnsEntry)
err = proto.Unmarshal(dhtrec.GetValue(), e)
if err != nil {
return "", 0, err
}
return path.Path(e.Value), e.GetSequence(), nil
}