panic wrapper functions

This commit is contained in:
Ethan Buchman
2015-07-21 10:46:05 -04:00
parent 5983088a32
commit 8e50bf15de
45 changed files with 229 additions and 275 deletions
+7 -19
View File
@@ -64,21 +64,17 @@ func (cache *BlockCache) GetAccount(addr []byte) *acm.Account {
func (cache *BlockCache) UpdateAccount(acc *acm.Account) {
addr := acc.Address
_, storage, removed, _ := cache.accounts[string(addr)].unpack()
// SANITY CHECK
if removed {
panic("UpdateAccount on a removed account")
PanicSanity("UpdateAccount on a removed account")
}
// SANITY CHECK END
cache.accounts[string(addr)] = accountInfo{acc, storage, false, true}
}
func (cache *BlockCache) RemoveAccount(addr []byte) {
// SANITY CHECK
_, _, removed, _ := cache.accounts[string(addr)].unpack()
if removed {
panic("RemoveAccount on a removed account")
PanicSanity("RemoveAccount on a removed account")
}
// SANITY CHECK END
cache.accounts[string(addr)] = accountInfo{nil, nil, true, false}
}
@@ -95,11 +91,9 @@ func (cache *BlockCache) GetStorage(addr Word256, key Word256) (value Word256) {
// Get or load storage
acc, storage, removed, dirty := cache.accounts[string(addr.Postfix(20))].unpack()
// SANITY CHECK
if removed {
panic("GetStorage() on removed account")
PanicSanity("GetStorage() on removed account")
}
// SANITY CHECK END
if acc != nil && storage == nil {
storage = makeStorage(cache.db, acc.StorageRoot)
cache.accounts[string(addr.Postfix(20))] = accountInfo{acc, storage, false, dirty}
@@ -119,12 +113,10 @@ func (cache *BlockCache) GetStorage(addr Word256, key Word256) (value Word256) {
// NOTE: Set value to zero to removed from the trie.
func (cache *BlockCache) SetStorage(addr Word256, key Word256, value Word256) {
// SANITY CHECK
_, _, removed, _ := cache.accounts[string(addr.Postfix(20))].unpack()
if removed {
panic("SetStorage() on a removed account")
PanicSanity("SetStorage() on a removed account")
}
// SANITY CHECK END
cache.storages[Tuple256{addr, key}] = storageInfo{value, true}
}
@@ -151,12 +143,10 @@ func (cache *BlockCache) UpdateNameRegEntry(entry *types.NameRegEntry) {
}
func (cache *BlockCache) RemoveNameRegEntry(name string) {
// SANITY CHECK
_, removed, _ := cache.names[name].unpack()
if removed {
panic("RemoveNameRegEntry on a removed entry")
PanicSanity("RemoveNameRegEntry on a removed entry")
}
// SANITY CHECK END
cache.names[name] = nameInfo{nil, true, false}
}
@@ -222,8 +212,7 @@ func (cache *BlockCache) Sync() {
if removed {
removed := cache.backend.RemoveAccount(acc.Address)
if !removed {
// SOMETHING HORRIBLE HAS GONE WRONG
panic(Fmt("Could not remove account to be removed: %X", acc.Address))
PanicCrisis(Fmt("Could not remove account to be removed: %X", acc.Address))
}
} else {
if acc == nil {
@@ -256,8 +245,7 @@ func (cache *BlockCache) Sync() {
if removed {
removed := cache.backend.RemoveNameRegEntry(nameStr)
if !removed {
// SOMETHING HORRIBLE HAS GONE WRONG
panic(Fmt("Could not remove namereg entry to be removed: %s", nameStr))
PanicCrisis(Fmt("Could not remove namereg entry to be removed: %s", nameStr))
}
} else {
if entry == nil {
+14 -22
View File
@@ -57,29 +57,28 @@ func execBlock(s *State, block *types.Block, blockPartsHeader types.PartSetHeade
}
// Update Validator.LastCommitHeight as necessary.
// If we panic in here, something has gone horribly wrong
for i, precommit := range block.LastValidation.Precommits {
if precommit == nil {
continue
}
_, val := s.LastBondedValidators.GetByIndex(i)
if val == nil {
panic(Fmt("Failed to fetch validator at index %v", i))
PanicCrisis(Fmt("Failed to fetch validator at index %v", i))
}
if _, val_ := s.BondedValidators.GetByAddress(val.Address); val_ != nil {
val_.LastCommitHeight = block.Height - 1
updated := s.BondedValidators.Update(val_)
if !updated {
panic("Failed to update bonded validator LastCommitHeight")
PanicCrisis("Failed to update bonded validator LastCommitHeight")
}
} else if _, val_ := s.UnbondingValidators.GetByAddress(val.Address); val_ != nil {
val_.LastCommitHeight = block.Height - 1
updated := s.UnbondingValidators.Update(val_)
if !updated {
panic("Failed to update unbonding validator LastCommitHeight")
PanicCrisis("Failed to update unbonding validator LastCommitHeight")
}
} else {
panic("Could not find validator")
PanicCrisis("Could not find validator")
}
}
@@ -213,11 +212,9 @@ func checkInputPubKey(acc *acm.Account, in *types.TxInput) error {
func validateInputs(accounts map[string]*acm.Account, signBytes []byte, ins []*types.TxInput) (total int64, err error) {
for _, in := range ins {
acc := accounts[string(in.Address)]
// SANITY CHECK
if acc == nil {
panic("validateInputs() expects account in accounts")
PanicSanity("validateInputs() expects account in accounts")
}
// SANITY CHECK END
err = validateInput(acc, signBytes, in)
if err != nil {
return
@@ -266,14 +263,12 @@ func validateOutputs(outs []*types.TxOutput) (total int64, err error) {
func adjustByInputs(accounts map[string]*acm.Account, ins []*types.TxInput) {
for _, in := range ins {
acc := accounts[string(in.Address)]
// SANITY CHECK
if acc == nil {
panic("adjustByInputs() expects account in accounts")
PanicSanity("adjustByInputs() expects account in accounts")
}
if acc.Balance < in.Amount {
panic("adjustByInputs() expects sufficient funds")
PanicSanity("adjustByInputs() expects sufficient funds")
}
// SANITY CHECK END
acc.Balance -= in.Amount
acc.Sequence += 1
}
@@ -282,11 +277,9 @@ func adjustByInputs(accounts map[string]*acm.Account, ins []*types.TxInput) {
func adjustByOutputs(accounts map[string]*acm.Account, outs []*types.TxOutput) {
for _, out := range outs {
acc := accounts[string(out.Address)]
// SANITY CHECK
if acc == nil {
panic("adjustByOutputs() expects account in accounts")
PanicSanity("adjustByOutputs() expects account in accounts")
}
// SANITY CHECK END
acc.Balance += out.Amount
}
}
@@ -701,8 +694,7 @@ func ExecTx(blockCache *BlockCache, tx types.Tx, runCall bool, evc events.Fireab
Accum: 0,
})
if !added {
// SOMETHING HAS GONE HORRIBLY WRONG
panic("Failed to add validator")
PanicCrisis("Failed to add validator")
}
if evc != nil {
evc.FireEvent(types.EventStringBond(), tx)
@@ -802,9 +794,9 @@ func ExecTx(blockCache *BlockCache, tx types.Tx, runCall bool, evc events.Fireab
return nil
default:
// SANITY CHECK (binary decoding should catch bad tx types
// before they get here
panic("Unknown Tx type")
// binary decoding should not let this happen
PanicSanity("Unknown Tx type")
return nil
}
}
@@ -813,7 +805,7 @@ func ExecTx(blockCache *BlockCache, tx types.Tx, runCall bool, evc events.Fireab
// Get permission on an account or fall back to global value
func HasPermission(state AccountGetter, acc *acm.Account, perm ptypes.PermFlag) bool {
if perm > ptypes.AllBasePermFlags {
panic("Checking an unknown permission in state should never happen")
PanicSanity("Checking an unknown permission in state should never happen")
}
if acc == nil {
@@ -826,7 +818,7 @@ func HasPermission(state AccountGetter, acc *acm.Account, perm ptypes.PermFlag)
if _, ok := err.(ptypes.ErrValueNotSet); ok {
log.Info("Account does not have permission", "account", acc, "accPermissions", acc.Permissions, "perm", perm)
if state == nil {
panic("All known global permissions should be set!")
PanicSanity("All known global permissions should be set!")
}
log.Info("Querying GlobalPermissionsAddress")
return HasPermission(nil, state.GetAccount(ptypes.GlobalPermissionsAddress), perm)
+4 -6
View File
@@ -30,9 +30,8 @@ func voteToStep(vote *types.Vote) int8 {
case types.VoteTypePrecommit:
return stepPrecommit
default:
// SANITY CHECK (binary decoding should catch bad vote types
// before they get here (right?!)
panic("Unknown vote type")
PanicSanity("Unknown vote type")
return 0
}
}
@@ -95,14 +94,13 @@ func (privVal *PrivValidator) Save() {
func (privVal *PrivValidator) save() {
if privVal.filePath == "" {
// SANITY CHECK
panic("Cannot save PrivValidator: filePath not set")
PanicSanity("Cannot save PrivValidator: filePath not set")
}
jsonBytes := binary.JSONBytes(privVal)
err := WriteFileAtomic(privVal.filePath, jsonBytes)
if err != nil {
// `@; BOOM!!!
panic(err)
PanicCrisis(err)
}
}
+10 -23
View File
@@ -92,8 +92,7 @@ func (s *State) Save() {
binary.WriteByteSlice(s.validatorInfos.Hash(), buf, n, err)
binary.WriteByteSlice(s.nameReg.Hash(), buf, n, err)
if *err != nil {
// SOMETHING HAS GONE HORRIBLY WRONG
panic(*err)
PanicCrisis(*err)
}
s.DB.Set(stateKey, buf.Bytes())
}
@@ -216,14 +215,12 @@ func (s *State) unbondValidator(val *Validator) {
// Move validator to UnbondingValidators
val, removed := s.BondedValidators.Remove(val.Address)
if !removed {
// SOMETHING HAS GONE HORRIBLY WRONG
panic("Couldn't remove validator for unbonding")
PanicCrisis("Couldn't remove validator for unbonding")
}
val.UnbondHeight = s.LastBlockHeight + 1
added := s.UnbondingValidators.Add(val)
if !added {
// SOMETHING HAS GONE HORRIBLY WRONG
panic("Couldn't add validator for unbonding")
PanicCrisis("Couldn't add validator for unbonding")
}
}
@@ -231,35 +228,29 @@ func (s *State) rebondValidator(val *Validator) {
// Move validator to BondingValidators
val, removed := s.UnbondingValidators.Remove(val.Address)
if !removed {
// SOMETHING HAS GONE HORRIBLY WRONG
panic("Couldn't remove validator for rebonding")
PanicCrisis("Couldn't remove validator for rebonding")
}
val.BondHeight = s.LastBlockHeight + 1
added := s.BondedValidators.Add(val)
if !added {
// SOMETHING HAS GONE HORRIBLY WRONG
panic("Couldn't add validator for rebonding")
PanicCrisis("Couldn't add validator for rebonding")
}
}
func (s *State) releaseValidator(val *Validator) {
// Update validatorInfo
valInfo := s.GetValidatorInfo(val.Address)
// SANITY CHECK
if valInfo == nil {
panic("Couldn't find validatorInfo for release")
PanicSanity("Couldn't find validatorInfo for release")
}
// SANITY CHECK END
valInfo.ReleasedHeight = s.LastBlockHeight + 1
s.SetValidatorInfo(valInfo)
// Send coins back to UnbondTo outputs
accounts, err := getOrMakeOutputs(s, nil, valInfo.UnbondTo)
// SANITY CHECK
if err != nil {
panic("Couldn't get or make unbondTo accounts")
PanicSanity("Couldn't get or make unbondTo accounts")
}
// SANITY CHECK END
adjustByOutputs(accounts, valInfo.UnbondTo)
for _, acc := range accounts {
s.UpdateAccount(acc)
@@ -268,19 +259,16 @@ func (s *State) releaseValidator(val *Validator) {
// Remove validator from UnbondingValidators
_, removed := s.UnbondingValidators.Remove(val.Address)
if !removed {
// SOMETHING HAS GONE HORRIBLY WRONG
panic("Couldn't remove validator for release")
PanicCrisis("Couldn't remove validator for release")
}
}
func (s *State) destroyValidator(val *Validator) {
// Update validatorInfo
valInfo := s.GetValidatorInfo(val.Address)
// SANITY CHECK
if valInfo == nil {
panic("Couldn't find validatorInfo for release")
PanicSanity("Couldn't find validatorInfo for release")
}
// SANITY CHECK END
valInfo.DestroyedHeight = s.LastBlockHeight + 1
valInfo.DestroyedAmount = val.VotingPower
s.SetValidatorInfo(valInfo)
@@ -290,8 +278,7 @@ func (s *State) destroyValidator(val *Validator) {
if !removed {
_, removed := s.UnbondingValidators.Remove(val.Address)
if !removed {
// SOMETHING HAS GONE HORRIBLY WRONG
panic("Couldn't remove validator for destruction")
PanicCrisis("Couldn't remove validator for destruction")
}
}
+1 -1
View File
@@ -18,7 +18,7 @@ import (
func Tempfile(prefix string) (*os.File, string) {
file, err := ioutil.TempFile("", prefix)
if err != nil {
panic(err)
PanicCrisis(err)
}
return file, file.Name()
}
+6 -11
View File
@@ -42,23 +42,19 @@ func (cache *TxCache) GetAccount(addr Word256) *vm.Account {
func (cache *TxCache) UpdateAccount(acc *vm.Account) {
addr := acc.Address
// SANITY CHECK
_, removed := vmUnpack(cache.accounts[addr])
if removed {
panic("UpdateAccount on a removed account")
PanicSanity("UpdateAccount on a removed account")
}
// SANITY CHECK END
cache.accounts[addr] = vmAccountInfo{acc, false}
}
func (cache *TxCache) RemoveAccount(acc *vm.Account) {
addr := acc.Address
// SANITY CHECK
_, removed := vmUnpack(cache.accounts[addr])
if removed {
panic("RemoveAccount on a removed account")
PanicSanity("RemoveAccount on a removed account")
}
// SANITY CHECK END
cache.accounts[addr] = vmAccountInfo{acc, true}
}
@@ -86,8 +82,9 @@ func (cache *TxCache) CreateAccount(creator *vm.Account) *vm.Account {
cache.accounts[addr] = vmAccountInfo{account, false}
return account
} else {
// NONCE HANDLING SANITY CHECK OR SHA3 IS BROKEN
panic(Fmt("Could not create account, address already exists: %X", addr))
// either we've messed up nonce handling, or sha3 is broken
PanicSanity(Fmt("Could not create account, address already exists: %X", addr))
return nil
}
}
@@ -108,12 +105,10 @@ func (cache *TxCache) GetStorage(addr Word256, key Word256) Word256 {
// NOTE: Set value to zero to removed from the trie.
func (cache *TxCache) SetStorage(addr Word256, key Word256, value Word256) {
// SANITY CHECK
_, removed := vmUnpack(cache.accounts[addr])
if removed {
panic("SetStorage() on a removed account")
PanicSanity("SetStorage() on a removed account")
}
// SANITY CHECK END
cache.storages[Tuple256{addr, key}] = value
}
+5 -3
View File
@@ -7,6 +7,7 @@ import (
acm "github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/types"
)
@@ -77,8 +78,8 @@ func (v *Validator) CompareAccum(other *Validator) *Validator {
} else if bytes.Compare(v.Address, other.Address) > 0 {
return other
} else {
// SANITY CHECK
panic("Cannot compare identical validators")
PanicSanity("Cannot compare identical validators")
return nil
}
}
}
@@ -116,5 +117,6 @@ func (vc validatorCodec) Decode(r io.Reader, n *int64, err *error) interface{} {
}
func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int {
panic("ValidatorCodec.Compare not implemented")
PanicSanity("ValidatorCodec.Compare not implemented")
return 0
}