-
Notifications
You must be signed in to change notification settings - Fork 363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(blobstream): utilizes sdk.Dec type for power difference calculation #2719
Changes from 11 commits
8751950
db47d1d
ff5851a
20a1b59
cc712ab
6683a04
0d49079
73c7664
b3dcdcc
91080ff
b061f0c
97c1d42
463dcc4
d2d16cc
6de95f5
aa7a736
7e96c5b
c3cf767
5fe16f0
f7d07c7
bddfcfa
d3c7ae0
9951654
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,11 @@ package types | |
|
||
import ( | ||
"bytes" | ||
math "math" | ||
"math" | ||
"sort" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
|
||
"cosmossdk.io/errors" | ||
|
@@ -32,7 +34,7 @@ func (b BridgeValidators) ToInternal() (*InternalBridgeValidators, error) { | |
return &ret, nil | ||
} | ||
|
||
// Bridge Validator but with validated EVMAddress. | ||
// InternalBridgeValidator bridges Validator but with validated EVMAddress. | ||
type InternalBridgeValidator struct { | ||
Power uint64 | ||
EVMAddress common.Address | ||
|
@@ -112,11 +114,11 @@ func EVMAddrLessThan(e common.Address, o common.Address) bool { | |
// increases by 1% due to inflation, we shouldn't have to generate a new | ||
// validator set, after all the validators retained their relative percentages | ||
// during inflation and normalized Blobstream power shows no difference. | ||
func (ibv InternalBridgeValidators) PowerDiff(c InternalBridgeValidators) float64 { | ||
func (ibv InternalBridgeValidators) PowerDiff(c InternalBridgeValidators) sdk.Dec { | ||
powers := map[string]int64{} | ||
// loop over ibv and initialize the map with their powers | ||
for _, bv := range ibv { | ||
powers[bv.EVMAddress.Hex()] = int64(bv.Power) | ||
powers[bv.EVMAddress.Hex()] = int64(bv.Power) // TODO: overflow? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The validator powers are of type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch 👍 But I think if the state machine got to a state where this overflows, then something is seriously messed up with the staking module. In fact, values like those will not be accepted by the staking module, then we normalize those values before this step. I think that we can use a validation at this level as a last line of defense, but reaching it is super unlikely if I understand corerctly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So, does it mean that there will never be a case in which a validator's power reside in this range [math.MaxInt64, math.MaxUint64]? and it is guaranteed by the staking module? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, the validator power is using
The power is being reduced by a factor of:
However, the tokens amount is represented by an int256... So, there might be overflow there when going from tokens to consensus-engine power unless there is the assumption that the staked tokens wouldn't be that much. cc @evan-forbes any idea? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry mind pointing out where's the int256 to save me a sec 😅 this is good to keep in the back of our heads, especially for networks such as arabica where the supply and voting power is getting rather insane. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the Then, these are used when calculating the validator delegation and power in here after getting the |
||
} | ||
|
||
// subtract c powers from powers in the map, initializing | ||
|
@@ -135,7 +137,11 @@ func (ibv InternalBridgeValidators) PowerDiff(c InternalBridgeValidators) float6 | |
delta += math.Abs(float64(v)) | ||
} | ||
|
||
return math.Abs(delta / float64(math.MaxUint32)) | ||
decDelta := sdk.NewDec(int64(delta)) | ||
staheri14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
decMaxUint32 := sdk.NewDec(math.MaxUint32) | ||
q := decDelta.Quo(decMaxUint32) | ||
|
||
return q.Abs() | ||
} | ||
|
||
// TotalPower returns the total power in the bridge validator set. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[note to the reviewers] Alternatively, we could perform all calculations using
sdk.Dec
from the outset. However, since there are no floating-point calculations involved until later in this function, I thought it might be more efficient to continue using integer operations until that point. Nevertheless, if it is preferable to usesdk.Dec
arithmetic throughout, I can update the initial portion of the function accordingly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think switching to
sdk.Dec
would be also future-proof in case we wanted to make any changes not to give to future devs the ability to choose. So, I would go with switching everything tosdk.Dec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your comment!
I'm unsure how this change would effectively enforce any constraints on future developers when the function signature remains unchanged, and only its internals are to be modified to utilize
sdk.Dec
. Could you please provide more insight into your perspective?