-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (129 loc) · 2.96 KB
/
main.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
package main
import (
"context"
"fmt"
api "github.com/ipfs/go-ipfs-api"
"os"
"strconv"
"sync"
"time"
)
var wg sync.WaitGroup
func main() {
keys := []string{}
// Create a shell to the local node
sh := api.NewLocalShell()
ctx := context.Background()
machine := os.Args[1]
nrMachines, _ := strconv.Atoi(os.Args[2])
sleep := time.Minute
switch os.Args[3] {
case "5m":
sleep = 5 * time.Minute
case "15m":
sleep = 15 * time.Minute
case "30m":
sleep = 30 * time.Minute
case "1h":
sleep = time.Hour
case "2h":
sleep = 2 * time.Hour
case "3h":
sleep = 3 * time.Hour
case "6h":
sleep = 6 * time.Hour
case "12h":
sleep = 12 * time.Hour
default:
os.Exit(1)
}
for i := 1; i <= nrMachines; i++ {
fileName := fmt.Sprintf("key%d.key", i)
file, err := os.Open(fileName)
if err != nil {
fmt.Println(err)
}
err = sh.KeyImport(ctx, strconv.Itoa(i), file)
if err != nil {
fmt.Println(err)
}
}
keyList, _ := sh.KeyList(ctx)
fmt.Println(keyList)
for _, key := range keyList {
if key.Name != machine && key.Name != "self" {
keys = append(keys, key.Id)
sh.KeyRm(ctx, key.Name)
fmt.Println("Removed key: ", key.Name)
}
}
fmt.Println(keys)
wg.Add(2)
go allResolves(sh, keys)
go publish(sh, sleep, machine)
wg.Wait()
}
func publish(sh *api.Shell, sleep time.Duration, machine string) {
counter := 0
for {
file, err := os.OpenFile("file.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
//writes the version number to the file so the hash changes
counter++
s := fmt.Sprintf("%s Version: %d \n", time.Now(), counter)
_, err = file.WriteString(s)
if err != nil {
fmt.Println(err)
}
file.Close()
file, err = os.OpenFile("file.txt", os.O_APPEND|os.O_CREATE|os.O_RDONLY, 0666)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
cid, err := sh.Add(file)
if err != nil {
fmt.Printf("error adding file: %s", err)
}
file.Close()
lifetime, err := time.ParseDuration("24h")
if err != nil {
fmt.Errorf("failed to parse lifetime: %w", err)
}
//so we dont have cache
ttl, err := time.ParseDuration("1ns")
if err != nil {
fmt.Errorf("failed to parse ttl: %w", err)
}
// Publish the IPNS record using the default keypair
_, err = sh.PublishWithDetails(cid, machine, lifetime, ttl, false)
if err != nil {
fmt.Errorf("failed to publish IPNS record: %w", err)
}
//waits for the next republish
time.Sleep(sleep)
//After the time, unpin the file so that it can be garbage collected
sh.Unpin(cid)
}
}
func resolve(sh *api.Shell, key string) {
for {
go func() {
// Resolve the IPNS record to a valid IPFS path
ipfsPath, err := sh.Resolve(key)
if err != nil {
fmt.Errorf("failed to resolve IPNS record: %s", ipfsPath)
}
}()
//waits 30 seconds to make each resolve
time.Sleep(30 * time.Second)
}
}
func allResolves(sh *api.Shell, keys []string) {
for _, key := range keys {
go resolve(sh, key)
}
}