Skip to content

Commit

Permalink
fix(core): Fix Republish Method in Kademlia core
Browse files Browse the repository at this point in the history
  • Loading branch information
rolysr committed Jun 19, 2023
1 parent 0543f57 commit ecb3e08
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
11 changes: 8 additions & 3 deletions core/fullNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,17 +566,22 @@ func (fn *FullNode) PrintRoutingTable() {

func (fn *FullNode) Republish() {
for {
<-time.After(time.Hour)
<-time.After(time.Minute)
keys := fn.dht.Storage.GetKeys()
if keys == nil {
break
if len(keys) == 0 {
continue
}
for _, key := range keys {
data, _ := fn.dht.Storage.Read(key, 0, 0)
keyStr := base58.Encode(key)
fmt.Println("Key:", keyStr, "Data:", data)
if len(keyStr) == 0 || len(*data) == 0 {
break
}
go func() {
fn.StoreValue(keyStr, data)
}()
}
fmt.Println("Republish worked good for node:", fn.dht.ID)
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func main() {
if value == nil || (value != nil && len(value) == 0) {
//fmt.Println("There is no value in the network for that key")
} else {
//fmt.Println("The retrived value is:", string(value))
fmt.Println("The retrived value is:", string(value))
}
case "dht":
fullNode.PrintRoutingTable()
Expand Down
10 changes: 6 additions & 4 deletions structs/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package structs

import (
"errors"

"github.com/jbenet/go-base58"
"fmt"
)

type Storage struct {
Expand Down Expand Up @@ -57,9 +56,12 @@ func (s *Storage) Delete(key []byte) error {

func (s *Storage) GetKeys() [][]byte {
keys := [][]byte{}
for k := range s.KV {
keyStr := base58.Decode(k)
for k, v := range s.KV {
fmt.Println("Inside GetKeys", k, v)
keyStr := []byte(k)
fmt.Println(keyStr)
keys = append(keys, keyStr)
}
fmt.Println(keys)
return keys
}

0 comments on commit ecb3e08

Please sign in to comment.