diff --git a/permission/types/permissions.go b/permission/types/permissions.go index cee1a43b..4eeec55c 100644 --- a/permission/types/permissions.go +++ b/permission/types/permissions.go @@ -10,8 +10,6 @@ import ( var ( GlobalPermissionsAddress = Zero256[:20] GlobalPermissionsAddress256 = Zero256 - DougAddress = append([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, []byte("THISISDOUG")...) - DougAddress256 = LeftPadWord256(DougAddress) ) // A particular permission @@ -154,6 +152,9 @@ func (aP *AccountPermissions) RmRole(role string) bool { } //-------------------------------------------------------------------------------- +// string utilities + +// CONTRACT: PermFlagToString functions assume the permFlag is valid, else return "#-UNKNOWN-#" func PermFlagToString(pf PermFlag) string { if pf < FirstSNativePermFlag { return BasePermFlagToString(pf) @@ -178,6 +179,8 @@ func BasePermFlagToString(pf PermFlag) (perm string) { perm = "bond" case Name: perm = "name" + default: + perm = "#-UNKNOWN-#" } return } diff --git a/permission/types/snatives.go b/permission/types/snatives.go index 047edde0..95c5336b 100644 --- a/permission/types/snatives.go +++ b/permission/types/snatives.go @@ -14,18 +14,17 @@ const ( FirstSNativePermFlag PermFlag = 1 << 32 ) -// we need to reset iota with no const block +// we need to reset iota with new const block const ( // each snative has an associated permission flag HasBase PermFlag = FirstSNativePermFlag << iota SetBase UnsetBase SetGlobal - ClearBase HasRole AddRole RmRole - NumSNativePermissions uint = 8 // NOTE adjust this too + NumSNativePermissions uint = 7 // NOTE adjust this too TopSNativePermFlag PermFlag = FirstSNativePermFlag << (NumSNativePermissions - 1) AllSNativePermFlags PermFlag = (TopSNativePermFlag | (TopSNativePermFlag - 1)) &^ (FirstSNativePermFlag - 1) @@ -34,9 +33,10 @@ const ( //--------------------------------------------------------------------------------------------------- // snative tx interface and argument encoding -// SNatives are represented as an interface in the SNative tx, -// so binary does the work of handling args and determining which snative we're calling -// NOTE: we're definining an implicit map from registration bytes to perm flags +// SNativesArgs are a registered interface in the SNativeTx, +// so binary handles the arguments and each snative gets a type-byte +// PermFlag() maps the type-byte to the permission +// The account sending the SNativeTx must have this PermFlag set type SNativeArgs interface { PermFlag() PermFlag } @@ -46,10 +46,9 @@ const ( SNativeArgsTypeSetBase = byte(0x02) SNativeArgsTypeUnsetBase = byte(0x03) SNativeArgsTypeSetGlobal = byte(0x04) - SNativeArgsTypeClearBase = byte(0x05) - SNativeArgsTypeHasRole = byte(0x06) - SNativeArgsTypeAddRole = byte(0x07) - SNativeArgsTypeRmRole = byte(0x08) + SNativeArgsTypeHasRole = byte(0x05) + SNativeArgsTypeAddRole = byte(0x06) + SNativeArgsTypeRmRole = byte(0x07) ) // for binary.readReflect @@ -59,7 +58,6 @@ var _ = binary.RegisterInterface( binary.ConcreteType{&SetBaseArgs{}, SNativeArgsTypeSetBase}, binary.ConcreteType{&UnsetBaseArgs{}, SNativeArgsTypeUnsetBase}, binary.ConcreteType{&SetGlobalArgs{}, SNativeArgsTypeSetGlobal}, - binary.ConcreteType{&ClearBaseArgs{}, SNativeArgsTypeClearBase}, binary.ConcreteType{&HasRoleArgs{}, SNativeArgsTypeHasRole}, binary.ConcreteType{&AddRoleArgs{}, SNativeArgsTypeAddRole}, binary.ConcreteType{&RmRoleArgs{}, SNativeArgsTypeRmRole}, @@ -102,14 +100,6 @@ func (*SetGlobalArgs) PermFlag() PermFlag { return SetGlobal } -type ClearBaseArgs struct { - // -} - -func (*ClearBaseArgs) PermFlag() PermFlag { - return ClearBase -} - type HasRoleArgs struct { Address []byte Role string @@ -150,8 +140,6 @@ func SNativePermFlagToString(pF PermFlag) (perm string) { perm = "UnsetBase" case SetGlobal: perm = "SetGlobal" - case ClearBase: - perm = "ClearBase" case HasRole: perm = "HasRole" case AddRole: @@ -174,8 +162,6 @@ func SNativeStringToPermFlag(perm string) (pF PermFlag, err error) { pF = UnsetBase case "SetGlobal": pF = SetGlobal - case "ClearBase": - pF = ClearBase case "HasRole": pF = HasRole case "AddRole": diff --git a/state/execution.go b/state/execution.go index 586c20ee..ddad8cfb 100644 --- a/state/execution.go +++ b/state/execution.go @@ -807,7 +807,7 @@ func ExecTx(blockCache *BlockCache, tx types.Tx, runCall bool, evc events.Fireab permFlag := tx.SNative.PermFlag() // check permission if !hasSNativePermission(blockCache, inAcc, permFlag) { - return fmt.Errorf("Account %X does not have permission to call snative %s (%b)", tx.Input.Address, tx.SNative, permFlag) + return fmt.Errorf("Account %X does not have permission to call snative %s (%b)", tx.Input.Address, ptypes.SNativePermFlagToString(permFlag), permFlag) } // pubKey should be present in either "inAcc" or "tx.Input" @@ -843,11 +843,9 @@ func ExecTx(blockCache *BlockCache, tx types.Tx, runCall bool, evc events.Fireab err = permAcc.Permissions.Base.Unset(args.Permission) case *ptypes.SetGlobalArgs: if permAcc = blockCache.GetAccount(ptypes.GlobalPermissionsAddress); permAcc == nil { - // PanicSanity("can't find global permissions account") + PanicSanity("can't find global permissions account") } err = permAcc.Permissions.Base.Set(args.Permission, args.Value) - case *ptypes.ClearBaseArgs: - // case *ptypes.HasRoleArgs: return fmt.Errorf("HasRole is for contracts, not humans. Just look at the blockchain") case *ptypes.AddRoleArgs: @@ -865,7 +863,7 @@ func ExecTx(blockCache *BlockCache, tx types.Tx, runCall bool, evc events.Fireab return fmt.Errorf("Role (%s) does not exist for account %X", args.Role, args.Address) } default: - // PanicSanity("invalid snative") + PanicSanity(Fmt("invalid snative: %s", ptypes.SNativePermFlagToString(permFlag))) } // TODO: maybe we want to take funds on error and allow txs in that don't do anythingi? diff --git a/state/permissions_test.go b/state/permissions_test.go index 5a4a6d53..de0cf207 100644 --- a/state/permissions_test.go +++ b/state/permissions_test.go @@ -836,6 +836,9 @@ func TestCreateAccountPermission(t *testing.T) { } +// holla at my boy +var DougAddress = append([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, []byte("THISISDOUG")...) + func TestSNativeCALL(t *testing.T) { stateDB := dbm.GetDB("state") genDoc := newBaseGenDoc(PermsAllFalse, PermsAllFalse) @@ -851,7 +854,7 @@ func TestSNativeCALL(t *testing.T) { // make the main contract once doug := &acm.Account{ - Address: ptypes.DougAddress, + Address: DougAddress, Balance: 0, Code: nil, Sequence: 0, @@ -925,9 +928,6 @@ func TestSNativeCALL(t *testing.T) { return nil }) - // ClearBase - // TODO - fmt.Println("\n#### HasRole") // HasRole snativeAddress, data = snativeRoleTestInputCALL("HasRole", user[3], "bumble") @@ -1022,9 +1022,6 @@ func TestSNativeTx(t *testing.T) { t.Fatal("expected permission to be set true") } - // ClearBase - // TODO - fmt.Println("\n#### AddRole") // AddRole snativeArgs = snativeRoleTestInputTx("AddRole", user[3], "chuck") @@ -1184,7 +1181,6 @@ func snativePermTestInputCALL(name string, user *acm.PrivAccount, perm ptypes.Pe case "SetGlobal": data = Uint64ToWord256(uint64(perm)).Bytes() data = append(data, boolToWord256(val).Bytes()...) - case "ClearBase": } return } @@ -1199,7 +1195,6 @@ func snativePermTestInputTx(name string, user *acm.PrivAccount, perm ptypes.Perm snativeArgs = &ptypes.SetBaseArgs{user.Address, perm, val} case "SetGlobal": snativeArgs = &ptypes.SetGlobalArgs{perm, val} - case "ClearBase": } return } diff --git a/vm/snative.go b/vm/snative.go index 7df081fb..bd72d9dd 100644 --- a/vm/snative.go +++ b/vm/snative.go @@ -15,9 +15,9 @@ type snativeInfo struct { } // Takes an appState so it can lookup/update accounts, -// an account to check for permission to access the snative contract -// and some input bytes (presumably 32byte words) -type SNativeContract func(appState AppState, acc *Account, input []byte) (output []byte, err error) +// and an input byte array containing at least one Word256 +// TODO: ABI +type SNativeContract func(appState AppState, input []byte) (output []byte, err error) //------------------------------------------------------------------------------------------------ // Registered SNative contracts @@ -28,15 +28,13 @@ func getSNativeInfo(permFlag ptypes.PermFlag) *snativeInfo { var errS string switch permFlag { case ptypes.HasBase: - si.NArgs, errS, si.Executable = 2, "hasBasePerm() takes two arguments (address, permission number)", hasBasePerm + si.NArgs, errS, si.Executable = 2, "hasBase() takes two arguments (address, permFlag)", hasBasePerm case ptypes.SetBase: - si.NArgs, errS, si.Executable = 3, "setBasePerm() takes three arguments (address, permission number, permission value)", setBasePerm + si.NArgs, errS, si.Executable = 3, "setBase() takes three arguments (address, permFlag, permission value)", setBasePerm case ptypes.UnsetBase: - si.NArgs, errS, si.Executable = 2, "unsetBasePerm() takes two arguments (address, permission number)", unsetBasePerm + si.NArgs, errS, si.Executable = 2, "unsetBase() takes two arguments (address, permFlag)", unsetBasePerm case ptypes.SetGlobal: - si.NArgs, errS, si.Executable = 2, "setGlobalPerm() takes two arguments (permission number, permission value)", setGlobalPerm - case ptypes.ClearBase: - // + si.NArgs, errS, si.Executable = 2, "setGlobal() takes two arguments (permFlag, permission value)", setGlobalPerm case ptypes.HasRole: si.NArgs, errS, si.Executable = 2, "hasRole() takes two arguments (address, role)", hasRole case ptypes.AddRole: @@ -56,7 +54,7 @@ func getSNativeInfo(permFlag ptypes.PermFlag) *snativeInfo { // TODO: catch errors, log em, return 0s to the vm (should some errors cause exceptions though?) -func hasBasePerm(appState AppState, acc *Account, args []byte) (output []byte, err error) { +func hasBasePerm(appState AppState, args []byte) (output []byte, err error) { addr, permNum := returnTwoArgs(args) vmAcc := appState.GetAccount(addr) if vmAcc == nil { @@ -76,7 +74,7 @@ func hasBasePerm(appState AppState, acc *Account, args []byte) (output []byte, e return LeftPadWord256([]byte{permInt}).Bytes(), nil } -func setBasePerm(appState AppState, acc *Account, args []byte) (output []byte, err error) { +func setBasePerm(appState AppState, args []byte) (output []byte, err error) { addr, permNum, perm := returnThreeArgs(args) vmAcc := appState.GetAccount(addr) if vmAcc == nil { @@ -95,7 +93,7 @@ func setBasePerm(appState AppState, acc *Account, args []byte) (output []byte, e return perm.Bytes(), nil } -func unsetBasePerm(appState AppState, acc *Account, args []byte) (output []byte, err error) { +func unsetBasePerm(appState AppState, args []byte) (output []byte, err error) { addr, permNum := returnTwoArgs(args) vmAcc := appState.GetAccount(addr) if vmAcc == nil { @@ -113,7 +111,7 @@ func unsetBasePerm(appState AppState, acc *Account, args []byte) (output []byte, return permNum.Bytes(), nil } -func setGlobalPerm(appState AppState, acc *Account, args []byte) (output []byte, err error) { +func setGlobalPerm(appState AppState, args []byte) (output []byte, err error) { permNum, perm := returnTwoArgs(args) vmAcc := appState.GetAccount(ptypes.GlobalPermissionsAddress256) if vmAcc == nil { @@ -132,12 +130,7 @@ func setGlobalPerm(appState AppState, acc *Account, args []byte) (output []byte, return perm.Bytes(), nil } -// TODO: needs access to an iterator ... -func clearPerm(appState AppState, acc *Account, args []byte) (output []byte, err error) { - return nil, nil -} - -func hasRole(appState AppState, acc *Account, args []byte) (output []byte, err error) { +func hasRole(appState AppState, args []byte) (output []byte, err error) { addr, role := returnTwoArgs(args) vmAcc := appState.GetAccount(addr) if vmAcc == nil { @@ -154,7 +147,7 @@ func hasRole(appState AppState, acc *Account, args []byte) (output []byte, err e return LeftPadWord256([]byte{permInt}).Bytes(), nil } -func addRole(appState AppState, acc *Account, args []byte) (output []byte, err error) { +func addRole(appState AppState, args []byte) (output []byte, err error) { addr, role := returnTwoArgs(args) vmAcc := appState.GetAccount(addr) if vmAcc == nil { @@ -172,7 +165,7 @@ func addRole(appState AppState, acc *Account, args []byte) (output []byte, err e return LeftPadWord256([]byte{permInt}).Bytes(), nil } -func rmRole(appState AppState, acc *Account, args []byte) (output []byte, err error) { +func rmRole(appState AppState, args []byte) (output []byte, err error) { addr, role := returnTwoArgs(args) vmAcc := appState.GetAccount(addr) if vmAcc == nil { diff --git a/vm/vm.go b/vm/vm.go index d837abbd..a3934a9d 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -127,7 +127,7 @@ func (vm *VM) callSNative(addr Word256, permFlag ptypes.PermFlag, caller *Accoun return } // SNATIVE ACCESS - ret, err = snInfo.Executable(vm.appState, caller, input) + ret, err = snInfo.Executable(vm.appState, input) // END SNATIVE ACCESS if err != nil { *exception = err.Error()