diff --git a/.circleci/config.yml b/.circleci/config.yml index 2accbf179..cd930d52b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -38,6 +38,7 @@ jobs: command: | export PATH="$GOBIN:$PATH" make install + make install_examples - persist_to_workspace: root: /tmp/workspace paths: diff --git a/CHANGELOG.md b/CHANGELOG.md index b3db2c75f..855454050 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,48 @@ # Changelog -## 0.18.0 +## 0.18.1 -*TBD* +BREAKING CHANGES + +* [x/auth] move stuff specific to auth anteHandler to the auth module rather than the types folder. This includes: + * StdTx (and its related stuff i.e. StdSignDoc, etc) + * StdFee + * StdSignature + * Account interface + * Related to this organization, I also: +* [x/auth] got rid of AccountMapper interface (in favor of the struct already in auth module) +* [x/auth] removed the FeeHandler function from the AnteHandler, Replaced with FeeKeeper +* [x/auth] Removed GetSignatures() from Tx interface (as different Tx styles might use something different than StdSignature) +* [store] Removed SubspaceIterator and ReverseSubspaceIterator from KVStore interface and replaced them with helper functions in /types +* Switch to bech32cosmos on all human readable inputs and outputs + +BUG FIXES + +* auto-sequencing transactions correctly +* query sequence via account store +* fixed duplicate pub_key in stake.Validator + +## 0.18.0 + +_TBD_ + +BREAKING CHANGES + +* [stake] candidate -> validator throughout (details in refactor comment) +* [stake] delegate-bond -> delegation throughout +* [stake] `gaiacli query validator` takes and argument instead of using the `--address-candidate` flag +* [stake] introduce `gaiacli query delegations` +* [stake] staking refactor + * ValidatorsBonded store now take sorted pubKey-address instead of validator owner-address, + is sorted like Tendermint by pk's address + * store names more understandable + * removed temporary ToKick store, just needs a local map! + * removed distinction between candidates and validators + * everything is now a validator + * only validators with a status == bonded are actively validating/receiving rewards + * Introduction of Unbonding fields, lowlevel logic throughout (not fully implemented with queue) + * Introduction of PoolShares type within validators, + replaces three rational fields (BondedShares, UnbondingShares, UnbondedShares FEATURES @@ -12,10 +52,23 @@ FEATURES * Transactions which run out of gas stop execution and revert state changes * A "simulate" query has been added to determine how much gas a transaction will need * Modules can include their own gas costs for execution of particular message types +* [stake] Seperation of fee distribution to a new module +* [stake] Creation of a validator/delegation generics in `/types` +* [stake] Helper Description of the store in x/stake/store.md +* [stake] removed use of caches in the stake keeper +* [stake] Added REST API +* [Makefile] Added terraform/ansible playbooks to easily create remote testnets on Digital Ocean + +BUG FIXES + +* Auto-sequencing now works correctly +* [stake] staking delegator shares exchange rate now relative to equivalent-bonded-tokens the validator has instead of bonded tokens + ^ this is important for unbonded validators in the power store! +* [docs] Downgraded Swagger to v2 for downstream compatibility ## 0.17.2 -*May 20, 2018* +_May 20, 2018_ Update to Tendermint v0.19.5 (reduce WAL use, bound the mempool and some rpcs, improve logging) @@ -28,6 +81,7 @@ Update to Tendermint v0.19.4 (fixes a consensus bug and improves logging) BREAKING CHANGES * [stake] MarshalJSON -> MarshalBinary +* Queries against the store must be prefixed with the path "/store" FEATURES @@ -55,17 +109,16 @@ BUG FIXES BREAKING CHANGES * Move module REST/CLI packages to x/[module]/client/rest and x/[module]/client/cli -* Gaia simple-staking bond and unbond functions replaced +* Gaia simple-staking bond and unbond functions replaced * [stake] Delegator bonds now store the height at which they were updated * All module keepers now require a codespace, see basecoin or democoin for usage -* Many changes to names throughout - * Type as a prefix naming convention applied (ex. BondMsg -> MsgBond) +* Many changes to names throughout + * Type as a prefix naming convention applied (ex. BondMsg -> MsgBond) * Removed redundancy in names (ex. stake.StakeKeeper -> stake.Keeper) * Removed SealedAccountMapper -* gaiad init now requires use of `--name` flag +* gaiad init now requires use of `--name` flag * Removed Get from Msg interface * types/rational now extends big.Rat -* Queries against the store must be prefixed with the path "/store" FEATURES: @@ -74,14 +127,15 @@ FEATURES: * Repo is now lint compliant / GoMetaLinter with tendermint-lint integrated into CI * Better key output, pubkey go-amino hex bytes now output by default * gaiad init overhaul - * Create genesis transactions with `gaiad init gen-tx` + * Create genesis transactions with `gaiad init gen-tx` * New genesis account keys are automatically added to the client keybase (introduce `--client-home` flag) * Initialize with genesis txs using `--gen-txs` flag * Context now has access to the application-configured logger -* Add (non-proof) subspace query helper functions +* Add (non-proof) subspace query helper functions * Add more staking query functions: candidates, delegator-bonds BUG FIXES + * Gaia now uses stake, ported from github.com/cosmos/gaia ## 0.15.1 (April 29, 2018) @@ -93,7 +147,7 @@ IMPROVEMENTS: ## 0.15.0 (April 29, 2018) NOTE: v0.15.0 is a large breaking change that updates the encoding scheme to use -[Amino](github.com/tendermint/go-amino). +[Amino](github.com/tendermint/go-amino). For details on how this changes encoding for public keys and addresses, see the [docs](https://github.com/tendermint/tendermint/blob/v0.19.1/docs/specification/new-spec/encoding.md#public-key-cryptography). @@ -111,6 +165,7 @@ FEATURES: * Add FeeHandler to ante handler BUG FIXES + * MountStoreWithDB without providing a custom store works. ## 0.14.1 (April 9, 2018) @@ -139,7 +194,7 @@ FEATURES: BUG FIXES * [client] Reuse Tendermint RPC client to avoid excessive open files -* [client] Fix setting log level +* [client] Fix setting log level * [basecoin] Sort coins in genesis ## 0.13.1 (April 3, 2018) @@ -212,11 +267,12 @@ FEATURES IMPROVEMENTS * Lots more tests! -* [client/builder] Helpers for forming and signing transactions +* [client/builder] Helpers for forming and signing transactions * [types] sdk.Address * [specs] Staking BUG FIXES + * [x/auth] Fix setting pubkey on new account * [x/auth] Require signatures to include the sequences * [baseapp] Dont panic on nil handler @@ -277,7 +333,7 @@ BREAKING CHANGES * Massive refactor. Basecoin works. Still needs <3 -## 0.8.1 +## 0.8.1 * Updates for dependencies @@ -315,29 +371,30 @@ Make lots of small cli fixes that arose when people were using the tools for the testnet. IMPROVEMENTS: -- basecoin - - `basecoin start` supports all flags that `tendermint node` does, such as - `--rpc.laddr`, `--p2p.seeds`, and `--p2p.skip_upnp` - - fully supports `--log_level` and `--trace` for logger configuration - - merkleeyes no longers spams the logs... unless you want it - - Example: `basecoin start --log_level="merkleeyes:info,state:info,*:error"` - - Example: `basecoin start --log_level="merkleeyes:debug,state:info,*:error"` -- basecli - - `basecli init` is more intelligent and only complains if there really was - a connected chain, not just random files - - support `localhost:46657` or `http://localhost:46657` format for nodes, - not just `tcp://localhost:46657` - - Add `--genesis` to init to specify chain-id and validator hash - - Example: `basecli init --node=localhost:46657 --genesis=$HOME/.basecoin/genesis.json` - - `basecli rpc` has a number of methods to easily accept tendermint rpc, and verifies what it can + +* basecoin + * `basecoin start` supports all flags that `tendermint node` does, such as + `--rpc.laddr`, `--p2p.seeds`, and `--p2p.skip_upnp` + * fully supports `--log_level` and `--trace` for logger configuration + * merkleeyes no longers spams the logs... unless you want it + * Example: `basecoin start --log_level="merkleeyes:info,state:info,*:error"` + * Example: `basecoin start --log_level="merkleeyes:debug,state:info,*:error"` +* basecli + * `basecli init` is more intelligent and only complains if there really was + a connected chain, not just random files + * support `localhost:46657` or `http://localhost:46657` format for nodes, + not just `tcp://localhost:46657` + * Add `--genesis` to init to specify chain-id and validator hash + * Example: `basecli init --node=localhost:46657 --genesis=$HOME/.basecoin/genesis.json` + * `basecli rpc` has a number of methods to easily accept tendermint rpc, and verifies what it can BUG FIXES: -- basecli - - `basecli query account` accepts hex account address with or without `0x` - prefix - - gives error message when running commands on an unitialized chain, rather - than some unintelligable panic +* basecli + * `basecli query account` accepts hex account address with or without `0x` + prefix + * gives error message when running commands on an unitialized chain, rather + than some unintelligable panic ## 0.6.0 (June 22, 2017) @@ -345,111 +402,118 @@ Make the basecli command the only way to use client-side, to enforce best security practices. Lots of enhancements to get it up to production quality. BREAKING CHANGES: -- ./cmd/commands -> ./cmd/basecoin/commands -- basecli - - `basecli proof state get` -> `basecli query key` - - `basecli proof tx get` -> `basecli query tx` - - `basecli proof state get --app=account` -> `basecli query account` - - use `--chain-id` not `--chainid` for consistency - - update to use `--trace` not `--debug` for stack traces on errors - - complete overhaul on how tx and query subcommands are added. (see counter or trackomatron for examples) - - no longer supports counter app (see new countercli) -- basecoin - - `basecoin init` takes an argument, an address to allocate funds to in the genesis - - removed key2.json - - removed all client side functionality from it (use basecli now for proofs) - - no tx subcommand - - no query subcommand - - no account (query) subcommand - - a few other random ones... - - enhanced relay subcommand - - relay start did what relay used to do - - relay init registers both chains on one another (to set it up so relay start just works) -- docs - - removed `example-plugin`, put `counter` inside `docs/guide` -- app - - Implements ABCI handshake by proxying merkleeyes.Info() + +* ./cmd/commands -> ./cmd/basecoin/commands +* basecli + * `basecli proof state get` -> `basecli query key` + * `basecli proof tx get` -> `basecli query tx` + * `basecli proof state get --app=account` -> `basecli query account` + * use `--chain-id` not `--chainid` for consistency + * update to use `--trace` not `--debug` for stack traces on errors + * complete overhaul on how tx and query subcommands are added. (see counter or trackomatron for examples) + * no longer supports counter app (see new countercli) +* basecoin + * `basecoin init` takes an argument, an address to allocate funds to in the genesis + * removed key2.json + * removed all client side functionality from it (use basecli now for proofs) + * no tx subcommand + * no query subcommand + * no account (query) subcommand + * a few other random ones... + * enhanced relay subcommand + * relay start did what relay used to do + * relay init registers both chains on one another (to set it up so relay start just works) +* docs + * removed `example-plugin`, put `counter` inside `docs/guide` +* app + * Implements ABCI handshake by proxying merkleeyes.Info() IMPROVEMENTS: -- `basecoin init` support `--chain-id` -- intergrates tendermint 0.10.0 (not the rc-2, but the real thing) -- commands return error code (1) on failure for easier script testing -- add `reset_all` to basecli, and never delete keys on `init` -- new shutil based unit tests, with better coverage of the cli actions -- just `make fresh` when things are getting stale ;) + +* `basecoin init` support `--chain-id` +* intergrates tendermint 0.10.0 (not the rc-2, but the real thing) +* commands return error code (1) on failure for easier script testing +* add `reset_all` to basecli, and never delete keys on `init` +* new shutil based unit tests, with better coverage of the cli actions +* just `make fresh` when things are getting stale ;) BUG FIXES: -- app: no longer panics on missing app_options in genesis (thanks, anton) -- docs: updated all docs... again -- ibc: fix panic on getting BlockID from commit without 100% precommits (still a TODO) + +* app: no longer panics on missing app_options in genesis (thanks, anton) +* docs: updated all docs... again +* ibc: fix panic on getting BlockID from commit without 100% precommits (still a TODO) ## 0.5.2 (June 2, 2017) BUG FIXES: -- fix parsing of the log level from Tendermint config (#97) + +* fix parsing of the log level from Tendermint config (#97) ## 0.5.1 (May 30, 2017) BUG FIXES: -- fix ibc demo app to use proper tendermint flags, 0.10.0-rc2 compatibility -- Make sure all cli uses new json.Marshal not wire.JSONBytes + +* fix ibc demo app to use proper tendermint flags, 0.10.0-rc2 compatibility +* Make sure all cli uses new json.Marshal not wire.JSONBytes ## 0.5.0 (May 27, 2017) BREAKING CHANGES: -- only those related to the tendermint 0.9 -> 0.10 upgrade + +* only those related to the tendermint 0.9 -> 0.10 upgrade IMPROVEMENTS: -- basecoin cli - - integrates tendermint 0.10.0 and unifies cli (init, unsafe_reset_all, ...) - - integrate viper, all command line flags can also be defined in environmental variables or config.toml -- genesis file - - you can define accounts with either address or pub_key - - sorts coins for you, so no silent errors if not in alphabetical order -- [light-client](https://github.com/tendermint/light-client) integration - - no longer must you trust the node you connect to, prove everything! - - new [basecli command](./cmd/basecli/README.md) - - integrated [key management](https://github.com/tendermint/go-crypto/blob/master/cmd/README.md), stored encrypted locally - - tracks validator set changes and proves everything from one initial validator seed - - `basecli proof state` gets complete proofs for any abci state - - `basecli proof tx` gets complete proof where a tx was stored in the chain - - `basecli proxy` exposes tendermint rpc, but only passes through results after doing complete verification + +* basecoin cli + * integrates tendermint 0.10.0 and unifies cli (init, unsafe_reset_all, ...) + * integrate viper, all command line flags can also be defined in environmental variables or config.toml +* genesis file + * you can define accounts with either address or pub_key + * sorts coins for you, so no silent errors if not in alphabetical order +* [light-client](https://github.com/tendermint/light-client) integration + * no longer must you trust the node you connect to, prove everything! + * new [basecli command](./cmd/basecli/README.md) + * integrated [key management](https://github.com/tendermint/go-crypto/blob/master/cmd/README.md), stored encrypted locally + * tracks validator set changes and proves everything from one initial validator seed + * `basecli proof state` gets complete proofs for any abci state + * `basecli proof tx` gets complete proof where a tx was stored in the chain + * `basecli proxy` exposes tendermint rpc, but only passes through results after doing complete verification BUG FIXES: -- no more silently ignored error with invalid coin names (eg. "17.22foo coin" used to parse as "17 foo", not warning/error) +* no more silently ignored error with invalid coin names (eg. "17.22foo coin" used to parse as "17 foo", not warning/error) ## 0.4.1 (April 26, 2017) BUG FIXES: -- Fix bug in `basecoin unsafe_reset_X` where the `priv_validator.json` was not being reset +* Fix bug in `basecoin unsafe_reset_X` where the `priv_validator.json` was not being reset ## 0.4.0 (April 21, 2017) BREAKING CHANGES: -- CLI now uses Cobra, which forced changes to some of the flag names and orderings +* CLI now uses Cobra, which forced changes to some of the flag names and orderings IMPROVEMENTS: -- `basecoin init` doesn't generate error if already initialized -- Much more testing +* `basecoin init` doesn't generate error if already initialized +* Much more testing ## 0.3.1 (March 23, 2017) IMPROVEMENTS: -- CLI returns exit code 1 and logs error before exiting +* CLI returns exit code 1 and logs error before exiting ## 0.3.0 (March 23, 2017) BREAKING CHANGES: -- Remove `--data` flag and use `BCHOME` to set the home directory (defaults to `~/.basecoin`) -- Remove `--in-proc` flag and start Tendermint in-process by default (expect Tendermint files in $BCHOME/tendermint). -To start just the ABCI app/server, use `basecoin start --without-tendermint`. -- Consolidate genesis files so the Basecoin genesis is an object under `app_options` in Tendermint genesis. For instance: +* Remove `--data` flag and use `BCHOME` to set the home directory (defaults to `~/.basecoin`) +* Remove `--in-proc` flag and start Tendermint in-process by default (expect Tendermint files in $BCHOME/tendermint). + To start just the ABCI app/server, use `basecoin start --without-tendermint`. +* Consolidate genesis files so the Basecoin genesis is an object under `app_options` in Tendermint genesis. For instance: ``` { @@ -493,48 +557,45 @@ We also changed `chainID` to `chain_id` and consolidated to have just one of the FEATURES: -- Introduce `basecoin init` and `basecoin unsafe_reset_all` +* Introduce `basecoin init` and `basecoin unsafe_reset_all` ## 0.2.0 (March 6, 2017) BREAKING CHANGES: -- Update to ABCI v0.4.0 and Tendermint v0.9.0 -- Coins are specified on the CLI as `Xcoin`, eg. `5gold` -- `Cost` is now `Fee` +* Update to ABCI v0.4.0 and Tendermint v0.9.0 +* Coins are specified on the CLI as `Xcoin`, eg. `5gold` +* `Cost` is now `Fee` FEATURES: -- CLI for sending transactions and querying the state, -designed to be easily extensible as plugins are implemented -- Run Basecoin in-process with Tendermint -- Add `/account` path in Query -- IBC plugin for InterBlockchain Communication -- Demo script of IBC between two chains +* CLI for sending transactions and querying the state, + designed to be easily extensible as plugins are implemented +* Run Basecoin in-process with Tendermint +* Add `/account` path in Query +* IBC plugin for InterBlockchain Communication +* Demo script of IBC between two chains IMPROVEMENTS: -- Use new Tendermint `/commit` endpoint for crafting IBC transactions -- More unit tests -- Use go-crypto S structs and go-data for more standard JSON -- Demo uses fewer sleeps +* Use new Tendermint `/commit` endpoint for crafting IBC transactions +* More unit tests +* Use go-crypto S structs and go-data for more standard JSON +* Demo uses fewer sleeps BUG FIXES: -- Various little fixes in coin arithmetic -- More commit validation in IBC -- Return results from transactions +* Various little fixes in coin arithmetic +* More commit validation in IBC +* Return results from transactions ## PreHistory ##### January 14-18, 2017 -- Update to Tendermint v0.8.0 -- Cleanup a bit and release blog post +* Update to Tendermint v0.8.0 +* Cleanup a bit and release blog post ##### September 22, 2016 -- Basecoin compiles again - - - +* Basecoin compiles again diff --git a/Gopkg.lock b/Gopkg.lock index 37a32820c..9ca4de685 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -13,6 +13,12 @@ packages = ["btcec"] revision = "1432d294a5b055c297457c25434efbf13384cc46" +[[projects]] + branch = "master" + name = "github.com/cosmos/bech32cosmos" + packages = ["go"] + revision = "efca97cd8c0852c44d96dfdcc70565c306eddff0" + [[projects]] name = "github.com/davecgh/go-spew" packages = ["spew"] @@ -457,6 +463,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "7540d2ecdb5d7d5084ab4e6132e929bbd501bd6add3006d8f08a6b2c127e0c7d" + inputs-digest = "f0c6224dc5f30c1a7dea716d619665831ea0932b0eb9afc6ac897dbc459134fa" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 5480bc03f..a1adc79ac 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -85,6 +85,11 @@ name = "google.golang.org/genproto" revision = "7fd901a49ba6a7f87732eb344f6e3c5b19d1b200" + [[constraint]] + name = "github.com/cosmos/bech32cosmos" + branch = "master" + + [prune] go-tests = true unused-packages = true diff --git a/Makefile b/Makefile index 043645c67..7d2d23ad6 100644 --- a/Makefile +++ b/Makefile @@ -126,7 +126,32 @@ devdoc_update: docker pull tendermint/devdoc +######################################## +### Remote validator nodes using terraform and ansible + +# Build linux binary +build-linux: + GOOS=linux GOARCH=amd64 $(MAKE) build + +TESTNET_NAME?=remotenet +SERVERS?=4 +BINARY=$(CURDIR)/build/gaiad +remotenet-start: + @if [ -z "$(DO_API_TOKEN)" ]; then echo "DO_API_TOKEN environment variable not set." ; false ; fi + @if ! [ -f $(HOME)/.ssh/id_rsa.pub ]; then ssh-keygen ; fi + @if [ -z "`file $(BINARY) | grep 'ELF 64-bit'`" ]; then echo "Please build a linux binary using 'make build-linux'." ; false ; fi + cd networks/remote/terraform && terraform init && terraform apply -var DO_API_TOKEN="$(DO_API_TOKEN)" -var SSH_PUBLIC_FILE="$(HOME)/.ssh/id_rsa.pub" -var SSH_PRIVATE_FILE="$(HOME)/.ssh/id_rsa" -var TESTNET_NAME="$(TESTNET_NAME)" -var SERVERS="$(SERVERS)" + cd networks/remote/ansible && ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory/digital_ocean.py -l "$(TESTNET_NAME)" -e BINARY=$(BINARY) -e TESTNET_NAME="$(TESTNET_NAME)" setup-validators.yml + cd networks/remote/ansible && ansible-playbook -i inventory/digital_ocean.py -l "$(TESTNET_NAME)" start.yml + +remotenet-stop: + @if [ -z "$(DO_API_TOKEN)" ]; then echo "DO_API_TOKEN environment variable not set." ; false ; fi + cd networks/remote/terraform && terraform destroy -var DO_API_TOKEN="$(DO_API_TOKEN)" -var SSH_PUBLIC_FILE="$(HOME)/.ssh/id_rsa.pub" -var SSH_PRIVATE_FILE="$(HOME)/.ssh/id_rsa" + +remotenet-status: + cd networks/remote/ansible && ansible-playbook -i inventory/digital_ocean.py -l "$(TESTNET_NAME)" status.yml + # To avoid unintended conflicts with file names, always add to .PHONY # unless there is a reason not to. # https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html -.PHONY: build build_examples install install_examples dist check_tools get_tools get_vendor_deps draw_deps test test_cli test_unit test_cover test_lint benchmark devdoc_init devdoc devdoc_save devdoc_update +.PHONY: build build_examples install install_examples dist check_tools get_tools get_vendor_deps draw_deps test test_cli test_unit test_cover test_lint benchmark devdoc_init devdoc devdoc_save devdoc_update remotenet-start remotenet-stop remotenet-status diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index ef3bbc3c7..4ce8a05d9 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -15,6 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/store" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" + "github.com/cosmos/cosmos-sdk/x/auth" ) // Key to store the header in the DB itself. @@ -125,7 +126,7 @@ func (app *BaseApp) SetTxDecoder(txDecoder sdk.TxDecoder) { // default custom logic for transaction decoding func defaultTxDecoder(cdc *wire.Codec) sdk.TxDecoder { return func(txBytes []byte) (sdk.Tx, sdk.Error) { - var tx = sdk.StdTx{} + var tx = auth.StdTx{} if len(txBytes) == 0 { return nil, sdk.ErrTxDecode("txBytes are empty") diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 969a9f9ad..61498b1b1 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -11,13 +11,14 @@ import ( "github.com/stretchr/testify/require" abci "github.com/tendermint/abci/types" - "github.com/tendermint/go-crypto" + crypto "github.com/tendermint/go-crypto" cmn "github.com/tendermint/tmlibs/common" dbm "github.com/tendermint/tmlibs/db" "github.com/tendermint/tmlibs/log" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" + "github.com/cosmos/cosmos-sdk/x/auth" ) func defaultLogger() log.Logger { @@ -446,12 +447,12 @@ type testUpdatePowerTx struct { const msgType = "testUpdatePowerTx" -func (tx testUpdatePowerTx) Type() string { return msgType } -func (tx testUpdatePowerTx) GetMsg() sdk.Msg { return tx } -func (tx testUpdatePowerTx) GetSignBytes() []byte { return nil } -func (tx testUpdatePowerTx) ValidateBasic() sdk.Error { return nil } -func (tx testUpdatePowerTx) GetSigners() []sdk.Address { return nil } -func (tx testUpdatePowerTx) GetSignatures() []sdk.StdSignature { return nil } +func (tx testUpdatePowerTx) Type() string { return msgType } +func (tx testUpdatePowerTx) GetMsg() sdk.Msg { return tx } +func (tx testUpdatePowerTx) GetSignBytes() []byte { return nil } +func (tx testUpdatePowerTx) ValidateBasic() sdk.Error { return nil } +func (tx testUpdatePowerTx) GetSigners() []sdk.Address { return nil } +func (tx testUpdatePowerTx) GetSignatures() []auth.StdSignature { return nil } func TestValidatorChange(t *testing.T) { diff --git a/client/context/helpers.go b/client/context/helpers.go index 562bde9b4..f47cc7ff4 100644 --- a/client/context/helpers.go +++ b/client/context/helpers.go @@ -6,6 +6,7 @@ import ( "github.com/pkg/errors" "github.com/cosmos/cosmos-sdk/wire" + "github.com/cosmos/cosmos-sdk/x/auth" rpcclient "github.com/tendermint/tendermint/rpc/client" ctypes "github.com/tendermint/tendermint/rpc/core/types" cmn "github.com/tendermint/tmlibs/common" @@ -58,7 +59,7 @@ func (ctx CoreContext) QuerySubspace(cdc *wire.Codec, subspace []byte, storeName // Query from Tendermint with the provided storename and path func (ctx CoreContext) query(key cmn.HexBytes, storeName, endPath string) (res []byte, err error) { - path := fmt.Sprintf("/store/%s/key", storeName) + path := fmt.Sprintf("/store/%s/%s", storeName, endPath) node, err := ctx.GetNode() if err != nil { return res, err @@ -109,11 +110,11 @@ func (ctx CoreContext) SignAndBuild(name, passphrase string, msg sdk.Msg, cdc *w return nil, errors.Errorf("Chain ID required but not specified") } sequence := ctx.Sequence - signMsg := sdk.StdSignMsg{ + signMsg := auth.StdSignMsg{ ChainID: chainID, Sequences: []int64{sequence}, Msg: msg, - Fee: sdk.NewStdFee(10000, sdk.Coin{}), // TODO run simulate to estimate gas? + Fee: auth.NewStdFee(10000, sdk.Coin{}), // TODO run simulate to estimate gas? } keybase, err := keys.GetKeyBase() @@ -128,14 +129,14 @@ func (ctx CoreContext) SignAndBuild(name, passphrase string, msg sdk.Msg, cdc *w if err != nil { return nil, err } - sigs := []sdk.StdSignature{{ + sigs := []auth.StdSignature{{ PubKey: pubkey, Signature: sig, Sequence: sequence, }} // marshal bytes - tx := sdk.NewStdTx(signMsg.Msg, signMsg.Fee, sigs) + tx := auth.NewStdTx(signMsg.Msg, signMsg.Fee, sigs) return cdc.MarshalBinary(tx) } diff --git a/client/context/types.go b/client/context/types.go index e580027d6..da15b3293 100644 --- a/client/context/types.go +++ b/client/context/types.go @@ -3,7 +3,7 @@ package context import ( rpcclient "github.com/tendermint/tendermint/rpc/client" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" ) // typical context created in sdk modules for transactions/queries @@ -15,7 +15,7 @@ type CoreContext struct { FromAddressName string Sequence int64 Client rpcclient.Client - Decoder sdk.AccountDecoder + Decoder auth.AccountDecoder AccountStore string } @@ -63,7 +63,7 @@ func (c CoreContext) WithClient(client rpcclient.Client) CoreContext { } // WithDecoder - return a copy of the context with an updated Decoder -func (c CoreContext) WithDecoder(decoder sdk.AccountDecoder) CoreContext { +func (c CoreContext) WithDecoder(decoder auth.AccountDecoder) CoreContext { c.Decoder = decoder return c } diff --git a/client/keys/list.go b/client/keys/list.go index 26e06d9da..9af511e5c 100644 --- a/client/keys/list.go +++ b/client/keys/list.go @@ -4,6 +4,7 @@ import ( "encoding/json" "net/http" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/spf13/cobra" ) @@ -55,7 +56,7 @@ func QueryKeysRequestHandler(w http.ResponseWriter, r *http.Request) { } keysOutput := make([]KeyOutput, len(infos)) for i, info := range infos { - keysOutput[i] = KeyOutput{Name: info.Name, Address: info.PubKey.Address().String()} + keysOutput[i] = KeyOutput{Name: info.Name, Address: sdk.Address(info.PubKey.Address().Bytes())} } output, err := json.MarshalIndent(keysOutput, "", " ") if err != nil { diff --git a/client/keys/show.go b/client/keys/show.go index d0555764d..c7be9cc9d 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -4,6 +4,7 @@ import ( "encoding/json" "net/http" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/gorilla/mux" keys "github.com/tendermint/go-crypto/keys" @@ -50,7 +51,7 @@ func GetKeyRequestHandler(w http.ResponseWriter, r *http.Request) { return } - keyOutput := KeyOutput{Name: info.Name, Address: info.PubKey.Address().String()} + keyOutput := KeyOutput{Name: info.Name, Address: sdk.Address(info.PubKey.Address())} output, err := json.MarshalIndent(keyOutput, "", " ") if err != nil { w.WriteHeader(500) diff --git a/client/keys/utils.go b/client/keys/utils.go index 013aa1848..1a358cfc9 100644 --- a/client/keys/utils.go +++ b/client/keys/utils.go @@ -1,19 +1,20 @@ package keys import ( - "encoding/hex" "encoding/json" "fmt" "path/filepath" - "strings" "github.com/spf13/viper" + crypto "github.com/tendermint/go-crypto" keys "github.com/tendermint/go-crypto/keys" "github.com/tendermint/tmlibs/cli" dbm "github.com/tendermint/tmlibs/db" "github.com/cosmos/cosmos-sdk/client" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // KeyDBName is the directory under root where we store the keys @@ -47,16 +48,16 @@ func SetKeyBase(kb keys.Keybase) { // used for outputting keys.Info over REST type KeyOutput struct { - Name string `json:"name"` - Address string `json:"address"` - PubKey string `json:"pub_key"` + Name string `json:"name"` + Address sdk.Address `json:"address"` + PubKey crypto.PubKey `json:"pub_key"` } func NewKeyOutput(info keys.Info) KeyOutput { return KeyOutput{ Name: info.Name, - Address: info.PubKey.Address().String(), - PubKey: strings.ToUpper(hex.EncodeToString(info.PubKey.Bytes())), + Address: sdk.Address(info.PubKey.Address().Bytes()), + PubKey: info.PubKey, } } @@ -72,8 +73,8 @@ func printInfo(info keys.Info) { ko := NewKeyOutput(info) switch viper.Get(cli.OutputFlag) { case "text": - fmt.Printf("NAME:\tADDRESS:\t\t\t\t\tPUBKEY:\n") - fmt.Printf("%s\t%s\t%s\n", ko.Name, ko.Address, ko.PubKey) + fmt.Printf("NAME:\tADDRESS:\t\t\t\t\t\tPUBKEY:\n") + printKeyOutput(ko) case "json": out, err := json.MarshalIndent(ko, "", "\t") if err != nil { @@ -87,9 +88,9 @@ func printInfos(infos []keys.Info) { kos := NewKeyOutputs(infos) switch viper.Get(cli.OutputFlag) { case "text": - fmt.Printf("NAME:\tADDRESS:\t\t\t\t\tPUBKEY:\n") + fmt.Printf("NAME:\tADDRESS:\t\t\t\t\t\tPUBKEY:\n") for _, ko := range kos { - fmt.Printf("%s\t%s\t%s\n", ko.Name, ko.Address, ko.PubKey) + printKeyOutput(ko) } case "json": out, err := json.MarshalIndent(kos, "", "\t") @@ -99,3 +100,15 @@ func printInfos(infos []keys.Info) { fmt.Println(string(out)) } } + +func printKeyOutput(ko KeyOutput) { + bechAccount, err := sdk.Bech32CosmosifyAcc(ko.Address) + if err != nil { + panic(err) + } + bechPubKey, err := sdk.Bech32CosmosifyAccPub(ko.PubKey) + if err != nil { + panic(err) + } + fmt.Printf("%s\t%s\t%s\n", ko.Name, bechAccount, bechPubKey) +} diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index 66a8a4085..8c6530dec 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -2,6 +2,7 @@ package lcd import ( "bytes" + "encoding/hex" "encoding/json" "fmt" "io/ioutil" @@ -16,6 +17,7 @@ import ( "github.com/stretchr/testify/require" abci "github.com/tendermint/abci/types" + crypto "github.com/tendermint/go-crypto" cryptoKeys "github.com/tendermint/go-crypto/keys" tmcfg "github.com/tendermint/tendermint/config" nm "github.com/tendermint/tendermint/node" @@ -31,16 +33,21 @@ import ( client "github.com/cosmos/cosmos-sdk/client" keys "github.com/cosmos/cosmos-sdk/client/keys" - bapp "github.com/cosmos/cosmos-sdk/examples/basecoin/app" - btypes "github.com/cosmos/cosmos-sdk/examples/basecoin/types" + gapp "github.com/cosmos/cosmos-sdk/cmd/gaia/app" tests "github.com/cosmos/cosmos-sdk/tests" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/wire" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/stake" ) var ( - coinDenom = "mycoin" + coinDenom = "steak" coinAmount = int64(10000000) + validatorAddr1 = "" + validatorAddr2 = "" + // XXX bad globals name = "test" password = "0123456789" @@ -91,10 +98,13 @@ func TestKeys(t *testing.T) { err = cdc.UnmarshalJSON([]byte(body), &m) require.Nil(t, err) + sendAddrAcc, _ := sdk.GetAccAddressHex(sendAddr) + addrAcc, _ := sdk.GetAccAddressHex(addr) + assert.Equal(t, m[0].Name, name, "Did not serve keys name correctly") - assert.Equal(t, m[0].Address, sendAddr, "Did not serve keys Address correctly") + assert.Equal(t, m[0].Address, sendAddrAcc, "Did not serve keys Address correctly") assert.Equal(t, m[1].Name, newName, "Did not serve keys name correctly") - assert.Equal(t, m[1].Address, addr, "Did not serve keys Address correctly") + assert.Equal(t, m[1].Address, addrAcc, "Did not serve keys Address correctly") // select key keyEndpoint := fmt.Sprintf("/keys/%s", newName) @@ -105,7 +115,7 @@ func TestKeys(t *testing.T) { require.Nil(t, err) assert.Equal(t, newName, m2.Name, "Did not serve keys name correctly") - assert.Equal(t, addr, m2.Address, "Did not serve keys Address correctly") + assert.Equal(t, addrAcc, m2.Address, "Did not serve keys Address correctly") // update key jsonStr = []byte(fmt.Sprintf(`{"old_password":"%s", "new_password":"12345678901"}`, newPassword)) @@ -216,6 +226,7 @@ func TestValidators(t *testing.T) { func TestCoinSend(t *testing.T) { // query empty + //res, body := request(t, port, "GET", "/accounts/8FA6AB57AD6870F6B5B2E57735F38F2F30E73CB6", nil) res, body := request(t, port, "GET", "/accounts/8FA6AB57AD6870F6B5B2E57735F38F2F30E73CB6", nil) require.Equal(t, http.StatusNoContent, res.StatusCode, body) @@ -305,6 +316,63 @@ func TestTxs(t *testing.T) { // assert.NotEqual(t, "[]", body) } +func TestValidatorsQuery(t *testing.T) { + validators := getValidators(t) + assert.Equal(t, len(validators), 2) + + // make sure all the validators were found (order unknown because sorted by owner addr) + foundVal1, foundVal2 := false, false + res1, res2 := hex.EncodeToString(validators[0].Owner), hex.EncodeToString(validators[1].Owner) + if res1 == validatorAddr1 || res2 == validatorAddr1 { + foundVal1 = true + } + if res1 == validatorAddr2 || res2 == validatorAddr2 { + foundVal2 = true + } + assert.True(t, foundVal1, "validatorAddr1 %v, res1 %v, res2 %v", validatorAddr1, res1, res2) + assert.True(t, foundVal2, "validatorAddr2 %v, res1 %v, res2 %v", validatorAddr2, res1, res2) +} + +func TestBond(t *testing.T) { + + // create bond TX + resultTx := doBond(t, port, seed) + tests.WaitForHeight(resultTx.Height+1, port) + + // check if tx was commited + assert.Equal(t, uint32(0), resultTx.CheckTx.Code) + assert.Equal(t, uint32(0), resultTx.DeliverTx.Code) + + // query sender + acc := getAccount(t, sendAddr) + coins := acc.GetCoins() + assert.Equal(t, int64(87), coins.AmountOf(coinDenom)) + + // query candidate + bond := getDelegation(t, sendAddr, validatorAddr1) + assert.Equal(t, "10/1", bond.Shares.String()) +} + +func TestUnbond(t *testing.T) { + + // create unbond TX + resultTx := doUnbond(t, port, seed) + tests.WaitForHeight(resultTx.Height+1, port) + + // check if tx was commited + assert.Equal(t, uint32(0), resultTx.CheckTx.Code) + assert.Equal(t, uint32(0), resultTx.DeliverTx.Code) + + // query sender + acc := getAccount(t, sendAddr) + coins := acc.GetCoins() + assert.Equal(t, int64(98), coins.AmountOf(coinDenom)) + + // query candidate + bond := getDelegation(t, sendAddr, validatorAddr1) + assert.Equal(t, "9/1", bond.Shares.String()) +} + //__________________________________________________________ // helpers @@ -320,26 +388,18 @@ func startTMAndLCD() (*nm.Node, net.Listener, error) { if err != nil { return nil, nil, err } - var info cryptoKeys.Info - info, seed, err = kb.Create(name, password, cryptoKeys.AlgoEd25519) // XXX global seed - if err != nil { - return nil, nil, err - } - - pubKey := info.PubKey - sendAddr = pubKey.Address().String() // XXX global config := GetConfig() config.Consensus.TimeoutCommit = 1000 config.Consensus.SkipTimeoutCommit = false logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)) - // logger = log.NewFilter(logger, log.AllowError()) + logger = log.NewFilter(logger, log.AllowError()) privValidatorFile := config.PrivValidatorFile() privVal := pvm.LoadOrGenFilePV(privValidatorFile) db := dbm.NewMemDB() - app := bapp.NewBasecoinApp(logger, db) - cdc = bapp.MakeCodec() // XXX + app := gapp.NewGaiaApp(logger, db) + cdc = gapp.MakeCodec() // XXX genesisFile := config.GenesisFile() genDoc, err := tmtypes.GenesisDocFromFile(genesisFile) @@ -347,21 +407,53 @@ func startTMAndLCD() (*nm.Node, net.Listener, error) { return nil, nil, err } - coins := sdk.Coins{{coinDenom, coinAmount}} - appState := map[string]interface{}{ - "accounts": []*btypes.GenesisAccount{ - { - Name: "tester", - Address: pubKey.Address(), - Coins: coins, - }, + genDoc.Validators = append(genDoc.Validators, + tmtypes.GenesisValidator{ + PubKey: crypto.GenPrivKeyEd25519().PubKey(), + Power: 1, + Name: "val", }, - } - stateBytes, err := json.Marshal(appState) + ) + + pk1 := genDoc.Validators[0].PubKey + pk2 := genDoc.Validators[1].PubKey + validatorAddr1 = hex.EncodeToString(pk1.Address()) + validatorAddr2 = hex.EncodeToString(pk2.Address()) + + // NOTE it's bad practice to reuse pk address for the owner address but doing in the + // test for simplicity + var appGenTxs [2]json.RawMessage + appGenTxs[0], _, _, err = gapp.GaiaAppGenTxNF(cdc, pk1, pk1.Address(), "test_val1", true) if err != nil { return nil, nil, err } - genDoc.AppStateJSON = stateBytes + appGenTxs[1], _, _, err = gapp.GaiaAppGenTxNF(cdc, pk2, pk2.Address(), "test_val2", true) + if err != nil { + return nil, nil, err + } + + genesisState, err := gapp.GaiaAppGenState(cdc, appGenTxs[:]) + if err != nil { + return nil, nil, err + } + + // add the sendAddr to genesis + var info cryptoKeys.Info + info, seed, err = kb.Create(name, password, cryptoKeys.AlgoEd25519) // XXX global seed + if err != nil { + return nil, nil, err + } + sendAddr = info.PubKey.Address().String() // XXX global + accAuth := auth.NewBaseAccountWithAddress(info.PubKey.Address()) + accAuth.Coins = sdk.Coins{{"steak", 100}} + acc := gapp.NewGenesisAccount(&accAuth) + genesisState.Accounts = append(genesisState.Accounts, acc) + + appState, err := wire.MarshalJSONIndent(cdc, genesisState) + if err != nil { + return nil, nil, err + } + genDoc.AppStateJSON = appState // LCD listen address port = fmt.Sprintf("%d", 17377) // XXX @@ -375,7 +467,7 @@ func startTMAndLCD() (*nm.Node, net.Listener, error) { if err != nil { return nil, nil, err } - lcd, err := startLCD(logger, listenAddr) + lcd, err := startLCD(logger, listenAddr, cdc) if err != nil { return nil, nil, err } @@ -414,7 +506,7 @@ func startTM(cfg *tmcfg.Config, logger log.Logger, genDoc *tmtypes.GenesisDoc, p } // start the LCD. note this blocks! -func startLCD(logger log.Logger, listenAddr string) (net.Listener, error) { +func startLCD(logger log.Logger, listenAddr string, cdc *wire.Codec) (net.Listener, error) { handler := createHandler(cdc) return tmrpc.StartHTTPServer(listenAddr, handler, logger) } @@ -436,11 +528,11 @@ func request(t *testing.T, port, method, path string, payload []byte) (*http.Res return res, string(output) } -func getAccount(t *testing.T, sendAddr string) sdk.Account { +func getAccount(t *testing.T, sendAddr string) auth.Account { // get the account to get the sequence res, body := request(t, port, "GET", "/accounts/"+sendAddr, nil) require.Equal(t, http.StatusOK, res.StatusCode, body) - var acc sdk.Account + var acc auth.Account err := cdc.UnmarshalJSON([]byte(body), &acc) require.Nil(t, err) return acc @@ -490,3 +582,81 @@ func doIBCTransfer(t *testing.T, port, seed string) (resultTx ctypes.ResultBroad return resultTx } + +func getDelegation(t *testing.T, delegatorAddr, candidateAddr string) stake.Delegation { + // get the account to get the sequence + res, body := request(t, port, "GET", "/stake/"+delegatorAddr+"/bonding_status/"+candidateAddr, nil) + require.Equal(t, http.StatusOK, res.StatusCode, body) + var bond stake.Delegation + err := cdc.UnmarshalJSON([]byte(body), &bond) + require.Nil(t, err) + return bond +} + +func doBond(t *testing.T, port, seed string) (resultTx ctypes.ResultBroadcastTxCommit) { + // get the account to get the sequence + acc := getAccount(t, sendAddr) + sequence := acc.GetSequence() + + // send + jsonStr := []byte(fmt.Sprintf(`{ + "name": "%s", + "password": "%s", + "sequence": %d, + "delegate": [ + { + "delegator_addr": "%x", + "validator_addr": "%s", + "bond": { "denom": "%s", "amount": 10 } + } + ], + "unbond": [] + }`, name, password, sequence, acc.GetAddress(), validatorAddr1, coinDenom)) + res, body := request(t, port, "POST", "/stake/delegations", jsonStr) + require.Equal(t, http.StatusOK, res.StatusCode, body) + + var results []ctypes.ResultBroadcastTxCommit + err := cdc.UnmarshalJSON([]byte(body), &results) + require.Nil(t, err) + + return results[0] +} + +func doUnbond(t *testing.T, port, seed string) (resultTx ctypes.ResultBroadcastTxCommit) { + // get the account to get the sequence + acc := getAccount(t, sendAddr) + sequence := acc.GetSequence() + + // send + jsonStr := []byte(fmt.Sprintf(`{ + "name": "%s", + "password": "%s", + "sequence": %d, + "bond": [], + "unbond": [ + { + "delegator_addr": "%x", + "validator_addr": "%s", + "shares": "1" + } + ] + }`, name, password, sequence, acc.GetAddress(), validatorAddr1)) + res, body := request(t, port, "POST", "/stake/delegations", jsonStr) + require.Equal(t, http.StatusOK, res.StatusCode, body) + + var results []ctypes.ResultBroadcastTxCommit + err := cdc.UnmarshalJSON([]byte(body), &results) + require.Nil(t, err) + + return results[0] +} + +func getValidators(t *testing.T) []stake.Validator { + // get the account to get the sequence + res, body := request(t, port, "GET", "/stake/validators", nil) + require.Equal(t, http.StatusOK, res.StatusCode, body) + var validators stake.Validators + err := cdc.UnmarshalJSON([]byte(body), &validators) + require.Nil(t, err) + return validators +} diff --git a/client/lcd/root.go b/client/lcd/root.go index a7be5079b..df8be897c 100644 --- a/client/lcd/root.go +++ b/client/lcd/root.go @@ -22,6 +22,7 @@ import ( auth "github.com/cosmos/cosmos-sdk/x/auth/client/rest" bank "github.com/cosmos/cosmos-sdk/x/bank/client/rest" ibc "github.com/cosmos/cosmos-sdk/x/ibc/client/rest" + stake "github.com/cosmos/cosmos-sdk/x/stake/client/rest" ) const ( @@ -83,5 +84,6 @@ func createHandler(cdc *wire.Codec) http.Handler { auth.RegisterRoutes(ctx, r, cdc, "acc") bank.RegisterRoutes(ctx, r, cdc, kb) ibc.RegisterRoutes(ctx, r, cdc, kb) + stake.RegisterRoutes(ctx, r, cdc, kb) return r } diff --git a/client/tx/query.go b/client/tx/query.go index 2078b7883..7673dd38d 100644 --- a/client/tx/query.go +++ b/client/tx/query.go @@ -17,6 +17,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" + "github.com/cosmos/cosmos-sdk/x/auth" ) // Get the default command for a tx query @@ -95,7 +96,7 @@ type txInfo struct { } func parseTx(cdc *wire.Codec, txBytes []byte) (sdk.Tx, error) { - var tx sdk.StdTx + var tx auth.StdTx err := cdc.UnmarshalBinary(txBytes, &tx) if err != nil { return nil, err diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index 5ff532bff..dbecada00 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -40,10 +40,11 @@ type GaiaApp struct { keyStake *sdk.KVStoreKey // Manage getting and setting accounts - accountMapper sdk.AccountMapper - coinKeeper bank.Keeper - ibcMapper ibc.Mapper - stakeKeeper stake.Keeper + accountMapper auth.AccountMapper + feeCollectionKeeper auth.FeeCollectionKeeper + coinKeeper bank.Keeper + ibcMapper ibc.Mapper + stakeKeeper stake.Keeper } func NewGaiaApp(logger log.Logger, db dbm.DB) *GaiaApp { @@ -81,7 +82,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB) *GaiaApp { app.SetInitChainer(app.initChainer) app.SetEndBlocker(stake.NewEndBlocker(app.stakeKeeper)) app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyIBC, app.keyStake) - app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, stake.FeeHandler)) + app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper)) err := app.LoadLatestVersion(app.keyMain) if err != nil { cmn.Exit(err.Error()) @@ -131,7 +132,7 @@ func (app *GaiaApp) ExportAppStateJSON() (appState json.RawMessage, err error) { // iterate to get the accounts accounts := []GenesisAccount{} - appendAccount := func(acc sdk.Account) (stop bool) { + appendAccount := func(acc auth.Account) (stop bool) { account := NewGenesisAccountI(acc) accounts = append(accounts, account) return false diff --git a/cmd/gaia/app/app_test.go b/cmd/gaia/app/app_test.go index fd26ce27a..b6f1c9e03 100644 --- a/cmd/gaia/app/app_test.go +++ b/cmd/gaia/app/app_test.go @@ -38,7 +38,7 @@ var ( coins = sdk.Coins{{"foocoin", 10}} halfCoins = sdk.Coins{{"foocoin", 5}} manyCoins = sdk.Coins{{"foocoin", 1}, {"barcoin", 1}} - fee = sdk.StdFee{ + fee = auth.StdFee{ sdk.Coins{{"foocoin", 0}}, 100000, } @@ -105,7 +105,7 @@ func setGenesis(gapp *GaiaApp, accs ...*auth.BaseAccount) error { genesisState := GenesisState{ Accounts: genaccs, - StakeData: stake.GetDefaultGenesisState(), + StakeData: stake.DefaultGenesisState(), } stateBytes, err := wire.MarshalJSONIndent(gapp.cdc, genesisState) @@ -147,7 +147,7 @@ func setGenesisAccounts(gapp *GaiaApp, accs ...*auth.BaseAccount) error { genesisState := GenesisState{ Accounts: genaccs, - StakeData: stake.GetDefaultGenesisState(), + StakeData: stake.DefaultGenesisState(), } stateBytes, err := json.MarshalIndent(genesisState, "", "\t") @@ -405,9 +405,15 @@ func TestStakeMsgs(t *testing.T) { ctxDeliver := gapp.BaseApp.NewContext(false, abci.Header{}) res1 = gapp.accountMapper.GetAccount(ctxDeliver, addr1) require.Equal(t, genCoins.Minus(sdk.Coins{bondCoin}), res1.GetCoins()) - candidate, found := gapp.stakeKeeper.GetCandidate(ctxDeliver, addr1) + validator, found := gapp.stakeKeeper.GetValidator(ctxDeliver, addr1) require.True(t, found) - require.Equal(t, candidate.Address, addr1) + require.Equal(t, addr1, validator.Owner) + require.Equal(t, sdk.Bonded, validator.Status()) + require.True(sdk.RatEq(t, sdk.NewRat(10), validator.PoolShares.Bonded())) + + // check the bond that should have been created as well + bond, found := gapp.stakeKeeper.GetDelegation(ctxDeliver, addr1, addr1) + require.True(sdk.RatEq(t, sdk.NewRat(10), bond.Shares)) // Edit Candidacy @@ -417,9 +423,9 @@ func TestStakeMsgs(t *testing.T) { ) SignDeliver(t, gapp, editCandidacyMsg, []int64{1}, true, priv1) - candidate, found = gapp.stakeKeeper.GetCandidate(ctxDeliver, addr1) + validator, found = gapp.stakeKeeper.GetValidator(ctxDeliver, addr1) require.True(t, found) - require.Equal(t, candidate.Description, description) + require.Equal(t, description, validator.Description) // Delegate @@ -428,12 +434,13 @@ func TestStakeMsgs(t *testing.T) { ) SignDeliver(t, gapp, delegateMsg, []int64{0}, true, priv2) - ctxDeliver = gapp.BaseApp.NewContext(false, abci.Header{}) res2 = gapp.accountMapper.GetAccount(ctxDeliver, addr2) require.Equal(t, genCoins.Minus(sdk.Coins{bondCoin}), res2.GetCoins()) - bond, found := gapp.stakeKeeper.GetDelegatorBond(ctxDeliver, addr2, addr1) + bond, found = gapp.stakeKeeper.GetDelegation(ctxDeliver, addr2, addr1) require.True(t, found) - require.Equal(t, bond.DelegatorAddr, addr2) + require.Equal(t, addr2, bond.DelegatorAddr) + require.Equal(t, addr1, bond.ValidatorAddr) + require.True(sdk.RatEq(t, sdk.NewRat(10), bond.Shares)) // Unbond @@ -442,10 +449,9 @@ func TestStakeMsgs(t *testing.T) { ) SignDeliver(t, gapp, unbondMsg, []int64{1}, true, priv2) - ctxDeliver = gapp.BaseApp.NewContext(false, abci.Header{}) res2 = gapp.accountMapper.GetAccount(ctxDeliver, addr2) require.Equal(t, genCoins, res2.GetCoins()) - _, found = gapp.stakeKeeper.GetDelegatorBond(ctxDeliver, addr2, addr1) + _, found = gapp.stakeKeeper.GetDelegation(ctxDeliver, addr2, addr1) require.False(t, found) } @@ -457,17 +463,17 @@ func CheckBalance(t *testing.T, gapp *GaiaApp, addr sdk.Address, balExpected str assert.Equal(t, balExpected, fmt.Sprintf("%v", res2.GetCoins())) } -func genTx(msg sdk.Msg, seq []int64, priv ...crypto.PrivKeyEd25519) sdk.StdTx { - sigs := make([]sdk.StdSignature, len(priv)) +func genTx(msg sdk.Msg, seq []int64, priv ...crypto.PrivKeyEd25519) auth.StdTx { + sigs := make([]auth.StdSignature, len(priv)) for i, p := range priv { - sigs[i] = sdk.StdSignature{ + sigs[i] = auth.StdSignature{ PubKey: p.PubKey(), - Signature: p.Sign(sdk.StdSignBytes(chainID, seq, fee, msg)), + Signature: p.Sign(auth.StdSignBytes(chainID, seq, fee, msg)), Sequence: seq[i], } } - return sdk.NewStdTx(msg, fee, sigs) + return auth.NewStdTx(msg, fee, sigs) } diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index b70ac17a1..4f76d6073 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -35,7 +35,7 @@ func NewGenesisAccount(acc *auth.BaseAccount) GenesisAccount { } } -func NewGenesisAccountI(acc sdk.Account) GenesisAccount { +func NewGenesisAccountI(acc auth.Account) GenesisAccount { return GenesisAccount{ Address: acc.GetAddress(), Coins: acc.GetCoins(), @@ -74,7 +74,7 @@ func GaiaAppInit() server.AppInit { FlagsAppGenState: fsAppGenState, FlagsAppGenTx: fsAppGenTx, AppGenTx: GaiaAppGenTx, - AppGenState: GaiaAppGenState, + AppGenState: GaiaAppGenStateJSON, } } @@ -85,22 +85,35 @@ type GaiaGenTx struct { PubKey crypto.PubKey `json:"pub_key"` } -// Generate a gaia genesis transaction +// Generate a gaia genesis transaction with flags func GaiaAppGenTx(cdc *wire.Codec, pk crypto.PubKey) ( appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) { - - var addr sdk.Address - var secret string clientRoot := viper.GetString(flagClientHome) overwrite := viper.GetBool(flagOWK) name := viper.GetString(flagName) if name == "" { return nil, nil, tmtypes.GenesisValidator{}, errors.New("Must specify --name (validator moniker)") } + + var addr sdk.Address + var secret string addr, secret, err = server.GenerateSaveCoinKey(clientRoot, name, "1234567890", overwrite) if err != nil { return } + mm := map[string]string{"secret": secret} + var bz []byte + bz, err = cdc.MarshalJSON(mm) + if err != nil { + return + } + cliPrint = json.RawMessage(bz) + return GaiaAppGenTxNF(cdc, pk, addr, name, overwrite) +} + +// Generate a gaia genesis transaction without flags +func GaiaAppGenTxNF(cdc *wire.Codec, pk crypto.PubKey, addr sdk.Address, name string, overwrite bool) ( + appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) { var bz []byte gaiaGenTx := GaiaGenTx{ @@ -114,13 +127,6 @@ func GaiaAppGenTx(cdc *wire.Codec, pk crypto.PubKey) ( } appGenTx = json.RawMessage(bz) - mm := map[string]string{"secret": secret} - bz, err = cdc.MarshalJSON(mm) - if err != nil { - return - } - cliPrint = json.RawMessage(bz) - validator = tmtypes.GenesisValidator{ PubKey: pk, Power: freeFermionVal, @@ -130,7 +136,7 @@ func GaiaAppGenTx(cdc *wire.Codec, pk crypto.PubKey) ( // Create the core parameters for genesis initialization for gaia // note that the pubkey input is this machines pubkey -func GaiaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { +func GaiaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (genesisState GenesisState, err error) { if len(appGenTxs) == 0 { err = errors.New("must provide at least genesis transaction") @@ -138,7 +144,7 @@ func GaiaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState jso } // start with the default staking genesis state - stakeData := stake.GetDefaultGenesisState() + stakeData := stake.DefaultGenesisState() // get genesis flag account information genaccs := make([]GenesisAccount, len(appGenTxs)) @@ -158,27 +164,37 @@ func GaiaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState jso } acc := NewGenesisAccount(&accAuth) genaccs[i] = acc - stakeData.Pool.TotalSupply += freeFermionsAcc // increase the supply + stakeData.Pool.LooseUnbondedTokens += freeFermionsAcc // increase the supply // add the validator if len(genTx.Name) > 0 { desc := stake.NewDescription(genTx.Name, "", "", "") - candidate := stake.NewCandidate(genTx.Address, genTx.PubKey, desc) - candidate.Assets = sdk.NewRat(freeFermionVal) - stakeData.Candidates = append(stakeData.Candidates, candidate) + validator := stake.NewValidator(genTx.Address, genTx.PubKey, desc) + validator.PoolShares = stake.NewBondedShares(sdk.NewRat(freeFermionVal)) + stakeData.Validators = append(stakeData.Validators, validator) // pool logic - stakeData.Pool.TotalSupply += freeFermionVal - stakeData.Pool.BondedPool += freeFermionVal - stakeData.Pool.BondedShares = sdk.NewRat(stakeData.Pool.BondedPool) + stakeData.Pool.BondedTokens += freeFermionVal + stakeData.Pool.BondedShares = sdk.NewRat(stakeData.Pool.BondedTokens) } } // create the final app state - genesisState := GenesisState{ + genesisState = GenesisState{ Accounts: genaccs, StakeData: stakeData, } + return +} + +// GaiaAppGenState but with JSON +func GaiaAppGenStateJSON(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { + + // create the final app state + genesisState, err := GaiaAppGenState(cdc, appGenTxs) + if err != nil { + return nil, err + } appState, err = wire.MarshalJSONIndent(cdc, genesisState) return } diff --git a/cmd/gaia/cli_test/cli_test.go b/cmd/gaia/cli_test/cli_test.go index b4529efd8..0f549d6d6 100644 --- a/cmd/gaia/cli_test/cli_test.go +++ b/cmd/gaia/cli_test/cli_test.go @@ -13,9 +13,11 @@ import ( "github.com/cosmos/cosmos-sdk/cmd/gaia/app" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/tests" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/stake" + crypto "github.com/tendermint/go-crypto" ) func TestGaiaCLISend(t *testing.T) { @@ -38,24 +40,32 @@ func TestGaiaCLISend(t *testing.T) { fooAddr, _ := executeGetAddrPK(t, "gaiacli keys show foo --output=json") barAddr, _ := executeGetAddrPK(t, "gaiacli keys show bar --output=json") - fooAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", fooAddr, flags)) + fooBech, err := sdk.Bech32CosmosifyAcc(fooAddr) + if err != nil { + t.Error(err) + } + barBech, err := sdk.Bech32CosmosifyAcc(barAddr) + if err != nil { + t.Error(err) + } + fooAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", fooBech, flags)) assert.Equal(t, int64(50), fooAcc.GetCoins().AmountOf("steak")) executeWrite(t, fmt.Sprintf("gaiacli send %v --amount=10steak --to=%v --name=foo", flags, barAddr), pass) time.Sleep(time.Second * 3) // waiting for some blocks to pass - barAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barAddr, flags)) + barAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barBech, flags)) assert.Equal(t, int64(10), barAcc.GetCoins().AmountOf("steak")) - fooAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", fooAddr, flags)) + fooAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", fooBech, flags)) assert.Equal(t, int64(40), fooAcc.GetCoins().AmountOf("steak")) // test autosequencing executeWrite(t, fmt.Sprintf("gaiacli send %v --amount=10steak --to=%v --name=foo", flags, barAddr), pass) time.Sleep(time.Second * 3) // waiting for some blocks to pass - barAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barAddr, flags)) + barAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barBech, flags)) assert.Equal(t, int64(20), barAcc.GetCoins().AmountOf("steak")) - fooAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", fooAddr, flags)) + fooAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", fooBech, flags)) assert.Equal(t, int64(30), fooAcc.GetCoins().AmountOf("steak")) } @@ -77,21 +87,32 @@ func TestGaiaCLIDeclareCandidacy(t *testing.T) { defer cmd.Process.Kill() fooAddr, _ := executeGetAddrPK(t, "gaiacli keys show foo --output=json") - barAddr, barPubKey := executeGetAddrPK(t, "gaiacli keys show bar --output=json") + barAddr, _ := executeGetAddrPK(t, "gaiacli keys show bar --output=json") - executeWrite(t, fmt.Sprintf("gaiacli send %v --amount=10steak --to=%v --name=foo", flags, barAddr), pass) + fooBech, err := sdk.Bech32CosmosifyAcc(fooAddr) + if err != nil { + t.Error(err) + } + barBech, err := sdk.Bech32CosmosifyAcc(barAddr) + if err != nil { + t.Error(err) + } + valPrivkey := crypto.GenPrivKeyEd25519() + valAddr := sdk.Address((valPrivkey.PubKey().Address())) + bechVal, err := sdk.Bech32CosmosifyVal(valAddr) + + executeWrite(t, fmt.Sprintf("gaiacli send %v --amount=10steak --to=%v --name=foo", flags, barBech), pass) time.Sleep(time.Second * 3) // waiting for some blocks to pass - fooAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", fooAddr, flags)) + fooAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", fooBech, flags)) assert.Equal(t, int64(40), fooAcc.GetCoins().AmountOf("steak")) - barAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barAddr, flags)) + barAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barBech, flags)) assert.Equal(t, int64(10), barAcc.GetCoins().AmountOf("steak")) // declare candidacy - declStr := fmt.Sprintf("gaiacli declare-candidacy %v", flags) + declStr := fmt.Sprintf("gaiacli create-validator %v", flags) declStr += fmt.Sprintf(" --name=%v", "bar") - declStr += fmt.Sprintf(" --address-candidate=%v", barAddr) - declStr += fmt.Sprintf(" --pubkey=%v", barPubKey) + declStr += fmt.Sprintf(" --validator-address=%v", bechVal) declStr += fmt.Sprintf(" --amount=%v", "3steak") declStr += fmt.Sprintf(" --moniker=%v", "bar-vally") fmt.Printf("debug declStr: %v\n", declStr) @@ -100,8 +121,8 @@ func TestGaiaCLIDeclareCandidacy(t *testing.T) { barAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barAddr, flags)) assert.Equal(t, int64(7), barAcc.GetCoins().AmountOf("steak")) candidate := executeGetCandidate(t, fmt.Sprintf("gaiacli candidate %v --address-candidate=%v", flags, barAddr)) - assert.Equal(t, candidate.Address.String(), barAddr) - assert.Equal(t, int64(3), candidate.Assets.Evaluate()) + assert.Equal(t, candidate.Owner.String(), barAddr) + assert.Equal(t, int64(3), candidate.PoolShares) // TODO timeout issues if not connected to the internet // unbond a single share @@ -117,7 +138,7 @@ func TestGaiaCLIDeclareCandidacy(t *testing.T) { //barAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barAddr, flags)) //assert.Equal(t, int64(99998), barAcc.GetCoins().AmountOf("steak")) //candidate = executeGetCandidate(t, fmt.Sprintf("gaiacli candidate %v --address-candidate=%v", flags, barAddr)) - //assert.Equal(t, int64(2), candidate.Assets.Evaluate()) + //assert.Equal(t, int64(2), candidate.BondedShares.Evaluate()) } func executeWrite(t *testing.T, cmdStr string, writes ...string) { @@ -125,6 +146,9 @@ func executeWrite(t *testing.T, cmdStr string, writes ...string) { for _, write := range writes { _, err := wc.Write([]byte(write + "\n")) + if err != nil { + fmt.Println(err) + } require.NoError(t, err) } fmt.Printf("debug waiting cmdStr: %v\n", cmdStr) @@ -159,7 +183,7 @@ func executeInit(t *testing.T, cmdStr string) (chainID string) { return } -func executeGetAddrPK(t *testing.T, cmdStr string) (addr, pubKey string) { +func executeGetAddrPK(t *testing.T, cmdStr string) (sdk.Address, crypto.PubKey) { out := tests.ExecuteT(t, cmdStr) var ko keys.KeyOutput keys.UnmarshalJSON([]byte(out), &ko) @@ -180,9 +204,9 @@ func executeGetAccount(t *testing.T, cmdStr string) auth.BaseAccount { return acc } -func executeGetCandidate(t *testing.T, cmdStr string) stake.Candidate { +func executeGetCandidate(t *testing.T, cmdStr string) stake.Validator { out := tests.ExecuteT(t, cmdStr) - var candidate stake.Candidate + var candidate stake.Validator cdc := app.MakeCodec() err := cdc.UnmarshalJSON([]byte(out), &candidate) require.NoError(t, err, "out %v, err %v", out, err) diff --git a/cmd/gaia/cmd/gaiacli/main.go b/cmd/gaia/cmd/gaiacli/main.go index 8de2e3acc..2f36201b2 100644 --- a/cmd/gaia/cmd/gaiacli/main.go +++ b/cmd/gaia/cmd/gaiacli/main.go @@ -45,10 +45,10 @@ func main() { rootCmd.AddCommand( client.GetCommands( authcmd.GetAccountCmd("acc", cdc, authcmd.GetAccountDecoder(cdc)), - stakecmd.GetCmdQueryCandidate("stake", cdc), - stakecmd.GetCmdQueryCandidates("stake", cdc), - stakecmd.GetCmdQueryDelegatorBond("stake", cdc), - //stakecmd.GetCmdQueryDelegatorBonds("stake", cdc), + stakecmd.GetCmdQueryValidator("stake", cdc), + stakecmd.GetCmdQueryValidators("stake", cdc), + stakecmd.GetCmdQueryDelegation("stake", cdc), + stakecmd.GetCmdQueryDelegations("stake", cdc), )...) rootCmd.AddCommand( client.PostCommands( diff --git a/docs/sdk/lcd-rest-api.yaml b/docs/sdk/lcd-rest-api.yaml index f88d4380f..34ccaa0ac 100644 --- a/docs/sdk/lcd-rest-api.yaml +++ b/docs/sdk/lcd-rest-api.yaml @@ -1,11 +1,14 @@ -openapi: 3.0.0 -servers: - - url: 'http://localhost:8998' +swagger: '2.0' info: - version: "1.1.0" - title: Light client daemon to interface with full Gaia node via REST - description: Specification for the LCD provided by `gaia rest-server` - + version: '1.1.0' + title: Light client daemon to interface with Cosmos baseserver via REST + description: Specification for the LCD provided by `gaiacli rest-server` + + +securityDefinitions: + kms: + type: basic + paths: /version: get: @@ -13,131 +16,131 @@ paths: description: Get the version of the LCD running locally to compare against expected responses: 200: - description: Plaintext version i.e. "0.16.0-dev-26440095" + description: Plaintext version i.e. "v0.5.0" /node_info: - description: Only the node info. Block information can be queried via /block/latest get: + description: Only the node info. Block information can be queried via /block/latest summary: The propertied of the connected node + produces: + - application/json responses: 200: description: Node status - content: - application/json: - schema: - type: object - properties: - id: - description: ??? - type: string - listen_addr: - type: string - example: 192.168.56.1:46656 - network: - type: string - example: gaia-5000 - version: - description: Tendermint version - type: string - example: 0.19.1 - channels: - description: ??? - type: string - pub_key: - $ref: '#/components/schemas/PubKey' - moniker: - type: string - example: 159.89.198.221 - remote_addr: - type: string - other: - description: more information on versions and options for the node - type: array + schema: + type: object + properties: + pub_key: + $ref: '#/definitions/PubKey' + moniker: + type: string + example: 159.89.198.221 + network: + type: string + example: gaia-2 + remote_addr: + type: string + listen_addr: + type: string + example: 192.168.56.1:46656 + version: + description: Tendermint version + type: string + example: 0.15.0 + other: + description: more information on versions + type: array + items: + type: string /syncing: get: summary: Syncing state of node description: Get if the node is currently syning with other nodes responses: 200: - description: "true" or "false" + description: '"true" or "false"' /keys: get: summary: List of accounts stored locally + produces: + - application/json responses: 200: description: Array of accounts - content: - application/json: - schema: - type: array - items: - $ref: '#/components/schemas/Account' + schema: + type: array + items: + $ref: '#/definitions/Account' post: summary: Create a new account locally + consumes: + - application/json + parameters: + - in: body + name: account + description: The account to create + schema: + type: object + required: + - name + - password + - seed + properties: + name: + type: string + password: + type: string + seed: + type: string responses: 200: description: Returns address of the account created - requestBody: - content: - application/json: - schema: - type: object - required: - - name - - password - - seed - properties: - name: - type: string - password: - type: string - seed: - type: string - description: The account to create. /keys/seed: get: summary: Create a new seed to create a new account with + produces: + - application/json responses: 200: description: 12 word Seed - content: - application/json: - schema: - type: string + schema: + type: string /keys/{name}: parameters: - in: path name: name description: Account name required: true - schema: - type: string + type: string get: summary: Get a certain locally stored account + produces: + - application/json responses: 200: description: Locally stored account - content: - application/json: - schema: - $ref: "#/components/schemas/Account" + schema: + $ref: "#/definitions/Account" 404: description: Account is not available put: - summary: Update the password for this account - requestBody: - content: - application/json: - schema: - type: object - required: - - new_password - - old_password - properties: - new_password: - type: string - old_password: - type: string + summary: Update the password for this account in the KMS + consumes: + - application/json + parameters: + - in: body + name: account + description: The new and old password + schema: + type: object + required: + - new_password + - old_password + properties: + new_password: + type: string + old_password: + type: string responses: 200: description: Updated password @@ -147,16 +150,19 @@ paths: description: Account is not available delete: summary: Remove an account - requestBody: - content: - application/json: - schema: - type: object - required: - - password - properties: - password: - type: string + consumes: + - application/json + parameters: + - in: body + name: account + description: The password of the account to remove from the KMS + schema: + type: object + required: + - password + properties: + password: + type: string responses: 200: description: Removed account @@ -164,7 +170,7 @@ paths: description: Password is wrong 404: description: Account is not available - # /accounts/send: +# /accounts/send: # post: # summary: Send coins (build -> sign -> send) # security: @@ -176,18 +182,18 @@ paths: # type: object # properties: # fees: - # $ref: "#/components/schemas/Coins" + # $ref: "#/definitions/Coins" # outputs: # type: array # items: # type: object # properties: # pub_key: - # $ref: "#/components/schemas/PubKey" + # $ref: "#/definitions/PubKey" # amount: # type: array # items: - # $ref: "#/components/schemas/Coins" + # $ref: "#/definitions/Coins" # responses: # 202: # description: Tx was send and will probably be added to the next block @@ -200,21 +206,16 @@ paths: name: address description: Account address required: true - schema: - $ref: "#/components/schemas/Address" + type: string get: summary: Get the account balances + produces: + - application/json responses: 200: description: Account balances - content: - application/json: - schema: - type: - description: "???" - type: string - value: - $ref: "#/components/schemas/Balance" + schema: + $ref: "#/definitions/Balance" 204: description: There is no data for the requested account. This is not a 404 as the account might exist, just does not hold data. /accounts/{address}/send: @@ -223,99 +224,107 @@ paths: name: address description: Account address required: true - schema: - $ref: "#/components/schemas/Address" + type: string post: summary: Send coins (build -> sign -> send) security: - - sign: [] - requestBody: - content: - application/json: - schema: - type: object - properties: - name: - description: Name of locally stored key - type: string - password: - type: string - amount: - type: array - items: - $ref: "#/components/schemas/Coins" - chain_id: - description: Target chain - type: string - src_chain_id: - type: string - squence: - type: number + - kms: [] + consumes: + - application/json + parameters: + - in: body + name: account + description: The password of the account to remove from the KMS + schema: + type: object + properties: + name: + type: string + password: + type: string + amount: + type: array + items: + $ref: "#/definitions/Coins" + chain_id: + type: string + squence: + type: number responses: 202: description: Tx was send and will probably be added to the next block 400: description: The Tx was malformated + /accounts/{address}/nonce: + parameters: + - in: path + name: address + description: Account address + required: true + type: string + get: + summary: Get the nonce for a certain account + responses: + 200: + description: Plaintext nonce i.e. "4" defaults to "0" /blocks/latest: get: summary: Get the latest block + produces: + - application/json responses: 200: description: The latest block - content: - application/json: - schema: - $ref: "#/components/schemas/Block" + schema: + $ref: "#/definitions/Block" /blocks/{height}: parameters: - in: path name: height description: Block height required: true - schema: - type: number + type: number get: summary: Get a block at a certain height + produces: + - application/json responses: 200: description: The block at a specific height - content: - application/json: - schema: - $ref: "#/components/schemas/Block" + schema: + $ref: "#/definitions/Block" 404: description: Block at height is not available /validatorsets/latest: get: summary: Get the latest validator set + produces: + - application/json responses: 200: description: The validator set at the latest block height - content: - application/json: - schema: - type: array - items: - $ref: "#/components/schemas/Delegate" + schema: + type: array + items: + $ref: "#/definitions/Delegate" /validatorsets/{height}: parameters: - in: path name: height description: Block height required: true - schema: - type: number + type: number get: summary: Get a validator set a certain height + produces: + - application/json responses: 200: description: The validator set at a specific block height - content: - application/json: - schema: - type: array - items: - $ref: "#/components/schemas/Delegate" + schema: + type: array + items: + $ref: "#/definitions/Delegate" 404: description: Block at height not available # /txs: @@ -348,7 +357,7 @@ paths: # schema: # type: array # items: - # $ref: "#/components/schemas/Tx" + # $ref: "#/definitions/Tx" # 404: # description: Pagination is out of bounds # /txs/sign: @@ -361,14 +370,14 @@ paths: # content: # application/json: # schema: - # $ref: "#/components/schemas/TxBuild" + # $ref: "#/definitions/TxBuild" # responses: # 200: # description: The signed Tx # content: # application/json: # schema: - # $ref: "#/components/schemas/TxSigned" + # $ref: "#/definitions/TxSigned" # 401: # description: Account name and/or password where wrong # /txs/broadcast: @@ -378,7 +387,7 @@ paths: # content: # application/json: # schema: - # $ref: "#/components/schemas/TxSigned" + # $ref: "#/definitions/TxSigned" # responses: # 202: # description: Tx was send and will probably be added to the next block @@ -390,17 +399,16 @@ paths: name: hash description: Tx hash required: true - schema: - $ref: "#/components/schemas/Hash" + type: string get: summary: Get a Tx by hash + produces: + - application/json responses: 200: description: Tx with the provided hash - content: - application/json: - schema: - $ref: "#/components/schemas/Tx" + schema: + $ref: "#/definitions/Tx" 404: description: Tx not available for provided hash # /delegates: @@ -409,7 +417,7 @@ paths: # name: delegator # description: Query for all delegates a delegator has stake with # schema: - # $ref: "#/components/schemas/Address" + # $ref: "#/definitions/Address" # get: # summary: Get a list of canidates/delegates/validators (optionally filtered by delegator) # responses: @@ -420,7 +428,7 @@ paths: # schema: # type: array # items: - # $ref: "#/components/schemas/Delegate" + # $ref: "#/definitions/Delegate" # /delegates/bond: # post: # summary: Bond atoms (build -> sign -> send) @@ -435,9 +443,9 @@ paths: # type: object # properties: # amount: - # $ref: "#/components/schemas/Coins" + # $ref: "#/definitions/Coins" # pub_key: - # $ref: "#/components/schemas/PubKey" + # $ref: "#/definitions/PubKey" # responses: # 202: # description: Tx was send and will probably be added to the next block @@ -457,9 +465,9 @@ paths: # type: object # properties: # amount: - # $ref: "#/components/schemas/Coins" + # $ref: "#/definitions/Coins" # pub_key: - # $ref: "#/components/schemas/PubKey" + # $ref: "#/definitions/PubKey" # responses: # 202: # description: Tx was send and will probably be added to the next block @@ -482,7 +490,7 @@ paths: # content: # application/json: # schema: - # $ref: "#/components/schemas/Delegate" + # $ref: "#/definitions/Delegate" # 404: # description: No delegate found for provided pub_key # /delegates/{pubkey}/bond: @@ -505,7 +513,7 @@ paths: # type: object # properties: # amount: - # $ref: "#/components/schemas/Coins" + # $ref: "#/definitions/Coins" # responses: # 202: # description: Tx was send and will probably be added to the next block @@ -531,230 +539,233 @@ paths: # type: object # properties: # amount: - # $ref: "#/components/schemas/Coins" + # $ref: "#/definitions/Coins" # responses: # 202: # description: Tx was send and will probably be added to the next block # 400: # description: The Tx was malformated - -components: - schemas: - Address: - type: string - example: DF096FDE8D380FA5B2AD20DB2962C82DDEA1ED9B - Coins: - type: object - properties: - denom: - type: string - example: steak - amount: - type: number - example: 50 - Hash: - type: string - example: EE5F3404034C524501629B56E0DDC38FAD651F04 - Tx: - type: object - properties: - type: - type: string - enum: - - stake/delegate - data: + +definitions: + Address: + type: string + example: DF096FDE8D380FA5B2AD20DB2962C82DDEA1ED9B + Coins: + type: object + properties: + denom: + type: string + example: steak + amount: + type: number + example: 50 + Hash: + type: string + example: EE5F3404034C524501629B56E0DDC38FAD651F04 + Tx: + type: object + properties: + type: + type: string + enum: + - stake/delegate + data: + type: object + TxChain: + type: object + properties: + type: + type: string + default: chain/tx + data: + type: object + properties: + chain_id: + type: string + example: gaia-2 + expires_at: + type: number + example: 0 + tx: + type: object + properties: + type: + type: string + default: nonce + data: + type: object + properties: + sequence: + type: number + example: 0 + signers: + type: array + items: + type: object + properties: + chain: + type: string + example: '' + app: + type: string + default: sigs + addr: + $ref: "#/definitions/Address" + tx: + $ref: "#/definitions/Tx" + TxBuild: + type: object + properties: + type: + type: string + default: sigs/one + data: + type: object + properties: + tx: + $ref: "#/definitions/Tx" + signature: + type: object + properties: + Sig: + type: string + default: '' + Pubkey: + type: string + default: '' + TxSigned: + type: object + properties: + type: + type: string + default: sigs/one + data: + type: object + properties: + tx: + $ref: "#/definitions/Tx" + signature: + type: object + properties: + Sig: + type: string + example: 81B11E717789600CC192B26F452A983DF13B985EE75ABD9DD9E68D7BA007A958 + Pubkey: + $ref: "#/definitions/PubKey" + PubKey: + type: object + properties: + type: + type: string + enum: + - ed25519 + data: + type: string + example: 81B11E717789600CC192B26F452A983DF13B985EE75ABD9DD9E68D7BA007A958 + Account: + type: object + properties: + name: + type: string + example: Main Account + address: + $ref: "#/definitions/Address" + pub_key: + $ref: "#/definitions/PubKey" + Balance: + type: object + properties: + height: + type: number + example: 123456 + coins: + type: array + items: + $ref: "#/definitions/Coins" + credit: + type: array + items: type: object - TxChain: - type: object - properties: - type: - type: string - default: chain/tx - data: + BlockID: + type: object + properties: + hash: + $ref: "#/definitions/Hash" + parts: + type: object + properties: + total: + type: number + example: 0 + hash: + $ref: "#/definitions/Hash" + Block: + type: object + properties: + header: + type: object + properties: + chain_id: + type: string + example: gaia-2 + height: + type: number + example: 1 + time: + type: string + example: '2017-12-30T05:53:09.287+01:00' + num_txs: + type: number + example: 0 + last_block_id: + $ref: "#/definitions/BlockID" + total_txs: + type: number + example: 35 + last_commit_hash: + $ref: "#/definitions/Hash" + data_hash: + $ref: "#/definitions/Hash" + validators_hash: + $ref: "#/definitions/Hash" + consensus_hash: + $ref: "#/definitions/Hash" + app_hash: + $ref: "#/definitions/Hash" + last_results_hash: + $ref: "#/definitions/Hash" + evidence_hash: + $ref: "#/definitions/Hash" + txs: + type: array + items: + $ref: "#/definitions/Tx" + evidence: + type: array + items: type: object - properties: - chain_id: - type: string - example: gaia-2 - expires_at: - type: number - example: 0 - tx: + last_commit: + type: object + properties: + blockID: + $ref: "#/definitions/BlockID" + precommits: + type: array + items: type: object - properties: - type: - type: string - default: nonce - data: - type: object - properties: - sequence: - type: number - example: 0 - signers: - type: array - items: - type: object - properties: - chain: - type: string - example: '' - app: - type: string - default: sigs - addr: - $ref: "#/components/schemas/Address" - tx: - $ref: "#/components/schemas/Tx" - TxBuild: - type: object - properties: - type: - type: string - default: sigs/one - data: - type: object - properties: - tx: - $ref: "#/components/schemas/Tx" - signature: - type: object - properties: - Sig: - type: string - default: '' - Pubkey: - type: string - default: '' - TxSigned: - type: object - properties: - type: - type: string - default: sigs/one - data: - type: object - properties: - tx: - $ref: "#/components/schemas/Tx" - signature: - type: object - properties: - Sig: - type: string - example: 81B11E717789600CC192B26F452A983DF13B985EE75ABD9DD9E68D7BA007A958 - Pubkey: - $ref: "#/components/schemas/PubKey" - PubKey: - type: object - properties: - type: - type: string - enum: - - ed25519 - data: - type: string - example: 81B11E717789600CC192B26F452A983DF13B985EE75ABD9DD9E68D7BA007A958 - Account: - type: object - properties: - name: - type: string - example: Main Account - address: - $ref: "#/components/schemas/Address" - pub_key: - $ref: "#/components/schemas/PubKey" - Balance: - type: object - properties: - address: - type: string - coins: - type: array - items: - $ref: "#/components/schemas/Coins" - public_key: - $ref: "#/components/schemas/PubKey" - sequence: - type: number - BlockID: - type: object - properties: - hash: - $ref: "#/components/schemas/Hash" - parts: - type: object - properties: - total: - type: number - example: 0 - hash: - $ref: "#/components/schemas/Hash" - Block: - type: object - properties: - header: - type: object - properties: - chain_id: - type: string - example: gaia-2 - height: - type: number - example: 1 - time: - type: string - example: '2017-12-30T05:53:09.287+01:00' - num_txs: - type: number - example: 0 - last_block_id: - $ref: "#/components/schemas/BlockID" - total_txs: - type: number - example: 35 - last_commit_hash: - $ref: "#/components/schemas/Hash" - data_hash: - $ref: "#/components/schemas/Hash" - validators_hash: - $ref: "#/components/schemas/Hash" - consensus_hash: - $ref: "#/components/schemas/Hash" - app_hash: - $ref: "#/components/schemas/Hash" - last_results_hash: - $ref: "#/components/schemas/Hash" - evidence_hash: - $ref: "#/components/schemas/Hash" - txs: - type: array - items: - $ref: "#/components/schemas/Tx" - evidence: - type: array - last_commit: - type: object - properties: - blockID: - $ref: "#/components/schemas/BlockID" - precommits: - type: array - Delegate: - type: object - properties: - pub_key: - $ref: "#/components/schemas/PubKey" - power: - type: number - example: 1000 - name: - type: string - example: "159.89.3.34" - - - securitySchemes: - sign: - type: http - scheme: basic + Delegate: + type: object + properties: + pub_key: + $ref: "#/definitions/PubKey" + power: + type: number + example: 1000 + name: + type: string + example: "159.89.3.34" +# Added by API Auto Mocking Plugin +host: virtserver.swaggerhub.com +basePath: /faboweb1/Cosmos-LCD-2/1.0.0 +schemes: + - https diff --git a/docs/spec/governance/state.md b/docs/spec/governance/state.md index e533ec6fe..91cfa1f76 100644 --- a/docs/spec/governance/state.md +++ b/docs/spec/governance/state.md @@ -103,9 +103,9 @@ type ValidatorGovInfo struct { * `Proposals: int64 => Proposal` maps `proposalID` to the `Proposal` `proposalID` -* `Options: => VoteType`: maps to the vote of the `voterAddress` for `proposalID` re its delegation to `validatorAddress`. +* `Options: => VoteType`: maps to the vote of the `voterAddress` for `proposalID` re its delegation to `Address`. Returns 0x0 If `voterAddress` has not voted under this validator. -* `ValidatorGovInfos: => ValidatorGovInfo`: maps to the gov info for the `validatorAddress` and `proposalID`. +* `ValidatorGovInfos: => ValidatorGovInfo`: maps to the gov info for the `Address` and `proposalID`. Returns `nil` if proposal has not entered voting period or if `address` was not the address of a validator when proposal entered voting period. diff --git a/docs/spec/governance/transactions.md b/docs/spec/governance/transactions.md index 5f5401de8..aacf046c1 100644 --- a/docs/spec/governance/transactions.md +++ b/docs/spec/governance/transactions.md @@ -184,7 +184,7 @@ vote on the proposal. type TxGovVote struct { ProposalID int64 // proposalID of the proposal Option string // option from OptionSet chosen by the voter - ValidatorAddress crypto.address // Address of the validator voter wants to tie its vote to + Address crypto.address // Address of the validator voter wants to tie its vote to } ``` @@ -202,7 +202,7 @@ Votes need to be tied to a validator in order to compute validator's voting power. If a delegator is bonded to multiple validators, it will have to send one transaction per validator (the UI should facilitate this so that multiple transactions can be sent in one "vote flow"). If the sender is the validator -itself, then it will input its own address as `ValidatorAddress` +itself, then it will input its own address as `Address` Next is a pseudocode proposal of the way `TxGovVote` transactions are handled: @@ -223,39 +223,39 @@ handled: // There is no proposal for this proposalID throw - validator = load(CurrentValidators, txGovVote.ValidatorAddress) + validator = load(CurrentValidators, txGovVote.Address) if !proposal.InitProcedure.OptionSet.includes(txGovVote.Option) OR (validator == nil) then // Throws if // Option is not in Option Set of procedure that was active when vote opened OR if - // ValidatorAddress is not the address of a current validator + // Address is not the address of a current validator throw - option = load(Options, ::) + option = load(Options, ::) if (option != nil) - // sender has already voted with the Atoms bonded to ValidatorAddress + // sender has already voted with the Atoms bonded to Address throw if (proposal.VotingStartBlock < 0) OR (CurrentBlock > proposal.VotingStartBlock + proposal.InitProcedure.VotingPeriod) OR - (proposal.VotingStartBlock < lastBondingBlock(sender, txGovVote.ValidatorAddress) OR + (proposal.VotingStartBlock < lastBondingBlock(sender, txGovVote.Address) OR (proposal.VotingStartBlock < lastUnbondingBlock(sender, txGovVote.Address) OR (proposal.Votes.YesVotes/proposal.InitTotalVotingPower >= 2/3) then // Throws if // Vote has not started OR if // Vote had ended OR if - // sender bonded Atoms to ValidatorAddress after start of vote OR if - // sender unbonded Atoms from ValidatorAddress after start of vote OR if + // sender bonded Atoms to Address after start of vote OR if + // sender unbonded Atoms from Address after start of vote OR if // special condition is met, i.e. proposal is accepted and closed throw - validatorGovInfo = load(ValidatorGovInfos, :) + validatorGovInfo = load(ValidatorGovInfos, :) if (validatorGovInfo == nil) // validator became validator after proposal entered voting period @@ -263,39 +263,39 @@ handled: // sender can vote, check if sender == validator and store sender's option in Options - store(Options, ::, txGovVote.Option) + store(Options, ::, txGovVote.Option) if (sender != validator.address) - // Here, sender is not the Address of the validator whose Address is txGovVote.ValidatorAddress + // Here, sender is not the Address of the validator whose Address is txGovVote.Address - if sender does not have bonded Atoms to txGovVote.ValidatorAddress then + if sender does not have bonded Atoms to txGovVote.Address then // check in Staking module throw - validatorOption = load(Options, ::) + validatorOption = load(Options, ::) if (validatorOption == nil) // Validator has not voted already - validatorGovInfo.Minus += sender.bondedAmounTo(txGovVote.ValidatorAddress) - store(ValidatorGovInfos, :, validatorGovInfo) + validatorGovInfo.Minus += sender.bondedAmounTo(txGovVote.Address) + store(ValidatorGovInfos, :, validatorGovInfo) else // Validator has already voted // Reduce votes of option chosen by validator by sender's bonded Amount - proposal.Votes.validatorOption -= sender.bondedAmountTo(txGovVote.ValidatorAddress) + proposal.Votes.validatorOption -= sender.bondedAmountTo(txGovVote.Address) // increase votes of option chosen by sender by bonded Amount senderOption = txGovVote.Option - propoal.Votes.senderOption -= sender.bondedAmountTo(txGovVote.ValidatorAddress) + propoal.Votes.senderOption -= sender.bondedAmountTo(txGovVote.Address) store(Proposals, txGovVote.ProposalID, proposal) else - // sender is the address of the validator whose main Address is txGovVote.ValidatorAddress + // sender is the address of the validator whose main Address is txGovVote.Address // i.e. sender == validator proposal.Votes.validatorOption += (validatorGovInfo.InitVotingPower - validatorGovInfo.Minus) diff --git a/examples/basecoin/app/app.go b/examples/basecoin/app/app.go index b1a434fa2..086fa32b3 100644 --- a/examples/basecoin/app/app.go +++ b/examples/basecoin/app/app.go @@ -35,10 +35,11 @@ type BasecoinApp struct { keyStake *sdk.KVStoreKey // Manage getting and setting accounts - accountMapper sdk.AccountMapper - coinKeeper bank.Keeper - ibcMapper ibc.Mapper - stakeKeeper stake.Keeper + accountMapper auth.AccountMapper + feeCollectionKeeper auth.FeeCollectionKeeper + coinKeeper bank.Keeper + ibcMapper ibc.Mapper + stakeKeeper stake.Keeper } func NewBasecoinApp(logger log.Logger, db dbm.DB) *BasecoinApp { @@ -70,7 +71,7 @@ func NewBasecoinApp(logger log.Logger, db dbm.DB) *BasecoinApp { // register message routes app.Router(). - AddRoute("auth", auth.NewHandler(app.accountMapper.(auth.AccountMapper))). + AddRoute("auth", auth.NewHandler(app.accountMapper)). AddRoute("bank", bank.NewHandler(app.coinKeeper)). AddRoute("ibc", ibc.NewHandler(app.ibcMapper, app.coinKeeper)). AddRoute("stake", stake.NewHandler(app.stakeKeeper)) @@ -78,7 +79,7 @@ func NewBasecoinApp(logger log.Logger, db dbm.DB) *BasecoinApp { // Initialize BaseApp. app.SetInitChainer(app.initChainer) app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyIBC, app.keyStake) - app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, auth.BurnFeeHandler)) + app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper)) err := app.LoadLatestVersion(app.keyMain) if err != nil { cmn.Exit(err.Error()) @@ -96,7 +97,7 @@ func MakeCodec() *wire.Codec { ibc.RegisterWire(cdc) // register custom AppAccount - cdc.RegisterInterface((*sdk.Account)(nil), nil) + cdc.RegisterInterface((*auth.Account)(nil), nil) cdc.RegisterConcrete(&types.AppAccount{}, "basecoin/Account", nil) return cdc } @@ -129,7 +130,7 @@ func (app *BasecoinApp) ExportAppStateJSON() (appState json.RawMessage, err erro // iterate to get the accounts accounts := []*types.GenesisAccount{} - appendAccount := func(acc sdk.Account) (stop bool) { + appendAccount := func(acc auth.Account) (stop bool) { account := &types.GenesisAccount{ Address: acc.GetAddress(), Coins: acc.GetCoins(), diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index d0b59f331..e297288d3 100644 --- a/examples/basecoin/app/app_test.go +++ b/examples/basecoin/app/app_test.go @@ -37,7 +37,7 @@ var ( coins = sdk.Coins{{"foocoin", 10}} halfCoins = sdk.Coins{{"foocoin", 5}} manyCoins = sdk.Coins{{"foocoin", 1}, {"barcoin", 1}} - fee = sdk.StdFee{ + fee = auth.StdFee{ sdk.Coins{{"foocoin", 0}}, 100000, } @@ -471,17 +471,17 @@ func TestIBCMsgs(t *testing.T) { SignCheckDeliver(t, bapp, receiveMsg, []int64{3}, false, priv1) } -func genTx(msg sdk.Msg, seq []int64, priv ...crypto.PrivKeyEd25519) sdk.StdTx { - sigs := make([]sdk.StdSignature, len(priv)) +func genTx(msg sdk.Msg, seq []int64, priv ...crypto.PrivKeyEd25519) auth.StdTx { + sigs := make([]auth.StdSignature, len(priv)) for i, p := range priv { - sigs[i] = sdk.StdSignature{ + sigs[i] = auth.StdSignature{ PubKey: p.PubKey(), - Signature: p.Sign(sdk.StdSignBytes(chainID, seq, fee, msg)), + Signature: p.Sign(auth.StdSignBytes(chainID, seq, fee, msg)), Sequence: seq[i], } } - return sdk.NewStdTx(msg, fee, sigs) + return auth.NewStdTx(msg, fee, sigs) } diff --git a/examples/basecoin/types/account.go b/examples/basecoin/types/account.go index e6eb5d7b4..223e0b9eb 100644 --- a/examples/basecoin/types/account.go +++ b/examples/basecoin/types/account.go @@ -6,7 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" ) -var _ sdk.Account = (*AppAccount)(nil) +var _ auth.Account = (*AppAccount)(nil) // Custom extensions for this application. This is just an example of // extending auth.BaseAccount with custom fields. @@ -23,8 +23,8 @@ func (acc AppAccount) GetName() string { return acc.Name } func (acc *AppAccount) SetName(name string) { acc.Name = name } // Get the AccountDecoder function for the custom AppAccount -func GetAccountDecoder(cdc *wire.Codec) sdk.AccountDecoder { - return func(accBytes []byte) (res sdk.Account, err error) { +func GetAccountDecoder(cdc *wire.Codec) auth.AccountDecoder { + return func(accBytes []byte) (res auth.Account, err error) { if len(accBytes) == 0 { return nil, sdk.ErrTxDecode("accBytes are empty") } diff --git a/examples/democoin/app/app.go b/examples/democoin/app/app.go index 7c8250b18..2075a64da 100644 --- a/examples/democoin/app/app.go +++ b/examples/democoin/app/app.go @@ -39,14 +39,15 @@ type DemocoinApp struct { capKeyStakingStore *sdk.KVStoreKey // keepers - coinKeeper bank.Keeper - coolKeeper cool.Keeper - powKeeper pow.Keeper - ibcMapper ibc.Mapper - stakeKeeper simplestake.Keeper + feeCollectionKeeper auth.FeeCollectionKeeper + coinKeeper bank.Keeper + coolKeeper cool.Keeper + powKeeper pow.Keeper + ibcMapper ibc.Mapper + stakeKeeper simplestake.Keeper // Manage getting and setting accounts - accountMapper sdk.AccountMapper + accountMapper auth.AccountMapper } func NewDemocoinApp(logger log.Logger, db dbm.DB) *DemocoinApp { @@ -89,7 +90,7 @@ func NewDemocoinApp(logger log.Logger, db dbm.DB) *DemocoinApp { // Initialize BaseApp. app.SetInitChainer(app.initChainerFn(app.coolKeeper, app.powKeeper)) app.MountStoresIAVL(app.capKeyMainStore, app.capKeyAccountStore, app.capKeyPowStore, app.capKeyIBCStore, app.capKeyStakingStore) - app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, auth.BurnFeeHandler)) + app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper)) err := app.LoadLatestVersion(app.capKeyMainStore) if err != nil { cmn.Exit(err.Error()) @@ -109,7 +110,7 @@ func MakeCodec() *wire.Codec { simplestake.RegisterWire(cdc) // Register AppAccount - cdc.RegisterInterface((*sdk.Account)(nil), nil) + cdc.RegisterInterface((*auth.Account)(nil), nil) cdc.RegisterConcrete(&types.AppAccount{}, "democoin/Account", nil) return cdc } @@ -158,7 +159,7 @@ func (app *DemocoinApp) ExportAppStateJSON() (appState json.RawMessage, err erro // iterate to get the accounts accounts := []*types.GenesisAccount{} - appendAccount := func(acc sdk.Account) (stop bool) { + appendAccount := func(acc auth.Account) (stop bool) { account := &types.GenesisAccount{ Address: acc.GetAddress(), Coins: acc.GetCoins(), diff --git a/examples/democoin/app/app_test.go b/examples/democoin/app/app_test.go index b0f188f10..e025c5062 100644 --- a/examples/democoin/app/app_test.go +++ b/examples/democoin/app/app_test.go @@ -31,7 +31,7 @@ var ( addr1 = priv1.PubKey().Address() addr2 = crypto.GenPrivKeyEd25519().PubKey().Address() coins = sdk.Coins{{"foocoin", 10}} - fee = sdk.StdFee{ + fee = auth.StdFee{ sdk.Coins{{"foocoin", 0}}, 1000000, } @@ -93,8 +93,8 @@ func TestMsgs(t *testing.T) { sequences := []int64{0} for i, m := range msgs { - sig := priv1.Sign(sdk.StdSignBytes(chainID, sequences, fee, m.msg)) - tx := sdk.NewStdTx(m.msg, fee, []sdk.StdSignature{{ + sig := priv1.Sign(auth.StdSignBytes(chainID, sequences, fee, m.msg)) + tx := auth.NewStdTx(m.msg, fee, []auth.StdSignature{{ PubKey: priv1.PubKey(), Signature: sig, }}) @@ -194,8 +194,8 @@ func TestMsgSendWithAccounts(t *testing.T) { // Sign the tx sequences := []int64{0} - sig := priv1.Sign(sdk.StdSignBytes(chainID, sequences, fee, sendMsg)) - tx := sdk.NewStdTx(sendMsg, fee, []sdk.StdSignature{{ + sig := priv1.Sign(auth.StdSignBytes(chainID, sequences, fee, sendMsg)) + tx := auth.NewStdTx(sendMsg, fee, []auth.StdSignature{{ PubKey: priv1.PubKey(), Signature: sig, }}) @@ -227,7 +227,7 @@ func TestMsgSendWithAccounts(t *testing.T) { // resigning the tx with the bumped sequence should work sequences = []int64{1} - sig = priv1.Sign(sdk.StdSignBytes(chainID, sequences, fee, tx.Msg)) + sig = priv1.Sign(auth.StdSignBytes(chainID, sequences, fee, tx.Msg)) tx.Signatures[0].Signature = sig res = bapp.Deliver(tx) assert.Equal(t, sdk.ABCICodeOK, res.Code, res.Log) @@ -394,9 +394,9 @@ func TestHandler(t *testing.T) { func SignCheckDeliver(t *testing.T, bapp *DemocoinApp, msg sdk.Msg, seq int64, expPass bool) { // Sign the tx - tx := sdk.NewStdTx(msg, fee, []sdk.StdSignature{{ + tx := auth.NewStdTx(msg, fee, []auth.StdSignature{{ PubKey: priv1.PubKey(), - Signature: priv1.Sign(sdk.StdSignBytes(chainID, []int64{seq}, fee, msg)), + Signature: priv1.Sign(auth.StdSignBytes(chainID, []int64{seq}, fee, msg)), Sequence: seq, }}) diff --git a/examples/democoin/types/account.go b/examples/democoin/types/account.go index 8a744229a..211cf3c41 100644 --- a/examples/democoin/types/account.go +++ b/examples/democoin/types/account.go @@ -9,7 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/examples/democoin/x/pow" ) -var _ sdk.Account = (*AppAccount)(nil) +var _ auth.Account = (*AppAccount)(nil) // Custom extensions for this application. This is just an example of // extending auth.BaseAccount with custom fields. @@ -26,8 +26,8 @@ func (acc AppAccount) GetName() string { return acc.Name } func (acc *AppAccount) SetName(name string) { acc.Name = name } // Get the AccountDecoder function for the custom AppAccount -func GetAccountDecoder(cdc *wire.Codec) sdk.AccountDecoder { - return func(accBytes []byte) (res sdk.Account, err error) { +func GetAccountDecoder(cdc *wire.Codec) auth.AccountDecoder { + return func(accBytes []byte) (res auth.Account, err error) { if len(accBytes) == 0 { return nil, sdk.ErrTxDecode("accBytes are empty") } diff --git a/examples/democoin/x/simplestake/keeper_test.go b/examples/democoin/x/simplestake/keeper_test.go index 302a2e58b..515c19cc5 100644 --- a/examples/democoin/x/simplestake/keeper_test.go +++ b/examples/democoin/x/simplestake/keeper_test.go @@ -31,10 +31,13 @@ func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey) { } func TestKeeperGetSet(t *testing.T) { - ms, _, capKey := setupMultiStore() + ms, authKey, capKey := setupMultiStore() + cdc := wire.NewCodec() + auth.RegisterBaseAccount(cdc) ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger()) - stakeKeeper := NewKeeper(capKey, bank.NewKeeper(nil), DefaultCodespace) + accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{}) + stakeKeeper := NewKeeper(capKey, bank.NewKeeper(accountMapper), DefaultCodespace) addr := sdk.Address([]byte("some-address")) bi := stakeKeeper.getBondInfo(ctx, addr) diff --git a/examples/examples.md b/examples/examples.md new file mode 100644 index 000000000..55db606c4 --- /dev/null +++ b/examples/examples.md @@ -0,0 +1,321 @@ +# Basecoin Example + +Here we explain how to get started with a basic Basecoin blockchain, how +to send transactions between accounts using the ``basecli`` tool, and +what is happening under the hood. + +## Setup and Install + +You will need to have go installed on your computer. Please refer to the [cosmos testnet tutorial](https://cosmos.network/validators/tutorial), which will always have the most updated instructions on how to get setup with go and the cosmos repository. + +Once you have go installed, run the command: + +``` +go get github.com/cosmos/cosmos-sdk +``` + +There will be an error stating `can't load package: package github.com/cosmos/cosmos-sdk: no Go files`, however you can ignore this error, it doesn't affect us. Now change directories to: + +``` +cd $GOPATH/src/github.com/cosmos/cosmos-sdk +``` + +And run : + +``` +make get_tools // run make update_tools if you already had it installed +make get_vendor_deps +make install_examples +``` +Then run `make install_examples`, which creates binaries for `basecli` and `basecoind`. You can look at the Makefile if you want to see the details on what these make commands are doing. + +## Using basecli and basecoind + +Check the versions by running: + +``` +basecli version +basecoind version +``` + +They should read something like `0.17.1-5d18d5f`, but the versions will be constantly updating so don't worry if your version is higher that 0.17.1. That's a good thing. + +Note that you can always check help in the terminal by running `basecli -h` or `basecoind -h`. It is good to check these out if you are stuck, because updates to the code base might slightly change the commands, and you might find the correct command in there. + +Let's start by initializing the basecoind daemon. Run the command + +``` +basecoind init +``` + +And you should see something like this: + +``` +{ + "chain_id": "test-chain-z77iHG", + "node_id": "e14c5056212b5736e201dd1d64c89246f3288129", + "app_message": { + "secret": "pluck life bracket worry guilt wink upgrade olive tilt output reform census member trouble around abandon" + } +} +``` + +This creates the `~/.basecoind folder`, which has config.toml, genesis.json, node_key.json, priv_validator.json. Take some time to review what is contained in these files if you want to understand what is going on at a deeper level. + + +## Generating keys + +The next thing we'll need to do is add the key from priv_validator.json to the gaiacli key manager. For this we need the 16 word seed that represents the private key, and a password. You can also get the 16 word seed from the output seen above, under `"secret"`. Then run the command: + +``` +basecli keys add alice --recover +``` + +Which will give you three prompts: + +``` +Enter a passphrase for your key: +Repeat the passphrase: +Enter your recovery seed phrase: +``` + +You just created your first locally stored key, under the name alice, and this account is linked to the private key that is running the basecoind validator node. Once you do this, the ~/.basecli folder is created, which will hold the alice key and any other keys you make. Now that you have the key for alice, you can start up the blockchain by running + +``` +basecoind start +``` + +You should see blocks being created at a fast rate, with a lot of output in the terminal. + +Next we need to make some more keys so we can use the send transaction functionality of basecoin. Open a new terminal, and run the following commands, to make two new accounts, and give each account a password you can remember: + +``` +basecli keys add bob +basecli keys add charlie +``` + +You can see your keys with the command: + +``` +basecli keys list +``` + +You should now see alice, bob and charlie's account all show up. + +``` +NAME: ADDRESS: PUBKEY: +alice 90B0B9BE0914ECEE0B6DB74E67B07A00056B9BBD 1624DE62201D47E63694448665F5D0217EA8458177728C91C373047A42BD3C0FB78BD0BFA7 +bob 29D721F054537C91F618A0FDBF770DA51EF8C48D 1624DE6220F54B2A2CA9EB4EE30DE23A73D15902E087C09CC5616456DDDD3814769E2E0A16 +charlie 2E8E13EEB8E3F0411ACCBC9BE0384732C24FBD5E 1624DE6220F8C9FB8B07855FD94126F88A155BD6EB973509AE5595EFDE1AF05B4964836A53 +``` + + +## Send transactions + +Lets send bob and charlie some tokens. First, lets query alice's account so we can see what kind of tokens she has: + +``` +basecli account 90B0B9BE0914ECEE0B6DB74E67B07A00056B9BBD +``` + +Where `90B0B9BE0914ECEE0B6DB74E67B07A00056B9BBD` is alice's address we got from running `basecli keys list`. You should see a large amount of "mycoin" there. If you search for bob's or charlie's address, the command will fail, because they haven't been added into the blockchain database yet since they have no coins. We need to send them some! + +The following command will send coins from alice, to bob: + +``` +basecli send --name=alice --amount=10000mycoin --to=29D721F054537C91F618A0FDBF770DA51EF8C48D +--sequence=0 --chain-id=test-chain-AE4XQo +``` + +Flag Descriptions: +- `name` is the name you gave your key +- `mycoin` is the name of the token for this basecoin demo, initialized in the genesis.json file +- `sequence` is a tally of how many transactions have been made by this account. Since this is the first tx on this account, it is 0 +- `chain-id` is the unique ID that helps tendermint identify which network to connect to. You can find it in the terminal output from the gaiad daemon in the header block , or in the genesis.json file at `~/.basecoind/config/genesis.json` + +Now if we check bobs account, it should have `10000 mycoin`. You can do so by running : + +``` +basecli account 29D721F054537C91F618A0FDBF770DA51EF8C48D +``` + +Now lets send some from bob to charlie. Make sure you send less than bob has, otherwise the transaction will fail: + +``` +basecli send --name=bob --amount=5000mycoin --to=2E8E13EEB8E3F0411ACCBC9BE0384732C24FBD5E +--sequence=0 --chain-id=test-chain-AE4XQo +``` + +Note how we use the ``--name`` flag to select a different account to send from. + +Lets now try to send from bob back to alice: + +``` +basecli send --name=bob --amount=3000mycoin --to=90B0B9BE0914ECEE0B6DB74E67B07A00056B9BBD +--sequence=1 --chain-id=test-chain-AE4XQo +``` + +Notice that the sequence is now 1, since we have already recorded bobs 1st transaction as `sequence 0`. Also note the ``hash`` value in the response in the terminal - this is the hash of the transaction. We can query for the transaction with this command: + +``` +basecli tx +``` + +It will return the details of the transaction hash, such as how many coins were send and to which address, and on what block it occurred. + +That is the basic implementation of basecoin! + + +## Reset the basecoind blockchain and basecli data + +**WARNING:** Running these commands will wipe out any existing +information in both the ``~/.basecli`` and ``~/.basecoind`` directories, +including private keys. This should be no problem considering that basecoin +is just an example, but it is always good to pay extra attention when +you are removing private keys, in any scenario involving a blockchain. + +To remove all the files created and refresh your environment (e.g., if +starting this tutorial again or trying something new), the following +commands are run: + +``` +basecoind unsafe_reset_all +rm -rf ~/.basecoind +rm -rf ~/.basecli +``` + +## Technical Details on how Basecoin Works + +This section describes some of the more technical aspects for what is going on under the hood of Basecoin. + +## Proof + +Even if you don't see it in the UI, the result of every query comes with +a proof. This is a Merkle proof that the result of the query is actually +contained in the state. And the state's Merkle root is contained in a +recent block header. Behind the scenes, ``basecli`` will not only +verify that this state matches the header, but also that the header is +properly signed by the known validator set. It will even update the +validator set as needed, so long as there have not been major changes +and it is secure to do so. So, if you wonder why the query may take a +second... there is a lot of work going on in the background to make sure +even a lying full node can't trick your client. + +## Accounts and Transactions + +For a better understanding of how to further use the tools, it helps to +understand the underlying data structures, so lets look at accounts and transactions. + +### Accounts + +The Basecoin state consists entirely of a set of accounts. Each account +contains an address, a public key, a balance in many different coin denominations, +and a strictly increasing sequence number for replay protection. This +type of account was directly inspired by accounts in Ethereum, and is +unlike Bitcoin's use of Unspent Transaction Outputs (UTXOs). + +``` +type BaseAccount struct { + Address sdk.Address `json:"address"` + Coins sdk.Coins `json:"coins"` + PubKey crypto.PubKey `json:"public_key"` + Sequence int64 `json:"sequence"` +} +``` + +You can also add more fields to accounts, and basecoin actually does so. Basecoin +adds a Name field in order to show how easily the base account structure can be +modified to suit any applications needs. It takes the `auth.BaseAccount` we see above, +and extends it with `Name`. + +``` +type AppAccount struct { + auth.BaseAccount + Name string `json:"name"` +} +``` + +Within accounts, coin balances are stored. Basecoin is a multi-asset cryptocurrency, so each account can have many +different kinds of tokens, which are held in an array. + +``` +type Coins []Coin + +type Coin struct { + Denom string `json:"denom"` + Amount int64 `json:"amount"` +} +``` + +If you want to add more coins to a blockchain, you can do so manually in +the ``~/.basecoin/genesis.json`` before you start the blockchain for the +first time. + +Accounts are serialized and stored in a Merkle tree under the key +``base/a/
``, where ``
`` is the address of the account. +Typically, the address of the account is the 20-byte ``RIPEMD160`` hash +of the public key, but other formats are acceptable as well, as defined +in the `Tendermint crypto +library `__. The Merkle tree +used in Basecoin is a balanced, binary search tree, which we call an +`IAVL tree `__. + +### Transactions + +Basecoin defines a transaction type, the `SendTx`, which allows tokens +to be sent to other accounts. The `SendTx` takes a list of inputs and +a list of outputs, and transfers all the tokens listed in the inputs +from their corresponding accounts to the accounts listed in the output. +The `SendTx` is structured as follows: +``` +type SendTx struct { + Gas int64 `json:"gas"` + Fee Coin `json:"fee"` + Inputs []TxInput `json:"inputs"` + Outputs []TxOutput `json:"outputs"` +} + +type TxInput struct { + Address []byte `json:"address"` // Hash of the PubKey + Coins Coins `json:"coins"` // + Sequence int `json:"sequence"` // Must be 1 greater than the last committed TxInput + Signature crypto.Signature `json:"signature"` // Depends on the PubKey type and the whole Tx + PubKey crypto.PubKey `json:"pub_key"` // Is present iff Sequence == 0 +} + +type TxOutput struct { + Address []byte `json:"address"` // Hash of the PubKey + Coins Coins `json:"coins"` // +} +``` +Note the `SendTx` includes a field for `Gas` and `Fee`. The +`Gas` limits the total amount of computation that can be done by the +transaction, while the `Fee` refers to the total amount paid in fees. +This is slightly different from Ethereum's concept of `Gas` and +`GasPrice`, where `Fee = Gas x GasPrice`. In Basecoin, the `Gas` +and `Fee` are independent, and the `GasPrice` is implicit. + +In Basecoin, the `Fee` is meant to be used by the validators to inform +the ordering of transactions, like in Bitcoin. And the `Gas` is meant +to be used by the application plugin to control its execution. There is +currently no means to pass `Fee` information to the Tendermint +validators, but it will come soon... so this version of Basecoin does +not actually fully implement fees and gas, but it still allows us +to send transactions between accounts. + +Note also that the `PubKey` only needs to be sent for +`Sequence == 0`. After that, it is stored under the account in the +Merkle tree and subsequent transactions can exclude it, using only the +`Address` to refer to the sender. Ethereum does not require public +keys to be sent in transactions as it uses a different elliptic curve +scheme which enables the public key to be derived from the signature +itself. + +Finally, note that the use of multiple inputs and multiple outputs +allows us to send many different types of tokens between many different +accounts at once in an atomic transaction. Thus, the `SendTx` can +serve as a basic unit of decentralized exchange. When using multiple +inputs and outputs, you must make sure that the sum of coins of the +inputs equals the sum of coins of the outputs (no creating money), and +that all accounts that provide inputs have signed the transaction. + diff --git a/examples/kvstore/tx.go b/examples/kvstore/tx.go index 12bce0736..fa32d93bf 100644 --- a/examples/kvstore/tx.go +++ b/examples/kvstore/tx.go @@ -4,6 +4,7 @@ import ( "bytes" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" ) // An sdk.Tx which is its own sdk.Msg. @@ -34,7 +35,7 @@ func (tx kvstoreTx) GetSigners() []sdk.Address { return nil } -func (tx kvstoreTx) GetSignatures() []sdk.StdSignature { +func (tx kvstoreTx) GetSignatures() []auth.StdSignature { return nil } diff --git a/mock/tx.go b/mock/tx.go index 81dea4571..bd437f2d0 100644 --- a/mock/tx.go +++ b/mock/tx.go @@ -6,6 +6,7 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" ) // An sdk.Tx which is its own sdk.Msg. @@ -47,7 +48,7 @@ func (tx kvstoreTx) GetSigners() []sdk.Address { return nil } -func (tx kvstoreTx) GetSignatures() []sdk.StdSignature { +func (tx kvstoreTx) GetSignatures() []auth.StdSignature { return nil } diff --git a/networks/remote/README.rst b/networks/remote/README.rst new file mode 100644 index 000000000..de694d049 --- /dev/null +++ b/networks/remote/README.rst @@ -0,0 +1,67 @@ +Terraform & Ansible +=================== + +Automated deployments are done using `Terraform `__ to create servers on Digital Ocean then +`Ansible `__ to create and manage testnets on those servers. + +Prerequisites +------------- + +- Install `Terraform `__ and `Ansible `__ on a Linux machine. +- Create a `DigitalOcean API token `__ with read and write capability. +- Install the python dopy package (``pip install dopy``) (This is necessary for the digitalocean.py script for ansible.) +- Create SSH keys + +:: + + export DO_API_TOKEN="abcdef01234567890abcdef01234567890" + export TESTNET_NAME="remotenet" + export SSH_PRIVATE_FILE="$HOME/.ssh/id_rsa" + export SSH_PUBLIC_FILE="$HOME/.ssh/id_rsa.pub" + +These will be used by both ``terraform`` and ``ansible``. + +Create a remote network +----------------------- + +:: + + make remotenet-start + + +Optionally, you can set the number of servers you want to launch and the name of the testnet (which defaults to remotenet): + +:: + + TESTNET_NAME="mytestnet" SERVERS=7 make remotenet-start + + +Quickly see the /status endpoint +-------------------------------- + +:: + + make remotenet-status + + +Delete servers +-------------- + +:: + + make remotenet-stop + +Logging +------- + +You can ship logs to Logz.io, an Elastic stack (Elastic search, Logstash and Kibana) service provider. You can set up your nodes to log there automatically. Create an account and get your API key from the notes on `this page `__, then: + +:: + + yum install systemd-devel || echo "This will only work on RHEL-based systems." + apt-get install libsystemd-dev || echo "This will only work on Debian-based systems." + + go get github.com/mheese/journalbeat + ansible-playbook -i inventory/digital_ocean.py -l remotenet logzio.yml -e LOGZIO_TOKEN=ABCDEFGHIJKLMNOPQRSTUVWXYZ012345 + + diff --git a/networks/remote/ansible/.gitignore b/networks/remote/ansible/.gitignore new file mode 100644 index 000000000..8826f63a7 --- /dev/null +++ b/networks/remote/ansible/.gitignore @@ -0,0 +1,2 @@ +*.retry +files/* diff --git a/networks/remote/ansible/ansible.cfg b/networks/remote/ansible/ansible.cfg new file mode 100644 index 000000000..045c1ea60 --- /dev/null +++ b/networks/remote/ansible/ansible.cfg @@ -0,0 +1,4 @@ +[defaults] +retry_files_enabled = False +host_key_checking = False + diff --git a/networks/remote/ansible/clear-config.yml b/networks/remote/ansible/clear-config.yml new file mode 100644 index 000000000..675cdd072 --- /dev/null +++ b/networks/remote/ansible/clear-config.yml @@ -0,0 +1,9 @@ +--- + +- hosts: all + user: root + any_errors_fatal: true + gather_facts: no + roles: + - clear-config + diff --git a/networks/remote/ansible/inventory/COPYING b/networks/remote/ansible/inventory/COPYING new file mode 100644 index 000000000..10926e87f --- /dev/null +++ b/networks/remote/ansible/inventory/COPYING @@ -0,0 +1,675 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. + diff --git a/networks/remote/ansible/inventory/digital_ocean.ini b/networks/remote/ansible/inventory/digital_ocean.ini new file mode 100644 index 000000000..b809554b2 --- /dev/null +++ b/networks/remote/ansible/inventory/digital_ocean.ini @@ -0,0 +1,34 @@ +# Ansible DigitalOcean external inventory script settings +# + +[digital_ocean] + +# The module needs your DigitalOcean API Token. +# It may also be specified on the command line via --api-token +# or via the environment variables DO_API_TOKEN or DO_API_KEY +# +#api_token = 123456abcdefg + + +# API calls to DigitalOcean may be slow. For this reason, we cache the results +# of an API call. Set this to the path you want cache files to be written to. +# One file will be written to this directory: +# - ansible-digital_ocean.cache +# +cache_path = /tmp + + +# The number of seconds a cache file is considered valid. After this many +# seconds, a new API call will be made, and the cache file will be updated. +# +cache_max_age = 300 + +# Use the private network IP address instead of the public when available. +# +use_private_network = False + +# Pass variables to every group, e.g.: +# +# group_variables = { 'ansible_user': 'root' } +# +group_variables = {} diff --git a/networks/remote/ansible/inventory/digital_ocean.py b/networks/remote/ansible/inventory/digital_ocean.py new file mode 100755 index 000000000..24ba64370 --- /dev/null +++ b/networks/remote/ansible/inventory/digital_ocean.py @@ -0,0 +1,471 @@ +#!/usr/bin/env python + +''' +DigitalOcean external inventory script +====================================== + +Generates Ansible inventory of DigitalOcean Droplets. + +In addition to the --list and --host options used by Ansible, there are options +for generating JSON of other DigitalOcean data. This is useful when creating +droplets. For example, --regions will return all the DigitalOcean Regions. +This information can also be easily found in the cache file, whose default +location is /tmp/ansible-digital_ocean.cache). + +The --pretty (-p) option pretty-prints the output for better human readability. + +---- +Although the cache stores all the information received from DigitalOcean, +the cache is not used for current droplet information (in --list, --host, +--all, and --droplets). This is so that accurate droplet information is always +found. You can force this script to use the cache with --force-cache. + +---- +Configuration is read from `digital_ocean.ini`, then from environment variables, +then and command-line arguments. + +Most notably, the DigitalOcean API Token must be specified. It can be specified +in the INI file or with the following environment variables: + export DO_API_TOKEN='abc123' or + export DO_API_KEY='abc123' + +Alternatively, it can be passed on the command-line with --api-token. + +If you specify DigitalOcean credentials in the INI file, a handy way to +get them into your environment (e.g., to use the digital_ocean module) +is to use the output of the --env option with export: + export $(digital_ocean.py --env) + +---- +The following groups are generated from --list: + - ID (droplet ID) + - NAME (droplet NAME) + - image_ID + - image_NAME + - distro_NAME (distribution NAME from image) + - region_NAME + - size_NAME + - status_STATUS + +For each host, the following variables are registered: + - do_backup_ids + - do_created_at + - do_disk + - do_features - list + - do_id + - do_image - object + - do_ip_address + - do_private_ip_address + - do_kernel - object + - do_locked + - do_memory + - do_name + - do_networks - object + - do_next_backup_window + - do_region - object + - do_size - object + - do_size_slug + - do_snapshot_ids - list + - do_status + - do_tags + - do_vcpus + - do_volume_ids + +----- +``` +usage: digital_ocean.py [-h] [--list] [--host HOST] [--all] + [--droplets] [--regions] [--images] [--sizes] + [--ssh-keys] [--domains] [--pretty] + [--cache-path CACHE_PATH] + [--cache-max_age CACHE_MAX_AGE] + [--force-cache] + [--refresh-cache] + [--api-token API_TOKEN] + +Produce an Ansible Inventory file based on DigitalOcean credentials + +optional arguments: + -h, --help show this help message and exit + --list List all active Droplets as Ansible inventory + (default: True) + --host HOST Get all Ansible inventory variables about a specific + Droplet + --all List all DigitalOcean information as JSON + --droplets List Droplets as JSON + --regions List Regions as JSON + --images List Images as JSON + --sizes List Sizes as JSON + --ssh-keys List SSH keys as JSON + --domains List Domains as JSON + --pretty, -p Pretty-print results + --cache-path CACHE_PATH + Path to the cache files (default: .) + --cache-max_age CACHE_MAX_AGE + Maximum age of the cached items (default: 0) + --force-cache Only use data from the cache + --refresh-cache Force refresh of cache by making API requests to + DigitalOcean (default: False - use cache files) + --api-token API_TOKEN, -a API_TOKEN + DigitalOcean API Token +``` + +''' + +# (c) 2013, Evan Wies +# +# Inspired by the EC2 inventory plugin: +# https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.py +# +# This file is part of Ansible, +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +###################################################################### + +import os +import sys +import re +import argparse +from time import time +import ConfigParser +import ast + +try: + import json +except ImportError: + import simplejson as json + +try: + from dopy.manager import DoManager +except ImportError as e: + sys.exit("failed=True msg='`dopy` library required for this script'") + + +class DigitalOceanInventory(object): + + ########################################################################### + # Main execution path + ########################################################################### + + def __init__(self): + ''' Main execution path ''' + + # DigitalOceanInventory data + self.data = {} # All DigitalOcean data + self.inventory = {} # Ansible Inventory + + # Define defaults + self.cache_path = '.' + self.cache_max_age = 0 + self.use_private_network = False + self.group_variables = {} + + # Read settings, environment variables, and CLI arguments + self.read_settings() + self.read_environment() + self.read_cli_args() + + # Verify credentials were set + if not hasattr(self, 'api_token'): + sys.stderr.write('''Could not find values for DigitalOcean api_token. +They must be specified via either ini file, command line argument (--api-token), +or environment variables (DO_API_TOKEN)\n''') + sys.exit(-1) + + # env command, show DigitalOcean credentials + if self.args.env: + print("DO_API_TOKEN=%s" % self.api_token) + sys.exit(0) + + # Manage cache + self.cache_filename = self.cache_path + "/ansible-digital_ocean.cache" + self.cache_refreshed = False + + if self.is_cache_valid(): + self.load_from_cache() + if len(self.data) == 0: + if self.args.force_cache: + sys.stderr.write('''Cache is empty and --force-cache was specified\n''') + sys.exit(-1) + + self.manager = DoManager(None, self.api_token, api_version=2) + + # Pick the json_data to print based on the CLI command + if self.args.droplets: + self.load_from_digital_ocean('droplets') + json_data = {'droplets': self.data['droplets']} + elif self.args.regions: + self.load_from_digital_ocean('regions') + json_data = {'regions': self.data['regions']} + elif self.args.images: + self.load_from_digital_ocean('images') + json_data = {'images': self.data['images']} + elif self.args.sizes: + self.load_from_digital_ocean('sizes') + json_data = {'sizes': self.data['sizes']} + elif self.args.ssh_keys: + self.load_from_digital_ocean('ssh_keys') + json_data = {'ssh_keys': self.data['ssh_keys']} + elif self.args.domains: + self.load_from_digital_ocean('domains') + json_data = {'domains': self.data['domains']} + elif self.args.all: + self.load_from_digital_ocean() + json_data = self.data + elif self.args.host: + json_data = self.load_droplet_variables_for_host() + else: # '--list' this is last to make it default + self.load_from_digital_ocean('droplets') + self.build_inventory() + json_data = self.inventory + + if self.cache_refreshed: + self.write_to_cache() + + if self.args.pretty: + print(json.dumps(json_data, sort_keys=True, indent=2)) + else: + print(json.dumps(json_data)) + # That's all she wrote... + + ########################################################################### + # Script configuration + ########################################################################### + + def read_settings(self): + ''' Reads the settings from the digital_ocean.ini file ''' + config = ConfigParser.SafeConfigParser() + config.read(os.path.dirname(os.path.realpath(__file__)) + '/digital_ocean.ini') + + # Credentials + if config.has_option('digital_ocean', 'api_token'): + self.api_token = config.get('digital_ocean', 'api_token') + + # Cache related + if config.has_option('digital_ocean', 'cache_path'): + self.cache_path = config.get('digital_ocean', 'cache_path') + if config.has_option('digital_ocean', 'cache_max_age'): + self.cache_max_age = config.getint('digital_ocean', 'cache_max_age') + + # Private IP Address + if config.has_option('digital_ocean', 'use_private_network'): + self.use_private_network = config.getboolean('digital_ocean', 'use_private_network') + + # Group variables + if config.has_option('digital_ocean', 'group_variables'): + self.group_variables = ast.literal_eval(config.get('digital_ocean', 'group_variables')) + + def read_environment(self): + ''' Reads the settings from environment variables ''' + # Setup credentials + if os.getenv("DO_API_TOKEN"): + self.api_token = os.getenv("DO_API_TOKEN") + if os.getenv("DO_API_KEY"): + self.api_token = os.getenv("DO_API_KEY") + + def read_cli_args(self): + ''' Command line argument processing ''' + parser = argparse.ArgumentParser(description='Produce an Ansible Inventory file based on DigitalOcean credentials') + + parser.add_argument('--list', action='store_true', help='List all active Droplets as Ansible inventory (default: True)') + parser.add_argument('--host', action='store', help='Get all Ansible inventory variables about a specific Droplet') + + parser.add_argument('--all', action='store_true', help='List all DigitalOcean information as JSON') + parser.add_argument('--droplets', '-d', action='store_true', help='List Droplets as JSON') + parser.add_argument('--regions', action='store_true', help='List Regions as JSON') + parser.add_argument('--images', action='store_true', help='List Images as JSON') + parser.add_argument('--sizes', action='store_true', help='List Sizes as JSON') + parser.add_argument('--ssh-keys', action='store_true', help='List SSH keys as JSON') + parser.add_argument('--domains', action='store_true', help='List Domains as JSON') + + parser.add_argument('--pretty', '-p', action='store_true', help='Pretty-print results') + + parser.add_argument('--cache-path', action='store', help='Path to the cache files (default: .)') + parser.add_argument('--cache-max_age', action='store', help='Maximum age of the cached items (default: 0)') + parser.add_argument('--force-cache', action='store_true', default=False, help='Only use data from the cache') + parser.add_argument('--refresh-cache', '-r', action='store_true', default=False, + help='Force refresh of cache by making API requests to DigitalOcean (default: False - use cache files)') + + parser.add_argument('--env', '-e', action='store_true', help='Display DO_API_TOKEN') + parser.add_argument('--api-token', '-a', action='store', help='DigitalOcean API Token') + + self.args = parser.parse_args() + + if self.args.api_token: + self.api_token = self.args.api_token + + # Make --list default if none of the other commands are specified + if (not self.args.droplets and not self.args.regions and + not self.args.images and not self.args.sizes and + not self.args.ssh_keys and not self.args.domains and + not self.args.all and not self.args.host): + self.args.list = True + + ########################################################################### + # Data Management + ########################################################################### + + def load_from_digital_ocean(self, resource=None): + '''Get JSON from DigitalOcean API''' + if self.args.force_cache and os.path.isfile(self.cache_filename): + return + # We always get fresh droplets + if self.is_cache_valid() and not (resource == 'droplets' or resource is None): + return + if self.args.refresh_cache: + resource = None + + if resource == 'droplets' or resource is None: + self.data['droplets'] = self.manager.all_active_droplets() + self.cache_refreshed = True + if resource == 'regions' or resource is None: + self.data['regions'] = self.manager.all_regions() + self.cache_refreshed = True + if resource == 'images' or resource is None: + self.data['images'] = self.manager.all_images(filter=None) + self.cache_refreshed = True + if resource == 'sizes' or resource is None: + self.data['sizes'] = self.manager.sizes() + self.cache_refreshed = True + if resource == 'ssh_keys' or resource is None: + self.data['ssh_keys'] = self.manager.all_ssh_keys() + self.cache_refreshed = True + if resource == 'domains' or resource is None: + self.data['domains'] = self.manager.all_domains() + self.cache_refreshed = True + + def build_inventory(self): + '''Build Ansible inventory of droplets''' + self.inventory = { + 'all': { + 'hosts': [], + 'vars': self.group_variables + }, + '_meta': {'hostvars': {}} + } + + # add all droplets by id and name + for droplet in self.data['droplets']: + # when using private_networking, the API reports the private one in "ip_address". + if 'private_networking' in droplet['features'] and not self.use_private_network: + for net in droplet['networks']['v4']: + if net['type'] == 'public': + dest = net['ip_address'] + else: + continue + else: + dest = droplet['ip_address'] + + self.inventory['all']['hosts'].append(dest) + + self.inventory[droplet['id']] = [dest] + self.inventory[droplet['name']] = [dest] + + # groups that are always present + for group in ('region_' + droplet['region']['slug'], + 'image_' + str(droplet['image']['id']), + 'size_' + droplet['size']['slug'], + 'distro_' + self.to_safe(droplet['image']['distribution']), + 'status_' + droplet['status']): + if group not in self.inventory: + self.inventory[group] = {'hosts': [], 'vars': {}} + self.inventory[group]['hosts'].append(dest) + + # groups that are not always present + for group in (droplet['image']['slug'], + droplet['image']['name']): + if group: + image = 'image_' + self.to_safe(group) + if image not in self.inventory: + self.inventory[image] = {'hosts': [], 'vars': {}} + self.inventory[image]['hosts'].append(dest) + + if droplet['tags']: + for tag in droplet['tags']: + if tag not in self.inventory: + self.inventory[tag] = {'hosts': [], 'vars': {}} + self.inventory[tag]['hosts'].append(dest) + + # hostvars + info = self.do_namespace(droplet) + self.inventory['_meta']['hostvars'][dest] = info + + def load_droplet_variables_for_host(self): + '''Generate a JSON response to a --host call''' + host = int(self.args.host) + droplet = self.manager.show_droplet(host) + info = self.do_namespace(droplet) + return {'droplet': info} + + ########################################################################### + # Cache Management + ########################################################################### + + def is_cache_valid(self): + ''' Determines if the cache files have expired, or if it is still valid ''' + if os.path.isfile(self.cache_filename): + mod_time = os.path.getmtime(self.cache_filename) + current_time = time() + if (mod_time + self.cache_max_age) > current_time: + return True + return False + + def load_from_cache(self): + ''' Reads the data from the cache file and assigns it to member variables as Python Objects''' + try: + cache = open(self.cache_filename, 'r') + json_data = cache.read() + cache.close() + data = json.loads(json_data) + except IOError: + data = {'data': {}, 'inventory': {}} + + self.data = data['data'] + self.inventory = data['inventory'] + + def write_to_cache(self): + ''' Writes data in JSON format to a file ''' + data = {'data': self.data, 'inventory': self.inventory} + json_data = json.dumps(data, sort_keys=True, indent=2) + + cache = open(self.cache_filename, 'w') + cache.write(json_data) + cache.close() + + ########################################################################### + # Utilities + ########################################################################### + + def push(self, my_dict, key, element): + ''' Pushed an element onto an array that may not have been defined in the dict ''' + if key in my_dict: + my_dict[key].append(element) + else: + my_dict[key] = [element] + + def to_safe(self, word): + ''' Converts 'bad' characters in a string to underscores so they can be used as Ansible groups ''' + return re.sub("[^A-Za-z0-9\-\.]", "_", word) + + def do_namespace(self, data): + ''' Returns a copy of the dictionary with all the keys put in a 'do_' namespace ''' + info = {} + for k, v in data.items(): + info['do_' + k] = v + return info + + +########################################################################### +# Run the script +DigitalOceanInventory() diff --git a/networks/remote/ansible/logzio.yml b/networks/remote/ansible/logzio.yml new file mode 100644 index 000000000..da3c43890 --- /dev/null +++ b/networks/remote/ansible/logzio.yml @@ -0,0 +1,14 @@ +--- + +#Note: You need to add LOGZIO_TOKEN variable with your API key. Like this: ansible-playbook -e LOGZIO_TOKEN=ABCXYZ123456 + +- hosts: all + user: root + any_errors_fatal: true + gather_facts: no + vars: + - service: gaiad + - JOURNALBEAT_BINARY: "{{lookup('env', 'GOPATH')}}/bin/journalbeat" + roles: + - logzio + diff --git a/networks/remote/ansible/roles/clear-config/tasks/main.yml b/networks/remote/ansible/roles/clear-config/tasks/main.yml new file mode 100644 index 000000000..5b4504cfb --- /dev/null +++ b/networks/remote/ansible/roles/clear-config/tasks/main.yml @@ -0,0 +1,12 @@ +--- + +- name: Stop service + service: name=gaiad state=stopped + +- name: Delete files + file: "path={{item}} state=absent" + with_items: + - /usr/bin/gaiad + - /home/gaiad/.gaiad + - /home/gaiad/.gaiacli + diff --git a/networks/remote/ansible/roles/logzio/files/journalbeat.service b/networks/remote/ansible/roles/logzio/files/journalbeat.service new file mode 100644 index 000000000..3cb66a454 --- /dev/null +++ b/networks/remote/ansible/roles/logzio/files/journalbeat.service @@ -0,0 +1,15 @@ +[Unit] +Description=journalbeat +#propagates activation, deactivation and activation fails. +Requires=network-online.target +After=network-online.target + +[Service] +Restart=on-failure +ExecStart=/usr/bin/journalbeat -c /etc/journalbeat/journalbeat.yml -path.home /usr/share/journalbeat -path.config /etc/journalbeat -path.data /var/lib/journalbeat -path.logs /var/log/journalbeat +Restart=always + +[Install] +WantedBy=multi-user.target + + diff --git a/networks/remote/ansible/roles/logzio/handlers/main.yml b/networks/remote/ansible/roles/logzio/handlers/main.yml new file mode 100644 index 000000000..0b371fc51 --- /dev/null +++ b/networks/remote/ansible/roles/logzio/handlers/main.yml @@ -0,0 +1,8 @@ +--- + +- name: reload daemon + command: "systemctl daemon-reload" + +- name: restart journalbeat + service: name=journalbeat state=restarted + diff --git a/networks/remote/ansible/roles/logzio/tasks/main.yml b/networks/remote/ansible/roles/logzio/tasks/main.yml new file mode 100644 index 000000000..ab3976f22 --- /dev/null +++ b/networks/remote/ansible/roles/logzio/tasks/main.yml @@ -0,0 +1,27 @@ +--- + +- name: Copy journalbeat binary + copy: src="{{JOURNALBEAT_BINARY}}" dest=/usr/bin/journalbeat mode=0755 + notify: restart journalbeat + +- name: Create folders + file: "path={{item}} state=directory recurse=yes" + with_items: + - /etc/journalbeat + - /etc/pki/tls/certs + - /usr/share/journalbeat + - /var/log/journalbeat + +- name: Copy journalbeat config + template: src=journalbeat.yml.j2 dest=/etc/journalbeat/journalbeat.yml mode=0600 + notify: restart journalbeat + +- name: Get server certificate for Logz.io + get_url: "url=https://raw.githubusercontent.com/logzio/public-certificates/master/COMODORSADomainValidationSecureServerCA.crt force=yes dest=/etc/pki/tls/certs/COMODORSADomainValidationSecureServerCA.crt" + +- name: Copy journalbeat service config + copy: src=journalbeat.service dest=/etc/systemd/system/journalbeat.service + notify: + - reload daemon + - restart journalbeat + diff --git a/networks/remote/ansible/roles/logzio/templates/journalbeat.yml.j2 b/networks/remote/ansible/roles/logzio/templates/journalbeat.yml.j2 new file mode 100644 index 000000000..a421ec8a5 --- /dev/null +++ b/networks/remote/ansible/roles/logzio/templates/journalbeat.yml.j2 @@ -0,0 +1,342 @@ +#======================== Journalbeat Configuration ============================ + +journalbeat: + # What position in journald to seek to at start up + # options: cursor, tail, head (defaults to tail) + #seek_position: tail + + # If seek_position is set to cursor and seeking to cursor fails + # fall back to this method. If set to none will it will exit + # options: tail, head, none (defaults to tail) + #cursor_seek_fallback: tail + + # Store the cursor of the successfully published events + #write_cursor_state: true + + # Path to the file to store the cursor (defaults to ".journalbeat-cursor-state") + #cursor_state_file: .journalbeat-cursor-state + + # How frequently should we save the cursor to disk (defaults to 5s) + #cursor_flush_period: 5s + + # Path to the file to store the queue of events pending (defaults to ".journalbeat-pending-queue") + #pending_queue.file: .journalbeat-pending-queue + + # How frequently should we save the queue to disk (defaults to 1s). + # Pending queue represents the WAL of events queued to be published + # or being published and waiting for acknowledgement. In case of a + # regular restart of journalbeat all the events not yet acknowledged + # will be flushed to disk during the shutdown. + # In case of disaster most probably journalbeat won't get a chance to shutdown + # itself gracefully and this flush period option will serve you as a + # backup creation frequency option. + #pending_queue.flush_period: 1s + + # Lowercase and remove leading underscores, e.g. "_MESSAGE" -> "message" + # (defaults to false) + #clean_field_names: false + + # All journal entries are strings by default. You can try to convert them to numbers. + # (defaults to false) + #convert_to_numbers: false + + # Store all the fields of the Systemd Journal entry under this field + # Can be almost any string suitable to be a field name of an ElasticSearch document. + # Dots can be used to create nested fields. + # Two exceptions: + # - no repeated dots; + # - no trailing dots, e.g. "journal..field_name." will fail + # (defaults to "" hence stores on the upper level of the event) + #move_metadata_to_field: "" + + # Specific units to monitor. + units: ["{{service}}.service"] + + # Specify Journal paths to open. You can pass an array of paths to Systemd Journal paths. + # If you want to open Journal from directory just pass an array consisting of one element + # representing the path. See: https://www.freedesktop.org/software/systemd/man/sd_journal_open.html + # By default this setting is empty thus journalbeat will attempt to find all journal files automatically + #journal_paths: ["/var/log/journal"] + + #default_type: journal + +#================================ General ====================================== + +# The name of the shipper that publishes the network data. It can be used to group +# all the transactions sent by a single shipper in the web interface. +# If this options is not defined, the hostname is used. +#name: journalbeat + +# The tags of the shipper are included in their own field with each +# transaction published. Tags make it easy to group servers by different +# logical properties. +tags: ["{{service}}"] + +# Optional fields that you can specify to add additional information to the +# output. Fields can be scalar values, arrays, dictionaries, or any nested +# combination of these. +fields: + logzio_codec: plain + token: {{LOGZIO_TOKEN}} + +# If this option is set to true, the custom fields are stored as top-level +# fields in the output document instead of being grouped under a fields +# sub-dictionary. Default is false. +fields_under_root: true + +# Internal queue size for single events in processing pipeline +#queue_size: 1000 + +# The internal queue size for bulk events in the processing pipeline. +# Do not modify this value. +#bulk_queue_size: 0 + +# Sets the maximum number of CPUs that can be executing simultaneously. The +# default is the number of logical CPUs available in the system. +#max_procs: + +#================================ Processors =================================== + +# Processors are used to reduce the number of fields in the exported event or to +# enhance the event with external metadata. This section defines a list of +# processors that are applied one by one and the first one receives the initial +# event: +# +# event -> filter1 -> event1 -> filter2 ->event2 ... +# +# The supported processors are drop_fields, drop_event, include_fields, and +# add_cloud_metadata. +# +# For example, you can use the following processors to keep the fields that +# contain CPU load percentages, but remove the fields that contain CPU ticks +# values: +# +processors: +#- include_fields: +# fields: ["cpu"] +- drop_fields: + fields: ["beat.name", "beat.version", "logzio_codec", "SYSLOG_IDENTIFIER", "SYSLOG_FACILITY", "PRIORITY"] +# +# The following example drops the events that have the HTTP response code 200: +# +#processors: +#- drop_event: +# when: +# equals: +# http.code: 200 +# +# The following example enriches each event with metadata from the cloud +# provider about the host machine. It works on EC2, GCE, and DigitalOcean. +# +#processors: +#- add_cloud_metadata: +# + +#================================ Outputs ====================================== + +# Configure what outputs to use when sending the data collected by the beat. +# Multiple outputs may be used. + +#----------------------------- Logstash output --------------------------------- +output.logstash: + # Boolean flag to enable or disable the output module. + enabled: true + + # The Logstash hosts + hosts: ["listener.logz.io:5015"] + + # Number of workers per Logstash host. + #worker: 1 + + # Set gzip compression level. + #compression_level: 3 + + # Optional load balance the events between the Logstash hosts + #loadbalance: true + + # Number of batches to be send asynchronously to logstash while processing + # new batches. + #pipelining: 0 + + # Optional index name. The default index name is set to name of the beat + # in all lowercase. + #index: 'beatname' + + # SOCKS5 proxy server URL + #proxy_url: socks5://user:password@socks5-server:2233 + + # Resolve names locally when using a proxy server. Defaults to false. + #proxy_use_local_resolver: false + + # Enable SSL support. SSL is automatically enabled, if any SSL setting is set. + ssl.enabled: true + + # Configure SSL verification mode. If `none` is configured, all server hosts + # and certificates will be accepted. In this mode, SSL based connections are + # susceptible to man-in-the-middle attacks. Use only for testing. Default is + # `full`. + ssl.verification_mode: full + + # List of supported/valid TLS versions. By default all TLS versions 1.0 up to + # 1.2 are enabled. + #ssl.supported_protocols: [TLSv1.0, TLSv1.1, TLSv1.2] + + # Optional SSL configuration options. SSL is off by default. + # List of root certificates for HTTPS server verifications + ssl.certificate_authorities: ["/etc/pki/tls/certs/COMODORSADomainValidationSecureServerCA.crt"] + + # Certificate for SSL client authentication + #ssl.certificate: "/etc/pki/client/cert.pem" + + # Client Certificate Key + #ssl.key: "/etc/pki/client/cert.key" + + # Optional passphrase for decrypting the Certificate Key. + #ssl.key_passphrase: '' + + # Configure cipher suites to be used for SSL connections + #ssl.cipher_suites: [] + + # Configure curve types for ECDHE based cipher suites + #ssl.curve_types: [] + +#------------------------------- File output ----------------------------------- +#output.file: + # Boolean flag to enable or disable the output module. + #enabled: true + + # Path to the directory where to save the generated files. The option is + # mandatory. + #path: "/tmp/beatname" + + # Name of the generated files. The default is `beatname` and it generates + # files: `beatname`, `beatname.1`, `beatname.2`, etc. + #filename: beatname + + # Maximum size in kilobytes of each file. When this size is reached, and on + # every beatname restart, the files are rotated. The default value is 10240 + # kB. + #rotate_every_kb: 10000 + + # Maximum number of files under path. When this number of files is reached, + # the oldest file is deleted and the rest are shifted from last to first. The + # default is 7 files. + #number_of_files: 7 + + +#----------------------------- Console output --------------------------------- +#output.console: + # Boolean flag to enable or disable the output module. + #enabled: true + + # Pretty print json event + #pretty: false + +#================================= Paths ====================================== + +# The home path for the beatname installation. This is the default base path +# for all other path settings and for miscellaneous files that come with the +# distribution (for example, the sample dashboards). +# If not set by a CLI flag or in the configuration file, the default for the +# home path is the location of the binary. +#path.home: + +# The configuration path for the beatname installation. This is the default +# base path for configuration files, including the main YAML configuration file +# and the Elasticsearch template file. If not set by a CLI flag or in the +# configuration file, the default for the configuration path is the home path. +#path.config: ${path.home} + +# The data path for the beatname installation. This is the default base path +# for all the files in which beatname needs to store its data. If not set by a +# CLI flag or in the configuration file, the default for the data path is a data +# subdirectory inside the home path. +#path.data: ${path.home}/data + +# The logs path for a beatname installation. This is the default location for +# the Beat's log files. If not set by a CLI flag or in the configuration file, +# the default for the logs path is a logs subdirectory inside the home path. +#path.logs: ${path.home}/logs + +#============================== Dashboards ===================================== +# These settings control loading the sample dashboards to the Kibana index. Loading +# the dashboards is disabled by default and can be enabled either by setting the +# options here, or by using the `-setup` CLI flag. +#dashboards.enabled: false + +# The URL from where to download the dashboards archive. By default this URL +# has a value which is computed based on the Beat name and version. For released +# versions, this URL points to the dashboard archive on the artifacts.elastic.co +# website. +#dashboards.url: + +# The directory from where to read the dashboards. It is used instead of the URL +# when it has a value. +#dashboards.directory: + +# The file archive (zip file) from where to read the dashboards. It is used instead +# of the URL when it has a value. +#dashboards.file: + +# If this option is enabled, the snapshot URL is used instead of the default URL. +#dashboards.snapshot: false + +# The URL from where to download the snapshot version of the dashboards. By default +# this has a value which is computed based on the Beat name and version. +#dashboards.snapshot_url + +# In case the archive contains the dashboards from multiple Beats, this lets you +# select which one to load. You can load all the dashboards in the archive by +# setting this to the empty string. +#dashboards.beat: beatname + +# The name of the Kibana index to use for setting the configuration. Default is ".kibana" +#dashboards.kibana_index: .kibana + +# The Elasticsearch index name. This overwrites the index name defined in the +# dashboards and index pattern. Example: testbeat-* +#dashboards.index: + +#================================ Logging ====================================== +# There are three options for the log output: syslog, file, stderr. +# Under Windows systems, the log files are per default sent to the file output, +# under all other system per default to syslog. + +# Sets log level. The default log level is info. +# Available log levels are: critical, error, warning, info, debug +#logging.level: info + +# Enable debug output for selected components. To enable all selectors use ["*"] +# Other available selectors are "beat", "publish", "service" +# Multiple selectors can be chained. +#logging.selectors: [ ] + +# Send all logging output to syslog. The default is false. +#logging.to_syslog: true + +# If enabled, beatname periodically logs its internal metrics that have changed +# in the last period. For each metric that changed, the delta from the value at +# the beginning of the period is logged. Also, the total values for +# all non-zero internal metrics are logged on shutdown. The default is true. +#logging.metrics.enabled: true + +# The period after which to log the internal metrics. The default is 30s. +#logging.metrics.period: 30s + +# Logging to rotating files files. Set logging.to_files to false to disable logging to +# files. +logging.to_files: true +logging.files: + # Configure the path where the logs are written. The default is the logs directory + # under the home path (the binary location). + #path: /var/log/beatname + + # The name of the files where the logs are written to. + #name: beatname + + # Configure log file size limit. If limit is reached, log file will be + # automatically rotated + #rotateeverybytes: 10485760 # = 10MB + + # Number of rotated log files to keep. Oldest files will be deleted first. + #keepfiles: 7 diff --git a/networks/remote/ansible/roles/setup-validators/defaults/main.yml b/networks/remote/ansible/roles/setup-validators/defaults/main.yml new file mode 100644 index 000000000..a535d201d --- /dev/null +++ b/networks/remote/ansible/roles/setup-validators/defaults/main.yml @@ -0,0 +1,4 @@ +--- + +TESTNET_NAME: remotenet + diff --git a/networks/remote/ansible/roles/setup-validators/tasks/main.yml b/networks/remote/ansible/roles/setup-validators/tasks/main.yml new file mode 100644 index 000000000..8023a67b4 --- /dev/null +++ b/networks/remote/ansible/roles/setup-validators/tasks/main.yml @@ -0,0 +1,50 @@ +--- + +- name: Copy binary + copy: + src: "{{BINARY}}" + dest: /usr/bin + mode: 0755 + +- name: Get node ID + command: "cat /etc/gaiad-nodeid" + changed_when: false + register: nodeid + +- name: Create initial transaction + command: "/usr/bin/gaiad init gen-tx --name=node{{nodeid.stdout_lines[0]}}" + become: yes + become_user: gaiad + args: + creates: /home/gaiad/.gaiad/config/gentx + +- name: Find gentx file + command: "ls /home/gaiad/.gaiad/config/gentx" + changed_when: false + register: gentxfile + +- name: Clear local gen-tx list + file: path=files/ state=absent + connection: local + run_once: yes + +- name: Get gen-tx + fetch: + dest: files/ + src: "/home/gaiad/.gaiad/config/gentx/{{gentxfile.stdout_lines[0]}}" + flat: yes + +- name: Copy generated transactions to all nodes + copy: + src: files/ + dest: /home/gaiad/.gaiad/config/gentx/ + become: yes + become_user: gaiad + +- name: Generate genesis.json + command: "/usr/bin/gaiad init --gen-txs --name=node{{nodeid.stdout_lines[0]}} --chain-id={{TESTNET_NAME}}" + become: yes + become_user: gaiad + args: + creates: /home/gaiad/.gaiad/config/genesis.json + diff --git a/networks/remote/ansible/roles/start/tasks/main.yml b/networks/remote/ansible/roles/start/tasks/main.yml new file mode 100644 index 000000000..6bc611c91 --- /dev/null +++ b/networks/remote/ansible/roles/start/tasks/main.yml @@ -0,0 +1,5 @@ +--- + +- name: start service + service: "name={{service}} state=started" + diff --git a/networks/remote/ansible/roles/stop/tasks/main.yml b/networks/remote/ansible/roles/stop/tasks/main.yml new file mode 100644 index 000000000..7db356f22 --- /dev/null +++ b/networks/remote/ansible/roles/stop/tasks/main.yml @@ -0,0 +1,5 @@ +--- + +- name: stop service + service: "name={{service}} state=stopped" + diff --git a/networks/remote/ansible/setup-validators.yml b/networks/remote/ansible/setup-validators.yml new file mode 100644 index 000000000..f5010777b --- /dev/null +++ b/networks/remote/ansible/setup-validators.yml @@ -0,0 +1,9 @@ +--- + +- hosts: all + user: root + any_errors_fatal: true + gather_facts: no + roles: + - setup-validators + diff --git a/networks/remote/ansible/start.yml b/networks/remote/ansible/start.yml new file mode 100644 index 000000000..e9c84ce9e --- /dev/null +++ b/networks/remote/ansible/start.yml @@ -0,0 +1,11 @@ +--- + +- hosts: all + user: root + any_errors_fatal: true + gather_facts: no + vars: + - service: gaiad + roles: + - start + diff --git a/networks/remote/ansible/status.yml b/networks/remote/ansible/status.yml new file mode 100644 index 000000000..fffba41fc --- /dev/null +++ b/networks/remote/ansible/status.yml @@ -0,0 +1,17 @@ +--- + +- hosts: all + connection: local + any_errors_fatal: true + gather_facts: no + + tasks: + - name: Gather status + uri: + body_format: json + url: "http://{{inventory_hostname}}:46657/status" + register: status + + - name: Print status + debug: var=status.json.result + diff --git a/networks/remote/ansible/stop.yml b/networks/remote/ansible/stop.yml new file mode 100644 index 000000000..d41caa43f --- /dev/null +++ b/networks/remote/ansible/stop.yml @@ -0,0 +1,11 @@ +--- + +- hosts: all + user: root + any_errors_fatal: true + gather_facts: no + vars: + - service: gaiad + roles: + - stop + diff --git a/networks/remote/terraform/.gitignore b/networks/remote/terraform/.gitignore new file mode 100644 index 000000000..0cc2d499a --- /dev/null +++ b/networks/remote/terraform/.gitignore @@ -0,0 +1,4 @@ +.terraform +terraform.tfstate +terraform.tfstate.backup +terraform.tfstate.d diff --git a/networks/remote/terraform/README.rst b/networks/remote/terraform/README.rst new file mode 100644 index 000000000..fdccb1a6a --- /dev/null +++ b/networks/remote/terraform/README.rst @@ -0,0 +1,53 @@ +Using Terraform +=============== + +This is a `Terraform `__ configuration that sets up DigitalOcean droplets. + +Prerequisites +------------- + +- Install `HashiCorp Terraform `__ on a linux machine. +- Create a `DigitalOcean API token `__ with read and write capability. +- Create SSH keys + +Build +----- + +:: + + export DO_API_TOKEN="abcdef01234567890abcdef01234567890" + export TESTNET_NAME="remotenet" + export SSH_PUBLIC_FILE="$HOME/.ssh/id_rsa.pub" + export SSH_PRIVATE_FILE="$HOME/.ssh/id_rsa" + + terraform init + terraform apply -var DO_API_TOKEN="$DO_API_TOKEN" -var SSH_PUBLIC_FILE="$SSH_PUBLIC_FILE" -var SSH_PRIVATE_FILE="$SSH_PRIVATE_FILE" + +At the end you will get a list of IP addresses that belongs to your new droplets. + +Destroy +------- + +Run the below: + +:: + + terraform destroy -var DO_API_TOKEN="$DO_API_TOKEN" -var SSH_PUBLIC_FILE="$SSH_PUBLIC_FILE" -var SSH_PRIVATE_FILE="$SSH_PRIVATE_FILE" + +Good to know +------------ + +The DigitalOcean API was not very reliable for me. If you find that terraform fails to install a specific server (for example cluster[2]), check +the regions variable and remove data center names that you find unreliable. The variable is at cluster/variables.tf + +Example: + +:: + + variable "regions" { + description = "Regions to launch in" + type = "list" + default = ["TOR1", "LON1"] + } + + diff --git a/networks/remote/terraform/cluster/main.tf b/networks/remote/terraform/cluster/main.tf new file mode 100644 index 000000000..110f92a00 --- /dev/null +++ b/networks/remote/terraform/cluster/main.tf @@ -0,0 +1,46 @@ +resource "digitalocean_tag" "cluster" { + name = "${var.name}" +} + +resource "digitalocean_ssh_key" "cluster" { + name = "${var.name}" + public_key = "${file(var.ssh_public_file)}" +} + +resource "digitalocean_droplet" "cluster" { + name = "${var.name}-node${count.index}" + image = "centos-7-x64" + size = "${var.instance_size}" + region = "${element(var.regions, count.index)}" + ssh_keys = ["${digitalocean_ssh_key.cluster.id}"] + count = "${var.servers}" + tags = ["${digitalocean_tag.cluster.id}"] + + lifecycle = { + prevent_destroy = false + } + + connection { + private_key = "${file(var.ssh_private_file)}" + timeout = "30s" + } + + provisioner "file" { + source = "files/terraform.sh" + destination = "/tmp/terraform.sh" + } + + provisioner "file" { + source = "files/gaiad.service" + destination = "/etc/systemd/system/gaiad.service" + } + + provisioner "remote-exec" { + inline = [ + "chmod +x /tmp/terraform.sh", + "/tmp/terraform.sh ${var.name} ${count.index}", + ] + } + +} + diff --git a/networks/remote/terraform/cluster/outputs.tf b/networks/remote/terraform/cluster/outputs.tf new file mode 100644 index 000000000..78291b6a9 --- /dev/null +++ b/networks/remote/terraform/cluster/outputs.tf @@ -0,0 +1,15 @@ +// The cluster name +output "name" { + value = "${var.name}" +} + +// The list of cluster instance IDs +output "instances" { + value = ["${digitalocean_droplet.cluster.*.id}"] +} + +// The list of cluster instance public IPs +output "public_ips" { + value = ["${digitalocean_droplet.cluster.*.ipv4_address}"] +} + diff --git a/networks/remote/terraform/cluster/variables.tf b/networks/remote/terraform/cluster/variables.tf new file mode 100644 index 000000000..e2654b11d --- /dev/null +++ b/networks/remote/terraform/cluster/variables.tf @@ -0,0 +1,30 @@ +variable "name" { + description = "The cluster name, e.g remotenet" +} + +variable "regions" { + description = "Regions to launch in" + type = "list" + default = ["AMS2", "TOR1", "LON1", "NYC3", "SFO2", "SGP1", "FRA1"] +} + +variable "ssh_private_file" { + description = "SSH private key filename to use to connect to the nodes" + type = "string" +} + +variable "ssh_public_file" { + description = "SSH public key filename to copy to the nodes" + type = "string" +} + +variable "instance_size" { + description = "The instance size to use" + default = "2gb" +} + +variable "servers" { + description = "Desired instance count" + default = 4 +} + diff --git a/networks/remote/terraform/files/gaiad.service b/networks/remote/terraform/files/gaiad.service new file mode 100644 index 000000000..697166567 --- /dev/null +++ b/networks/remote/terraform/files/gaiad.service @@ -0,0 +1,17 @@ +[Unit] +Description=gaiad +Requires=network-online.target +After=network-online.target + +[Service] +Restart=on-failure +User=gaiad +Group=gaiad +PermissionsStartOnly=true +ExecStart=/usr/bin/gaiad start +ExecReload=/bin/kill -HUP $MAINPID +KillSignal=SIGTERM + +[Install] +WantedBy=multi-user.target + diff --git a/networks/remote/terraform/files/terraform.sh b/networks/remote/terraform/files/terraform.sh new file mode 100644 index 000000000..39d89ea82 --- /dev/null +++ b/networks/remote/terraform/files/terraform.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# Script to initialize a testnet settings on a server + +#Usage: terraform.sh + +#Add gaiad node number for remote identification +echo "$2" > /etc/gaiad-nodeid + +#Create gaiad user +useradd -m -s /bin/bash gaiad +#cp -r /root/.ssh /home/gaiad/.ssh +#chown -R gaiad.gaiad /home/gaiad/.ssh +#chmod -R 700 /home/gaiad/.ssh + +#Reload services to enable the gaiad service (note that the gaiad binary is not available yet) +systemctl daemon-reload +systemctl enable gaiad + + diff --git a/networks/remote/terraform/main.tf b/networks/remote/terraform/main.tf new file mode 100644 index 000000000..fb78a3785 --- /dev/null +++ b/networks/remote/terraform/main.tf @@ -0,0 +1,43 @@ +#Terraform Configuration + +variable "DO_API_TOKEN" { + description = "DigitalOcean Access Token" +} + +variable "TESTNET_NAME" { + description = "Name of the testnet" + default = "remotenet" +} + +variable "SSH_PRIVATE_FILE" { + description = "SSH private key file to be used to connect to the nodes" + type = "string" +} + +variable "SSH_PUBLIC_FILE" { + description = "SSH public key file to be used on the nodes" + type = "string" +} + +variable "SERVERS" { + description = "Number of nodes in testnet" + default = "4" +} + +provider "digitalocean" { + token = "${var.DO_API_TOKEN}" +} + +module "cluster" { + source = "./cluster" + name = "${var.TESTNET_NAME}" + ssh_private_file = "${var.SSH_PRIVATE_FILE}" + ssh_public_file = "${var.SSH_PUBLIC_FILE}" + servers = "${var.SERVERS}" +} + + +output "public_ips" { + value = "${module.cluster.public_ips}" +} + diff --git a/server/tm_cmds.go b/server/tm_cmds.go index d581ca5f7..fcec57f09 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -1,14 +1,13 @@ package server import ( - "encoding/hex" "fmt" - "strings" "github.com/cosmos/cosmos-sdk/wire" "github.com/spf13/cobra" "github.com/spf13/viper" + sdk "github.com/cosmos/cosmos-sdk/types" tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" "github.com/tendermint/tendermint/p2p" pvm "github.com/tendermint/tendermint/types/priv_validator" @@ -41,21 +40,24 @@ func ShowValidatorCmd(ctx *Context) *cobra.Command { cfg := ctx.Config privValidator := pvm.LoadOrGenFilePV(cfg.PrivValidatorFile()) - pubKey := privValidator.PubKey + valPubKey := privValidator.PubKey if viper.GetBool(flagJSON) { cdc := wire.NewCodec() wire.RegisterCrypto(cdc) - pubKeyJSONBytes, err := cdc.MarshalJSON(pubKey) + pubKeyJSONBytes, err := cdc.MarshalJSON(valPubKey) if err != nil { return err } fmt.Println(string(pubKeyJSONBytes)) return nil } - pubKeyHex := strings.ToUpper(hex.EncodeToString(pubKey.Bytes())) - fmt.Println(pubKeyHex) + addr, err := sdk.Bech32CosmosifyValPub(valPubKey) + if err != nil { + return err + } + fmt.Println(addr) return nil }, } diff --git a/store/cachekvstore.go b/store/cachekvstore.go index 772b59305..109bbfc75 100644 --- a/store/cachekvstore.go +++ b/store/cachekvstore.go @@ -5,7 +5,6 @@ import ( "sort" "sync" - sdk "github.com/cosmos/cosmos-sdk/types" cmn "github.com/tendermint/tmlibs/common" ) @@ -134,16 +133,6 @@ func (ci *cacheKVStore) ReverseIterator(start, end []byte) Iterator { return ci.iterator(start, end, false) } -// Implements KVStore. -func (ci *cacheKVStore) SubspaceIterator(prefix []byte) Iterator { - return ci.iterator(prefix, sdk.PrefixEndBytes(prefix), true) -} - -// Implements KVStore. -func (ci *cacheKVStore) ReverseSubspaceIterator(prefix []byte) Iterator { - return ci.iterator(prefix, sdk.PrefixEndBytes(prefix), false) -} - func (ci *cacheKVStore) iterator(start, end []byte, ascending bool) Iterator { var parent, cache Iterator if ascending { diff --git a/store/dbstoreadapter.go b/store/dbstoreadapter.go index 7a299471f..58c9e1b29 100644 --- a/store/dbstoreadapter.go +++ b/store/dbstoreadapter.go @@ -19,13 +19,5 @@ func (dsa dbStoreAdapter) CacheWrap() CacheWrap { return NewCacheKVStore(dsa) } -func (dsa dbStoreAdapter) SubspaceIterator(prefix []byte) Iterator { - return dsa.Iterator(prefix, sdk.PrefixEndBytes(prefix)) -} - -func (dsa dbStoreAdapter) ReverseSubspaceIterator(prefix []byte) Iterator { - return dsa.ReverseIterator(prefix, sdk.PrefixEndBytes(prefix)) -} - // dbm.DB implements KVStore so we can CacheKVStore it. var _ KVStore = dbStoreAdapter{dbm.DB(nil)} diff --git a/store/gaskvstore.go b/store/gaskvstore.go index 9f50f3444..db65921da 100644 --- a/store/gaskvstore.go +++ b/store/gaskvstore.go @@ -75,16 +75,6 @@ func (gi *gasKVStore) ReverseIterator(start, end []byte) sdk.Iterator { return gi.iterator(start, end, false) } -// Implements KVStore. -func (gi *gasKVStore) SubspaceIterator(prefix []byte) sdk.Iterator { - return gi.iterator(prefix, sdk.PrefixEndBytes(prefix), true) -} - -// Implements KVStore. -func (gi *gasKVStore) ReverseSubspaceIterator(prefix []byte) sdk.Iterator { - return gi.iterator(prefix, sdk.PrefixEndBytes(prefix), false) -} - // Implements KVStore. func (gi *gasKVStore) CacheWrap() sdk.CacheWrap { panic("you cannot CacheWrap a GasKVStore") diff --git a/store/iavlstore.go b/store/iavlstore.go index 5399b3d5c..2b7914c4e 100644 --- a/store/iavlstore.go +++ b/store/iavlstore.go @@ -125,16 +125,6 @@ func (st *iavlStore) ReverseIterator(start, end []byte) Iterator { return newIAVLIterator(st.tree.Tree(), start, end, false) } -// Implements KVStore. -func (st *iavlStore) SubspaceIterator(prefix []byte) Iterator { - return st.Iterator(prefix, sdk.PrefixEndBytes(prefix)) -} - -// Implements KVStore. -func (st *iavlStore) ReverseSubspaceIterator(prefix []byte) Iterator { - return st.ReverseIterator(prefix, sdk.PrefixEndBytes(prefix)) -} - // Query implements ABCI interface, allows queries // // by default we will return from (latest height -1), @@ -180,7 +170,7 @@ func (st *iavlStore) Query(req abci.RequestQuery) (res abci.ResponseQuery) { subspace := req.Data res.Key = subspace var KVs []KVPair - iterator := st.SubspaceIterator(subspace) + iterator := sdk.KVStorePrefixIterator(st, subspace) for ; iterator.Valid(); iterator.Next() { KVs = append(KVs, KVPair{iterator.Key(), iterator.Value()}) } diff --git a/store/iavlstore_test.go b/store/iavlstore_test.go index 32bc1ebe0..e324758c5 100644 --- a/store/iavlstore_test.go +++ b/store/iavlstore_test.go @@ -157,7 +157,7 @@ func TestIAVLSubspaceIterator(t *testing.T) { i := 0 - iter := iavlStore.SubspaceIterator([]byte("test")) + iter := sdk.KVStorePrefixIterator(iavlStore, []byte("test")) expected := []string{"test1", "test2", "test3"} for i = 0; iter.Valid(); iter.Next() { expectedKey := expected[i] @@ -168,7 +168,7 @@ func TestIAVLSubspaceIterator(t *testing.T) { } assert.Equal(t, len(expected), i) - iter = iavlStore.SubspaceIterator([]byte{byte(55), byte(255), byte(255)}) + iter = sdk.KVStorePrefixIterator(iavlStore, []byte{byte(55), byte(255), byte(255)}) expected2 := [][]byte{ []byte{byte(55), byte(255), byte(255), byte(0)}, []byte{byte(55), byte(255), byte(255), byte(1)}, @@ -183,7 +183,7 @@ func TestIAVLSubspaceIterator(t *testing.T) { } assert.Equal(t, len(expected), i) - iter = iavlStore.SubspaceIterator([]byte{byte(255), byte(255)}) + iter = sdk.KVStorePrefixIterator(iavlStore, []byte{byte(255), byte(255)}) expected2 = [][]byte{ []byte{byte(255), byte(255), byte(0)}, []byte{byte(255), byte(255), byte(1)}, @@ -216,7 +216,7 @@ func TestIAVLReverseSubspaceIterator(t *testing.T) { i := 0 - iter := iavlStore.ReverseSubspaceIterator([]byte("test")) + iter := sdk.KVStoreReversePrefixIterator(iavlStore, []byte("test")) expected := []string{"test3", "test2", "test1"} for i = 0; iter.Valid(); iter.Next() { expectedKey := expected[i] @@ -227,7 +227,7 @@ func TestIAVLReverseSubspaceIterator(t *testing.T) { } assert.Equal(t, len(expected), i) - iter = iavlStore.ReverseSubspaceIterator([]byte{byte(55), byte(255), byte(255)}) + iter = sdk.KVStoreReversePrefixIterator(iavlStore, []byte{byte(55), byte(255), byte(255)}) expected2 := [][]byte{ []byte{byte(55), byte(255), byte(255), byte(255)}, []byte{byte(55), byte(255), byte(255), byte(1)}, @@ -242,7 +242,7 @@ func TestIAVLReverseSubspaceIterator(t *testing.T) { } assert.Equal(t, len(expected), i) - iter = iavlStore.ReverseSubspaceIterator([]byte{byte(255), byte(255)}) + iter = sdk.KVStoreReversePrefixIterator(iavlStore, []byte{byte(255), byte(255)}) expected2 = [][]byte{ []byte{byte(255), byte(255), byte(255)}, []byte{byte(255), byte(255), byte(1)}, diff --git a/types/account.go b/types/account.go index 74cd87f38..b593dfb53 100644 --- a/types/account.go +++ b/types/account.go @@ -3,16 +3,46 @@ package types import ( "encoding/hex" "errors" + "fmt" + bech32cosmos "github.com/cosmos/bech32cosmos/go" crypto "github.com/tendermint/go-crypto" cmn "github.com/tendermint/tmlibs/common" ) -// Address in go-crypto style +//Address is a go crypto-style Address type Address = cmn.HexBytes +// Bech32 prefixes +const ( + Bech32PrefixAccAddr = "cosmosaccaddr" + Bech32PrefixAccPub = "cosmosaccpub" + Bech32PrefixValAddr = "cosmosvaladdr" + Bech32PrefixValPub = "cosmosvalpub" +) + +// Bech32CosmosifyAcc takes Address and returns the Bech32Cosmos encoded string +func Bech32CosmosifyAcc(addr Address) (string, error) { + return bech32cosmos.ConvertAndEncode(Bech32PrefixAccAddr, addr.Bytes()) +} + +// Bech32CosmosifyAccPub takes AccountPubKey and returns the Bech32Cosmos encoded string +func Bech32CosmosifyAccPub(pub crypto.PubKey) (string, error) { + return bech32cosmos.ConvertAndEncode(Bech32PrefixAccPub, pub.Bytes()) +} + +// Bech32CosmosifyVal returns the Bech32Cosmos encoded string for a validator address +func Bech32CosmosifyVal(addr Address) (string, error) { + return bech32cosmos.ConvertAndEncode(Bech32PrefixValAddr, addr.Bytes()) +} + +// Bech32CosmosifyValPub returns the Bech32Cosmos encoded string for a validator pubkey +func Bech32CosmosifyValPub(pub crypto.PubKey) (string, error) { + return bech32cosmos.ConvertAndEncode(Bech32PrefixValPub, pub.Bytes()) +} + // create an Address from a string -func GetAddress(address string) (addr Address, err error) { +func GetAccAddressHex(address string) (addr Address, err error) { if len(address) == 0 { return addr, errors.New("must use provide address") } @@ -23,30 +53,63 @@ func GetAddress(address string) (addr Address, err error) { return Address(bz), nil } -// Account is a standard account using a sequence number for replay protection -// and a pubkey for authentication. -type Account interface { - GetAddress() Address - SetAddress(Address) error // errors if already set. - - GetPubKey() crypto.PubKey // can return nil. - SetPubKey(crypto.PubKey) error - - GetSequence() int64 - SetSequence(int64) error - - GetCoins() Coins - SetCoins(Coins) error +// create an Address from a string +func GetAccAddressBech32Cosmos(address string) (addr Address, err error) { + bz, err := getFromBech32Cosmos(address, Bech32PrefixAccAddr) + if err != nil { + return nil, err + } + return Address(bz), nil } -// AccountMapper stores and retrieves accounts from stores -// retrieved from the context. -type AccountMapper interface { - NewAccountWithAddress(ctx Context, addr Address) Account - GetAccount(ctx Context, addr Address) Account - SetAccount(ctx Context, acc Account) - IterateAccounts(ctx Context, process func(Account) (stop bool)) +// create an Address from a hex string +func GetValAddressHex(address string) (addr Address, err error) { + if len(address) == 0 { + return addr, errors.New("must use provide address") + } + bz, err := hex.DecodeString(address) + if err != nil { + return nil, err + } + return Address(bz), nil } -// AccountDecoder unmarshals account bytes -type AccountDecoder func(accountBytes []byte) (Account, error) +// create an Address from a bech32cosmos string +func GetValAddressBech32Cosmos(address string) (addr Address, err error) { + bz, err := getFromBech32Cosmos(address, Bech32PrefixValAddr) + if err != nil { + return nil, err + } + return Address(bz), nil +} + +//Decode a validator publickey into a public key +func GetValPubKeyBech32Cosmos(pubkey string) (pk crypto.PubKey, err error) { + bz, err := getFromBech32Cosmos(pubkey, Bech32PrefixValPub) + if err != nil { + return nil, err + } + + pk, err = crypto.PubKeyFromBytes(bz) + if err != nil { + return nil, err + } + + return pk, nil +} + +func getFromBech32Cosmos(bech32, prefix string) ([]byte, error) { + if len(bech32) == 0 { + return nil, errors.New("must provide non-empty string") + } + hrp, bz, err := bech32cosmos.DecodeAndConvert(bech32) + if err != nil { + return nil, err + } + + if hrp != prefix { + return nil, fmt.Errorf("Invalid bech32 prefix. Expected %s, Got %s", prefix, hrp) + } + + return bz, nil +} diff --git a/types/handler.go b/types/handler.go index 679a3b1a7..129f42647 100644 --- a/types/handler.go +++ b/types/handler.go @@ -3,8 +3,5 @@ package types // core function variable which application runs for transactions type Handler func(ctx Context, msg Msg) Result -// core function variable which application runs to handle fees -type FeeHandler func(ctx Context, tx Tx, fee Coins) - // If newCtx.IsZero(), ctx is used instead. type AnteHandler func(ctx Context, tx Tx) (newCtx Context, result Result, abort bool) diff --git a/types/rational.go b/types/rational.go index 7e0a09107..0709a350f 100644 --- a/types/rational.go +++ b/types/rational.go @@ -5,6 +5,7 @@ import ( "math/big" "strconv" "strings" + "testing" ) // "that's one big rat!" @@ -80,17 +81,17 @@ func NewRatFromDecimal(decimalStr string) (f Rat, err Error) { } //nolint -func (r Rat) Num() int64 { return r.Rat.Num().Int64() } // Num - return the numerator -func (r Rat) Denom() int64 { return r.Rat.Denom().Int64() } // Denom - return the denominator -func (r Rat) IsZero() bool { return r.Num() == 0 } // IsZero - Is the Rat equal to zero -func (r Rat) Equal(r2 Rat) bool { return (&(r.Rat)).Cmp(&(r2.Rat)) == 0 } // Equal - rationals are equal -func (r Rat) GT(r2 Rat) bool { return (&(r.Rat)).Cmp(&(r2.Rat)) == 1 } // Equal - rationals are equal -func (r Rat) LT(r2 Rat) bool { return (&(r.Rat)).Cmp(&(r2.Rat)) == -1 } // Equal - rationals are equal +func (r Rat) Num() int64 { return r.Rat.Num().Int64() } // Num - return the numerator +func (r Rat) Denom() int64 { return r.Rat.Denom().Int64() } // Denom - return the denominator +func (r Rat) IsZero() bool { return r.Num() == 0 } // IsZero - Is the Rat equal to zero +func (r Rat) Equal(r2 Rat) bool { return (&(r.Rat)).Cmp(&(r2.Rat)) == 0 } +func (r Rat) GT(r2 Rat) bool { return (&(r.Rat)).Cmp(&(r2.Rat)) == 1 } // greater than +func (r Rat) LT(r2 Rat) bool { return (&(r.Rat)).Cmp(&(r2.Rat)) == -1 } // less than func (r Rat) Mul(r2 Rat) Rat { return Rat{*new(big.Rat).Mul(&(r.Rat), &(r2.Rat))} } // Mul - multiplication func (r Rat) Quo(r2 Rat) Rat { return Rat{*new(big.Rat).Quo(&(r.Rat), &(r2.Rat))} } // Quo - quotient func (r Rat) Add(r2 Rat) Rat { return Rat{*new(big.Rat).Add(&(r.Rat), &(r2.Rat))} } // Add - addition func (r Rat) Sub(r2 Rat) Rat { return Rat{*new(big.Rat).Sub(&(r.Rat), &(r2.Rat))} } // Sub - subtraction -func (r Rat) String() string { return fmt.Sprintf("%v/%v", r.Num(), r.Denom()) } // Sub - subtraction +func (r Rat) String() string { return fmt.Sprintf("%v/%v", r.Num(), r.Denom()) } var ( zero = big.NewInt(0) @@ -168,3 +169,25 @@ func (r *Rat) UnmarshalAmino(text string) (err error) { r.Rat = *tempRat return nil } + +//___________________________________________________________________________________ +// helpers + +// test if two rat arrays are the equal +func RatsEqual(r1s, r2s []Rat) bool { + if len(r1s) != len(r2s) { + return false + } + + for i, r1 := range r1s { + if !r1.Equal(r2s[i]) { + return false + } + } + return true +} + +// intended to be used with require/assert: require.True(RatEq(...)) +func RatEq(t *testing.T, exp, got Rat) (*testing.T, bool, string, Rat, Rat) { + return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp, got +} diff --git a/types/rational_test.go b/types/rational_test.go index e59545bfc..30abb1a51 100644 --- a/types/rational_test.go +++ b/types/rational_test.go @@ -276,3 +276,24 @@ func TestEmbeddedStructSerializationGoWire(t *testing.T) { assert.Equal(t, obj.Field2, obj2.Field2) assert.True(t, obj.Field3.Equal(obj2.Field3), "original: %v, unmarshalled: %v", obj, obj2) } + +func TestRatsEqual(t *testing.T) { + tests := []struct { + r1s, r2s []Rat + eq bool + }{ + {[]Rat{NewRat(0)}, []Rat{NewRat(0)}, true}, + {[]Rat{NewRat(0)}, []Rat{NewRat(1)}, false}, + {[]Rat{NewRat(0)}, []Rat{}, false}, + {[]Rat{NewRat(0), NewRat(1)}, []Rat{NewRat(0), NewRat(1)}, true}, + {[]Rat{NewRat(1), NewRat(0)}, []Rat{NewRat(1), NewRat(0)}, true}, + {[]Rat{NewRat(1), NewRat(0)}, []Rat{NewRat(0), NewRat(1)}, false}, + {[]Rat{NewRat(1), NewRat(0)}, []Rat{NewRat(1)}, false}, + } + + for _, tc := range tests { + assert.Equal(t, tc.eq, RatsEqual(tc.r1s, tc.r2s)) + assert.Equal(t, tc.eq, RatsEqual(tc.r2s, tc.r1s)) + } + +} diff --git a/types/signature.go b/types/signature.go deleted file mode 100644 index 5bca2f606..000000000 --- a/types/signature.go +++ /dev/null @@ -1,10 +0,0 @@ -package types - -import crypto "github.com/tendermint/go-crypto" - -// Standard Signature -type StdSignature struct { - crypto.PubKey `json:"pub_key"` // optional - crypto.Signature `json:"signature"` - Sequence int64 `json:"sequence"` -} diff --git a/types/stake.go b/types/stake.go new file mode 100644 index 000000000..6a1a3a95f --- /dev/null +++ b/types/stake.go @@ -0,0 +1,79 @@ +package types + +import ( + abci "github.com/tendermint/abci/types" + "github.com/tendermint/go-crypto" +) + +// status of a validator +type BondStatus byte + +// nolint +const ( + Unbonded BondStatus = 0x00 + Unbonding BondStatus = 0x01 + Bonded BondStatus = 0x02 +) + +//BondStatusToString for pretty prints of Bond Status +func BondStatusToString(b BondStatus) string { + switch b { + case 0x00: + return "Unbonded" + case 0x01: + return "Unbonding" + case 0x02: + return "Bonded" + default: + return "" + } +} + +// validator for a delegated proof of stake system +type Validator interface { + GetStatus() BondStatus // status of the validator + GetOwner() Address // owner address to receive/return validators coins + GetPubKey() crypto.PubKey // validation pubkey + GetPower() Rat // validation power + GetBondHeight() int64 // height in which the validator became active +} + +// validator which fulfills abci validator interface for use in Tendermint +func ABCIValidator(v Validator) abci.Validator { + return abci.Validator{ + PubKey: v.GetPubKey().Bytes(), + Power: v.GetPower().Evaluate(), + } +} + +// properties for the set of all validators +type ValidatorSet interface { + // iterate through validator by owner-address, execute func for each validator + IterateValidators(Context, + func(index int64, validator Validator) (stop bool)) + + // iterate through bonded validator by pubkey-address, execute func for each validator + IterateValidatorsBonded(Context, + func(index int64, validator Validator) (stop bool)) + + Validator(Context, Address) Validator // get a particular validator by owner address + TotalPower(Context) Rat // total power of the validator set +} + +//_______________________________________________________________________________ + +// delegation bond for a delegated proof of stake system +type Delegation interface { + GetDelegator() Address // delegator address for the bond + GetValidator() Address // validator owner address for the bond + GetBondShares() Rat // amount of validator's shares +} + +// properties for the set of all delegations for a particular +type DelegationSet interface { + + // iterate through all delegations from one delegator by validator-address, + // execute func for each validator + IterateDelegators(Context, delegator Address, + fn func(index int64, delegation Delegation) (stop bool)) +} diff --git a/types/store.go b/types/store.go index abf02ec07..2bd34bebd 100644 --- a/types/store.go +++ b/types/store.go @@ -84,7 +84,7 @@ type CommitMultiStore interface { LoadVersion(ver int64) error } -//---------------------------------------- +//---------subsp------------------------------- // KVStore // KVStore is a simple interface to get/set data @@ -113,25 +113,26 @@ type KVStore interface { // CONTRACT: No writes may happen within a domain while an iterator exists over it. ReverseIterator(start, end []byte) Iterator - // Iterator over all the keys with a certain prefix in ascending order. - // CONTRACT: No writes may happen within a domain while an iterator exists over it. - SubspaceIterator(prefix []byte) Iterator - - // Iterator over all the keys with a certain prefix in descending order. - // CONTRACT: No writes may happen within a domain while an iterator exists over it. - ReverseSubspaceIterator(prefix []byte) Iterator - // TODO Not yet implemented. // CreateSubKVStore(key *storeKey) (KVStore, error) // TODO Not yet implemented. // GetSubKVStore(key *storeKey) KVStore - } // Alias iterator to db's Iterator for convenience. type Iterator = dbm.Iterator +// Iterator over all the keys with a certain prefix in ascending order +func KVStorePrefixIterator(kvs KVStore, prefix []byte) Iterator { + return kvs.Iterator(prefix, PrefixEndBytes(prefix)) +} + +// Iterator over all the keys with a certain prefix in descending order. +func KVStoreReversePrefixIterator(kvs KVStore, prefix []byte) Iterator { + return kvs.ReverseIterator(prefix, PrefixEndBytes(prefix)) +} + // CacheKVStore cache-wraps a KVStore. After calling .Write() on // the CacheKVStore, all previously created CacheKVStores on the // object expire. diff --git a/types/tx_msg.go b/types/tx_msg.go index e17d152a5..240db3106 100644 --- a/types/tx_msg.go +++ b/types/tx_msg.go @@ -31,128 +31,11 @@ type Tx interface { // Gets the Msg. GetMsg() Msg - - // Signatures returns the signature of signers who signed the Msg. - // CONTRACT: Length returned is same as length of - // pubkeys returned from MsgKeySigners, and the order - // matches. - // CONTRACT: If the signature is missing (ie the Msg is - // invalid), then the corresponding signature is - // .Empty(). - GetSignatures() []StdSignature -} - -var _ Tx = (*StdTx)(nil) - -// StdTx is a standard way to wrap a Msg with Fee and Signatures. -// NOTE: the first signature is the FeePayer (Signatures must not be nil). -type StdTx struct { - Msg `json:"msg"` - Fee StdFee `json:"fee"` - Signatures []StdSignature `json:"signatures"` -} - -func NewStdTx(msg Msg, fee StdFee, sigs []StdSignature) StdTx { - return StdTx{ - Msg: msg, - Fee: fee, - Signatures: sigs, - } -} - -//nolint -func (tx StdTx) GetMsg() Msg { return tx.Msg } -func (tx StdTx) GetSignatures() []StdSignature { return tx.Signatures } - -// FeePayer returns the address responsible for paying the fees -// for the transactions. It's the first address returned by msg.GetSigners(). -// If GetSigners() is empty, this panics. -func FeePayer(tx Tx) Address { - return tx.GetMsg().GetSigners()[0] } //__________________________________________________________ -// StdFee includes the amount of coins paid in fees and the maximum -// gas to be used by the transaction. The ratio yields an effective "gasprice", -// which must be above some miminum to be accepted into the mempool. -type StdFee struct { - Amount Coins `json:"amount"` - Gas int64 `json:"gas"` -} - -func NewStdFee(gas int64, amount ...Coin) StdFee { - return StdFee{ - Amount: amount, - Gas: gas, - } -} - -// fee bytes for signing later -func (fee StdFee) Bytes() []byte { - // normalize. XXX - // this is a sign of something ugly - // (in the lcd_test, client side its null, - // server side its []) - if len(fee.Amount) == 0 { - fee.Amount = Coins{} - } - bz, err := json.Marshal(fee) // TODO - if err != nil { - panic(err) - } - return bz -} - -//__________________________________________________________ - -// StdSignDoc is replay-prevention structure. -// It includes the result of msg.GetSignBytes(), -// as well as the ChainID (prevent cross chain replay) -// and the Sequence numbers for each signature (prevent -// inchain replay and enforce tx ordering per account). -type StdSignDoc struct { - ChainID string `json:"chain_id"` - Sequences []int64 `json:"sequences"` - FeeBytes []byte `json:"fee_bytes"` - MsgBytes []byte `json:"msg_bytes"` - AltBytes []byte `json:"alt_bytes"` -} - -// StdSignBytes returns the bytes to sign for a transaction. -// TODO: change the API to just take a chainID and StdTx ? -func StdSignBytes(chainID string, sequences []int64, fee StdFee, msg Msg) []byte { - bz, err := json.Marshal(StdSignDoc{ - ChainID: chainID, - Sequences: sequences, - FeeBytes: fee.Bytes(), - MsgBytes: msg.GetSignBytes(), - }) - if err != nil { - panic(err) - } - return bz -} - -// StdSignMsg is a convenience structure for passing along -// a Msg with the other requirements for a StdSignDoc before -// it is signed. For use in the CLI. -type StdSignMsg struct { - ChainID string - Sequences []int64 - Fee StdFee - Msg Msg - // XXX: Alt -} - -// get message bytes -func (msg StdSignMsg) Bytes() []byte { - return StdSignBytes(msg.ChainID, msg.Sequences, msg.Fee, msg.Msg) -} - -//__________________________________________________________ - -// TxDeocder unmarshals transaction bytes +// TxDecoder unmarshals transaction bytes type TxDecoder func(txBytes []byte) (Tx, Error) //__________________________________________________________ diff --git a/x/auth/baseaccount.go b/x/auth/account.go similarity index 74% rename from x/auth/baseaccount.go rename to x/auth/account.go index ff907fc38..0ae72a8a6 100644 --- a/x/auth/baseaccount.go +++ b/x/auth/account.go @@ -3,16 +3,34 @@ package auth import ( "errors" - "github.com/tendermint/go-crypto" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" + crypto "github.com/tendermint/go-crypto" ) +// Account is a standard account using a sequence number for replay protection +// and a pubkey for authentication. +type Account interface { + GetAddress() sdk.Address + SetAddress(sdk.Address) error // errors if already set. + + GetPubKey() crypto.PubKey // can return nil. + SetPubKey(crypto.PubKey) error + + GetSequence() int64 + SetSequence(int64) error + + GetCoins() sdk.Coins + SetCoins(sdk.Coins) error +} + +// AccountDecoder unmarshals account bytes +type AccountDecoder func(accountBytes []byte) (Account, error) + //----------------------------------------------------------- // BaseAccount -var _ sdk.Account = (*BaseAccount)(nil) +var _ Account = (*BaseAccount)(nil) // BaseAccount - base account structure. // Extend this by embedding this in your AppAccount. @@ -82,7 +100,7 @@ func (acc *BaseAccount) SetSequence(seq int64) error { // Most users shouldn't use this, but this comes handy for tests. func RegisterBaseAccount(cdc *wire.Codec) { - cdc.RegisterInterface((*sdk.Account)(nil), nil) + cdc.RegisterInterface((*Account)(nil), nil) cdc.RegisterConcrete(&BaseAccount{}, "cosmos-sdk/BaseAccount", nil) wire.RegisterCrypto(cdc) } diff --git a/x/auth/baseaccount_test.go b/x/auth/account_test.go similarity index 97% rename from x/auth/baseaccount_test.go rename to x/auth/account_test.go index d3363e4fb..a06545d3b 100644 --- a/x/auth/baseaccount_test.go +++ b/x/auth/account_test.go @@ -18,7 +18,7 @@ func keyPubAddr() (crypto.PrivKey, crypto.PubKey, sdk.Address) { return key, pub, addr } -func TestBaseAccountAddressPubKey(t *testing.T) { +func TestBaseAddressPubKey(t *testing.T) { _, pub1, addr1 := keyPubAddr() _, pub2, addr2 := keyPubAddr() acc := NewBaseAccountWithAddress(addr1) diff --git a/x/auth/ante.go b/x/auth/ante.go index 248083206..9663bcfe4 100644 --- a/x/auth/ante.go +++ b/x/auth/ante.go @@ -9,19 +9,27 @@ import ( ) const ( - verifyCost = 100 + deductFeesCost sdk.Gas = 10 + verifyCost = 100 ) // NewAnteHandler returns an AnteHandler that checks // and increments sequence numbers, checks signatures, // and deducts fees from the first signer. -func NewAnteHandler(am sdk.AccountMapper, feeHandler sdk.FeeHandler) sdk.AnteHandler { +func NewAnteHandler(am AccountMapper, fck FeeCollectionKeeper) sdk.AnteHandler { + return func( ctx sdk.Context, tx sdk.Tx, ) (_ sdk.Context, _ sdk.Result, abort bool) { + // This AnteHandler requires Txs to be StdTxs + stdTx, ok := tx.(StdTx) + if !ok { + return ctx, sdk.ErrInternal("tx must be StdTx").Result(), true + } + // Assert that there are signatures. - var sigs = tx.GetSignatures() + var sigs = stdTx.GetSignatures() if len(sigs) == 0 { return ctx, sdk.ErrUnauthorized("no signers").Result(), @@ -30,12 +38,6 @@ func NewAnteHandler(am sdk.AccountMapper, feeHandler sdk.FeeHandler) sdk.AnteHan msg := tx.GetMsg() - // TODO: will this always be a stdtx? should that be used in the function signature? - stdTx, ok := tx.(sdk.StdTx) - if !ok { - return ctx, sdk.ErrInternal("tx must be sdk.StdTx").Result(), true - } - // Assert that number of signatures is correct. var signerAddrs = msg.GetSigners() if len(sigs) != len(signerAddrs) { @@ -56,10 +58,10 @@ func NewAnteHandler(am sdk.AccountMapper, feeHandler sdk.FeeHandler) sdk.AnteHan if chainID == "" { chainID = viper.GetString("chain-id") } - signBytes := sdk.StdSignBytes(ctx.ChainID(), sequences, fee, msg) + signBytes := StdSignBytes(ctx.ChainID(), sequences, fee, msg) // Check sig and nonce and collect signer accounts. - var signerAccs = make([]sdk.Account, len(signerAddrs)) + var signerAccs = make([]Account, len(signerAddrs)) for i := 0; i < len(sigs); i++ { signerAddr, sig := signerAddrs[i], sigs[i] @@ -76,11 +78,12 @@ func NewAnteHandler(am sdk.AccountMapper, feeHandler sdk.FeeHandler) sdk.AnteHan if i == 0 { // TODO: min fee if !fee.Amount.IsZero() { + ctx.GasMeter().ConsumeGas(deductFeesCost, "deductFees") signerAcc, res = deductFees(signerAcc, fee) - feeHandler(ctx, tx, fee.Amount) if !res.IsOK() { return ctx, res, true } + fck.addCollectedFees(ctx, fee.Amount) } } @@ -104,9 +107,9 @@ func NewAnteHandler(am sdk.AccountMapper, feeHandler sdk.FeeHandler) sdk.AnteHan // verify the signature and increment the sequence. // if the account doesn't have a pubkey, set it. func processSig( - ctx sdk.Context, am sdk.AccountMapper, - addr sdk.Address, sig sdk.StdSignature, signBytes []byte) ( - acc sdk.Account, res sdk.Result) { + ctx sdk.Context, am AccountMapper, + addr sdk.Address, sig StdSignature, signBytes []byte) ( + acc Account, res sdk.Result) { // Get the account. acc = am.GetAccount(ctx, addr) @@ -152,7 +155,7 @@ func processSig( // Deduct the fee from the account. // We could use the CoinKeeper (in addition to the AccountMapper, // because the CoinKeeper doesn't give us accounts), but it seems easier to do this. -func deductFees(acc sdk.Account, fee sdk.StdFee) (sdk.Account, sdk.Result) { +func deductFees(acc Account, fee StdFee) (Account, sdk.Result) { coins := acc.GetCoins() feeAmount := fee.Amount @@ -166,5 +169,4 @@ func deductFees(acc sdk.Account, fee sdk.StdFee) (sdk.Account, sdk.Result) { } // BurnFeeHandler burns all fees (decreasing total supply) -func BurnFeeHandler(ctx sdk.Context, tx sdk.Tx, fee sdk.Coins) { -} +func BurnFeeHandler(_ sdk.Context, _ sdk.Tx, _ sdk.Coins) {} diff --git a/x/auth/ante_test.go b/x/auth/ante_test.go index ec296b12b..b7f22e5d5 100644 --- a/x/auth/ante_test.go +++ b/x/auth/ante_test.go @@ -17,8 +17,8 @@ func newTestMsg(addrs ...sdk.Address) *sdk.TestMsg { return sdk.NewTestMsg(addrs...) } -func newStdFee() sdk.StdFee { - return sdk.NewStdFee(100, +func newStdFee() StdFee { + return NewStdFee(100, sdk.Coin{"atom", 150}, ) } @@ -52,28 +52,29 @@ func checkInvalidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, assert.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, code), result.Code) } -func newTestTx(ctx sdk.Context, msg sdk.Msg, privs []crypto.PrivKey, seqs []int64, fee sdk.StdFee) sdk.Tx { - signBytes := sdk.StdSignBytes(ctx.ChainID(), seqs, fee, msg) +func newTestTx(ctx sdk.Context, msg sdk.Msg, privs []crypto.PrivKey, seqs []int64, fee StdFee) sdk.Tx { + signBytes := StdSignBytes(ctx.ChainID(), seqs, fee, msg) return newTestTxWithSignBytes(msg, privs, seqs, fee, signBytes) } -func newTestTxWithSignBytes(msg sdk.Msg, privs []crypto.PrivKey, seqs []int64, fee sdk.StdFee, signBytes []byte) sdk.Tx { - sigs := make([]sdk.StdSignature, len(privs)) +func newTestTxWithSignBytes(msg sdk.Msg, privs []crypto.PrivKey, seqs []int64, fee StdFee, signBytes []byte) sdk.Tx { + sigs := make([]StdSignature, len(privs)) for i, priv := range privs { - sigs[i] = sdk.StdSignature{PubKey: priv.PubKey(), Signature: priv.Sign(signBytes), Sequence: seqs[i]} + sigs[i] = StdSignature{PubKey: priv.PubKey(), Signature: priv.Sign(signBytes), Sequence: seqs[i]} } - tx := sdk.NewStdTx(msg, fee, sigs) + tx := NewStdTx(msg, fee, sigs) return tx } // Test various error cases in the AnteHandler control flow. func TestAnteHandlerSigErrors(t *testing.T) { // setup - ms, capKey := setupMultiStore() + ms, capKey, capKey2 := setupMultiStore() cdc := wire.NewCodec() RegisterBaseAccount(cdc) mapper := NewAccountMapper(cdc, capKey, &BaseAccount{}) - anteHandler := NewAnteHandler(mapper, BurnFeeHandler) + feeCollector := NewFeeCollectionKeeper(cdc, capKey2) + anteHandler := NewAnteHandler(mapper, feeCollector) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger()) // keys and addresses @@ -110,11 +111,12 @@ func TestAnteHandlerSigErrors(t *testing.T) { // Test logic around sequence checking with one signer and many signers. func TestAnteHandlerSequences(t *testing.T) { // setup - ms, capKey := setupMultiStore() + ms, capKey, capKey2 := setupMultiStore() cdc := wire.NewCodec() RegisterBaseAccount(cdc) mapper := NewAccountMapper(cdc, capKey, &BaseAccount{}) - anteHandler := NewAnteHandler(mapper, BurnFeeHandler) + feeCollector := NewFeeCollectionKeeper(cdc, capKey2) + anteHandler := NewAnteHandler(mapper, feeCollector) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger()) // keys and addresses @@ -176,11 +178,12 @@ func TestAnteHandlerSequences(t *testing.T) { // Test logic around fee deduction. func TestAnteHandlerFees(t *testing.T) { // setup - ms, capKey := setupMultiStore() + ms, capKey, capKey2 := setupMultiStore() cdc := wire.NewCodec() RegisterBaseAccount(cdc) mapper := NewAccountMapper(cdc, capKey, &BaseAccount{}) - anteHandler := NewAnteHandler(mapper, BurnFeeHandler) + feeCollector := NewFeeCollectionKeeper(cdc, capKey2) + anteHandler := NewAnteHandler(mapper, feeCollector) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger()) // keys and addresses @@ -194,7 +197,7 @@ func TestAnteHandlerFees(t *testing.T) { var tx sdk.Tx msg := newTestMsg(addr1) privs, seqs := []crypto.PrivKey{priv1}, []int64{0} - fee := sdk.NewStdFee(100, + fee := NewStdFee(100, sdk.Coin{"atom", 150}, ) @@ -206,18 +209,23 @@ func TestAnteHandlerFees(t *testing.T) { mapper.SetAccount(ctx, acc1) checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeInsufficientFunds) + assert.True(t, feeCollector.GetCollectedFees(ctx).IsEqual(emptyCoins)) + acc1.SetCoins(sdk.Coins{{"atom", 150}}) mapper.SetAccount(ctx, acc1) checkValidTx(t, anteHandler, ctx, tx) + + assert.True(t, feeCollector.GetCollectedFees(ctx).IsEqual(sdk.Coins{{"atom", 150}})) } func TestAnteHandlerBadSignBytes(t *testing.T) { // setup - ms, capKey := setupMultiStore() + ms, capKey, capKey2 := setupMultiStore() cdc := wire.NewCodec() RegisterBaseAccount(cdc) mapper := NewAccountMapper(cdc, capKey, &BaseAccount{}) - anteHandler := NewAnteHandler(mapper, BurnFeeHandler) + feeCollector := NewFeeCollectionKeeper(cdc, capKey2) + anteHandler := NewAnteHandler(mapper, feeCollector) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger()) // keys and addresses @@ -252,7 +260,7 @@ func TestAnteHandlerBadSignBytes(t *testing.T) { cases := []struct { chainID string seqs []int64 - fee sdk.StdFee + fee StdFee msg sdk.Msg code sdk.CodeType }{ @@ -268,7 +276,7 @@ func TestAnteHandlerBadSignBytes(t *testing.T) { for _, cs := range cases { tx := newTestTxWithSignBytes( msg, privs, seqs, fee, - sdk.StdSignBytes(cs.chainID, cs.seqs, cs.fee, cs.msg), + StdSignBytes(cs.chainID, cs.seqs, cs.fee, cs.msg), ) checkInvalidTx(t, anteHandler, ctx, tx, cs.code) } @@ -288,11 +296,12 @@ func TestAnteHandlerBadSignBytes(t *testing.T) { func TestAnteHandlerSetPubKey(t *testing.T) { // setup - ms, capKey := setupMultiStore() + ms, capKey, capKey2 := setupMultiStore() cdc := wire.NewCodec() RegisterBaseAccount(cdc) mapper := NewAccountMapper(cdc, capKey, &BaseAccount{}) - anteHandler := NewAnteHandler(mapper, BurnFeeHandler) + feeCollector := NewFeeCollectionKeeper(cdc, capKey2) + anteHandler := NewAnteHandler(mapper, feeCollector) ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger()) // keys and addresses @@ -322,7 +331,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) { // test public key not found msg = newTestMsg(addr2) tx = newTestTx(ctx, msg, privs, seqs, fee) - sigs := tx.GetSignatures() + sigs := tx.(StdTx).GetSignatures() sigs[0].PubKey = nil checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeInvalidPubKey) diff --git a/x/auth/client/cli/account.go b/x/auth/client/cli/account.go index b45cb12dd..4006baa14 100644 --- a/x/auth/client/cli/account.go +++ b/x/auth/client/cli/account.go @@ -1,7 +1,6 @@ package cli import ( - "encoding/hex" "fmt" "github.com/spf13/cobra" @@ -9,6 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" + "github.com/cosmos/cosmos-sdk/x/auth" ) // GetAccountCmd for the auth.BaseAccount type @@ -17,8 +17,8 @@ func GetAccountCmdDefault(storeName string, cdc *wire.Codec) *cobra.Command { } // Get account decoder for auth.DefaultAccount -func GetAccountDecoder(cdc *wire.Codec) sdk.AccountDecoder { - return func(accBytes []byte) (acct sdk.Account, err error) { +func GetAccountDecoder(cdc *wire.Codec) auth.AccountDecoder { + return func(accBytes []byte) (acct auth.Account, err error) { // acct := new(auth.BaseAccount) err = cdc.UnmarshalBinaryBare(accBytes, &acct) if err != nil { @@ -30,7 +30,7 @@ func GetAccountDecoder(cdc *wire.Codec) sdk.AccountDecoder { // GetAccountCmd returns a query account that will display the // state of the account at a given address -func GetAccountCmd(storeName string, cdc *wire.Codec, decoder sdk.AccountDecoder) *cobra.Command { +func GetAccountCmd(storeName string, cdc *wire.Codec, decoder auth.AccountDecoder) *cobra.Command { return &cobra.Command{ Use: "account [address]", Short: "Query account balance", @@ -39,11 +39,11 @@ func GetAccountCmd(storeName string, cdc *wire.Codec, decoder sdk.AccountDecoder // find the key to look up the account addr := args[0] - bz, err := hex.DecodeString(addr) + + key, err := sdk.GetAccAddressBech32Cosmos(addr) if err != nil { return err } - key := sdk.Address(bz) // perform query ctx := context.NewCoreContextFromViper() diff --git a/x/auth/client/rest/query.go b/x/auth/client/rest/query.go index 52244fec9..a60ce5cdb 100644 --- a/x/auth/client/rest/query.go +++ b/x/auth/client/rest/query.go @@ -10,19 +10,20 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" - auth "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + "github.com/cosmos/cosmos-sdk/x/auth" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" ) // register REST routes func RegisterRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec, storeName string) { r.HandleFunc( "/accounts/{address}", - QueryAccountRequestHandlerFn(storeName, cdc, auth.GetAccountDecoder(cdc), ctx), + QueryAccountRequestHandlerFn(storeName, cdc, authcmd.GetAccountDecoder(cdc), ctx), ).Methods("GET") } // query accountREST Handler -func QueryAccountRequestHandlerFn(storeName string, cdc *wire.Codec, decoder sdk.AccountDecoder, ctx context.CoreContext) http.HandlerFunc { +func QueryAccountRequestHandlerFn(storeName string, cdc *wire.Codec, decoder auth.AccountDecoder, ctx context.CoreContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) addr := vars["address"] diff --git a/x/auth/context.go b/x/auth/context.go index b233f1e86..40fb17785 100644 --- a/x/auth/context.go +++ b/x/auth/context.go @@ -34,15 +34,15 @@ const ( ) // add the signers to the context -func WithSigners(ctx types.Context, accounts []types.Account) types.Context { +func WithSigners(ctx types.Context, accounts []Account) types.Context { return ctx.WithValue(contextKeySigners, accounts) } // get the signers from the context -func GetSigners(ctx types.Context) []types.Account { +func GetSigners(ctx types.Context) []Account { v := ctx.Value(contextKeySigners) if v == nil { - return []types.Account{} + return []Account{} } - return v.([]types.Account) + return v.([]Account) } diff --git a/x/auth/context_test.go b/x/auth/context_test.go index 89e318e0a..a93de44d0 100644 --- a/x/auth/context_test.go +++ b/x/auth/context_test.go @@ -12,7 +12,7 @@ import ( ) func TestContextWithSigners(t *testing.T) { - ms, _ := setupMultiStore() + ms, _, _ := setupMultiStore() ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger()) _, _, addr1 := keyPubAddr() @@ -26,7 +26,7 @@ func TestContextWithSigners(t *testing.T) { signers := GetSigners(ctx) assert.Equal(t, 0, len(signers)) - ctx2 := WithSigners(ctx, []sdk.Account{&acc1, &acc2}) + ctx2 := WithSigners(ctx, []Account{&acc1, &acc2}) // original context is unchanged signers = GetSigners(ctx) diff --git a/x/auth/feekeeper.go b/x/auth/feekeeper.go new file mode 100644 index 000000000..3e03a81aa --- /dev/null +++ b/x/auth/feekeeper.go @@ -0,0 +1,62 @@ +package auth + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + wire "github.com/cosmos/cosmos-sdk/wire" +) + +var ( + collectedFeesKey = []byte("collectedFees") +) + +// This FeeCollectionKeeper handles collection of fees in the anteHandler +// and setting of MinFees for different fee tokens +type FeeCollectionKeeper struct { + + // The (unexposed) key used to access the fee store from the Context. + key sdk.StoreKey + + // The wire codec for binary encoding/decoding of accounts. + cdc *wire.Codec +} + +// NewFeeKeeper returns a new FeeKeeper +func NewFeeCollectionKeeper(cdc *wire.Codec, key sdk.StoreKey) FeeCollectionKeeper { + return FeeCollectionKeeper{ + key: key, + cdc: cdc, + } +} + +// Adds to Collected Fee Pool +func (fck FeeCollectionKeeper) GetCollectedFees(ctx sdk.Context) sdk.Coins { + store := ctx.KVStore(fck.key) + bz := store.Get(collectedFeesKey) + if bz == nil { + return sdk.Coins{} + } + + feePool := &(sdk.Coins{}) + fck.cdc.MustUnmarshalBinary(bz, feePool) + return *feePool +} + +// Sets to Collected Fee Pool +func (fck FeeCollectionKeeper) setCollectedFees(ctx sdk.Context, coins sdk.Coins) { + bz := fck.cdc.MustMarshalBinary(coins) + store := ctx.KVStore(fck.key) + store.Set(collectedFeesKey, bz) +} + +// Adds to Collected Fee Pool +func (fck FeeCollectionKeeper) addCollectedFees(ctx sdk.Context, coins sdk.Coins) sdk.Coins { + newCoins := fck.GetCollectedFees(ctx).Plus(coins) + fck.setCollectedFees(ctx, newCoins) + + return newCoins +} + +// Clears the collected Fee Pool +func (fck FeeCollectionKeeper) ClearCollectedFees(ctx sdk.Context) { + fck.setCollectedFees(ctx, sdk.Coins{}) +} diff --git a/x/auth/feekeeper_test.go b/x/auth/feekeeper_test.go new file mode 100644 index 000000000..2f1ffc59b --- /dev/null +++ b/x/auth/feekeeper_test.go @@ -0,0 +1,75 @@ +package auth + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + abci "github.com/tendermint/abci/types" + "github.com/tendermint/tmlibs/log" + + sdk "github.com/cosmos/cosmos-sdk/types" + wire "github.com/cosmos/cosmos-sdk/wire" +) + +var ( + emptyCoins = sdk.Coins{} + oneCoin = sdk.Coins{{"foocoin", 1}} + twoCoins = sdk.Coins{{"foocoin", 2}} +) + +func TestFeeCollectionKeeperGetSet(t *testing.T) { + ms, _, capKey2 := setupMultiStore() + cdc := wire.NewCodec() + + // make context and keeper + ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger()) + fck := NewFeeCollectionKeeper(cdc, capKey2) + + // no coins initially + currFees := fck.GetCollectedFees(ctx) + assert.True(t, currFees.IsEqual(emptyCoins)) + + // set feeCollection to oneCoin + fck.setCollectedFees(ctx, oneCoin) + + // check that it is equal to oneCoin + assert.True(t, fck.GetCollectedFees(ctx).IsEqual(oneCoin)) +} + +func TestFeeCollectionKeeperAdd(t *testing.T) { + ms, _, capKey2 := setupMultiStore() + cdc := wire.NewCodec() + + // make context and keeper + ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger()) + fck := NewFeeCollectionKeeper(cdc, capKey2) + + // no coins initially + assert.True(t, fck.GetCollectedFees(ctx).IsEqual(emptyCoins)) + + // add oneCoin and check that pool is now oneCoin + fck.addCollectedFees(ctx, oneCoin) + assert.True(t, fck.GetCollectedFees(ctx).IsEqual(oneCoin)) + + // add oneCoin again and check that pool is now twoCoins + fck.addCollectedFees(ctx, oneCoin) + assert.True(t, fck.GetCollectedFees(ctx).IsEqual(twoCoins)) +} + +func TestFeeCollectionKeeperClear(t *testing.T) { + ms, _, capKey2 := setupMultiStore() + cdc := wire.NewCodec() + + // make context and keeper + ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger()) + fck := NewFeeCollectionKeeper(cdc, capKey2) + + // set coins initially + fck.setCollectedFees(ctx, twoCoins) + assert.True(t, fck.GetCollectedFees(ctx).IsEqual(twoCoins)) + + // clear fees and see that pool is now empty + fck.ClearCollectedFees(ctx) + assert.True(t, fck.GetCollectedFees(ctx).IsEqual(emptyCoins)) +} diff --git a/x/auth/handler.go b/x/auth/handler.go index 8a0e1061a..764c6f7a2 100644 --- a/x/auth/handler.go +++ b/x/auth/handler.go @@ -6,14 +6,14 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -// NewHandler returns a handler for "auth" type messages. +// NewHandler returns a handler for "baseaccount" type messages. func NewHandler(am AccountMapper) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { switch msg := msg.(type) { case MsgChangeKey: return handleMsgChangeKey(ctx, am, msg) default: - errMsg := "Unrecognized auth Msg type: " + reflect.TypeOf(msg).Name() + errMsg := "Unrecognized baseaccount Msg type: " + reflect.TypeOf(msg).Name() return sdk.ErrUnknownRequest(errMsg).Result() } } @@ -23,7 +23,7 @@ func NewHandler(am AccountMapper) sdk.Handler { // Should be very expensive, because once this happens, an account is un-prunable func handleMsgChangeKey(ctx sdk.Context, am AccountMapper, msg MsgChangeKey) sdk.Result { - err := am.setPubKey(ctx, msg.Address, msg.NewPubKey) + err := am.SetPubKey(ctx, msg.Address, msg.NewPubKey) if err != nil { return err.Result() } diff --git a/x/auth/mapper.go b/x/auth/mapper.go index 3666f13b6..cdab2480e 100644 --- a/x/auth/mapper.go +++ b/x/auth/mapper.go @@ -9,9 +9,6 @@ import ( crypto "github.com/tendermint/go-crypto" ) -var _ sdk.AccountMapper = (*AccountMapper)(nil) - -// Implements sdk.AccountMapper. // This AccountMapper encodes/decodes accounts using the // go-amino (binary) encoding/decoding library. type AccountMapper struct { @@ -19,8 +16,8 @@ type AccountMapper struct { // The (unexposed) key used to access the store from the Context. key sdk.StoreKey - // The prototypical sdk.Account concrete type. - proto sdk.Account + // The prototypical Account concrete type. + proto Account // The wire codec for binary encoding/decoding of accounts. cdc *wire.Codec @@ -29,7 +26,7 @@ type AccountMapper struct { // NewAccountMapper returns a new sdk.AccountMapper that // uses go-amino to (binary) encode and decode concrete sdk.Accounts. // nolint -func NewAccountMapper(cdc *wire.Codec, key sdk.StoreKey, proto sdk.Account) AccountMapper { +func NewAccountMapper(cdc *wire.Codec, key sdk.StoreKey, proto Account) AccountMapper { return AccountMapper{ key: key, proto: proto, @@ -38,14 +35,14 @@ func NewAccountMapper(cdc *wire.Codec, key sdk.StoreKey, proto sdk.Account) Acco } // Implaements sdk.AccountMapper. -func (am AccountMapper) NewAccountWithAddress(ctx sdk.Context, addr sdk.Address) sdk.Account { +func (am AccountMapper) NewAccountWithAddress(ctx sdk.Context, addr sdk.Address) Account { acc := am.clonePrototype() acc.SetAddress(addr) return acc } // Implements sdk.AccountMapper. -func (am AccountMapper) GetAccount(ctx sdk.Context, addr sdk.Address) sdk.Account { +func (am AccountMapper) GetAccount(ctx sdk.Context, addr sdk.Address) Account { store := ctx.KVStore(am.key) bz := store.Get(addr) if bz == nil { @@ -56,7 +53,7 @@ func (am AccountMapper) GetAccount(ctx sdk.Context, addr sdk.Address) sdk.Accoun } // Implements sdk.AccountMapper. -func (am AccountMapper) SetAccount(ctx sdk.Context, acc sdk.Account) { +func (am AccountMapper) SetAccount(ctx sdk.Context, acc Account) { addr := acc.GetAddress() store := ctx.KVStore(am.key) bz := am.encodeAccount(acc) @@ -64,7 +61,7 @@ func (am AccountMapper) SetAccount(ctx sdk.Context, acc sdk.Account) { } // Implements sdk.AccountMapper. -func (am AccountMapper) IterateAccounts(ctx sdk.Context, process func(sdk.Account) (stop bool)) { +func (am AccountMapper) IterateAccounts(ctx sdk.Context, process func(Account) (stop bool)) { store := ctx.KVStore(am.key) iter := store.Iterator(nil, nil) for { @@ -89,7 +86,8 @@ func (am AccountMapper) GetPubKey(ctx sdk.Context, addr sdk.Address) (crypto.Pub return acc.GetPubKey(), nil } -func (am AccountMapper) setPubKey(ctx sdk.Context, addr sdk.Address, newPubKey crypto.PubKey) sdk.Error { +// Sets the PubKey of the account at address +func (am AccountMapper) SetPubKey(ctx sdk.Context, addr sdk.Address, newPubKey crypto.PubKey) sdk.Error { acc := am.GetAccount(ctx, addr) if acc == nil { return sdk.ErrUnknownAddress(addr.String()) @@ -122,7 +120,7 @@ func (am AccountMapper) setSequence(ctx sdk.Context, addr sdk.Address, newSequen // misc. // Creates a new struct (or pointer to struct) from am.proto. -func (am AccountMapper) clonePrototype() sdk.Account { +func (am AccountMapper) clonePrototype() Account { protoRt := reflect.TypeOf(am.proto) if protoRt.Kind() == reflect.Ptr { protoCrt := protoRt.Elem() @@ -130,7 +128,7 @@ func (am AccountMapper) clonePrototype() sdk.Account { panic("accountMapper requires a struct proto sdk.Account, or a pointer to one") } protoRv := reflect.New(protoCrt) - clone, ok := protoRv.Interface().(sdk.Account) + clone, ok := protoRv.Interface().(Account) if !ok { panic(fmt.Sprintf("accountMapper requires a proto sdk.Account, but %v doesn't implement sdk.Account", protoRt)) } @@ -138,14 +136,14 @@ func (am AccountMapper) clonePrototype() sdk.Account { } protoRv := reflect.New(protoRt).Elem() - clone, ok := protoRv.Interface().(sdk.Account) + clone, ok := protoRv.Interface().(Account) if !ok { panic(fmt.Sprintf("accountMapper requires a proto sdk.Account, but %v doesn't implement sdk.Account", protoRt)) } return clone } -func (am AccountMapper) encodeAccount(acc sdk.Account) []byte { +func (am AccountMapper) encodeAccount(acc Account) []byte { bz, err := am.cdc.MarshalBinaryBare(acc) if err != nil { panic(err) @@ -153,7 +151,7 @@ func (am AccountMapper) encodeAccount(acc sdk.Account) []byte { return bz } -func (am AccountMapper) decodeAccount(bz []byte) (acc sdk.Account) { +func (am AccountMapper) decodeAccount(bz []byte) (acc Account) { err := am.cdc.UnmarshalBinaryBare(bz, &acc) if err != nil { panic(err) diff --git a/x/auth/mapper_test.go b/x/auth/mapper_test.go index cdd418990..7f6397069 100644 --- a/x/auth/mapper_test.go +++ b/x/auth/mapper_test.go @@ -14,17 +14,19 @@ import ( wire "github.com/cosmos/cosmos-sdk/wire" ) -func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey) { +func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey) { db := dbm.NewMemDB() capKey := sdk.NewKVStoreKey("capkey") + capKey2 := sdk.NewKVStoreKey("capkey2") ms := store.NewCommitMultiStore(db) ms.MountStoreWithDB(capKey, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(capKey2, sdk.StoreTypeIAVL, db) ms.LoadLatestVersion() - return ms, capKey + return ms, capKey, capKey2 } func TestAccountMapperGetSet(t *testing.T) { - ms, capKey := setupMultiStore() + ms, capKey, _ := setupMultiStore() cdc := wire.NewCodec() RegisterBaseAccount(cdc) diff --git a/x/auth/msgs.go b/x/auth/msgs.go index 545b296e5..c449b837b 100644 --- a/x/auth/msgs.go +++ b/x/auth/msgs.go @@ -3,9 +3,8 @@ package auth import ( "encoding/json" - "github.com/tendermint/go-crypto" - sdk "github.com/cosmos/cosmos-sdk/types" + crypto "github.com/tendermint/go-crypto" ) // MsgChangeKey - high level transaction of the auth module diff --git a/x/auth/stdtx.go b/x/auth/stdtx.go new file mode 100644 index 000000000..bc01b0149 --- /dev/null +++ b/x/auth/stdtx.go @@ -0,0 +1,131 @@ +package auth + +import ( + "encoding/json" + + sdk "github.com/cosmos/cosmos-sdk/types" + crypto "github.com/tendermint/go-crypto" +) + +var _ sdk.Tx = (*StdTx)(nil) + +// StdTx is a standard way to wrap a Msg with Fee and Signatures. +// NOTE: the first signature is the FeePayer (Signatures must not be nil). +type StdTx struct { + Msg sdk.Msg `json:"msg"` + Fee StdFee `json:"fee"` + Signatures []StdSignature `json:"signatures"` +} + +func NewStdTx(msg sdk.Msg, fee StdFee, sigs []StdSignature) StdTx { + return StdTx{ + Msg: msg, + Fee: fee, + Signatures: sigs, + } +} + +//nolint +func (tx StdTx) GetMsg() sdk.Msg { return tx.Msg } + +// Signatures returns the signature of signers who signed the Msg. +// CONTRACT: Length returned is same as length of +// pubkeys returned from MsgKeySigners, and the order +// matches. +// CONTRACT: If the signature is missing (ie the Msg is +// invalid), then the corresponding signature is +// .Empty(). +func (tx StdTx) GetSignatures() []StdSignature { return tx.Signatures } + +// FeePayer returns the address responsible for paying the fees +// for the transactions. It's the first address returned by msg.GetSigners(). +// If GetSigners() is empty, this panics. +func FeePayer(tx sdk.Tx) sdk.Address { + return tx.GetMsg().GetSigners()[0] +} + +//__________________________________________________________ + +// StdFee includes the amount of coins paid in fees and the maximum +// gas to be used by the transaction. The ratio yields an effective "gasprice", +// which must be above some miminum to be accepted into the mempool. +type StdFee struct { + Amount sdk.Coins `json:"amount"` + Gas int64 `json:"gas"` +} + +func NewStdFee(gas int64, amount ...sdk.Coin) StdFee { + return StdFee{ + Amount: amount, + Gas: gas, + } +} + +// fee bytes for signing later +func (fee StdFee) Bytes() []byte { + // normalize. XXX + // this is a sign of something ugly + // (in the lcd_test, client side its null, + // server side its []) + if len(fee.Amount) == 0 { + fee.Amount = sdk.Coins{} + } + bz, err := json.Marshal(fee) // TODO + if err != nil { + panic(err) + } + return bz +} + +//__________________________________________________________ + +// StdSignDoc is replay-prevention structure. +// It includes the result of msg.GetSignBytes(), +// as well as the ChainID (prevent cross chain replay) +// and the Sequence numbers for each signature (prevent +// inchain replay and enforce tx ordering per account). +type StdSignDoc struct { + ChainID string `json:"chain_id"` + Sequences []int64 `json:"sequences"` + FeeBytes []byte `json:"fee_bytes"` + MsgBytes []byte `json:"msg_bytes"` + AltBytes []byte `json:"alt_bytes"` +} + +// StdSignBytes returns the bytes to sign for a transaction. +// TODO: change the API to just take a chainID and StdTx ? +func StdSignBytes(chainID string, sequences []int64, fee StdFee, msg sdk.Msg) []byte { + bz, err := json.Marshal(StdSignDoc{ + ChainID: chainID, + Sequences: sequences, + FeeBytes: fee.Bytes(), + MsgBytes: msg.GetSignBytes(), + }) + if err != nil { + panic(err) + } + return bz +} + +// StdSignMsg is a convenience structure for passing along +// a Msg with the other requirements for a StdSignDoc before +// it is signed. For use in the CLI. +type StdSignMsg struct { + ChainID string + Sequences []int64 + Fee StdFee + Msg sdk.Msg + // XXX: Alt +} + +// get message bytes +func (msg StdSignMsg) Bytes() []byte { + return StdSignBytes(msg.ChainID, msg.Sequences, msg.Fee, msg.Msg) +} + +// Standard Signature +type StdSignature struct { + crypto.PubKey `json:"pub_key"` // optional + crypto.Signature `json:"signature"` + Sequence int64 `json:"sequence"` +} diff --git a/types/tx_msg_test.go b/x/auth/stdtx_test.go similarity index 70% rename from types/tx_msg_test.go rename to x/auth/stdtx_test.go index f72cdea26..412b37c20 100644 --- a/types/tx_msg_test.go +++ b/x/auth/stdtx_test.go @@ -1,4 +1,4 @@ -package types +package auth import ( "testing" @@ -6,18 +6,20 @@ import ( "github.com/stretchr/testify/assert" crypto "github.com/tendermint/go-crypto" + + sdk "github.com/cosmos/cosmos-sdk/types" ) -func newStdFee() StdFee { - return NewStdFee(100, - Coin{"atom", 150}, - ) -} +// func newStdFee() StdFee { +// return NewStdFee(100, +// Coin{"atom", 150}, +// ) +// } func TestStdTx(t *testing.T) { priv := crypto.GenPrivKeyEd25519() addr := priv.PubKey().Address() - msg := NewTestMsg(addr) + msg := sdk.NewTestMsg(addr) fee := newStdFee() sigs := []StdSignature{} diff --git a/x/auth/wire.go b/x/auth/wire.go index 9db1b85cc..42b34b96d 100644 --- a/x/auth/wire.go +++ b/x/auth/wire.go @@ -1,12 +1,12 @@ package auth import ( - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" ) // Register concrete types on wire codec for default AppAccount func RegisterWire(cdc *wire.Codec) { - cdc.RegisterInterface((*sdk.Account)(nil), nil) + cdc.RegisterInterface((*Account)(nil), nil) cdc.RegisterConcrete(&BaseAccount{}, "auth/Account", nil) + cdc.RegisterConcrete(MsgChangeKey{}, "auth/ChangeKey", nil) } diff --git a/x/bank/client/cli/sendtx.go b/x/bank/client/cli/sendtx.go index 910984288..5ed56a85b 100644 --- a/x/bank/client/cli/sendtx.go +++ b/x/bank/client/cli/sendtx.go @@ -1,7 +1,6 @@ package cli import ( - "encoding/hex" "fmt" "github.com/spf13/cobra" @@ -34,12 +33,11 @@ func SendTxCmd(cdc *wire.Codec) *cobra.Command { } toStr := viper.GetString(flagTo) - bz, err := hex.DecodeString(toStr) + + to, err := sdk.GetAccAddressBech32Cosmos(toStr) if err != nil { return err } - to := sdk.Address(bz) - // parse coins amount := viper.GetString(flagAmount) coins, err := sdk.ParseCoins(amount) diff --git a/x/bank/client/rest/sendtx.go b/x/bank/client/rest/sendtx.go index 6ce472b6f..916aa08fe 100644 --- a/x/bank/client/rest/sendtx.go +++ b/x/bank/client/rest/sendtx.go @@ -1,7 +1,6 @@ package rest import ( - "encoding/hex" "encoding/json" "io/ioutil" "net/http" @@ -58,13 +57,12 @@ func SendRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, ctx context.CoreCont return } - bz, err := hex.DecodeString(address) + to, err := sdk.GetAccAddressHex(address) if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte(err.Error())) return } - to := sdk.Address(bz) // build message msg := client.BuildMsg(info.PubKey.Address(), to, m.Amount) diff --git a/x/bank/keeper.go b/x/bank/keeper.go index 6ef73c68b..b14da4d81 100644 --- a/x/bank/keeper.go +++ b/x/bank/keeper.go @@ -4,6 +4,7 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" ) const ( @@ -16,11 +17,11 @@ const ( // Keeper manages transfers between accounts type Keeper struct { - am sdk.AccountMapper + am auth.AccountMapper } // NewKeeper returns a new Keeper -func NewKeeper(am sdk.AccountMapper) Keeper { +func NewKeeper(am auth.AccountMapper) Keeper { return Keeper{am: am} } @@ -63,11 +64,11 @@ func (keeper Keeper) InputOutputCoins(ctx sdk.Context, inputs []Input, outputs [ // SendKeeper only allows transfers between accounts, without the possibility of creating coins type SendKeeper struct { - am sdk.AccountMapper + am auth.AccountMapper } // NewSendKeeper returns a new Keeper -func NewSendKeeper(am sdk.AccountMapper) SendKeeper { +func NewSendKeeper(am auth.AccountMapper) SendKeeper { return SendKeeper{am: am} } @@ -95,11 +96,11 @@ func (keeper SendKeeper) InputOutputCoins(ctx sdk.Context, inputs []Input, outpu // ViewKeeper only allows reading of balances type ViewKeeper struct { - am sdk.AccountMapper + am auth.AccountMapper } // NewViewKeeper returns a new Keeper -func NewViewKeeper(am sdk.AccountMapper) ViewKeeper { +func NewViewKeeper(am auth.AccountMapper) ViewKeeper { return ViewKeeper{am: am} } @@ -115,7 +116,7 @@ func (keeper ViewKeeper) HasCoins(ctx sdk.Context, addr sdk.Address, amt sdk.Coi //______________________________________________________________________________________________ -func getCoins(ctx sdk.Context, am sdk.AccountMapper, addr sdk.Address) sdk.Coins { +func getCoins(ctx sdk.Context, am auth.AccountMapper, addr sdk.Address) sdk.Coins { ctx.GasMeter().ConsumeGas(costGetCoins, "getCoins") acc := am.GetAccount(ctx, addr) if acc == nil { @@ -124,7 +125,7 @@ func getCoins(ctx sdk.Context, am sdk.AccountMapper, addr sdk.Address) sdk.Coins return acc.GetCoins() } -func setCoins(ctx sdk.Context, am sdk.AccountMapper, addr sdk.Address, amt sdk.Coins) sdk.Error { +func setCoins(ctx sdk.Context, am auth.AccountMapper, addr sdk.Address, amt sdk.Coins) sdk.Error { ctx.GasMeter().ConsumeGas(costSetCoins, "setCoins") acc := am.GetAccount(ctx, addr) if acc == nil { @@ -136,13 +137,13 @@ func setCoins(ctx sdk.Context, am sdk.AccountMapper, addr sdk.Address, amt sdk.C } // HasCoins returns whether or not an account has at least amt coins. -func hasCoins(ctx sdk.Context, am sdk.AccountMapper, addr sdk.Address, amt sdk.Coins) bool { +func hasCoins(ctx sdk.Context, am auth.AccountMapper, addr sdk.Address, amt sdk.Coins) bool { ctx.GasMeter().ConsumeGas(costHasCoins, "hasCoins") return getCoins(ctx, am, addr).IsGTE(amt) } // SubtractCoins subtracts amt from the coins at the addr. -func subtractCoins(ctx sdk.Context, am sdk.AccountMapper, addr sdk.Address, amt sdk.Coins) (sdk.Coins, sdk.Tags, sdk.Error) { +func subtractCoins(ctx sdk.Context, am auth.AccountMapper, addr sdk.Address, amt sdk.Coins) (sdk.Coins, sdk.Tags, sdk.Error) { ctx.GasMeter().ConsumeGas(costSubtractCoins, "subtractCoins") oldCoins := getCoins(ctx, am, addr) newCoins := oldCoins.Minus(amt) @@ -155,7 +156,7 @@ func subtractCoins(ctx sdk.Context, am sdk.AccountMapper, addr sdk.Address, amt } // AddCoins adds amt to the coins at the addr. -func addCoins(ctx sdk.Context, am sdk.AccountMapper, addr sdk.Address, amt sdk.Coins) (sdk.Coins, sdk.Tags, sdk.Error) { +func addCoins(ctx sdk.Context, am auth.AccountMapper, addr sdk.Address, amt sdk.Coins) (sdk.Coins, sdk.Tags, sdk.Error) { ctx.GasMeter().ConsumeGas(costAddCoins, "addCoins") oldCoins := getCoins(ctx, am, addr) newCoins := oldCoins.Plus(amt) @@ -169,7 +170,7 @@ func addCoins(ctx sdk.Context, am sdk.AccountMapper, addr sdk.Address, amt sdk.C // SendCoins moves coins from one account to another // NOTE: Make sure to revert state changes from tx on error -func sendCoins(ctx sdk.Context, am sdk.AccountMapper, fromAddr sdk.Address, toAddr sdk.Address, amt sdk.Coins) (sdk.Tags, sdk.Error) { +func sendCoins(ctx sdk.Context, am auth.AccountMapper, fromAddr sdk.Address, toAddr sdk.Address, amt sdk.Coins) (sdk.Tags, sdk.Error) { _, subTags, err := subtractCoins(ctx, am, fromAddr, amt) if err != nil { return nil, err @@ -185,7 +186,7 @@ func sendCoins(ctx sdk.Context, am sdk.AccountMapper, fromAddr sdk.Address, toAd // InputOutputCoins handles a list of inputs and outputs // NOTE: Make sure to revert state changes from tx on error -func inputOutputCoins(ctx sdk.Context, am sdk.AccountMapper, inputs []Input, outputs []Output) (sdk.Tags, sdk.Error) { +func inputOutputCoins(ctx sdk.Context, am auth.AccountMapper, inputs []Input, outputs []Output) (sdk.Tags, sdk.Error) { allTags := sdk.EmptyTags() for _, in := range inputs { diff --git a/x/fee_distribution/keeper.go b/x/fee_distribution/keeper.go new file mode 100644 index 000000000..145071719 --- /dev/null +++ b/x/fee_distribution/keeper.go @@ -0,0 +1,91 @@ +package stake + +//// keeper of the staking store +//type Keeper struct { +//storeKey sdk.StoreKey +//cdc *wire.Codec +//coinKeeper bank.Keeper + +//// codespace +//codespace sdk.CodespaceType +//} + +//func NewKeeper(cdc *wire.Codec, key sdk.StoreKey, ck bank.Keeper, codespace sdk.CodespaceType) Keeper { +//keeper := Keeper{ +//storeKey: key, +//cdc: cdc, +//coinKeeper: ck, +//codespace: codespace, +//} +//return keeper +//} + +////_________________________________________________________________________ + +//// cummulative power of the non-absent prevotes +//func (k Keeper) GetTotalPrecommitVotingPower(ctx sdk.Context) sdk.Rat { +//store := ctx.KVStore(k.storeKey) + +//// get absent prevote indexes +//absents := ctx.AbsentValidators() + +//TotalPower := sdk.ZeroRat() +//i := int32(0) +//iterator := store.SubspaceIterator(ValidatorsBondedKey) +//for ; iterator.Valid(); iterator.Next() { + +//skip := false +//for j, absentIndex := range absents { +//if absentIndex > i { +//break +//} + +//// if non-voting validator found, skip adding its power +//if absentIndex == i { +//absents = append(absents[:j], absents[j+1:]...) // won't need again +//skip = true +//break +//} +//} +//if skip { +//continue +//} + +//bz := iterator.Value() +//var validator Validator +//k.cdc.MustUnmarshalBinary(bz, &validator) +//TotalPower = TotalPower.Add(validator.Power) +//i++ +//} +//iterator.Close() +//return TotalPower +//} + +////_______________________________________________________________________ + +//// XXX TODO trim functionality + +//// retrieve all the power changes which occur after a height +//func (k Keeper) GetPowerChangesAfterHeight(ctx sdk.Context, earliestHeight int64) (pcs []PowerChange) { +//store := ctx.KVStore(k.storeKey) + +//iterator := store.SubspaceIterator(PowerChangeKey) //smallest to largest +//for ; iterator.Valid(); iterator.Next() { +//pcBytes := iterator.Value() +//var pc PowerChange +//k.cdc.MustUnmarshalBinary(pcBytes, &pc) +//if pc.Height < earliestHeight { +//break +//} +//pcs = append(pcs, pc) +//} +//iterator.Close() +//return +//} + +//// set a power change +//func (k Keeper) setPowerChange(ctx sdk.Context, pc PowerChange) { +//store := ctx.KVStore(k.storeKey) +//b := k.cdc.MustMarshalBinary(pc) +//store.Set(GetPowerChangeKey(pc.Height), b) +//} diff --git a/x/fee_distribution/keeper_test.go b/x/fee_distribution/keeper_test.go new file mode 100644 index 000000000..4ad8180e1 --- /dev/null +++ b/x/fee_distribution/keeper_test.go @@ -0,0 +1,31 @@ +package stake + +//// test if is a gotValidator from the last update +//func TestGetTotalPrecommitVotingPower(t *testing.T) { +//ctx, _, keeper := createTestInput(t, false, 0) + +//amts := []int64{10000, 1000, 100, 10, 1} +//var candidatesIn [5]Candidate +//for i, amt := range amts { +//candidatesIn[i] = NewCandidate(addrVals[i], pks[i], Description{}) +//candidatesIn[i].BondedShares = sdk.NewRat(amt) +//candidatesIn[i].DelegatorShares = sdk.NewRat(amt) +//keeper.setCandidate(ctx, candidatesIn[i]) +//} + +//// test that an empty gotValidator set doesn't have any gotValidators +//gotValidators := keeper.GetValidators(ctx) +//assert.Equal(t, 5, len(gotValidators)) + +//totPow := keeper.GetTotalPrecommitVotingPower(ctx) +//exp := sdk.NewRat(11111) +//assert.True(t, exp.Equal(totPow), "exp %v, got %v", exp, totPow) + +//// set absent gotValidators to be the 1st and 3rd record sorted by pubKey address +//ctx = ctx.WithAbsentValidators([]int32{1, 3}) +//totPow = keeper.GetTotalPrecommitVotingPower(ctx) + +//// XXX verify that this order should infact exclude these two records +//exp = sdk.NewRat(11100) +//assert.True(t, exp.Equal(totPow), "exp %v, got %v", exp, totPow) +//} diff --git a/x/fee_distribution/movement.go b/x/fee_distribution/movement.go new file mode 100644 index 000000000..03c4de72c --- /dev/null +++ b/x/fee_distribution/movement.go @@ -0,0 +1,72 @@ +package stake + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// burn burn burn +func BurnFeeHandler(ctx sdk.Context, _ sdk.Tx, collectedFees sdk.Coins) {} + +//// Handle fee distribution to the validators and delegators +//func (k Keeper) FeeHandler(ctx sdk.Context, collectedFees sdk.Coins) { +//pool := k.GetPool(ctx) +//params := k.GetParams(ctx) + +//// XXX determine +//candidate := NewCandidate(addrs[0], pks[0], Description{}) + +//// calculate the proposer reward +//precommitPower := k.GetTotalPrecommitVotingPower(ctx) +//toProposer := coinsMulRat(collectedFees, (sdk.NewRat(1, 100).Add(sdk.NewRat(4, 100).Mul(precommitPower).Quo(pool.BondedShares)))) +//candidate.ProposerRewardPool = candidate.ProposerRewardPool.Plus(toProposer) + +//toReservePool := coinsMulRat(collectedFees, params.ReservePoolFee) +//pool.FeeReservePool = pool.FeeReservePool.Plus(toReservePool) + +//distributedReward := (collectedFees.Minus(toProposer)).Minus(toReservePool) +//pool.FeePool = pool.FeePool.Plus(distributedReward) +//pool.FeeSumReceived = pool.FeeSumReceived.Plus(distributedReward) +//pool.FeeRecent = distributedReward + +//// lastly update the FeeRecent term +//pool.FeeRecent = collectedFees + +//k.setPool(ctx, pool) +//} + +//func coinsMulRat(coins sdk.Coins, rat sdk.Rat) sdk.Coins { +//var res sdk.Coins +//for _, coin := range coins { +//coinMulAmt := rat.Mul(sdk.NewRat(coin.Amount)).Evaluate() +//coinMul := sdk.Coins{{coin.Denom, coinMulAmt}} +//res = res.Plus(coinMul) +//} +//return res +//} + +////____________________________________________________________________________- + +//// calculate adjustment changes for a candidate at a height +//func CalculateAdjustmentChange(candidate Candidate, pool Pool, denoms []string, height int64) (Candidate, Pool) { + +//heightRat := sdk.NewRat(height) +//lastHeightRat := sdk.NewRat(height - 1) +//candidateFeeCount := candidate.BondedShares.Mul(heightRat) +//poolFeeCount := pool.BondedShares.Mul(heightRat) + +//for i, denom := range denoms { +//poolFeeSumReceived := sdk.NewRat(pool.FeeSumReceived.AmountOf(denom)) +//poolFeeRecent := sdk.NewRat(pool.FeeRecent.AmountOf(denom)) +//// calculate simple and projected pools +//simplePool := candidateFeeCount.Quo(poolFeeCount).Mul(poolFeeSumReceived) +//calc1 := candidate.PrevBondedShares.Mul(lastHeightRat).Quo(pool.PrevBondedShares.Mul(lastHeightRat)).Mul(poolFeeRecent) +//calc2 := candidate.BondedShares.Quo(pool.BondedShares).Mul(poolFeeRecent) +//projectedPool := calc1.Add(calc2) + +//AdjustmentChange := simplePool.Sub(projectedPool) +//candidate.FeeAdjustments[i] = candidate.FeeAdjustments[i].Add(AdjustmentChange) +//pool.FeeAdjustments[i] = pool.FeeAdjustments[i].Add(AdjustmentChange) +//} + +//return candidate, pool +//} diff --git a/x/fee_distribution/types.go b/x/fee_distribution/types.go new file mode 100644 index 000000000..f9d4f905f --- /dev/null +++ b/x/fee_distribution/types.go @@ -0,0 +1,107 @@ +package stake + +//// GenesisState - all staking state that must be provided at genesis +//type GenesisState struct { +//Pool Pool `json:"pool"` +//Params Params `json:"params"` +//} + +//func NewGenesisState(pool Pool, params Params, candidates []Candidate, bonds []Delegation) GenesisState { +//return GenesisState{ +//Pool: pool, +//Params: params, +//} +//} + +//// get raw genesis raw message for testing +//func DefaultGenesisState() GenesisState { +//return GenesisState{ +//Pool: initialPool(), +//Params: defaultParams(), +//} +//} + +//// fee information for a validator +//type Validator struct { +//Adjustments []sdk.Rat `json:"fee_adjustments"` // XXX Adjustment factors for lazy fee accounting, couples with Params.BondDenoms +//PrevBondedShares sdk.Rat `json:"prev_bonded_shares"` // total shares of a global hold pools +//} + +////_________________________________________________________________________ + +//// Params defines the high level settings for staking +//type Params struct { +//FeeDenoms []string `json:"fee_denoms"` // accepted fee denoms +//ReservePoolFee sdk.Rat `json:"reserve_pool_fee"` // percent of fees which go to reserve pool +//} + +//func (p Params) equal(p2 Params) bool { +//return p.BondDenom == p2.BondDenom && +//p.ReservePoolFee.Equal(p2.ReservePoolFee) +//} + +//func defaultParams() Params { +//return Params{ +//FeeDenoms: []string{"steak"}, +//ReservePoolFee: sdk.NewRat(5, 100), +//} +//} + +////_________________________________________________________________________ + +//// Pool - dynamic parameters of the current state +//type Pool struct { +//FeeReservePool sdk.Coins `json:"fee_reserve_pool"` // XXX reserve pool of collected fees for use by governance +//FeePool sdk.Coins `json:"fee_pool"` // XXX fee pool for all the fee shares which have already been distributed +//FeeSumReceived sdk.Coins `json:"fee_sum_received"` // XXX sum of all fees received, post reserve pool `json:"fee_sum_received"` +//FeeRecent sdk.Coins `json:"fee_recent"` // XXX most recent fee collected +//FeeAdjustments []sdk.Rat `json:"fee_adjustments"` // XXX Adjustment factors for lazy fee accounting, couples with Params.BondDenoms +//PrevBondedShares sdk.Rat `json:"prev_bonded_shares"` // XXX last recorded bonded shares +//} + +//func (p Pool) equal(p2 Pool) bool { +//return p.FeeReservePool.IsEqual(p2.FeeReservePool) && +//p.FeePool.IsEqual(p2.FeePool) && +//p.FeeSumReceived.IsEqual(p2.FeeSumReceived) && +//p.FeeRecent.IsEqual(p2.FeeRecent) && +//sdk.RatsEqual(p.FeeAdjustments, p2.FeeAdjustments) && +//p.PrevBondedShares.Equal(p2.PrevBondedShares) +//} + +//// initial pool for testing +//func initialPool() Pool { +//return Pool{ +//FeeReservePool: sdk.Coins(nil), +//FeePool: sdk.Coins(nil), +//FeeSumReceived: sdk.Coins(nil), +//FeeRecent: sdk.Coins(nil), +//FeeAdjustments: []sdk.Rat{sdk.ZeroRat()}, +//PrevBondedShares: sdk.ZeroRat(), +//} +//} + +////_________________________________________________________________________ + +//// Used in calculation of fee shares, added to a queue for each block where a power change occures +//type PowerChange struct { +//Height int64 `json:"height"` // block height at change +//Power sdk.Rat `json:"power"` // total power at change +//PrevPower sdk.Rat `json:"prev_power"` // total power at previous height-1 +//FeesIn sdk.Coins `json:"fees_in"` // fees in at block height +//PrevFeePool sdk.Coins `json:"prev_fee_pool"` // total fees in at previous block height +//} + +////_________________________________________________________________________ +//// KEY MANAGEMENT + +//var ( +//// Keys for store prefixes +//PowerChangeKey = []byte{0x09} // prefix for power change object +//) + +//// get the key for the accumulated update validators +//func GetPowerChangeKey(height int64) []byte { +//heightBytes := make([]byte, binary.MaxVarintLen64) +//binary.BigEndian.PutUint64(heightBytes, ^uint64(height)) // invert height (older validators first) +//return append(PowerChangeKey, heightBytes...) +//} diff --git a/x/ibc/client/cli/relay.go b/x/ibc/client/cli/relay.go index 86e8b1dbf..caf96d60a 100644 --- a/x/ibc/client/cli/relay.go +++ b/x/ibc/client/cli/relay.go @@ -12,6 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" wire "github.com/cosmos/cosmos-sdk/wire" + "github.com/cosmos/cosmos-sdk/x/auth" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/cosmos/cosmos-sdk/x/ibc" ) @@ -27,9 +28,10 @@ const ( type relayCommander struct { cdc *wire.Codec address sdk.Address - decoder sdk.AccountDecoder + decoder auth.AccountDecoder mainStore string ibcStore string + accStore string logger log.Logger } @@ -41,6 +43,7 @@ func IBCRelayCmd(cdc *wire.Codec) *cobra.Command { decoder: authcmd.GetAccountDecoder(cdc), ibcStore: "ibc", mainStore: "main", + accStore: "acc", logger: log.NewTMLogger(log.NewSyncWriter(os.Stdout)), } @@ -157,17 +160,20 @@ func (c relayCommander) broadcastTx(seq int64, node string, tx []byte) error { } func (c relayCommander) getSequence(node string) int64 { - res, err := query(node, c.address, c.mainStore) + res, err := query(node, c.address, c.accStore) if err != nil { panic(err) } + if nil != res { + account, err := c.decoder(res) + if err != nil { + panic(err) + } - account, err := c.decoder(res) - if err != nil { - panic(err) + return account.GetSequence() } - return account.GetSequence() + return 0 } func (c relayCommander) refine(bz []byte, sequence int64, passphrase string) []byte { @@ -182,7 +188,7 @@ func (c relayCommander) refine(bz []byte, sequence int64, passphrase string) []b Sequence: sequence, } - ctx := context.NewCoreContextFromViper() + ctx := context.NewCoreContextFromViper().WithSequence(sequence) res, err := ctx.SignAndBuild(ctx.FromAddressName, passphrase, msg, c.cdc) if err != nil { panic(err) diff --git a/x/ibc/ibc_test.go b/x/ibc/ibc_test.go index 60cc59bad..e13df4f8d 100644 --- a/x/ibc/ibc_test.go +++ b/x/ibc/ibc_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/assert" abci "github.com/tendermint/abci/types" - "github.com/tendermint/go-crypto" + crypto "github.com/tendermint/go-crypto" dbm "github.com/tendermint/tmlibs/db" "github.com/tendermint/tmlibs/log" @@ -49,7 +49,7 @@ func makeCodec() *wire.Codec { cdc.RegisterConcrete(IBCReceiveMsg{}, "test/ibc/IBCReceiveMsg", nil) // Register AppAccount - cdc.RegisterInterface((*sdk.Account)(nil), nil) + cdc.RegisterInterface((*auth.Account)(nil), nil) cdc.RegisterConcrete(&auth.BaseAccount{}, "test/ibc/Account", nil) wire.RegisterCrypto(cdc) diff --git a/x/stake/client/cli/flags.go b/x/stake/client/cli/flags.go index 98ea3bcce..eea7e2031 100644 --- a/x/stake/client/cli/flags.go +++ b/x/stake/client/cli/flags.go @@ -7,7 +7,7 @@ import ( // nolint const ( FlagAddressDelegator = "address-delegator" - FlagAddressCandidate = "address-candidate" + FlagAddressValidator = "address-validator" FlagPubKey = "pubkey" FlagAmount = "amount" FlagShares = "shares" @@ -24,18 +24,18 @@ var ( fsAmount = flag.NewFlagSet("", flag.ContinueOnError) fsShares = flag.NewFlagSet("", flag.ContinueOnError) fsDescription = flag.NewFlagSet("", flag.ContinueOnError) - fsCandidate = flag.NewFlagSet("", flag.ContinueOnError) + fsValidator = flag.NewFlagSet("", flag.ContinueOnError) fsDelegator = flag.NewFlagSet("", flag.ContinueOnError) ) func init() { - fsPk.String(FlagPubKey, "", "Go-Amino encoded hex PubKey of the validator-candidate. For Ed25519 the go-amino prepend hex is 1624de6220") + fsPk.String(FlagPubKey, "", "Go-Amino encoded hex PubKey of the validator. For Ed25519 the go-amino prepend hex is 1624de6220") fsAmount.String(FlagAmount, "1steak", "Amount of coins to bond") fsShares.String(FlagShares, "", "Amount of shares to unbond, either in decimal or keyword MAX (ex. 1.23456789, 99, MAX)") - fsDescription.String(FlagMoniker, "", "validator-candidate name") + fsDescription.String(FlagMoniker, "", "validator name") fsDescription.String(FlagIdentity, "", "optional keybase signature") fsDescription.String(FlagWebsite, "", "optional website") fsDescription.String(FlagDetails, "", "optional details") - fsCandidate.String(FlagAddressCandidate, "", "hex address of the validator/candidate") + fsValidator.String(FlagAddressValidator, "", "hex address of the validator") fsDelegator.String(FlagAddressDelegator, "", "hex address of the delegator") } diff --git a/x/stake/client/cli/query.go b/x/stake/client/cli/query.go index 8a5a06a70..84ce9886a 100644 --- a/x/stake/client/cli/query.go +++ b/x/stake/client/cli/query.go @@ -1,13 +1,11 @@ package cli import ( - "encoding/hex" "fmt" "github.com/spf13/cobra" "github.com/spf13/viper" - - crypto "github.com/tendermint/go-crypto" + "github.com/tendermint/tmlibs/cli" "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" @@ -15,70 +13,91 @@ import ( "github.com/cosmos/cosmos-sdk/x/stake" ) -// get the command to query a candidate -func GetCmdQueryCandidate(storeName string, cdc *wire.Codec) *cobra.Command { +// get the command to query a validator +func GetCmdQueryValidator(storeName string, cdc *wire.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "candidate", - Short: "Query a validator-candidate account", + Use: "validator [owner-addr]", + Short: "Query a validator", + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - addr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate)) + addr, err := sdk.GetAccAddressBech32Cosmos(args[0]) if err != nil { return err } - - key := stake.GetCandidateKey(addr) + key := stake.GetValidatorKey(addr) ctx := context.NewCoreContextFromViper() res, err := ctx.Query(key, storeName) if err != nil { return err } + validator := new(stake.Validator) + cdc.MustUnmarshalBinary(res, validator) - // parse out the candidate - candidate := new(stake.Candidate) - cdc.MustUnmarshalBinary(res, candidate) - output, err := wire.MarshalJSONIndent(cdc, candidate) - if err != nil { - return err + switch viper.Get(cli.OutputFlag) { + case "text": + human, err := validator.HumanReadableString() + if err != nil { + return err + } + fmt.Println(human) + + case "json": + // parse out the validator + output, err := wire.MarshalJSONIndent(cdc, validator) + if err != nil { + return err + } + fmt.Println(string(output)) } - fmt.Println(string(output)) - return nil - // TODO output with proofs / machine parseable etc. + return nil }, } - cmd.Flags().AddFlagSet(fsCandidate) return cmd } -// get the command to query a candidate -func GetCmdQueryCandidates(storeName string, cdc *wire.Codec) *cobra.Command { +// get the command to query a validator +func GetCmdQueryValidators(storeName string, cdc *wire.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "candidates", - Short: "Query for all validator-candidate accounts", + Use: "validators", + Short: "Query for all validators", RunE: func(cmd *cobra.Command, args []string) error { - key := stake.CandidatesKey + key := stake.ValidatorsKey ctx := context.NewCoreContextFromViper() resKVs, err := ctx.QuerySubspace(cdc, key, storeName) if err != nil { return err } - // parse out the candidates - var candidates []stake.Candidate + // parse out the validators + var validators []stake.Validator for _, KV := range resKVs { - var candidate stake.Candidate - cdc.MustUnmarshalBinary(KV.Value, &candidate) - candidates = append(candidates, candidate) + var validator stake.Validator + cdc.MustUnmarshalBinary(KV.Value, &validator) + validators = append(validators, validator) } - output, err := wire.MarshalJSONIndent(cdc, candidates) - if err != nil { - return err + + switch viper.Get(cli.OutputFlag) { + case "text": + for _, validator := range validators { + resp, err := validator.HumanReadableString() + if err != nil { + return err + } + fmt.Println(resp) + } + case "json": + output, err := wire.MarshalJSONIndent(cdc, validators) + if err != nil { + return err + } + fmt.Println(string(output)) + return nil } - fmt.Println(string(output)) return nil // TODO output with proofs / machine parseable etc. @@ -87,25 +106,24 @@ func GetCmdQueryCandidates(storeName string, cdc *wire.Codec) *cobra.Command { return cmd } -// get the command to query a single delegator bond -func GetCmdQueryDelegatorBond(storeName string, cdc *wire.Codec) *cobra.Command { +// get the command to query a single delegation bond +func GetCmdQueryDelegation(storeName string, cdc *wire.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "delegator-bond", - Short: "Query a delegators bond based on address and candidate pubkey", + Use: "delegation", + Short: "Query a delegations bond based on address and validator address", RunE: func(cmd *cobra.Command, args []string) error { - addr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate)) + addr, err := sdk.GetAccAddressBech32Cosmos(viper.GetString(FlagAddressValidator)) if err != nil { return err } - bz, err := hex.DecodeString(viper.GetString(FlagAddressDelegator)) + delAddr, err := sdk.GetValAddressHex(viper.GetString(FlagAddressDelegator)) if err != nil { return err } - delegator := crypto.Address(bz) - key := stake.GetDelegatorBondKey(delegator, addr, cdc) + key := stake.GetDelegationKey(delAddr, addr, cdc) ctx := context.NewCoreContextFromViper() res, err := ctx.Query(key, storeName) if err != nil { @@ -113,51 +131,61 @@ func GetCmdQueryDelegatorBond(storeName string, cdc *wire.Codec) *cobra.Command } // parse out the bond - bond := new(stake.DelegatorBond) - cdc.MustUnmarshalBinary(res, bond) - output, err := wire.MarshalJSONIndent(cdc, bond) - if err != nil { - return err - } - fmt.Println(string(output)) - return nil + bond := new(stake.Delegation) - // TODO output with proofs / machine parseable etc. + switch viper.Get(cli.OutputFlag) { + case "text": + resp, err := bond.HumanReadableString() + if err != nil { + return err + } + fmt.Println(resp) + case "json": + cdc.MustUnmarshalBinary(res, bond) + output, err := wire.MarshalJSONIndent(cdc, bond) + if err != nil { + return err + } + fmt.Println(string(output)) + return nil + } + return nil }, } - cmd.Flags().AddFlagSet(fsCandidate) + cmd.Flags().AddFlagSet(fsValidator) cmd.Flags().AddFlagSet(fsDelegator) return cmd } -// get the command to query all the candidates bonded to a delegator -func GetCmdQueryDelegatorBonds(storeName string, cdc *wire.Codec) *cobra.Command { +// get the command to query all the validators bonded to a delegation +func GetCmdQueryDelegations(storeName string, cdc *wire.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "delegator-candidates", - Short: "Query all delegators bonds based on delegator-address", + Use: "delegations [delegator-addr]", + Short: "Query all delegations made from one delegator", + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - delegatorAddr, err := sdk.GetAddress(viper.GetString(FlagAddressDelegator)) + delegatorAddr, err := sdk.GetAccAddressBech32Cosmos(args[0]) if err != nil { return err } - key := stake.GetDelegatorBondsKey(delegatorAddr, cdc) + key := stake.GetDelegationsKey(delegatorAddr, cdc) ctx := context.NewCoreContextFromViper() resKVs, err := ctx.QuerySubspace(cdc, key, storeName) if err != nil { return err } - // parse out the candidates - var delegators []stake.DelegatorBond + // parse out the validators + var delegations []stake.Delegation for _, KV := range resKVs { - var delegator stake.DelegatorBond - cdc.MustUnmarshalBinary(KV.Value, &delegator) - delegators = append(delegators, delegator) + var delegation stake.Delegation + cdc.MustUnmarshalBinary(KV.Value, &delegation) + delegations = append(delegations, delegation) } - output, err := wire.MarshalJSONIndent(cdc, delegators) + output, err := wire.MarshalJSONIndent(cdc, delegations) if err != nil { return err } @@ -167,6 +195,5 @@ func GetCmdQueryDelegatorBonds(storeName string, cdc *wire.Codec) *cobra.Command // TODO output with proofs / machine parseable etc. }, } - cmd.Flags().AddFlagSet(fsDelegator) return cmd } diff --git a/x/stake/client/cli/tx.go b/x/stake/client/cli/tx.go index d4f97fd52..dc88bfc20 100644 --- a/x/stake/client/cli/tx.go +++ b/x/stake/client/cli/tx.go @@ -1,14 +1,11 @@ package cli import ( - "encoding/hex" "fmt" "github.com/spf13/cobra" "github.com/spf13/viper" - crypto "github.com/tendermint/go-crypto" - "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" @@ -19,8 +16,8 @@ import ( // create declare candidacy command func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "declare-candidacy", - Short: "create new validator-candidate account and delegate some coins to it", + Use: "create-validator", + Short: "create new validator initialized with a self-delegation to it", RunE: func(cmd *cobra.Command, args []string) error { ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc)) @@ -28,7 +25,7 @@ func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command { if err != nil { return err } - candidateAddr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate)) + validatorAddr, err := sdk.GetAccAddressBech32Cosmos(viper.GetString(FlagAddressValidator)) if err != nil { return err } @@ -37,17 +34,12 @@ func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command { if len(pkStr) == 0 { return fmt.Errorf("must use --pubkey flag") } - pkBytes, err := hex.DecodeString(pkStr) + pk, err := sdk.GetValPubKeyBech32Cosmos(pkStr) if err != nil { return err } - pk, err := crypto.PubKeyFromBytes(pkBytes) - if err != nil { - return err - } - if viper.GetString(FlagMoniker) == "" { - return fmt.Errorf("please enter a moniker for the validator-candidate using --moniker") + return fmt.Errorf("please enter a moniker for the validator using --moniker") } description := stake.Description{ Moniker: viper.GetString(FlagMoniker), @@ -55,7 +47,7 @@ func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command { Website: viper.GetString(FlagWebsite), Details: viper.GetString(FlagDetails), } - msg := stake.NewMsgDeclareCandidacy(candidateAddr, pk, amount, description) + msg := stake.NewMsgDeclareCandidacy(validatorAddr, pk, amount, description) // build and sign the transaction, then broadcast to Tendermint res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, msg, cdc) @@ -71,18 +63,18 @@ func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command { cmd.Flags().AddFlagSet(fsPk) cmd.Flags().AddFlagSet(fsAmount) cmd.Flags().AddFlagSet(fsDescription) - cmd.Flags().AddFlagSet(fsCandidate) + cmd.Flags().AddFlagSet(fsValidator) return cmd } // create edit candidacy command func GetCmdEditCandidacy(cdc *wire.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "edit-candidacy", - Short: "edit and existing validator-candidate account", + Use: "edit-validator", + Short: "edit and existing validator account", RunE: func(cmd *cobra.Command, args []string) error { - candidateAddr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate)) + validatorAddr, err := sdk.GetAccAddressBech32Cosmos(viper.GetString(FlagAddressValidator)) if err != nil { return err } @@ -92,7 +84,7 @@ func GetCmdEditCandidacy(cdc *wire.Codec) *cobra.Command { Website: viper.GetString(FlagWebsite), Details: viper.GetString(FlagDetails), } - msg := stake.NewMsgEditCandidacy(candidateAddr, description) + msg := stake.NewMsgEditCandidacy(validatorAddr, description) // build and sign the transaction, then broadcast to Tendermint ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc)) @@ -108,7 +100,7 @@ func GetCmdEditCandidacy(cdc *wire.Codec) *cobra.Command { } cmd.Flags().AddFlagSet(fsDescription) - cmd.Flags().AddFlagSet(fsCandidate) + cmd.Flags().AddFlagSet(fsValidator) return cmd } @@ -116,20 +108,20 @@ func GetCmdEditCandidacy(cdc *wire.Codec) *cobra.Command { func GetCmdDelegate(cdc *wire.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "delegate", - Short: "delegate coins to an existing validator/candidate", + Short: "delegate coins to an existing validator", RunE: func(cmd *cobra.Command, args []string) error { amount, err := sdk.ParseCoin(viper.GetString(FlagAmount)) if err != nil { return err } - delegatorAddr, err := sdk.GetAddress(viper.GetString(FlagAddressDelegator)) - candidateAddr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate)) + delegatorAddr, err := sdk.GetAccAddressBech32Cosmos(viper.GetString(FlagAddressDelegator)) + validatorAddr, err := sdk.GetAccAddressBech32Cosmos(viper.GetString(FlagAddressValidator)) if err != nil { return err } - msg := stake.NewMsgDelegate(delegatorAddr, candidateAddr, amount) + msg := stake.NewMsgDelegate(delegatorAddr, validatorAddr, amount) // build and sign the transaction, then broadcast to Tendermint ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc)) @@ -146,7 +138,7 @@ func GetCmdDelegate(cdc *wire.Codec) *cobra.Command { cmd.Flags().AddFlagSet(fsAmount) cmd.Flags().AddFlagSet(fsDelegator) - cmd.Flags().AddFlagSet(fsCandidate) + cmd.Flags().AddFlagSet(fsValidator) return cmd } @@ -154,7 +146,7 @@ func GetCmdDelegate(cdc *wire.Codec) *cobra.Command { func GetCmdUnbond(cdc *wire.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "unbond", - Short: "unbond coins from a validator/candidate", + Short: "unbond shares from a validator", RunE: func(cmd *cobra.Command, args []string) error { // check the shares before broadcasting @@ -171,13 +163,13 @@ func GetCmdUnbond(cdc *wire.Codec) *cobra.Command { } } - delegatorAddr, err := sdk.GetAddress(viper.GetString(FlagAddressDelegator)) - candidateAddr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate)) + delegatorAddr, err := sdk.GetAccAddressBech32Cosmos(viper.GetString(FlagAddressDelegator)) + validatorAddr, err := sdk.GetAccAddressBech32Cosmos(viper.GetString(FlagAddressValidator)) if err != nil { return err } - msg := stake.NewMsgUnbond(delegatorAddr, candidateAddr, sharesStr) + msg := stake.NewMsgUnbond(delegatorAddr, validatorAddr, sharesStr) // build and sign the transaction, then broadcast to Tendermint ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc)) @@ -194,6 +186,6 @@ func GetCmdUnbond(cdc *wire.Codec) *cobra.Command { cmd.Flags().AddFlagSet(fsShares) cmd.Flags().AddFlagSet(fsDelegator) - cmd.Flags().AddFlagSet(fsCandidate) + cmd.Flags().AddFlagSet(fsValidator) return cmd } diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index 8eb0e03ce..3e439c2b4 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -6,7 +6,6 @@ import ( "net/http" "github.com/gorilla/mux" - "github.com/tendermint/go-crypto/keys" "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" @@ -14,14 +13,21 @@ import ( "github.com/cosmos/cosmos-sdk/x/stake" ) -// RegisterRoutes - Central function to define routes that get registered by the main application -func RegisterRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec, kb keys.Keybase) { - r.HandleFunc("/stake/{delegator}/bonding_status/{candidate}", BondingStatusHandlerFn("stake", cdc, kb, ctx)).Methods("GET") +func registerQueryRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec) { + r.HandleFunc( + "/stake/{delegator}/bonding_status/{validator}", + bondingStatusHandlerFn(ctx, "stake", cdc), + ).Methods("GET") + r.HandleFunc( + "/stake/validators", + validatorsHandlerFn(ctx, "stake", cdc), + ).Methods("GET") } -// BondingStatusHandlerFn - http request handler to query delegator bonding status -func BondingStatusHandlerFn(storeName string, cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) http.HandlerFunc { +// http request handler to query delegator bonding status +func bondingStatusHandlerFn(ctx context.CoreContext, storeName string, cdc *wire.Codec) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + // read parameters vars := mux.Vars(r) delegator := vars["delegator"] @@ -41,9 +47,9 @@ func BondingStatusHandlerFn(storeName string, cdc *wire.Codec, kb keys.Keybase, w.Write([]byte(err.Error())) return } - candidateAddr := sdk.Address(bz) + validatorAddr := sdk.Address(bz) - key := stake.GetDelegatorBondKey(delegatorAddr, candidateAddr, cdc) + key := stake.GetDelegationKey(delegatorAddr, validatorAddr, cdc) res, err := ctx.Query(key, storeName) if err != nil { @@ -58,7 +64,7 @@ func BondingStatusHandlerFn(storeName string, cdc *wire.Codec, kb keys.Keybase, return } - var bond stake.DelegatorBond + var bond stake.Delegation err = cdc.UnmarshalBinary(res, &bond) if err != nil { w.WriteHeader(http.StatusInternalServerError) @@ -76,3 +82,43 @@ func BondingStatusHandlerFn(storeName string, cdc *wire.Codec, kb keys.Keybase, w.Write(output) } } + +// http request handler to query list of validators +func validatorsHandlerFn(ctx context.CoreContext, storeName string, cdc *wire.Codec) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + kvs, err := ctx.QuerySubspace(cdc, stake.ValidatorsKey, storeName) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(fmt.Sprintf("Couldn't query validators. Error: %s", err.Error()))) + return + } + + // the query will return empty if there are no validators + if len(kvs) == 0 { + w.WriteHeader(http.StatusNoContent) + return + } + + // parse out the validators + validators := make([]stake.Validator, len(kvs)) + for i, kv := range kvs { + var validator stake.Validator + err = cdc.UnmarshalBinary(kv.Value, &validator) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(fmt.Sprintf("Couldn't decode validator. Error: %s", err.Error()))) + return + } + validators[i] = validator + } + + output, err := cdc.MarshalJSON(validators) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + w.Write(output) + } +} diff --git a/x/stake/client/rest/rest.go b/x/stake/client/rest/rest.go new file mode 100644 index 000000000..1f3a2957d --- /dev/null +++ b/x/stake/client/rest/rest.go @@ -0,0 +1,15 @@ +package rest + +import ( + "github.com/gorilla/mux" + "github.com/tendermint/go-crypto/keys" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/wire" +) + +// RegisterRoutes registers staking-related REST handlers to a router +func RegisterRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec, kb keys.Keybase) { + registerQueryRoutes(ctx, r, cdc) + registerTxRoutes(ctx, r, cdc, kb) +} diff --git a/x/stake/client/rest/tx.go b/x/stake/client/rest/tx.go new file mode 100644 index 000000000..eaf206bf6 --- /dev/null +++ b/x/stake/client/rest/tx.go @@ -0,0 +1,120 @@ +package rest + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" + + "github.com/gorilla/mux" + "github.com/tendermint/go-crypto/keys" + ctypes "github.com/tendermint/tendermint/rpc/core/types" + + "github.com/cosmos/cosmos-sdk/client/context" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/wire" + "github.com/cosmos/cosmos-sdk/x/stake" +) + +func registerTxRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec, kb keys.Keybase) { + r.HandleFunc( + "/stake/delegations", + editDelegationsRequestHandlerFn(cdc, kb, ctx), + ).Methods("POST") +} + +type editDelegationsBody struct { + LocalAccountName string `json:"name"` + Password string `json:"password"` + ChainID string `json:"chain_id"` + Sequence int64 `json:"sequence"` + Delegate []stake.MsgDelegate `json:"delegate"` + Unbond []stake.MsgUnbond `json:"unbond"` +} + +func editDelegationsRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var m editDelegationsBody + body, err := ioutil.ReadAll(r.Body) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(err.Error())) + return + } + err = json.Unmarshal(body, &m) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(err.Error())) + return + } + + info, err := kb.Get(m.LocalAccountName) + if err != nil { + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte(err.Error())) + return + } + + // build messages + messages := make([]sdk.Msg, len(m.Delegate)+len(m.Unbond)) + i := 0 + for _, msg := range m.Delegate { + if !bytes.Equal(info.Address(), msg.DelegatorAddr) { + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte("Must use own delegator address")) + return + } + messages[i] = msg + i++ + } + for _, msg := range m.Unbond { + if !bytes.Equal(info.Address(), msg.DelegatorAddr) { + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte("Must use own delegator address")) + return + } + messages[i] = msg + i++ + } + + // sign messages + signedTxs := make([][]byte, len(messages[:])) + for i, msg := range messages { + // increment sequence for each message + ctx = ctx.WithSequence(m.Sequence) + m.Sequence++ + + txBytes, err := ctx.SignAndBuild(m.LocalAccountName, m.Password, msg, cdc) + if err != nil { + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte(err.Error())) + return + } + + signedTxs[i] = txBytes + } + + // send + // XXX the operation might not be atomic if a tx fails + // should we have a sdk.MultiMsg type to make sending atomic? + results := make([]*ctypes.ResultBroadcastTxCommit, len(signedTxs[:])) + for i, txBytes := range signedTxs { + res, err := ctx.BroadcastTx(txBytes) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + results[i] = res + } + + output, err := json.MarshalIndent(results[:], "", " ") + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + w.Write(output) + } +} diff --git a/x/stake/delegation.go b/x/stake/delegation.go new file mode 100644 index 000000000..6877034d7 --- /dev/null +++ b/x/stake/delegation.go @@ -0,0 +1,54 @@ +package stake + +import ( + "bytes" + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// Delegation represents the bond with tokens held by an account. It is +// owned by one delegator, and is associated with the voting power of one +// pubKey. +// TODO better way of managing space +type Delegation struct { + DelegatorAddr sdk.Address `json:"delegator_addr"` + ValidatorAddr sdk.Address `json:"validator_addr"` + Shares sdk.Rat `json:"shares"` + Height int64 `json:"height"` // Last height bond updated +} + +func (b Delegation) equal(b2 Delegation) bool { + return bytes.Equal(b.DelegatorAddr, b2.DelegatorAddr) && + bytes.Equal(b.ValidatorAddr, b2.ValidatorAddr) && + b.Height == b2.Height && + b.Shares.Equal(b2.Shares) +} + +// ensure fulfills the sdk validator types +var _ sdk.Delegation = Delegation{} + +// nolint - for sdk.Delegation +func (b Delegation) GetDelegator() sdk.Address { return b.DelegatorAddr } +func (b Delegation) GetValidator() sdk.Address { return b.ValidatorAddr } +func (b Delegation) GetBondShares() sdk.Rat { return b.Shares } + +//Human Friendly pretty printer +func (b Delegation) HumanReadableString() (string, error) { + bechAcc, err := sdk.Bech32CosmosifyAcc(b.DelegatorAddr) + if err != nil { + return "", err + } + bechVal, err := sdk.Bech32CosmosifyAcc(b.ValidatorAddr) + if err != nil { + return "", err + } + resp := "Delegation \n" + resp += fmt.Sprintf("Delegator: %s\n", bechAcc) + resp += fmt.Sprintf("Validator: %s\n", bechVal) + resp += fmt.Sprintf("Shares: %s", b.Shares.String()) + resp += fmt.Sprintf("Height: %d", b.Height) + + return resp, nil + +} diff --git a/x/stake/errors.go b/x/stake/errors.go index 008d51bc9..83c38d528 100644 --- a/x/stake/errors.go +++ b/x/stake/errors.go @@ -14,9 +14,8 @@ const ( // Gaia errors reserve 200 ~ 299. CodeInvalidValidator CodeType = 201 - CodeInvalidCandidate CodeType = 202 - CodeInvalidBond CodeType = 203 - CodeInvalidInput CodeType = 204 + CodeInvalidBond CodeType = 202 + CodeInvalidInput CodeType = 203 CodeUnauthorized CodeType = sdk.CodeUnauthorized CodeInternal CodeType = sdk.CodeInternal CodeUnknownRequest CodeType = sdk.CodeUnknownRequest @@ -27,8 +26,6 @@ func codeToDefaultMsg(code CodeType) string { switch code { case CodeInvalidValidator: return "Invalid Validator" - case CodeInvalidCandidate: - return "Invalid Candidate" case CodeInvalidBond: return "Invalid Bond" case CodeInvalidInput: @@ -50,8 +47,8 @@ func codeToDefaultMsg(code CodeType) string { func ErrNotEnoughBondShares(codespace sdk.CodespaceType, shares string) sdk.Error { return newError(codespace, CodeInvalidBond, fmt.Sprintf("not enough shares only have %v", shares)) } -func ErrCandidateEmpty(codespace sdk.CodespaceType) sdk.Error { - return newError(codespace, CodeInvalidValidator, "Cannot bond to an empty candidate") +func ErrValidatorEmpty(codespace sdk.CodespaceType) sdk.Error { + return newError(codespace, CodeInvalidValidator, "Cannot bond to an empty validator") } func ErrBadBondingDenom(codespace sdk.CodespaceType) sdk.Error { return newError(codespace, CodeInvalidBond, "Invalid coin denomination") @@ -71,16 +68,13 @@ func ErrCommissionHuge(codespace sdk.CodespaceType) sdk.Error { func ErrBadValidatorAddr(codespace sdk.CodespaceType) sdk.Error { return newError(codespace, CodeInvalidValidator, "Validator does not exist for that address") } -func ErrBadCandidateAddr(codespace sdk.CodespaceType) sdk.Error { - return newError(codespace, CodeInvalidValidator, "Candidate does not exist for that address") -} func ErrBadDelegatorAddr(codespace sdk.CodespaceType) sdk.Error { return newError(codespace, CodeInvalidValidator, "Delegator does not exist for that address") } -func ErrCandidateExistsAddr(codespace sdk.CodespaceType) sdk.Error { - return newError(codespace, CodeInvalidValidator, "Candidate already exist, cannot re-declare candidacy") +func ErrValidatorExistsAddr(codespace sdk.CodespaceType) sdk.Error { + return newError(codespace, CodeInvalidValidator, "Validator already exist, cannot re-declare candidacy") } -func ErrCandidateRevoked(codespace sdk.CodespaceType) sdk.Error { +func ErrValidatorRevoked(codespace sdk.CodespaceType) sdk.Error { return newError(codespace, CodeInvalidValidator, "Candidacy for this address is currently revoked") } func ErrMissingSignature(codespace sdk.CodespaceType) sdk.Error { @@ -89,7 +83,7 @@ func ErrMissingSignature(codespace sdk.CodespaceType) sdk.Error { func ErrBondNotNominated(codespace sdk.CodespaceType) sdk.Error { return newError(codespace, CodeInvalidValidator, "Cannot bond to non-nominated account") } -func ErrNoCandidateForAddress(codespace sdk.CodespaceType) sdk.Error { +func ErrNoValidatorForAddress(codespace sdk.CodespaceType) sdk.Error { return newError(codespace, CodeInvalidValidator, "Validator does not exist for that address") } func ErrNoDelegatorForAddress(codespace sdk.CodespaceType) sdk.Error { diff --git a/x/stake/fee_distribution.go b/x/stake/fee_distribution.go deleted file mode 100644 index 940a66e38..000000000 --- a/x/stake/fee_distribution.go +++ /dev/null @@ -1,10 +0,0 @@ -package stake - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" -) - -// Handle fee distribution to the validators and delegators -func FeeHandler(ctx sdk.Context, tx sdk.Tx, fee sdk.Coins) { - -} diff --git a/x/stake/genesis.go b/x/stake/genesis.go new file mode 100644 index 000000000..be8d0dbe4 --- /dev/null +++ b/x/stake/genesis.go @@ -0,0 +1,54 @@ +package stake + +import sdk "github.com/cosmos/cosmos-sdk/types" + +// GenesisState - all staking state that must be provided at genesis +type GenesisState struct { + Pool Pool `json:"pool"` + Params Params `json:"params"` + Validators []Validator `json:"validators"` + Bonds []Delegation `json:"bonds"` +} + +func NewGenesisState(pool Pool, params Params, validators []Validator, bonds []Delegation) GenesisState { + return GenesisState{ + Pool: pool, + Params: params, + Validators: validators, + Bonds: bonds, + } +} + +// get raw genesis raw message for testing +func DefaultGenesisState() GenesisState { + return GenesisState{ + Pool: InitialPool(), + Params: DefaultParams(), + } +} + +// InitGenesis - store genesis parameters +func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) { + k.setPool(ctx, data.Pool) + k.setNewParams(ctx, data.Params) + for _, validator := range data.Validators { + k.updateValidator(ctx, validator) + } + for _, bond := range data.Bonds { + k.setDelegation(ctx, bond) + } +} + +// WriteGenesis - output genesis parameters +func WriteGenesis(ctx sdk.Context, k Keeper) GenesisState { + pool := k.GetPool(ctx) + params := k.GetParams(ctx) + validators := k.getAllValidators(ctx) + bonds := k.getAllDelegations(ctx) + return GenesisState{ + pool, + params, + validators, + bonds, + } +} diff --git a/x/stake/handler.go b/x/stake/handler.go index 8d3bbb8b8..53653557c 100644 --- a/x/stake/handler.go +++ b/x/stake/handler.go @@ -7,16 +7,6 @@ import ( abci "github.com/tendermint/abci/types" ) -//nolint -const ( - GasDeclareCandidacy int64 = 20 - GasEditCandidacy int64 = 20 - GasDelegate int64 = 20 - GasUnbond int64 = 20 -) - -//_______________________________________________________________________ - func NewHandler(k Keeper) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { // NOTE msg already has validate basic run @@ -35,8 +25,6 @@ func NewHandler(k Keeper) sdk.Handler { } } -//_____________________________________________________________________ - // NewEndBlocker generates sdk.EndBlocker // Performs tick functionality func NewEndBlocker(k Keeper) sdk.EndBlocker { @@ -48,60 +36,35 @@ func NewEndBlocker(k Keeper) sdk.EndBlocker { //_____________________________________________________________________ -// InitGenesis - store genesis parameters -func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) { - k.setPool(ctx, data.Pool) - k.setParams(ctx, data.Params) - for _, candidate := range data.Candidates { - k.setCandidate(ctx, candidate) - } - for _, bond := range data.Bonds { - k.setDelegatorBond(ctx, bond) - } -} - -// WriteGenesis - output genesis parameters -func WriteGenesis(ctx sdk.Context, k Keeper) GenesisState { - pool := k.GetPool(ctx) - params := k.GetParams(ctx) - candidates := k.GetCandidates(ctx, 32767) - bonds := k.getBonds(ctx, 32767) - return GenesisState{ - pool, - params, - candidates, - bonds, - } -} - -//_____________________________________________________________________ - // These functions assume everything has been authenticated, // now we just perform action and save func handleMsgDeclareCandidacy(ctx sdk.Context, msg MsgDeclareCandidacy, k Keeper) sdk.Result { // check to see if the pubkey or sender has been registered before - _, found := k.GetCandidate(ctx, msg.CandidateAddr) + _, found := k.GetValidator(ctx, msg.ValidatorAddr) if found { - return ErrCandidateExistsAddr(k.codespace).Result() + return ErrValidatorExistsAddr(k.codespace).Result() } if msg.Bond.Denom != k.GetParams(ctx).BondDenom { return ErrBadBondingDenom(k.codespace).Result() } if ctx.IsCheckTx() { - return sdk.Result{ - GasUsed: GasDeclareCandidacy, - } + return sdk.Result{} } - candidate := NewCandidate(msg.CandidateAddr, msg.PubKey, msg.Description) - k.setCandidate(ctx, candidate) - tags := sdk.NewTags("action", []byte("declareCandidacy"), "candidate", msg.CandidateAddr.Bytes(), "moniker", []byte(msg.Description.Moniker), "identity", []byte(msg.Description.Identity)) + validator := NewValidator(msg.ValidatorAddr, msg.PubKey, msg.Description) + k.setValidator(ctx, validator) + tags := sdk.NewTags( + "action", []byte("declareCandidacy"), + "validator", msg.ValidatorAddr.Bytes(), + "moniker", []byte(msg.Description.Moniker), + "identity", []byte(msg.Description.Identity), + ) // move coins from the msg.Address account to a (self-bond) delegator account - // the candidate account and global shares are updated within here - delegateTags, err := delegate(ctx, k, msg.CandidateAddr, msg.Bond, candidate) + // the validator account and global shares are updated within here + delegateTags, err := delegate(ctx, k, msg.ValidatorAddr, msg.Bond, validator) if err != nil { return err.Result() } @@ -113,26 +76,29 @@ func handleMsgDeclareCandidacy(ctx sdk.Context, msg MsgDeclareCandidacy, k Keepe func handleMsgEditCandidacy(ctx sdk.Context, msg MsgEditCandidacy, k Keeper) sdk.Result { - // candidate must already be registered - candidate, found := k.GetCandidate(ctx, msg.CandidateAddr) + // validator must already be registered + validator, found := k.GetValidator(ctx, msg.ValidatorAddr) if !found { - return ErrBadCandidateAddr(k.codespace).Result() + return ErrBadValidatorAddr(k.codespace).Result() } if ctx.IsCheckTx() { - return sdk.Result{ - GasUsed: GasEditCandidacy, - } + return sdk.Result{} } // XXX move to types // replace all editable fields (clients should autofill existing values) - candidate.Description.Moniker = msg.Description.Moniker - candidate.Description.Identity = msg.Description.Identity - candidate.Description.Website = msg.Description.Website - candidate.Description.Details = msg.Description.Details + validator.Description.Moniker = msg.Description.Moniker + validator.Description.Identity = msg.Description.Identity + validator.Description.Website = msg.Description.Website + validator.Description.Details = msg.Description.Details - k.setCandidate(ctx, candidate) - tags := sdk.NewTags("action", []byte("editCandidacy"), "candidate", msg.CandidateAddr.Bytes(), "moniker", []byte(msg.Description.Moniker), "identity", []byte(msg.Description.Identity)) + k.updateValidator(ctx, validator) + tags := sdk.NewTags( + "action", []byte("editCandidacy"), + "validator", msg.ValidatorAddr.Bytes(), + "moniker", []byte(msg.Description.Moniker), + "identity", []byte(msg.Description.Identity), + ) return sdk.Result{ Tags: tags, } @@ -140,22 +106,20 @@ func handleMsgEditCandidacy(ctx sdk.Context, msg MsgEditCandidacy, k Keeper) sdk func handleMsgDelegate(ctx sdk.Context, msg MsgDelegate, k Keeper) sdk.Result { - candidate, found := k.GetCandidate(ctx, msg.CandidateAddr) + validator, found := k.GetValidator(ctx, msg.ValidatorAddr) if !found { - return ErrBadCandidateAddr(k.codespace).Result() + return ErrBadValidatorAddr(k.codespace).Result() } if msg.Bond.Denom != k.GetParams(ctx).BondDenom { return ErrBadBondingDenom(k.codespace).Result() } - if candidate.Status == Revoked { - return ErrCandidateRevoked(k.codespace).Result() + if validator.Revoked == true { + return ErrValidatorRevoked(k.codespace).Result() } if ctx.IsCheckTx() { - return sdk.Result{ - GasUsed: GasDelegate, - } + return sdk.Result{} } - tags, err := delegate(ctx, k, msg.DelegatorAddr, msg.Bond, candidate) + tags, err := delegate(ctx, k, msg.DelegatorAddr, msg.Bond, validator) if err != nil { return err.Result() } @@ -166,14 +130,14 @@ func handleMsgDelegate(ctx sdk.Context, msg MsgDelegate, k Keeper) sdk.Result { // common functionality between handlers func delegate(ctx sdk.Context, k Keeper, delegatorAddr sdk.Address, - bondAmt sdk.Coin, candidate Candidate) (sdk.Tags, sdk.Error) { + bondAmt sdk.Coin, validator Validator) (sdk.Tags, sdk.Error) { // Get or create the delegator bond - bond, found := k.GetDelegatorBond(ctx, delegatorAddr, candidate.Address) + bond, found := k.GetDelegation(ctx, delegatorAddr, validator.Owner) if !found { - bond = DelegatorBond{ + bond = Delegation{ DelegatorAddr: delegatorAddr, - CandidateAddr: candidate.Address, + ValidatorAddr: validator.Owner, Shares: sdk.ZeroRat(), } } @@ -184,31 +148,28 @@ func delegate(ctx sdk.Context, k Keeper, delegatorAddr sdk.Address, if err != nil { return nil, err } - pool, candidate, newShares := pool.candidateAddTokens(candidate, bondAmt.Amount) + validator, pool, newShares := validator.addTokensFromDel(pool, bondAmt.Amount) bond.Shares = bond.Shares.Add(newShares) // Update bond height bond.Height = ctx.BlockHeight() - k.setDelegatorBond(ctx, bond) - k.setCandidate(ctx, candidate) k.setPool(ctx, pool) - tags := sdk.NewTags("action", []byte("delegate"), "delegator", delegatorAddr.Bytes(), "candidate", candidate.Address.Bytes()) + k.setDelegation(ctx, bond) + k.updateValidator(ctx, validator) + tags := sdk.NewTags("action", []byte("delegate"), "delegator", delegatorAddr.Bytes(), "validator", validator.Owner.Bytes()) return tags, nil } func handleMsgUnbond(ctx sdk.Context, msg MsgUnbond, k Keeper) sdk.Result { // check if bond has any shares in it unbond - bond, found := k.GetDelegatorBond(ctx, msg.DelegatorAddr, msg.CandidateAddr) + bond, found := k.GetDelegation(ctx, msg.DelegatorAddr, msg.ValidatorAddr) if !found { return ErrNoDelegatorForAddress(k.codespace).Result() } - if !bond.Shares.GT(sdk.ZeroRat()) { // bond shares < msg shares - return ErrInsufficientFunds(k.codespace).Result() - } - var shares sdk.Rat + var delShares sdk.Rat // test that there are enough shares to unbond if msg.Shares == "MAX" { @@ -217,81 +178,71 @@ func handleMsgUnbond(ctx sdk.Context, msg MsgUnbond, k Keeper) sdk.Result { } } else { var err sdk.Error - shares, err = sdk.NewRatFromDecimal(msg.Shares) + delShares, err = sdk.NewRatFromDecimal(msg.Shares) if err != nil { return err.Result() } - if bond.Shares.LT(shares) { + if bond.Shares.LT(delShares) { return ErrNotEnoughBondShares(k.codespace, msg.Shares).Result() } } - // get candidate - candidate, found := k.GetCandidate(ctx, msg.CandidateAddr) + // get validator + validator, found := k.GetValidator(ctx, msg.ValidatorAddr) if !found { - return ErrNoCandidateForAddress(k.codespace).Result() + return ErrNoValidatorForAddress(k.codespace).Result() } if ctx.IsCheckTx() { - return sdk.Result{ - GasUsed: GasUnbond, - } + return sdk.Result{} } // retrieve the amount of bonds to remove (TODO remove redundancy already serialized) if msg.Shares == "MAX" { - shares = bond.Shares + delShares = bond.Shares } // subtract bond tokens from delegator bond - bond.Shares = bond.Shares.Sub(shares) + bond.Shares = bond.Shares.Sub(delShares) // remove the bond revokeCandidacy := false if bond.Shares.IsZero() { - // if the bond is the owner of the candidate then + // if the bond is the owner of the validator then // trigger a revoke candidacy - if bytes.Equal(bond.DelegatorAddr, candidate.Address) && - candidate.Status != Revoked { + if bytes.Equal(bond.DelegatorAddr, validator.Owner) && + validator.Revoked == false { revokeCandidacy = true } - k.removeDelegatorBond(ctx, bond) + k.removeDelegation(ctx, bond) } else { // Update bond height bond.Height = ctx.BlockHeight() - k.setDelegatorBond(ctx, bond) + k.setDelegation(ctx, bond) } // Add the coins - p := k.GetPool(ctx) - p, candidate, returnAmount := p.candidateRemoveShares(candidate, shares) + pool := k.GetPool(ctx) + validator, pool, returnAmount := validator.removeDelShares(pool, delShares) + k.setPool(ctx, pool) returnCoins := sdk.Coins{{k.GetParams(ctx).BondDenom, returnAmount}} k.coinKeeper.AddCoins(ctx, bond.DelegatorAddr, returnCoins) ///////////////////////////////////// - - // revoke candidate if necessary + // revoke validator if necessary if revokeCandidacy { - - // change the share types to unbonded if they were not already - if candidate.Status == Bonded { - p, candidate = p.bondedToUnbondedPool(candidate) - } - - // lastly update the status - candidate.Status = Revoked + validator.Revoked = true } - // deduct shares from the candidate - if candidate.Liabilities.IsZero() { - k.removeCandidate(ctx, candidate.Address) - } else { - k.setCandidate(ctx, candidate) + validator = k.updateValidator(ctx, validator) + + if validator.DelegatorShares.IsZero() { + k.removeValidator(ctx, validator.Owner) } - k.setPool(ctx, p) - tags := sdk.NewTags("action", []byte("unbond"), "delegator", msg.DelegatorAddr.Bytes(), "candidate", msg.CandidateAddr.Bytes()) + + tags := sdk.NewTags("action", []byte("unbond"), "delegator", msg.DelegatorAddr.Bytes(), "validator", msg.ValidatorAddr.Bytes()) return sdk.Result{ Tags: tags, } diff --git a/x/stake/handler_test.go b/x/stake/handler_test.go index b4db952f0..19848e8e6 100644 --- a/x/stake/handler_test.go +++ b/x/stake/handler_test.go @@ -17,16 +17,16 @@ import ( func newTestMsgDeclareCandidacy(address sdk.Address, pubKey crypto.PubKey, amt int64) MsgDeclareCandidacy { return MsgDeclareCandidacy{ Description: Description{}, - CandidateAddr: address, - Bond: sdk.Coin{"steak", amt}, + ValidatorAddr: address, PubKey: pubKey, + Bond: sdk.Coin{"steak", amt}, } } -func newTestMsgDelegate(delegatorAddr, candidateAddr sdk.Address, amt int64) MsgDelegate { +func newTestMsgDelegate(delegatorAddr, validatorAddr sdk.Address, amt int64) MsgDelegate { return MsgDelegate{ DelegatorAddr: delegatorAddr, - CandidateAddr: candidateAddr, + ValidatorAddr: validatorAddr, Bond: sdk.Coin{"steak", amt}, } } @@ -36,21 +36,21 @@ func newTestMsgDelegate(delegatorAddr, candidateAddr sdk.Address, amt int64) Msg func TestDuplicatesMsgDeclareCandidacy(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 1000) - candidateAddr := addrs[0] + validatorAddr := addrs[0] pk := pks[0] - msgDeclareCandidacy := newTestMsgDeclareCandidacy(candidateAddr, pk, 10) + msgDeclareCandidacy := newTestMsgDeclareCandidacy(validatorAddr, pk, 10) got := handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper) assert.True(t, got.IsOK(), "%v", got) - candidate, found := keeper.GetCandidate(ctx, candidateAddr) + validator, found := keeper.GetValidator(ctx, validatorAddr) require.True(t, found) - assert.Equal(t, Unbonded, candidate.Status) - assert.Equal(t, candidateAddr, candidate.Address) - assert.Equal(t, pk, candidate.PubKey) - assert.Equal(t, sdk.NewRat(10), candidate.Assets) - assert.Equal(t, sdk.NewRat(10), candidate.Liabilities) - assert.Equal(t, Description{}, candidate.Description) + assert.Equal(t, sdk.Bonded, validator.Status()) + assert.Equal(t, validatorAddr, validator.Owner) + assert.Equal(t, pk, validator.PubKey) + assert.Equal(t, sdk.NewRat(10), validator.PoolShares.Bonded()) + assert.Equal(t, sdk.NewRat(10), validator.DelegatorShares) + assert.Equal(t, Description{}, validator.Description) - // one candidate cannot bond twice + // one validator cannot bond twice msgDeclareCandidacy.PubKey = pks[1] got = handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper) assert.False(t, got.IsOK(), "%v", got) @@ -62,20 +62,34 @@ func TestIncrementsMsgDelegate(t *testing.T) { params := keeper.GetParams(ctx) bondAmount := int64(10) - candidateAddr, delegatorAddr := addrs[0], addrs[1] + validatorAddr, delegatorAddr := addrs[0], addrs[1] // first declare candidacy - msgDeclareCandidacy := newTestMsgDeclareCandidacy(candidateAddr, pks[0], bondAmount) + msgDeclareCandidacy := newTestMsgDeclareCandidacy(validatorAddr, pks[0], bondAmount) got := handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper) assert.True(t, got.IsOK(), "expected declare candidacy msg to be ok, got %v", got) - candidate, found := keeper.GetCandidate(ctx, candidateAddr) + validator, found := keeper.GetValidator(ctx, validatorAddr) require.True(t, found) - assert.Equal(t, bondAmount, candidate.Liabilities.Evaluate()) - assert.Equal(t, bondAmount, candidate.Assets.Evaluate()) + require.Equal(t, sdk.Bonded, validator.Status()) + assert.Equal(t, bondAmount, validator.DelegatorShares.Evaluate()) + assert.Equal(t, bondAmount, validator.PoolShares.Bonded().Evaluate(), "validator: %v", validator) + + _, found = keeper.GetDelegation(ctx, delegatorAddr, validatorAddr) + require.False(t, found) + + bond, found := keeper.GetDelegation(ctx, validatorAddr, validatorAddr) + require.True(t, found) + assert.Equal(t, bondAmount, bond.Shares.Evaluate()) + + pool := keeper.GetPool(ctx) + exRate := validator.DelegatorShareExRate(pool) + require.True(t, exRate.Equal(sdk.OneRat()), "expected exRate 1 got %v", exRate) + assert.Equal(t, bondAmount, pool.BondedShares.Evaluate()) + assert.Equal(t, bondAmount, pool.BondedTokens) // just send the same msgbond multiple times - msgDelegate := newTestMsgDelegate(delegatorAddr, candidateAddr, bondAmount) + msgDelegate := newTestMsgDelegate(delegatorAddr, validatorAddr, bondAmount) for i := 0; i < 5; i++ { ctx = ctx.WithBlockHeight(int64(i)) @@ -84,30 +98,34 @@ func TestIncrementsMsgDelegate(t *testing.T) { require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got) //Check that the accounts and the bond account have the appropriate values - candidate, found := keeper.GetCandidate(ctx, candidateAddr) + validator, found := keeper.GetValidator(ctx, validatorAddr) require.True(t, found) - bond, found := keeper.GetDelegatorBond(ctx, delegatorAddr, candidateAddr) + bond, found := keeper.GetDelegation(ctx, delegatorAddr, validatorAddr) require.True(t, found) + pool := keeper.GetPool(ctx) + exRate := validator.DelegatorShareExRate(pool) + require.True(t, exRate.Equal(sdk.OneRat()), "expected exRate 1 got %v, i = %v", exRate, i) + expBond := int64(i+1) * bondAmount - expLiabilities := int64(i+2) * bondAmount // (1 self delegation) + expDelegatorShares := int64(i+2) * bondAmount // (1 self delegation) expDelegatorAcc := initBond - expBond require.Equal(t, bond.Height, int64(i), "Incorrect bond height") gotBond := bond.Shares.Evaluate() - gotLiabilities := candidate.Liabilities.Evaluate() + gotDelegatorShares := validator.DelegatorShares.Evaluate() gotDelegatorAcc := accMapper.GetAccount(ctx, delegatorAddr).GetCoins().AmountOf(params.BondDenom) require.Equal(t, expBond, gotBond, - "i: %v\nexpBond: %v\ngotBond: %v\ncandidate: %v\nbond: %v\n", - i, expBond, gotBond, candidate, bond) - require.Equal(t, expLiabilities, gotLiabilities, - "i: %v\nexpLiabilities: %v\ngotLiabilities: %v\ncandidate: %v\nbond: %v\n", - i, expLiabilities, gotLiabilities, candidate, bond) + "i: %v\nexpBond: %v\ngotBond: %v\nvalidator: %v\nbond: %v\n", + i, expBond, gotBond, validator, bond) + require.Equal(t, expDelegatorShares, gotDelegatorShares, + "i: %v\nexpDelegatorShares: %v\ngotDelegatorShares: %v\nvalidator: %v\nbond: %v\n", + i, expDelegatorShares, gotDelegatorShares, validator, bond) require.Equal(t, expDelegatorAcc, gotDelegatorAcc, - "i: %v\nexpDelegatorAcc: %v\ngotDelegatorAcc: %v\ncandidate: %v\nbond: %v\n", - i, expDelegatorAcc, gotDelegatorAcc, candidate, bond) + "i: %v\nexpDelegatorAcc: %v\ngotDelegatorAcc: %v\nvalidator: %v\nbond: %v\n", + i, expDelegatorAcc, gotDelegatorAcc, validator, bond) } } @@ -117,53 +135,53 @@ func TestIncrementsMsgUnbond(t *testing.T) { params := keeper.GetParams(ctx) // declare candidacy, delegate - candidateAddr, delegatorAddr := addrs[0], addrs[1] + validatorAddr, delegatorAddr := addrs[0], addrs[1] - msgDeclareCandidacy := newTestMsgDeclareCandidacy(candidateAddr, pks[0], initBond) + msgDeclareCandidacy := newTestMsgDeclareCandidacy(validatorAddr, pks[0], initBond) got := handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper) assert.True(t, got.IsOK(), "expected declare-candidacy to be ok, got %v", got) - msgDelegate := newTestMsgDelegate(delegatorAddr, candidateAddr, initBond) + msgDelegate := newTestMsgDelegate(delegatorAddr, validatorAddr, initBond) got = handleMsgDelegate(ctx, msgDelegate, keeper) assert.True(t, got.IsOK(), "expected delegation to be ok, got %v", got) - candidate, found := keeper.GetCandidate(ctx, candidateAddr) + validator, found := keeper.GetValidator(ctx, validatorAddr) require.True(t, found) - assert.Equal(t, initBond*2, candidate.Liabilities.Evaluate()) - assert.Equal(t, initBond*2, candidate.Assets.Evaluate()) + assert.Equal(t, initBond*2, validator.DelegatorShares.Evaluate()) + assert.Equal(t, initBond*2, validator.PoolShares.Bonded().Evaluate()) // just send the same msgUnbond multiple times // TODO use decimals here unbondShares, unbondSharesStr := int64(10), "10" - msgUnbond := NewMsgUnbond(delegatorAddr, candidateAddr, unbondSharesStr) + msgUnbond := NewMsgUnbond(delegatorAddr, validatorAddr, unbondSharesStr) numUnbonds := 5 for i := 0; i < numUnbonds; i++ { got := handleMsgUnbond(ctx, msgUnbond, keeper) require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got) //Check that the accounts and the bond account have the appropriate values - candidate, found = keeper.GetCandidate(ctx, candidateAddr) + validator, found = keeper.GetValidator(ctx, validatorAddr) require.True(t, found) - bond, found := keeper.GetDelegatorBond(ctx, delegatorAddr, candidateAddr) + bond, found := keeper.GetDelegation(ctx, delegatorAddr, validatorAddr) require.True(t, found) expBond := initBond - int64(i+1)*unbondShares - expLiabilities := 2*initBond - int64(i+1)*unbondShares + expDelegatorShares := 2*initBond - int64(i+1)*unbondShares expDelegatorAcc := initBond - expBond gotBond := bond.Shares.Evaluate() - gotLiabilities := candidate.Liabilities.Evaluate() + gotDelegatorShares := validator.DelegatorShares.Evaluate() gotDelegatorAcc := accMapper.GetAccount(ctx, delegatorAddr).GetCoins().AmountOf(params.BondDenom) require.Equal(t, expBond, gotBond, - "i: %v\nexpBond: %v\ngotBond: %v\ncandidate: %v\nbond: %v\n", - i, expBond, gotBond, candidate, bond) - require.Equal(t, expLiabilities, gotLiabilities, - "i: %v\nexpLiabilities: %v\ngotLiabilities: %v\ncandidate: %v\nbond: %v\n", - i, expLiabilities, gotLiabilities, candidate, bond) + "i: %v\nexpBond: %v\ngotBond: %v\nvalidator: %v\nbond: %v\n", + i, expBond, gotBond, validator, bond) + require.Equal(t, expDelegatorShares, gotDelegatorShares, + "i: %v\nexpDelegatorShares: %v\ngotDelegatorShares: %v\nvalidator: %v\nbond: %v\n", + i, expDelegatorShares, gotDelegatorShares, validator, bond) require.Equal(t, expDelegatorAcc, gotDelegatorAcc, - "i: %v\nexpDelegatorAcc: %v\ngotDelegatorAcc: %v\ncandidate: %v\nbond: %v\n", - i, expDelegatorAcc, gotDelegatorAcc, candidate, bond) + "i: %v\nexpDelegatorAcc: %v\ngotDelegatorAcc: %v\nvalidator: %v\nbond: %v\n", + i, expDelegatorAcc, gotDelegatorAcc, validator, bond) } // these are more than we have bonded now @@ -176,7 +194,7 @@ func TestIncrementsMsgUnbond(t *testing.T) { } for _, c := range errorCases { unbondShares := strconv.Itoa(int(c)) - msgUnbond := NewMsgUnbond(delegatorAddr, candidateAddr, unbondShares) + msgUnbond := NewMsgUnbond(delegatorAddr, validatorAddr, unbondShares) got = handleMsgUnbond(ctx, msgUnbond, keeper) require.False(t, got.IsOK(), "expected unbond msg to fail") } @@ -185,14 +203,14 @@ func TestIncrementsMsgUnbond(t *testing.T) { // should be unable to unbond one more than we have unbondSharesStr = strconv.Itoa(int(leftBonded) + 1) - msgUnbond = NewMsgUnbond(delegatorAddr, candidateAddr, unbondSharesStr) + msgUnbond = NewMsgUnbond(delegatorAddr, validatorAddr, unbondSharesStr) got = handleMsgUnbond(ctx, msgUnbond, keeper) assert.False(t, got.IsOK(), "got: %v\nmsgUnbond: %v\nshares: %v\nleftBonded: %v\n", got, msgUnbond, unbondSharesStr, leftBonded) // should be able to unbond just what we have unbondSharesStr = strconv.Itoa(int(leftBonded)) - msgUnbond = NewMsgUnbond(delegatorAddr, candidateAddr, unbondSharesStr) + msgUnbond = NewMsgUnbond(delegatorAddr, validatorAddr, unbondSharesStr) got = handleMsgUnbond(ctx, msgUnbond, keeper) assert.True(t, got.IsOK(), "got: %v\nmsgUnbond: %v\nshares: %v\nleftBonded: %v\n", got, msgUnbond, unbondSharesStr, leftBonded) @@ -202,108 +220,108 @@ func TestMultipleMsgDeclareCandidacy(t *testing.T) { initBond := int64(1000) ctx, accMapper, keeper := createTestInput(t, false, initBond) params := keeper.GetParams(ctx) - candidateAddrs := []sdk.Address{addrs[0], addrs[1], addrs[2]} + validatorAddrs := []sdk.Address{addrs[0], addrs[1], addrs[2]} // bond them all - for i, candidateAddr := range candidateAddrs { - msgDeclareCandidacy := newTestMsgDeclareCandidacy(candidateAddr, pks[i], 10) + for i, validatorAddr := range validatorAddrs { + msgDeclareCandidacy := newTestMsgDeclareCandidacy(validatorAddr, pks[i], 10) got := handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper) require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got) //Check that the account is bonded - candidates := keeper.GetCandidates(ctx, 100) - require.Equal(t, (i + 1), len(candidates)) - val := candidates[i] + validators := keeper.GetValidators(ctx, 100) + require.Equal(t, (i + 1), len(validators)) + val := validators[i] balanceExpd := initBond - 10 - balanceGot := accMapper.GetAccount(ctx, val.Address).GetCoins().AmountOf(params.BondDenom) - require.Equal(t, i+1, len(candidates), "expected %d candidates got %d, candidates: %v", i+1, len(candidates), candidates) - require.Equal(t, 10, int(val.Liabilities.Evaluate()), "expected %d shares, got %d", 10, val.Liabilities) + balanceGot := accMapper.GetAccount(ctx, val.Owner).GetCoins().AmountOf(params.BondDenom) + require.Equal(t, i+1, len(validators), "expected %d validators got %d, validators: %v", i+1, len(validators), validators) + require.Equal(t, 10, int(val.DelegatorShares.Evaluate()), "expected %d shares, got %d", 10, val.DelegatorShares) require.Equal(t, balanceExpd, balanceGot, "expected account to have %d, got %d", balanceExpd, balanceGot) } // unbond them all - for i, candidateAddr := range candidateAddrs { - candidatePre, found := keeper.GetCandidate(ctx, candidateAddr) + for i, validatorAddr := range validatorAddrs { + validatorPre, found := keeper.GetValidator(ctx, validatorAddr) require.True(t, found) - msgUnbond := NewMsgUnbond(candidateAddr, candidateAddr, "10") // self-delegation + msgUnbond := NewMsgUnbond(validatorAddr, validatorAddr, "10") // self-delegation got := handleMsgUnbond(ctx, msgUnbond, keeper) require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got) //Check that the account is unbonded - candidates := keeper.GetCandidates(ctx, 100) - require.Equal(t, len(candidateAddrs)-(i+1), len(candidates), - "expected %d candidates got %d", len(candidateAddrs)-(i+1), len(candidates)) + validators := keeper.GetValidators(ctx, 100) + require.Equal(t, len(validatorAddrs)-(i+1), len(validators), + "expected %d validators got %d", len(validatorAddrs)-(i+1), len(validators)) - _, found = keeper.GetCandidate(ctx, candidateAddr) + _, found = keeper.GetValidator(ctx, validatorAddr) require.False(t, found) expBalance := initBond - gotBalance := accMapper.GetAccount(ctx, candidatePre.Address).GetCoins().AmountOf(params.BondDenom) + gotBalance := accMapper.GetAccount(ctx, validatorPre.Owner).GetCoins().AmountOf(params.BondDenom) require.Equal(t, expBalance, gotBalance, "expected account to have %d, got %d", expBalance, gotBalance) } } func TestMultipleMsgDelegate(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 1000) - candidateAddr, delegatorAddrs := addrs[0], addrs[1:] + validatorAddr, delegatorAddrs := addrs[0], addrs[1:] - //first make a candidate - msgDeclareCandidacy := newTestMsgDeclareCandidacy(candidateAddr, pks[0], 10) + //first make a validator + msgDeclareCandidacy := newTestMsgDeclareCandidacy(validatorAddr, pks[0], 10) got := handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper) require.True(t, got.IsOK(), "expected msg to be ok, got %v", got) // delegate multiple parties for i, delegatorAddr := range delegatorAddrs { - msgDelegate := newTestMsgDelegate(delegatorAddr, candidateAddr, 10) + msgDelegate := newTestMsgDelegate(delegatorAddr, validatorAddr, 10) got := handleMsgDelegate(ctx, msgDelegate, keeper) require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got) //Check that the account is bonded - bond, found := keeper.GetDelegatorBond(ctx, delegatorAddr, candidateAddr) + bond, found := keeper.GetDelegation(ctx, delegatorAddr, validatorAddr) require.True(t, found) require.NotNil(t, bond, "expected delegatee bond %d to exist", bond) } // unbond them all for i, delegatorAddr := range delegatorAddrs { - msgUnbond := NewMsgUnbond(delegatorAddr, candidateAddr, "10") + msgUnbond := NewMsgUnbond(delegatorAddr, validatorAddr, "10") got := handleMsgUnbond(ctx, msgUnbond, keeper) require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got) //Check that the account is unbonded - _, found := keeper.GetDelegatorBond(ctx, delegatorAddr, candidateAddr) + _, found := keeper.GetDelegation(ctx, delegatorAddr, validatorAddr) require.False(t, found) } } func TestVoidCandidacy(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 1000) - candidateAddr, delegatorAddr := addrs[0], addrs[1] + validatorAddr, delegatorAddr := addrs[0], addrs[1] - // create the candidate - msgDeclareCandidacy := newTestMsgDeclareCandidacy(candidateAddr, pks[0], 10) + // create the validator + msgDeclareCandidacy := newTestMsgDeclareCandidacy(validatorAddr, pks[0], 10) got := handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper) require.True(t, got.IsOK(), "expected no error on runMsgDeclareCandidacy") // bond a delegator - msgDelegate := newTestMsgDelegate(delegatorAddr, candidateAddr, 10) + msgDelegate := newTestMsgDelegate(delegatorAddr, validatorAddr, 10) got = handleMsgDelegate(ctx, msgDelegate, keeper) require.True(t, got.IsOK(), "expected ok, got %v", got) - // unbond the candidates bond portion - msgUnbondCandidate := NewMsgUnbond(candidateAddr, candidateAddr, "10") - got = handleMsgUnbond(ctx, msgUnbondCandidate, keeper) + // unbond the validators bond portion + msgUnbondValidator := NewMsgUnbond(validatorAddr, validatorAddr, "10") + got = handleMsgUnbond(ctx, msgUnbondValidator, keeper) require.True(t, got.IsOK(), "expected no error on runMsgDeclareCandidacy") - candidate, found := keeper.GetCandidate(ctx, candidateAddr) + validator, found := keeper.GetValidator(ctx, validatorAddr) require.True(t, found) - require.Equal(t, Revoked, candidate.Status) + require.True(t, validator.Revoked) // test that this address cannot yet be bonded too because is revoked got = handleMsgDelegate(ctx, msgDelegate, keeper) assert.False(t, got.IsOK(), "expected error, got %v", got) // test that the delegator can still withdraw their bonds - msgUnbondDelegator := NewMsgUnbond(delegatorAddr, candidateAddr, "10") + msgUnbondDelegator := NewMsgUnbond(delegatorAddr, validatorAddr, "10") got = handleMsgUnbond(ctx, msgUnbondDelegator, keeper) require.True(t, got.IsOK(), "expected no error on runMsgDeclareCandidacy") diff --git a/x/stake/keeper.go b/x/stake/keeper.go index 605f675db..ce84b1e17 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -2,6 +2,7 @@ package stake import ( "bytes" + "fmt" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" @@ -15,10 +16,6 @@ type Keeper struct { cdc *wire.Codec coinKeeper bank.Keeper - // caches - pool Pool - params Params - // codespace codespace sdk.CodespaceType } @@ -33,53 +30,57 @@ func NewKeeper(cdc *wire.Codec, key sdk.StoreKey, ck bank.Keeper, codespace sdk. return keeper } -// get the current in-block validator operation counter -func (k Keeper) getCounter(ctx sdk.Context) int16 { - store := ctx.KVStore(k.storeKey) - b := store.Get(CounterKey) - if b == nil { - return 0 - } - var counter int16 - err := k.cdc.UnmarshalBinary(b, &counter) - if err != nil { - panic(err) - } - return counter -} - -// set the current in-block validator operation counter -func (k Keeper) setCounter(ctx sdk.Context, counter int16) { - store := ctx.KVStore(k.storeKey) - bz, err := k.cdc.MarshalBinary(counter) - if err != nil { - panic(err) - } - store.Set(CounterKey, bz) -} - //_________________________________________________________________________ -// get a single candidate -func (k Keeper) GetCandidate(ctx sdk.Context, addr sdk.Address) (candidate Candidate, found bool) { +// get a single validator +func (k Keeper) GetValidator(ctx sdk.Context, addr sdk.Address) (validator Validator, found bool) { store := ctx.KVStore(k.storeKey) - b := store.Get(GetCandidateKey(addr)) - if b == nil { - return candidate, false - } - err := k.cdc.UnmarshalBinary(b, &candidate) - if err != nil { - panic(err) - } - return candidate, true + return k.getValidator(store, addr) } -// Get the set of all candidates, retrieve a maxRetrieve number of records -func (k Keeper) GetCandidates(ctx sdk.Context, maxRetrieve int16) (candidates Candidates) { - store := ctx.KVStore(k.storeKey) - iterator := store.Iterator(subspace(CandidatesKey)) +// get a single validator (reuse store) +func (k Keeper) getValidator(store sdk.KVStore, addr sdk.Address) (validator Validator, found bool) { + b := store.Get(GetValidatorKey(addr)) + if b == nil { + return validator, false + } + k.cdc.MustUnmarshalBinary(b, &validator) + return validator, true +} - candidates = make([]Candidate, maxRetrieve) +// set the main record holding validator details +func (k Keeper) setValidator(ctx sdk.Context, validator Validator) { + store := ctx.KVStore(k.storeKey) + bz := k.cdc.MustMarshalBinary(validator) + store.Set(GetValidatorKey(validator.Owner), bz) +} + +// Get the set of all validators with no limits, used during genesis dump +func (k Keeper) getAllValidators(ctx sdk.Context) (validators Validators) { + store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey) + + i := 0 + for ; ; i++ { + if !iterator.Valid() { + iterator.Close() + break + } + bz := iterator.Value() + var validator Validator + k.cdc.MustUnmarshalBinary(bz, &validator) + validators = append(validators, validator) + iterator.Next() + } + return validators +} + +// Get the set of all validators, retrieve a maxRetrieve number of records +func (k Keeper) GetValidators(ctx sdk.Context, maxRetrieve int16) (validators Validators) { + store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey) + + validators = make([]Validator, maxRetrieve) i := 0 for ; ; i++ { if !iterator.Valid() || i > int(maxRetrieve-1) { @@ -87,321 +88,444 @@ func (k Keeper) GetCandidates(ctx sdk.Context, maxRetrieve int16) (candidates Ca break } bz := iterator.Value() - var candidate Candidate - err := k.cdc.UnmarshalBinary(bz, &candidate) - if err != nil { - panic(err) - } - candidates[i] = candidate + var validator Validator + k.cdc.MustUnmarshalBinary(bz, &validator) + validators[i] = validator iterator.Next() } - return candidates[:i] // trim -} - -func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) { - store := ctx.KVStore(k.storeKey) - address := candidate.Address - - // retreive the old candidate record - oldCandidate, oldFound := k.GetCandidate(ctx, address) - - // if found, copy the old block height and counter - if oldFound { - candidate.ValidatorBondHeight = oldCandidate.ValidatorBondHeight - candidate.ValidatorBondCounter = oldCandidate.ValidatorBondCounter - } - - // marshal the candidate record and add to the state - bz, err := k.cdc.MarshalBinary(candidate) - if err != nil { - panic(err) - } - store.Set(GetCandidateKey(candidate.Address), bz) - - // if the voting power is the same no need to update any of the other indexes - if oldFound && oldCandidate.Assets.Equal(candidate.Assets) { - return - } - - updateHeight := false - - // update the list ordered by voting power - if oldFound { - if !k.isNewValidator(ctx, store, candidate.Address) { - updateHeight = true - } - // else already in the validator set - retain the old validator height and counter - store.Delete(GetValidatorKey(address, oldCandidate.Assets, oldCandidate.ValidatorBondHeight, oldCandidate.ValidatorBondCounter, k.cdc)) - } else { - updateHeight = true - } - - if updateHeight { - // wasn't a candidate or wasn't in the validator set, update the validator block height and counter - candidate.ValidatorBondHeight = ctx.BlockHeight() - counter := k.getCounter(ctx) - candidate.ValidatorBondCounter = counter - k.setCounter(ctx, counter+1) - } - - // update the candidate record - bz, err = k.cdc.MarshalBinary(candidate) - if err != nil { - panic(err) - } - store.Set(GetCandidateKey(candidate.Address), bz) - - // marshal the new validator record - validator := candidate.validator() - bz, err = k.cdc.MarshalBinary(validator) - if err != nil { - panic(err) - } - - store.Set(GetValidatorKey(address, validator.Power, validator.Height, validator.Counter, k.cdc), bz) - - // add to the validators to update list if is already a validator - // or is a new validator - setAcc := false - if store.Get(GetRecentValidatorKey(address)) != nil { - setAcc = true - - // want to check in the else statement because inefficient - } else if k.isNewValidator(ctx, store, address) { - setAcc = true - } - if setAcc { - bz, err = k.cdc.MarshalBinary(validator.abciValidator(k.cdc)) - if err != nil { - panic(err) - } - store.Set(GetAccUpdateValidatorKey(validator.Address), bz) - - } - - return -} - -func (k Keeper) removeCandidate(ctx sdk.Context, address sdk.Address) { - - // first retreive the old candidate record - candidate, found := k.GetCandidate(ctx, address) - if !found { - return - } - - // delete the old candidate record - store := ctx.KVStore(k.storeKey) - store.Delete(GetCandidateKey(address)) - store.Delete(GetValidatorKey(address, candidate.Assets, candidate.ValidatorBondHeight, candidate.ValidatorBondCounter, k.cdc)) - - // delete from recent and power weighted validator groups if the validator - // exists and add validator with zero power to the validator updates - if store.Get(GetRecentValidatorKey(address)) == nil { - return - } - bz, err := k.cdc.MarshalBinary(candidate.validator().abciValidatorZero(k.cdc)) - if err != nil { - panic(err) - } - store.Set(GetAccUpdateValidatorKey(address), bz) - store.Delete(GetRecentValidatorKey(address)) + return validators[:i] // trim } //___________________________________________________________________________ -// Get the validator set from the candidates. The correct subset is retrieved -// by iterating through an index of the candidates sorted by power, stored -// using the ValidatorsKey. Simultaniously the most recent the validator -// records are updated in store with the RecentValidatorsKey. This store is -// used to determine if a candidate is a validator without needing to iterate -// over the subspace as we do in GetValidators -func (k Keeper) GetValidators(ctx sdk.Context) (validators []Validator) { +// get the group of the bonded validators +func (k Keeper) GetValidatorsBonded(ctx sdk.Context) (validators []Validator) { store := ctx.KVStore(k.storeKey) - // clear the recent validators store, add to the ToKickOut Temp store - iterator := store.Iterator(subspace(RecentValidatorsKey)) - for ; iterator.Valid(); iterator.Next() { - addr := AddrFromKey(iterator.Key()) - - // iterator.Value is the validator object - store.Set(GetToKickOutValidatorKey(addr), iterator.Value()) - store.Delete(iterator.Key()) - } - iterator.Close() - // add the actual validator power sorted store maxValidators := k.GetParams(ctx).MaxValidators - iterator = store.ReverseIterator(subspace(ValidatorsKey)) // largest to smallest validators = make([]Validator, maxValidators) + + iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedKey) i := 0 - for ; ; i++ { + for ; iterator.Valid(); iterator.Next() { + + // sanity check + if i > int(maxValidators-1) { + panic("maxValidators is less than the number of records in ValidatorsBonded Store, store should have been updated") + } + address := iterator.Value() + validator, found := k.getValidator(store, address) + if !found { + panic(fmt.Sprintf("validator record not found for address: %v\n", address)) + } + + validators[i] = validator + i++ + } + iterator.Close() + return validators[:i] // trim +} + +// get the group of bonded validators sorted by power-rank +func (k Keeper) GetValidatorsByPower(ctx sdk.Context) []Validator { + store := ctx.KVStore(k.storeKey) + maxValidators := k.GetParams(ctx).MaxValidators + validators := make([]Validator, maxValidators) + iterator := sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerKey) // largest to smallest + i := 0 + for { if !iterator.Valid() || i > int(maxValidators-1) { iterator.Close() break } - bz := iterator.Value() - var validator Validator - err := k.cdc.UnmarshalBinary(bz, &validator) - if err != nil { - panic(err) + address := iterator.Value() + validator, found := k.getValidator(store, address) + if !found { + panic(fmt.Sprintf("validator record not found for address: %v\n", address)) + } + if validator.Status() == sdk.Bonded { + validators[i] = validator + i++ } - validators[i] = validator - - // remove from ToKickOut group - store.Delete(GetToKickOutValidatorKey(validator.Address)) - - // also add to the recent validators group - store.Set(GetRecentValidatorKey(validator.Address), bz) - iterator.Next() } - - // add any kicked out validators to the acc change - iterator = store.Iterator(subspace(ToKickOutValidatorsKey)) - for ; iterator.Valid(); iterator.Next() { - key := iterator.Key() - addr := AddrFromKey(key) - - // get the zero abci validator from the ToKickOut iterator value - bz := iterator.Value() - var validator Validator - err := k.cdc.UnmarshalBinary(bz, &validator) - if err != nil { - panic(err) - } - bz, err = k.cdc.MarshalBinary(validator.abciValidatorZero(k.cdc)) - if err != nil { - panic(err) - } - - store.Set(GetAccUpdateValidatorKey(addr), bz) - store.Delete(key) - } - iterator.Close() - return validators[:i] // trim } -// TODO this is madly inefficient because need to call every time we set a candidate -// Should use something better than an iterator maybe? -// Used to determine if something has just been added to the actual validator set -func (k Keeper) isNewValidator(ctx sdk.Context, store sdk.KVStore, address sdk.Address) bool { - // add the actual validator power sorted store - maxVal := k.GetParams(ctx).MaxValidators - iterator := store.ReverseIterator(subspace(ValidatorsKey)) // largest to smallest - for i := 0; ; i++ { - if !iterator.Valid() || i > int(maxVal-1) { - iterator.Close() - break - } - bz := iterator.Value() - var val Validator - err := k.cdc.UnmarshalBinary(bz, &val) - if err != nil { - panic(err) - } - if bytes.Equal(val.Address, address) { - return true - } - iterator.Next() - } - - return false -} - -// Is the address provided a part of the most recently saved validator group? -func (k Keeper) IsRecentValidator(ctx sdk.Context, address sdk.Address) bool { - store := ctx.KVStore(k.storeKey) - if store.Get(GetRecentValidatorKey(address)) == nil { - return false - } - return true -} - //_________________________________________________________________________ -// Accumulated updates to the validator set +// Accumulated updates to the active/bonded validator set for tendermint // get the most recently updated validators -func (k Keeper) getAccUpdateValidators(ctx sdk.Context) (updates []abci.Validator) { +func (k Keeper) getTendermintUpdates(ctx sdk.Context) (updates []abci.Validator) { store := ctx.KVStore(k.storeKey) - iterator := store.Iterator(subspace(AccUpdateValidatorsKey)) //smallest to largest + iterator := sdk.KVStorePrefixIterator(store, TendermintUpdatesKey) //smallest to largest for ; iterator.Valid(); iterator.Next() { valBytes := iterator.Value() var val abci.Validator - err := k.cdc.UnmarshalBinary(valBytes, &val) - if err != nil { - panic(err) - } + k.cdc.MustUnmarshalBinary(valBytes, &val) updates = append(updates, val) } iterator.Close() return } -// remove all validator update entries -func (k Keeper) clearAccUpdateValidators(ctx sdk.Context) { +// remove all validator update entries after applied to Tendermint +func (k Keeper) clearTendermintUpdates(ctx sdk.Context) { store := ctx.KVStore(k.storeKey) // delete subspace - iterator := store.Iterator(subspace(AccUpdateValidatorsKey)) + iterator := sdk.KVStorePrefixIterator(store, TendermintUpdatesKey) for ; iterator.Valid(); iterator.Next() { store.Delete(iterator.Key()) } iterator.Close() } +//___________________________________________________________________________ + +// perfom all the nessisary steps for when a validator changes its power +// updates all validator stores as well as tendermint update store +// may kick out validators if new validator is entering the bonded validator group +func (k Keeper) updateValidator(ctx sdk.Context, validator Validator) Validator { + store := ctx.KVStore(k.storeKey) + pool := k.getPool(store) + ownerAddr := validator.Owner + + // always update the main list ordered by owner address before exiting + defer func() { + bz := k.cdc.MustMarshalBinary(validator) + store.Set(GetValidatorKey(ownerAddr), bz) + }() + + // retreive the old validator record + oldValidator, oldFound := k.GetValidator(ctx, ownerAddr) + + if validator.Revoked && oldValidator.Status() == sdk.Bonded { + validator, pool = validator.UpdateStatus(pool, sdk.Unbonded) + k.setPool(ctx, pool) + } + + powerIncreasing := false + if oldFound && oldValidator.PoolShares.Bonded().LT(validator.PoolShares.Bonded()) { + powerIncreasing = true + } + + // if already a validator, copy the old block height and counter, else set them + if oldFound && oldValidator.Status() == sdk.Bonded { + validator.BondHeight = oldValidator.BondHeight + validator.BondIntraTxCounter = oldValidator.BondIntraTxCounter + } else { + validator.BondHeight = ctx.BlockHeight() + counter := k.getIntraTxCounter(ctx) + validator.BondIntraTxCounter = counter + k.setIntraTxCounter(ctx, counter+1) + } + + // update the list ordered by voting power + if oldFound { + store.Delete(GetValidatorsByPowerKey(oldValidator, pool)) + } + valPower := GetValidatorsByPowerKey(validator, pool) + store.Set(valPower, validator.Owner) + + // efficiency case: + // if already bonded and power increasing only need to update tendermint + if powerIncreasing && !validator.Revoked && oldValidator.Status() == sdk.Bonded { + bz := k.cdc.MustMarshalBinary(validator.abciValidator(k.cdc)) + store.Set(GetTendermintUpdatesKey(ownerAddr), bz) + return validator + } + + // efficiency case: + // if was unbonded/or is a new validator - and the new power is less than the cliff validator + cliffPower := k.getCliffValidatorPower(ctx) + if cliffPower != nil && + (!oldFound || (oldFound && oldValidator.Status() == sdk.Unbonded)) && + bytes.Compare(valPower, cliffPower) == -1 { //(valPower < cliffPower + return validator + } + + // update the validator set for this validator + updatedVal := k.updateBondedValidators(ctx, store, validator) + if updatedVal.Owner != nil { // updates to validator occured to be updated + validator = updatedVal + } + return validator +} + +// XXX TODO build in consideration for revoked +// +// Update the validator group and kick out any old validators. In addition this +// function adds (or doesn't add) a validator which has updated its bonded +// tokens to the validator group. -> this validator is specified through the +// updatedValidatorAddr term. +// +// The correct subset is retrieved by iterating through an index of the +// validators sorted by power, stored using the ValidatorsByPowerKey. Simultaniously +// the current validator records are updated in store with the +// ValidatorsBondedKey. This store is used to determine if a validator is a +// validator without needing to iterate over the subspace as we do in +// GetValidators. +// +// Optionally also return the validator from a retrieve address if the validator has been bonded +func (k Keeper) updateBondedValidators(ctx sdk.Context, store sdk.KVStore, + newValidator Validator) (updatedVal Validator) { + + kickCliffValidator := false + oldCliffValidatorAddr := k.getCliffValidator(ctx) + + // add the actual validator power sorted store + maxValidators := k.GetParams(ctx).MaxValidators + iterator := sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerKey) // largest to smallest + bondedValidatorsCount := 0 + var validator Validator + for { + if !iterator.Valid() || bondedValidatorsCount > int(maxValidators-1) { + + // TODO benchmark if we should read the current power and not write if it's the same + if bondedValidatorsCount == int(maxValidators) { // is cliff validator + k.setCliffValidator(ctx, validator, k.GetPool(ctx)) + } + iterator.Close() + break + } + + // either retrieve the original validator from the store, + // or under the situation that this is the "new validator" just + // use the validator provided because it has not yet been updated + // in the main validator store + ownerAddr := iterator.Value() + if bytes.Equal(ownerAddr, newValidator.Owner) { + validator = newValidator + } else { + var found bool + validator, found = k.getValidator(store, ownerAddr) + if !found { + panic(fmt.Sprintf("validator record not found for address: %v\n", ownerAddr)) + } + } + + // if not previously a validator (and unrevoked), + // kick the cliff validator / bond this new validator + if validator.Status() != sdk.Bonded && !validator.Revoked { + kickCliffValidator = true + + validator = k.bondValidator(ctx, store, validator) + if bytes.Equal(ownerAddr, newValidator.Owner) { + updatedVal = validator + } + } + + if validator.Revoked && validator.Status() == sdk.Bonded { + panic(fmt.Sprintf("revoked validator cannot be bonded, address: %v\n", ownerAddr)) + } else { + bondedValidatorsCount++ + } + + iterator.Next() + } + + // perform the actual kicks + if oldCliffValidatorAddr != nil && kickCliffValidator { + validator, found := k.getValidator(store, oldCliffValidatorAddr) + if !found { + panic(fmt.Sprintf("validator record not found for address: %v\n", oldCliffValidatorAddr)) + } + k.unbondValidator(ctx, store, validator) + } + + return +} + +// full update of the bonded validator set, many can be added/kicked +func (k Keeper) updateBondedValidatorsFull(ctx sdk.Context, store sdk.KVStore) { + // clear the current validators store, add to the ToKickOut temp store + toKickOut := make(map[string]byte) + iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedKey) + for ; iterator.Valid(); iterator.Next() { + ownerAddr := iterator.Value() + toKickOut[string(ownerAddr)] = 0 // set anything + } + iterator.Close() + + // add the actual validator power sorted store + maxValidators := k.GetParams(ctx).MaxValidators + iterator = sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerKey) // largest to smallest + bondedValidatorsCount := 0 + var validator Validator + for { + if !iterator.Valid() || bondedValidatorsCount > int(maxValidators-1) { + + if bondedValidatorsCount == int(maxValidators) { // is cliff validator + k.setCliffValidator(ctx, validator, k.GetPool(ctx)) + } + iterator.Close() + break + } + + // either retrieve the original validator from the store, + // or under the situation that this is the "new validator" just + // use the validator provided because it has not yet been updated + // in the main validator store + ownerAddr := iterator.Value() + var found bool + validator, found = k.getValidator(store, ownerAddr) + if !found { + panic(fmt.Sprintf("validator record not found for address: %v\n", ownerAddr)) + } + + _, found = toKickOut[string(ownerAddr)] + if found { + delete(toKickOut, string(ownerAddr)) + } else { + + // if it wasn't in the toKickOut group it means + // this wasn't a previously a validator, therefor + // update the validator to enter the validator group + validator = k.bondValidator(ctx, store, validator) + } + + if validator.Revoked && validator.Status() == sdk.Bonded { + panic(fmt.Sprintf("revoked validator cannot be bonded, address: %v\n", ownerAddr)) + } else { + bondedValidatorsCount++ + } + + iterator.Next() + } + + // perform the actual kicks + for key := range toKickOut { + ownerAddr := []byte(key) + validator, found := k.getValidator(store, ownerAddr) + if !found { + panic(fmt.Sprintf("validator record not found for address: %v\n", ownerAddr)) + } + k.unbondValidator(ctx, store, validator) + } + return +} + +// perform all the store operations for when a validator status becomes unbonded +func (k Keeper) unbondValidator(ctx sdk.Context, store sdk.KVStore, validator Validator) { + pool := k.GetPool(ctx) + + // sanity check + if validator.Status() == sdk.Unbonded { + panic(fmt.Sprintf("should not already be be unbonded, validator: %v\n", validator)) + } + + // set the status + validator, pool = validator.UpdateStatus(pool, sdk.Unbonded) + k.setPool(ctx, pool) + + // save the now unbonded validator record + bzVal := k.cdc.MustMarshalBinary(validator) + store.Set(GetValidatorKey(validator.Owner), bzVal) + + // add to accumulated changes for tendermint + bzABCI := k.cdc.MustMarshalBinary(validator.abciValidatorZero(k.cdc)) + store.Set(GetTendermintUpdatesKey(validator.Owner), bzABCI) + + // also remove from the Bonded Validators Store + store.Delete(GetValidatorsBondedKey(validator.PubKey)) +} + +// perform all the store operations for when a validator status becomes bonded +func (k Keeper) bondValidator(ctx sdk.Context, store sdk.KVStore, validator Validator) Validator { + pool := k.GetPool(ctx) + + // sanity check + if validator.Status() == sdk.Bonded { + panic(fmt.Sprintf("should not already be be bonded, validator: %v\n", validator)) + } + + // set the status + validator, pool = validator.UpdateStatus(pool, sdk.Bonded) + k.setPool(ctx, pool) + + // save the now bonded validator record to the three referenced stores + bzVal := k.cdc.MustMarshalBinary(validator) + store.Set(GetValidatorKey(validator.Owner), bzVal) + store.Set(GetValidatorsBondedKey(validator.PubKey), validator.Owner) + + // add to accumulated changes for tendermint + bzABCI := k.cdc.MustMarshalBinary(validator.abciValidator(k.cdc)) + store.Set(GetTendermintUpdatesKey(validator.Owner), bzABCI) + + return validator +} + +func (k Keeper) removeValidator(ctx sdk.Context, address sdk.Address) { + + // first retreive the old validator record + validator, found := k.GetValidator(ctx, address) + if !found { + return + } + + // delete the old validator record + store := ctx.KVStore(k.storeKey) + pool := k.getPool(store) + store.Delete(GetValidatorKey(address)) + store.Delete(GetValidatorsByPowerKey(validator, pool)) + + // delete from the current and power weighted validator groups if the validator + // is bonded - and add validator with zero power to the validator updates + if store.Get(GetValidatorsBondedKey(validator.PubKey)) == nil { + return + } + store.Delete(GetValidatorsBondedKey(validator.PubKey)) + + bz := k.cdc.MustMarshalBinary(validator.abciValidatorZero(k.cdc)) + store.Set(GetTendermintUpdatesKey(address), bz) +} + //_____________________________________________________________________ // load a delegator bond -func (k Keeper) GetDelegatorBond(ctx sdk.Context, - delegatorAddr, candidateAddr sdk.Address) (bond DelegatorBond, found bool) { +func (k Keeper) GetDelegation(ctx sdk.Context, + delegatorAddr, validatorAddr sdk.Address) (bond Delegation, found bool) { store := ctx.KVStore(k.storeKey) - delegatorBytes := store.Get(GetDelegatorBondKey(delegatorAddr, candidateAddr, k.cdc)) + delegatorBytes := store.Get(GetDelegationKey(delegatorAddr, validatorAddr, k.cdc)) if delegatorBytes == nil { return bond, false } - err := k.cdc.UnmarshalBinary(delegatorBytes, &bond) - if err != nil { - panic(err) - } + k.cdc.MustUnmarshalBinary(delegatorBytes, &bond) return bond, true } -// load all bonds -func (k Keeper) getBonds(ctx sdk.Context, maxRetrieve int16) (bonds []DelegatorBond) { +// load all delegations used during genesis dump +func (k Keeper) getAllDelegations(ctx sdk.Context) (delegations []Delegation) { store := ctx.KVStore(k.storeKey) - iterator := store.Iterator(subspace(DelegatorBondKeyPrefix)) + iterator := sdk.KVStorePrefixIterator(store, DelegationKey) - bonds = make([]DelegatorBond, maxRetrieve) i := 0 for ; ; i++ { - if !iterator.Valid() || i > int(maxRetrieve-1) { + if !iterator.Valid() { iterator.Close() break } bondBytes := iterator.Value() - var bond DelegatorBond - err := k.cdc.UnmarshalBinary(bondBytes, &bond) - if err != nil { - panic(err) - } - bonds[i] = bond + var delegation Delegation + k.cdc.MustUnmarshalBinary(bondBytes, &delegation) + delegations = append(delegations, delegation) iterator.Next() } - return bonds[:i] // trim + return delegations[:i] // trim } // load all bonds of a delegator -func (k Keeper) GetDelegatorBonds(ctx sdk.Context, delegator sdk.Address, maxRetrieve int16) (bonds []DelegatorBond) { +func (k Keeper) GetDelegations(ctx sdk.Context, delegator sdk.Address, maxRetrieve int16) (bonds []Delegation) { store := ctx.KVStore(k.storeKey) - delegatorPrefixKey := GetDelegatorBondsKey(delegator, k.cdc) - iterator := store.Iterator(subspace(delegatorPrefixKey)) //smallest to largest + delegatorPrefixKey := GetDelegationsKey(delegator, k.cdc) + iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) //smallest to largest - bonds = make([]DelegatorBond, maxRetrieve) + bonds = make([]Delegation, maxRetrieve) i := 0 for ; ; i++ { if !iterator.Valid() || i > int(maxRetrieve-1) { @@ -409,87 +533,219 @@ func (k Keeper) GetDelegatorBonds(ctx sdk.Context, delegator sdk.Address, maxRet break } bondBytes := iterator.Value() - var bond DelegatorBond - err := k.cdc.UnmarshalBinary(bondBytes, &bond) - if err != nil { - panic(err) - } + var bond Delegation + k.cdc.MustUnmarshalBinary(bondBytes, &bond) bonds[i] = bond iterator.Next() } return bonds[:i] // trim } -func (k Keeper) setDelegatorBond(ctx sdk.Context, bond DelegatorBond) { +func (k Keeper) setDelegation(ctx sdk.Context, bond Delegation) { store := ctx.KVStore(k.storeKey) - b, err := k.cdc.MarshalBinary(bond) - if err != nil { - panic(err) - } - store.Set(GetDelegatorBondKey(bond.DelegatorAddr, bond.CandidateAddr, k.cdc), b) + b := k.cdc.MustMarshalBinary(bond) + store.Set(GetDelegationKey(bond.DelegatorAddr, bond.ValidatorAddr, k.cdc), b) } -func (k Keeper) removeDelegatorBond(ctx sdk.Context, bond DelegatorBond) { +func (k Keeper) removeDelegation(ctx sdk.Context, bond Delegation) { store := ctx.KVStore(k.storeKey) - store.Delete(GetDelegatorBondKey(bond.DelegatorAddr, bond.CandidateAddr, k.cdc)) + store.Delete(GetDelegationKey(bond.DelegatorAddr, bond.ValidatorAddr, k.cdc)) } //_______________________________________________________________________ // load/save the global staking params -func (k Keeper) GetParams(ctx sdk.Context) (params Params) { - // check if cached before anything - if !k.params.equal(Params{}) { - return k.params - } +func (k Keeper) GetParams(ctx sdk.Context) Params { store := ctx.KVStore(k.storeKey) + return k.getParams(store) +} +func (k Keeper) getParams(store sdk.KVStore) (params Params) { b := store.Get(ParamKey) if b == nil { panic("Stored params should not have been nil") } - err := k.cdc.UnmarshalBinary(b, ¶ms) - if err != nil { - panic(err) - } + k.cdc.MustUnmarshalBinary(b, ¶ms) return } + +// Need a distinct function because setParams depends on an existing previous +// record of params to exist (to check if maxValidators has changed) - and we +// panic on retrieval if it doesn't exist - hence if we use setParams for the very +// first params set it will panic. +func (k Keeper) setNewParams(ctx sdk.Context, params Params) { + store := ctx.KVStore(k.storeKey) + b := k.cdc.MustMarshalBinary(params) + store.Set(ParamKey, b) +} + func (k Keeper) setParams(ctx sdk.Context, params Params) { store := ctx.KVStore(k.storeKey) - b, err := k.cdc.MarshalBinary(params) - if err != nil { - panic(err) + exParams := k.getParams(store) + + // if max validator count changes, must recalculate validator set + if exParams.MaxValidators != params.MaxValidators { + k.updateBondedValidatorsFull(ctx, store) } + b := k.cdc.MustMarshalBinary(params) store.Set(ParamKey, b) - k.params = Params{} // clear the cache } //_______________________________________________________________________ // load/save the pool func (k Keeper) GetPool(ctx sdk.Context) (pool Pool) { - // check if cached before anything - if !k.pool.equal(Pool{}) { - return k.pool - } store := ctx.KVStore(k.storeKey) + return k.getPool(store) +} +func (k Keeper) getPool(store sdk.KVStore) (pool Pool) { b := store.Get(PoolKey) if b == nil { panic("Stored pool should not have been nil") } - err := k.cdc.UnmarshalBinary(b, &pool) - if err != nil { - panic(err) // This error should never occur big problem if does - } + k.cdc.MustUnmarshalBinary(b, &pool) return } func (k Keeper) setPool(ctx sdk.Context, p Pool) { store := ctx.KVStore(k.storeKey) - b, err := k.cdc.MarshalBinary(p) - if err != nil { - panic(err) - } + b := k.cdc.MustMarshalBinary(p) store.Set(PoolKey, b) - k.pool = Pool{} //clear the cache +} + +//__________________________________________________________________________ + +// get the current in-block validator operation counter +func (k Keeper) getIntraTxCounter(ctx sdk.Context) int16 { + store := ctx.KVStore(k.storeKey) + b := store.Get(IntraTxCounterKey) + if b == nil { + return 0 + } + var counter int16 + k.cdc.MustUnmarshalBinary(b, &counter) + return counter +} + +// set the current in-block validator operation counter +func (k Keeper) setIntraTxCounter(ctx sdk.Context, counter int16) { + store := ctx.KVStore(k.storeKey) + bz := k.cdc.MustMarshalBinary(counter) + store.Set(IntraTxCounterKey, bz) +} + +//__________________________________________________________________________ + +// get the current validator on the cliff +func (k Keeper) getCliffValidator(ctx sdk.Context) []byte { + store := ctx.KVStore(k.storeKey) + return store.Get(ValidatorCliffKey) +} + +// get the current power of the validator on the cliff +func (k Keeper) getCliffValidatorPower(ctx sdk.Context) []byte { + store := ctx.KVStore(k.storeKey) + return store.Get(ValidatorPowerCliffKey) +} + +// set the current validator and power of the validator on the cliff +func (k Keeper) setCliffValidator(ctx sdk.Context, validator Validator, pool Pool) { + store := ctx.KVStore(k.storeKey) + bz := GetValidatorsByPowerKey(validator, pool) + store.Set(ValidatorPowerCliffKey, bz) + store.Set(ValidatorCliffKey, validator.Owner) +} + +//__________________________________________________________________________ + +// Implements ValidatorSet + +var _ sdk.ValidatorSet = Keeper{} + +// iterate through the active validator set and perform the provided function +func (k Keeper) IterateValidators(ctx sdk.Context, fn func(index int64, validator sdk.Validator) (stop bool)) { + store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey) + i := int64(0) + for ; iterator.Valid(); iterator.Next() { + bz := iterator.Value() + var validator Validator + k.cdc.MustUnmarshalBinary(bz, &validator) + stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to? + if stop { + break + } + i++ + } + iterator.Close() +} + +// iterate through the active validator set and perform the provided function +func (k Keeper) IterateValidatorsBonded(ctx sdk.Context, fn func(index int64, validator sdk.Validator) (stop bool)) { + store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedKey) + i := int64(0) + for ; iterator.Valid(); iterator.Next() { + address := iterator.Value() + validator, found := k.getValidator(store, address) + if !found { + panic(fmt.Sprintf("validator record not found for address: %v\n", address)) + } + + stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to? + if stop { + break + } + i++ + } + iterator.Close() +} + +// get the sdk.validator for a particular address +func (k Keeper) Validator(ctx sdk.Context, addr sdk.Address) sdk.Validator { + val, found := k.GetValidator(ctx, addr) + if !found { + return nil + } + return val +} + +// total power from the bond +func (k Keeper) TotalPower(ctx sdk.Context) sdk.Rat { + pool := k.GetPool(ctx) + return pool.BondedShares +} + +//__________________________________________________________________________ + +// Implements DelegationSet + +var _ sdk.ValidatorSet = Keeper{} + +// get the delegation for a particular set of delegator and validator addresses +func (k Keeper) Delegation(ctx sdk.Context, addrDel sdk.Address, addrVal sdk.Address) sdk.Delegation { + bond, ok := k.GetDelegation(ctx, addrDel, addrVal) + if !ok { + return nil + } + return bond +} + +// iterate through the active validator set and perform the provided function +func (k Keeper) IterateDelegators(ctx sdk.Context, delAddr sdk.Address, fn func(index int64, delegation sdk.Delegation) (stop bool)) { + store := ctx.KVStore(k.storeKey) + key := GetDelegationsKey(delAddr, k.cdc) + iterator := sdk.KVStorePrefixIterator(store, key) + i := int64(0) + for ; iterator.Valid(); iterator.Next() { + bz := iterator.Value() + var delegation Delegation + k.cdc.MustUnmarshalBinary(bz, &delegation) + stop := fn(i, delegation) // XXX is this safe will the fields be able to get written to? + if stop { + break + } + i++ + } + iterator.Close() } diff --git a/x/stake/keeper_keys.go b/x/stake/keeper_keys.go index eb641b883..5a84d08f2 100644 --- a/x/stake/keeper_keys.go +++ b/x/stake/keeper_keys.go @@ -5,6 +5,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" + crypto "github.com/tendermint/go-crypto" ) // TODO remove some of these prefixes once have working multistore @@ -12,67 +13,64 @@ import ( //nolint var ( // Keys for store prefixes - ParamKey = []byte{0x00} // key for global parameters relating to staking - PoolKey = []byte{0x01} // key for global parameters relating to staking - CandidatesKey = []byte{0x02} // prefix for each key to a candidate - ValidatorsKey = []byte{0x03} // prefix for each key to a validator - AccUpdateValidatorsKey = []byte{0x04} // prefix for each key to a validator which is being updated - RecentValidatorsKey = []byte{0x05} // prefix for each key to the last updated validator group - - ToKickOutValidatorsKey = []byte{0x06} // prefix for each key to the last updated validator group - - DelegatorBondKeyPrefix = []byte{0x07} // prefix for each key to a delegator's bond - - CounterKey = []byte{0x08} // key for block-local tx index + ParamKey = []byte{0x00} // key for parameters relating to staking + PoolKey = []byte{0x01} // key for the staking pools + ValidatorsKey = []byte{0x02} // prefix for each key to a validator + ValidatorsBondedKey = []byte{0x03} // prefix for each key to bonded/actively validating validators + ValidatorsByPowerKey = []byte{0x04} // prefix for each key to a validator sorted by power + ValidatorCliffKey = []byte{0x05} // key for block-local tx index + ValidatorPowerCliffKey = []byte{0x06} // key for block-local tx index + TendermintUpdatesKey = []byte{0x07} // prefix for each key to a validator which is being updated + DelegationKey = []byte{0x08} // prefix for each key to a delegator's bond + IntraTxCounterKey = []byte{0x09} // key for block-local tx index ) const maxDigitsForAccount = 12 // ~220,000,000 atoms created at launch -// get the key for the candidate with address -func GetCandidateKey(addr sdk.Address) []byte { - return append(CandidatesKey, addr.Bytes()...) +// get the key for the validator with address +func GetValidatorKey(ownerAddr sdk.Address) []byte { + return append(ValidatorsKey, ownerAddr.Bytes()...) +} + +// get the key for the current validator group, ordered like tendermint +func GetValidatorsBondedKey(pk crypto.PubKey) []byte { + addr := pk.Address() + return append(ValidatorsBondedKey, addr.Bytes()...) } // get the key for the validator used in the power-store -func GetValidatorKey(addr sdk.Address, power sdk.Rat, height int64, counter int16, cdc *wire.Codec) []byte { +func GetValidatorsByPowerKey(validator Validator, pool Pool) []byte { + + power := validator.EquivalentBondedShares(pool) powerBytes := []byte(power.ToLeftPadded(maxDigitsForAccount)) // power big-endian (more powerful validators first) + + // TODO ensure that the key will be a readable string.. probably should add seperators and have + // heightBytes and counterBytes represent strings like powerBytes does heightBytes := make([]byte, binary.MaxVarintLen64) - binary.BigEndian.PutUint64(heightBytes, ^uint64(height)) // invert height (older validators first) + binary.BigEndian.PutUint64(heightBytes, ^uint64(validator.BondHeight)) // invert height (older validators first) counterBytes := make([]byte, 2) - binary.BigEndian.PutUint16(counterBytes, ^uint16(counter)) // invert counter (first txns have priority) - return append(ValidatorsKey, append(powerBytes, append(heightBytes, append(counterBytes, addr.Bytes()...)...)...)...) + binary.BigEndian.PutUint16(counterBytes, ^uint16(validator.BondIntraTxCounter)) // invert counter (first txns have priority) + return append(ValidatorsByPowerKey, + append(powerBytes, + append(heightBytes, + append(counterBytes, validator.Owner.Bytes()...)...)...)...) // TODO don't technically need to store owner } // get the key for the accumulated update validators -func GetAccUpdateValidatorKey(addr sdk.Address) []byte { - return append(AccUpdateValidatorsKey, addr.Bytes()...) +func GetTendermintUpdatesKey(ownerAddr sdk.Address) []byte { + return append(TendermintUpdatesKey, ownerAddr.Bytes()...) } -// get the key for the accumulated update validators -func GetRecentValidatorKey(addr sdk.Address) []byte { - return append(RecentValidatorsKey, addr.Bytes()...) +// get the key for delegator bond with validator +func GetDelegationKey(delegatorAddr, validatorAddr sdk.Address, cdc *wire.Codec) []byte { + return append(GetDelegationsKey(delegatorAddr, cdc), validatorAddr.Bytes()...) } -// reverse operation of GetRecentValidatorKey -func AddrFromKey(key []byte) sdk.Address { - return key[1:] -} - -// get the key for the accumulated update validators -func GetToKickOutValidatorKey(addr sdk.Address) []byte { - return append(ToKickOutValidatorsKey, addr.Bytes()...) -} - -// get the key for delegator bond with candidate -func GetDelegatorBondKey(delegatorAddr, candidateAddr sdk.Address, cdc *wire.Codec) []byte { - return append(GetDelegatorBondsKey(delegatorAddr, cdc), candidateAddr.Bytes()...) -} - -// get the prefix for a delegator for all candidates -func GetDelegatorBondsKey(delegatorAddr sdk.Address, cdc *wire.Codec) []byte { +// get the prefix for a delegator for all validators +func GetDelegationsKey(delegatorAddr sdk.Address, cdc *wire.Codec) []byte { res, err := cdc.MarshalBinary(&delegatorAddr) if err != nil { panic(err) } - return append(DelegatorBondKeyPrefix, res...) + return append(DelegationKey, res...) } diff --git a/x/stake/keeper_test.go b/x/stake/keeper_test.go index 8156071f1..01d4434e8 100644 --- a/x/stake/keeper_test.go +++ b/x/stake/keeper_test.go @@ -23,665 +23,728 @@ var ( } ) -// This function tests GetCandidate, GetCandidates, setCandidate, removeCandidate -func TestCandidate(t *testing.T) { +func TestSetValidator(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) + pool := keeper.GetPool(ctx) - //construct the candidates - var candidates [3]Candidate + // test how the validator is set from a purely unbonbed pool + validator := NewValidator(addrVals[0], pks[0], Description{}) + validator, pool, _ = validator.addTokensFromDel(pool, 10) + require.Equal(t, sdk.Unbonded, validator.Status()) + assert.True(sdk.RatEq(t, sdk.NewRat(10), validator.PoolShares.Unbonded())) + assert.True(sdk.RatEq(t, sdk.NewRat(10), validator.DelegatorShares)) + keeper.setPool(ctx, pool) + keeper.updateValidator(ctx, validator) + + // after the save the validator should be bonded + validator, found := keeper.GetValidator(ctx, addrVals[0]) + require.True(t, found) + require.Equal(t, sdk.Bonded, validator.Status()) + assert.True(sdk.RatEq(t, sdk.NewRat(10), validator.PoolShares.Bonded())) + assert.True(sdk.RatEq(t, sdk.NewRat(10), validator.DelegatorShares)) + + // Check each store for being saved + resVal, found := keeper.GetValidator(ctx, addrVals[0]) + assert.True(ValEq(t, validator, resVal)) + + resVals := keeper.GetValidatorsBonded(ctx) + require.Equal(t, 1, len(resVals)) + assert.True(ValEq(t, validator, resVals[0])) + + resVals = keeper.GetValidatorsByPower(ctx) + require.Equal(t, 1, len(resVals)) + assert.True(ValEq(t, validator, resVals[0])) + + updates := keeper.getTendermintUpdates(ctx) + require.Equal(t, 1, len(updates)) + assert.Equal(t, validator.abciValidator(keeper.cdc), updates[0]) + +} + +// This function tests updateValidator, GetValidator, GetValidatorsBonded, removeValidator +func TestValidatorBasics(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + pool := keeper.GetPool(ctx) + + //construct the validators + var validators [3]Validator amts := []int64{9, 8, 7} for i, amt := range amts { - candidates[i] = Candidate{ - Address: addrVals[i], - PubKey: pks[i], - Assets: sdk.NewRat(amt), - Liabilities: sdk.NewRat(amt), - } + validators[i] = NewValidator(addrVals[i], pks[i], Description{}) + validators[i].PoolShares = NewUnbondedShares(sdk.ZeroRat()) + validators[i].addTokensFromDel(pool, amt) } // check the empty keeper first - _, found := keeper.GetCandidate(ctx, addrVals[0]) + _, found := keeper.GetValidator(ctx, addrVals[0]) assert.False(t, found) - resCands := keeper.GetCandidates(ctx, 100) - assert.Zero(t, len(resCands)) + resVals := keeper.GetValidatorsBonded(ctx) + assert.Zero(t, len(resVals)) // set and retrieve a record - keeper.setCandidate(ctx, candidates[0]) - resCand, found := keeper.GetCandidate(ctx, addrVals[0]) + validators[0] = keeper.updateValidator(ctx, validators[0]) + resVal, found := keeper.GetValidator(ctx, addrVals[0]) require.True(t, found) - assert.True(t, candidatesEqual(candidates[0], resCand), "%v \n %v", resCand, candidates[0]) + assert.True(ValEq(t, validators[0], resVal)) + + resVals = keeper.GetValidatorsBonded(ctx) + require.Equal(t, 1, len(resVals)) + assert.True(ValEq(t, validators[0], resVals[0])) // modify a records, save, and retrieve - candidates[0].Liabilities = sdk.NewRat(99) - keeper.setCandidate(ctx, candidates[0]) - resCand, found = keeper.GetCandidate(ctx, addrVals[0]) + validators[0].PoolShares = NewBondedShares(sdk.NewRat(10)) + validators[0].DelegatorShares = sdk.NewRat(10) + validators[0] = keeper.updateValidator(ctx, validators[0]) + resVal, found = keeper.GetValidator(ctx, addrVals[0]) require.True(t, found) - assert.True(t, candidatesEqual(candidates[0], resCand)) + assert.True(ValEq(t, validators[0], resVal)) - // also test that the address has been added to address list - resCands = keeper.GetCandidates(ctx, 100) - require.Equal(t, 1, len(resCands)) - assert.Equal(t, addrVals[0], resCands[0].Address) + resVals = keeper.GetValidatorsBonded(ctx) + require.Equal(t, 1, len(resVals)) + assert.True(ValEq(t, validators[0], resVals[0])) - // add other candidates - keeper.setCandidate(ctx, candidates[1]) - keeper.setCandidate(ctx, candidates[2]) - resCand, found = keeper.GetCandidate(ctx, addrVals[1]) + // add other validators + validators[1] = keeper.updateValidator(ctx, validators[1]) + validators[2] = keeper.updateValidator(ctx, validators[2]) + resVal, found = keeper.GetValidator(ctx, addrVals[1]) require.True(t, found) - assert.True(t, candidatesEqual(candidates[1], resCand), "%v \n %v", resCand, candidates[1]) - resCand, found = keeper.GetCandidate(ctx, addrVals[2]) + assert.True(ValEq(t, validators[1], resVal)) + resVal, found = keeper.GetValidator(ctx, addrVals[2]) require.True(t, found) - assert.True(t, candidatesEqual(candidates[2], resCand), "%v \n %v", resCand, candidates[2]) - resCands = keeper.GetCandidates(ctx, 100) - require.Equal(t, 3, len(resCands)) - assert.True(t, candidatesEqual(candidates[0], resCands[0]), "%v \n %v", resCands[0], candidates[0]) - assert.True(t, candidatesEqual(candidates[1], resCands[1]), "%v \n %v", resCands[1], candidates[1]) - assert.True(t, candidatesEqual(candidates[2], resCands[2]), "%v \n %v", resCands[2], candidates[2]) + assert.True(ValEq(t, validators[2], resVal)) + + resVals = keeper.GetValidatorsBonded(ctx) + require.Equal(t, 3, len(resVals)) + assert.True(ValEq(t, validators[0], resVals[2])) // order doesn't matter here + assert.True(ValEq(t, validators[1], resVals[0])) + assert.True(ValEq(t, validators[2], resVals[1])) // remove a record - keeper.removeCandidate(ctx, candidates[1].Address) - _, found = keeper.GetCandidate(ctx, addrVals[1]) + keeper.removeValidator(ctx, validators[1].Owner) + _, found = keeper.GetValidator(ctx, addrVals[1]) assert.False(t, found) } -// tests GetDelegatorBond, GetDelegatorBonds, SetDelegatorBond, removeDelegatorBond, GetBonds +// test how the validators are sorted, tests GetValidatorsByPower +func GetValidatorSortingUnmixed(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + + // initialize some validators into the state + amts := []int64{0, 100, 1, 400, 200} + n := len(amts) + var validators [5]Validator + for i, amt := range amts { + validators[i] = NewValidator(addrs[i], pks[i], Description{}) + validators[i].PoolShares = NewBondedShares(sdk.NewRat(amt)) + validators[i].DelegatorShares = sdk.NewRat(amt) + keeper.updateValidator(ctx, validators[i]) + } + + // first make sure everything made it in to the gotValidator group + resValidators := keeper.GetValidatorsByPower(ctx) + require.Equal(t, n, len(resValidators)) + assert.Equal(t, sdk.NewRat(400), resValidators[0].PoolShares.Bonded(), "%v", resValidators) + assert.Equal(t, sdk.NewRat(200), resValidators[1].PoolShares.Bonded(), "%v", resValidators) + assert.Equal(t, sdk.NewRat(100), resValidators[2].PoolShares.Bonded(), "%v", resValidators) + assert.Equal(t, sdk.NewRat(1), resValidators[3].PoolShares.Bonded(), "%v", resValidators) + assert.Equal(t, sdk.NewRat(0), resValidators[4].PoolShares.Bonded(), "%v", resValidators) + assert.Equal(t, validators[3].Owner, resValidators[0].Owner, "%v", resValidators) + assert.Equal(t, validators[4].Owner, resValidators[1].Owner, "%v", resValidators) + assert.Equal(t, validators[1].Owner, resValidators[2].Owner, "%v", resValidators) + assert.Equal(t, validators[2].Owner, resValidators[3].Owner, "%v", resValidators) + assert.Equal(t, validators[0].Owner, resValidators[4].Owner, "%v", resValidators) + + // test a basic increase in voting power + validators[3].PoolShares = NewBondedShares(sdk.NewRat(500)) + keeper.updateValidator(ctx, validators[3]) + resValidators = keeper.GetValidatorsByPower(ctx) + require.Equal(t, len(resValidators), n) + assert.True(ValEq(t, validators[3], resValidators[0])) + + // test a decrease in voting power + validators[3].PoolShares = NewBondedShares(sdk.NewRat(300)) + keeper.updateValidator(ctx, validators[3]) + resValidators = keeper.GetValidatorsByPower(ctx) + require.Equal(t, len(resValidators), n) + assert.True(ValEq(t, validators[3], resValidators[0])) + assert.True(ValEq(t, validators[4], resValidators[1])) + + // test equal voting power, different age + validators[3].PoolShares = NewBondedShares(sdk.NewRat(200)) + ctx = ctx.WithBlockHeight(10) + keeper.updateValidator(ctx, validators[3]) + resValidators = keeper.GetValidatorsByPower(ctx) + require.Equal(t, len(resValidators), n) + assert.True(ValEq(t, validators[3], resValidators[0])) + assert.True(ValEq(t, validators[4], resValidators[1])) + assert.Equal(t, int64(0), resValidators[0].BondHeight, "%v", resValidators) + assert.Equal(t, int64(0), resValidators[1].BondHeight, "%v", resValidators) + + // no change in voting power - no change in sort + ctx = ctx.WithBlockHeight(20) + keeper.updateValidator(ctx, validators[4]) + resValidators = keeper.GetValidatorsByPower(ctx) + require.Equal(t, len(resValidators), n) + assert.True(ValEq(t, validators[3], resValidators[0])) + assert.True(ValEq(t, validators[4], resValidators[1])) + + // change in voting power of both validators, both still in v-set, no age change + validators[3].PoolShares = NewBondedShares(sdk.NewRat(300)) + validators[4].PoolShares = NewBondedShares(sdk.NewRat(300)) + keeper.updateValidator(ctx, validators[3]) + resValidators = keeper.GetValidatorsByPower(ctx) + require.Equal(t, len(resValidators), n) + ctx = ctx.WithBlockHeight(30) + keeper.updateValidator(ctx, validators[4]) + resValidators = keeper.GetValidatorsByPower(ctx) + require.Equal(t, len(resValidators), n, "%v", resValidators) + assert.True(ValEq(t, validators[3], resValidators[0])) + assert.True(ValEq(t, validators[4], resValidators[1])) +} + +func GetValidatorSortingMixed(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + + // now 2 max resValidators + params := keeper.GetParams(ctx) + params.MaxValidators = 2 + keeper.setParams(ctx, params) + + // initialize some validators into the state + amts := []int64{0, 100, 1, 400, 200} + + n := len(amts) + var validators [5]Validator + for i, amt := range amts { + validators[i] = NewValidator(addrs[i], pks[i], Description{}) + validators[i].DelegatorShares = sdk.NewRat(amt) + } + validators[0].PoolShares = NewUnbondedShares(sdk.NewRat(amts[0])) + validators[1].PoolShares = NewUnbondedShares(sdk.NewRat(amts[1])) + validators[2].PoolShares = NewUnbondedShares(sdk.NewRat(amts[2])) + validators[3].PoolShares = NewBondedShares(sdk.NewRat(amts[3])) + validators[4].PoolShares = NewBondedShares(sdk.NewRat(amts[4])) + for i := range amts { + keeper.updateValidator(ctx, validators[i]) + } + val0, found := keeper.GetValidator(ctx, addrs[0]) + require.True(t, found) + val1, found := keeper.GetValidator(ctx, addrs[1]) + require.True(t, found) + val2, found := keeper.GetValidator(ctx, addrs[2]) + require.True(t, found) + val3, found := keeper.GetValidator(ctx, addrs[3]) + require.True(t, found) + val4, found := keeper.GetValidator(ctx, addrs[4]) + require.True(t, found) + assert.Equal(t, sdk.Unbonded, val0.Status()) + assert.Equal(t, sdk.Unbonded, val1.Status()) + assert.Equal(t, sdk.Unbonded, val2.Status()) + assert.Equal(t, sdk.Bonded, val3.Status()) + assert.Equal(t, sdk.Bonded, val4.Status()) + + // first make sure everything made it in to the gotValidator group + resValidators := keeper.GetValidatorsByPower(ctx) + require.Equal(t, n, len(resValidators)) + assert.Equal(t, sdk.NewRat(400), resValidators[0].PoolShares.Bonded(), "%v", resValidators) + assert.Equal(t, sdk.NewRat(200), resValidators[1].PoolShares.Bonded(), "%v", resValidators) + assert.Equal(t, sdk.NewRat(100), resValidators[2].PoolShares.Bonded(), "%v", resValidators) + assert.Equal(t, sdk.NewRat(1), resValidators[3].PoolShares.Bonded(), "%v", resValidators) + assert.Equal(t, sdk.NewRat(0), resValidators[4].PoolShares.Bonded(), "%v", resValidators) + assert.Equal(t, validators[3].Owner, resValidators[0].Owner, "%v", resValidators) + assert.Equal(t, validators[4].Owner, resValidators[1].Owner, "%v", resValidators) + assert.Equal(t, validators[1].Owner, resValidators[2].Owner, "%v", resValidators) + assert.Equal(t, validators[2].Owner, resValidators[3].Owner, "%v", resValidators) + assert.Equal(t, validators[0].Owner, resValidators[4].Owner, "%v", resValidators) +} + +// TODO seperate out into multiple tests +func TestGetValidatorsEdgeCases(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + var found bool + + // now 2 max resValidators + params := keeper.GetParams(ctx) + nMax := uint16(2) + params.MaxValidators = nMax + keeper.setParams(ctx, params) + + // initialize some validators into the state + amts := []int64{0, 100, 400, 400} + var validators [4]Validator + for i, amt := range amts { + validators[i] = NewValidator(addrs[i], pks[i], Description{}) + validators[i].PoolShares = NewUnbondedShares(sdk.NewRat(amt)) + validators[i].DelegatorShares = sdk.NewRat(amt) + validators[i] = keeper.updateValidator(ctx, validators[i]) + } + for i := range amts { + validators[i], found = keeper.GetValidator(ctx, validators[i].Owner) + require.True(t, found) + } + resValidators := keeper.GetValidatorsByPower(ctx) + require.Equal(t, nMax, uint16(len(resValidators))) + assert.True(ValEq(t, validators[2], resValidators[0])) + assert.True(ValEq(t, validators[3], resValidators[1])) + + validators[0].PoolShares = NewUnbondedShares(sdk.NewRat(500)) + validators[0] = keeper.updateValidator(ctx, validators[0]) + resValidators = keeper.GetValidatorsByPower(ctx) + require.Equal(t, nMax, uint16(len(resValidators))) + assert.True(ValEq(t, validators[0], resValidators[0])) + assert.True(ValEq(t, validators[2], resValidators[1])) + + // A validator which leaves the gotValidator set due to a decrease in voting power, + // then increases to the original voting power, does not get its spot back in the + // case of a tie. + + // validator 3 enters bonded validator set + ctx = ctx.WithBlockHeight(40) + + validators[3], found = keeper.GetValidator(ctx, validators[3].Owner) + require.True(t, found) + validators[3].PoolShares = NewUnbondedShares(sdk.NewRat(401)) + validators[3] = keeper.updateValidator(ctx, validators[3]) + resValidators = keeper.GetValidatorsByPower(ctx) + require.Equal(t, nMax, uint16(len(resValidators))) + assert.True(ValEq(t, validators[0], resValidators[0])) + assert.True(ValEq(t, validators[3], resValidators[1])) + + // validator 3 kicked out temporarily + validators[3].PoolShares = NewBondedShares(sdk.NewRat(200)) + validators[3] = keeper.updateValidator(ctx, validators[3]) + resValidators = keeper.GetValidatorsByPower(ctx) + require.Equal(t, nMax, uint16(len(resValidators))) + assert.True(ValEq(t, validators[0], resValidators[0])) + assert.True(ValEq(t, validators[2], resValidators[1])) + + // validator 4 does not get spot back + validators[3].PoolShares = NewBondedShares(sdk.NewRat(400)) + validators[3] = keeper.updateValidator(ctx, validators[3]) + resValidators = keeper.GetValidatorsByPower(ctx) + require.Equal(t, nMax, uint16(len(resValidators))) + assert.True(ValEq(t, validators[0], resValidators[0])) + assert.True(ValEq(t, validators[2], resValidators[1])) + validator, exists := keeper.GetValidator(ctx, validators[3].Owner) + require.Equal(t, exists, true) + require.Equal(t, int64(40), validator.BondHeight) +} + +func TestValidatorBondHeight(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + + // now 2 max resValidators + params := keeper.GetParams(ctx) + params.MaxValidators = 2 + keeper.setParams(ctx, params) + + // initialize some validators into the state + var validators [3]Validator + validators[0] = NewValidator(addrs[0], pks[0], Description{}) + validators[0].PoolShares = NewUnbondedShares(sdk.NewRat(200)) + validators[0].DelegatorShares = sdk.NewRat(200) + validators[1] = NewValidator(addrs[1], pks[1], Description{}) + validators[1].PoolShares = NewUnbondedShares(sdk.NewRat(100)) + validators[1].DelegatorShares = sdk.NewRat(100) + validators[2] = NewValidator(addrs[2], pks[2], Description{}) + validators[2].PoolShares = NewUnbondedShares(sdk.NewRat(100)) + validators[2].DelegatorShares = sdk.NewRat(100) + + validators[0] = keeper.updateValidator(ctx, validators[0]) + //////////////////////////////////////// + // If two validators both increase to the same voting power in the same block, + // the one with the first transaction should become bonded + validators[1] = keeper.updateValidator(ctx, validators[1]) + validators[2] = keeper.updateValidator(ctx, validators[2]) + resValidators := keeper.GetValidatorsByPower(ctx) + require.Equal(t, uint16(len(resValidators)), params.MaxValidators) + + assert.True(ValEq(t, validators[0], resValidators[0])) + assert.True(ValEq(t, validators[1], resValidators[1])) + validators[1].PoolShares = NewUnbondedShares(sdk.NewRat(150)) + validators[2].PoolShares = NewUnbondedShares(sdk.NewRat(150)) + validators[2] = keeper.updateValidator(ctx, validators[2]) + resValidators = keeper.GetValidatorsByPower(ctx) + require.Equal(t, params.MaxValidators, uint16(len(resValidators))) + validators[1] = keeper.updateValidator(ctx, validators[1]) + assert.True(ValEq(t, validators[0], resValidators[0])) + assert.True(ValEq(t, validators[2], resValidators[1])) +} + +func TestFullValidatorSetPowerChange(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + params := keeper.GetParams(ctx) + max := 2 + params.MaxValidators = uint16(2) + keeper.setParams(ctx, params) + + // initialize some validators into the state + amts := []int64{0, 100, 400, 400, 200} + var validators [5]Validator + for i, amt := range amts { + validators[i] = NewValidator(addrs[i], pks[i], Description{}) + validators[i].PoolShares = NewUnbondedShares(sdk.NewRat(amt)) + validators[i].DelegatorShares = sdk.NewRat(amt) + keeper.updateValidator(ctx, validators[i]) + } + for i := range amts { + var found bool + validators[i], found = keeper.GetValidator(ctx, validators[i].Owner) + require.True(t, found) + } + assert.Equal(t, sdk.Unbonded, validators[0].Status()) + assert.Equal(t, sdk.Unbonded, validators[1].Status()) + assert.Equal(t, sdk.Bonded, validators[2].Status()) + assert.Equal(t, sdk.Bonded, validators[3].Status()) + assert.Equal(t, sdk.Unbonded, validators[4].Status()) + resValidators := keeper.GetValidatorsByPower(ctx) + require.Equal(t, max, len(resValidators)) + assert.True(ValEq(t, validators[2], resValidators[0])) // in the order of txs + assert.True(ValEq(t, validators[3], resValidators[1])) + + // test a swap in voting power + validators[0].PoolShares = NewUnbondedShares(sdk.NewRat(600)) + validators[0] = keeper.updateValidator(ctx, validators[0]) + resValidators = keeper.GetValidatorsByPower(ctx) + require.Equal(t, max, len(resValidators)) + assert.True(ValEq(t, validators[0], resValidators[0])) + assert.True(ValEq(t, validators[2], resValidators[1])) +} + +// clear the tracked changes to the gotValidator set +func TestClearTendermintUpdates(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + + amts := []int64{100, 400, 200} + validators := make([]Validator, len(amts)) + for i, amt := range amts { + validators[i] = NewValidator(addrs[i], pks[i], Description{}) + validators[i].PoolShares = NewUnbondedShares(sdk.NewRat(amt)) + validators[i].DelegatorShares = sdk.NewRat(amt) + keeper.updateValidator(ctx, validators[i]) + } + + updates := keeper.getTendermintUpdates(ctx) + assert.Equal(t, len(amts), len(updates)) + keeper.clearTendermintUpdates(ctx) + updates = keeper.getTendermintUpdates(ctx) + assert.Equal(t, 0, len(updates)) +} + +func TestGetTendermintUpdatesAllNone(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + + amts := []int64{10, 20} + var validators [2]Validator + for i, amt := range amts { + validators[i] = NewValidator(addrs[i], pks[i], Description{}) + validators[i].PoolShares = NewUnbondedShares(sdk.NewRat(amt)) + validators[i].DelegatorShares = sdk.NewRat(amt) + } + + // test from nothing to something + // tendermintUpdate set: {} -> {c1, c3} + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) + validators[0] = keeper.updateValidator(ctx, validators[0]) + validators[1] = keeper.updateValidator(ctx, validators[1]) + + updates := keeper.getTendermintUpdates(ctx) + require.Equal(t, 2, len(updates)) + assert.Equal(t, validators[0].abciValidator(keeper.cdc), updates[0]) + assert.Equal(t, validators[1].abciValidator(keeper.cdc), updates[1]) + + // test from something to nothing + // tendermintUpdate set: {} -> {c1, c2, c3, c4} + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) + + keeper.removeValidator(ctx, validators[0].Owner) + keeper.removeValidator(ctx, validators[1].Owner) + + updates = keeper.getTendermintUpdates(ctx) + require.Equal(t, 2, len(updates)) + assert.Equal(t, validators[0].PubKey.Bytes(), updates[0].PubKey) + assert.Equal(t, validators[1].PubKey.Bytes(), updates[1].PubKey) + assert.Equal(t, int64(0), updates[0].Power) + assert.Equal(t, int64(0), updates[1].Power) +} + +func TestGetTendermintUpdatesIdentical(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + + amts := []int64{10, 20} + var validators [2]Validator + for i, amt := range amts { + validators[i] = NewValidator(addrs[i], pks[i], Description{}) + validators[i].PoolShares = NewUnbondedShares(sdk.NewRat(amt)) + validators[i].DelegatorShares = sdk.NewRat(amt) + } + validators[0] = keeper.updateValidator(ctx, validators[0]) + validators[1] = keeper.updateValidator(ctx, validators[1]) + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) + + // test identical, + // tendermintUpdate set: {} -> {} + validators[0] = keeper.updateValidator(ctx, validators[0]) + validators[1] = keeper.updateValidator(ctx, validators[1]) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) +} + +func TestGetTendermintUpdatesSingleValueChange(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + + amts := []int64{10, 20} + var validators [2]Validator + for i, amt := range amts { + validators[i] = NewValidator(addrs[i], pks[i], Description{}) + validators[i].PoolShares = NewUnbondedShares(sdk.NewRat(amt)) + validators[i].DelegatorShares = sdk.NewRat(amt) + } + validators[0] = keeper.updateValidator(ctx, validators[0]) + validators[1] = keeper.updateValidator(ctx, validators[1]) + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) + + // test single value change + // tendermintUpdate set: {} -> {c1'} + validators[0].PoolShares = NewBondedShares(sdk.NewRat(600)) + validators[0] = keeper.updateValidator(ctx, validators[0]) + + updates := keeper.getTendermintUpdates(ctx) + + require.Equal(t, 1, len(updates)) + assert.Equal(t, validators[0].abciValidator(keeper.cdc), updates[0]) +} + +func TestGetTendermintUpdatesMultipleValueChange(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + + amts := []int64{10, 20} + var validators [2]Validator + for i, amt := range amts { + validators[i] = NewValidator(addrs[i], pks[i], Description{}) + validators[i].PoolShares = NewUnbondedShares(sdk.NewRat(amt)) + validators[i].DelegatorShares = sdk.NewRat(amt) + } + validators[0] = keeper.updateValidator(ctx, validators[0]) + validators[1] = keeper.updateValidator(ctx, validators[1]) + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) + + // test multiple value change + // tendermintUpdate set: {c1, c3} -> {c1', c3'} + validators[0].PoolShares = NewBondedShares(sdk.NewRat(200)) + validators[1].PoolShares = NewBondedShares(sdk.NewRat(100)) + validators[0] = keeper.updateValidator(ctx, validators[0]) + validators[1] = keeper.updateValidator(ctx, validators[1]) + + updates := keeper.getTendermintUpdates(ctx) + require.Equal(t, 2, len(updates)) + require.Equal(t, validators[0].abciValidator(keeper.cdc), updates[0]) + require.Equal(t, validators[1].abciValidator(keeper.cdc), updates[1]) +} + +func TestGetTendermintUpdatesInserted(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + + amts := []int64{10, 20, 5, 15, 25} + var validators [5]Validator + for i, amt := range amts { + validators[i] = NewValidator(addrs[i], pks[i], Description{}) + validators[i].PoolShares = NewUnbondedShares(sdk.NewRat(amt)) + validators[i].DelegatorShares = sdk.NewRat(amt) + } + validators[0] = keeper.updateValidator(ctx, validators[0]) + validators[1] = keeper.updateValidator(ctx, validators[1]) + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) + + // test validtor added at the beginning + // tendermintUpdate set: {} -> {c0} + validators[2] = keeper.updateValidator(ctx, validators[2]) + updates := keeper.getTendermintUpdates(ctx) + require.Equal(t, 1, len(updates)) + require.Equal(t, validators[2].abciValidator(keeper.cdc), updates[0]) + + // test validtor added at the beginning + // tendermintUpdate set: {} -> {c0} + keeper.clearTendermintUpdates(ctx) + validators[3] = keeper.updateValidator(ctx, validators[3]) + updates = keeper.getTendermintUpdates(ctx) + require.Equal(t, 1, len(updates)) + require.Equal(t, validators[3].abciValidator(keeper.cdc), updates[0]) + + // test validtor added at the end + // tendermintUpdate set: {} -> {c0} + keeper.clearTendermintUpdates(ctx) + validators[4] = keeper.updateValidator(ctx, validators[4]) + updates = keeper.getTendermintUpdates(ctx) + require.Equal(t, 1, len(updates)) + require.Equal(t, validators[4].abciValidator(keeper.cdc), updates[0]) +} + +func TestGetTendermintUpdatesNotValidatorCliff(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + params := DefaultParams() + params.MaxValidators = 2 + keeper.setParams(ctx, params) + + amts := []int64{10, 20, 5} + var validators [5]Validator + for i, amt := range amts { + validators[i] = NewValidator(addrs[i], pks[i], Description{}) + validators[i].PoolShares = NewUnbondedShares(sdk.NewRat(amt)) + validators[i].DelegatorShares = sdk.NewRat(amt) + } + validators[0] = keeper.updateValidator(ctx, validators[0]) + validators[1] = keeper.updateValidator(ctx, validators[1]) + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) + + // test validator added at the end but not inserted in the valset + // tendermintUpdate set: {} -> {} + keeper.updateValidator(ctx, validators[2]) + updates := keeper.getTendermintUpdates(ctx) + require.Equal(t, 0, len(updates)) + + // test validator change its power and become a gotValidator (pushing out an existing) + // tendermintUpdate set: {} -> {c0, c4} + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) + + validators[2].PoolShares = NewUnbondedShares(sdk.NewRat(15)) + validators[2] = keeper.updateValidator(ctx, validators[2]) + + updates = keeper.getTendermintUpdates(ctx) + require.Equal(t, 2, len(updates), "%v", updates) + require.Equal(t, validators[0].abciValidatorZero(keeper.cdc), updates[0]) + require.Equal(t, validators[2].abciValidator(keeper.cdc), updates[1]) +} + +// tests GetDelegation, GetDelegations, SetDelegation, removeDelegation, GetBonds func TestBond(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) - //construct the candidates + //construct the validators amts := []int64{9, 8, 7} - var candidates [3]Candidate + var validators [3]Validator for i, amt := range amts { - candidates[i] = Candidate{ - Address: addrVals[i], - PubKey: pks[i], - Assets: sdk.NewRat(amt), - Liabilities: sdk.NewRat(amt), - } + validators[i] = NewValidator(addrVals[i], pks[i], Description{}) + validators[i].PoolShares = NewUnbondedShares(sdk.NewRat(amt)) + validators[i].DelegatorShares = sdk.NewRat(amt) } - // first add a candidates[0] to delegate too - keeper.setCandidate(ctx, candidates[0]) + // first add a validators[0] to delegate too + validators[0] = keeper.updateValidator(ctx, validators[0]) - bond1to1 := DelegatorBond{ + bond1to1 := Delegation{ DelegatorAddr: addrDels[0], - CandidateAddr: addrVals[0], + ValidatorAddr: addrVals[0], Shares: sdk.NewRat(9), } // check the empty keeper first - _, found := keeper.GetDelegatorBond(ctx, addrDels[0], addrVals[0]) + _, found := keeper.GetDelegation(ctx, addrDels[0], addrVals[0]) assert.False(t, found) // set and retrieve a record - keeper.setDelegatorBond(ctx, bond1to1) - resBond, found := keeper.GetDelegatorBond(ctx, addrDels[0], addrVals[0]) + keeper.setDelegation(ctx, bond1to1) + resBond, found := keeper.GetDelegation(ctx, addrDels[0], addrVals[0]) assert.True(t, found) - assert.True(t, bondsEqual(bond1to1, resBond)) + assert.True(t, bond1to1.equal(resBond)) // modify a records, save, and retrieve bond1to1.Shares = sdk.NewRat(99) - keeper.setDelegatorBond(ctx, bond1to1) - resBond, found = keeper.GetDelegatorBond(ctx, addrDels[0], addrVals[0]) + keeper.setDelegation(ctx, bond1to1) + resBond, found = keeper.GetDelegation(ctx, addrDels[0], addrVals[0]) assert.True(t, found) - assert.True(t, bondsEqual(bond1to1, resBond)) + assert.True(t, bond1to1.equal(resBond)) // add some more records - keeper.setCandidate(ctx, candidates[1]) - keeper.setCandidate(ctx, candidates[2]) - bond1to2 := DelegatorBond{addrDels[0], addrVals[1], sdk.NewRat(9), 0} - bond1to3 := DelegatorBond{addrDels[0], addrVals[2], sdk.NewRat(9), 1} - bond2to1 := DelegatorBond{addrDels[1], addrVals[0], sdk.NewRat(9), 2} - bond2to2 := DelegatorBond{addrDels[1], addrVals[1], sdk.NewRat(9), 3} - bond2to3 := DelegatorBond{addrDels[1], addrVals[2], sdk.NewRat(9), 4} - keeper.setDelegatorBond(ctx, bond1to2) - keeper.setDelegatorBond(ctx, bond1to3) - keeper.setDelegatorBond(ctx, bond2to1) - keeper.setDelegatorBond(ctx, bond2to2) - keeper.setDelegatorBond(ctx, bond2to3) + validators[1] = keeper.updateValidator(ctx, validators[1]) + validators[2] = keeper.updateValidator(ctx, validators[2]) + bond1to2 := Delegation{addrDels[0], addrVals[1], sdk.NewRat(9), 0} + bond1to3 := Delegation{addrDels[0], addrVals[2], sdk.NewRat(9), 1} + bond2to1 := Delegation{addrDels[1], addrVals[0], sdk.NewRat(9), 2} + bond2to2 := Delegation{addrDels[1], addrVals[1], sdk.NewRat(9), 3} + bond2to3 := Delegation{addrDels[1], addrVals[2], sdk.NewRat(9), 4} + keeper.setDelegation(ctx, bond1to2) + keeper.setDelegation(ctx, bond1to3) + keeper.setDelegation(ctx, bond2to1) + keeper.setDelegation(ctx, bond2to2) + keeper.setDelegation(ctx, bond2to3) // test all bond retrieve capabilities - resBonds := keeper.GetDelegatorBonds(ctx, addrDels[0], 5) + resBonds := keeper.GetDelegations(ctx, addrDels[0], 5) require.Equal(t, 3, len(resBonds)) - assert.True(t, bondsEqual(bond1to1, resBonds[0])) - assert.True(t, bondsEqual(bond1to2, resBonds[1])) - assert.True(t, bondsEqual(bond1to3, resBonds[2])) - resBonds = keeper.GetDelegatorBonds(ctx, addrDels[0], 3) + assert.True(t, bond1to1.equal(resBonds[0])) + assert.True(t, bond1to2.equal(resBonds[1])) + assert.True(t, bond1to3.equal(resBonds[2])) + resBonds = keeper.GetDelegations(ctx, addrDels[0], 3) require.Equal(t, 3, len(resBonds)) - resBonds = keeper.GetDelegatorBonds(ctx, addrDels[0], 2) + resBonds = keeper.GetDelegations(ctx, addrDels[0], 2) require.Equal(t, 2, len(resBonds)) - resBonds = keeper.GetDelegatorBonds(ctx, addrDels[1], 5) + resBonds = keeper.GetDelegations(ctx, addrDels[1], 5) require.Equal(t, 3, len(resBonds)) - assert.True(t, bondsEqual(bond2to1, resBonds[0])) - assert.True(t, bondsEqual(bond2to2, resBonds[1])) - assert.True(t, bondsEqual(bond2to3, resBonds[2])) - allBonds := keeper.getBonds(ctx, 1000) + assert.True(t, bond2to1.equal(resBonds[0])) + assert.True(t, bond2to2.equal(resBonds[1])) + assert.True(t, bond2to3.equal(resBonds[2])) + allBonds := keeper.getAllDelegations(ctx) require.Equal(t, 6, len(allBonds)) - assert.True(t, bondsEqual(bond1to1, allBonds[0])) - assert.True(t, bondsEqual(bond1to2, allBonds[1])) - assert.True(t, bondsEqual(bond1to3, allBonds[2])) - assert.True(t, bondsEqual(bond2to1, allBonds[3])) - assert.True(t, bondsEqual(bond2to2, allBonds[4])) - assert.True(t, bondsEqual(bond2to3, allBonds[5])) + assert.True(t, bond1to1.equal(allBonds[0])) + assert.True(t, bond1to2.equal(allBonds[1])) + assert.True(t, bond1to3.equal(allBonds[2])) + assert.True(t, bond2to1.equal(allBonds[3])) + assert.True(t, bond2to2.equal(allBonds[4])) + assert.True(t, bond2to3.equal(allBonds[5])) // delete a record - keeper.removeDelegatorBond(ctx, bond2to3) - _, found = keeper.GetDelegatorBond(ctx, addrDels[1], addrVals[2]) + keeper.removeDelegation(ctx, bond2to3) + _, found = keeper.GetDelegation(ctx, addrDels[1], addrVals[2]) assert.False(t, found) - resBonds = keeper.GetDelegatorBonds(ctx, addrDels[1], 5) + resBonds = keeper.GetDelegations(ctx, addrDels[1], 5) require.Equal(t, 2, len(resBonds)) - assert.True(t, bondsEqual(bond2to1, resBonds[0])) - assert.True(t, bondsEqual(bond2to2, resBonds[1])) + assert.True(t, bond2to1.equal(resBonds[0])) + assert.True(t, bond2to2.equal(resBonds[1])) // delete all the records from delegator 2 - keeper.removeDelegatorBond(ctx, bond2to1) - keeper.removeDelegatorBond(ctx, bond2to2) - _, found = keeper.GetDelegatorBond(ctx, addrDels[1], addrVals[0]) + keeper.removeDelegation(ctx, bond2to1) + keeper.removeDelegation(ctx, bond2to2) + _, found = keeper.GetDelegation(ctx, addrDels[1], addrVals[0]) assert.False(t, found) - _, found = keeper.GetDelegatorBond(ctx, addrDels[1], addrVals[1]) + _, found = keeper.GetDelegation(ctx, addrDels[1], addrVals[1]) assert.False(t, found) - resBonds = keeper.GetDelegatorBonds(ctx, addrDels[1], 5) + resBonds = keeper.GetDelegations(ctx, addrDels[1], 5) require.Equal(t, 0, len(resBonds)) } -// TODO integrate in testing for equal validators, whichever one was a validator -// first remains the validator https://github.com/cosmos/cosmos-sdk/issues/582 -func TestGetValidators(t *testing.T) { - ctx, _, keeper := createTestInput(t, false, 0) - - // initialize some candidates into the state - amts := []int64{0, 100, 1, 400, 200} - n := len(amts) - var candidates [5]Candidate - for i, amt := range amts { - c := Candidate{ - Status: Unbonded, - PubKey: pks[i], - Address: addrs[i], - Assets: sdk.NewRat(amt), - Liabilities: sdk.NewRat(amt), - } - keeper.setCandidate(ctx, c) - candidates[i] = c - } - - // first make sure everything as normal is ordered - validators := keeper.GetValidators(ctx) - require.Equal(t, len(validators), n) - assert.Equal(t, sdk.NewRat(400), validators[0].Power, "%v", validators) - assert.Equal(t, sdk.NewRat(200), validators[1].Power, "%v", validators) - assert.Equal(t, sdk.NewRat(100), validators[2].Power, "%v", validators) - assert.Equal(t, sdk.NewRat(1), validators[3].Power, "%v", validators) - assert.Equal(t, sdk.NewRat(0), validators[4].Power, "%v", validators) - assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) - assert.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators) - assert.Equal(t, candidates[1].Address, validators[2].Address, "%v", validators) - assert.Equal(t, candidates[2].Address, validators[3].Address, "%v", validators) - assert.Equal(t, candidates[0].Address, validators[4].Address, "%v", validators) - - // test a basic increase in voting power - candidates[3].Assets = sdk.NewRat(500) - keeper.setCandidate(ctx, candidates[3]) - validators = keeper.GetValidators(ctx) - require.Equal(t, len(validators), n) - assert.Equal(t, sdk.NewRat(500), validators[0].Power, "%v", validators) - assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) - - // test a decrease in voting power - candidates[3].Assets = sdk.NewRat(300) - keeper.setCandidate(ctx, candidates[3]) - validators = keeper.GetValidators(ctx) - require.Equal(t, len(validators), n) - assert.Equal(t, sdk.NewRat(300), validators[0].Power, "%v", validators) - assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) - - // test equal voting power, different age - candidates[3].Assets = sdk.NewRat(200) - ctx = ctx.WithBlockHeight(10) - keeper.setCandidate(ctx, candidates[3]) - validators = keeper.GetValidators(ctx) - require.Equal(t, len(validators), n) - assert.Equal(t, sdk.NewRat(200), validators[0].Power, "%v", validators) - assert.Equal(t, sdk.NewRat(200), validators[1].Power, "%v", validators) - assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) - assert.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators) - assert.Equal(t, int64(0), validators[0].Height, "%v", validators) - assert.Equal(t, int64(0), validators[1].Height, "%v", validators) - - // no change in voting power - no change in sort - ctx = ctx.WithBlockHeight(20) - keeper.setCandidate(ctx, candidates[4]) - validators = keeper.GetValidators(ctx) - require.Equal(t, len(validators), n) - assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) - assert.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators) - - // change in voting power of both candidates, both still in v-set, no age change - candidates[3].Assets = sdk.NewRat(300) - candidates[4].Assets = sdk.NewRat(300) - keeper.setCandidate(ctx, candidates[3]) - validators = keeper.GetValidators(ctx) - require.Equal(t, len(validators), n) - ctx = ctx.WithBlockHeight(30) - keeper.setCandidate(ctx, candidates[4]) - validators = keeper.GetValidators(ctx) - require.Equal(t, len(validators), n, "%v", validators) - assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) - assert.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators) - - // now 2 max validators - params := keeper.GetParams(ctx) - params.MaxValidators = 2 - keeper.setParams(ctx, params) - candidates[0].Assets = sdk.NewRat(500) - keeper.setCandidate(ctx, candidates[0]) - validators = keeper.GetValidators(ctx) - require.Equal(t, uint16(len(validators)), params.MaxValidators) - require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - // candidate 3 was set before candidate 4 - require.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators) - - /* - A candidate which leaves the validator set due to a decrease in voting power, - then increases to the original voting power, does not get its spot back in the - case of a tie. - - ref https://github.com/cosmos/cosmos-sdk/issues/582#issuecomment-380757108 - */ - candidates[4].Assets = sdk.NewRat(301) - keeper.setCandidate(ctx, candidates[4]) - validators = keeper.GetValidators(ctx) - require.Equal(t, uint16(len(validators)), params.MaxValidators) - require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - require.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators) - ctx = ctx.WithBlockHeight(40) - // candidate 4 kicked out temporarily - candidates[4].Assets = sdk.NewRat(200) - keeper.setCandidate(ctx, candidates[4]) - validators = keeper.GetValidators(ctx) - require.Equal(t, uint16(len(validators)), params.MaxValidators) - require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - require.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators) - // candidate 4 does not get spot back - candidates[4].Assets = sdk.NewRat(300) - keeper.setCandidate(ctx, candidates[4]) - validators = keeper.GetValidators(ctx) - require.Equal(t, uint16(len(validators)), params.MaxValidators) - require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - require.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators) - candidate, exists := keeper.GetCandidate(ctx, candidates[4].Address) - require.Equal(t, exists, true) - require.Equal(t, candidate.ValidatorBondHeight, int64(40)) - - /* - If two candidates both increase to the same voting power in the same block, - the one with the first transaction should take precedence (become a validator). - - ref https://github.com/cosmos/cosmos-sdk/issues/582#issuecomment-381250392 - */ - candidates[0].Assets = sdk.NewRat(2000) - keeper.setCandidate(ctx, candidates[0]) - candidates[1].Assets = sdk.NewRat(1000) - candidates[2].Assets = sdk.NewRat(1000) - keeper.setCandidate(ctx, candidates[1]) - keeper.setCandidate(ctx, candidates[2]) - validators = keeper.GetValidators(ctx) - require.Equal(t, uint16(len(validators)), params.MaxValidators) - require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - require.Equal(t, candidates[1].Address, validators[1].Address, "%v", validators) - candidates[1].Assets = sdk.NewRat(1100) - candidates[2].Assets = sdk.NewRat(1100) - keeper.setCandidate(ctx, candidates[2]) - keeper.setCandidate(ctx, candidates[1]) - validators = keeper.GetValidators(ctx) - require.Equal(t, uint16(len(validators)), params.MaxValidators) - require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - require.Equal(t, candidates[2].Address, validators[1].Address, "%v", validators) - - // reset assets / heights - params.MaxValidators = 100 - keeper.setParams(ctx, params) - candidates[0].Assets = sdk.NewRat(0) - candidates[1].Assets = sdk.NewRat(100) - candidates[2].Assets = sdk.NewRat(1) - candidates[3].Assets = sdk.NewRat(300) - candidates[4].Assets = sdk.NewRat(200) - ctx = ctx.WithBlockHeight(0) - keeper.setCandidate(ctx, candidates[0]) - keeper.setCandidate(ctx, candidates[1]) - keeper.setCandidate(ctx, candidates[2]) - keeper.setCandidate(ctx, candidates[3]) - keeper.setCandidate(ctx, candidates[4]) - - // test a swap in voting power - candidates[0].Assets = sdk.NewRat(600) - keeper.setCandidate(ctx, candidates[0]) - validators = keeper.GetValidators(ctx) - require.Equal(t, len(validators), n) - assert.Equal(t, sdk.NewRat(600), validators[0].Power, "%v", validators) - assert.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - assert.Equal(t, sdk.NewRat(300), validators[1].Power, "%v", validators) - assert.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators) - - // test the max validators term - params = keeper.GetParams(ctx) - n = 2 - params.MaxValidators = uint16(n) - keeper.setParams(ctx, params) - validators = keeper.GetValidators(ctx) - require.Equal(t, len(validators), n) - assert.Equal(t, sdk.NewRat(600), validators[0].Power, "%v", validators) - assert.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - assert.Equal(t, sdk.NewRat(300), validators[1].Power, "%v", validators) - assert.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators) -} - -// clear the tracked changes to the validator set -func TestClearAccUpdateValidators(t *testing.T) { - ctx, _, keeper := createTestInput(t, false, 0) - - amts := []int64{100, 400, 200} - candidates := make([]Candidate, len(amts)) - for i, amt := range amts { - c := Candidate{ - Status: Unbonded, - PubKey: pks[i], - Address: addrs[i], - Assets: sdk.NewRat(amt), - Liabilities: sdk.NewRat(amt), - } - candidates[i] = c - keeper.setCandidate(ctx, c) - } - - acc := keeper.getAccUpdateValidators(ctx) - assert.Equal(t, len(amts), len(acc)) - keeper.clearAccUpdateValidators(ctx) - acc = keeper.getAccUpdateValidators(ctx) - assert.Equal(t, 0, len(acc)) -} - -// test the mechanism which keeps track of a validator set change -func TestGetAccUpdateValidators(t *testing.T) { - ctx, _, keeper := createTestInput(t, false, 0) - params := defaultParams() - params.MaxValidators = 4 - keeper.setParams(ctx, params) - - // TODO eliminate use of candidatesIn here - // tests could be clearer if they just - // created the candidate at time of use - // and were labelled by power in the comments - // outlining in each test - amts := []int64{10, 11, 12, 13, 1} - var candidatesIn [5]Candidate - for i, amt := range amts { - candidatesIn[i] = Candidate{ - Address: addrs[i], - PubKey: pks[i], - Assets: sdk.NewRat(amt), - Liabilities: sdk.NewRat(amt), - } - } - - // test from nothing to something - // candidate set: {} -> {c1, c3} - // validator set: {} -> {c1, c3} - // accUpdate set: {} -> {c1, c3} - assert.Equal(t, 0, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 0, len(keeper.GetValidators(ctx))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) - - keeper.setCandidate(ctx, candidatesIn[1]) - keeper.setCandidate(ctx, candidatesIn[3]) - - vals := keeper.GetValidators(ctx) // to init recent validator set - require.Equal(t, 2, len(vals)) - acc := keeper.getAccUpdateValidators(ctx) - require.Equal(t, 2, len(acc)) - candidates := keeper.GetCandidates(ctx, 5) - require.Equal(t, 2, len(candidates)) - assert.Equal(t, candidates[0].validator().abciValidator(keeper.cdc), acc[0]) - assert.Equal(t, candidates[1].validator().abciValidator(keeper.cdc), acc[1]) - assert.True(t, validatorsEqual(candidates[0].validator(), vals[1])) - assert.True(t, validatorsEqual(candidates[1].validator(), vals[0])) - - // test identical, - // candidate set: {c1, c3} -> {c1, c3} - // accUpdate set: {} -> {} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) - - keeper.setCandidate(ctx, candidates[0]) - keeper.setCandidate(ctx, candidates[1]) - - require.Equal(t, 2, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) - - // test single value change - // candidate set: {c1, c3} -> {c1', c3} - // accUpdate set: {} -> {c1'} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) - - candidates[0].Assets = sdk.NewRat(600) - keeper.setCandidate(ctx, candidates[0]) - - candidates = keeper.GetCandidates(ctx, 5) - require.Equal(t, 2, len(candidates)) - assert.True(t, candidates[0].Assets.Equal(sdk.NewRat(600))) - acc = keeper.getAccUpdateValidators(ctx) - require.Equal(t, 1, len(acc)) - assert.Equal(t, candidates[0].validator().abciValidator(keeper.cdc), acc[0]) - - // test multiple value change - // candidate set: {c1, c3} -> {c1', c3'} - // accUpdate set: {c1, c3} -> {c1', c3'} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) - - candidates[0].Assets = sdk.NewRat(200) - candidates[1].Assets = sdk.NewRat(100) - keeper.setCandidate(ctx, candidates[0]) - keeper.setCandidate(ctx, candidates[1]) - - acc = keeper.getAccUpdateValidators(ctx) - require.Equal(t, 2, len(acc)) - candidates = keeper.GetCandidates(ctx, 5) - require.Equal(t, 2, len(candidates)) - require.Equal(t, candidates[0].validator().abciValidator(keeper.cdc), acc[0]) - require.Equal(t, candidates[1].validator().abciValidator(keeper.cdc), acc[1]) - - // test validtor added at the beginning - // candidate set: {c1, c3} -> {c0, c1, c3} - // accUpdate set: {} -> {c0} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) - - keeper.setCandidate(ctx, candidatesIn[0]) - acc = keeper.getAccUpdateValidators(ctx) - require.Equal(t, 1, len(acc)) - candidates = keeper.GetCandidates(ctx, 5) - require.Equal(t, 3, len(candidates)) - assert.Equal(t, candidates[0].validator().abciValidator(keeper.cdc), acc[0]) - - // test validator added at the middle - // candidate set: {c0, c1, c3} -> {c0, c1, c2, c3] - // accUpdate set: {} -> {c2} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 3, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) - - keeper.setCandidate(ctx, candidatesIn[2]) - acc = keeper.getAccUpdateValidators(ctx) - require.Equal(t, 1, len(acc)) - candidates = keeper.GetCandidates(ctx, 5) - require.Equal(t, 4, len(candidates)) - assert.Equal(t, candidates[2].validator().abciValidator(keeper.cdc), acc[0]) - - // test candidate added at the end but not inserted in the valset - // candidate set: {c0, c1, c2, c3} -> {c0, c1, c2, c3, c4} - // validator set: {c0, c1, c2, c3} -> {c0, c1, c2, c3} - // accUpdate set: {} -> {} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 4, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 4, len(keeper.GetValidators(ctx))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) - - keeper.setCandidate(ctx, candidatesIn[4]) - - assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 4, len(keeper.GetValidators(ctx))) - require.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) // max validator number is 4 - - // test candidate change its power but still not in the valset - // candidate set: {c0, c1, c2, c3, c4} -> {c0, c1, c2, c3, c4} - // validator set: {c0, c1, c2, c3} -> {c0, c1, c2, c3} - // accUpdate set: {} -> {} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 4, len(keeper.GetValidators(ctx))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) - - candidatesIn[4].Assets = sdk.NewRat(1) - keeper.setCandidate(ctx, candidatesIn[4]) - - assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 4, len(keeper.GetValidators(ctx))) - require.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) // max validator number is 4 - - // test candidate change its power and become a validator (pushing out an existing) - // candidate set: {c0, c1, c2, c3, c4} -> {c0, c1, c2, c3, c4} - // validator set: {c0, c1, c2, c3} -> {c1, c2, c3, c4} - // accUpdate set: {} -> {c0, c4} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 4, len(keeper.GetValidators(ctx))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) - - candidatesIn[4].Assets = sdk.NewRat(1000) - keeper.setCandidate(ctx, candidatesIn[4]) - - candidates = keeper.GetCandidates(ctx, 5) - require.Equal(t, 5, len(candidates)) - vals = keeper.GetValidators(ctx) - require.Equal(t, 4, len(vals)) - assert.Equal(t, candidatesIn[1].Address, vals[1].Address) - assert.Equal(t, candidatesIn[2].Address, vals[3].Address) - assert.Equal(t, candidatesIn[3].Address, vals[2].Address) - assert.Equal(t, candidatesIn[4].Address, vals[0].Address) - - acc = keeper.getAccUpdateValidators(ctx) - require.Equal(t, 2, len(acc), "%v", acc) - - assert.Equal(t, candidatesIn[0].PubKey.Bytes(), acc[0].PubKey) - assert.Equal(t, int64(0), acc[0].Power) - assert.Equal(t, vals[0].abciValidator(keeper.cdc), acc[1]) - - // test from something to nothing - // candidate set: {c0, c1, c2, c3, c4} -> {} - // validator set: {c1, c2, c3, c4} -> {} - // accUpdate set: {} -> {c1, c2, c3, c4} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 4, len(keeper.GetValidators(ctx))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) - - keeper.removeCandidate(ctx, candidatesIn[0].Address) - keeper.removeCandidate(ctx, candidatesIn[1].Address) - keeper.removeCandidate(ctx, candidatesIn[2].Address) - keeper.removeCandidate(ctx, candidatesIn[3].Address) - keeper.removeCandidate(ctx, candidatesIn[4].Address) - - vals = keeper.GetValidators(ctx) - assert.Equal(t, 0, len(vals), "%v", vals) - candidates = keeper.GetCandidates(ctx, 5) - require.Equal(t, 0, len(candidates)) - acc = keeper.getAccUpdateValidators(ctx) - require.Equal(t, 4, len(acc)) - assert.Equal(t, candidatesIn[1].PubKey.Bytes(), acc[0].PubKey) - assert.Equal(t, candidatesIn[2].PubKey.Bytes(), acc[1].PubKey) - assert.Equal(t, candidatesIn[3].PubKey.Bytes(), acc[2].PubKey) - assert.Equal(t, candidatesIn[4].PubKey.Bytes(), acc[3].PubKey) - assert.Equal(t, int64(0), acc[0].Power) - assert.Equal(t, int64(0), acc[1].Power) - assert.Equal(t, int64(0), acc[2].Power) - assert.Equal(t, int64(0), acc[3].Power) -} - -// test if is a validator from the last update -func TestIsRecentValidator(t *testing.T) { - ctx, _, keeper := createTestInput(t, false, 0) - - amts := []int64{9, 8, 7, 10, 6} - var candidatesIn [5]Candidate - for i, amt := range amts { - candidatesIn[i] = Candidate{ - Address: addrVals[i], - PubKey: pks[i], - Assets: sdk.NewRat(amt), - Liabilities: sdk.NewRat(amt), - } - } - - // test that an empty validator set doesn't have any validators - validators := keeper.GetValidators(ctx) - assert.Equal(t, 0, len(validators)) - - // get the validators for the first time - keeper.setCandidate(ctx, candidatesIn[0]) - keeper.setCandidate(ctx, candidatesIn[1]) - validators = keeper.GetValidators(ctx) - require.Equal(t, 2, len(validators)) - assert.True(t, validatorsEqual(candidatesIn[0].validator(), validators[0])) - c1ValWithCounter := candidatesIn[1].validator() - c1ValWithCounter.Counter = int16(1) - assert.True(t, validatorsEqual(c1ValWithCounter, validators[1])) - - // test a basic retrieve of something that should be a recent validator - assert.True(t, keeper.IsRecentValidator(ctx, candidatesIn[0].Address)) - assert.True(t, keeper.IsRecentValidator(ctx, candidatesIn[1].Address)) - - // test a basic retrieve of something that should not be a recent validator - assert.False(t, keeper.IsRecentValidator(ctx, candidatesIn[2].Address)) - - // remove that validator, but don't retrieve the recent validator group - keeper.removeCandidate(ctx, candidatesIn[0].Address) - - // test that removed validator is not considered a recent validator - assert.False(t, keeper.IsRecentValidator(ctx, candidatesIn[0].Address)) -} - func TestParams(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) - expParams := defaultParams() + expParams := DefaultParams() //check that the empty keeper loads the default resParams := keeper.GetParams(ctx) - assert.Equal(t, expParams, resParams) + assert.True(t, expParams.equal(resParams)) //modify a params, save, and retrieve expParams.MaxValidators = 777 keeper.setParams(ctx, expParams) resParams = keeper.GetParams(ctx) - assert.Equal(t, expParams, resParams) + assert.True(t, expParams.equal(resParams)) } func TestPool(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) - expPool := initialPool() + expPool := InitialPool() //check that the empty keeper loads the default resPool := keeper.GetPool(ctx) - assert.Equal(t, expPool, resPool) + assert.True(t, expPool.equal(resPool)) //modify a params, save, and retrieve - expPool.TotalSupply = 777 + expPool.BondedTokens = 777 keeper.setPool(ctx, expPool) resPool = keeper.GetPool(ctx) - assert.Equal(t, expPool, resPool) + assert.True(t, expPool.equal(resPool)) } diff --git a/x/stake/msg.go b/x/stake/msg.go index 4e322e640..1cbf31c82 100644 --- a/x/stake/msg.go +++ b/x/stake/msg.go @@ -31,24 +31,26 @@ func init() { // MsgDeclareCandidacy - struct for unbonding transactions type MsgDeclareCandidacy struct { Description - CandidateAddr sdk.Address `json:"address"` + ValidatorAddr sdk.Address `json:"address"` PubKey crypto.PubKey `json:"pubkey"` Bond sdk.Coin `json:"bond"` } -func NewMsgDeclareCandidacy(candidateAddr sdk.Address, pubkey crypto.PubKey, +func NewMsgDeclareCandidacy(validatorAddr sdk.Address, pubkey crypto.PubKey, bond sdk.Coin, description Description) MsgDeclareCandidacy { return MsgDeclareCandidacy{ Description: description, - CandidateAddr: candidateAddr, + ValidatorAddr: validatorAddr, PubKey: pubkey, Bond: bond, } } //nolint -func (msg MsgDeclareCandidacy) Type() string { return MsgType } //TODO update "stake/declarecandidacy" -func (msg MsgDeclareCandidacy) GetSigners() []sdk.Address { return []sdk.Address{msg.CandidateAddr} } +func (msg MsgDeclareCandidacy) Type() string { return MsgType } //TODO update "stake/declarecandidacy" +func (msg MsgDeclareCandidacy) GetSigners() []sdk.Address { + return []sdk.Address{msg.ValidatorAddr} +} // get the bytes for the message signer to sign on func (msg MsgDeclareCandidacy) GetSignBytes() []byte { @@ -57,8 +59,8 @@ func (msg MsgDeclareCandidacy) GetSignBytes() []byte { // quick validity check func (msg MsgDeclareCandidacy) ValidateBasic() sdk.Error { - if msg.CandidateAddr == nil { - return ErrCandidateEmpty(DefaultCodespace) + if msg.ValidatorAddr == nil { + return ErrValidatorEmpty(DefaultCodespace) } if msg.Bond.Denom != StakingToken { return ErrBadBondingDenom(DefaultCodespace) @@ -75,22 +77,24 @@ func (msg MsgDeclareCandidacy) ValidateBasic() sdk.Error { //______________________________________________________________________ -// MsgEditCandidacy - struct for editing a candidate +// MsgEditCandidacy - struct for editing a validator type MsgEditCandidacy struct { Description - CandidateAddr sdk.Address `json:"address"` + ValidatorAddr sdk.Address `json:"address"` } -func NewMsgEditCandidacy(candidateAddr sdk.Address, description Description) MsgEditCandidacy { +func NewMsgEditCandidacy(validatorAddr sdk.Address, description Description) MsgEditCandidacy { return MsgEditCandidacy{ Description: description, - CandidateAddr: candidateAddr, + ValidatorAddr: validatorAddr, } } //nolint -func (msg MsgEditCandidacy) Type() string { return MsgType } //TODO update "stake/msgeditcandidacy" -func (msg MsgEditCandidacy) GetSigners() []sdk.Address { return []sdk.Address{msg.CandidateAddr} } +func (msg MsgEditCandidacy) Type() string { return MsgType } //TODO update "stake/msgeditcandidacy" +func (msg MsgEditCandidacy) GetSigners() []sdk.Address { + return []sdk.Address{msg.ValidatorAddr} +} // get the bytes for the message signer to sign on func (msg MsgEditCandidacy) GetSignBytes() []byte { @@ -103,8 +107,8 @@ func (msg MsgEditCandidacy) GetSignBytes() []byte { // quick validity check func (msg MsgEditCandidacy) ValidateBasic() sdk.Error { - if msg.CandidateAddr == nil { - return ErrCandidateEmpty(DefaultCodespace) + if msg.ValidatorAddr == nil { + return ErrValidatorEmpty(DefaultCodespace) } empty := Description{} if msg.Description == empty { @@ -117,22 +121,24 @@ func (msg MsgEditCandidacy) ValidateBasic() sdk.Error { // MsgDelegate - struct for bonding transactions type MsgDelegate struct { - DelegatorAddr sdk.Address `json:"address"` - CandidateAddr sdk.Address `json:"address"` + DelegatorAddr sdk.Address `json:"delegator_addr"` + ValidatorAddr sdk.Address `json:"validator_addr"` Bond sdk.Coin `json:"bond"` } -func NewMsgDelegate(delegatorAddr, candidateAddr sdk.Address, bond sdk.Coin) MsgDelegate { +func NewMsgDelegate(delegatorAddr, validatorAddr sdk.Address, bond sdk.Coin) MsgDelegate { return MsgDelegate{ DelegatorAddr: delegatorAddr, - CandidateAddr: candidateAddr, + ValidatorAddr: validatorAddr, Bond: bond, } } //nolint -func (msg MsgDelegate) Type() string { return MsgType } //TODO update "stake/msgeditcandidacy" -func (msg MsgDelegate) GetSigners() []sdk.Address { return []sdk.Address{msg.DelegatorAddr} } +func (msg MsgDelegate) Type() string { return MsgType } //TODO update "stake/msgeditcandidacy" +func (msg MsgDelegate) GetSigners() []sdk.Address { + return []sdk.Address{msg.DelegatorAddr} +} // get the bytes for the message signer to sign on func (msg MsgDelegate) GetSignBytes() []byte { @@ -148,8 +154,8 @@ func (msg MsgDelegate) ValidateBasic() sdk.Error { if msg.DelegatorAddr == nil { return ErrBadDelegatorAddr(DefaultCodespace) } - if msg.CandidateAddr == nil { - return ErrBadCandidateAddr(DefaultCodespace) + if msg.ValidatorAddr == nil { + return ErrBadValidatorAddr(DefaultCodespace) } if msg.Bond.Denom != StakingToken { return ErrBadBondingDenom(DefaultCodespace) @@ -164,15 +170,15 @@ func (msg MsgDelegate) ValidateBasic() sdk.Error { // MsgUnbond - struct for unbonding transactions type MsgUnbond struct { - DelegatorAddr sdk.Address `json:"address"` - CandidateAddr sdk.Address `json:"address"` + DelegatorAddr sdk.Address `json:"delegator_addr"` + ValidatorAddr sdk.Address `json:"validator_addr"` Shares string `json:"shares"` } -func NewMsgUnbond(delegatorAddr, candidateAddr sdk.Address, shares string) MsgUnbond { +func NewMsgUnbond(delegatorAddr, validatorAddr sdk.Address, shares string) MsgUnbond { return MsgUnbond{ DelegatorAddr: delegatorAddr, - CandidateAddr: candidateAddr, + ValidatorAddr: validatorAddr, Shares: shares, } } @@ -195,8 +201,8 @@ func (msg MsgUnbond) ValidateBasic() sdk.Error { if msg.DelegatorAddr == nil { return ErrBadDelegatorAddr(DefaultCodespace) } - if msg.CandidateAddr == nil { - return ErrBadCandidateAddr(DefaultCodespace) + if msg.ValidatorAddr == nil { + return ErrBadValidatorAddr(DefaultCodespace) } if msg.Shares != "MAX" { rat, err := sdk.NewRatFromDecimal(msg.Shares) diff --git a/x/stake/msg_test.go b/x/stake/msg_test.go index 19e8335be..03a5fbf62 100644 --- a/x/stake/msg_test.go +++ b/x/stake/msg_test.go @@ -22,7 +22,7 @@ var ( func TestMsgDeclareCandidacy(t *testing.T) { tests := []struct { name, moniker, identity, website, details string - candidateAddr sdk.Address + validatorAddr sdk.Address pubkey crypto.PubKey bond sdk.Coin expectPass bool @@ -40,7 +40,7 @@ func TestMsgDeclareCandidacy(t *testing.T) { for _, tc := range tests { description := NewDescription(tc.moniker, tc.identity, tc.website, tc.details) - msg := NewMsgDeclareCandidacy(tc.candidateAddr, tc.pubkey, tc.bond, description) + msg := NewMsgDeclareCandidacy(tc.validatorAddr, tc.pubkey, tc.bond, description) if tc.expectPass { assert.Nil(t, msg.ValidateBasic(), "test: %v", tc.name) } else { @@ -53,7 +53,7 @@ func TestMsgDeclareCandidacy(t *testing.T) { func TestMsgEditCandidacy(t *testing.T) { tests := []struct { name, moniker, identity, website, details string - candidateAddr sdk.Address + validatorAddr sdk.Address expectPass bool }{ {"basic good", "a", "b", "c", "d", addrs[0], true}, @@ -64,7 +64,7 @@ func TestMsgEditCandidacy(t *testing.T) { for _, tc := range tests { description := NewDescription(tc.moniker, tc.identity, tc.website, tc.details) - msg := NewMsgEditCandidacy(tc.candidateAddr, description) + msg := NewMsgEditCandidacy(tc.validatorAddr, description) if tc.expectPass { assert.Nil(t, msg.ValidateBasic(), "test: %v", tc.name) } else { @@ -78,21 +78,21 @@ func TestMsgDelegate(t *testing.T) { tests := []struct { name string delegatorAddr sdk.Address - candidateAddr sdk.Address + validatorAddr sdk.Address bond sdk.Coin expectPass bool }{ {"basic good", addrs[0], addrs[1], coinPos, true}, {"self bond", addrs[0], addrs[0], coinPos, true}, {"empty delegator", emptyAddr, addrs[0], coinPos, false}, - {"empty candidate", addrs[0], emptyAddr, coinPos, false}, + {"empty validator", addrs[0], emptyAddr, coinPos, false}, {"empty bond", addrs[0], addrs[1], coinZero, false}, {"negative bond", addrs[0], addrs[1], coinNeg, false}, {"wrong staking token", addrs[0], addrs[1], coinPosNotAtoms, false}, } for _, tc := range tests { - msg := NewMsgDelegate(tc.delegatorAddr, tc.candidateAddr, tc.bond) + msg := NewMsgDelegate(tc.delegatorAddr, tc.validatorAddr, tc.bond) if tc.expectPass { assert.Nil(t, msg.ValidateBasic(), "test: %v", tc.name) } else { @@ -106,7 +106,7 @@ func TestMsgUnbond(t *testing.T) { tests := []struct { name string delegatorAddr sdk.Address - candidateAddr sdk.Address + validatorAddr sdk.Address shares string expectPass bool }{ @@ -116,11 +116,11 @@ func TestMsgUnbond(t *testing.T) { {"zero unbond", addrs[0], addrs[1], "0.0", false}, {"invalid decimal", addrs[0], addrs[0], "sunny", false}, {"empty delegator", emptyAddr, addrs[0], "0.1", false}, - {"empty candidate", addrs[0], emptyAddr, "0.1", false}, + {"empty validator", addrs[0], emptyAddr, "0.1", false}, } for _, tc := range tests { - msg := NewMsgUnbond(tc.delegatorAddr, tc.candidateAddr, tc.shares) + msg := NewMsgUnbond(tc.delegatorAddr, tc.validatorAddr, tc.shares) if tc.expectPass { assert.Nil(t, msg.ValidateBasic(), "test: %v", tc.name) } else { diff --git a/x/stake/params.go b/x/stake/params.go new file mode 100644 index 000000000..ace39935c --- /dev/null +++ b/x/stake/params.go @@ -0,0 +1,36 @@ +package stake + +import ( + "bytes" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// Params defines the high level settings for staking +type Params struct { + InflationRateChange sdk.Rat `json:"inflation_rate_change"` // maximum annual change in inflation rate + InflationMax sdk.Rat `json:"inflation_max"` // maximum inflation rate + InflationMin sdk.Rat `json:"inflation_min"` // minimum inflation rate + GoalBonded sdk.Rat `json:"goal_bonded"` // Goal of percent bonded atoms + + MaxValidators uint16 `json:"max_validators"` // maximum number of validators + BondDenom string `json:"bond_denom"` // bondable coin denomination +} + +func (p Params) equal(p2 Params) bool { + bz1 := cdcEmpty.MustMarshalBinary(&p) + bz2 := cdcEmpty.MustMarshalBinary(&p2) + return bytes.Equal(bz1, bz2) +} + +// default params +func DefaultParams() Params { + return Params{ + InflationRateChange: sdk.NewRat(13, 100), + InflationMax: sdk.NewRat(20, 100), + InflationMin: sdk.NewRat(7, 100), + GoalBonded: sdk.NewRat(67, 100), + MaxValidators: 100, + BondDenom: "steak", + } +} diff --git a/x/stake/pool.go b/x/stake/pool.go index f0c7bfae5..0b320432e 100644 --- a/x/stake/pool.go +++ b/x/stake/pool.go @@ -1,13 +1,65 @@ package stake import ( + "bytes" + sdk "github.com/cosmos/cosmos-sdk/types" ) +// Pool - dynamic parameters of the current state +type Pool struct { + LooseUnbondedTokens int64 `json:"loose_unbonded_tokens"` // tokens not associated with any validator + UnbondedTokens int64 `json:"unbonded_tokens"` // reserve of unbonded tokens held with validators + UnbondingTokens int64 `json:"unbonding_tokens"` // tokens moving from bonded to unbonded pool + BondedTokens int64 `json:"bonded_tokens"` // reserve of bonded tokens + UnbondedShares sdk.Rat `json:"unbonded_shares"` // sum of all shares distributed for the Unbonded Pool + UnbondingShares sdk.Rat `json:"unbonding_shares"` // shares moving from Bonded to Unbonded Pool + BondedShares sdk.Rat `json:"bonded_shares"` // sum of all shares distributed for the Bonded Pool + InflationLastTime int64 `json:"inflation_last_time"` // block which the last inflation was processed // TODO make time + Inflation sdk.Rat `json:"inflation"` // current annual inflation rate + + DateLastCommissionReset int64 `json:"date_last_commission_reset"` // unix timestamp for last commission accounting reset (daily) + + // Fee Related + PrevBondedShares sdk.Rat `json:"prev_bonded_shares"` // last recorded bonded shares - for fee calcualtions +} + +func (p Pool) equal(p2 Pool) bool { + bz1 := cdcEmpty.MustMarshalBinary(&p) + bz2 := cdcEmpty.MustMarshalBinary(&p2) + return bytes.Equal(bz1, bz2) +} + +// initial pool for testing +func InitialPool() Pool { + return Pool{ + LooseUnbondedTokens: 0, + BondedTokens: 0, + UnbondingTokens: 0, + UnbondedTokens: 0, + BondedShares: sdk.ZeroRat(), + UnbondingShares: sdk.ZeroRat(), + UnbondedShares: sdk.ZeroRat(), + InflationLastTime: 0, + Inflation: sdk.NewRat(7, 100), + DateLastCommissionReset: 0, + PrevBondedShares: sdk.ZeroRat(), + } +} + +//____________________________________________________________________ + +// Sum total of all staking tokens in the pool +func (p Pool) TokenSupply() int64 { + return p.LooseUnbondedTokens + p.UnbondedTokens + p.UnbondingTokens + p.BondedTokens +} + +//____________________________________________________________________ + // get the bond ratio of the global state func (p Pool) bondedRatio() sdk.Rat { - if p.TotalSupply > 0 { - return sdk.NewRat(p.BondedPool, p.TotalSupply) + if p.TokenSupply() > 0 { + return sdk.NewRat(p.BondedTokens, p.TokenSupply()) } return sdk.ZeroRat() } @@ -17,102 +69,65 @@ func (p Pool) bondedShareExRate() sdk.Rat { if p.BondedShares.IsZero() { return sdk.OneRat() } - return sdk.NewRat(p.BondedPool).Quo(p.BondedShares) + return sdk.NewRat(p.BondedTokens).Quo(p.BondedShares) } -// get the exchange rate of unbonded tokens held in candidates per issued share +// get the exchange rate of unbonding tokens held in validators per issued share +func (p Pool) unbondingShareExRate() sdk.Rat { + if p.UnbondingShares.IsZero() { + return sdk.OneRat() + } + return sdk.NewRat(p.UnbondingTokens).Quo(p.UnbondingShares) +} + +// get the exchange rate of unbonded tokens held in validators per issued share func (p Pool) unbondedShareExRate() sdk.Rat { if p.UnbondedShares.IsZero() { return sdk.OneRat() } - return sdk.NewRat(p.UnbondedPool).Quo(p.UnbondedShares) -} - -// move a candidates asset pool from bonded to unbonded pool -func (p Pool) bondedToUnbondedPool(candidate Candidate) (Pool, Candidate) { - - // replace bonded shares with unbonded shares - p, tokens := p.removeSharesBonded(candidate.Assets) - p, candidate.Assets = p.addTokensUnbonded(tokens) - candidate.Status = Unbonded - return p, candidate -} - -// move a candidates asset pool from unbonded to bonded pool -func (p Pool) unbondedToBondedPool(candidate Candidate) (Pool, Candidate) { - - // replace unbonded shares with bonded shares - p, tokens := p.removeSharesUnbonded(candidate.Assets) - p, candidate.Assets = p.addTokensBonded(tokens) - candidate.Status = Bonded - return p, candidate + return sdk.NewRat(p.UnbondedTokens).Quo(p.UnbondedShares) } //_______________________________________________________________________ -func (p Pool) addTokensBonded(amount int64) (p2 Pool, issuedShares sdk.Rat) { - issuedShares = sdk.NewRat(amount).Quo(p.bondedShareExRate()) // tokens * (shares/tokens) - p.BondedPool += amount - p.BondedShares = p.BondedShares.Add(issuedShares) - return p, issuedShares -} - -func (p Pool) removeSharesBonded(shares sdk.Rat) (p2 Pool, removedTokens int64) { - removedTokens = p.bondedShareExRate().Mul(shares).Evaluate() // (tokens/shares) * shares - p.BondedShares = p.BondedShares.Sub(shares) - p.BondedPool = p.BondedPool - removedTokens - return p, removedTokens -} - -func (p Pool) addTokensUnbonded(amount int64) (p2 Pool, issuedShares sdk.Rat) { - issuedShares = sdk.NewRat(amount).Quo(p.unbondedShareExRate()) // tokens * (shares/tokens) - p.UnbondedShares = p.UnbondedShares.Add(issuedShares) - p.UnbondedPool += amount - return p, issuedShares +func (p Pool) addTokensUnbonded(amount int64) (p2 Pool, issuedShares PoolShares) { + issuedSharesAmount := sdk.NewRat(amount).Quo(p.unbondedShareExRate()) // tokens * (shares/tokens) + p.UnbondedShares = p.UnbondedShares.Add(issuedSharesAmount) + p.UnbondedTokens += amount + return p, NewUnbondedShares(issuedSharesAmount) } func (p Pool) removeSharesUnbonded(shares sdk.Rat) (p2 Pool, removedTokens int64) { removedTokens = p.unbondedShareExRate().Mul(shares).Evaluate() // (tokens/shares) * shares p.UnbondedShares = p.UnbondedShares.Sub(shares) - p.UnbondedPool -= removedTokens + p.UnbondedTokens -= removedTokens return p, removedTokens } -//_______________________________________________________________________ - -// add tokens to a candidate -func (p Pool) candidateAddTokens(candidate Candidate, - amount int64) (p2 Pool, candidate2 Candidate, issuedDelegatorShares sdk.Rat) { - - exRate := candidate.delegatorShareExRate() - - var receivedGlobalShares sdk.Rat - if candidate.Status == Bonded { - p, receivedGlobalShares = p.addTokensBonded(amount) - } else { - p, receivedGlobalShares = p.addTokensUnbonded(amount) - } - candidate.Assets = candidate.Assets.Add(receivedGlobalShares) - - issuedDelegatorShares = exRate.Mul(receivedGlobalShares) - candidate.Liabilities = candidate.Liabilities.Add(issuedDelegatorShares) - - return p, candidate, issuedDelegatorShares +func (p Pool) addTokensUnbonding(amount int64) (p2 Pool, issuedShares PoolShares) { + issuedSharesAmount := sdk.NewRat(amount).Quo(p.unbondingShareExRate()) // tokens * (shares/tokens) + p.UnbondingShares = p.UnbondingShares.Add(issuedSharesAmount) + p.UnbondingTokens += amount + return p, NewUnbondingShares(issuedSharesAmount) } -// remove shares from a candidate -func (p Pool) candidateRemoveShares(candidate Candidate, - shares sdk.Rat) (p2 Pool, candidate2 Candidate, createdCoins int64) { - - //exRate := candidate.delegatorShareExRate() //XXX make sure not used - - globalPoolSharesToRemove := candidate.delegatorShareExRate().Mul(shares) - if candidate.Status == Bonded { - p, createdCoins = p.removeSharesBonded(globalPoolSharesToRemove) - } else { - p, createdCoins = p.removeSharesUnbonded(globalPoolSharesToRemove) - } - candidate.Assets = candidate.Assets.Sub(globalPoolSharesToRemove) - candidate.Liabilities = candidate.Liabilities.Sub(shares) - return p, candidate, createdCoins +func (p Pool) removeSharesUnbonding(shares sdk.Rat) (p2 Pool, removedTokens int64) { + removedTokens = p.unbondingShareExRate().Mul(shares).Evaluate() // (tokens/shares) * shares + p.UnbondingShares = p.UnbondingShares.Sub(shares) + p.UnbondingTokens -= removedTokens + return p, removedTokens +} + +func (p Pool) addTokensBonded(amount int64) (p2 Pool, issuedShares PoolShares) { + issuedSharesAmount := sdk.NewRat(amount).Quo(p.bondedShareExRate()) // tokens * (shares/tokens) + p.BondedShares = p.BondedShares.Add(issuedSharesAmount) + p.BondedTokens += amount + return p, NewBondedShares(issuedSharesAmount) +} + +func (p Pool) removeSharesBonded(shares sdk.Rat) (p2 Pool, removedTokens int64) { + removedTokens = p.bondedShareExRate().Mul(shares).Evaluate() // (tokens/shares) * shares + p.BondedShares = p.BondedShares.Sub(shares) + p.BondedTokens -= removedTokens + return p, removedTokens } diff --git a/x/stake/pool_test.go b/x/stake/pool_test.go index 6d70a85af..99cfa5a12 100644 --- a/x/stake/pool_test.go +++ b/x/stake/pool_test.go @@ -1,8 +1,6 @@ package stake import ( - "fmt" - "math/rand" "testing" "github.com/stretchr/testify/assert" @@ -14,21 +12,22 @@ import ( func TestBondedRatio(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) pool := keeper.GetPool(ctx) - pool.TotalSupply = 3 - pool.BondedPool = 2 + pool.LooseUnbondedTokens = 1 + pool.BondedTokens = 2 // bonded pool / total supply require.Equal(t, pool.bondedRatio(), sdk.NewRat(2).Quo(sdk.NewRat(3))) - pool.TotalSupply = 0 // avoids divide-by-zero + pool.LooseUnbondedTokens = 0 + pool.BondedTokens = 0 require.Equal(t, pool.bondedRatio(), sdk.ZeroRat()) } func TestBondedShareExRate(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) pool := keeper.GetPool(ctx) - pool.BondedPool = 3 + pool.BondedTokens = 3 pool.BondedShares = sdk.NewRat(10) // bonded pool / bonded shares @@ -39,10 +38,24 @@ func TestBondedShareExRate(t *testing.T) { require.Equal(t, pool.bondedShareExRate(), sdk.OneRat()) } +func TestUnbondingShareExRate(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + pool := keeper.GetPool(ctx) + pool.UnbondingTokens = 3 + pool.UnbondingShares = sdk.NewRat(10) + + // unbonding pool / unbonding shares + require.Equal(t, pool.unbondingShareExRate(), sdk.NewRat(3).Quo(sdk.NewRat(10))) + pool.UnbondingShares = sdk.ZeroRat() + + // avoids divide-by-zero + require.Equal(t, pool.unbondingShareExRate(), sdk.OneRat()) +} + func TestUnbondedShareExRate(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) pool := keeper.GetPool(ctx) - pool.UnbondedPool = 3 + pool.UnbondedTokens = 3 pool.UnbondedShares = sdk.NewRat(10) // unbonded pool / unbonded shares @@ -53,61 +66,6 @@ func TestUnbondedShareExRate(t *testing.T) { require.Equal(t, pool.unbondedShareExRate(), sdk.OneRat()) } -func TestBondedToUnbondedPool(t *testing.T) { - ctx, _, keeper := createTestInput(t, false, 0) - - poolA := keeper.GetPool(ctx) - assert.Equal(t, poolA.bondedShareExRate(), sdk.OneRat()) - assert.Equal(t, poolA.unbondedShareExRate(), sdk.OneRat()) - candA := Candidate{ - Status: Bonded, - Address: addrs[0], - PubKey: pks[0], - Assets: sdk.OneRat(), - Liabilities: sdk.OneRat(), - } - poolB, candB := poolA.bondedToUnbondedPool(candA) - - // status unbonded - assert.Equal(t, candB.Status, Unbonded) - // same exchange rate, assets unchanged - assert.Equal(t, candB.Assets, candA.Assets) - // bonded pool decreased - assert.Equal(t, poolB.BondedPool, poolA.BondedPool-candA.Assets.Evaluate()) - // unbonded pool increased - assert.Equal(t, poolB.UnbondedPool, poolA.UnbondedPool+candA.Assets.Evaluate()) - // conservation of tokens - assert.Equal(t, poolB.UnbondedPool+poolB.BondedPool, poolA.BondedPool+poolA.UnbondedPool) -} - -func TestUnbonbedtoBondedPool(t *testing.T) { - ctx, _, keeper := createTestInput(t, false, 0) - - poolA := keeper.GetPool(ctx) - assert.Equal(t, poolA.bondedShareExRate(), sdk.OneRat()) - assert.Equal(t, poolA.unbondedShareExRate(), sdk.OneRat()) - candA := Candidate{ - Status: Bonded, - Address: addrs[0], - PubKey: pks[0], - Assets: sdk.OneRat(), - Liabilities: sdk.OneRat(), - } - candA.Status = Unbonded - poolB, candB := poolA.unbondedToBondedPool(candA) - - // status bonded - assert.Equal(t, candB.Status, Bonded) - // same exchange rate, assets unchanged - assert.Equal(t, candB.Assets, candA.Assets) - // bonded pool increased - assert.Equal(t, poolB.BondedPool, poolA.BondedPool+candA.Assets.Evaluate()) - // unbonded pool decreased - assert.Equal(t, poolB.UnbondedPool, poolA.UnbondedPool-candA.Assets.Evaluate()) - // conservation of tokens - assert.Equal(t, poolB.UnbondedPool+poolB.BondedPool, poolA.BondedPool+poolA.UnbondedPool) -} - func TestAddTokensBonded(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) @@ -117,11 +75,11 @@ func TestAddTokensBonded(t *testing.T) { assert.Equal(t, poolB.bondedShareExRate(), sdk.OneRat()) // correct changes to bonded shares and bonded pool - assert.Equal(t, poolB.BondedShares, poolA.BondedShares.Add(sharesB)) - assert.Equal(t, poolB.BondedPool, poolA.BondedPool+10) + assert.Equal(t, poolB.BondedShares, poolA.BondedShares.Add(sharesB.Amount)) + assert.Equal(t, poolB.BondedTokens, poolA.BondedTokens+10) // same number of bonded shares / tokens when exchange rate is one - assert.True(t, poolB.BondedShares.Equal(sdk.NewRat(poolB.BondedPool))) + assert.True(t, poolB.BondedShares.Equal(sdk.NewRat(poolB.BondedTokens))) } func TestRemoveSharesBonded(t *testing.T) { @@ -134,10 +92,10 @@ func TestRemoveSharesBonded(t *testing.T) { // correct changes to bonded shares and bonded pool assert.Equal(t, poolB.BondedShares, poolA.BondedShares.Sub(sdk.NewRat(10))) - assert.Equal(t, poolB.BondedPool, poolA.BondedPool-tokensB) + assert.Equal(t, poolB.BondedTokens, poolA.BondedTokens-tokensB) // same number of bonded shares / tokens when exchange rate is one - assert.True(t, poolB.BondedShares.Equal(sdk.NewRat(poolB.BondedPool))) + assert.True(t, poolB.BondedShares.Equal(sdk.NewRat(poolB.BondedTokens))) } func TestAddTokensUnbonded(t *testing.T) { @@ -149,11 +107,11 @@ func TestAddTokensUnbonded(t *testing.T) { assert.Equal(t, poolB.unbondedShareExRate(), sdk.OneRat()) // correct changes to unbonded shares and unbonded pool - assert.Equal(t, poolB.UnbondedShares, poolA.UnbondedShares.Add(sharesB)) - assert.Equal(t, poolB.UnbondedPool, poolA.UnbondedPool+10) + assert.Equal(t, poolB.UnbondedShares, poolA.UnbondedShares.Add(sharesB.Amount)) + assert.Equal(t, poolB.UnbondedTokens, poolA.UnbondedTokens+10) // same number of unbonded shares / tokens when exchange rate is one - assert.True(t, poolB.UnbondedShares.Equal(sdk.NewRat(poolB.UnbondedPool))) + assert.True(t, poolB.UnbondedShares.Equal(sdk.NewRat(poolB.UnbondedTokens))) } func TestRemoveSharesUnbonded(t *testing.T) { @@ -166,359 +124,8 @@ func TestRemoveSharesUnbonded(t *testing.T) { // correct changes to unbonded shares and bonded pool assert.Equal(t, poolB.UnbondedShares, poolA.UnbondedShares.Sub(sdk.NewRat(10))) - assert.Equal(t, poolB.UnbondedPool, poolA.UnbondedPool-tokensB) + assert.Equal(t, poolB.UnbondedTokens, poolA.UnbondedTokens-tokensB) // same number of unbonded shares / tokens when exchange rate is one - assert.True(t, poolB.UnbondedShares.Equal(sdk.NewRat(poolB.UnbondedPool))) -} - -func TestCandidateAddTokens(t *testing.T) { - ctx, _, keeper := createTestInput(t, false, 0) - - poolA := keeper.GetPool(ctx) - candA := Candidate{ - Status: Bonded, - Address: addrs[0], - PubKey: pks[0], - Assets: sdk.NewRat(9), - Liabilities: sdk.NewRat(9), - } - poolA.BondedPool = candA.Assets.Evaluate() - poolA.BondedShares = candA.Assets - assert.Equal(t, candA.delegatorShareExRate(), sdk.OneRat()) - assert.Equal(t, poolA.bondedShareExRate(), sdk.OneRat()) - assert.Equal(t, poolA.unbondedShareExRate(), sdk.OneRat()) - poolB, candB, sharesB := poolA.candidateAddTokens(candA, 10) - - // shares were issued - assert.Equal(t, sdk.NewRat(10).Mul(candA.delegatorShareExRate()), sharesB) - // pool shares were added - assert.Equal(t, candB.Assets, candA.Assets.Add(sdk.NewRat(10))) - // conservation of tokens - assert.Equal(t, poolB.BondedPool, 10+poolA.BondedPool) -} - -func TestCandidateRemoveShares(t *testing.T) { - ctx, _, keeper := createTestInput(t, false, 0) - - poolA := keeper.GetPool(ctx) - candA := Candidate{ - Status: Bonded, - Address: addrs[0], - PubKey: pks[0], - Assets: sdk.NewRat(9), - Liabilities: sdk.NewRat(9), - } - poolA.BondedPool = candA.Assets.Evaluate() - poolA.BondedShares = candA.Assets - assert.Equal(t, candA.delegatorShareExRate(), sdk.OneRat()) - assert.Equal(t, poolA.bondedShareExRate(), sdk.OneRat()) - assert.Equal(t, poolA.unbondedShareExRate(), sdk.OneRat()) - poolB, candB, coinsB := poolA.candidateRemoveShares(candA, sdk.NewRat(10)) - - // coins were created - assert.Equal(t, coinsB, int64(10)) - // pool shares were removed - assert.Equal(t, candB.Assets, candA.Assets.Sub(sdk.NewRat(10).Mul(candA.delegatorShareExRate()))) - // conservation of tokens - assert.Equal(t, poolB.UnbondedPool+poolB.BondedPool+coinsB, poolA.UnbondedPool+poolA.BondedPool) - - // specific case from random tests - assets := sdk.NewRat(5102) - liabilities := sdk.NewRat(115) - cand := Candidate{ - Status: Bonded, - Address: addrs[0], - PubKey: pks[0], - Assets: assets, - Liabilities: liabilities, - } - pool := Pool{ - TotalSupply: 0, - BondedShares: sdk.NewRat(248305), - UnbondedShares: sdk.NewRat(232147), - BondedPool: 248305, - UnbondedPool: 232147, - InflationLastTime: 0, - Inflation: sdk.NewRat(7, 100), - } - shares := sdk.NewRat(29) - msg := fmt.Sprintf("candidate %s (status: %d, assets: %v, liabilities: %v, delegatorShareExRate: %v)", - cand.Address, cand.Status, cand.Assets, cand.Liabilities, cand.delegatorShareExRate()) - msg = fmt.Sprintf("Removed %v shares from %s", shares, msg) - newPool, _, tokens := pool.candidateRemoveShares(cand, shares) - require.Equal(t, - tokens+newPool.UnbondedPool+newPool.BondedPool, - pool.BondedPool+pool.UnbondedPool, - "Tokens were not conserved: %s", msg) -} - -///////////////////////////////////// -// TODO Make all random tests less obfuscated! - -// generate a random candidate -func randomCandidate(r *rand.Rand) Candidate { - var status CandidateStatus - if r.Float64() < float64(0.5) { - status = Bonded - } else { - status = Unbonded - } - assets := sdk.NewRat(int64(r.Int31n(10000))) - liabilities := sdk.NewRat(int64(r.Int31n(10000))) - return Candidate{ - Status: status, - Address: addrs[0], - PubKey: pks[0], - Assets: assets, - Liabilities: liabilities, - } -} - -// generate a random staking state -func randomSetup(r *rand.Rand, numCandidates int) (Pool, Candidates) { - pool := Pool{ - TotalSupply: 0, - BondedShares: sdk.ZeroRat(), - UnbondedShares: sdk.ZeroRat(), - BondedPool: 0, - UnbondedPool: 0, - InflationLastTime: 0, - Inflation: sdk.NewRat(7, 100), - } - - candidates := make([]Candidate, numCandidates) - for i := 0; i < numCandidates; i++ { - candidate := randomCandidate(r) - if candidate.Status == Bonded { - pool.BondedShares = pool.BondedShares.Add(candidate.Assets) - pool.BondedPool += candidate.Assets.Evaluate() - } else if candidate.Status == Unbonded { - pool.UnbondedShares = pool.UnbondedShares.Add(candidate.Assets) - pool.UnbondedPool += candidate.Assets.Evaluate() - } - candidates[i] = candidate - } - return pool, candidates -} - -// any operation that transforms staking state -// takes in RNG instance, pool, candidate -// returns updated pool, updated candidate, delta tokens, descriptive message -type Operation func(r *rand.Rand, p Pool, c Candidate) (Pool, Candidate, int64, string) - -// operation: bond or unbond a candidate depending on current status -func OpBondOrUnbond(r *rand.Rand, p Pool, cand Candidate) (Pool, Candidate, int64, string) { - var msg string - if cand.Status == Bonded { - msg = fmt.Sprintf("Unbonded previously bonded candidate %s (assets: %v, liabilities: %v, delegatorShareExRate: %v)", - cand.Address, cand.Assets, cand.Liabilities, cand.delegatorShareExRate()) - p, cand = p.bondedToUnbondedPool(cand) - - } else if cand.Status == Unbonded { - msg = fmt.Sprintf("Bonded previously unbonded candidate %s (assets: %v, liabilities: %v, delegatorShareExRate: %v)", - cand.Address, cand.Assets, cand.Liabilities, cand.delegatorShareExRate()) - p, cand = p.unbondedToBondedPool(cand) - } - return p, cand, 0, msg -} - -// operation: add a random number of tokens to a candidate -func OpAddTokens(r *rand.Rand, p Pool, cand Candidate) (Pool, Candidate, int64, string) { - tokens := int64(r.Int31n(1000)) - msg := fmt.Sprintf("candidate %s (status: %d, assets: %v, liabilities: %v, delegatorShareExRate: %v)", - cand.Address, cand.Status, cand.Assets, cand.Liabilities, cand.delegatorShareExRate()) - p, cand, _ = p.candidateAddTokens(cand, tokens) - msg = fmt.Sprintf("Added %d tokens to %s", tokens, msg) - return p, cand, -1 * tokens, msg // tokens are removed so for accounting must be negative -} - -// operation: remove a random number of shares from a candidate -func OpRemoveShares(r *rand.Rand, p Pool, cand Candidate) (Pool, Candidate, int64, string) { - var shares sdk.Rat - for { - shares = sdk.NewRat(int64(r.Int31n(1000))) - if shares.LT(cand.Liabilities) { - break - } - } - - msg := fmt.Sprintf("Removed %v shares from candidate %s (status: %d, assets: %v, liabilities: %v, delegatorShareExRate: %v)", - shares, cand.Address, cand.Status, cand.Assets, cand.Liabilities, cand.delegatorShareExRate()) - - p, cand, tokens := p.candidateRemoveShares(cand, shares) - return p, cand, tokens, msg -} - -// pick a random staking operation -func randomOperation(r *rand.Rand) Operation { - operations := []Operation{ - OpBondOrUnbond, - OpAddTokens, - OpRemoveShares, - } - r.Shuffle(len(operations), func(i, j int) { - operations[i], operations[j] = operations[j], operations[i] - }) - return operations[0] -} - -// ensure invariants that should always be true are true -func assertInvariants(t *testing.T, msg string, - pOrig Pool, cOrig Candidates, pMod Pool, cMods Candidates, tokens int64) { - - // total tokens conserved - require.Equal(t, - pOrig.UnbondedPool+pOrig.BondedPool, - pMod.UnbondedPool+pMod.BondedPool+tokens, - "Tokens not conserved - msg: %v\n, pOrig.BondedShares: %v, pOrig.UnbondedShares: %v, pMod.BondedShares: %v, pMod.UnbondedShares: %v, pOrig.UnbondedPool: %v, pOrig.BondedPool: %v, pMod.UnbondedPool: %v, pMod.BondedPool: %v, tokens: %v\n", - msg, - pOrig.BondedShares, pOrig.UnbondedShares, - pMod.BondedShares, pMod.UnbondedShares, - pOrig.UnbondedPool, pOrig.BondedPool, - pMod.UnbondedPool, pMod.BondedPool, tokens) - - // nonnegative bonded shares - require.False(t, pMod.BondedShares.LT(sdk.ZeroRat()), - "Negative bonded shares - msg: %v\npOrig: %#v\npMod: %#v\ntokens: %v\n", - msg, pOrig, pMod, tokens) - - // nonnegative unbonded shares - require.False(t, pMod.UnbondedShares.LT(sdk.ZeroRat()), - "Negative unbonded shares - msg: %v\npOrig: %#v\npMod: %#v\ntokens: %v\n", - msg, pOrig, pMod, tokens) - - // nonnegative bonded ex rate - require.False(t, pMod.bondedShareExRate().LT(sdk.ZeroRat()), - "Applying operation \"%s\" resulted in negative bondedShareExRate: %d", - msg, pMod.bondedShareExRate().Evaluate()) - - // nonnegative unbonded ex rate - require.False(t, pMod.unbondedShareExRate().LT(sdk.ZeroRat()), - "Applying operation \"%s\" resulted in negative unbondedShareExRate: %d", - msg, pMod.unbondedShareExRate().Evaluate()) - - for _, cMod := range cMods { - - // nonnegative ex rate - require.False(t, cMod.delegatorShareExRate().LT(sdk.ZeroRat()), - "Applying operation \"%s\" resulted in negative candidate.delegatorShareExRate(): %v (candidate.Address: %s)", - msg, - cMod.delegatorShareExRate(), - cMod.Address, - ) - - // nonnegative assets - require.False(t, cMod.Assets.LT(sdk.ZeroRat()), - "Applying operation \"%s\" resulted in negative candidate.Assets: %v (candidate.Liabilities: %v, candidate.delegatorShareExRate: %v, candidate.Address: %s)", - msg, - cMod.Assets, - cMod.Liabilities, - cMod.delegatorShareExRate(), - cMod.Address, - ) - - // nonnegative liabilities - require.False(t, cMod.Liabilities.LT(sdk.ZeroRat()), - "Applying operation \"%s\" resulted in negative candidate.Liabilities: %v (candidate.Assets: %v, candidate.delegatorShareExRate: %v, candidate.Address: %s)", - msg, - cMod.Liabilities, - cMod.Assets, - cMod.delegatorShareExRate(), - cMod.Address, - ) - - } - -} - -func TestPossibleOverflow(t *testing.T) { - assets := sdk.NewRat(2159) - liabilities := sdk.NewRat(391432570689183511).Quo(sdk.NewRat(40113011844664)) - cand := Candidate{ - Status: Bonded, - Address: addrs[0], - PubKey: pks[0], - Assets: assets, - Liabilities: liabilities, - } - pool := Pool{ - TotalSupply: 0, - BondedShares: assets, - UnbondedShares: sdk.ZeroRat(), - BondedPool: assets.Evaluate(), - UnbondedPool: 0, - InflationLastTime: 0, - Inflation: sdk.NewRat(7, 100), - } - tokens := int64(71) - msg := fmt.Sprintf("candidate %s (status: %d, assets: %v, liabilities: %v, delegatorShareExRate: %v)", - cand.Address, cand.Status, cand.Assets, cand.Liabilities, cand.delegatorShareExRate()) - _, newCandidate, _ := pool.candidateAddTokens(cand, tokens) - - msg = fmt.Sprintf("Added %d tokens to %s", tokens, msg) - require.False(t, newCandidate.delegatorShareExRate().LT(sdk.ZeroRat()), - "Applying operation \"%s\" resulted in negative delegatorShareExRate(): %v", - msg, newCandidate.delegatorShareExRate()) -} - -// run random operations in a random order on a random single-candidate state, assert invariants hold -func TestSingleCandidateIntegrationInvariants(t *testing.T) { - r := rand.New(rand.NewSource(41)) - - for i := 0; i < 10; i++ { - poolOrig, candidatesOrig := randomSetup(r, 1) - require.Equal(t, 1, len(candidatesOrig)) - - // sanity check - assertInvariants(t, "no operation", - poolOrig, candidatesOrig, - poolOrig, candidatesOrig, 0) - - for j := 0; j < 5; j++ { - poolMod, candidateMod, tokens, msg := randomOperation(r)(r, poolOrig, candidatesOrig[0]) - - candidatesMod := make([]Candidate, len(candidatesOrig)) - copy(candidatesMod[:], candidatesOrig[:]) - require.Equal(t, 1, len(candidatesOrig), "j %v", j) - require.Equal(t, 1, len(candidatesMod), "j %v", j) - candidatesMod[0] = candidateMod - - assertInvariants(t, msg, - poolOrig, candidatesOrig, - poolMod, candidatesMod, tokens) - - poolOrig = poolMod - candidatesOrig = candidatesMod - } - } -} - -// run random operations in a random order on a random multi-candidate state, assert invariants hold -func TestMultiCandidateIntegrationInvariants(t *testing.T) { - r := rand.New(rand.NewSource(42)) - - for i := 0; i < 10; i++ { - poolOrig, candidatesOrig := randomSetup(r, 100) - - assertInvariants(t, "no operation", - poolOrig, candidatesOrig, - poolOrig, candidatesOrig, 0) - - for j := 0; j < 5; j++ { - index := int(r.Int31n(int32(len(candidatesOrig)))) - poolMod, candidateMod, tokens, msg := randomOperation(r)(r, poolOrig, candidatesOrig[index]) - candidatesMod := make([]Candidate, len(candidatesOrig)) - copy(candidatesMod[:], candidatesOrig[:]) - candidatesMod[index] = candidateMod - - assertInvariants(t, msg, - poolOrig, candidatesOrig, - poolMod, candidatesMod, tokens) - - poolOrig = poolMod - candidatesOrig = candidatesMod - - } - } + assert.True(t, poolB.UnbondedShares.Equal(sdk.NewRat(poolB.UnbondedTokens))) } diff --git a/x/stake/shares.go b/x/stake/shares.go new file mode 100644 index 000000000..d5fe93844 --- /dev/null +++ b/x/stake/shares.go @@ -0,0 +1,133 @@ +package stake + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// kind of shares +type PoolShareKind byte + +// pool shares held by a validator +type PoolShares struct { + Status sdk.BondStatus `json:"status"` + Amount sdk.Rat `json:"amount"` // total shares of type ShareKind +} + +// only the vitals - does not check bond height of IntraTxCounter +func (s PoolShares) Equal(s2 PoolShares) bool { + return s.Status == s2.Status && + s.Amount.Equal(s2.Amount) +} + +func NewUnbondedShares(amount sdk.Rat) PoolShares { + return PoolShares{ + Status: sdk.Unbonded, + Amount: amount, + } +} + +func NewUnbondingShares(amount sdk.Rat) PoolShares { + return PoolShares{ + Status: sdk.Unbonding, + Amount: amount, + } +} + +func NewBondedShares(amount sdk.Rat) PoolShares { + return PoolShares{ + Status: sdk.Bonded, + Amount: amount, + } +} + +//_________________________________________________________________________________________________________ + +// amount of unbonded shares +func (s PoolShares) Unbonded() sdk.Rat { + if s.Status == sdk.Unbonded { + return s.Amount + } + return sdk.ZeroRat() +} + +// amount of unbonding shares +func (s PoolShares) Unbonding() sdk.Rat { + if s.Status == sdk.Unbonding { + return s.Amount + } + return sdk.ZeroRat() +} + +// amount of bonded shares +func (s PoolShares) Bonded() sdk.Rat { + if s.Status == sdk.Bonded { + return s.Amount + } + return sdk.ZeroRat() +} + +//_________________________________________________________________________________________________________ + +// equivalent amount of shares if the shares were unbonded +func (s PoolShares) ToUnbonded(p Pool) PoolShares { + var amount sdk.Rat + switch s.Status { + case sdk.Bonded: + exRate := p.bondedShareExRate().Quo(p.unbondedShareExRate()) // (tok/bondedshr)/(tok/unbondedshr) = unbondedshr/bondedshr + amount = s.Amount.Mul(exRate) // bondedshr*unbondedshr/bondedshr = unbondedshr + case sdk.Unbonding: + exRate := p.unbondingShareExRate().Quo(p.unbondedShareExRate()) // (tok/unbondingshr)/(tok/unbondedshr) = unbondedshr/unbondingshr + amount = s.Amount.Mul(exRate) // unbondingshr*unbondedshr/unbondingshr = unbondedshr + case sdk.Unbonded: + amount = s.Amount + } + return NewUnbondedShares(amount) +} + +// equivalent amount of shares if the shares were unbonding +func (s PoolShares) ToUnbonding(p Pool) PoolShares { + var amount sdk.Rat + switch s.Status { + case sdk.Bonded: + exRate := p.bondedShareExRate().Quo(p.unbondingShareExRate()) // (tok/bondedshr)/(tok/unbondingshr) = unbondingshr/bondedshr + amount = s.Amount.Mul(exRate) // bondedshr*unbondingshr/bondedshr = unbondingshr + case sdk.Unbonding: + amount = s.Amount + case sdk.Unbonded: + exRate := p.unbondedShareExRate().Quo(p.unbondingShareExRate()) // (tok/unbondedshr)/(tok/unbondingshr) = unbondingshr/unbondedshr + amount = s.Amount.Mul(exRate) // unbondedshr*unbondingshr/unbondedshr = unbondingshr + } + return NewUnbondingShares(amount) +} + +// equivalent amount of shares if the shares were bonded +func (s PoolShares) ToBonded(p Pool) PoolShares { + var amount sdk.Rat + switch s.Status { + case sdk.Bonded: + amount = s.Amount + case sdk.Unbonding: + exRate := p.unbondingShareExRate().Quo(p.bondedShareExRate()) // (tok/ubshr)/(tok/bshr) = bshr/ubshr + amount = s.Amount.Mul(exRate) // ubshr*bshr/ubshr = bshr + case sdk.Unbonded: + exRate := p.unbondedShareExRate().Quo(p.bondedShareExRate()) // (tok/ubshr)/(tok/bshr) = bshr/ubshr + amount = s.Amount.Mul(exRate) // ubshr*bshr/ubshr = bshr + } + return NewUnbondedShares(amount) +} + +//_________________________________________________________________________________________________________ + +// get the equivalent amount of tokens contained by the shares +func (s PoolShares) Tokens(p Pool) sdk.Rat { + switch s.Status { + case sdk.Bonded: + return p.unbondedShareExRate().Mul(s.Amount) // (tokens/shares) * shares + case sdk.Unbonding: + return p.unbondedShareExRate().Mul(s.Amount) + case sdk.Unbonded: + return p.unbondedShareExRate().Mul(s.Amount) + default: + panic("unknown share kind") + } +} diff --git a/x/stake/store.md b/x/stake/store.md new file mode 100644 index 000000000..1c95ffe87 --- /dev/null +++ b/x/stake/store.md @@ -0,0 +1,45 @@ +# Stores + +This document provided a bit more insight as to the purpose of several related +prefixed areas of the staking store which are accessed in `x/stake/keeper.go`. + + +## Validators + - Prefix Key Space: ValidatorsKey + - Key/Sort: Validator Owner Address + - Value: Validator Object + - Contains: All Validator records independent of being bonded or not + - Used For: Retrieve validator from owner address, general validator retrieval + +## Validators By Power + - Prefix Key Space: ValidatorsByPowerKey + - Key/Sort: Validator Power (equivalent bonded shares) then Block + Height then Transaction Order + - Value: Validator Owner Address + - Contains: All Validator records independent of being bonded or not + - Used For: Determining who the top validators are whom should be bonded + +## Validators Cliff Power + - Prefix Key Space: ValidatorCliffKey + - Key/Sort: single-record + - Value: Validator Power Key (as above store) + - Contains: The cliff validator (ex. 100th validator) power + - Used For: Efficient updates to validator status + +## Validators Bonded + - Prefix Key Space: ValidatorsBondedKey + - Key/Sort: Validator PubKey Address (NOTE same as Tendermint) + - Value: Validator Owner Address + - Contains: Only currently bonded Validators + - Used For: Retrieving the list of all currently bonded validators when updating + for a new validator entering the validator set we may want to loop + through this set to determine who we've kicked out. + retrieving validator by tendermint index + +## Tendermint Updates + - Prefix Key Space: TendermintUpdatesKey + - Key/Sort: Validator Owner Address + - Value: Tendermint ABCI Validator + - Contains: Validators are queued to affect the consensus validation set in Tendermint + - Used For: Informing Tendermint of the validator set updates, is used only intra-block, as the + updates are applied then cleared on endblock diff --git a/x/stake/test_common.go b/x/stake/test_common.go index 27acebe08..9f52ad8de 100644 --- a/x/stake/test_common.go +++ b/x/stake/test_common.go @@ -22,16 +22,16 @@ import ( // dummy addresses used for testing var ( addrs = []sdk.Address{ - testAddr("A58856F0FD53BF058B4909A21AEC019107BA6160"), - testAddr("A58856F0FD53BF058B4909A21AEC019107BA6161"), - testAddr("A58856F0FD53BF058B4909A21AEC019107BA6162"), - testAddr("A58856F0FD53BF058B4909A21AEC019107BA6163"), - testAddr("A58856F0FD53BF058B4909A21AEC019107BA6164"), - testAddr("A58856F0FD53BF058B4909A21AEC019107BA6165"), - testAddr("A58856F0FD53BF058B4909A21AEC019107BA6166"), - testAddr("A58856F0FD53BF058B4909A21AEC019107BA6167"), - testAddr("A58856F0FD53BF058B4909A21AEC019107BA6168"), - testAddr("A58856F0FD53BF058B4909A21AEC019107BA6169"), + testAddr("A58856F0FD53BF058B4909A21AEC019107BA6160", "cosmosaccaddr:5ky9du8a2wlstz6fpx3p4mqpjyrm5ctqyxjnwh"), + testAddr("A58856F0FD53BF058B4909A21AEC019107BA6161", "cosmosaccaddr:5ky9du8a2wlstz6fpx3p4mqpjyrm5ctpesxxn9"), + testAddr("A58856F0FD53BF058B4909A21AEC019107BA6162", "cosmosaccaddr:5ky9du8a2wlstz6fpx3p4mqpjyrm5ctzhrnsa6"), + testAddr("A58856F0FD53BF058B4909A21AEC019107BA6163", "cosmosaccaddr:5ky9du8a2wlstz6fpx3p4mqpjyrm5ctr2489qg"), + testAddr("A58856F0FD53BF058B4909A21AEC019107BA6164", "cosmosaccaddr:5ky9du8a2wlstz6fpx3p4mqpjyrm5ctytvs4pd"), + testAddr("A58856F0FD53BF058B4909A21AEC019107BA6165", "cosmosaccaddr:5ky9du8a2wlstz6fpx3p4mqpjyrm5ct9k6yqul"), + testAddr("A58856F0FD53BF058B4909A21AEC019107BA6166", "cosmosaccaddr:5ky9du8a2wlstz6fpx3p4mqpjyrm5ctxcf3kjq"), + testAddr("A58856F0FD53BF058B4909A21AEC019107BA6167", "cosmosaccaddr:5ky9du8a2wlstz6fpx3p4mqpjyrm5ct89l9r0j"), + testAddr("A58856F0FD53BF058B4909A21AEC019107BA6168", "cosmosaccaddr:5ky9du8a2wlstz6fpx3p4mqpjyrm5ctg6jkls2"), + testAddr("A58856F0FD53BF058B4909A21AEC019107BA6169", "cosmosaccaddr:5ky9du8a2wlstz6fpx3p4mqpjyrm5ctf8yz2dc"), } // dummy pubkeys used for testing @@ -52,70 +52,14 @@ var ( emptyPubkey crypto.PubKey ) -func validatorsEqual(b1, b2 Validator) bool { - return bytes.Equal(b1.Address, b2.Address) && - b1.PubKey.Equals(b2.PubKey) && - b1.Power.Equal(b2.Power) && - b1.Height == b2.Height && - b1.Counter == b2.Counter +//_______________________________________________________________________________________ + +// intended to be used with require/assert: require.True(ValEq(...)) +func ValEq(t *testing.T, exp, got Validator) (*testing.T, bool, string, Validator, Validator) { + return t, exp.equal(got), "expected:\t%v\ngot:\t\t%v", exp, got } -func candidatesEqual(c1, c2 Candidate) bool { - return c1.Status == c2.Status && - c1.PubKey.Equals(c2.PubKey) && - bytes.Equal(c1.Address, c2.Address) && - c1.Assets.Equal(c2.Assets) && - c1.Liabilities.Equal(c2.Liabilities) && - c1.Description == c2.Description -} - -func bondsEqual(b1, b2 DelegatorBond) bool { - return bytes.Equal(b1.DelegatorAddr, b2.DelegatorAddr) && - bytes.Equal(b1.CandidateAddr, b2.CandidateAddr) && - b1.Height == b2.Height && - b1.Shares.Equal(b2.Shares) -} - -// default params for testing -func defaultParams() Params { - return Params{ - InflationRateChange: sdk.NewRat(13, 100), - InflationMax: sdk.NewRat(20, 100), - InflationMin: sdk.NewRat(7, 100), - GoalBonded: sdk.NewRat(67, 100), - MaxValidators: 100, - BondDenom: "steak", - } -} - -// initial pool for testing -func initialPool() Pool { - return Pool{ - TotalSupply: 0, - BondedShares: sdk.ZeroRat(), - UnbondedShares: sdk.ZeroRat(), - BondedPool: 0, - UnbondedPool: 0, - InflationLastTime: 0, - Inflation: sdk.NewRat(7, 100), - } -} - -// get raw genesis raw message for testing -func GetDefaultGenesisState() GenesisState { - return GenesisState{ - Pool: initialPool(), - Params: defaultParams(), - } -} - -// XXX reference the common declaration of this function -func subspace(prefix []byte) (start, end []byte) { - end = make([]byte, len(prefix)) - copy(end, prefix) - end[len(end)-1]++ - return prefix, end -} +//_______________________________________________________________________________________ func makeTestCodec() *wire.Codec { var cdc = wire.NewCodec() @@ -129,7 +73,7 @@ func makeTestCodec() *wire.Codec { cdc.RegisterConcrete(MsgUnbond{}, "test/stake/Unbond", nil) // Register AppAccount - cdc.RegisterInterface((*sdk.Account)(nil), nil) + cdc.RegisterInterface((*auth.Account)(nil), nil) cdc.RegisterConcrete(&auth.BaseAccount{}, "test/stake/Account", nil) wire.RegisterCrypto(cdc) @@ -148,13 +92,15 @@ func paramsNoInflation() Params { } // hogpodge of all sorts of input required for testing -func createTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context, sdk.AccountMapper, Keeper) { - db := dbm.NewMemDB() - keyStake := sdk.NewKVStoreKey("stake") - keyMain := keyStake //sdk.NewKVStoreKey("main") //TODO fix multistore +func createTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context, auth.AccountMapper, Keeper) { + keyStake := sdk.NewKVStoreKey("stake") + keyAcc := sdk.NewKVStoreKey("acc") + + db := dbm.NewMemDB() ms := store.NewCommitMultiStore(db) ms.MountStoreWithDB(keyStake, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) err := ms.LoadLatestVersion() require.Nil(t, err) @@ -162,13 +108,13 @@ func createTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context cdc := makeTestCodec() accountMapper := auth.NewAccountMapper( cdc, // amino codec - keyMain, // target store + keyAcc, // target store &auth.BaseAccount{}, // prototype ) ck := bank.NewKeeper(accountMapper) keeper := NewKeeper(cdc, keyStake, ck, DefaultCodespace) - keeper.setPool(ctx, initialPool()) - keeper.setParams(ctx, defaultParams()) + keeper.setPool(ctx, InitialPool()) + keeper.setNewParams(ctx, DefaultParams()) // fill all the addresses with some coins for _, addr := range addrs { @@ -192,10 +138,27 @@ func newPubKey(pk string) (res crypto.PubKey) { } // for incode address generation -func testAddr(addr string) sdk.Address { - res, err := sdk.GetAddress(addr) +func testAddr(addr string, bech string) sdk.Address { + + res, err := sdk.GetAccAddressHex(addr) if err != nil { panic(err) } + bechexpected, err := sdk.Bech32CosmosifyAcc(res) + if err != nil { + panic(err) + } + if bech != bechexpected { + panic("Bech encoding doesn't match reference") + } + + bechres, err := sdk.GetAccAddressBech32Cosmos(bech) + if err != nil { + panic(err) + } + if bytes.Compare(bechres, res) != 0 { + panic("Bech decode and hex decode don't match") + } + return res } diff --git a/x/stake/tick.go b/x/stake/tick.go index 9ca484061..a8d945734 100644 --- a/x/stake/tick.go +++ b/x/stake/tick.go @@ -26,12 +26,14 @@ func (k Keeper) Tick(ctx sdk.Context) (change []abci.Validator) { // save the params k.setPool(ctx, p) - // reset the counter - k.setCounter(ctx, 0) + // reset the intra-transaction counter + k.setIntraTxCounter(ctx, 0) - change = k.getAccUpdateValidators(ctx) + // calculate validator set changes + change = k.getTendermintUpdates(ctx) + k.clearTendermintUpdates(ctx) - return + return change } // process provisions for an hour period @@ -44,9 +46,8 @@ func (k Keeper) processProvisions(ctx sdk.Context) Pool { // more bonded tokens are added proportionally to all validators the only term // which needs to be updated is the `BondedPool`. So for each previsions cycle: - provisions := pool.Inflation.Mul(sdk.NewRat(pool.TotalSupply)).Quo(hrsPerYrRat).Evaluate() - pool.BondedPool += provisions - pool.TotalSupply += provisions + provisions := pool.Inflation.Mul(sdk.NewRat(pool.TokenSupply())).Quo(hrsPerYrRat).Evaluate() + pool.BondedTokens += provisions return pool } diff --git a/x/stake/tick_test.go b/x/stake/tick_test.go index f75cf65b5..438b678f1 100644 --- a/x/stake/tick_test.go +++ b/x/stake/tick_test.go @@ -15,39 +15,39 @@ func TestGetInflation(t *testing.T) { hrsPerYrRat := sdk.NewRat(hrsPerYr) // Governing Mechanism: - // bondedRatio = BondedPool / TotalSupply + // bondedRatio = BondedTokens / TotalSupply // inflationRateChangePerYear = (1- bondedRatio/ GoalBonded) * MaxInflationRateChange tests := []struct { - name string - setBondedPool, setTotalSupply int64 - setInflation, expectedChange sdk.Rat + name string + setBondedTokens, setLooseTokens int64 + setInflation, expectedChange sdk.Rat }{ // with 0% bonded atom supply the inflation should increase by InflationRateChange {"test 1", 0, 0, sdk.NewRat(7, 100), params.InflationRateChange.Quo(hrsPerYrRat).Round(precision)}, // 100% bonded, starting at 20% inflation and being reduced // (1 - (1/0.67))*(0.13/8667) - {"test 2", 1, 1, sdk.NewRat(20, 100), + {"test 2", 1, 0, sdk.NewRat(20, 100), sdk.OneRat().Sub(sdk.OneRat().Quo(params.GoalBonded)).Mul(params.InflationRateChange).Quo(hrsPerYrRat).Round(precision)}, // 50% bonded, starting at 10% inflation and being increased - {"test 3", 1, 2, sdk.NewRat(10, 100), + {"test 3", 1, 1, sdk.NewRat(10, 100), sdk.OneRat().Sub(sdk.NewRat(1, 2).Quo(params.GoalBonded)).Mul(params.InflationRateChange).Quo(hrsPerYrRat).Round(precision)}, // test 7% minimum stop (testing with 100% bonded) - {"test 4", 1, 1, sdk.NewRat(7, 100), sdk.ZeroRat()}, - {"test 5", 1, 1, sdk.NewRat(70001, 1000000), sdk.NewRat(-1, 1000000).Round(precision)}, + {"test 4", 1, 0, sdk.NewRat(7, 100), sdk.ZeroRat()}, + {"test 5", 1, 0, sdk.NewRat(70001, 1000000), sdk.NewRat(-1, 1000000).Round(precision)}, // test 20% maximum stop (testing with 0% bonded) {"test 6", 0, 0, sdk.NewRat(20, 100), sdk.ZeroRat()}, {"test 7", 0, 0, sdk.NewRat(199999, 1000000), sdk.NewRat(1, 1000000).Round(precision)}, // perfect balance shouldn't change inflation - {"test 8", 67, 100, sdk.NewRat(15, 100), sdk.ZeroRat()}, + {"test 8", 67, 33, sdk.NewRat(15, 100), sdk.ZeroRat()}, } for _, tc := range tests { - pool.BondedPool, pool.TotalSupply = tc.setBondedPool, tc.setTotalSupply + pool.BondedTokens, pool.LooseUnbondedTokens = tc.setBondedTokens, tc.setLooseTokens pool.Inflation = tc.setInflation keeper.setPool(ctx, pool) @@ -61,73 +61,80 @@ func TestGetInflation(t *testing.T) { func TestProcessProvisions(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) - params := defaultParams() + params := DefaultParams() + params.MaxValidators = 2 keeper.setParams(ctx, params) pool := keeper.GetPool(ctx) - // create some candidates some bonded, some unbonded - candidates := make([]Candidate, 10) - for i := 0; i < 10; i++ { - c := Candidate{ - Status: Unbonded, - PubKey: pks[i], - Address: addrs[i], - Assets: sdk.NewRat(0), - Liabilities: sdk.NewRat(0), - } - if i < 5 { - c.Status = Bonded - } - mintedTokens := int64((i + 1) * 10000000) - pool.TotalSupply += mintedTokens - pool, c, _ = pool.candidateAddTokens(c, mintedTokens) - - keeper.setCandidate(ctx, c) - candidates[i] = c - } - keeper.setPool(ctx, pool) - var totalSupply int64 = 550000000 + var tokenSupply int64 = 550000000 var bondedShares int64 = 150000000 var unbondedShares int64 = 400000000 - assert.Equal(t, totalSupply, pool.TotalSupply) - assert.Equal(t, bondedShares, pool.BondedPool) - assert.Equal(t, unbondedShares, pool.UnbondedPool) + + // create some validators some bonded, some unbonded + var validators [5]Validator + validators[0] = NewValidator(addrs[0], pks[0], Description{}) + validators[0], pool, _ = validators[0].addTokensFromDel(pool, 150000000) + keeper.setPool(ctx, pool) + validators[0] = keeper.updateValidator(ctx, validators[0]) + pool = keeper.GetPool(ctx) + require.Equal(t, bondedShares, pool.BondedTokens, "%v", pool) + + validators[1] = NewValidator(addrs[1], pks[1], Description{}) + validators[1], pool, _ = validators[1].addTokensFromDel(pool, 100000000) + keeper.setPool(ctx, pool) + validators[1] = keeper.updateValidator(ctx, validators[1]) + validators[2] = NewValidator(addrs[2], pks[2], Description{}) + validators[2], pool, _ = validators[2].addTokensFromDel(pool, 100000000) + keeper.setPool(ctx, pool) + validators[2] = keeper.updateValidator(ctx, validators[2]) + validators[3] = NewValidator(addrs[3], pks[3], Description{}) + validators[3], pool, _ = validators[3].addTokensFromDel(pool, 100000000) + keeper.setPool(ctx, pool) + validators[3] = keeper.updateValidator(ctx, validators[3]) + validators[4] = NewValidator(addrs[4], pks[4], Description{}) + validators[4], pool, _ = validators[4].addTokensFromDel(pool, 100000000) + keeper.setPool(ctx, pool) + validators[4] = keeper.updateValidator(ctx, validators[4]) + + assert.Equal(t, tokenSupply, pool.TokenSupply()) + assert.Equal(t, bondedShares, pool.BondedTokens) + assert.Equal(t, unbondedShares, pool.UnbondedTokens) // initial bonded ratio ~ 27% - assert.True(t, pool.bondedRatio().Equal(sdk.NewRat(bondedShares, totalSupply)), "%v", pool.bondedRatio()) + assert.True(t, pool.bondedRatio().Equal(sdk.NewRat(bondedShares, tokenSupply)), "%v", pool.bondedRatio()) - // test the value of candidate shares + // test the value of validator shares assert.True(t, pool.bondedShareExRate().Equal(sdk.OneRat()), "%v", pool.bondedShareExRate()) - initialSupply := pool.TotalSupply - initialUnbonded := pool.TotalSupply - pool.BondedPool + initialSupply := pool.TokenSupply() + initialUnbonded := pool.TokenSupply() - pool.BondedTokens // process the provisions a year for hr := 0; hr < 8766; hr++ { pool := keeper.GetPool(ctx) expInflation := keeper.nextInflation(ctx).Round(1000000000) - expProvisions := (expInflation.Mul(sdk.NewRat(pool.TotalSupply)).Quo(hrsPerYrRat)).Evaluate() - startBondedPool := pool.BondedPool - startTotalSupply := pool.TotalSupply + expProvisions := (expInflation.Mul(sdk.NewRat(pool.TokenSupply())).Quo(hrsPerYrRat)).Evaluate() + startBondedTokens := pool.BondedTokens + startTotalSupply := pool.TokenSupply() pool = keeper.processProvisions(ctx) keeper.setPool(ctx, pool) - //fmt.Printf("hr %v, startBondedPool %v, expProvisions %v, pool.BondedPool %v\n", hr, startBondedPool, expProvisions, pool.BondedPool) - require.Equal(t, startBondedPool+expProvisions, pool.BondedPool, "hr %v", hr) - require.Equal(t, startTotalSupply+expProvisions, pool.TotalSupply) + //fmt.Printf("hr %v, startBondedTokens %v, expProvisions %v, pool.BondedTokens %v\n", hr, startBondedTokens, expProvisions, pool.BondedTokens) + require.Equal(t, startBondedTokens+expProvisions, pool.BondedTokens, "hr %v", hr) + require.Equal(t, startTotalSupply+expProvisions, pool.TokenSupply()) } pool = keeper.GetPool(ctx) - assert.NotEqual(t, initialSupply, pool.TotalSupply) - assert.Equal(t, initialUnbonded, pool.UnbondedPool) - //panic(fmt.Sprintf("debug total %v, bonded %v, diff %v\n", p.TotalSupply, p.BondedPool, pool.TotalSupply-pool.BondedPool)) + assert.NotEqual(t, initialSupply, pool.TokenSupply()) + assert.Equal(t, initialUnbonded, pool.UnbondedTokens) + //panic(fmt.Sprintf("debug total %v, bonded %v, diff %v\n", p.TotalSupply, p.BondedTokens, pool.TokenSupply()-pool.BondedTokens)) // initial bonded ratio ~ from 27% to 40% increase for bonded holders ownership of total supply assert.True(t, pool.bondedRatio().Equal(sdk.NewRat(211813022, 611813022)), "%v", pool.bondedRatio()) // global supply - assert.Equal(t, int64(611813022), pool.TotalSupply) - assert.Equal(t, int64(211813022), pool.BondedPool) - assert.Equal(t, unbondedShares, pool.UnbondedPool) + assert.Equal(t, int64(611813022), pool.TokenSupply()) + assert.Equal(t, int64(211813022), pool.BondedTokens) + assert.Equal(t, unbondedShares, pool.UnbondedTokens) - // test the value of candidate shares + // test the value of validator shares assert.True(t, pool.bondedShareExRate().Mul(sdk.NewRat(bondedShares)).Equal(sdk.NewRat(211813022)), "%v", pool.bondedShareExRate()) } diff --git a/x/stake/types.go b/x/stake/types.go deleted file mode 100644 index 741783fa1..000000000 --- a/x/stake/types.go +++ /dev/null @@ -1,189 +0,0 @@ -package stake - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/wire" - abci "github.com/tendermint/abci/types" - crypto "github.com/tendermint/go-crypto" -) - -// GenesisState - all staking state that must be provided at genesis -type GenesisState struct { - Pool Pool `json:"pool"` - Params Params `json:"params"` - Candidates []Candidate `json:"candidates"` - Bonds []DelegatorBond `json:"bonds"` -} - -//_________________________________________________________________________ - -// Params defines the high level settings for staking -type Params struct { - InflationRateChange sdk.Rat `json:"inflation_rate_change"` // maximum annual change in inflation rate - InflationMax sdk.Rat `json:"inflation_max"` // maximum inflation rate - InflationMin sdk.Rat `json:"inflation_min"` // minimum inflation rate - GoalBonded sdk.Rat `json:"goal_bonded"` // Goal of percent bonded atoms - - MaxValidators uint16 `json:"max_validators"` // maximum number of validators - BondDenom string `json:"bond_denom"` // bondable coin denomination -} - -func (p Params) equal(p2 Params) bool { - return p.InflationRateChange.Equal(p2.InflationRateChange) && - p.InflationMax.Equal(p2.InflationMax) && - p.InflationMin.Equal(p2.InflationMin) && - p.GoalBonded.Equal(p2.GoalBonded) && - p.MaxValidators == p2.MaxValidators && - p.BondDenom == p2.BondDenom -} - -//_________________________________________________________________________ - -// Pool - dynamic parameters of the current state -type Pool struct { - TotalSupply int64 `json:"total_supply"` // total supply of all tokens - BondedShares sdk.Rat `json:"bonded_shares"` // sum of all shares distributed for the Bonded Pool - UnbondedShares sdk.Rat `json:"unbonded_shares"` // sum of all shares distributed for the Unbonded Pool - BondedPool int64 `json:"bonded_pool"` // reserve of bonded tokens - UnbondedPool int64 `json:"unbonded_pool"` // reserve of unbonded tokens held with candidates - InflationLastTime int64 `json:"inflation_last_time"` // block which the last inflation was processed // TODO make time - Inflation sdk.Rat `json:"inflation"` // current annual inflation rate -} - -func (p Pool) equal(p2 Pool) bool { - return p.BondedShares.Equal(p2.BondedShares) && - p.UnbondedShares.Equal(p2.UnbondedShares) && - p.Inflation.Equal(p2.Inflation) && - p.TotalSupply == p2.TotalSupply && - p.BondedPool == p2.BondedPool && - p.UnbondedPool == p2.UnbondedPool && - p.InflationLastTime == p2.InflationLastTime -} - -//_________________________________________________________________________ - -// CandidateStatus - status of a validator-candidate -type CandidateStatus byte - -const ( - // nolint - Bonded CandidateStatus = 0x00 - Unbonded CandidateStatus = 0x01 - Revoked CandidateStatus = 0x02 -) - -// Candidate defines the total amount of bond shares and their exchange rate to -// coins. Accumulation of interest is modelled as an in increase in the -// exchange rate, and slashing as a decrease. When coins are delegated to this -// candidate, the candidate is credited with a DelegatorBond whose number of -// bond shares is based on the amount of coins delegated divided by the current -// exchange rate. Voting power can be calculated as total bonds multiplied by -// exchange rate. -type Candidate struct { - Status CandidateStatus `json:"status"` // Bonded status - Address sdk.Address `json:"owner"` // Sender of BondTx - UnbondTx returns here - PubKey crypto.PubKey `json:"pub_key"` // Pubkey of candidate - Assets sdk.Rat `json:"assets"` // total shares of a global hold pools - Liabilities sdk.Rat `json:"liabilities"` // total shares issued to a candidate's delegators - Description Description `json:"description"` // Description terms for the candidate - ValidatorBondHeight int64 `json:"validator_bond_height"` // Earliest height as a bonded validator - ValidatorBondCounter int16 `json:"validator_bond_counter"` // Block-local tx index of validator change -} - -// Candidates - list of Candidates -type Candidates []Candidate - -// NewCandidate - initialize a new candidate -func NewCandidate(address sdk.Address, pubKey crypto.PubKey, description Description) Candidate { - return Candidate{ - Status: Unbonded, - Address: address, - PubKey: pubKey, - Assets: sdk.ZeroRat(), - Liabilities: sdk.ZeroRat(), - Description: description, - ValidatorBondHeight: int64(0), - ValidatorBondCounter: int16(0), - } -} - -// Description - description fields for a candidate -type Description struct { - Moniker string `json:"moniker"` - Identity string `json:"identity"` - Website string `json:"website"` - Details string `json:"details"` -} - -func NewDescription(moniker, identity, website, details string) Description { - return Description{ - Moniker: moniker, - Identity: identity, - Website: website, - Details: details, - } -} - -// get the exchange rate of global pool shares over delegator shares -func (c Candidate) delegatorShareExRate() sdk.Rat { - if c.Liabilities.IsZero() { - return sdk.OneRat() - } - return c.Assets.Quo(c.Liabilities) -} - -// Validator returns a copy of the Candidate as a Validator. -// Should only be called when the Candidate qualifies as a validator. -func (c Candidate) validator() Validator { - return Validator{ - Address: c.Address, - PubKey: c.PubKey, - Power: c.Assets, - Height: c.ValidatorBondHeight, - Counter: c.ValidatorBondCounter, - } -} - -//XXX updateDescription function -//XXX enforce limit to number of description characters - -//______________________________________________________________________ - -// Validator is one of the top Candidates -type Validator struct { - Address sdk.Address `json:"address"` - PubKey crypto.PubKey `json:"pub_key"` - Power sdk.Rat `json:"voting_power"` - Height int64 `json:"height"` // Earliest height as a validator - Counter int16 `json:"counter"` // Block-local tx index for resolving equal voting power & height -} - -// abci validator from stake validator type -func (v Validator) abciValidator(cdc *wire.Codec) abci.Validator { - return abci.Validator{ - PubKey: v.PubKey.Bytes(), - Power: v.Power.Evaluate(), - } -} - -// abci validator from stake validator type -// with zero power used for validator updates -func (v Validator) abciValidatorZero(cdc *wire.Codec) abci.Validator { - return abci.Validator{ - PubKey: v.PubKey.Bytes(), - Power: 0, - } -} - -//_________________________________________________________________________ - -// DelegatorBond represents the bond with tokens held by an account. It is -// owned by one delegator, and is associated with the voting power of one -// pubKey. -// TODO better way of managing space -type DelegatorBond struct { - DelegatorAddr sdk.Address `json:"delegator_addr"` - CandidateAddr sdk.Address `json:"candidate_addr"` - Shares sdk.Rat `json:"shares"` - Height int64 `json:"height"` // Last height bond updated -} diff --git a/x/stake/types_test.go b/x/stake/types_test.go deleted file mode 100644 index ec16f32d9..000000000 --- a/x/stake/types_test.go +++ /dev/null @@ -1,3 +0,0 @@ -package stake - -// XXX test global state functions, candidate exchange rate functions etc. diff --git a/x/stake/validator.go b/x/stake/validator.go new file mode 100644 index 000000000..cf4ee85fd --- /dev/null +++ b/x/stake/validator.go @@ -0,0 +1,266 @@ +package stake + +import ( + "bytes" + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/wire" + abci "github.com/tendermint/abci/types" + crypto "github.com/tendermint/go-crypto" +) + +// Validator defines the total amount of bond shares and their exchange rate to +// coins. Accumulation of interest is modelled as an in increase in the +// exchange rate, and slashing as a decrease. When coins are delegated to this +// validator, the validator is credited with a Delegation whose number of +// bond shares is based on the amount of coins delegated divided by the current +// exchange rate. Voting power can be calculated as total bonds multiplied by +// exchange rate. +type Validator struct { + Owner sdk.Address `json:"owner"` // sender of BondTx - UnbondTx returns here + PubKey crypto.PubKey `json:"pub_key"` // pubkey of validator + Revoked bool `json:"revoked"` // has the validator been revoked from bonded status? + + PoolShares PoolShares `json:"pool_shares"` // total shares for tokens held in the pool + DelegatorShares sdk.Rat `json:"delegator_shares"` // total shares issued to a validator's delegators + + Description Description `json:"description"` // description terms for the validator + BondHeight int64 `json:"bond_height"` // earliest height as a bonded validator + BondIntraTxCounter int16 `json:"bond_intra_tx_counter"` // block-local tx index of validator change + ProposerRewardPool sdk.Coins `json:"proposer_reward_pool"` // XXX reward pool collected from being the proposer + + Commission sdk.Rat `json:"commission"` // XXX the commission rate of fees charged to any delegators + CommissionMax sdk.Rat `json:"commission_max"` // XXX maximum commission rate which this validator can ever charge + CommissionChangeRate sdk.Rat `json:"commission_change_rate"` // XXX maximum daily increase of the validator commission + CommissionChangeToday sdk.Rat `json:"commission_change_today"` // XXX commission rate change today, reset each day (UTC time) + + // fee related + PrevBondedShares sdk.Rat `json:"prev_bonded_shares"` // total shares of a global hold pools +} + +// Validators - list of Validators +type Validators []Validator + +// NewValidator - initialize a new validator +func NewValidator(owner sdk.Address, pubKey crypto.PubKey, description Description) Validator { + return Validator{ + Owner: owner, + PubKey: pubKey, + PoolShares: NewUnbondedShares(sdk.ZeroRat()), + DelegatorShares: sdk.ZeroRat(), + Description: description, + BondHeight: int64(0), + BondIntraTxCounter: int16(0), + ProposerRewardPool: sdk.Coins{}, + Commission: sdk.ZeroRat(), + CommissionMax: sdk.ZeroRat(), + CommissionChangeRate: sdk.ZeroRat(), + CommissionChangeToday: sdk.ZeroRat(), + PrevBondedShares: sdk.ZeroRat(), + } +} + +// only the vitals - does not check bond height of IntraTxCounter +func (v Validator) equal(c2 Validator) bool { + return v.PubKey.Equals(c2.PubKey) && + bytes.Equal(v.Owner, c2.Owner) && + v.PoolShares.Equal(c2.PoolShares) && + v.DelegatorShares.Equal(c2.DelegatorShares) && + v.Description == c2.Description && + //v.BondHeight == c2.BondHeight && + //v.BondIntraTxCounter == c2.BondIntraTxCounter && // counter is always changing + v.ProposerRewardPool.IsEqual(c2.ProposerRewardPool) && + v.Commission.Equal(c2.Commission) && + v.CommissionMax.Equal(c2.CommissionMax) && + v.CommissionChangeRate.Equal(c2.CommissionChangeRate) && + v.CommissionChangeToday.Equal(c2.CommissionChangeToday) && + v.PrevBondedShares.Equal(c2.PrevBondedShares) +} + +// Description - description fields for a validator +type Description struct { + Moniker string `json:"moniker"` + Identity string `json:"identity"` + Website string `json:"website"` + Details string `json:"details"` +} + +func NewDescription(moniker, identity, website, details string) Description { + return Description{ + Moniker: moniker, + Identity: identity, + Website: website, + Details: details, + } +} + +//XXX updateDescription function which enforce limit to number of description characters + +// abci validator from stake validator type +func (v Validator) abciValidator(cdc *wire.Codec) abci.Validator { + return abci.Validator{ + PubKey: v.PubKey.Bytes(), + Power: v.PoolShares.Bonded().Evaluate(), + } +} + +// abci validator from stake validator type +// with zero power used for validator updates +func (v Validator) abciValidatorZero(cdc *wire.Codec) abci.Validator { + return abci.Validator{ + PubKey: v.PubKey.Bytes(), + Power: 0, + } +} + +// abci validator from stake validator type +func (v Validator) Status() sdk.BondStatus { + return v.PoolShares.Status +} + +// update the location of the shares within a validator if its bond status has changed +func (v Validator) UpdateStatus(pool Pool, NewStatus sdk.BondStatus) (Validator, Pool) { + var tokens int64 + + switch v.Status() { + case sdk.Unbonded: + if NewStatus == sdk.Unbonded { + return v, pool + } + pool, tokens = pool.removeSharesUnbonded(v.PoolShares.Amount) + + case sdk.Unbonding: + if NewStatus == sdk.Unbonding { + return v, pool + } + pool, tokens = pool.removeSharesUnbonding(v.PoolShares.Amount) + + case sdk.Bonded: + if NewStatus == sdk.Bonded { // return if nothing needs switching + return v, pool + } + pool, tokens = pool.removeSharesBonded(v.PoolShares.Amount) + } + + switch NewStatus { + case sdk.Unbonded: + pool, v.PoolShares = pool.addTokensUnbonded(tokens) + case sdk.Unbonding: + pool, v.PoolShares = pool.addTokensUnbonding(tokens) + case sdk.Bonded: + pool, v.PoolShares = pool.addTokensBonded(tokens) + } + return v, pool +} + +// XXX TEST +// get the power or potential power for a validator +// if bonded, the power is the BondedShares +// if not bonded, the power is the amount of bonded shares which the +// the validator would have it was bonded +func (v Validator) EquivalentBondedShares(pool Pool) (eqBondedShares sdk.Rat) { + return v.PoolShares.ToBonded(pool).Amount +} + +//_________________________________________________________________________________________________________ + +// XXX Audit this function further to make sure it's correct +// add tokens to a validator +func (v Validator) addTokensFromDel(pool Pool, + amount int64) (validator2 Validator, p2 Pool, issuedDelegatorShares sdk.Rat) { + + exRate := v.DelegatorShareExRate(pool) // bshr/delshr + + var poolShares PoolShares + var equivalentBondedShares sdk.Rat + switch v.Status() { + case sdk.Unbonded: + pool, poolShares = pool.addTokensUnbonded(amount) + case sdk.Unbonding: + pool, poolShares = pool.addTokensUnbonding(amount) + case sdk.Bonded: + pool, poolShares = pool.addTokensBonded(amount) + } + v.PoolShares.Amount = v.PoolShares.Amount.Add(poolShares.Amount) + equivalentBondedShares = poolShares.ToBonded(pool).Amount + + issuedDelegatorShares = equivalentBondedShares.Quo(exRate) // bshr/(bshr/delshr) = delshr + v.DelegatorShares = v.DelegatorShares.Add(issuedDelegatorShares) + + return v, pool, issuedDelegatorShares +} + +// remove delegator shares from a validator +// NOTE this function assumes the shares have already been updated for the validator status +func (v Validator) removeDelShares(pool Pool, + delShares sdk.Rat) (validator2 Validator, p2 Pool, createdCoins int64) { + + amount := v.DelegatorShareExRate(pool).Mul(delShares) + eqBondedSharesToRemove := NewBondedShares(amount) + v.DelegatorShares = v.DelegatorShares.Sub(delShares) + + switch v.Status() { + case sdk.Unbonded: + unbondedShares := eqBondedSharesToRemove.ToUnbonded(pool).Amount + pool, createdCoins = pool.removeSharesUnbonded(unbondedShares) + v.PoolShares.Amount = v.PoolShares.Amount.Sub(unbondedShares) + case sdk.Unbonding: + unbondingShares := eqBondedSharesToRemove.ToUnbonding(pool).Amount + pool, createdCoins = pool.removeSharesUnbonding(unbondingShares) + v.PoolShares.Amount = v.PoolShares.Amount.Sub(unbondingShares) + case sdk.Bonded: + pool, createdCoins = pool.removeSharesBonded(eqBondedSharesToRemove.Amount) + v.PoolShares.Amount = v.PoolShares.Amount.Sub(eqBondedSharesToRemove.Amount) + } + return v, pool, createdCoins +} + +// get the exchange rate of tokens over delegator shares +// UNITS: eq-val-bonded-shares/delegator-shares +func (v Validator) DelegatorShareExRate(pool Pool) sdk.Rat { + if v.DelegatorShares.IsZero() { + return sdk.OneRat() + } + eqBondedShares := v.PoolShares.ToBonded(pool).Amount + return eqBondedShares.Quo(v.DelegatorShares) +} + +//______________________________________________________________________ + +// ensure fulfills the sdk validator types +var _ sdk.Validator = Validator{} + +// nolint - for sdk.Validator +func (v Validator) GetStatus() sdk.BondStatus { return v.Status() } +func (v Validator) GetOwner() sdk.Address { return v.Owner } +func (v Validator) GetPubKey() crypto.PubKey { return v.PubKey } +func (v Validator) GetPower() sdk.Rat { return v.PoolShares.Bonded() } +func (v Validator) GetBondHeight() int64 { return v.BondHeight } + +//Human Friendly pretty printer +func (v Validator) HumanReadableString() (string, error) { + bechOwner, err := sdk.Bech32CosmosifyAcc(v.Owner) + if err != nil { + return "", err + } + bechVal, err := sdk.Bech32CosmosifyValPub(v.PubKey) + if err != nil { + return "", err + } + resp := "Validator \n" + resp += fmt.Sprintf("Owner: %s\n", bechOwner) + resp += fmt.Sprintf("Validator: %s\n", bechVal) + resp += fmt.Sprintf("Shares: Status %s, Amount: %s\n", sdk.BondStatusToString(v.PoolShares.Status), v.PoolShares.Amount.String()) + resp += fmt.Sprintf("Delegator Shares: %s\n", v.DelegatorShares.String()) + resp += fmt.Sprintf("Description: %s\n", v.Description) + resp += fmt.Sprintf("Bond Height: %d\n", v.BondHeight) + resp += fmt.Sprintf("Proposer Reward Pool: %s\n", v.ProposerRewardPool.String()) + resp += fmt.Sprintf("Commission: %s\n", v.Commission.String()) + resp += fmt.Sprintf("Max Commission Rate: %s\n", v.CommissionMax.String()) + resp += fmt.Sprintf("Comission Change Rate: %s\n", v.CommissionChangeRate.String()) + resp += fmt.Sprintf("Commission Change Today: %s\n", v.CommissionChangeToday.String()) + resp += fmt.Sprintf("Previously Bonded Stares: %s\n", v.PrevBondedShares.String()) + + return resp, nil +} diff --git a/x/stake/validator_test.go b/x/stake/validator_test.go new file mode 100644 index 000000000..db6ab6f4c --- /dev/null +++ b/x/stake/validator_test.go @@ -0,0 +1,408 @@ +package stake + +import ( + "fmt" + "math/rand" + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestAddTokensValidatorBonded(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + + pool := keeper.GetPool(ctx) + val := NewValidator(addrs[0], pks[0], Description{}) + val, pool = val.UpdateStatus(pool, sdk.Bonded) + val, pool, delShares := val.addTokensFromDel(pool, 10) + + assert.Equal(t, sdk.OneRat(), val.DelegatorShareExRate(pool)) + assert.Equal(t, sdk.OneRat(), pool.bondedShareExRate()) + assert.Equal(t, sdk.OneRat(), pool.unbondingShareExRate()) + assert.Equal(t, sdk.OneRat(), pool.unbondedShareExRate()) + + assert.True(sdk.RatEq(t, sdk.NewRat(10), delShares)) + assert.True(sdk.RatEq(t, sdk.NewRat(10), val.PoolShares.Bonded())) +} + +func TestAddTokensValidatorUnbonding(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + + pool := keeper.GetPool(ctx) + val := NewValidator(addrs[0], pks[0], Description{}) + val, pool = val.UpdateStatus(pool, sdk.Unbonding) + val, pool, delShares := val.addTokensFromDel(pool, 10) + + assert.Equal(t, sdk.OneRat(), val.DelegatorShareExRate(pool)) + assert.Equal(t, sdk.OneRat(), pool.bondedShareExRate()) + assert.Equal(t, sdk.OneRat(), pool.unbondingShareExRate()) + assert.Equal(t, sdk.OneRat(), pool.unbondedShareExRate()) + + assert.True(sdk.RatEq(t, sdk.NewRat(10), delShares)) + assert.True(sdk.RatEq(t, sdk.NewRat(10), val.PoolShares.Unbonding())) +} + +func TestAddTokensValidatorUnbonded(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + + pool := keeper.GetPool(ctx) + val := NewValidator(addrs[0], pks[0], Description{}) + val, pool = val.UpdateStatus(pool, sdk.Unbonded) + val, pool, delShares := val.addTokensFromDel(pool, 10) + + assert.Equal(t, sdk.OneRat(), val.DelegatorShareExRate(pool)) + assert.Equal(t, sdk.OneRat(), pool.bondedShareExRate()) + assert.Equal(t, sdk.OneRat(), pool.unbondingShareExRate()) + assert.Equal(t, sdk.OneRat(), pool.unbondedShareExRate()) + + assert.True(sdk.RatEq(t, sdk.NewRat(10), delShares)) + assert.True(sdk.RatEq(t, sdk.NewRat(10), val.PoolShares.Unbonded())) +} + +// TODO refactor to make simpler like the AddToken tests above +func TestRemoveShares(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + + poolA := keeper.GetPool(ctx) + valA := Validator{ + Owner: addrs[0], + PubKey: pks[0], + PoolShares: NewBondedShares(sdk.NewRat(9)), + DelegatorShares: sdk.NewRat(9), + } + poolA.BondedTokens = valA.PoolShares.Bonded().Evaluate() + poolA.BondedShares = valA.PoolShares.Bonded() + assert.Equal(t, valA.DelegatorShareExRate(poolA), sdk.OneRat()) + assert.Equal(t, poolA.bondedShareExRate(), sdk.OneRat()) + assert.Equal(t, poolA.unbondedShareExRate(), sdk.OneRat()) + valB, poolB, coinsB := valA.removeDelShares(poolA, sdk.NewRat(10)) + + // coins were created + assert.Equal(t, coinsB, int64(10)) + // pool shares were removed + assert.Equal(t, valB.PoolShares.Bonded(), valA.PoolShares.Bonded().Sub(sdk.NewRat(10).Mul(valA.DelegatorShareExRate(poolA)))) + // conservation of tokens + assert.Equal(t, poolB.UnbondedTokens+poolB.BondedTokens+coinsB, poolA.UnbondedTokens+poolA.BondedTokens) + + // specific case from random tests + poolShares := sdk.NewRat(5102) + delShares := sdk.NewRat(115) + val := Validator{ + Owner: addrs[0], + PubKey: pks[0], + PoolShares: NewBondedShares(poolShares), + DelegatorShares: delShares, + } + pool := Pool{ + BondedShares: sdk.NewRat(248305), + UnbondedShares: sdk.NewRat(232147), + BondedTokens: 248305, + UnbondedTokens: 232147, + InflationLastTime: 0, + Inflation: sdk.NewRat(7, 100), + } + shares := sdk.NewRat(29) + msg := fmt.Sprintf("validator %s (status: %d, poolShares: %v, delShares: %v, DelegatorShareExRate: %v)", + val.Owner, val.Status(), val.PoolShares.Bonded(), val.DelegatorShares, val.DelegatorShareExRate(pool)) + msg = fmt.Sprintf("Removed %v shares from %s", shares, msg) + _, newPool, tokens := val.removeDelShares(pool, shares) + require.Equal(t, + tokens+newPool.UnbondedTokens+newPool.BondedTokens, + pool.BondedTokens+pool.UnbondedTokens, + "Tokens were not conserved: %s", msg) +} + +func TestUpdateStatus(t *testing.T) { + ctx, _, keeper := createTestInput(t, false, 0) + pool := keeper.GetPool(ctx) + + val := NewValidator(addrs[0], pks[0], Description{}) + val, pool, _ = val.addTokensFromDel(pool, 100) + assert.Equal(t, int64(0), val.PoolShares.Bonded().Evaluate()) + assert.Equal(t, int64(0), val.PoolShares.Unbonding().Evaluate()) + assert.Equal(t, int64(100), val.PoolShares.Unbonded().Evaluate()) + assert.Equal(t, int64(0), pool.BondedTokens) + assert.Equal(t, int64(0), pool.UnbondingTokens) + assert.Equal(t, int64(100), pool.UnbondedTokens) + + val, pool = val.UpdateStatus(pool, sdk.Unbonding) + assert.Equal(t, int64(0), val.PoolShares.Bonded().Evaluate()) + assert.Equal(t, int64(100), val.PoolShares.Unbonding().Evaluate()) + assert.Equal(t, int64(0), val.PoolShares.Unbonded().Evaluate()) + assert.Equal(t, int64(0), pool.BondedTokens) + assert.Equal(t, int64(100), pool.UnbondingTokens) + assert.Equal(t, int64(0), pool.UnbondedTokens) + + val, pool = val.UpdateStatus(pool, sdk.Bonded) + assert.Equal(t, int64(100), val.PoolShares.Bonded().Evaluate()) + assert.Equal(t, int64(0), val.PoolShares.Unbonding().Evaluate()) + assert.Equal(t, int64(0), val.PoolShares.Unbonded().Evaluate()) + assert.Equal(t, int64(100), pool.BondedTokens) + assert.Equal(t, int64(0), pool.UnbondingTokens) + assert.Equal(t, int64(0), pool.UnbondedTokens) +} + +//________________________________________________________________________________ +// TODO refactor this random setup + +// generate a random validator +func randomValidator(r *rand.Rand) Validator { + + poolSharesAmt := sdk.NewRat(int64(r.Int31n(10000))) + delShares := sdk.NewRat(int64(r.Int31n(10000))) + + var pShares PoolShares + if r.Float64() < float64(0.5) { + pShares = NewBondedShares(poolSharesAmt) + } else { + pShares = NewUnbondedShares(poolSharesAmt) + } + return Validator{ + Owner: addrs[0], + PubKey: pks[0], + PoolShares: pShares, + DelegatorShares: delShares, + } +} + +// generate a random staking state +func randomSetup(r *rand.Rand, numValidators int) (Pool, Validators) { + pool := InitialPool() + + validators := make([]Validator, numValidators) + for i := 0; i < numValidators; i++ { + validator := randomValidator(r) + if validator.Status() == sdk.Bonded { + pool.BondedShares = pool.BondedShares.Add(validator.PoolShares.Bonded()) + pool.BondedTokens += validator.PoolShares.Bonded().Evaluate() + } else if validator.Status() == sdk.Unbonded { + pool.UnbondedShares = pool.UnbondedShares.Add(validator.PoolShares.Unbonded()) + pool.UnbondedTokens += validator.PoolShares.Unbonded().Evaluate() + } + validators[i] = validator + } + return pool, validators +} + +// any operation that transforms staking state +// takes in RNG instance, pool, validator +// returns updated pool, updated validator, delta tokens, descriptive message +type Operation func(r *rand.Rand, pool Pool, c Validator) (Pool, Validator, int64, string) + +// operation: bond or unbond a validator depending on current status +func OpBondOrUnbond(r *rand.Rand, pool Pool, val Validator) (Pool, Validator, int64, string) { + var msg string + var newStatus sdk.BondStatus + if val.Status() == sdk.Bonded { + msg = fmt.Sprintf("sdk.Unbonded previously bonded validator %s (poolShares: %v, delShares: %v, DelegatorShareExRate: %v)", + val.Owner, val.PoolShares.Bonded(), val.DelegatorShares, val.DelegatorShareExRate(pool)) + newStatus = sdk.Unbonded + + } else if val.Status() == sdk.Unbonded { + msg = fmt.Sprintf("sdk.Bonded previously unbonded validator %s (poolShares: %v, delShares: %v, DelegatorShareExRate: %v)", + val.Owner, val.PoolShares.Bonded(), val.DelegatorShares, val.DelegatorShareExRate(pool)) + newStatus = sdk.Bonded + } + val, pool = val.UpdateStatus(pool, newStatus) + return pool, val, 0, msg +} + +// operation: add a random number of tokens to a validator +func OpAddTokens(r *rand.Rand, pool Pool, val Validator) (Pool, Validator, int64, string) { + tokens := int64(r.Int31n(1000)) + msg := fmt.Sprintf("validator %s (status: %d, poolShares: %v, delShares: %v, DelegatorShareExRate: %v)", + val.Owner, val.Status(), val.PoolShares.Bonded(), val.DelegatorShares, val.DelegatorShareExRate(pool)) + val, pool, _ = val.addTokensFromDel(pool, tokens) + msg = fmt.Sprintf("Added %d tokens to %s", tokens, msg) + return pool, val, -1 * tokens, msg // tokens are removed so for accounting must be negative +} + +// operation: remove a random number of shares from a validator +func OpRemoveShares(r *rand.Rand, pool Pool, val Validator) (Pool, Validator, int64, string) { + var shares sdk.Rat + for { + shares = sdk.NewRat(int64(r.Int31n(1000))) + if shares.LT(val.DelegatorShares) { + break + } + } + + msg := fmt.Sprintf("Removed %v shares from validator %s (status: %d, poolShares: %v, delShares: %v, DelegatorShareExRate: %v)", + shares, val.Owner, val.Status(), val.PoolShares, val.DelegatorShares, val.DelegatorShareExRate(pool)) + + val, pool, tokens := val.removeDelShares(pool, shares) + return pool, val, tokens, msg +} + +// pick a random staking operation +func randomOperation(r *rand.Rand) Operation { + operations := []Operation{ + OpBondOrUnbond, + OpAddTokens, + OpRemoveShares, + } + r.Shuffle(len(operations), func(i, j int) { + operations[i], operations[j] = operations[j], operations[i] + }) + return operations[0] +} + +// ensure invariants that should always be true are true +func assertInvariants(t *testing.T, msg string, + pOrig Pool, cOrig Validators, pMod Pool, vMods Validators, tokens int64) { + + // total tokens conserved + require.Equal(t, + pOrig.UnbondedTokens+pOrig.BondedTokens, + pMod.UnbondedTokens+pMod.BondedTokens+tokens, + "Tokens not conserved - msg: %v\n, pOrig.PoolShares.Bonded(): %v, pOrig.PoolShares.Unbonded(): %v, pMod.PoolShares.Bonded(): %v, pMod.PoolShares.Unbonded(): %v, pOrig.UnbondedTokens: %v, pOrig.BondedTokens: %v, pMod.UnbondedTokens: %v, pMod.BondedTokens: %v, tokens: %v\n", + msg, + pOrig.BondedShares, pOrig.UnbondedShares, + pMod.BondedShares, pMod.UnbondedShares, + pOrig.UnbondedTokens, pOrig.BondedTokens, + pMod.UnbondedTokens, pMod.BondedTokens, tokens) + + // nonnegative bonded shares + require.False(t, pMod.BondedShares.LT(sdk.ZeroRat()), + "Negative bonded shares - msg: %v\npOrig: %v\npMod: %v\ntokens: %v\n", + msg, pOrig, pMod, tokens) + + // nonnegative unbonded shares + require.False(t, pMod.UnbondedShares.LT(sdk.ZeroRat()), + "Negative unbonded shares - msg: %v\npOrig: %v\npMod: %v\ntokens: %v\n", + msg, pOrig, pMod, tokens) + + // nonnegative bonded ex rate + require.False(t, pMod.bondedShareExRate().LT(sdk.ZeroRat()), + "Applying operation \"%s\" resulted in negative bondedShareExRate: %d", + msg, pMod.bondedShareExRate().Evaluate()) + + // nonnegative unbonded ex rate + require.False(t, pMod.unbondedShareExRate().LT(sdk.ZeroRat()), + "Applying operation \"%s\" resulted in negative unbondedShareExRate: %d", + msg, pMod.unbondedShareExRate().Evaluate()) + + for _, vMod := range vMods { + + // nonnegative ex rate + require.False(t, vMod.DelegatorShareExRate(pMod).LT(sdk.ZeroRat()), + "Applying operation \"%s\" resulted in negative validator.DelegatorShareExRate(): %v (validator.Owner: %s)", + msg, + vMod.DelegatorShareExRate(pMod), + vMod.Owner, + ) + + // nonnegative poolShares + require.False(t, vMod.PoolShares.Bonded().LT(sdk.ZeroRat()), + "Applying operation \"%s\" resulted in negative validator.PoolShares.Bonded(): %v (validator.DelegatorShares: %v, validator.DelegatorShareExRate: %v, validator.Owner: %s)", + msg, + vMod.PoolShares.Bonded(), + vMod.DelegatorShares, + vMod.DelegatorShareExRate(pMod), + vMod.Owner, + ) + + // nonnegative delShares + require.False(t, vMod.DelegatorShares.LT(sdk.ZeroRat()), + "Applying operation \"%s\" resulted in negative validator.DelegatorShares: %v (validator.PoolShares.Bonded(): %v, validator.DelegatorShareExRate: %v, validator.Owner: %s)", + msg, + vMod.DelegatorShares, + vMod.PoolShares.Bonded(), + vMod.DelegatorShareExRate(pMod), + vMod.Owner, + ) + + } + +} + +func TestPossibleOverflow(t *testing.T) { + poolShares := sdk.NewRat(2159) + delShares := sdk.NewRat(391432570689183511).Quo(sdk.NewRat(40113011844664)) + val := Validator{ + Owner: addrs[0], + PubKey: pks[0], + PoolShares: NewBondedShares(poolShares), + DelegatorShares: delShares, + } + pool := Pool{ + BondedShares: poolShares, + UnbondedShares: sdk.ZeroRat(), + BondedTokens: poolShares.Evaluate(), + UnbondedTokens: 0, + InflationLastTime: 0, + Inflation: sdk.NewRat(7, 100), + } + tokens := int64(71) + msg := fmt.Sprintf("validator %s (status: %d, poolShares: %v, delShares: %v, DelegatorShareExRate: %v)", + val.Owner, val.Status(), val.PoolShares.Bonded(), val.DelegatorShares, val.DelegatorShareExRate(pool)) + newValidator, _, _ := val.addTokensFromDel(pool, tokens) + + msg = fmt.Sprintf("Added %d tokens to %s", tokens, msg) + require.False(t, newValidator.DelegatorShareExRate(pool).LT(sdk.ZeroRat()), + "Applying operation \"%s\" resulted in negative DelegatorShareExRate(): %v", + msg, newValidator.DelegatorShareExRate(pool)) +} + +// run random operations in a random order on a random single-validator state, assert invariants hold +func TestSingleValidatorIntegrationInvariants(t *testing.T) { + r := rand.New(rand.NewSource(41)) + + for i := 0; i < 10; i++ { + poolOrig, validatorsOrig := randomSetup(r, 1) + require.Equal(t, 1, len(validatorsOrig)) + + // sanity check + assertInvariants(t, "no operation", + poolOrig, validatorsOrig, + poolOrig, validatorsOrig, 0) + + for j := 0; j < 5; j++ { + poolMod, validatorMod, tokens, msg := randomOperation(r)(r, poolOrig, validatorsOrig[0]) + + validatorsMod := make([]Validator, len(validatorsOrig)) + copy(validatorsMod[:], validatorsOrig[:]) + require.Equal(t, 1, len(validatorsOrig), "j %v", j) + require.Equal(t, 1, len(validatorsMod), "j %v", j) + validatorsMod[0] = validatorMod + + assertInvariants(t, msg, + poolOrig, validatorsOrig, + poolMod, validatorsMod, tokens) + + poolOrig = poolMod + validatorsOrig = validatorsMod + } + } +} + +// run random operations in a random order on a random multi-validator state, assert invariants hold +func TestMultiValidatorIntegrationInvariants(t *testing.T) { + r := rand.New(rand.NewSource(42)) + + for i := 0; i < 10; i++ { + poolOrig, validatorsOrig := randomSetup(r, 100) + + assertInvariants(t, "no operation", + poolOrig, validatorsOrig, + poolOrig, validatorsOrig, 0) + + for j := 0; j < 5; j++ { + index := int(r.Int31n(int32(len(validatorsOrig)))) + poolMod, validatorMod, tokens, msg := randomOperation(r)(r, poolOrig, validatorsOrig[index]) + validatorsMod := make([]Validator, len(validatorsOrig)) + copy(validatorsMod[:], validatorsOrig[:]) + validatorsMod[index] = validatorMod + + assertInvariants(t, msg, + poolOrig, validatorsOrig, + poolMod, validatorsMod, tokens) + + poolOrig = poolMod + validatorsOrig = validatorsMod + + } + } +} diff --git a/x/stake/view_slash_keeper.go b/x/stake/view_slash_keeper.go index 375c7323b..cb7d16ce9 100644 --- a/x/stake/view_slash_keeper.go +++ b/x/stake/view_slash_keeper.go @@ -17,13 +17,13 @@ func NewViewSlashKeeper(k Keeper) ViewSlashKeeper { } // load a delegator bond -func (v ViewSlashKeeper) GetDelegatorBond(ctx sdk.Context, - delegatorAddr, candidateAddr sdk.Address) (bond DelegatorBond, found bool) { - return v.keeper.GetDelegatorBond(ctx, delegatorAddr, candidateAddr) +func (v ViewSlashKeeper) GetDelegation(ctx sdk.Context, + delegatorAddr sdk.Address, validatorAddr sdk.Address) (bond Delegation, found bool) { + return v.keeper.GetDelegation(ctx, delegatorAddr, validatorAddr) } // load n delegator bonds -func (v ViewSlashKeeper) GetDelegatorBonds(ctx sdk.Context, - delegator sdk.Address, maxRetrieve int16) (bonds []DelegatorBond) { - return v.keeper.GetDelegatorBonds(ctx, delegator, maxRetrieve) +func (v ViewSlashKeeper) GetDelegations(ctx sdk.Context, + delegator sdk.Address, maxRetrieve int16) (bonds []Delegation) { + return v.keeper.GetDelegations(ctx, delegator, maxRetrieve) } diff --git a/x/stake/view_slash_keeper_test.go b/x/stake/view_slash_keeper_test.go index 738085910..7f481fc60 100644 --- a/x/stake/view_slash_keeper_test.go +++ b/x/stake/view_slash_keeper_test.go @@ -9,78 +9,78 @@ import ( "github.com/stretchr/testify/require" ) -// tests GetDelegatorBond, GetDelegatorBonds +// tests GetDelegation, GetDelegations func TestViewSlashBond(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) - //construct the candidates + //construct the validators amts := []int64{9, 8, 7} - var candidates [3]Candidate + var validators [3]Validator for i, amt := range amts { - candidates[i] = Candidate{ - Address: addrVals[i], - PubKey: pks[i], - Assets: sdk.NewRat(amt), - Liabilities: sdk.NewRat(amt), + validators[i] = Validator{ + Owner: addrVals[i], + PubKey: pks[i], + PoolShares: NewUnbondedShares(sdk.NewRat(amt)), + DelegatorShares: sdk.NewRat(amt), } } - // first add a candidates[0] to delegate too - keeper.setCandidate(ctx, candidates[0]) + // first add a validators[0] to delegate too + keeper.updateValidator(ctx, validators[0]) - bond1to1 := DelegatorBond{ + bond1to1 := Delegation{ DelegatorAddr: addrDels[0], - CandidateAddr: addrVals[0], + ValidatorAddr: addrVals[0], Shares: sdk.NewRat(9), } viewSlashKeeper := NewViewSlashKeeper(keeper) // check the empty keeper first - _, found := viewSlashKeeper.GetDelegatorBond(ctx, addrDels[0], addrVals[0]) + _, found := viewSlashKeeper.GetDelegation(ctx, addrDels[0], addrVals[0]) assert.False(t, found) // set and retrieve a record - keeper.setDelegatorBond(ctx, bond1to1) - resBond, found := viewSlashKeeper.GetDelegatorBond(ctx, addrDels[0], addrVals[0]) + keeper.setDelegation(ctx, bond1to1) + resBond, found := viewSlashKeeper.GetDelegation(ctx, addrDels[0], addrVals[0]) assert.True(t, found) - assert.True(t, bondsEqual(bond1to1, resBond)) + assert.True(t, bond1to1.equal(resBond)) // modify a records, save, and retrieve bond1to1.Shares = sdk.NewRat(99) - keeper.setDelegatorBond(ctx, bond1to1) - resBond, found = viewSlashKeeper.GetDelegatorBond(ctx, addrDels[0], addrVals[0]) + keeper.setDelegation(ctx, bond1to1) + resBond, found = viewSlashKeeper.GetDelegation(ctx, addrDels[0], addrVals[0]) assert.True(t, found) - assert.True(t, bondsEqual(bond1to1, resBond)) + assert.True(t, bond1to1.equal(resBond)) // add some more records - keeper.setCandidate(ctx, candidates[1]) - keeper.setCandidate(ctx, candidates[2]) - bond1to2 := DelegatorBond{addrDels[0], addrVals[1], sdk.NewRat(9), 0} - bond1to3 := DelegatorBond{addrDels[0], addrVals[2], sdk.NewRat(9), 1} - bond2to1 := DelegatorBond{addrDels[1], addrVals[0], sdk.NewRat(9), 2} - bond2to2 := DelegatorBond{addrDels[1], addrVals[1], sdk.NewRat(9), 3} - bond2to3 := DelegatorBond{addrDels[1], addrVals[2], sdk.NewRat(9), 4} - keeper.setDelegatorBond(ctx, bond1to2) - keeper.setDelegatorBond(ctx, bond1to3) - keeper.setDelegatorBond(ctx, bond2to1) - keeper.setDelegatorBond(ctx, bond2to2) - keeper.setDelegatorBond(ctx, bond2to3) + keeper.updateValidator(ctx, validators[1]) + keeper.updateValidator(ctx, validators[2]) + bond1to2 := Delegation{addrDels[0], addrVals[1], sdk.NewRat(9), 0} + bond1to3 := Delegation{addrDels[0], addrVals[2], sdk.NewRat(9), 1} + bond2to1 := Delegation{addrDels[1], addrVals[0], sdk.NewRat(9), 2} + bond2to2 := Delegation{addrDels[1], addrVals[1], sdk.NewRat(9), 3} + bond2to3 := Delegation{addrDels[1], addrVals[2], sdk.NewRat(9), 4} + keeper.setDelegation(ctx, bond1to2) + keeper.setDelegation(ctx, bond1to3) + keeper.setDelegation(ctx, bond2to1) + keeper.setDelegation(ctx, bond2to2) + keeper.setDelegation(ctx, bond2to3) // test all bond retrieve capabilities - resBonds := viewSlashKeeper.GetDelegatorBonds(ctx, addrDels[0], 5) + resBonds := viewSlashKeeper.GetDelegations(ctx, addrDels[0], 5) require.Equal(t, 3, len(resBonds)) - assert.True(t, bondsEqual(bond1to1, resBonds[0])) - assert.True(t, bondsEqual(bond1to2, resBonds[1])) - assert.True(t, bondsEqual(bond1to3, resBonds[2])) - resBonds = viewSlashKeeper.GetDelegatorBonds(ctx, addrDels[0], 3) + assert.True(t, bond1to1.equal(resBonds[0])) + assert.True(t, bond1to2.equal(resBonds[1])) + assert.True(t, bond1to3.equal(resBonds[2])) + resBonds = viewSlashKeeper.GetDelegations(ctx, addrDels[0], 3) require.Equal(t, 3, len(resBonds)) - resBonds = viewSlashKeeper.GetDelegatorBonds(ctx, addrDels[0], 2) + resBonds = viewSlashKeeper.GetDelegations(ctx, addrDels[0], 2) require.Equal(t, 2, len(resBonds)) - resBonds = viewSlashKeeper.GetDelegatorBonds(ctx, addrDels[1], 5) + resBonds = viewSlashKeeper.GetDelegations(ctx, addrDels[1], 5) require.Equal(t, 3, len(resBonds)) - assert.True(t, bondsEqual(bond2to1, resBonds[0])) - assert.True(t, bondsEqual(bond2to2, resBonds[1])) - assert.True(t, bondsEqual(bond2to3, resBonds[2])) + assert.True(t, bond2to1.equal(resBonds[0])) + assert.True(t, bond2to2.equal(resBonds[1])) + assert.True(t, bond2to3.equal(resBonds[2])) } diff --git a/x/stake/wire.go b/x/stake/wire.go index e99c621fa..6e6e38260 100644 --- a/x/stake/wire.go +++ b/x/stake/wire.go @@ -11,3 +11,5 @@ func RegisterWire(cdc *wire.Codec) { cdc.RegisterConcrete(MsgDelegate{}, "cosmos-sdk/MsgDelegate", nil) cdc.RegisterConcrete(MsgUnbond{}, "cosmos-sdk/MsgUnbond", nil) } + +var cdcEmpty = wire.NewCodec()