diff --git a/permission/types/permissions.go b/permission/types/permissions.go index 0451960f5..1a0bd6a7e 100644 --- a/permission/types/permissions.go +++ b/permission/types/permissions.go @@ -25,10 +25,11 @@ const ( CreateContract // 8 CreateAccount // 16 Bond // 32 + Name // 64 - DefaultBBPB = Send | Call | CreateContract | CreateAccount | Bond + DefaultBBPB = Send | Call | CreateContract | CreateAccount | Bond | Name - NumBasePermissions uint = 6 + NumBasePermissions uint = 7 TopBasePermission PermFlag = 1 << (NumBasePermissions - 1) AllSet PermFlag = (1 << 63) - 1 + (1 << 63) ) diff --git a/state/execution.go b/state/execution.go index 5a8e3fd19..853af2ff3 100644 --- a/state/execution.go +++ b/state/execution.go @@ -529,6 +529,10 @@ func ExecTx(blockCache *BlockCache, tx_ types.Tx, runCall bool, evc events.Firea log.Debug(Fmt("Can't find in account %X", tx.Input.Address)) return types.ErrTxInvalidAddress } + // check permission + if !hasNamePermission(blockCache, inAcc) { + return fmt.Errorf("Account %X does not have Name permission", tx.Input.Address) + } // pubKey should be present in either "inAcc" or "tx.Input" if err := checkInputPubKey(inAcc, tx.Input); err != nil { log.Debug(Fmt("Can't find pubkey for %X", tx.Input.Address)) @@ -841,6 +845,10 @@ func hasSendPermission(state AccountGetter, accs map[string]*account.Account) bo return true } +func hasNamePermission(state AccountGetter, acc *account.Account) bool { + return HasPermission(state, acc, ptypes.Name) +} + func hasCallPermission(state AccountGetter, acc *account.Account) bool { return HasPermission(state, acc, ptypes.Call) } diff --git a/state/permissions_test.go b/state/permissions_test.go index eca9820c0..b9d3c7cbd 100644 --- a/state/permissions_test.go +++ b/state/permissions_test.go @@ -39,6 +39,10 @@ x - contract runs create but doesn't have create perm x - contract runs create but has perm x - contract runs call with empty address (has call and create perm) +- NameTx + - no perm, send perm, call perm + - with perm + - BondTx x - 1 input, no perm x - 1 input, perm @@ -190,6 +194,40 @@ func TestSendFails(t *testing.T) { } } +func TestName(t *testing.T) { + stateDB := dbm.GetDB("state") + genDoc := newBaseGenDoc(PermsAllFalse, PermsAllFalse) + genDoc.Accounts[0].Permissions.Base.Set(ptypes.Send, true) + genDoc.Accounts[1].Permissions.Base.Set(ptypes.Name, true) + st := MakeGenesisState(stateDB, &genDoc) + blockCache := NewBlockCache(st) + + //------------------- + // name txs + + // simple name tx without perm should fail + tx, err := types.NewNameTx(st, user[0].PubKey, "somename", "somedata", 10000, 100) + if err != nil { + t.Fatal(err) + } + tx.Sign(chainID, user[0]) + if err := ExecTx(blockCache, tx, true, nil); err == nil { + t.Fatal("Expected error") + } else { + fmt.Println(err) + } + + // simple name tx with perm should pass + tx, err = types.NewNameTx(st, user[1].PubKey, "somename", "somedata", 10000, 100) + if err != nil { + t.Fatal(err) + } + tx.Sign(chainID, user[1]) + if err := ExecTx(blockCache, tx, true, nil); err != nil { + t.Fatal(err) + } +} + func TestCallFails(t *testing.T) { stateDB := dbm.GetDB("state") genDoc := newBaseGenDoc(PermsAllFalse, PermsAllFalse)