From 045d03bd88828f8cb12d6384af330eb390a5b51d Mon Sep 17 00:00:00 2001 From: David Florness Date: Mon, 8 Jun 2020 23:22:31 -0600 Subject: [PATCH] Extract results from constant --- voter.go | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/voter.go b/voter.go index 67169a6..3342789 100644 --- a/voter.go +++ b/voter.go @@ -167,7 +167,7 @@ func handleCmd(cmd string, rw *bufio.ReadWriter, stream network.Stream) { } func streamHandler(stream network.Stream) { - logger.Info("got a new stream!", stream) + logger.Info("got a new stream:", stream) logger.Info("remote peer:", stream.Conn().RemotePeer()) rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream)) @@ -203,7 +203,7 @@ func findPeers(closeElection <-chan int) { if peer.ID == me.ID() { continue } - fmt.Printf("found voter: %s\n", peer) + fmt.Printf("found voter: %s\n", peer.ID) logger.Info("connecting to:", peer) err = me.Connect(me.ctx, peer) @@ -289,7 +289,7 @@ func startVoting() { logger.Infof("our input: %s", me.input) ballot := vote(candidates) - logger.Info("our ballot:", ballot) + logger.Infof("our ballot: %v", ballot) // no +1 since we want degree k-1 where k is total number of voters me.polyMu.Lock() @@ -333,7 +333,18 @@ func startVoting() { mat := constructPolyMatrix() mat.RREF() - result := mat[0][len(mat[0])-1] + constant := mat[0][len(mat[0])-1] + if !constant.IsInt() { + panic("constant term is not an integer") + } + + result := constant.Num().Bytes() + + // number of bytes we need to insert at the front since they're zero + diff := (len(candidates)*len(candidates)) - len(result) + result = append(make([]byte, diff), result...) + + printResults(result, candidates) // temporary select {} @@ -367,3 +378,16 @@ func constructPolyMatrix() Matrix { return mat } + +func printResults(result []byte, candidates []Candidate) { + logger.Infof("result: %v", result) + fmt.Println("=== Results ===") + n := len(candidates) + for i, cand := range candidates { + for j, vs := range candidates { + if i != j { + fmt.Printf("%s over %s: %d\n", cand, vs, result[i * n + j]) + } + } + } +} -- 2.38.4