From 8d6f80592b3a929b9e6fa29086b084e2a2aae75a Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Fri, 26 Jun 2015 01:36:31 +0000 Subject: [PATCH] some panics, dont panic on invalid opcode --- state/execution.go | 18 ++++++++---------- state/genesis.go | 2 +- vm/native.go | 10 ++-------- vm/vm.go | 7 ++++++- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/state/execution.go b/state/execution.go index 65d161404..84e589ebe 100644 --- a/state/execution.go +++ b/state/execution.go @@ -293,15 +293,6 @@ func adjustByOutputs(accounts map[string]*account.Account, outs []*types.TxOutpu // If the tx is invalid, an error will be returned. // Unlike ExecBlock(), state will not be altered. func ExecTx(blockCache *BlockCache, tx_ types.Tx, runCall bool, evc events.Fireable) error { - - defer func() { - if r := recover(); r != nil { - err := errors.New(Fmt("Recovered from panic in ExecTx", "err", r, "tx", tx_)) - log.Error(err.Error()) - // TODO return error - } - }() - // TODO: do something with fees fees := int64(0) _s := blockCache.State() // hack to access validators and block height @@ -822,6 +813,10 @@ func ExecTx(blockCache *BlockCache, tx_ types.Tx, runCall bool, evc events.Firea // Get permission on an account or fall back to global value func HasPermission(state AccountGetter, acc *account.Account, perm ptypes.PermFlag) bool { + if perm > ptypes.AllBasePermissions { + panic("Checking an unknown permission in state should never happen") + } + if acc == nil { // TODO // this needs to fall back to global or do some other specific things @@ -830,7 +825,10 @@ func HasPermission(state AccountGetter, acc *account.Account, perm ptypes.PermFl v, err := acc.Permissions.Base.Get(perm) if _, ok := err.(ptypes.ErrValueNotSet); ok { - return HasPermission(state, state.GetAccount(ptypes.GlobalPermissionsAddress), perm) + if state == nil { + panic("All known global permissions should be set!") + } + return HasPermission(nil, state.GetAccount(ptypes.GlobalPermissionsAddress), perm) } return v } diff --git a/state/genesis.go b/state/genesis.go index 6fcc40e14..fdee9e3fd 100644 --- a/state/genesis.go +++ b/state/genesis.go @@ -108,7 +108,7 @@ func MakeGenesisState(db dbm.DB, genDoc *GenesisDoc) *State { if genDoc.Params != nil && genDoc.Params.GlobalPermissions != nil { globalPerms = genDoc.Params.GlobalPermissions // XXX: make sure the set bits are all true - // Without it the HasPermission() functions will recurse till death + // Without it the HasPermission() functions will fail globalPerms.Base.SetBit = ptypes.AllSet } diff --git a/vm/native.go b/vm/native.go index 003f557ff..27f2e8f4b 100644 --- a/vm/native.go +++ b/vm/native.go @@ -53,10 +53,7 @@ func sha256Func(input []byte, gas *int64) (output []byte, err error) { // Hash hasher := sha256.New() // CONTRACT: this does not err - _, err = hasher.Write(input) - if err != nil { - panic(err) - } + hasher.Write(input) return hasher.Sum(nil), nil } @@ -71,10 +68,7 @@ func ripemd160Func(input []byte, gas *int64) (output []byte, err error) { // Hash hasher := ripemd160.New() // CONTRACT: this does not err - _, err = hasher.Write(input) - if err != nil { - panic(err) - } + hasher.Write(input) return LeftPadBytes(hasher.Sum(nil), 32), nil } diff --git a/vm/vm.go b/vm/vm.go index f8c6e2bf2..8087443c6 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -90,11 +90,16 @@ func (vm *VM) EnablePermissions() { vm.perms = true } +// XXX: it is the duty of the contract writer to call known permissions +// we do not convey if a permission is not set +// (unlike in state/execution, where we guarantee HasPermission is called +// on known permissions and panics else) func HasPermission(appState AppState, acc *Account, perm ptypes.PermFlag) bool { v, err := acc.Permissions.Base.Get(perm) if _, ok := err.(ptypes.ErrValueNotSet); ok { if appState == nil { - panic(fmt.Sprintf("Global permission value not set for %b", perm)) + fmt.Printf("\n\n***** Unknown permission %b! ********\n\n", perm) + return false } return HasPermission(nil, appState.GetAccount(ptypes.GlobalPermissionsAddress256), perm) }