From 3bd39ad8b5dedb74a799ff0c06814c74e43b60b3 Mon Sep 17 00:00:00 2001 From: Osoro Bironga Date: Sun, 20 Sep 2020 11:43:57 +0300 Subject: [PATCH] accounts/abi: improve documentation and names (#21540) * accounts: abi/bid/backends; cleaned doc errors, camelCase refactors and anonymous variable assignments * acounts/abi/bind: doc errors, anonymous parameter assignments * accounts/abi: doc edits, camelCase refactors * accounts/abi/bind: review fix * reverted name changes * name revert Co-authored-by: Osoro Bironga --- accounts/abi/abi.go | 10 +++++----- accounts/abi/abi_test.go | 2 +- accounts/abi/argument.go | 18 +++++++++--------- accounts/abi/bind/auth.go | 2 +- accounts/abi/bind/backend.go | 6 +++--- accounts/abi/bind/base.go | 2 +- accounts/abi/bind/bind.go | 18 +++++++++--------- accounts/abi/bind/template.go | 11 ++++++----- accounts/abi/event.go | 2 +- accounts/abi/event_test.go | 2 +- accounts/abi/method.go | 4 ++-- accounts/abi/pack.go | 5 ++--- accounts/abi/reflect.go | 2 +- accounts/abi/topics.go | 4 ++-- accounts/abi/type.go | 4 ++-- accounts/abi/unpack.go | 17 +++++++++-------- 16 files changed, 55 insertions(+), 54 deletions(-) diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go index 712bd58348da4..09f30edee9080 100644 --- a/accounts/abi/abi.go +++ b/accounts/abi/abi.go @@ -80,7 +80,7 @@ func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) { return append(method.ID, arguments...), nil } -// Unpack output in v according to the abi specification +// Unpack output in v according to the abi specification. func (abi ABI) Unpack(v interface{}, name string, data []byte) (err error) { // since there can't be naming collisions with contracts and events, // we need to decide whether we're calling a method or an event @@ -96,7 +96,7 @@ func (abi ABI) Unpack(v interface{}, name string, data []byte) (err error) { return fmt.Errorf("abi: could not locate named method or event") } -// UnpackIntoMap unpacks a log into the provided map[string]interface{} +// UnpackIntoMap unpacks a log into the provided map[string]interface{}. func (abi ABI) UnpackIntoMap(v map[string]interface{}, name string, data []byte) (err error) { // since there can't be naming collisions with contracts and events, // we need to decide whether we're calling a method or an event @@ -112,7 +112,7 @@ func (abi ABI) UnpackIntoMap(v map[string]interface{}, name string, data []byte) return errors.New("abi: could not locate named method or event") } -// UnmarshalJSON implements json.Unmarshaler interface +// UnmarshalJSON implements json.Unmarshaler interface. func (abi *ABI) UnmarshalJSON(data []byte) error { var fields []struct { Type string @@ -201,8 +201,8 @@ func (abi *ABI) overloadedEventName(rawName string) string { return name } -// MethodById looks up a method by the 4-byte id -// returns nil if none found +// MethodById looks up a method by the 4-byte id, +// returns nil if none found. func (abi *ABI) MethodById(sigdata []byte) (*Method, error) { if len(sigdata) < 4 { return nil, fmt.Errorf("data too short (%d bytes) for abi method lookup", len(sigdata)) diff --git a/accounts/abi/abi_test.go b/accounts/abi/abi_test.go index 3d07e4cbf5175..d13aa7fc60495 100644 --- a/accounts/abi/abi_test.go +++ b/accounts/abi/abi_test.go @@ -1127,7 +1127,7 @@ func TestDoubleDuplicateEventNames(t *testing.T) { } // TestUnnamedEventParam checks that an event with unnamed parameters is -// correctly handled +// correctly handled. // The test runs the abi of the following contract. // contract TestEvent { // event send(uint256, uint256); diff --git a/accounts/abi/argument.go b/accounts/abi/argument.go index 81151ef0e0049..aaef3bd41cdf5 100644 --- a/accounts/abi/argument.go +++ b/accounts/abi/argument.go @@ -41,7 +41,7 @@ type ArgumentMarshaling struct { Indexed bool } -// UnmarshalJSON implements json.Unmarshaler interface +// UnmarshalJSON implements json.Unmarshaler interface. func (argument *Argument) UnmarshalJSON(data []byte) error { var arg ArgumentMarshaling err := json.Unmarshal(data, &arg) @@ -59,7 +59,7 @@ func (argument *Argument) UnmarshalJSON(data []byte) error { return nil } -// NonIndexed returns the arguments with indexed arguments filtered out +// NonIndexed returns the arguments with indexed arguments filtered out. func (arguments Arguments) NonIndexed() Arguments { var ret []Argument for _, arg := range arguments { @@ -70,12 +70,12 @@ func (arguments Arguments) NonIndexed() Arguments { return ret } -// isTuple returns true for non-atomic constructs, like (uint,uint) or uint[] +// isTuple returns true for non-atomic constructs, like (uint,uint) or uint[]. func (arguments Arguments) isTuple() bool { return len(arguments) > 1 } -// Unpack performs the operation hexdata -> Go format +// Unpack performs the operation hexdata -> Go format. func (arguments Arguments) Unpack(v interface{}, data []byte) error { if len(data) == 0 { if len(arguments) != 0 { @@ -100,7 +100,7 @@ func (arguments Arguments) Unpack(v interface{}, data []byte) error { return arguments.unpackAtomic(v, marshalledValues[0]) } -// UnpackIntoMap performs the operation hexdata -> mapping of argument name to argument value +// UnpackIntoMap performs the operation hexdata -> mapping of argument name to argument value. func (arguments Arguments) UnpackIntoMap(v map[string]interface{}, data []byte) error { // Make sure map is not nil if v == nil { @@ -122,7 +122,7 @@ func (arguments Arguments) UnpackIntoMap(v map[string]interface{}, data []byte) return nil } -// unpackAtomic unpacks ( hexdata -> go ) a single value +// unpackAtomic unpacks ( hexdata -> go ) a single value. func (arguments Arguments) unpackAtomic(v interface{}, marshalledValues interface{}) error { dst := reflect.ValueOf(v).Elem() src := reflect.ValueOf(marshalledValues) @@ -207,13 +207,13 @@ func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error) { return retval, nil } -// PackValues performs the operation Go format -> Hexdata -// It is the semantic opposite of UnpackValues +// PackValues performs the operation Go format -> Hexdata. +// It is the semantic opposite of UnpackValues. func (arguments Arguments) PackValues(args []interface{}) ([]byte, error) { return arguments.Pack(args...) } -// Pack performs the operation Go format -> Hexdata +// Pack performs the operation Go format -> Hexdata. func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) { // Make sure arguments match up and pack them abiArgs := arguments diff --git a/accounts/abi/bind/auth.go b/accounts/abi/bind/auth.go index 6051aebe583c0..22ae6a218fa58 100644 --- a/accounts/abi/bind/auth.go +++ b/accounts/abi/bind/auth.go @@ -54,7 +54,7 @@ func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) { } // NewKeyStoreTransactor is a utility method to easily create a transaction signer from -// an decrypted key from a keystore +// a decrypted key from a keystore. func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) { signer := types.HomesteadSigner{} return &TransactOpts{ diff --git a/accounts/abi/bind/backend.go b/accounts/abi/bind/backend.go index 25ac008c03eca..b067b7da2ac0c 100644 --- a/accounts/abi/bind/backend.go +++ b/accounts/abi/bind/backend.go @@ -41,7 +41,7 @@ var ( ErrNoCodeAfterDeploy = errors.New("no contract code after deployment") ) -// ContractCaller defines the methods needed to allow operating with contract on a read +// ContractCaller defines the methods needed to allow operating with a contract on a read // only basis. type ContractCaller interface { // CodeAt returns the code of the given account. This is needed to differentiate @@ -64,8 +64,8 @@ type PendingContractCaller interface { PendingCallContract(ctx context.Context, call XDPoSChain.CallMsg) ([]byte, error) } -// ContractTransactor defines the methods needed to allow operating with contract -// on a write only basis. Beside the transacting method, the remainder are helpers +// ContractTransactor defines the methods needed to allow operating with a contract +// on a write only basis. Besides the transacting method, the remainder are helpers // used when the user does not provide some needed values, but rather leaves it up // to the transactor to decide. type ContractTransactor interface { diff --git a/accounts/abi/bind/base.go b/accounts/abi/bind/base.go index 28fa475334eca..172a3ac64cf1b 100644 --- a/accounts/abi/bind/base.go +++ b/accounts/abi/bind/base.go @@ -185,7 +185,7 @@ func (c *BoundContract) Transact(opts *TransactOpts, method string, params ...in } // RawTransact initiates a transaction with the given raw calldata as the input. -// It's usually used to initiates transaction for invoking **Fallback** function. +// It's usually used to initiate transactions for invoking **Fallback** function. func (c *BoundContract) RawTransact(opts *TransactOpts, calldata []byte) (*types.Transaction, error) { // todo(rjl493456442) check the method is payable or not, // reject invalid transaction at the first place diff --git a/accounts/abi/bind/bind.go b/accounts/abi/bind/bind.go index 53e278274b153..0d2a0cc0a5c14 100644 --- a/accounts/abi/bind/bind.go +++ b/accounts/abi/bind/bind.go @@ -49,7 +49,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string] // contracts is the map of each individual contract requested binding contracts = make(map[string]*tmplContract) - // structs is the map of all reclared structs shared by passed contracts. + // structs is the map of all redeclared structs shared by passed contracts. structs = make(map[string]*tmplStruct) // isLib is the map used to flag each encountered library as such @@ -77,10 +77,10 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string] fallback *tmplMethod receive *tmplMethod - // identifiers are used to detect duplicated identifier of function - // and event. For all calls, transacts and events, abigen will generate + // identifiers are used to detect duplicated identifiers of functions + // and events. For all calls, transacts and events, abigen will generate // corresponding bindings. However we have to ensure there is no - // identifier coliision in the bindings of these categories. + // identifier collisions in the bindings of these categories. callIdentifiers = make(map[string]bool) transactIdentifiers = make(map[string]bool) eventIdentifiers = make(map[string]bool) @@ -238,7 +238,7 @@ var bindType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) stri LangGo: bindTypeGo, } -// bindBasicTypeGo converts basic solidity types(except array, slice and tuple) to Go one. +// bindBasicTypeGo converts basic solidity types(except array, slice and tuple) to Go ones. func bindBasicTypeGo(kind abi.Type) string { switch kind.T { case abi.AddressTy: @@ -285,7 +285,7 @@ var bindTopicType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) } // bindTopicTypeGo converts a Solidity topic type to a Go one. It is almost the same -// funcionality as for simple types, but dynamic types get converted to hashes. +// functionality as for simple types, but dynamic types get converted to hashes. func bindTopicTypeGo(kind abi.Type, structs map[string]*tmplStruct) string { bound := bindTypeGo(kind, structs) @@ -313,7 +313,7 @@ var bindStructType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct func bindStructTypeGo(kind abi.Type, structs map[string]*tmplStruct) string { switch kind.T { case abi.TupleTy: - // We compose raw struct name and canonical parameter expression + // We compose a raw struct name and a canonical parameter expression // together here. The reason is before solidity v0.5.11, kind.TupleRawName // is empty, so we use canonical parameter expression to distinguish // different struct definition. From the consideration of backward @@ -347,7 +347,7 @@ func bindStructTypeGo(kind abi.Type, structs map[string]*tmplStruct) string { } // namedType is a set of functions that transform language specific types to -// named versions that my be used inside method names. +// named versions that may be used inside method names. var namedType = map[Lang]func(string, abi.Type) string{ LangGo: func(string, abi.Type) string { panic("this shouldn't be needed") }, } @@ -362,7 +362,7 @@ func alias(aliases map[string]string, n string) string { } // methodNormalizer is a name transformer that modifies Solidity method names to -// conform to target language naming concentions. +// conform to target language naming conventions. var methodNormalizer = map[Lang]func(string) string{ LangGo: abi.ToCamelCase, } diff --git a/accounts/abi/bind/template.go b/accounts/abi/bind/template.go index 49c877d4a438e..13ead915b920d 100644 --- a/accounts/abi/bind/template.go +++ b/accounts/abi/bind/template.go @@ -30,7 +30,7 @@ type tmplData struct { type tmplContract struct { Type string // Type name of the main contract binding InputABI string // JSON ABI used as the input to generate the binding from - InputBin string // Optional EVM bytecode used to denetare deploy code from + InputBin string // Optional EVM bytecode used to generate deploy code from FuncSigs map[string]string // Optional map: string signature -> 4-byte signature Constructor abi.Method // Contract constructor for deploy parametrization Calls map[string]*tmplMethod // Contract calls that only read state data @@ -50,7 +50,8 @@ type tmplMethod struct { Structured bool // Whether the returns should be accumulated into a struct } -// tmplEvent is a wrapper around an a +// tmplEvent is a wrapper around an abi.Event that contains a few preprocessed +// and cached data fields. type tmplEvent struct { Original abi.Event // Original event as parsed by the abi package Normalized abi.Event // Normalized version of the parsed fields @@ -64,7 +65,7 @@ type tmplField struct { SolKind abi.Type // Raw abi type information } -// tmplStruct is a wrapper around an abi.tuple contains an auto-generated +// tmplStruct is a wrapper around an abi.tuple and contains an auto-generated // struct name. type tmplStruct struct { Name string // Auto-generated struct name(before solidity v0.5.11) or raw name. @@ -77,8 +78,8 @@ var tmplSource = map[Lang]string{ LangGo: tmplSourceGo, } -// tmplSourceGo is the Go source template use to generate the contract binding -// based on. +// tmplSourceGo is the Go source template that the generated Go contract binding +// is based on. const tmplSourceGo = ` // Code generated - DO NOT EDIT. // This file is a generated binding and any manual changes will be lost. diff --git a/accounts/abi/event.go b/accounts/abi/event.go index db01a0f6bfb81..1eb53d9e910b5 100644 --- a/accounts/abi/event.go +++ b/accounts/abi/event.go @@ -32,7 +32,7 @@ type Event struct { // the raw name and a suffix will be added in the case of a event overload. // // e.g. - // There are two events have same name: + // These are two events that have the same name: // * foo(int,int) // * foo(uint,uint) // The event name of the first one wll be resolved as foo while the second one diff --git a/accounts/abi/event_test.go b/accounts/abi/event_test.go index 836b3fadcf1b7..32d6d056092d2 100644 --- a/accounts/abi/event_test.go +++ b/accounts/abi/event_test.go @@ -371,7 +371,7 @@ func TestEventUnpackIndexed(t *testing.T) { require.Equal(t, uint8(8), rst.Value2) } -// TestEventIndexedWithArrayUnpack verifies that decoder will not overlow when static array is indexed input. +// TestEventIndexedWithArrayUnpack verifies that decoder will not overflow when static array is indexed input. func TestEventIndexedWithArrayUnpack(t *testing.T) { definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"string"}]}]` type testStruct struct { diff --git a/accounts/abi/method.go b/accounts/abi/method.go index 3e6573ad5cc79..c67a8a1e78c8d 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -45,7 +45,7 @@ const ( // If the method is `Const` no transaction needs to be created for this // particular Method call. It can easily be simulated using a local VM. // For example a `Balance()` method only needs to retrieve something -// from the storage and therefore requires no Tx to be send to the +// from the storage and therefore requires no Tx to be sent to the // network. A method such as `Transact` does require a Tx and thus will // be flagged `false`. // Input specifies the required input parameters for this gives method. @@ -54,7 +54,7 @@ type Method struct { // the raw name and a suffix will be added in the case of a function overload. // // e.g. - // There are two functions have same name: + // These are two functions that have the same name: // * foo(int,int) // * foo(uint,uint) // The method name of the first one will be resolved as foo while the second one diff --git a/accounts/abi/pack.go b/accounts/abi/pack.go index 59b8b785ec72a..42b314c73417b 100644 --- a/accounts/abi/pack.go +++ b/accounts/abi/pack.go @@ -27,7 +27,7 @@ import ( ) // packBytesSlice packs the given bytes as [L, V] as the canonical representation -// bytes slice +// bytes slice. func packBytesSlice(bytes []byte, l int) []byte { len := packNum(reflect.ValueOf(l)) return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...) @@ -70,7 +70,7 @@ func packElement(t Type, reflectValue reflect.Value) ([]byte, error) { } } -// packNum packs the given number (using the reflect value) and will cast it to appropriate number representation +// packNum packs the given number (using the reflect value) and will cast it to appropriate number representation. func packNum(value reflect.Value) []byte { switch kind := value.Kind(); kind { case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: @@ -82,5 +82,4 @@ func packNum(value reflect.Value) []byte { default: panic("abi: fatal error") } - } diff --git a/accounts/abi/reflect.go b/accounts/abi/reflect.go index 7892c730e3ac1..34a63f44d667e 100644 --- a/accounts/abi/reflect.go +++ b/accounts/abi/reflect.go @@ -61,7 +61,7 @@ func reflectIntType(unsigned bool, size int) reflect.Type { return reflect.TypeOf(&big.Int{}) } -// mustArrayToBytesSlice creates a new byte slice with the exact same size as value +// mustArrayToByteSlice creates a new byte slice with the exact same size as value // and copies the bytes in value to the new slice. func mustArrayToByteSlice(value reflect.Value) reflect.Value { slice := reflect.MakeSlice(reflect.TypeOf([]byte{}), value.Len(), value.Len()) diff --git a/accounts/abi/topics.go b/accounts/abi/topics.go index a46968b38544d..119ef06d246a4 100644 --- a/accounts/abi/topics.go +++ b/accounts/abi/topics.go @@ -102,7 +102,7 @@ func genIntType(rule int64, size uint) []byte { var topic [common.HashLength]byte if rule < 0 { // if a rule is negative, we need to put it into two's complement. - // extended to common.Hashlength bytes. + // extended to common.HashLength bytes. topic = [common.HashLength]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} } for i := uint(0); i < size; i++ { @@ -120,7 +120,7 @@ func ParseTopics(out interface{}, fields Arguments, topics []common.Hash) error }) } -// ParseTopicsIntoMap converts the indexed topic field-value pairs into map key-value pairs +// ParseTopicsIntoMap converts the indexed topic field-value pairs into map key-value pairs. func ParseTopicsIntoMap(out map[string]interface{}, fields Arguments, topics []common.Hash) error { return parseTopicWithSetter(fields, topics, func(arg Argument, reconstr interface{}) { diff --git a/accounts/abi/type.go b/accounts/abi/type.go index 83b553e6bf0c6..95b985f303cd1 100644 --- a/accounts/abi/type.go +++ b/accounts/abi/type.go @@ -44,7 +44,7 @@ const ( FunctionTy ) -// Type is the reflection of the supported argument type +// Type is the reflection of the supported argument type. type Type struct { Elem *Type Size int @@ -263,7 +263,7 @@ func overloadedArgName(rawName string, names map[string]string) (string, error) return fieldName, nil } -// String implements Stringer +// String implements Stringer. func (t Type) String() (out string) { return t.stringKind } diff --git a/accounts/abi/unpack.go b/accounts/abi/unpack.go index 26e7ea1fe6468..4bafd5b6dfa3b 100644 --- a/accounts/abi/unpack.go +++ b/accounts/abi/unpack.go @@ -27,13 +27,13 @@ import ( ) var ( - // MaxUint256 is the maximum value that can be represented by a uint256 + // MaxUint256 is the maximum value that can be represented by a uint256. MaxUint256 = new(big.Int).Sub(new(big.Int).Lsh(common.Big1, 256), common.Big1) - // MaxInt256 is the maximum value that can be represented by a int256 + // MaxInt256 is the maximum value that can be represented by a int256. MaxInt256 = new(big.Int).Sub(new(big.Int).Lsh(common.Big1, 255), common.Big1) ) -// ReadInteger reads the integer based on its kind and returns the appropriate value +// ReadInteger reads the integer based on its kind and returns the appropriate value. func ReadInteger(typ Type, b []byte) interface{} { if typ.T == UintTy { switch typ.Size { @@ -74,7 +74,7 @@ func ReadInteger(typ Type, b []byte) interface{} { } } -// reads a bool +// readBool reads a bool. func readBool(word []byte) (bool, error) { for _, b := range word[:31] { if b != 0 { @@ -92,7 +92,8 @@ func readBool(word []byte) (bool, error) { } // A function type is simply the address with the function selection signature at the end. -// This enforces that standard by always presenting it as a 24-array (address + sig = 24 bytes) +// +// readFunctionType enforces that standard by always presenting it as a 24-array (address + sig = 24 bytes) func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) { if t.T != FunctionTy { return [24]byte{}, errors.New("abi: invalid type in call to make function type byte array") @@ -105,7 +106,7 @@ func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) { return } -// ReadFixedBytes uses reflection to create a fixed array to be read from +// ReadFixedBytes uses reflection to create a fixed array to be read from. func ReadFixedBytes(t Type, word []byte) (interface{}, error) { if t.T != FixedBytesTy { return nil, errors.New("abi: invalid type in call to make fixed byte array") @@ -118,7 +119,7 @@ func ReadFixedBytes(t Type, word []byte) (interface{}, error) { } -// iteratively unpack elements +// forEachUnpack iteratively unpack elements. func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error) { if size < 0 { return nil, fmt.Errorf("cannot marshal input to array, size is negative (%d)", size) @@ -253,7 +254,7 @@ func toGoType(index int, t Type, output []byte) (interface{}, error) { } } -// interprets a 32 byte slice as an offset and then determines which indice to look to decode the type. +// lengthPrefixPointsTo interprets a 32 byte slice as an offset and then determines which indices to look to decode the type. func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err error) { bigOffsetEnd := big.NewInt(0).SetBytes(output[index : index+32]) bigOffsetEnd.Add(bigOffsetEnd, common.Big32)