// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package abi
import (
"fmt"
"strings"
"github.com/ethereum/go-ethereum/crypto"
)
// FunctionType represents different types of functions a contract might have.
// FunctionType表示合同可能具有的不同类型的功能。
type FunctionType int
const (
// Constructor represents the constructor of the contract.
// 构造函数代表合约的构造函数。
// The constructor function is called while deploying a contract.
// 在部署合约时调用构造函数。
Constructor FunctionType = iota
// Fallback represents the fallback function.
// 回退表示回退功能。
// This function is executed if no other function matches the given function
// signature and no receive function is specified.
// 如果没有其他函数与给定的函数签名匹配,并且没有指定接收函数,则执行此函数。
Fallback
// Receive represents the receive function.
// Receive表示接收函数。
// This function is executed on plain Ether transfers.
// 此功能在普通以太币传输上执行。
Receive
// Function represents a normal function.
// Function 表示正常函数。
Function
)
// Method represents a callable given a `Name` and whether the method is a constant.
// Method表示给定“Name”的可调用对象,以及该方法是否为常量。
// 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 sent to the
// network. A method such as `Transact` does require a Tx and thus will
// be flagged `false`.
// 如果方法是“Const”,则不需要为此特定方法调用创建事务。它可以很容易地使用本地虚拟机进行模拟。
// 例如,“Balance()”方法只需要从存储器中检索一些东西,因此不需要向网络发送Tx。诸如“Transact-”之类的方法确实需要Tx,因此将被标记为“false”。
// Input specifies the required input parameters for this gives method.
// Input指定了此给定方法所需的输入参数。
type Method struct {
// Name is the method name used for internal representation. It's derived from
// the raw name and a suffix will be added in the case of a function overload.
// Name是用于内部表示的方法名称。它源自原始名称,在函数重载的情况下会添加后缀。
// e.g.
// 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
// will be resolved as foo0.
// 第一个方法的方法名将解析为foo,而第二个方法的名称将解析为foo0。
Name string
RawName string // RawName is the raw method name parsed from ABI RawName是从ABI解析的原始方法名称
// Type indicates whether the method is a
// special fallback introduced in solidity v0.6.0
// 类型表示该方法是否是solidity v0.6.0中引入的特殊回退
Type FunctionType
// StateMutability indicates the mutability state of method,
// the default value is nonpayable. It can be empty if the abi
// is generated by legacy compiler.
// StateMutability表示方法的可变状态,默认值为nonpayable。如果abi是由遗留编译器生成的,那么它可以是空的。
StateMutability string
// Legacy indicators generated by compiler before v0.6.0 v0.6.0之前的编译器生成的旧指示符
Constant bool
Payable bool
Inputs Arguments
Outputs Arguments
str string
// Sig returns the methods string signature according to the ABI spec. Sig根据ABI规范返回方法字符串签名。
// e.g. function foo(uint32 a, int b) = "foo(uint32,int256)"
// Please note that "int" is substitute for its canonical representation "int256" 请注意,“int”可替代其规范表示“int256”
Sig string
// ID returns the canonical representation of the method's signature used by the
// abi definition to identify method names and types.
// ID返回abi定义用于标识方法名称和类型的方法签名的规范表示。
ID []byte
}
// NewMethod creates a new Method.
// NewMethod将创建一个新方法。
// A method should always be created using
// NewMethod. 应始终使用NewMethod创建方法。
// It also precomputes the sig representation and the string representation
// of the method.
// 它还预先计算方法的sig表示和字符串表示。
func NewMethod(name string, rawName string, funType FunctionType, mutability string, isConst, isPayable bool, inputs Arguments, outputs Arguments) Method {
var (
types = make([]string, len(inputs))
inputNames = make([]string, len(inputs))
outputNames = make([]string, len(outputs))
)
for i, input := range inputs {
inputNames[i] = fmt.Sprintf("%v %v", input.Type, input.Name)
types[i] = input.Type.String()
}
for i, output := range outputs {
outputNames[i] = output.Type.String()
if len(output.Name) > 0 {
outputNames[i] += fmt.Sprintf(" %v", output.Name)
}
}
// calculate the signature and method id. Note only function
// has meaningful signature and id.
// 计算签名和方法id。注意,只有函数具有有意义的签名和id。
var (
sig string
id []byte
)
if funType == Function {
sig = fmt.Sprintf("%v(%v)", rawName, strings.Join(types, ","))
id = crypto.Keccak256([]byte(sig))[:4]
}
// Extract meaningful state mutability of solidity method.
// 提取solidity方法的有意义状态可变性。
// If it's default value, never print it.
// 如果它是默认值,则永远不要打印它。
state := mutability
if state == "nonpayable" {
state = ""
}
if state != "" {
state = state + " "
}
identity := fmt.Sprintf("function %v", rawName)
if funType == Fallback {
identity = "fallback"
} else if funType == Receive {
identity = "receive"
} else if funType == Constructor {
identity = "constructor"
}
str := fmt.Sprintf("%v(%v) %sreturns(%v)", identity, strings.Join(inputNames, ", "), state, strings.Join(outputNames, ", "))
return Method{
Name: name,
RawName: rawName,
Type: funType,
StateMutability: mutability,
Constant: isConst,
Payable: isPayable,
Inputs: inputs,
Outputs: outputs,
str: str,
Sig: sig,
ID: id,
}
}
func (method Method) String() string {
return method.str
}
// IsConstant returns the indicator whether the method is read-only.
// IsConstant返回该方法是否为只读的指示符。
func (method Method) IsConstant() bool {
return method.StateMutability == "view" || method.StateMutability == "pure" || method.Constant
}
// IsPayable returns the indicator whether the method can process
// plain ether transfers.
// IsPayable返回该方法是否可以处理纯以太传输的指标。
func (method Method) IsPayable() bool {
return method.StateMutability == "payable" || method.Payable
}
【以太坊源码go-ethereum阅读】accounts/abi/method.go
猜你喜欢
转载自blog.csdn.net/qq2942713658/article/details/131097495
今日推荐
周排行