Skip to content

make VotingPower uint64 (Refs #344) - #354

Closed
melekes wants to merge 1 commit into
tendermint:unstablefrom
melekes:bugfix/negative-voting-power-and-overflow
Closed

melekes wants to merge 1 commit into
tendermint:unstablefrom
melekes:bugfix/negative-voting-power-and-overflow

Conversation

@melekes

@melekes melekes commented Jan 9, 2017 •

Copy link
Copy Markdown
Contributor

Also, deal with possible overflow issues.

This change breaks the test right now.

Is this a correct move? or should we let votingPower be negative? need to know before doing anything else.

Also deal with possible overflow issues.
@ethanfrey

Copy link
Copy Markdown
Contributor

I agree negative voting power is VERY bad. It is a backdoor in the system.

A, B, C, D all have 1 vote and 3 are needed for consensus. Grant E -3 votes, and suddenly 1 vote = 100% of total votes. Oops!

On the other hand, if we make it a uint, and some logic goes votes -= 1, where votes was already 0, suddenly that person gets 2^64-1 votes. Yikes!

I would leave it as int64, so we can detect underflow better. But add checks everytime it is set to return an error upon setting it negative.

Just my two cents...

Ex. see code sample:

package main

import "fmt"

func main() {
  var a uint64 = 1
  a -= 2
  fmt.Println(a)
}

@melekes

melekes commented Jan 9, 2017

Copy link
Copy Markdown
Contributor Author

On the other hand, if we make it a uint, and some logic goes votes -= 1, where votes were already 0, suddenly that person gets 2^64-1 votes. Yikes!

Underflow case in covered in this PR. Just to let you know. Not judging your opinion in any way. Let's wait what @ebuchman will say.

Comment thread types/validator_set.go
}

// addOrMax performs safe addition: if result overflows, it returns MaxUint64
func addOrMax(accum, value uint64) uint64 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I see. Very important functions here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ebuchman

ebuchman commented Jan 9, 2017

Copy link
Copy Markdown
Contributor

Hm. Obviously uint64 makes sense, assuming proper protections. I think at one point we went and changed everything from uint to int for consistency and simplicity of arithmetic. @jaekwon will have to weigh in here.

@akalyaev , how did you find the negative voting power in the first place? Isn't it something we can just prevent when the validator diffs are returned?

Comment thread types/validator.go
votePower := minPower
if randPower {
votePower += int64(RandUint32())
votePower += RandUint64()

@ethanfrey ethanfrey Jan 9, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this have a (tiny) chance of overflowing. Like (minPower / 2^64). Still not quite 0....

addOrMax(votePower, RandUint64())

Also, in this case with eg. 5 validators, it will be likely that the sum of all power is greater than math.MaxUint64. Maybe RandUint32() is better?

@melekes

melekes commented Jan 9, 2017

Copy link
Copy Markdown
Contributor Author

@ebuchman here #344 (comment)

@melekes

melekes commented Jan 9, 2017 •

Copy link
Copy Markdown
Contributor Author

Isn't it something we can just prevent when the validator diffs are returned?

Maybe, but it will break the app logic/tests (like this PR does).

https://github.com/akalyaev/tendermint/blob/d90d7258886d15b0f97e8f7d431e5e94b3dfcb3c/types/validator_set_test.go#L88

	// One validator has more than the others, but not enough to propose twice in a row
	*val3 = *newValidator(addr3, 400)
	vals = NewValidatorSet(valList)
	prop := vals.Proposer()
	if !bytes.Equal(prop.Address, addr3) {
		t.Fatalf("Expected address with highest voting power to be first proposer. Got %X", prop.Address)
	}
	vals.IncrementAccum(1)
	prop = vals.Proposer()
	if !bytes.Equal(prop.Address, addr1) {
		t.Fatalf("Expected smallest address to be validator. Got %X", prop.Address)
	}

val3 before IncrementAccum will have voting power = -200.

Comment thread state/execution.go

address := pubkey.Address()
power := int64(v.Power)
power := uint64(v.Power)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the check @ebuchman was referring to I think. We make sure they can't overflow when the TMSP app sends a validator set update. This worked for int64 or int32, since it is clear when there is a negative value.

Let's say the tmsp app has a tx that reduces a voting power by 2. And it doesn't check underflow. If the user was already at 1, they would do the bad math in the app => math.Maxunit64, which is sent to tendermint core, which cannot reliably distinguish if there was an error in the app, or this validator is suddenly REALLY important.

The old code at least would catch and report errors in app code. But I do agree, internally it is a bit annoying to have ints when you want uints...

Not sure the solution, but core should be defensive against errors in tmsp apps.

@jaekwon

jaekwon commented Jan 20, 2017

Copy link
Copy Markdown
Contributor

I'm in agreement with EthanFrey. I used to think that uintxx's were better, but their usage w/ arithmetic operations makes it more bug prone, and the code more complicated. It's much easier to add/subtract as necessary and check the sign.

Take validator diffs for example... a diff should be signed because it could make the validator voting power go up or down. So it should be an int64, but if VotingPower is a uint64, it creates an annoying type mismatch. We can use special functions to check for overflow/underflow, but somebody will forget somewhere.

So now I prefer to think of int64's sometimes as uint63's with a magical underflow check.

@jaekwon

jaekwon commented Jan 20, 2017

Copy link
Copy Markdown
Contributor

I'd even change abci.Validator's voting power to be an int64. Whenever addition/subtraction will happen frequently with a number, I'd opt for signed numbers. This doesn't happen often with block.Height, or partSet.Total, for example, so they are uintxx's.

@jaekwon

jaekwon commented Jan 20, 2017

Copy link
Copy Markdown
Contributor

BTW not sure why I missed this, but if I don't respond to an issue in 2 days feel free to just ping me on Slack.

@melekes

melekes commented Jan 20, 2017

Copy link
Copy Markdown
Contributor Author

@jaekwon good point! I think we can close this now.

@melekes melekes closed this Jan 20, 2017
adrianbrink pushed a commit to heliaxdev/tendermint that referenced this pull request May 23, 2023
)

by moving the `codecov.yml` file from .github the root folder.

---

#### PR checklist

- [x] Tests written/updated
- [x] Changelog entry added in `.changelog` (we use [unclog](https://github.com/informalsystems/unclog) to manage our changelog)
- [x] Updated relevant documentation (`docs/` or `spec/`) and code comments

(cherry picked from commit c906604)

Co-authored-by: Lasaro <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants