~edwargix/tallyard

045d03bd88828f8cb12d6384af330eb390a5b51d — David Florness 5 years ago ea0bd91
Extract results from constant
1 files changed, 28 insertions(+), 4 deletions(-)

M voter.go
M voter.go => voter.go +28 -4
@@ 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])
			}
		}
	}
}