forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Simple Staking as a module
The simple staking module allows validators to bond and add more stake to their bond. It doesn't allow partial unbond and has no delegation. The staking power per validator though is correctly reflected within the consensus.
- Loading branch information
Adrian Brink
committed
Mar 20, 2018
1 parent
4bfa40a
commit 75674a9
Showing
11 changed files
with
446 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package server | ||
|
||
import ( | ||
//"os" | ||
// "os" | ||
"testing" | ||
"time" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package commands | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
crypto "github.com/tendermint/go-crypto" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/builder" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/wire" | ||
"github.com/cosmos/cosmos-sdk/x/staking" | ||
) | ||
|
||
const ( | ||
flagStake = "stake" | ||
flagValidator = "validator" | ||
) | ||
|
||
func BondTxCmd(cdc *wire.Codec) *cobra.Command { | ||
cmdr := commander{cdc} | ||
cmd := &cobra.Command{ | ||
Use: "bond", | ||
Short: "Bond to a validator", | ||
RunE: cmdr.bondTxCmd, | ||
} | ||
cmd.Flags().String(flagStake, "", "Amount of coins to stake") | ||
cmd.Flags().String(flagValidator, "", "Validator address to stake") | ||
return cmd | ||
} | ||
|
||
func UnbondTxCmd(cdc *wire.Codec) *cobra.Command { | ||
cmdr := commander{cdc} | ||
cmd := &cobra.Command{ | ||
Use: "unbond", | ||
Short: "Unbond from a validator", | ||
RunE: cmdr.unbondTxCmd, | ||
} | ||
return cmd | ||
} | ||
|
||
type commander struct { | ||
cdc *wire.Codec | ||
} | ||
|
||
func (co commander) bondTxCmd(cmd *cobra.Command, args []string) error { | ||
from, err := builder.GetFromAddress() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
stake, err := sdk.ParseCoin(viper.GetString(flagStake)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rawPubKey, err := hex.DecodeString(viper.GetString(flagValidator)) | ||
if err != nil { | ||
return err | ||
} | ||
var pubKey crypto.PubKeyEd25519 | ||
copy(pubKey[:], rawPubKey) | ||
|
||
msg := staking.NewBondMsg(from, stake, pubKey.Wrap()) | ||
|
||
return co.sendMsg(msg) | ||
} | ||
|
||
func (co commander) unbondTxCmd(cmd *cobra.Command, args []string) error { | ||
from, err := builder.GetFromAddress() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
msg := staking.NewUnbondMsg(from) | ||
|
||
return co.sendMsg(msg) | ||
} | ||
|
||
func (co commander) sendMsg(msg sdk.Msg) error { | ||
name := viper.GetString(client.FlagName) | ||
buf := client.BufferStdin() | ||
prompt := fmt.Sprintf("Password to sign with '%s':", name) | ||
passphrase, err := client.GetPassword(prompt, buf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
res, err := builder.SignBuildBroadcast(name, passphrase, msg, co.cdc) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String()) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package staking | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
const ( | ||
// Staking errors reserve 300 - 399. | ||
CodeEmptyValidator sdk.CodeType = 300 | ||
CodeInvalidUnbond sdk.CodeType = 301 | ||
) | ||
|
||
func ErrEmptyValidator() sdk.Error { | ||
return newError(CodeEmptyValidator, "") | ||
} | ||
|
||
func ErrInvalidUnbond() sdk.Error { | ||
return newError(CodeInvalidUnbond, "") | ||
} | ||
|
||
// ----------------------------- | ||
// Helpers | ||
|
||
func newError(code sdk.CodeType, msg string) sdk.Error { | ||
return sdk.NewError(code, msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package staking | ||
|
||
import ( | ||
abci "github.com/tendermint/abci/types" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/bank" | ||
) | ||
|
||
func NewHandler(sm StakingMapper, ck bank.CoinKeeper) sdk.Handler { | ||
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { | ||
switch msg := msg.(type) { | ||
case BondMsg: | ||
return handleBondMsg(ctx, sm, ck, msg) | ||
case UnbondMsg: | ||
return handleUnbondMsg(ctx, sm, ck, msg) | ||
default: | ||
return sdk.ErrUnknownRequest("No match for message type.").Result() | ||
} | ||
} | ||
} | ||
|
||
func handleBondMsg(ctx sdk.Context, sm StakingMapper, ck bank.CoinKeeper, msg BondMsg) sdk.Result { | ||
_, err := ck.SubtractCoins(ctx, msg.Address, []sdk.Coin{msg.Stake}) | ||
if err != nil { | ||
return err.Result() | ||
} | ||
|
||
power, err := sm.Bond(ctx, msg.Address, msg.PubKey, msg.Stake.Amount) | ||
if err != nil { | ||
return err.Result() | ||
} | ||
|
||
valSet := abci.Validator{ | ||
PubKey: msg.PubKey.Bytes(), | ||
Power: power, | ||
} | ||
|
||
return sdk.Result{ | ||
Code: sdk.CodeOK, | ||
ValidatorUpdates: abci.Validators{valSet}, | ||
} | ||
} | ||
|
||
func handleUnbondMsg(ctx sdk.Context, sm StakingMapper, ck bank.CoinKeeper, msg UnbondMsg) sdk.Result { | ||
pubKey, power, err := sm.Unbond(ctx, msg.Address) | ||
if err != nil { | ||
return err.Result() | ||
} | ||
|
||
stake := sdk.Coin{ | ||
Denom: "mycoin", | ||
Amount: power, | ||
} | ||
_, err = ck.AddCoins(ctx, msg.Address, sdk.Coins{stake}) | ||
if err != nil { | ||
return err.Result() | ||
} | ||
|
||
valSet := abci.Validator{ | ||
PubKey: pubKey.Bytes(), | ||
Power: int64(0), | ||
} | ||
|
||
return sdk.Result{ | ||
Code: sdk.CodeOK, | ||
ValidatorUpdates: abci.Validators{valSet}, | ||
} | ||
} |
Oops, something went wrong.