Commit Graph

46 Commits

Author SHA1 Message Date
mergify[bot] ad4ad43f02
fix: decrement types.Dec max length to keep decimal bits in DecimalPrecisionBits (backport #11772) (#11805) 2022-04-27 14:16:29 -04:00
mergify[bot] 11bda928d8
fix(types): use correct bit length constant for sdk.Dec (backport #11332) (#11335)
* Use correct bit length constant for sdk.Dec (#11332)

Co-authored-by: Marko <marbar3778@yahoo.com>
(cherry picked from commit 3d3cf25546)

# Conflicts:
#	go.sum

* fix conflicts

Co-authored-by: Julien Robert <julien@rbrt.fr>
Co-authored-by: marbar3778 <marbar3778@yahoo.com>
2022-03-09 13:26:35 +01:00
Venkatesh Mankena 3841df6468
fix: Handle MAX_INT_256 (#9511)
Change maxBitLen of sdk.Int to handle max Erc20 value.

<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

Closes: #XXXX

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2021-06-16 14:18:02 +00:00
Techno Freak 9536ba7622
feat: add Dec.Float64() function (#9382)
* feat: add Dec.Float64() function

* chore: add CHANGELOG.md release entry

* chore: added MustFloat64(), return result with error on Float64()

* chore: add godoc

* Update types/decimal.go

Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>

* Update types/decimal.go

Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>

* chore: re-ordered changelog

Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>
2021-05-26 09:02:14 +00:00
Cuong Manh Le 3e6c4a6c79
types: make chopPrecisionAndTruncate always non-mutative (#9250)
Currently, chopPrecisionAndTruncate/chopPrecisionAndTruncateNonMutative
are doing the same thing, except that the former do it mutatively.

Looking at the usages of chopPrecisionAndTruncate, the callers must
alloc a new big.Int before calling it anyway, so any mutation will be
done with this new allocated big.Int, instead of input argument.

So, by making chopPrecisionAndTruncate always non-mutative, we simplify
the code, and make the code faster, less allocation in some benchmarks.

Benchmark for github.com/cosmos/cosmos-sdk/types:

name                                           old time/op    new time/op    delta
CoinsAdditionIntersect/sizes:_A_1,_B_1-8         35.4ns ± 0%    35.1ns ± 0%  -0.92%  (p=0.008 n=5+5)
CoinsAdditionIntersect/sizes:_A_5,_B_5-8          366ns ± 1%     363ns ± 1%    ~     (p=0.056 n=5+5)
CoinsAdditionIntersect/sizes:_A_5,_B_20-8         557ns ± 0%     553ns ± 0%  -0.64%  (p=0.008 n=5+5)
CoinsAdditionIntersect/sizes:_A_1,_B_1000-8      6.17µs ± 0%    6.18µs ± 0%    ~     (p=0.151 n=5+5)
CoinsAdditionIntersect/sizes:_A_2,_B_1000-8      6.30µs ± 0%    6.32µs ± 0%  +0.23%  (p=0.008 n=5+5)
CoinsAdditionNoIntersect/sizes:_A_1,_B_1-8       30.2ns ± 0%    29.9ns ± 1%  -0.98%  (p=0.008 n=5+5)
CoinsAdditionNoIntersect/sizes:_A_5,_B_5-8        226ns ± 0%     224ns ± 0%  -0.54%  (p=0.008 n=5+5)
CoinsAdditionNoIntersect/sizes:_A_5,_B_20-8       373ns ± 0%     372ns ± 0%  -0.26%  (p=0.040 n=5+5)
CoinsAdditionNoIntersect/sizes:_A_1,_B_1000-8    6.21µs ± 0%    6.22µs ± 0%    ~     (p=0.421 n=5+5)
CoinsAdditionNoIntersect/sizes:_A_2,_B_1000-8    6.22µs ± 0%    6.23µs ± 0%    ~     (p=0.286 n=5+5)
CoinsAdditionNoIntersect/sizes:_A_1000,_B_2-8    6.22µs ± 0%    6.23µs ± 0%    ~     (p=0.143 n=5+5)
ParseCoin-8                                      5.46µs ± 0%    5.39µs ± 0%  -1.26%  (p=0.008 n=5+5)
UintMarshal-8                                    1.01µs ± 0%    1.00µs ± 0%  -1.10%  (p=0.008 n=5+5)
IntMarshal-8                                      764ns ± 0%     758ns ± 1%  -0.79%  (p=0.024 n=5+5)
MarshalTo-8                                       192ns ± 0%     190ns ± 0%  -1.03%  (p=0.008 n=5+5)

name                                           old alloc/op   new alloc/op   delta
CoinsAdditionIntersect/sizes:_A_1,_B_1-8          32.0B ± 0%     32.0B ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_5,_B_5-8           520B ± 0%      520B ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_5,_B_20-8        1.38kB ± 0%    1.38kB ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_1,_B_1000-8      49.2kB ± 0%    49.2kB ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_2,_B_1000-8      49.3kB ± 0%    49.3kB ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_1,_B_1-8        24.0B ± 0%     24.0B ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_5,_B_5-8         488B ± 0%      488B ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_5,_B_20-8      1.35kB ± 0%    1.35kB ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_1,_B_1000-8    49.2kB ± 0%    49.2kB ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_2,_B_1000-8    49.2kB ± 0%    49.2kB ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_1000,_B_2-8    49.2kB ± 0%    49.2kB ± 0%    ~     (all equal)
ParseCoin-8                                      2.21kB ± 0%    2.21kB ± 0%    ~     (all equal)
UintMarshal-8                                      392B ± 0%      392B ± 0%    ~     (all equal)
IntMarshal-8                                       168B ± 0%      168B ± 0%    ~     (all equal)
MarshalTo-8                                       80.0B ± 0%     80.0B ± 0%    ~     (all equal)

name                                           old allocs/op  new allocs/op  delta
CoinsAdditionIntersect/sizes:_A_1,_B_1-8           1.00 ± 0%      1.00 ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_5,_B_5-8           12.0 ± 0%      12.0 ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_5,_B_20-8          14.0 ± 0%      14.0 ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_1,_B_1000-8        3.00 ± 0%      3.00 ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_2,_B_1000-8        6.00 ± 0%      6.00 ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_1,_B_1-8         1.00 ± 0%      1.00 ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_5,_B_5-8         5.00 ± 0%      5.00 ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_5,_B_20-8        6.00 ± 0%      6.00 ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_1,_B_1000-8      3.00 ± 0%      3.00 ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_2,_B_1000-8      4.00 ± 0%      4.00 ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_1000,_B_2-8      3.00 ± 0%      3.00 ± 0%    ~     (all equal)
ParseCoin-8                                        71.0 ± 0%      71.0 ± 0%    ~     (all equal)
UintMarshal-8                                      25.0 ± 0%      25.0 ± 0%    ~     (all equal)
IntMarshal-8                                       18.0 ± 0%      18.0 ± 0%    ~     (all equal)
MarshalTo-8                                        2.00 ± 0%      2.00 ± 0%    ~     (all equal)

name                                           old speed      new speed      delta
UintMarshal-8                                  6.91MB/s ± 0%  6.99MB/s ± 0%  +1.04%  (p=0.008 n=5+5)
IntMarshal-8                                   9.16MB/s ± 0%  9.23MB/s ± 1%  +0.83%  (p=0.032 n=5+5)

Benchmark for github.com/cosmos/cosmos-sdk/x/mint/types:

name              old time/op    new time/op    delta
BlockProvision-8     263ns ± 0%     257ns ± 0%   -2.23%  (p=0.008 n=5+5)

name              old alloc/op   new alloc/op   delta
BlockProvision-8      112B ± 0%      104B ± 0%   -7.14%  (p=0.008 n=5+5)

name              old allocs/op  new allocs/op  delta
BlockProvision-8      6.00 ± 0%      5.00 ± 0%  -16.67%  (p=0.008 n=5+5)

Fixes #9249

Co-authored-by: Emmanuel T Odeke <emmanuel@orijtech.com>
2021-05-04 00:32:15 +01:00
Cuong Manh Le e28271b8e6
types: make NewDecFromStr returns error for too large decimal (#9157)
Otherwise, NewDecFromStr may accept very large input, causing Dec
methods panic, e.g Dec.TruncateInt

Found by oss-fuzz: https://oss-fuzz.com/testcase-detail/6454129938530304

Fixes #9160
2021-04-22 11:21:27 +01:00
Alessio Treglia f2ee972e31
various linter fixes (#8666) 2021-02-23 08:46:01 +00:00
Emmanuel T Odeke 21716644d4
types: unconditionally divide by 2 because of integer truncation ⌊x/2⌋ (#8202)
Simplify the code in Dec.Power to unconditionally divide by 2, since:
    x /= 2
is the same result for both even and odd integers, and produces the
floor of the result of x/2 -> ⌊x/2⌋:
    99/2 ~> 49.5 ⌊49.5⌋ = 49
    98/2 ~> 49.0 ⌊49.0⌋ = 49
aka "integer truncation".

Fixes #7924
2020-12-20 10:41:59 -08:00
Miguel Dingli 018915b1a8
Fix ApproxRoot Infinite Looping (#7140)
Added a maximum number of iterations (100) to ApproxRoot
(and ApproxSqrt) to serve as a hard limit on the number of
iterations that the algorithm performs. If the answer converges
before the maximum iterations, it returns immediately. Otherwise,
if the answer never converges enough to satisfy the primary loop
condition, the looping stops after a hundred iterations.

Closes: #7038

Co-authored-by: Alessio Treglia <quadrispro@ubuntu.com>
2020-08-29 11:13:36 +01:00
Federico Kunze 7f0c3f0093
Merge PR #7084: types: fix panic on Int.BigInt() 2020-08-18 09:16:23 -04:00
Alessio Treglia 2ba21d33ab
types: better Dec.MarshalTo's performance (#6691)
Use big.Int.Cmp() and compare against zeroInt instead
of checking Bytes() against a 0-length byte slice.

Closes: #6680
Thanks: @odeke-em for the patch.

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
2020-07-12 13:26:51 +00:00
Marko 218ec99508
various linter fixes (#6106)
x/staking: Fix all linter warnings.

Fixed warnings across base packages.

New linters:
- unparam
- nolintlint

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
2020-05-02 21:26:59 +02:00
Alexander Bezobchuk 53bf2271d5
Merge PR #5600: Migrate x/staking to Protobuf 2020-02-06 14:21:02 -05:00
Alexander Bezobchuk 26d6e49d6a
Merge PR #5491: Protobuf Introduction + Types 2020-01-24 10:32:00 -05:00
Sunny Aggarwal 3fc6240c94 Merge PR #5447: Added nth root function to sdk.Decimal type 2020-01-04 15:16:12 -05:00
Alexander Bezobchuk 9a183ffbcc
Merge PR #5421: Refactor Error Handling 2019-12-27 12:57:54 -05:00
Sunny Aggarwal 3c2ace9510 Merge PR #5360: Sortable Decimal Bytes 2019-12-05 12:55:43 -05:00
Sunny Aggarwal d7a08ce5f2 Merge PR #5219: Added sqrt function to sdk.Decimal type 2019-10-18 12:25:07 -04:00
Marko 9f3789157e Merge PR #5192: golangcI-lint updates & set static version to 1.19 2019-10-14 11:43:19 -04:00
Marko 3a4f1fc4d4 Merge PR #4881: Linting Galore 2019-08-19 12:06:27 -04:00
Alessio Treglia 55928ad165
Fix BaseAccount's YAML serialization (#4577) 2019-06-18 18:03:16 +02:00
frog power 4000 38f49e4114
Merge PR #4389: stake invariance bug
* add trouble seed

* currentStakeRoundUp is now always atleast currentStake + smallest-decimal-precision

* remove unused code

* remove debugs

* @alexanderbez comment

* compile fix

* better comment, increase tolerance to 3 smallest decimal points
2019-05-24 21:23:53 -04:00
Alexander Bezobchuk 5f92fef4b0
Fix TruncateDecimal (#3913) 2019-03-16 16:14:37 -07:00
Christopher Goes 4c50380181
Merge PR #3750: Outstanding per-validator rewards; correctly handle same-BeginBlock redelegation-double-slash 2019-03-06 19:54:12 +01:00
frog power 4000 52f2ec71a9
Merge PR #3400: power reduction for Tendermint
* add uncompiled power functionality

* fix some compile errors

* Power -> TendermintPower

* tests rename GetTendermintPower

* test fix

* working

* fix delegation tests

* fix slash tests

* staking/keeper tests passing

* docs reversion

* debuggin workin

* x/staking test pass

* fix gov tests

* fix x/slashing tests

* working distribution test fixes

* fix distribution tests

* lint

* fix lcd tests

* fix gov test

* lint

* CLI fixes, rm stakingTypes

* typos

* working cli fixes

* cli test fix

* cli tests fixed

* testnet creation modification

* typo

* pending

* Sanitize Dec.Roundint64 (#3475)

* merge fixes

* @cwgoes comments

* fix tests

* change power reduction to 10^-6

* option to turn off minting for LCD tests
2019-02-05 21:30:48 -08:00
Jack Zampolin af60c75dd3 Merge PR #3352: Reenable simulation tests 2019-01-24 22:01:32 +01:00
Alexander Bezobchuk 36d1736a08 Refactor Gas/Fee Model (#3258) 2019-01-18 08:45:20 -08:00
frog power 4000 d9e32e93d5 Merge PR #3315: Increase decimal precision to 18
* Decimal precision increase WIP
* fix String()
* resolve many errors
* changelog
2019-01-17 18:15:10 +01:00
Jae Kwon bc51fa93b6
Fix updateValidatorDistInfoFromPool (#3046)
Fixes regression introduced by #2984.
Continuiation of #3033 , which didn't fix the simulation issues.
(candidate) Complete solution for #3019, 9002 halt bug.

From #2984, it isn't sufficient to take the fee pool rewards of a validator. Since we don't track delegator accums (as we do with validator accums), and because onValidatorModified >updateValidatorDistInfoFromPool is also being called upon delegation updates (or at least I believe this is the reason), it is necessary to also withdraw self delegation.

TODO: I don't think self-delegation should be required to be modified here... consider using a delegation hook to do the self-delegation withdraw part instead, e.g. splitting the updateValidatorDistInfoFromPool function into two. It might not result in cleaner code, however. Think hard.
2018-12-08 07:18:04 -08:00
Jae Kwon 1ba93ea1c1 Merge PR #3033: Fix negative stake & invariance bug
* Fix negative stake & invariance bug

* Merge PR #3037: Updates to negative stake fix

* Update invariant; fix lint

* Fix linter

* Add comment & TODO
2018-12-08 01:04:52 +01:00
frog power 4000 7cb1ba625e blockly minting (#2825)
* update mechanism to use average block time
* correctly sets accum height for zero-delegations
* update Decimal Format()
* clip withdrawal tokens
* PositiveDelegationInvariant
* DelegatorSharesInvariant
* DelAccumInvariants
2018-11-26 04:13:47 -08:00
Christopher Goes 306fb8fb6d Address @rigelrozanski comments 2018-10-23 20:33:39 +02:00
Christopher Goes 33c8238392 Merge branch 'develop' into cwgoes/check-supply-in-simulation 2018-10-23 17:54:58 +02:00
Jae Kwon 1cc74320df Replicate Rigel's changes but w/ modifications as discussed + some name changes 2018-10-22 01:46:47 -07:00
ValarDragon 1a463eb056 make more clear 2018-10-20 18:02:17 -07:00
ValarDragon c2d68928e7 Switch to new Decimal.String() implementation 2018-10-20 13:32:43 -07:00
ValarDragon 20cc13e5bf types: Dec.MarshalJSON now marshals as a normal decimal string
Closes #2475
2018-10-16 09:29:32 -07:00
rigelrozanski 2f4a01bcba address @ValarDragon comments, param store update 2018-10-15 15:51:51 -04:00
rigelrozanski 06fa518e30 address @cwgoes comments, add in precommit calculation 2018-10-12 07:15:13 -04:00
rigelrozanski 1e74da1abe validator_info tests 2018-10-04 03:00:24 -04:00
Dev Ojha e11c52e873 Merge PR #2408: decimal: Add a method to multiply a decimal by an Int
This is for usage within the slash function, to minimize operations
required to slash each unbonded and redelegating account.
2018-09-26 18:13:40 +08:00
Rigel de30281afa Distr-PR-2 Truncate Decimal Functionality (#2379) 2018-09-24 21:18:18 -07:00
Alexander Bezobchuk 9dafa3252d Merge PR #2365: Validator Commission Model
* Update validator commission fields

* Remove CommissionChangeToday and update to use CommissionChangeTime

* Implement commission as a first class citizen type

* Implement stringer for Comission

* Move commission type and logic to new  file

* Add new commission errors

* Add commission to create validator message

* Implement and call UpdateValidatorCommission

* Update godoc for UpdateValidatorCommission

* Add Abs to the decimal type

* Implement new SetValidatorCommission

* Update decimal short godocs

* Move set initial commission logic

* Move initial commission validation to Commission type

* Update initial validator commission logic and unit tests

* Remove commission update time from struct and move to validator

* Update validator create handler tests

* Implement commission logic for CLI

* Fix make lint failure

* Fix make cover failure

* Update edit validator logic to handle new commission rate

* Fix lint and cover

* Update create/edit validator simulation to include commission params

* Update MsgEditValidator godoc

* Update pending log

* Update staking tx docs

* Fix CLI create validator test

* Update variables names for commission  strings

* Merge UpdateTime into Commission type

* Update create-validator usage in docs

* Update more docs with examples

* More doc updates
2018-09-24 18:23:58 -04:00
Christopher Goes 1204857694 Merge PR #2122: Implement slashing period
* Update PENDING.md

* SlashingPeriod struct

* Seperate keys.go, constant prefixes

* Make linter happy

* Update Gopkg.lock

* Seek slashing period by infraction height

* Slashing period hooks

* Slashing period unit tests; bugfix

* Add simple hook tests

* Add sdk.ValidatorHooks interface

* No-op hooks

* Real hooks

* Fix iteration direction & duplicate key, update Gaia

* Correctly simulate past validator set signatures

* Tiny rename

* Update dep; 'make format'

* Add quick slashing period functionality test

* Additional unit tests

* Use current validators when selected

* Panic in the right place

* Address @rigelrozanski comments

* Fix linter errors

* Address @melekes suggestion

* Rename hook

* Update for new bech32 types

* 'make format'
2018-08-31 20:01:23 -04:00
Dev Ojha d37de06762 decimal: Remove unnecessary allocation in bankers round chop (#2030)
This is done by making the function mutative. A non-mutative variant
is created for functions that depend on it being non-mutative.
2018-08-14 21:49:40 -04:00
Rigel d9dc061b4f
Merge pull request #1819: rational -> decimal
* changelog

* ...

* decimal func working

* decimal complete, untested

* fixing tests

* decimal compile errors resolved

* test compile errors

* precision multiplier test

* 1% laptop battery

* fixed TestNewDecFromStr

* equalities working

* fix bankers round chop

* ...

* working, some decimal issues resolved

* fix rounding error

* rounding works

* decimal works

* ...

* deleted rational

* rational conversion working

* revert changelog

* code compiles (not tests)

* went through all NewDec, made sure they were converted from NewRat properly

* test debugging

* all testing bugs besides the json marshalling fixed

* json unmarshal

* lint

* document update

* fix lcd test

* cli test fix

* mostly undo Dece -> Rate

* val comments

* Efficiency improvements

This now caches all of the precision multipliers (as they were all
used in non-mutative functions), and caches the precisionInt calculation.
(Now it just copies the already calculated value)

* Cache another precisionInt() call.

* Improve banker rounding efficiency

* remove defer, make negation in-place.

* chris val comments

* bez comments

* Aditya comments

* ...

* val comments

* rebasing start

* ...

* compiling

* tests pass

* cli fix

* anton, cwgoes, val comments

* val and jae comments

* type

* undo reuse quo
2018-08-14 20:15:02 -04:00