From 13c3fbab94698231395298293fbae6e38c6d210f Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Mon, 15 May 2023 19:39:23 -0400 Subject: [PATCH] Fix some annoying bugs for the demo --- server/dht.go | 8 +++----- server/storage.go | 4 ++-- server/user.go | 4 ++-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/server/dht.go b/server/dht.go index 40bf4e6..19ffe17 100644 --- a/server/dht.go +++ b/server/dht.go @@ -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 diff --git a/server/storage.go b/server/storage.go index c05e29b..249b0d7 100644 --- a/server/storage.go +++ b/server/storage.go @@ -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)))) } } diff --git a/server/user.go b/server/user.go index cd31ca1..381c947 100644 --- a/server/user.go +++ b/server/user.go @@ -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) }