Skip to content
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

[R4R] Add Safety Measures to Coin/Coins #2797

Merged
merged 50 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
00a3f91
Partial implementation of coins => uint conversion
Nov 12, 2018
e67f179
DRY-up uint overflow logic
Nov 13, 2018
cc4616d
Improve Coins#Plus godoc
Nov 13, 2018
96db7b8
Implement modular sumCoins
Nov 13, 2018
85bf273
Update context tests
Nov 14, 2018
dc7cff4
Partial coins tests update
Nov 14, 2018
303cb3b
Remove IsPositive/IsNegative APIs and add Empty method
Nov 14, 2018
6d9b269
coins structure cleanup
Nov 14, 2018
6105342
Sort coins when comparing in Equal
Nov 14, 2018
0ee4825
Fix sum when remaining coins include zero coins
Nov 14, 2018
3c9df28
Move benchmarks to separate file
Nov 14, 2018
a4557ac
Implement SafeSub for Uint type
Nov 14, 2018
1533f49
Implement SafeMinus for Coin type
Nov 14, 2018
657c5b4
Add godoc for sumCoins
Nov 14, 2018
271cfc5
Partial coins update
Nov 15, 2018
0e5fbbd
Finish (preliminary) coins integer handling update
Nov 15, 2018
18b362c
Remove old coin op
Nov 15, 2018
fc7c9d2
Fix bug in denom check
Nov 15, 2018
600e49d
Update coins unit tests
Nov 15, 2018
8a213b2
Update IsValid to return false if 0 or negative
Nov 15, 2018
887b9f1
Improve coins godocs
Nov 15, 2018
6dae265
Merge branch 'develop' into bez/2776-uint-coins
Nov 15, 2018
2ef0a7f
Add TODO to coins type
Nov 15, 2018
36f3727
Update ante handler gas check
Nov 15, 2018
821ccf3
Update Plus godoc
Nov 15, 2018
d12fc03
Cleanup safePlus API
Nov 15, 2018
66bae3b
Add coin type TODO
Nov 15, 2018
e373950
Update and improve coin amount godoc
Nov 16, 2018
ddbe5cb
Update Coin#Minus godoc
Nov 16, 2018
ccd1080
Fix IsValid for single coin case
Nov 16, 2018
9a2b144
Update Plus godoc
Nov 16, 2018
98445af
Update Minus godoc
Nov 16, 2018
467269e
Modify coinsB in place during Minus
Nov 16, 2018
2fe1ecc
Rename bool variable from SafeMinus
Nov 16, 2018
5cffaad
DRY-up IsAllLT and IsAllLTE
Nov 16, 2018
35da226
Clarify sorting in IsEqual
Nov 16, 2018
5450548
Update removeZeroCoins to modify in place
Nov 16, 2018
96b6a74
DRY-up Coins#Minus and remove in-place logic
Nov 16, 2018
562dc6f
Panic on diff denom in Minus
Nov 16, 2018
5b3fdae
Add additional safety checks and revert API
Nov 16, 2018
3c14b20
Revert ante coin API update
Nov 16, 2018
746c083
Further cleanup and fix coverage
Nov 16, 2018
e2a84c6
Add PENDING log entry
Nov 16, 2018
0751925
Merge branch 'develop' into bez/2776-uint-coins
Nov 16, 2018
769cf56
Cleanup safePlus
Nov 19, 2018
7fff94c
Replace coin literals with constructor variants
Nov 19, 2018
3d1854c
Add test cases for zero amounts to plus/minus
Nov 19, 2018
68b508d
Merge branch 'develop' into bez/2776-uint-coins
Nov 19, 2018
7ad1672
Remove hasNeg check from LT/GT
jaekwon Nov 20, 2018
16d06a5
Merge branch 'develop' into bez/2776-uint-coins
jaekwon Nov 20, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ IMPROVEMENTS

* SDK
- [x/mock/simulation] [\#2720] major cleanup, introduction of helper objects, reorganization
- \#2821 Codespaces are now strings
- [types] #2776 Improve safety of `Coin` and `Coins` types. Various functions
and methods will panic when a negative amount is discovered.
- #2815 Gas unit fields changed from `int64` to `uint64`.
- #2821 Codespaces are now strings

* Tendermint
- #2796 Update to go-amino 0.14.1
Expand Down
6 changes: 4 additions & 2 deletions cmd/gaia/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,12 @@ func CollectStdTxs(cdc *codec.Codec, moniker string, genTxsDir string, genDoc tm
func NewDefaultGenesisAccount(addr sdk.AccAddress) GenesisAccount {
accAuth := auth.NewBaseAccountWithAddress(addr)
coins := sdk.Coins{
{"fooToken", sdk.NewInt(1000)},
{bondDenom, freeFermionsAcc},
sdk.NewCoin("fooToken", sdk.NewInt(1000)),
sdk.NewCoin(bondDenom, freeFermionsAcc),
}

coins.Sort()

accAuth.Coins = coins
return NewGenesisAccount(&accAuth)
}
2 changes: 1 addition & 1 deletion cmd/gaia/app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func appStateFn(r *rand.Rand, accs []simulation.Account) json.RawMessage {

// Randomly generate some genesis accounts
for _, acc := range accs {
coins := sdk.Coins{sdk.Coin{stakeTypes.DefaultBondDenom, sdk.NewInt(amount)}}
coins := sdk.Coins{sdk.NewCoin(stakeTypes.DefaultBondDenom, sdk.NewInt(amount))}
genesisAccounts = append(genesisAccounts, GenesisAccount{
Address: acc.Address,
Coins: coins,
Expand Down
2 changes: 1 addition & 1 deletion docs/_attic/sdk/core/examples/app2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestEncoding(t *testing.T) {
sendMsg := MsgSend{
From: addr1,
To: addr2,
Amount: sdk.Coins{{"testCoins", sdk.NewInt(100)}},
Amount: sdk.Coins{sdk.NewCoin("testCoins", sdk.NewInt(100))},
}

// Construct transaction
Expand Down
10 changes: 5 additions & 5 deletions docs/_attic/sdk/core/examples/app4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func InitTestChain(bc *bapp.BaseApp, chainID string, addrs ...sdk.AccAddress) {
for _, addr := range addrs {
acc := GenesisAccount{
Address: addr,
Coins: sdk.Coins{{"testCoin", sdk.NewInt(100)}},
Coins: sdk.Coins{sdk.NewCoin("testCoin", sdk.NewInt(100))},
}
accounts = append(accounts, &acc)
}
Expand Down Expand Up @@ -61,12 +61,12 @@ func TestBadMsg(t *testing.T) {
addr2 := priv2.PubKey().Address().Bytes()

// Attempt to spend non-existent funds
msg := GenerateSpendMsg(addr1, addr2, sdk.Coins{{"testCoin", sdk.NewInt(100)}})
msg := GenerateSpendMsg(addr1, addr2, sdk.Coins{sdk.NewCoin("testCoin", sdk.NewInt(100))})

// Construct transaction
fee := auth.StdFee{
Gas: 1000000000000000,
Amount: sdk.Coins{{"testCoin", sdk.NewInt(0)}},
Amount: sdk.Coins{sdk.NewCoin("testCoin", sdk.NewInt(0))},
}
signBytes := auth.StdSignBytes("test-chain", 0, 0, fee, []sdk.Msg{msg}, "")
sig, err := priv1.Sign(signBytes)
Expand Down Expand Up @@ -108,11 +108,11 @@ func TestMsgSend(t *testing.T) {
InitTestChain(bc, "test-chain", addr1)

// Send funds to addr2
msg := GenerateSpendMsg(addr1, addr2, sdk.Coins{{"testCoin", sdk.NewInt(100)}})
msg := GenerateSpendMsg(addr1, addr2, sdk.Coins{sdk.NewCoin("testCoin", sdk.NewInt(100))})

fee := auth.StdFee{
Gas: 1000000000000000,
Amount: sdk.Coins{{"testCoin", sdk.NewInt(0)}},
Amount: sdk.Coins{sdk.NewCoin("testCoin", sdk.NewInt(0))},
}
signBytes := auth.StdSignBytes("test-chain", 0, 0, fee, []sdk.Msg{msg}, "")
sig, err := priv1.Sign(signBytes)
Expand Down
10 changes: 5 additions & 5 deletions docs/examples/democoin/x/cool/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ func TestMsgQuiz(t *testing.T) {
// Set the trend, submit a really cool quiz and check for reward
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{setTrendMsg1}, []int64{0}, []int64{0}, true, true, priv1)
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{quizMsg1}, []int64{0}, []int64{1}, true, true, priv1)
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"icecold", sdk.NewInt(69)}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("icecold", sdk.NewInt(69))})
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{quizMsg2}, []int64{0}, []int64{2}, false, false, priv1) // result without reward
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"icecold", sdk.NewInt(69)}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("icecold", sdk.NewInt(69))})
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{quizMsg1}, []int64{0}, []int64{3}, true, true, priv1)
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"icecold", sdk.NewInt(138)}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("icecold", sdk.NewInt(138))})
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{setTrendMsg2}, []int64{0}, []int64{4}, true, true, priv1) // reset the trend
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{quizMsg1}, []int64{0}, []int64{5}, false, false, priv1) // the same answer will nolonger do!
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"icecold", sdk.NewInt(138)}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("icecold", sdk.NewInt(138))})
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{quizMsg2}, []int64{0}, []int64{6}, true, true, priv1) // earlier answer now relevant again
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"badvibesonly", sdk.NewInt(69)}, {"icecold", sdk.NewInt(138)}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("badvibesonly", sdk.NewInt(69)), sdk.NewCoin("icecold", sdk.NewInt(138))})
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{setTrendMsg3}, []int64{0}, []int64{7}, false, false, priv1) // expect to fail to set the trend to something which is not cool
}
6 changes: 3 additions & 3 deletions docs/examples/democoin/x/pow/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ func TestMsgMine(t *testing.T) {
// Mine and check for reward
mineMsg1 := GenerateMsgMine(addr1, 1, 2)
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{mineMsg1}, []int64{0}, []int64{0}, true, true, priv1)
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"pow", sdk.NewInt(1)}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("pow", sdk.NewInt(1))})
// Mine again and check for reward
mineMsg2 := GenerateMsgMine(addr1, 2, 3)
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{mineMsg2}, []int64{0}, []int64{1}, true, true, priv1)
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"pow", sdk.NewInt(2)}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("pow", sdk.NewInt(2))})
// Mine again - should be invalid
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{mineMsg2}, []int64{0}, []int64{1}, false, false, priv1)
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"pow", sdk.NewInt(2)}})
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("pow", sdk.NewInt(2))})
}
2 changes: 1 addition & 1 deletion docs/intro/ocap.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ principle:
type AppAccount struct {...}
var account := &AppAccount{
Address: pub.Address(),
Coins: sdk.Coins{{"ATM", 100}},
Coins: sdk.Coins{sdk.NewInt64Coin("ATM", 100)},
}
var sumValue := externalModule.ComputeSumValue(account)
```
Expand Down
Loading