Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
ValarDragon committed Aug 8, 2018
1 parent 41d0355 commit 2f85fff
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 30 deletions.
7 changes: 7 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## v0.24.0 PENDING
^--- PENDING wasn't purged on sdk v0.23.0 release.

BREAKING CHANGES
* Update to tendermint v0.23.0. This involves removing crypto.Pubkey,
maintaining a validator address to pubkey map, and using time.Time instead of int64 for time.

## PENDING

BREAKING CHANGES
Expand Down
3 changes: 1 addition & 2 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package app

import (
"encoding/json"
"fmt"
"io"
"os"

Expand Down Expand Up @@ -137,7 +136,6 @@ func MakeCodec() *wire.Codec {

// application updates every end block
func (app *GaiaApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
fmt.Println(ctx.SigningValidators())
tags := slashing.BeginBlocker(ctx, req, app.slashingKeeper)

return abci.ResponseBeginBlock{
Expand All @@ -150,6 +148,7 @@ func (app *GaiaApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) ab
func (app *GaiaApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
tags := gov.EndBlocker(ctx, app.govKeeper)
validatorUpdates := stake.EndBlocker(ctx, app.stakeKeeper)
// Add these new validators to the addr -> pubkey map.
app.slashingKeeper.AddValidators(validatorUpdates)
return abci.ResponseEndBlock{
ValidatorUpdates: validatorUpdates,
Expand Down
2 changes: 1 addition & 1 deletion docs/sdk/core/app2.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func NewCodec() *wire.Codec {
```

Note: We also register the types in the `tendermint/tendermint/crypto` module so that `crypto.PubKey`
and `[]byte` are encoded/decoded correctly.
is encoded/decoded correctly.

Amino supports encoding and decoding in both a binary and JSON format.
See the [codec API docs](https://godoc.org/github.com/tendermint/go-amino#Codec) for more details.
Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ 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 []byte `json:"signature"` // Depends on the PubKey type and the whole Tx
Signature []byte `json:"signature"` // Depends on the PubKey type and the whole Tx
PubKey crypto.PubKey `json:"pub_key"` // Is present iff Sequence == 0
}
Expand Down
10 changes: 1 addition & 9 deletions x/slashing/genesis.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
package slashing

import (
"fmt"

"github.com/cosmos/cosmos-sdk/x/stake/types"
"github.com/tendermint/tendermint/crypto/tmhash"
)

// InitGenesis initializes the keeper's address to pubkey map
func InitGenesis(keeper Keeper, data types.GenesisState) {
for _, validator := range data.Validators {
pubkey := validator.GetPubKey()
addr := new([tmhash.Size]byte)
copy(addr[:], pubkey.Bytes())
keeper.addressToPubkey[*addr] = pubkey
fmt.Println("WAS THIS CALLED?!?!??!")
fmt.Println(keeper.addressToPubkey)
keeper.addPubkey(validator.GetPubKey(), false)
}
return
}
33 changes: 22 additions & 11 deletions x/slashing/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ func (k Keeper) handleDoubleSign(ctx sdk.Context, pubkey crypto.PubKey, infracti

// handle a validator signature, must be called once per validator per block
// nolint gocyclo
// TODO: Change this to take in an address
func (k Keeper) handleValidatorSignature(ctx sdk.Context, pubkey crypto.PubKey, power int64, signed bool) {
func (k Keeper) handleValidatorSignature(ctx sdk.Context, addr crypto.Address, power int64, signed bool) {
logger := ctx.Logger().With("module", "x/slashing")
height := ctx.BlockHeight()
address := sdk.ValAddress(pubkey.Address())
address := sdk.ValAddress(addr)
pubkey := k.getPubkey(addr)
// Local index, so counts blocks validator *should* have signed
// Will use the 0-value default signing info if not present, except for start height
signInfo, found := k.getValidatorSigningInfo(ctx, address)
Expand Down Expand Up @@ -104,7 +104,7 @@ func (k Keeper) handleValidatorSignature(ctx sdk.Context, pubkey crypto.PubKey,
}

if !signed {
logger.Info(fmt.Sprintf("Absent validator %s at height %d, %d signed, threshold %d", pubkey.Address(), height, signInfo.SignedBlocksCounter, k.MinSignedPerWindow(ctx)))
logger.Info(fmt.Sprintf("Absent validator %s at height %d, %d signed, threshold %d", addr, height, signInfo.SignedBlocksCounter, k.MinSignedPerWindow(ctx)))
}
minHeight := signInfo.StartHeight + k.SignedBlocksWindow(ctx)
if height > minHeight && signInfo.SignedBlocksCounter < k.MinSignedPerWindow(ctx) {
Expand All @@ -127,19 +127,30 @@ func (k Keeper) handleValidatorSignature(ctx sdk.Context, pubkey crypto.PubKey,
k.setValidatorSigningInfo(ctx, address, signInfo)
}

// AddValidators adds the validators to the keepers validator addr to pubkey mapping.
func (k Keeper) AddValidators(vals []abci.Validator) {
for i := 0; i < len(vals); i++ {
val := vals[i]
pubkey, err := tmtypes.PB2TM.PubKey(val.PubKey)
if err != nil {
continue
}
addr := new([tmhash.Size]byte)
copy(addr[:], pubkey.Address())
if val.GetPower() != 0 {
k.addressToPubkey[*addr] = pubkey
} else {
delete(k.addressToPubkey, *addr)
}
k.addPubkey(pubkey, val.Power == 0)
}
}

func (k Keeper) addPubkey(pubkey crypto.PubKey, del bool) {
addr := new([tmhash.Size]byte)
copy(addr[:], pubkey.Address())
if del {
delete(k.addressToPubkey, *addr)
} else {
k.addressToPubkey[*addr] = pubkey
}
}

func (k Keeper) getPubkey(address crypto.Address) crypto.PubKey {
addr := new([tmhash.Size]byte)
copy(addr[:], address)
return k.addressToPubkey[*addr]
}
7 changes: 1 addition & 6 deletions x/slashing/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, sk Keeper) (tags
binary.LittleEndian.PutUint64(heightBytes, uint64(req.Header.Height))
tags = sdk.NewTags("height", heightBytes)

fmt.Println(ctx.SigningValidators())

// Iterate over all the validators which *should* have signed this block
// Store whether or not they have actually signed it and slash/unbond any
// which have missed too many blocks in a row (downtime slashing)
for _, signingValidator := range req.LastCommitInfo.GetValidators() {
present := signingValidator.SignedLastBlock
addr := new([20]byte)
copy(addr[:], signingValidator.Validator.Address)
pubkey := sk.addressToPubkey[*addr]
sk.handleValidatorSignature(ctx, pubkey, signingValidator.Validator.Power, present)
sk.handleValidatorSignature(ctx, signingValidator.Validator.Address, signingValidator.Validator.Power, present)
}

// Iterate through any newly discovered evidence of infraction
Expand Down

0 comments on commit 2f85fff

Please sign in to comment.