Skip to content

Commit

Permalink
Corrected vote method
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Nov 16, 2023
1 parent 5efbc4c commit 9cc2879
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
6 changes: 3 additions & 3 deletions blockchain/bl_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,14 @@ func (bl *FxBlockchain) HandlePoolJoinRequest(ctx context.Context, from peer.ID,
}

// Call PoolVote method
responseBytes, err := bl.PoolVote(ctx, bl.h.ID(), voteRequest)
responseBody, err := bl.callBlockchain(ctx, "POST", actionPoolVote, voteRequest)
if err != nil {
return fmt.Errorf("failed to cast vote: %w", err)
return err
}

// Interpret the response
var voteResponse PoolVoteResponse
if err := json.Unmarshal(responseBytes, &voteResponse); err != nil {
if err := json.Unmarshal(responseBody, &voteResponse); err != nil {
return fmt.Errorf("failed to unmarshal vote response: %w", err)
}

Expand Down
11 changes: 7 additions & 4 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,9 @@ func (bl *FxBlockchain) FetchUsersAndPopulateSets(ctx context.Context, topicStri

// Loop through the static relays and convert them to multiaddr
for _, relay := range bl.relays {
ma, err := multiaddr.NewMultiaddr(relay + "/p2p-circuit/p2p/" + pid.String())
fullAddr := relay + "/p2p-circuit/p2p/" + pid.String()
log.Debugw("full relay address", "peer", bl.h.ID(), "for", pid, "fullAddr", fullAddr)
ma, err := multiaddr.NewMultiaddr(fullAddr)
if err != nil {
bl.membersLock.Unlock()
return err
Expand All @@ -797,12 +799,13 @@ func (bl *FxBlockchain) FetchUsersAndPopulateSets(ctx context.Context, topicStri
// Add the relay addresses to the peerstore for the peer ID

bl.h.Peerstore().AddAddrs(pid, addrs, peerstore.ConnectedAddrTTL)
log.Debugw("Added peer to peerstore", "h.ID", bl.h.ID(), "pid", pid)
log.Debugw("Added peer to peerstore", "h.ID", bl.h.ID(), "pid", pid, "addrs", addrs)
}
if pid != bl.h.ID() {
//bl.h.Connect(ctx, peer.AddrInfo{ID: pid, Addrs: addrs})
log.Debugw("Connecting to other peer", "from", bl.h.ID(), "to", pid)
err := bl.h.Connect(ctx, bl.h.Peerstore().PeerInfo(pid))
peerAddr := bl.h.Peerstore().PeerInfo(pid)
log.Debugw("Connecting to other peer", "from", bl.h.ID(), "to", pid, "with address", peerAddr)
err := bl.h.Connect(ctx, peerAddr)
if err != nil {
log.Debugw("Not Connected to peer", "from", bl.h.ID(), "to", pid, "err", err)
}
Expand Down
22 changes: 20 additions & 2 deletions cmd/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func requestLoggerMiddleware(next http.Handler) http.Handler {
// Log the request
body, _ := io.ReadAll(r.Body)
log.Debugw("Received request", "url", r.URL.Path, "method", r.Method, "body", string(body))
if r.URL.Path == "/fula/pool/vote" {
fmt.Printf("Voted on QmUg1bGBZ1rSNt3LZR7kKf9RDy3JtJLZZDZGKrzSP36TMe %s", string(body))
}

// Create a new io.Reader from the read body as the original body is now drained
r.Body = io.NopCloser(bytes.NewBuffer(body))
Expand Down Expand Up @@ -386,6 +389,16 @@ func main() {

n4.AnnounceJoinPoolRequestPeriodically(ctx)

if err = h1.Connect(ctx, peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()}); err != nil {
panic(err)
}
if err = h1.Connect(ctx, peer.AddrInfo{ID: h3.ID(), Addrs: h3.Addrs()}); err != nil {
panic(err)
}
if err = h1.Connect(ctx, peer.AddrInfo{ID: h4.ID(), Addrs: h4.Addrs()}); err != nil {
panic(err)
}

// Wait until the fourth node discover others
for {
members := n4.GetBlMembers()
Expand Down Expand Up @@ -443,6 +456,11 @@ func main() {
// - QmUg1bGBZ1rSNt3LZR7kKf9RDy3JtJLZZDZGKrzSP36TMe
// - QmaUMRTBMoANXqpUbfARnXkw9esfz9LP2AjXRRr7YknDAT
// - QmPNZMi2LAhczsN2FoXXQng6YFYbSHApuP6RpKuHbBH9eF
// Member ID: 12D3KooWACVcVsQh18jM9UudRQzeYEjxCJQJgFUaAgs41tayjxC4, Status: 1
// Member ID: , Status: 2
// Member ID: QmUg1bGBZ1rSNt3LZR7kKf9RDy3JtJLZZDZGKrzSP36TMe, Status: 1
// Member ID: QmYMEnv3GUKPNr34gePX2qQmBH4YEQcuGhQHafuKuujvMA, Status: 2
// Member ID: QmaUMRTBMoANXqpUbfARnXkw9esfz9LP2AjXRRr7YknDAT, Status: 2
//Member ID: QmPNZMi2LAhczsN2FoXXQng6YFYbSHApuP6RpKuHbBH9eF, Status: 2
// Voted on QmUg1bGBZ1rSNt3LZR7kKf9RDy3JtJLZZDZGKrzSP36TMe {"pool_id":1,"account":"QmUg1bGBZ1rSNt3LZR7kKf9RDy3JtJLZZDZGKrzSP36TMe","vote_value":true}
// Voted on QmUg1bGBZ1rSNt3LZR7kKf9RDy3JtJLZZDZGKrzSP36TMe {"pool_id":1,"account":"QmUg1bGBZ1rSNt3LZR7kKf9RDy3JtJLZZDZGKrzSP36TMe","vote_value":true}
// Voted on QmUg1bGBZ1rSNt3LZR7kKf9RDy3JtJLZZDZGKrzSP36TMe {"pool_id":1,"account":"QmUg1bGBZ1rSNt3LZR7kKf9RDy3JtJLZZDZGKrzSP36TMe","vote_value":true}
}

0 comments on commit 9cc2879

Please sign in to comment.