~edwargix/tallyard

555f322f2b853774d97fce57d46c6bae402a28d5 — David Florness 5 years ago 680a588
Fetch election from master when not available via elections file
1 files changed, 47 insertions(+), 42 deletions(-)

M main.go
M main.go => main.go +47 -42
@@ 69,7 69,7 @@ func (localVoter *LocalVoter) Bootstrap() {
	wg.Wait()
}

func (election *Election) setupMaster() {
func (election *Election) SetupMaster() {
	fmt.Println("share this with peers:")
	fmt.Printf("%s\n", election.electionKey)



@@ 87,8 87,7 @@ func (election *Election) setupMaster() {
	})

	fmt.Println("press ENTER to solidify group of voters and start voting")
	stdReader := bufio.NewReader(os.Stdin)
	_, err := stdReader.ReadString('\n')
	_, err := bufio.NewReader(os.Stdin).ReadString('\n')
	if err != nil {
		panic(err)
	}


@@ 113,46 112,51 @@ func (election *Election) setupMaster() {
	election.RUnlock()
}

func (election *Election) setupSlave() {
	logger.Info("attempting to open stream with master peer...")
func (election *Election) SetupSlave() {
	localVoter := election.localVoter
	stream, err := localVoter.NewStream(localVoter.ctx, election.masterID, protocolID)
	rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream))
	if err != nil {
		panic(err)
	}
	logger.Info("opened stream with master peer")

	logger.Info("fetching election info from master")
	_, err = rw.WriteString("info")
	if err != nil {
		panic(err)
	}
	rw.Flush()
	stream.Close() // only stops writing
	// first field is the rendezvous string, which is used for peer
	// discovery
	str, err := rw.ReadString('\n')
	if err != nil && err != io.EOF {
		panic(err)
	}
	str = stripNewline(str)
	election.rendezvousNonce = Nonce(str)
	// remaining fields are the candidates
	for {
	// have candidates and nonce been loaded from elections file?
	// if not, fetch them from master
	if len(election.Candidates) == 0 || election.rendezvousNonce == "" {
		logger.Info("attempting to open stream with master peer...")
		stream, err := localVoter.NewStream(localVoter.ctx, election.masterID, protocolID)
		rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream))
		if err != nil {
			panic(err)
		}
		logger.Info("opened stream with master peer")

		logger.Info("fetching election info from master")
		_, err = rw.WriteString("info")
		if err != nil {
			panic(err)
		}
		rw.Flush()
		stream.Close() // only stops writing
		// first field is the rendezvous string, which is used for peer
		// discovery
		str, err := rw.ReadString('\n')
		if err != nil && err != io.EOF {
			panic(err)
		}
		str = stripNewline(str)
		if str != "" {
			election.Candidates = append(election.Candidates, Candidate(str))
		}
		if err == io.EOF {
			break
		election.rendezvousNonce = Nonce(str)
		// remaining fields are the candidates
		for {
			str, err := rw.ReadString('\n')
			if err != nil && err != io.EOF {
				panic(err)
			}
			str = stripNewline(str)
			if str != "" {
				election.Candidates = append(election.Candidates, Candidate(str))
			}
			if err == io.EOF {
				break
			}
		}
		logger.Info("done fetching election info")
	}
	logger.Info("done fetching election info")

	logger.Info("checking authenticity of election info...")
	verifyElectionInfo(election, election.merkleRoot)


@@ 164,6 168,7 @@ func (election *Election) setupSlave() {
	localVoter.SetStreamHandler(protocolID, func(stream network.Stream) {
		streamHandler(stream, election)
	})

	findPeers(ch, election)
}



@@ 196,8 201,7 @@ func main() {
		// TODO: ensure at least 2 candidates
		election = NewElectionWithCandidates(candidates)
	} else if electionKey := flag.Arg(0); electionKey != "" {
		// we are a slave slave the and election key was given via CLI
		// args
		// we are a slave the and election key was given via CLI args
		election = NewElectionWithElectionKey(electionKey)
	} else {
		// create/join election via TUI


@@ 208,16 212,17 @@ func main() {
		}
	}

	// saves identity
	if err := election.saveInfo(); err != nil {
		panic(err)
	}

	election.localVoter.Bootstrap()

	if election.masterID == election.localVoter.ID() {
		election.setupMaster()
		election.SetupMaster()
	} else {
		election.setupSlave()
	}

	if err := election.saveInfo(); err != nil {
		panic(err)
		election.SetupSlave()
	}

	startVoting(election)