From af51be5a97d8a8367b27ddd2d844f71e546d482c Mon Sep 17 00:00:00 2001 From: Silas Davis <silas@erisindustries.com> Date: Tue, 21 Feb 2017 00:31:02 +0000 Subject: [PATCH] Change case convention for permissions contract to match solidity style guide, add missing godoc comments, automate signature test --- client/rpc/client.go | 25 +-- client/rpc/client_test.go | 2 +- manager/eris-mint/evm/snative.go | 156 ++++++++++-------- manager/eris-mint/evm/snative_test.go | 93 ++++++----- manager/eris-mint/state/permissions_test.go | 64 +++---- permission/types/permissions.go | 28 ++-- util/snatives/templates/solidity_templates.go | 4 +- .../templates/solidity_templates_test.go | 6 +- 8 files changed, 202 insertions(+), 176 deletions(-) diff --git a/client/rpc/client.go b/client/rpc/client.go index 77ed13b5..72c2002f 100644 --- a/client/rpc/client.go +++ b/client/rpc/client.go @@ -99,19 +99,6 @@ func Name(nodeClient client.NodeClient, keyClient keys.KeyClient, pubkey, addr, return tx, nil } -type PermFunc struct { - Name string - Args string -} - -var PermsFuncs = []PermFunc{ - {"set_base", "address, permission flag, value"}, - {"unset_base", "address, permission flag"}, - {"set_global", "permission flag, value"}, - {"add_role", "address, role"}, - {"rm_role", "address, role"}, -} - func Permissions(nodeClient client.NodeClient, keyClient keys.KeyClient, pubkey, addrS, nonceS, permFunc string, argsS []string) (*txs.PermissionsTx, error) { pub, _, nonce, err := checkCommon(nodeClient, keyClient, pubkey, addrS, "0", nonceS) if err != nil { @@ -119,13 +106,13 @@ func Permissions(nodeClient client.NodeClient, keyClient keys.KeyClient, pubkey, } var args ptypes.PermArgs switch permFunc { - case "set_base": + case "setBase": addr, pF, err := decodeAddressPermFlag(argsS[0], argsS[1]) if err != nil { return nil, err } if len(argsS) != 3 { - return nil, fmt.Errorf("set_base also takes a value (true or false)") + return nil, fmt.Errorf("setBase also takes a value (true or false)") } var value bool if argsS[2] == "true" { @@ -136,13 +123,13 @@ func Permissions(nodeClient client.NodeClient, keyClient keys.KeyClient, pubkey, return nil, fmt.Errorf("Unknown value %s", argsS[2]) } args = &ptypes.SetBaseArgs{addr, pF, value} - case "unset_base": + case "unsetBase": addr, pF, err := decodeAddressPermFlag(argsS[0], argsS[1]) if err != nil { return nil, err } args = &ptypes.UnsetBaseArgs{addr, pF} - case "set_global": + case "setGlobal": pF, err := ptypes.PermStringToFlag(argsS[0]) if err != nil { return nil, err @@ -156,13 +143,13 @@ func Permissions(nodeClient client.NodeClient, keyClient keys.KeyClient, pubkey, return nil, fmt.Errorf("Unknown value %s", argsS[1]) } args = &ptypes.SetGlobalArgs{pF, value} - case "add_role": + case "addRole": addr, err := hex.DecodeString(argsS[0]) if err != nil { return nil, err } args = &ptypes.AddRoleArgs{addr, argsS[1]} - case "rm_role": + case "removeRole": addr, err := hex.DecodeString(argsS[0]) if err != nil { return nil, err diff --git a/client/rpc/client_test.go b/client/rpc/client_test.go index 6541bd3f..0c240a4c 100644 --- a/client/rpc/client_test.go +++ b/client/rpc/client_test.go @@ -147,7 +147,7 @@ func testPermissions(t *testing.T, nonceString := "" _, err := Permissions(nodeClient, keyClient, publicKeyString, addressString, - nonceString, "set_base", []string{permAddressString, "root", "true"}) + nonceString, "setBase", []string{permAddressString, "root", "true"}) if err != nil { t.Logf("Error in PermissionsTx: %s", err) t.Fail() diff --git a/manager/eris-mint/evm/snative.go b/manager/eris-mint/evm/snative.go index 20463dc8..afc2fc35 100644 --- a/manager/eris-mint/evm/snative.go +++ b/manager/eris-mint/evm/snative.go @@ -9,25 +9,44 @@ import ( . "github.com/eris-ltd/eris-db/word256" "strings" + "github.com/eris-ltd/eris-db/manager/eris-mint/evm/abi" ) -//------------------------------------------------------------------------------------------------ -// Registered SNative contracts +// +// SNative (from 'secure natives') are native (go) contracts that are dispatched +// based on account permissions and can access and modify an account's permissions +// +// Metadata for SNative contract. Acts as a call target from the EVM. Can be +// used to generate bindings in a smart contract languages. type SNativeContractDescription struct { - Comment string - Name string - functions map[FuncID]*SNativeFunctionDescription + // Comment describing purpose of SNative contract and reason for assembling + // the particular functions + Comment string + // Name of the SNative contract + Name string + functionsByID map[FuncID]*SNativeFunctionDescription + functions []*SNativeFunctionDescription } +// Metadata for SNative functions. Act as call targets for the EVM when +// collected into an SNativeContractDescription. Can be used to generate +// bindings in a smart contract languages. type SNativeFunctionDescription struct { - Comment string - Name string - Args []abi.Arg - Return abi.Return + // Comment describing function's purpose, parameters, and return value + Comment string + // Function name (used to form signature) + Name string + // Function arguments (used to form signature) + Args []abi.Arg + // Function return value + Return abi.Return + // Permissions required to call function PermFlag ptypes.PermFlag - F NativeContract + // Native function to which calls will be dispatched when a containing + // contract is called with a FuncID matching this NativeContract + F NativeContract } func registerSNativeContracts() { @@ -45,51 +64,51 @@ func SNativeContracts() map[string]*SNativeContractDescription { * Interface for managing Secure Native authorizations. * @dev This interface describes the functions exposed by the SNative permissions layer in the Monax blockchain (ErisDB). `, - "permissions_contract", + "Permissions", &SNativeFunctionDescription{` * @notice Adds a role to an account * @param _account account address * @param _role role name * @return result whether role was added `, - "add_role", + "addRole", []abi.Arg{ arg("_account", abi.Address), arg("_role", roleType), }, ret("result", abi.Bool), ptypes.AddRole, - add_role}, + addRole}, &SNativeFunctionDescription{` - * @notice Indicates whether an account has a role + * @notice Removes a role from an account * @param _account account address * @param _role role name - * @return result whether account has role + * @return result whether role was removed `, - "has_role", + "removeRole", []abi.Arg{ arg("_account", abi.Address), arg("_role", roleType), }, ret("result", abi.Bool), - ptypes.HasRole, - has_role}, + ptypes.RmRole, + removeRole}, &SNativeFunctionDescription{` - * @notice Removes a role from an account + * @notice Indicates whether an account has a role * @param _account account address * @param _role role name - * @return result whether role was removed + * @return result whether account has role `, - "rm_role", + "hasRole", []abi.Arg{ arg("_account", abi.Address), arg("_role", roleType), }, ret("result", abi.Bool), - ptypes.RmRole, - rm_role}, + ptypes.HasRole, + hasRole}, &SNativeFunctionDescription{` * @notice Sets the permission flags for an account. Makes them explicitly set (on or off). @@ -98,7 +117,7 @@ func SNativeContracts() map[string]*SNativeContractDescription { * @param _set whether to set or unset the permissions flags at the account level * @return result the resultant permissions flags on the account after the call `, - "set_base", + "setBase", []abi.Arg{ arg("_account", abi.Address), arg("_permission", permFlagType), @@ -106,21 +125,7 @@ func SNativeContracts() map[string]*SNativeContractDescription { }, ret("result", permFlagType), ptypes.SetBase, - set_base}, - - &SNativeFunctionDescription{` - * @notice Indicates whether an account has a subset of permissions set - * @param _account account address - * @param _permission the permissions flags (mask) to check whether enabled against base permissions for the account - * @return result whether account has the passed permissions flags set - `, - "has_base", - []abi.Arg{ - arg("_account", abi.Address), - arg("_permission", permFlagType)}, - ret("result", permFlagType), - ptypes.HasBase, - has_base}, + setBase}, &SNativeFunctionDescription{` * @notice Unsets the permissions flags for an account. Causes permissions being unset to fall through to global permissions. @@ -128,13 +133,27 @@ func SNativeContracts() map[string]*SNativeContractDescription { * @param _permission the permissions flags to unset for the account * @return result the resultant permissions flags on the account after the call `, - "unset_base", + "unsetBase", []abi.Arg{ arg("_account", abi.Address), arg("_permission", permFlagType)}, ret("result", permFlagType), ptypes.UnsetBase, - unset_base}, + unsetBase}, + + &SNativeFunctionDescription{` + * @notice Indicates whether an account has a subset of permissions set + * @param _account account address + * @param _permission the permissions flags (mask) to check whether enabled against base permissions for the account + * @return result whether account has the passed permissions flags set + `, + "hasBase", + []abi.Arg{ + arg("_account", abi.Address), + arg("_permission", permFlagType)}, + ret("result", permFlagType), + ptypes.HasBase, + hasBase}, &SNativeFunctionDescription{` * @notice Sets the global (default) permissions flags for the entire chain @@ -142,13 +161,13 @@ func SNativeContracts() map[string]*SNativeContractDescription { * @param _set whether to set (or unset) the permissions flags * @return result the resultant permissions flags on the account after the call `, - "set_global", + "setGlobal", []abi.Arg{ arg("_permission", permFlagType), arg("_set", abi.Bool)}, ret("result", permFlagType), ptypes.SetGlobal, - set_global}, + setGlobal}, ), } @@ -159,24 +178,26 @@ func SNativeContracts() map[string]*SNativeContractDescription { return contractMap } -//----------------------------------------------------------------------------- -// snative are native contracts that can access and modify an account's permissions +// Create a new SNative contract description object by passing a comment, name +// and a list of member functions descriptions +func NewSNativeContract(comment, name string, + functions ...*SNativeFunctionDescription) *SNativeContractDescription { -func NewSNativeContract(comment, name string, functions ...*SNativeFunctionDescription) *SNativeContractDescription { - fs := make(map[FuncID]*SNativeFunctionDescription, len(functions)) + functionsByID := make(map[FuncID]*SNativeFunctionDescription, len(functions)) for _, f := range functions { fid := f.ID() - otherF, ok := fs[fid] + otherF, ok := functionsByID[fid] if ok { panic(fmt.Errorf("Function with ID %x already defined: %s", fid, otherF)) } - fs[fid] = f + functionsByID[fid] = f } return &SNativeContractDescription{ - Comment: comment, - Name: name, - functions: fs, + Comment: comment, + Name: name, + functionsByID: functionsByID, + functions: functions, } } @@ -218,8 +239,9 @@ func (contract *SNativeContractDescription) Address() Word256 { return LeftPadWord256([]byte(contract.Name)) } +// Get function by calling identifier FuncID func (contract *SNativeContractDescription) FunctionByID(id FuncID) (*SNativeFunctionDescription, error) { - f, ok := contract.functions[id] + f, ok := contract.functionsByID[id] if !ok { return nil, fmt.Errorf("Unknown SNative function with ID %x", id) @@ -227,6 +249,7 @@ func (contract *SNativeContractDescription) FunctionByID(id FuncID) (*SNativeFun return f, nil } +// Get function by name func (contract *SNativeContractDescription) FunctionByName(name string) (*SNativeFunctionDescription, error) { for _, f := range contract.functions { if f.Name == name { @@ -236,17 +259,18 @@ func (contract *SNativeContractDescription) FunctionByName(name string) (*SNativ return nil, fmt.Errorf("Unknown SNative function with name %s", name) } +// Get functions in order of declaration func (contract *SNativeContractDescription) Functions() []*SNativeFunctionDescription { - fs := make([]*SNativeFunctionDescription, 0, len(contract.functions)) - for _, f := range contract.functions { - fs = append(fs, f) - } - return fs + functions := make([]*SNativeFunctionDescription, len(contract.functions)) + copy(functions, contract.functions) + return functions } // // SNative functions // + +// Get function signature func (function *SNativeFunctionDescription) Signature() string { argTypes := make([]string, len(function.Args)) for i, arg := range function.Args { @@ -256,10 +280,12 @@ func (function *SNativeFunctionDescription) Signature() string { strings.Join(argTypes, ",")) } +// Get function calling identifier FuncID func (function *SNativeFunctionDescription) ID() FuncID { return firstFourBytes(sha3.Sha3([]byte(function.Signature()))) } +// Get number of function arguments func (function *SNativeFunctionDescription) NArgs() int { return len(function.Args) } @@ -281,7 +307,7 @@ func ret(name string, abiType abi.Type) abi.Return { // Permission function defintions // TODO: catch errors, log em, return 0s to the vm (should some errors cause exceptions though?) -func has_base(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) { +func hasBase(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) { addr, permNum := returnTwoArgs(args) vmAcc := appState.GetAccount(addr) if vmAcc == nil { @@ -296,7 +322,7 @@ func has_base(appState AppState, caller *Account, args []byte, gas *int64) (outp return LeftPadWord256([]byte{permInt}).Bytes(), nil } -func set_base(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) { +func setBase(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) { addr, permNum, permVal := returnThreeArgs(args) vmAcc := appState.GetAccount(addr) if vmAcc == nil { @@ -315,7 +341,7 @@ func set_base(appState AppState, caller *Account, args []byte, gas *int64) (outp return resultantPermBytes(vmAcc.Permissions.Base), nil } -func unset_base(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) { +func unsetBase(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) { addr, permNum := returnTwoArgs(args) vmAcc := appState.GetAccount(addr) if vmAcc == nil { @@ -333,7 +359,7 @@ func unset_base(appState AppState, caller *Account, args []byte, gas *int64) (ou return resultantPermBytes(vmAcc.Permissions.Base), nil } -func set_global(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) { +func setGlobal(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) { permNum, permVal := returnTwoArgs(args) vmAcc := appState.GetAccount(ptypes.GlobalPermissionsAddress256) if vmAcc == nil { @@ -352,7 +378,7 @@ func set_global(appState AppState, caller *Account, args []byte, gas *int64) (ou return resultantPermBytes(vmAcc.Permissions.Base), nil } -func has_role(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) { +func hasRole(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) { addr, role := returnTwoArgs(args) vmAcc := appState.GetAccount(addr) if vmAcc == nil { @@ -364,7 +390,7 @@ func has_role(appState AppState, caller *Account, args []byte, gas *int64) (outp return LeftPadWord256([]byte{permInt}).Bytes(), nil } -func add_role(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) { +func addRole(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) { addr, role := returnTwoArgs(args) vmAcc := appState.GetAccount(addr) if vmAcc == nil { @@ -377,7 +403,7 @@ func add_role(appState AppState, caller *Account, args []byte, gas *int64) (outp return LeftPadWord256([]byte{permInt}).Bytes(), nil } -func rm_role(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) { +func removeRole(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) { addr, role := returnTwoArgs(args) vmAcc := appState.GetAccount(addr) if vmAcc == nil { diff --git a/manager/eris-mint/evm/snative_test.go b/manager/eris-mint/evm/snative_test.go index 5677e20b..52810efb 100644 --- a/manager/eris-mint/evm/snative_test.go +++ b/manager/eris-mint/evm/snative_test.go @@ -4,70 +4,65 @@ import ( "encoding/hex" "testing" - . "github.com/eris-ltd/eris-db/word256" - "github.com/stretchr/testify/assert" + "strings" + . "github.com/eris-ltd/eris-db/manager/eris-mint/evm/opcodes" ptypes "github.com/eris-ltd/eris-db/permission/types" + . "github.com/eris-ltd/eris-db/word256" + "github.com/stretchr/testify/assert" ) -/* Compiling the Permissions solidity contract at -(generated by Solidity() function) -https://ethereum.github.io/browser-solidity yields: - -Functions -3fbf7da5 add_role(address,bytes32) -744f5998 has_base(address,uint64) -e8145855 has_role(address,bytes32) -28fd0194 rm_role(address,bytes32) -c2174d8f set_base(address,uint64,bool) -85f1522b set_global(uint64,bool) -73448c99 unset_base(address,uint64) - -*/ +// Compiling the Permissions solidity contract at +// (generated by with 'make snatives' function) and passing to +// https://ethereum.github.io/browser-solidity (toggle details to get list) +// yields: +// Keep this updated to drive TestPermissionsContractSignatures +const compiledSigs = ` +a73f7f8a addRole(address,bytes32) +225b6574 hasBase(address,uint64) +ac4ab3fb hasRole(address,bytes32) +6853920e removeRole(address,bytes32) +dbd4a8ea setBase(address,uint64,bool) +c4bc7b70 setGlobal(uint64,bool) +b7d4dc0d unsetBase(address,uint64) +` func TestPermissionsContractSignatures(t *testing.T) { - contract := SNativeContracts()["permissions_contract"] - - assertFunctionIDSignature(t, contract, "3fbf7da5", - "add_role(address,bytes32)") + contract := SNativeContracts()["Permissions"] - assertFunctionIDSignature(t, contract, "744f5998", - "has_base(address,uint64)") + nFuncs := len(contract.functions) - assertFunctionIDSignature(t, contract, "e8145855", - "has_role(address,bytes32)") + sigMap := idToSignatureMap() - assertFunctionIDSignature(t, contract, "28fd0194", - "rm_role(address,bytes32)") + assert.Len(t, sigMap, nFuncs, + "Permissions contract defines %s functions so we need %s " + + "signatures in compiledSigs", + nFuncs, nFuncs) - assertFunctionIDSignature(t, contract, "c2174d8f", - "set_base(address,uint64,bool)") - - assertFunctionIDSignature(t, contract, "85f1522b", - "set_global(uint64,bool)") - - assertFunctionIDSignature(t, contract, "73448c99", - "unset_base(address,uint64)") + for funcID, signature := range sigMap { + assertFunctionIDSignature(t, contract, funcID, signature) + } } func TestSNativeContractDescription_Dispatch(t *testing.T) { - contract := SNativeContracts()["permissions_contract"] + contract := SNativeContracts()["Permissions"] state := newAppState() caller := &Account{ - Address: addr(1,1,1), + Address: addr(1, 1, 1), } grantee := &Account{ - Address: addr(2,2,2), + Address: addr(2, 2, 2), } state.UpdateAccount(grantee) - function, err := contract.FunctionByName("add_role") + function, err := contract.FunctionByName("addRole") if err != nil { t.Fatalf("Could not get function: %s", err) } funcID := function.ID() gas := int64(1000) + // Should fail since we have no permissions retValue, err := contract.Dispatch(state, caller, Bytecode(funcID[:], grantee.Address, permFlagToWord256(ptypes.CreateAccount)), &gas) assert.Error(t, err) @@ -75,11 +70,12 @@ func TestSNativeContractDescription_Dispatch(t *testing.T) { assert.Contains(t, err.Error(), "does not have permission") } + // Grant all permissions and dispatch should success caller.Permissions = allAccountPermissions() retValue, err = contract.Dispatch(state, caller, Bytecode(funcID[:], grantee.Address, permFlagToWord256(ptypes.CreateAccount)), &gas) assert.NoError(t, err) - assert.Equal(t, retValue, LeftPadBytes([]byte{1},32)) + assert.Equal(t, retValue, LeftPadBytes([]byte{1}, 32)) } // @@ -109,7 +105,7 @@ func permFlagToWord256(permFlag ptypes.PermFlag) Word256 { return Uint64ToWord256(uint64(permFlag)) } -func addr(rightBytes... uint8) Word256 { +func addr(rightBytes ...uint8) Word256 { return LeftPadWord256(rightBytes) } @@ -121,4 +117,19 @@ func allAccountPermissions() ptypes.AccountPermissions { }, Roles: []string{}, } -} \ No newline at end of file +} + +// turns the solidity compiler function summary into a map to drive signature +// test +func idToSignatureMap() map[string]string { + sigMap := make(map[string]string) + lines := strings.Split(compiledSigs, "\n") + for _, line := range lines { + trimmed := strings.Trim(line, " \t") + if trimmed != "" { + idSig := strings.Split(trimmed, " ") + sigMap[idSig[0]] = idSig[1] + } + } + return sigMap +} diff --git a/manager/eris-mint/state/permissions_test.go b/manager/eris-mint/state/permissions_test.go index a264178d..73d1ae4f 100644 --- a/manager/eris-mint/state/permissions_test.go +++ b/manager/eris-mint/state/permissions_test.go @@ -28,7 +28,7 @@ func init() { var ( dbBackend = "memdb" dbDir = "" - permissionsContract = vm.SNativeContracts()["permissions_contract"] + permissionsContract = vm.SNativeContracts()["Permissions"] ) /* @@ -885,7 +885,7 @@ func TestSNativeCALL(t *testing.T) { fmt.Println("\n#### HasBase") // HasBase - snativeAddress, pF, data := snativePermTestInputCALL("has_base", user[3], ptypes.Bond, false) + snativeAddress, pF, data := snativePermTestInputCALL("hasBase", user[3], ptypes.Bond, false) testSNativeCALLExpectFail(t, blockCache, doug, snativeAddress, data) testSNativeCALLExpectPass(t, blockCache, doug, pF, snativeAddress, data, func(ret []byte) error { // return value should be true or false as a 32 byte array... @@ -897,10 +897,10 @@ func TestSNativeCALL(t *testing.T) { fmt.Println("\n#### SetBase") // SetBase - snativeAddress, pF, data = snativePermTestInputCALL("set_base", user[3], ptypes.Bond, false) + snativeAddress, pF, data = snativePermTestInputCALL("setBase", user[3], ptypes.Bond, false) testSNativeCALLExpectFail(t, blockCache, doug, snativeAddress, data) testSNativeCALLExpectPass(t, blockCache, doug, pF, snativeAddress, data, func(ret []byte) error { return nil }) - snativeAddress, pF, data = snativePermTestInputCALL("has_base", user[3], ptypes.Bond, false) + snativeAddress, pF, data = snativePermTestInputCALL("hasBase", user[3], ptypes.Bond, false) testSNativeCALLExpectPass(t, blockCache, doug, pF, snativeAddress, data, func(ret []byte) error { // return value should be true or false as a 32 byte array... if !IsZeros(ret) { @@ -908,9 +908,9 @@ func TestSNativeCALL(t *testing.T) { } return nil }) - snativeAddress, pF, data = snativePermTestInputCALL("set_base", user[3], ptypes.CreateContract, true) + snativeAddress, pF, data = snativePermTestInputCALL("setBase", user[3], ptypes.CreateContract, true) testSNativeCALLExpectPass(t, blockCache, doug, pF, snativeAddress, data, func(ret []byte) error { return nil }) - snativeAddress, pF, data = snativePermTestInputCALL("has_base", user[3], ptypes.CreateContract, false) + snativeAddress, pF, data = snativePermTestInputCALL("hasBase", user[3], ptypes.CreateContract, false) testSNativeCALLExpectPass(t, blockCache, doug, pF, snativeAddress, data, func(ret []byte) error { // return value should be true or false as a 32 byte array... if !IsZeros(ret[:31]) || ret[31] != byte(1) { @@ -921,10 +921,10 @@ func TestSNativeCALL(t *testing.T) { fmt.Println("\n#### UnsetBase") // UnsetBase - snativeAddress, pF, data = snativePermTestInputCALL("unset_base", user[3], ptypes.CreateContract, false) + snativeAddress, pF, data = snativePermTestInputCALL("unsetBase", user[3], ptypes.CreateContract, false) testSNativeCALLExpectFail(t, blockCache, doug, snativeAddress, data) testSNativeCALLExpectPass(t, blockCache, doug, pF, snativeAddress, data, func(ret []byte) error { return nil }) - snativeAddress, pF, data = snativePermTestInputCALL("has_base", user[3], ptypes.CreateContract, false) + snativeAddress, pF, data = snativePermTestInputCALL("hasBase", user[3], ptypes.CreateContract, false) testSNativeCALLExpectPass(t, blockCache, doug, pF, snativeAddress, data, func(ret []byte) error { if !IsZeros(ret) { return fmt.Errorf("Expected 0. Got %X", ret) @@ -934,10 +934,10 @@ func TestSNativeCALL(t *testing.T) { fmt.Println("\n#### SetGlobal") // SetGlobalPerm - snativeAddress, pF, data = snativePermTestInputCALL("set_global", user[3], ptypes.CreateContract, true) + snativeAddress, pF, data = snativePermTestInputCALL("setGlobal", user[3], ptypes.CreateContract, true) testSNativeCALLExpectFail(t, blockCache, doug, snativeAddress, data) testSNativeCALLExpectPass(t, blockCache, doug, pF, snativeAddress, data, func(ret []byte) error { return nil }) - snativeAddress, pF, data = snativePermTestInputCALL("has_base", user[3], ptypes.CreateContract, false) + snativeAddress, pF, data = snativePermTestInputCALL("hasBase", user[3], ptypes.CreateContract, false) testSNativeCALLExpectPass(t, blockCache, doug, pF, snativeAddress, data, func(ret []byte) error { // return value should be true or false as a 32 byte array... if !IsZeros(ret[:31]) || ret[31] != byte(1) { @@ -948,7 +948,7 @@ func TestSNativeCALL(t *testing.T) { fmt.Println("\n#### HasRole") // HasRole - snativeAddress, pF, data = snativeRoleTestInputCALL("has_role", user[3], "bumble") + snativeAddress, pF, data = snativeRoleTestInputCALL("hasRole", user[3], "bumble") testSNativeCALLExpectFail(t, blockCache, doug, snativeAddress, data) testSNativeCALLExpectPass(t, blockCache, doug, pF, snativeAddress, data, func(ret []byte) error { if !IsZeros(ret[:31]) || ret[31] != byte(1) { @@ -959,17 +959,17 @@ func TestSNativeCALL(t *testing.T) { fmt.Println("\n#### AddRole") // AddRole - snativeAddress, pF, data = snativeRoleTestInputCALL("has_role", user[3], "chuck") + snativeAddress, pF, data = snativeRoleTestInputCALL("hasRole", user[3], "chuck") testSNativeCALLExpectPass(t, blockCache, doug, pF, snativeAddress, data, func(ret []byte) error { if !IsZeros(ret) { return fmt.Errorf("Expected 0. Got %X", ret) } return nil }) - snativeAddress, pF, data = snativeRoleTestInputCALL("add_role", user[3], "chuck") + snativeAddress, pF, data = snativeRoleTestInputCALL("addRole", user[3], "chuck") testSNativeCALLExpectFail(t, blockCache, doug, snativeAddress, data) testSNativeCALLExpectPass(t, blockCache, doug, pF, snativeAddress, data, func(ret []byte) error { return nil }) - snativeAddress, pF, data = snativeRoleTestInputCALL("has_role", user[3], "chuck") + snativeAddress, pF, data = snativeRoleTestInputCALL("hasRole", user[3], "chuck") testSNativeCALLExpectPass(t, blockCache, doug, pF, snativeAddress, data, func(ret []byte) error { if !IsZeros(ret[:31]) || ret[31] != byte(1) { return fmt.Errorf("Expected 1. Got %X", ret) @@ -979,10 +979,10 @@ func TestSNativeCALL(t *testing.T) { fmt.Println("\n#### RmRole") // RmRole - snativeAddress, pF, data = snativeRoleTestInputCALL("rm_role", user[3], "chuck") + snativeAddress, pF, data = snativeRoleTestInputCALL("removeRole", user[3], "chuck") testSNativeCALLExpectFail(t, blockCache, doug, snativeAddress, data) testSNativeCALLExpectPass(t, blockCache, doug, pF, snativeAddress, data, func(ret []byte) error { return nil }) - snativeAddress, pF, data = snativeRoleTestInputCALL("has_role", user[3], "chuck") + snativeAddress, pF, data = snativeRoleTestInputCALL("hasRole", user[3], "chuck") testSNativeCALLExpectPass(t, blockCache, doug, pF, snativeAddress, data, func(ret []byte) error { if !IsZeros(ret) { return fmt.Errorf("Expected 0. Got %X", ret) @@ -1006,14 +1006,14 @@ func TestSNativeTx(t *testing.T) { fmt.Println("\n#### SetBase") // SetBase - snativeArgs := snativePermTestInputTx("set_base", user[3], ptypes.Bond, false) + snativeArgs := snativePermTestInputTx("setBase", user[3], ptypes.Bond, false) testSNativeTxExpectFail(t, blockCache, snativeArgs) testSNativeTxExpectPass(t, blockCache, ptypes.SetBase, snativeArgs) acc := blockCache.GetAccount(user[3].Address) if v, _ := acc.Permissions.Base.Get(ptypes.Bond); v { t.Fatal("expected permission to be set false") } - snativeArgs = snativePermTestInputTx("set_base", user[3], ptypes.CreateContract, true) + snativeArgs = snativePermTestInputTx("setBase", user[3], ptypes.CreateContract, true) testSNativeTxExpectPass(t, blockCache, ptypes.SetBase, snativeArgs) acc = blockCache.GetAccount(user[3].Address) if v, _ := acc.Permissions.Base.Get(ptypes.CreateContract); !v { @@ -1022,7 +1022,7 @@ func TestSNativeTx(t *testing.T) { fmt.Println("\n#### UnsetBase") // UnsetBase - snativeArgs = snativePermTestInputTx("unset_base", user[3], ptypes.CreateContract, false) + snativeArgs = snativePermTestInputTx("unsetBase", user[3], ptypes.CreateContract, false) testSNativeTxExpectFail(t, blockCache, snativeArgs) testSNativeTxExpectPass(t, blockCache, ptypes.UnsetBase, snativeArgs) acc = blockCache.GetAccount(user[3].Address) @@ -1032,7 +1032,7 @@ func TestSNativeTx(t *testing.T) { fmt.Println("\n#### SetGlobal") // SetGlobalPerm - snativeArgs = snativePermTestInputTx("set_global", user[3], ptypes.CreateContract, true) + snativeArgs = snativePermTestInputTx("setGlobal", user[3], ptypes.CreateContract, true) testSNativeTxExpectFail(t, blockCache, snativeArgs) testSNativeTxExpectPass(t, blockCache, ptypes.SetGlobal, snativeArgs) acc = blockCache.GetAccount(ptypes.GlobalPermissionsAddress) @@ -1042,7 +1042,7 @@ func TestSNativeTx(t *testing.T) { fmt.Println("\n#### AddRole") // AddRole - snativeArgs = snativeRoleTestInputTx("add_role", user[3], "chuck") + snativeArgs = snativeRoleTestInputTx("addRole", user[3], "chuck") testSNativeTxExpectFail(t, blockCache, snativeArgs) testSNativeTxExpectPass(t, blockCache, ptypes.AddRole, snativeArgs) acc = blockCache.GetAccount(user[3].Address) @@ -1052,7 +1052,7 @@ func TestSNativeTx(t *testing.T) { fmt.Println("\n#### RmRole") // RmRole - snativeArgs = snativeRoleTestInputTx("rm_role", user[3], "chuck") + snativeArgs = snativeRoleTestInputTx("removeRole", user[3], "chuck") testSNativeTxExpectFail(t, blockCache, snativeArgs) testSNativeTxExpectPass(t, blockCache, ptypes.RmRole, snativeArgs) acc = blockCache.GetAccount(user[3].Address) @@ -1194,14 +1194,14 @@ func permNameToFuncID(name string) []byte { func snativePermTestInputCALL(name string, user *acm.PrivAccount, perm ptypes.PermFlag, val bool) (addr []byte, pF ptypes.PermFlag, data []byte) { addr = permissionsContract.Address().Postfix(20) switch name { - case "has_base", "unset_base": + case "hasBase", "unsetBase": data = LeftPadBytes(user.Address, 32) data = append(data, Uint64ToWord256(uint64(perm)).Bytes()...) - case "set_base": + case "setBase": data = LeftPadBytes(user.Address, 32) data = append(data, Uint64ToWord256(uint64(perm)).Bytes()...) data = append(data, boolToWord256(val).Bytes()...) - case "set_global": + case "setGlobal": data = Uint64ToWord256(uint64(perm)).Bytes() data = append(data, boolToWord256(val).Bytes()...) } @@ -1215,13 +1215,13 @@ func snativePermTestInputCALL(name string, user *acm.PrivAccount, perm ptypes.Pe func snativePermTestInputTx(name string, user *acm.PrivAccount, perm ptypes.PermFlag, val bool) (snativeArgs ptypes.PermArgs) { switch name { - case "has_base": + case "hasBase": snativeArgs = &ptypes.HasBaseArgs{user.Address, perm} - case "unset_base": + case "unsetBase": snativeArgs = &ptypes.UnsetBaseArgs{user.Address, perm} - case "set_base": + case "setBase": snativeArgs = &ptypes.SetBaseArgs{user.Address, perm, val} - case "set_global": + case "setGlobal": snativeArgs = &ptypes.SetGlobalArgs{perm, val} } return @@ -1242,11 +1242,11 @@ func snativeRoleTestInputCALL(name string, user *acm.PrivAccount, role string) ( func snativeRoleTestInputTx(name string, user *acm.PrivAccount, role string) (snativeArgs ptypes.PermArgs) { switch name { - case "has_role": + case "hasRole": snativeArgs = &ptypes.HasRoleArgs{user.Address, role} - case "add_role": + case "addRole": snativeArgs = &ptypes.AddRoleArgs{user.Address, role} - case "rm_role": + case "removeRole": snativeArgs = &ptypes.RmRoleArgs{user.Address, role} } return diff --git a/permission/types/permissions.go b/permission/types/permissions.go index bd6cd9ad..03ff4b6b 100644 --- a/permission/types/permissions.go +++ b/permission/types/permissions.go @@ -189,19 +189,19 @@ func PermFlagToString(pf PermFlag) (perm string) { case Name: perm = "name" case HasBase: - perm = "has_base" + perm = "hasBase" case SetBase: - perm = "set_base" + perm = "setBase" case UnsetBase: - perm = "unset_base" + perm = "unsetBase" case SetGlobal: - perm = "set_global" + perm = "setGlobal" case HasRole: - perm = "has_role" + perm = "hasRole" case AddRole: - perm = "add_role" + perm = "addRole" case RmRole: - perm = "rm_role" + perm = "removeRole" default: perm = "#-UNKNOWN-#" } @@ -224,19 +224,19 @@ func PermStringToFlag(perm string) (pf PermFlag, err error) { pf = Bond case "name": pf = Name - case "has_base": + case "hasBase": pf = HasBase - case "set_base": + case "setBase": pf = SetBase - case "unset_base": + case "unsetBase": pf = UnsetBase - case "set_global": + case "setGlobal": pf = SetGlobal - case "has_role": + case "hasRole": pf = HasRole - case "add_role": + case "addRole": pf = AddRole - case "rm_role": + case "removeRole": pf = RmRole default: err = fmt.Errorf("Unknown permission %s", perm) diff --git a/util/snatives/templates/solidity_templates.go b/util/snatives/templates/solidity_templates.go index f98f15e5..9fd0faf5 100644 --- a/util/snatives/templates/solidity_templates.go +++ b/util/snatives/templates/solidity_templates.go @@ -21,7 +21,9 @@ const functionTemplateText = `/** */ function [[.Name]]([[.ArgList]]) constant returns ([[.Return.Type]] [[.Return.Name]]);` -const indentString = "\t" +// Solidity style guide recommends 4 spaces per indentation level +// (see: http://solidity.readthedocs.io/en/develop/style-guide.html) +const indentString = " " var contractTemplate *template.Template var functionTemplate *template.Template diff --git a/util/snatives/templates/solidity_templates_test.go b/util/snatives/templates/solidity_templates_test.go index ffc92b02..d0a68e96 100644 --- a/util/snatives/templates/solidity_templates_test.go +++ b/util/snatives/templates/solidity_templates_test.go @@ -8,8 +8,8 @@ import ( ) func TestSNativeFuncTemplate(t *testing.T) { - contract := vm.SNativeContracts()["permissions_contract"] - function, err := contract.FunctionByName("rm_role") + contract := vm.SNativeContracts()["Permissions"] + function, err := contract.FunctionByName("removeRole") if err != nil { t.Fatal("Couldn't get function") } @@ -22,7 +22,7 @@ func TestSNativeFuncTemplate(t *testing.T) { // This test checks that we can generate the SNative contract interface and // prints it to stdout func TestSNativeContractTemplate(t *testing.T) { - contract := vm.SNativeContracts()["permissions_contract"] + contract := vm.SNativeContracts()["Permissions"] solidityContract := NewSolidityContract(contract) solidity, err := solidityContract.Solidity() assert.NoError(t, err) -- GitLab