Skip to content

Commit

Permalink
feat(pb): add ranges for request FindValue
Browse files Browse the repository at this point in the history
  • Loading branch information
leandro-driguez committed Jun 6, 2023
1 parent 64d0017 commit 3e52fc8
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 102 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

protoc:
cd proto && protoc --go_out=../pb --go_opt=paths=source_relative --go-grpc_out=../pb --go-grpc_opt=paths=source_relative *.proto && cd ..
64 changes: 36 additions & 28 deletions core/fullNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func NewFullNode(nodeIP string, nodePort, bootstrapPort int, storage interfaces.
dht := DHT{Node: node, RoutingTable: routingTable, Storage: storage}
fullNode := FullNode{dht: &dht}

go func() {
for {
<-time.After(10 * time.Second)
fmt.Println("\nROUTING TABLE:")
fullNode.PrintRoutingTable()
fmt.Printf("\n")
}
}()
// go func() {
// for {
// <-time.After(10 * time.Second)
// fmt.Println("\nROUTING TABLE:")
// fullNode.PrintRoutingTable()
// fmt.Printf("\n")
// }
// }()

if isBootstrapNode {
go fullNode.bootstrap(bootstrapPort)
Expand Down Expand Up @@ -153,7 +153,7 @@ func (fn *FullNode) Store(stream pb.FullNode_StoreServer) error {
return nil
}

func (fn *FullNode) FindNode(ctx context.Context, target *pb.TargetID) (*pb.KBucket, error) {
func (fn *FullNode) FindNode(ctx context.Context, target *pb.Target) (*pb.KBucket, error) {
// add the sender to the Routing Table
sender := structs.Node{
ID: target.Sender.ID,
Expand All @@ -167,7 +167,7 @@ func (fn *FullNode) FindNode(ctx context.Context, target *pb.TargetID) (*pb.KBuc
return getKBucketFromNodeArray(bucket), nil
}

func (fn *FullNode) FindValue(target *pb.TargetID, fv pb.FullNode_FindValueServer) error {
func (fn *FullNode) FindValue(target *pb.Target, stream pb.FullNode_FindValueServer) error {
// add the sender to the Routing Table
sender := structs.Node{
ID: target.Sender.ID,
Expand All @@ -177,23 +177,31 @@ func (fn *FullNode) FindValue(target *pb.TargetID, fv pb.FullNode_FindValueServe
fn.dht.RoutingTable.AddNode(sender)

value, neighbors := fn.dht.FindValue(&target.ID)

kbucket := &pb.KBucket{Bucket: []*pb.Node{}}
if neighbors != nil && len(*neighbors) > 0 {
kbucket = getKBucketFromNodeArray(neighbors)
}
if value == nil {
value = &[]byte{}
}
response := pb.FindValueResponse{
KNeartestBuckets: kbucket,
Value: &pb.Data{
Init: 0,
End: int32(len(*value)),
Buffer: (*value)[:],
},
response := pb.FindValueResponse{}

if value == nil && neighbors != nil {
response = pb.FindValueResponse{
KNeartestBuckets: getKBucketFromNodeArray(neighbors),
Value: &pb.Data{
Init: 0,
End: 0,
Buffer: []byte{},
},
}
} else if value != nil && neighbors == nil {
response = pb.FindValueResponse{
KNeartestBuckets: &pb.KBucket{Bucket: []*pb.Node{}},
Value: &pb.Data{
Init: target.Init,
End: target.End,
Buffer: (*value)[int(target.Init):int(target.End)],
},
}
} else {
return errors.New("check code because this case shouldn't be valid")
}
fv.Send(&response)
stream.Send(&response)

return nil
}

Expand Down Expand Up @@ -257,7 +265,7 @@ func (fn *FullNode) LookUp(target []byte) ([]structs.Node, error) {
defer cancel()

recvNodes, err := client.FindNode(ctx,
&pb.TargetID{
&pb.Target{
ID: node.ID,
Sender: &pb.Node{
ID: fn.dht.ID,
Expand Down Expand Up @@ -505,7 +513,7 @@ func (fn *FullNode) GetValue(target string) ([]byte, error) {
defer cancel()

receiver, err := client.FindValue(ctx,
&pb.TargetID{
&pb.Target{
ID: keyHash,
Sender: &pb.Node{
ID: fn.dht.ID,
Expand Down
125 changes: 71 additions & 54 deletions pb/kademlia.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions pb/kademlia_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3e52fc8

Please sign in to comment.