-
Notifications
You must be signed in to change notification settings - Fork 0
/
repub.go
168 lines (140 loc) · 4.33 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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"
ds "gx/ipfs/QmPpegoMqhAEqjncrzArm7KVWAkCm78rqL2DPuNjhPrshg/go-datastore"
logging "gx/ipfs/QmRb5jh8z2E8hMGN2tkvs1yHynUanqnZ3UeKwgN1i9P1F8/go-log"
goprocess "gx/ipfs/QmSF8fPo3jgVBAy8fpdjjYqgG87dkJgUprRBHRd2tmfgpP/goprocess"
gpctx "gx/ipfs/QmSF8fPo3jgVBAy8fpdjjYqgG87dkJgUprRBHRd2tmfgpP/goprocess/context"
routing "gx/ipfs/QmTiWLZ6Fo5j4KcTVutZJ5KWRRJrbxzmxA4td8NfEdrPh7/go-libp2p-routing"
recpb "gx/ipfs/QmUpttFinNDmNPgFwKN8sZK6BUtBmA68Y4KdSBDXa8t9sJ/go-libp2p-record/pb"
proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer"
ic "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
dshelp "gx/ipfs/QmdQTPWduSeyveSxeCAte33M592isSW5Z979g81aJphrgn/go-ipfs-ds-help"
)
var errNoEntry = errors.New("no previous entry")
var log = logging.Logger("ipns-repub")
// DefaultRebroadcastInterval is the default interval at which we rebroadcast IPNS records
var DefaultRebroadcastInterval = time.Hour * 4
// InitialRebroadcastDelay is the delay before first broadcasting IPNS records on start
var InitialRebroadcastDelay = time.Minute * 1
// FailureRetryInterval is the interval at which we retry IPNS records broadcasts (when they fail)
var FailureRetryInterval = time.Minute * 5
// DefaultRecordLifetime is the default lifetime for IPNS records
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) {
timer := time.NewTimer(InitialRebroadcastDelay)
defer timer.Stop()
if rp.Interval < InitialRebroadcastDelay {
timer.Reset(rp.Interval)
}
for {
select {
case <-timer.C:
timer.Reset(rp.Interval)
err := rp.republishEntries(proc)
if err != nil {
log.Error("Republisher failed to republish: ", err)
if FailureRetryInterval < rp.Interval {
timer.Reset(FailureRetryInterval)
}
}
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 {
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
}