-
Notifications
You must be signed in to change notification settings - Fork 276
Stateful Precompile Improvements #389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
2d47fde
557d1f4
77ce3c3
e3b7ff3
9e5c8c4
8868a99
357d9e6
1add41a
6770536
7f4c66b
aa152c9
77751e4
ce23fca
4bd2442
f806ad4
ea3c13b
39e16d8
9a63a87
c9a5811
9868f4e
81fb4a9
d518651
c81b44b
7f13c45
aa6842d
8ca09a3
1b995e2
e8b55cf
c5c1017
9059a09
9a8a85d
c004080
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,8 @@ const ( | |
| readAllowListFuncKey = "readAllowList" | ||
| ) | ||
|
|
||
| type BindHook func(lang Lang, pkg string, types []string, contracts map[string]*tmplContract, structs map[string]*tmplStruct) (data interface{}, templateSource string, err error) | ||
|
|
||
| // Lang is a target programming language selector to generate bindings for. | ||
| type Lang int | ||
|
|
||
|
|
@@ -101,7 +103,11 @@ func isKeyWord(arg string) bool { | |
| // to be used as is in client code, but rather as an intermediate struct which | ||
| // enforces compile time type safety and naming convention opposed to having to | ||
| // manually maintain hard coded strings that break on runtime. | ||
| func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string, isPrecompile bool) (string, error) { | ||
| func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string) (string, error) { | ||
| return bindHelper(types, abis, bytecodes, fsigs, pkg, lang, libs, aliases, nil) | ||
| } | ||
|
|
||
| func bindHelper(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string, bindHook BindHook) (string, error) { | ||
| var ( | ||
| // contracts is the map of each individual contract requested binding | ||
| contracts = make(map[string]*tmplContract) | ||
|
|
@@ -155,7 +161,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string] | |
| normalizedName := methodNormalizer[lang](alias(aliases, original.Name)) | ||
|
|
||
| // Ensure there is no duplicated identifier | ||
| var identifiers = callIdentifiers | ||
| identifiers := callIdentifiers | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: undo unneeded change |
||
| if !original.IsConstant() { | ||
| identifiers = transactIdentifiers | ||
| } | ||
|
|
@@ -178,11 +184,6 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string] | |
| normalized.Outputs = make([]abi.Argument, len(original.Outputs)) | ||
| copy(normalized.Outputs, original.Outputs) | ||
| for j, output := range normalized.Outputs { | ||
| if isPrecompile { | ||
| if output.Name == "" { | ||
| return "", fmt.Errorf("ABI outputs for %s require a name to generate the precompile binding, re-generate the ABI from a Solidity source file with all named outputs", normalized.Name) | ||
| } | ||
| } | ||
| if output.Name != "" { | ||
| normalized.Outputs[j].Name = capitalise(output.Name) | ||
| } | ||
|
|
@@ -291,19 +292,14 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string] | |
| templateSource string | ||
| ) | ||
|
|
||
| // Generate the contract template data according to contract type (precompile/non) | ||
| if isPrecompile { | ||
| if lang != LangGo { | ||
| return "", errors.New("only GoLang binding for precompiled contracts is supported yet") | ||
| } | ||
|
|
||
| if len(contracts) != 1 { | ||
| return "", errors.New("cannot generate more than 1 contract") | ||
| // Generate the contract template data according to hook | ||
| if bindHook != nil { | ||
| var err error | ||
| data, templateSource, err = bindHook(lang, pkg, types, contracts, structs) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| precompileType := types[0] | ||
| firstContract := contracts[precompileType] | ||
| data, templateSource = createPrecompileDataAndTemplate(firstContract, structs) | ||
| } else { | ||
| } else { // default to generate contract binding | ||
| templateSource = tmplSource[lang] | ||
| data = &tmplData{ | ||
| Package: pkg, | ||
|
|
@@ -710,45 +706,3 @@ func hasStruct(t abi.Type) bool { | |
| return false | ||
| } | ||
| } | ||
|
|
||
| func createPrecompileDataAndTemplate(contract *tmplContract, structs map[string]*tmplStruct) (interface{}, string) { | ||
| funcs := make(map[string]*tmplMethod) | ||
|
|
||
| for k, v := range contract.Transacts { | ||
| funcs[k] = v | ||
| } | ||
|
|
||
| for k, v := range contract.Calls { | ||
| funcs[k] = v | ||
| } | ||
| isAllowList := allowListEnabled(funcs) | ||
| if isAllowList { | ||
| // remove these functions as we will directly inherit AllowList | ||
| delete(funcs, readAllowListFuncKey) | ||
| delete(funcs, setAdminFuncKey) | ||
| delete(funcs, setEnabledFuncKey) | ||
| delete(funcs, setNoneFuncKey) | ||
| } | ||
|
|
||
| precompileContract := &tmplPrecompileContract{ | ||
| tmplContract: contract, | ||
| AllowList: isAllowList, | ||
| Funcs: funcs, | ||
| } | ||
|
|
||
| data := &tmplPrecompileData{ | ||
| Contract: precompileContract, | ||
| Structs: structs, | ||
| } | ||
| return data, tmplSourcePrecompileGo | ||
| } | ||
|
|
||
| func allowListEnabled(funcs map[string]*tmplMethod) bool { | ||
| keys := []string{readAllowListFuncKey, setAdminFuncKey, setEnabledFuncKey, setNoneFuncKey} | ||
| for _, key := range keys { | ||
| if _, ok := funcs[key]; !ok { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // (c) 2019-2020, Ava Labs, Inc. | ||
| // | ||
| // This file is a derived work, based on the go-ethereum library whose original | ||
| // notices appear below. | ||
| // | ||
| // It is distributed under a license compatible with the licensing terms of the | ||
| // original code from which it is derived. | ||
| // | ||
| // Much love to the original authors for their work. | ||
| // ********** | ||
| // Copyright 2016 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 bind generates Ethereum contract Go bindings. | ||
| // | ||
| // Detailed usage document and tutorial available on the go-ethereum Wiki page: | ||
| // https://github.com/ethereum/go-ethereum/wiki/Native-DApps:-Go-bindings-to-Ethereum-contracts | ||
| package bind | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // PrecompileBind generates a Go binding for a precompiled contract. It returns config binding and contract binding. | ||
| func PrecompileBind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string, abifilename string) (string, string, error) { | ||
|
||
| // create hooks | ||
| configHook := createPrecompileHook(abifilename, tmplSourcePrecompileConfigGo) | ||
| contractHook := createPrecompileHook(abifilename, tmplSourcePrecompileContractGo) | ||
|
|
||
| configBind, err := bindHelper(types, abis, bytecodes, fsigs, pkg, lang, libs, aliases, configHook) | ||
| if err != nil { | ||
| return "", "", err | ||
| } | ||
| contractBind, err := bindHelper(types, abis, bytecodes, fsigs, pkg, lang, libs, aliases, contractHook) | ||
|
|
||
| return configBind, contractBind, err | ||
| } | ||
|
|
||
| func createPrecompileHook(abifilename string, template string) BindHook { | ||
|
||
| var bindHook BindHook = func(lang Lang, pkg string, types []string, contracts map[string]*tmplContract, structs map[string]*tmplStruct) (interface{}, string, error) { | ||
| // verify first | ||
| if lang != LangGo { | ||
| return nil, "", errors.New("only GoLang binding for precompiled contracts is supported yet") | ||
| } | ||
|
|
||
| if len(types) != 1 { | ||
| return nil, "", errors.New("cannot generate more than 1 contract") | ||
| } | ||
| funcs := make(map[string]*tmplMethod) | ||
|
|
||
| contract := contracts[types[0]] | ||
|
|
||
| for k, v := range contract.Transacts { | ||
| if err := checkOutputName(*v); err != nil { | ||
| return nil, "", err | ||
| } | ||
| funcs[k] = v | ||
| } | ||
|
|
||
| for k, v := range contract.Calls { | ||
| if err := checkOutputName(*v); err != nil { | ||
| return nil, "", err | ||
| } | ||
| funcs[k] = v | ||
| } | ||
| isAllowList := allowListEnabled(funcs) | ||
| if isAllowList { | ||
| // remove these functions as we will directly inherit AllowList | ||
|
||
| delete(funcs, readAllowListFuncKey) | ||
| delete(funcs, setAdminFuncKey) | ||
| delete(funcs, setEnabledFuncKey) | ||
| delete(funcs, setNoneFuncKey) | ||
| } | ||
|
|
||
| precompileContract := &tmplPrecompileContract{ | ||
| tmplContract: contract, | ||
| AllowList: isAllowList, | ||
| Funcs: funcs, | ||
| ABIFilename: abifilename, | ||
| } | ||
|
|
||
| data := &tmplPrecompileData{ | ||
| Contract: precompileContract, | ||
| Structs: structs, | ||
| Package: pkg, | ||
| } | ||
| return data, template, nil | ||
| } | ||
| return bindHook | ||
| } | ||
|
|
||
| func allowListEnabled(funcs map[string]*tmplMethod) bool { | ||
| keys := []string{readAllowListFuncKey, setAdminFuncKey, setEnabledFuncKey, setNoneFuncKey} | ||
| for _, key := range keys { | ||
| if _, ok := funcs[key]; !ok { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func checkOutputName(method tmplMethod) error { | ||
| for _, output := range method.Original.Outputs { | ||
| if output.Name == "" { | ||
| return fmt.Errorf("ABI outputs for %s require a name to generate the precompile binding, re-generate the ABI from a Solidity source file with all named outputs", method.Original.Name) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have a comment to describe the Bindhook?