Fix some annoying bugs for the demo

This commit is contained in:
Anthony Wang 2023-05-15 19:39:23 -04:00
parent 94f2b70dc7
commit 13c3fbab94
Signed by: a
GPG key ID: 42A5B952E6DD8D38
3 changed files with 7 additions and 9 deletions

View file

@ -2,7 +2,6 @@ package main
import (
"bytes"
"crypto/ed25519"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
@ -114,12 +113,11 @@ func peerHandler(w http.ResponseWriter, r *http.Request) {
}
// Get the timestamp of this val
func timestamp(val []byte) int64 {
if len(val) < 8+ed25519.SignatureSize {
func timestamp(val []byte) uint64 {
if len(val) < 8 {
return 0
}
ret, _ := binary.Varint(val[:8])
return ret
return binary.LittleEndian.Uint64(val)
}
// Get the value for a key from the DHT

View file

@ -43,7 +43,7 @@ func replicate(id, s string) {
op := user.log[idx]
mu.Unlock()
file, _ := os.Open(op)
file, _ := os.Open(dataDir + "/" + id + "/" + op)
resp, err := http.Post(s+"/storage/"+id+"/"+op+"?idx="+fmt.Sprint(idx), "application/octet-stream", file)
if err != nil {
time.Sleep(50 * time.Millisecond)
@ -124,7 +124,6 @@ func storageHandler(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
log.Print(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
@ -184,5 +183,6 @@ func storageHandler(w http.ResponseWriter, r *http.Request) {
return
}
user.log = append(user.log, filename)
w.Write([]byte(fmt.Sprint(len(user.log))))
}
}

View file

@ -83,7 +83,7 @@ func reconfigure(id string, dhtVal []byte) {
persist(id)
if !inServers {
delete(users, id)
_ = os.RemoveAll(id)
_ = os.RemoveAll(dataDir + "/" + id)
}
}
@ -99,7 +99,7 @@ func userHandler(w http.ResponseWriter, r *http.Request) {
mu.Lock()
if _, ok := users[id]; !ok {
// Add user
users[id] = &user{dhtVal: val}
users[id] = &user{dhtVal: val, phase: 0}
os.Mkdir(dataDir+"/"+id, 0755)
persist(id)
}