nametx perm

This commit is contained in:
Ethan Buchman 2015-06-09 18:50:42 -04:00 committed by Jae Kwon
parent 7e1412dbc2
commit 231a003783
3 changed files with 49 additions and 2 deletions

View File

@ -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)
)

View File

@ -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)
}

View File

@ -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)