Skip to content
This repository was archived by the owner on Dec 20, 2022. It is now read-only.

Commit 2732ba9

Browse files
committed
Add validation of git connection
Adds a validation of the connection to the corresponding git provider before starting with a "commit" or "finalize" (as the proposer), in order to prevent failures during these processes. Closes #2
1 parent 8e10fb4 commit 2732ba9

4 files changed

Lines changed: 99 additions & 63 deletions

File tree

demo/demo.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func _executeVote(_proposalID string, _choice string, _salt string, _demoVoter i
108108
}
109109

110110
if proposal.Proposer == myAddress {
111-
return errors.New("You can't vote on your own proposal")
111+
return errors.New("The demo voter can't vote on his own proposal")
112112
}
113113

114114
// Retrieving the default stake from the ditContract
@@ -157,7 +157,7 @@ func _executeVote(_proposalID string, _choice string, _salt string, _demoVoter i
157157

158158
// If this is the case, the user can not vote again
159159
if oldDidCommit {
160-
return errors.New("You already voted on this proposal")
160+
return errors.New("The demo voter already voted on this proposal")
161161
}
162162

163163
var auth *bind.TransactOpts
@@ -174,7 +174,7 @@ func _executeVote(_proposalID string, _choice string, _salt string, _demoVoter i
174174
_, err = ditCoordingatorInstance.VoteOnProposal(auth, repoHash, big.NewInt(int64(proposalID)), voteHash)
175175
if err != nil {
176176
if strings.Contains(err.Error(), "insufficient funds") {
177-
return errors.New("Your account doesn't have enough ETH to pay for the transaction")
177+
return errors.New("The demo voters account doesn't have enough ETH to pay for the transaction")
178178
}
179179
return errors.New("Failed to commit the vote: " + err.Error())
180180
}
@@ -276,7 +276,7 @@ func Open(_proposalID string, _demoVoter int) error {
276276

277277
// If this is not the case the user never participated in this proposal through a vote
278278
if !didCommit {
279-
return errors.New("You didn't vote on this proposal")
279+
return errors.New("The demo voter didn't vote on this proposal")
280280
}
281281

282282
// Verifying whether the user has revealed his vote on this proposal
@@ -287,7 +287,7 @@ func Open(_proposalID string, _demoVoter int) error {
287287

288288
// If this is the case, the user already revealed his vote
289289
if oldDidReveal {
290-
return errors.New("You already opened your vote on this proposal")
290+
return errors.New("The demo voter already opened the vote on this proposal")
291291
}
292292

293293
// Crerating the transaction (basic values)
@@ -304,7 +304,7 @@ func Open(_proposalID string, _demoVoter int) error {
304304
_, err = ditCoordinatorInstance.OpenVoteOnProposal(auth, repoHash, big.NewInt(int64(proposalID)), choice, salt)
305305
if err != nil {
306306
if strings.Contains(err.Error(), "insufficient funds") {
307-
return errors.New("Your account doesn't have enough ETH to pay for the transaction")
307+
return errors.New("The demo voters account doesn't have enough ETH to pay for the transaction")
308308
}
309309
return errors.New("Failed to open the vote: " + err.Error())
310310
}
@@ -405,7 +405,7 @@ func Finalize(_proposalID string, _demoVoter int) (bool, error) {
405405

406406
// If not, we are not allowed to call this function (it would fail)
407407
if !didCommit && myAddress != proposal.Proposer {
408-
return false, errors.New("You didn't participate in this vote")
408+
return false, errors.New("The demo voter didn't participate in this vote")
409409
}
410410

411411
// Crerating the transaction (basic values)
@@ -418,7 +418,7 @@ func Finalize(_proposalID string, _demoVoter int) (bool, error) {
418418
_, err = ditCoordinatorInstance.FinalizeVote(auth, repoHash, big.NewInt(int64(proposalID)))
419419
if err != nil {
420420
if strings.Contains(err.Error(), "insufficient funds") {
421-
return false, errors.New("Your account doesn't have enough ETH to pay for the transaction")
421+
return false, errors.New("The demo voters account doesn't have enough ETH to pay for the transaction")
422422
}
423423
return false, errors.New("Failed to finalize the vote: " + err.Error())
424424
}

ethereum/ethereum.go

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func ProposeCommit(_commitMessage string) (string, int, error) {
264264
}
265265

266266
// Proposing the commit
267-
transaction, err := ditCoordinatorInstance.ProposeCommit(auth, repoHash, big.NewInt(int64(answerKnowledgeLabel-1)), big.NewInt(int64(120)), big.NewInt(int64(120)))
267+
transaction, err := ditCoordinatorInstance.ProposeCommit(auth, repoHash, big.NewInt(int64(answerKnowledgeLabel-1)), big.NewInt(int64(180)), big.NewInt(int64(180)))
268268
if err != nil {
269269
if strings.Contains(err.Error(), "insufficient funds") {
270270
return "", 0, errors.New("Your account doesn't have enough ETH to pay for the transaction")
@@ -667,21 +667,21 @@ func Open(_proposalID string) error {
667667
// including the ETH and KNW reward in case of a voting for the winning decision
668668
// or the losing of ETH and KNW in case of a voting for the losing decision
669669
// The first caller who executes this will also trigger the calculation whether the vote passed or not
670-
func Finalize(_proposalID string) (bool, error) {
670+
func Finalize(_proposalID string) (bool, bool, error) {
671671
// Converting the stdin string input of the user into an int
672672
proposalID, _ := strconv.Atoi(_proposalID)
673673

674674
// Searching for this repositories object in the config
675675
repoIndex, err := searchForRepoInConfig()
676676
if err != nil {
677-
return false, err
677+
return false, false, err
678678
}
679679

680680
repoHash := GetHashOfString(config.DitConfig.Repositories[repoIndex].Name)
681681

682682
connection, err := getConnection()
683683
if err != nil {
684-
return false, err
684+
return false, false, err
685685
}
686686

687687
// Convertig the hex-string-formatted address into an address object
@@ -690,19 +690,19 @@ func Finalize(_proposalID string) (bool, error) {
690690
// Create a new instance of the ditContract to access it
691691
ditCooordinatorInstance, err := getDitCoordinatorInstance(connection)
692692
if err != nil {
693-
return false, err
693+
return false, false, err
694694
}
695695

696696
// Create a new instance of the KNWToken contract to access it
697697
KNWTokenInstance, err := getKNWTokenInstance(connection)
698698
if err != nil {
699-
return false, err
699+
return false, false, err
700700
}
701701

702702
// Create a new instance of the KNWVoting contract to access it
703703
KNWVotingInstance, err := getKNWVotingInstance(connection)
704704
if err != nil {
705-
return false, err
705+
return false, false, err
706706
}
707707

708708
// Searching for the corresponding vote in the votes stored in the config
@@ -716,62 +716,65 @@ func Finalize(_proposalID string) (bool, error) {
716716

717717
// If this user already called the Resolve function for this vote it's not possible anymore
718718
if config.DitConfig.Repositories[repoIndex].ActiveVotes[voteIndex].Resolved {
719-
return false, errors.New("You already finalized this vote")
719+
return false, false, errors.New("You already finalized this vote")
720720
}
721721

722722
// Verifying whether the vote has already ended
723723
pollEnded, err := KNWVotingInstance.PollEnded(nil, big.NewInt(int64(config.DitConfig.Repositories[repoIndex].ActiveVotes[voteIndex].KNWVoteID)))
724724
if err != nil {
725-
return false, errors.New("Failed to retrieve vote status")
725+
return false, false, errors.New("Failed to retrieve vote status")
726726
}
727727

728728
// If not, we can't resolve it
729729
if !pollEnded {
730-
return false, errors.New("The vote hasn't ended yet")
730+
return false, false, errors.New("The vote hasn't ended yet")
731731
}
732732

733733
// Verifying whether the user is a participant of this vote
734734
didCommit, err := KNWVotingInstance.DidCommit(nil, myAddress, big.NewInt(int64(config.DitConfig.Repositories[repoIndex].ActiveVotes[voteIndex].KNWVoteID)))
735735
if err != nil {
736-
return false, errors.New("Failed to retrieve commit status")
736+
return false, false, errors.New("Failed to retrieve commit status")
737737
}
738738

739739
// Retrieve the selected proposal obkect
740740
proposal, err := ditCooordinatorInstance.ProposalsOfRepository(nil, repoHash, big.NewInt(int64(proposalID)))
741741
if err != nil {
742-
return false, errors.New("Failed to retrieve the new proposal")
742+
return false, false, errors.New("Failed to retrieve the new proposal")
743743
}
744744

745+
// Indicates whether the called was the proposer or not
746+
isProposer := (proposal.Proposer == myAddress)
747+
745748
// If not, we are not allowed to call this function (it would fail)
746749
if !didCommit && myAddress != proposal.Proposer {
747-
return false, errors.New("You didn't participate in this vote")
750+
return false, false, errors.New("You didn't participate in this vote")
748751
}
749752

750753
// Saving the old ETH balance
751754
oldEthBalance, err := connection.BalanceAt(context.Background(), myAddress, nil)
752755
if err != nil {
753-
return false, errors.New("Failed to retrieve ETH balance")
756+
return false, false, errors.New("Failed to retrieve ETH balance")
754757
}
755758

756759
// Saving the old KNW balance
757760
oldKNWBalance, err := KNWTokenInstance.BalanceOfLabel(nil, myAddress, config.DitConfig.Repositories[repoIndex].ActiveVotes[voteIndex].KnowledgeLabel)
758761
if err != nil {
759-
return false, errors.New("Failed to retrieve KNW balance")
762+
return false, false, errors.New("Failed to retrieve KNW balance")
760763
}
761764

762765
// Crerating the transaction (basic values)
763766
auth, err := populateTx(connection)
764767
if err != nil {
765-
return false, err
768+
return false, false, err
766769
}
767770

768771
// Resolving the vote
769772
transaction, err := ditCooordinatorInstance.FinalizeVote(auth, repoHash, big.NewInt(int64(proposalID)))
770773
if err != nil {
771774
if strings.Contains(err.Error(), "insufficient funds") {
772-
return false, errors.New("Your account doesn't have enough ETH to pay for the transaction")
775+
return false, false, errors.New("Your account doesn't have enough ETH to pay for the transaction")
773776
}
774-
return false, errors.New("Failed to finalize the vote: " + err.Error())
777+
return false, false, errors.New("Failed to finalize the vote: " + err.Error())
775778
}
776779

777780
// Waiting for the resolve transaction to be mined
@@ -785,7 +788,7 @@ func Finalize(_proposalID string) (bool, error) {
785788
// Checking the balance of the user every 5 seconds, if it changed, a transaction was executed
786789
newEthBalance, err = connection.BalanceAt(context.Background(), myAddress, nil)
787790
if err != nil {
788-
return false, errors.New("Failed to retrieve opening status")
791+
return false, false, errors.New("Failed to retrieve opening status")
789792
}
790793
// If we are waiting for more than 2 minutes, the transaction might have failed
791794
if waitingFor > 180 {
@@ -800,13 +803,13 @@ func Finalize(_proposalID string) (bool, error) {
800803
// Saving the new KNW balance after resolving the vote
801804
newKNWBalance, err := KNWTokenInstance.BalanceOfLabel(nil, myAddress, config.DitConfig.Repositories[repoIndex].ActiveVotes[voteIndex].KnowledgeLabel)
802805
if err != nil {
803-
return false, errors.New("Failed to retrieve KNW balance")
806+
return false, false, errors.New("Failed to retrieve KNW balance")
804807
}
805808

806809
// Retrieving the outcome of the vote
807810
pollPassed, err := KNWVotingInstance.IsPassed(nil, big.NewInt(int64(config.DitConfig.Repositories[repoIndex].ActiveVotes[voteIndex].KNWVoteID)))
808811
if err != nil {
809-
return false, errors.New("Failed to retrieve vote outcome")
812+
return false, false, errors.New("Failed to retrieve vote outcome")
810813
}
811814

812815
fmt.Printf("\n")
@@ -847,7 +850,7 @@ func Finalize(_proposalID string) (bool, error) {
847850
// Saving the config back to the file
848851
err = config.Save()
849852
if err != nil {
850-
return false, nil
853+
return false, false, err
851854
}
852855

853856
if config.DitConfig.DemoModeActive && len(config.DitConfig.Repositories[repoIndex].ActiveVotes[voteIndex].DemoChoices) == 3 {
@@ -858,7 +861,7 @@ func Finalize(_proposalID string) (bool, error) {
858861
}
859862
}
860863
}
861-
return pollPassed, nil
864+
return pollPassed, isProposer, nil
862865
}
863866

864867
// GetVoteInfo will print information about a vote
@@ -1125,16 +1128,16 @@ func initDitRepository(_ditCoordinatorInstance *ditCoordinator.DitCoordinator, _
11251128
voteSettings[2] = big.NewInt(0)
11261129

11271130
// TODO
1128-
voteSettings[3] = big.NewInt(120)
1131+
voteSettings[3] = big.NewInt(150)
11291132

11301133
// TODO
1131-
voteSettings[4] = big.NewInt(121)
1134+
voteSettings[4] = big.NewInt(86400)
11321135

11331136
// TODO
1134-
voteSettings[5] = big.NewInt(120)
1137+
voteSettings[5] = big.NewInt(150)
11351138

11361139
// TODO
1137-
voteSettings[6] = big.NewInt(121)
1140+
voteSettings[6] = big.NewInt(86400)
11381141

11391142
// Prompting the user to provide 1 to 3 knowledge-labels for this repository
11401143
helpers.PrintLine("Please provide knowledge labels that will be used for this repository:", 0)

git/git.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,33 @@ func checkGitSetup() error {
111111
return nil
112112
}
113113

114+
// Validate will return an error when the connection to
115+
// the git provider fails (validated through a git fetch)
116+
func Validate() error {
117+
repoName, err := GetRepository()
118+
if err != nil {
119+
return err
120+
}
121+
122+
// Verifying whether a master branch (or any branch) exists
123+
cmdName := "git"
124+
cmdArgs := []string{"branch"}
125+
cmdOutBranch, err := exec.Command(cmdName, cmdArgs...).CombinedOutput()
126+
if err != nil {
127+
return errors.New("There was an error running git branch")
128+
}
129+
130+
// Verifying whether the connection works correctly
131+
cmdName = "git"
132+
cmdArgs = []string{"fetch", "-v"}
133+
cmdOutFetch, err := exec.Command(cmdName, cmdArgs...).CombinedOutput()
134+
if (!strings.Contains(string(cmdOutFetch), repoName) && len(cmdOutBranch) > 0) || err != nil {
135+
return errors.New("The connection to the git provider failed - please ensure that the repository is configured correctly")
136+
}
137+
138+
return nil
139+
}
140+
114141
// CheckForChanges will return an error if there are no new files to commit
115142
func CheckForChanges() error {
116143
// Verifying whether files have been added

main.go

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,24 @@ func main() {
105105
break
106106
case "commit", "propose_commit", "demo_commit":
107107
checkIfExists(args, 1, "a commit message")
108-
var voteDetails string
109-
var proposalID int
110-
111-
err = git.CheckForChanges()
108+
err = git.Validate()
112109
if err == nil {
113-
// Propose a new commit
114-
voteDetails, proposalID, err = ethereum.ProposeCommit(args[1])
110+
var voteDetails string
111+
var proposalID int
112+
113+
err = git.CheckForChanges()
115114
if err == nil {
116-
// Push the commit into a proposal branch
117-
err = git.Commit(proposalID, args[1])
115+
// Propose a new commit
116+
voteDetails, proposalID, err = ethereum.ProposeCommit(args[1])
118117
if err == nil {
119-
// Print the details of the vote
120-
stringLines := strings.Split(voteDetails, "\n")
121-
for i := range stringLines {
122-
helpers.PrintLine(stringLines[i], 0)
118+
// Push the commit into a proposal branch
119+
err = git.Commit(proposalID, args[1])
120+
if err == nil {
121+
// Print the details of the vote
122+
stringLines := strings.Split(voteDetails, "\n")
123+
for i := range stringLines {
124+
helpers.PrintLine(stringLines[i], 0)
125+
}
123126
}
124127
}
125128
}
@@ -160,26 +163,29 @@ func main() {
160163
break
161164
case "finalize", "finalize_vote":
162165
checkIfExists(args, 1, "a proposal ID")
163-
var pollPassed bool
164-
// Resolves a proposal
165-
pollPassed, err = ethereum.Finalize(args[1])
166+
err = git.Validate()
166167
if err == nil {
167-
fmt.Println()
168-
if pollPassed {
169-
// Merges the proposal branch into master after a successful vote
170-
err = git.Merge(args[1])
171-
if err == nil {
172-
helpers.PrintLine("Successfully merged dit proposal "+args[1]+" into the master branch", 0)
173-
}
174-
} else {
175-
// Deletes the proposal branch after an unsuccessful vote
176-
err = git.DeleteBranch(args[1])
177-
if err == nil {
178-
helpers.PrintLine("Removed the dit proposal "+args[1]+" from the repository", 0)
168+
var pollPassed bool
169+
var isProposer bool
170+
// Finalizes a proposal
171+
pollPassed, isProposer, err = ethereum.Finalize(args[1])
172+
if err == nil && isProposer {
173+
fmt.Println()
174+
if pollPassed {
175+
// Merges the proposal branch into master after a successful vote
176+
err = git.Merge(args[1])
177+
if err == nil {
178+
helpers.PrintLine("Successfully merged dit proposal "+args[1]+" into the master branch", 0)
179+
}
180+
} else {
181+
// Deletes the proposal branch after an unsuccessful vote
182+
err = git.DeleteBranch(args[1])
183+
if err == nil {
184+
helpers.PrintLine("Removed the dit proposal "+args[1]+" from the repository", 0)
185+
}
179186
}
180187
}
181188
}
182-
183189
break
184190
default:
185191
printUsage()

0 commit comments

Comments
 (0)