From 0126bd209392db7cc4f09d01c92f715162b3cef9 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Thu, 25 Apr 2024 14:20:12 +0300 Subject: [PATCH] native: autogenerate nativehashes package Ref. https://github.com/nspcc-dev/neo-go/pull/3402#discussion_r1577879141. Signed-off-by: Anna Shaleva --- pkg/compiler/interop_test.go | 2 +- pkg/core/blockchain_neotest_test.go | 2 +- .../native/native_test/management_test.go | 8 +-- pkg/core/native/nativehashes/gen.go | 70 +++++++++++++++++++ pkg/core/native/nativehashes/hashes.go | 54 +++++++------- pkg/rpcclient/gas/gas.go | 2 +- pkg/rpcclient/management/management.go | 2 +- pkg/rpcclient/neo/neo.go | 2 +- pkg/rpcclient/oracle/oracle.go | 2 +- pkg/rpcclient/policy/policy.go | 2 +- pkg/rpcclient/rolemgmt/roles.go | 2 +- pkg/services/oracle/response.go | 4 +- pkg/services/rpcsrv/client_test.go | 2 +- 13 files changed, 111 insertions(+), 43 deletions(-) create mode 100644 pkg/core/native/nativehashes/gen.go diff --git a/pkg/compiler/interop_test.go b/pkg/compiler/interop_test.go index 449d45ccb2..da46e78604 100644 --- a/pkg/compiler/interop_test.go +++ b/pkg/compiler/interop_test.go @@ -610,7 +610,7 @@ func TestCallWithVersion(t *testing.T) { e.DeployContract(t, ctr, nil) c := e.CommitteeInvoker(ctr.Hash) - policyH := nativehashes.Policy + policyH := nativehashes.PolicyContract t.Run("good", func(t *testing.T) { c.Invoke(t, e.Chain.GetBaseExecFee(), "callWithVersion", policyH.BytesBE(), 0, "getExecFeeFactor") }) diff --git a/pkg/core/blockchain_neotest_test.go b/pkg/core/blockchain_neotest_test.go index 1ecd0baeb3..bec3fa91e9 100644 --- a/pkg/core/blockchain_neotest_test.go +++ b/pkg/core/blockchain_neotest_test.go @@ -2461,7 +2461,7 @@ func TestBlockchain_GenesisTransactionExtension(t *testing.T) { emit.Syscall(script.BinWriter, interopnames.SystemRuntimeCheckWitness) emit.Bytes(script.BinWriter, to.BytesBE()) emit.Syscall(script.BinWriter, interopnames.SystemRuntimeCheckWitness) - emit.AppCall(script.BinWriter, nativehashes.Neo, "transfer", callflag.All, from, to, amount, nil) + emit.AppCall(script.BinWriter, nativehashes.NeoToken, "transfer", callflag.All, from, to, amount, nil) emit.Opcodes(script.BinWriter, opcode.ASSERT) var sysFee int64 = 1_0000_0000 diff --git a/pkg/core/native/native_test/management_test.go b/pkg/core/native/native_test/management_test.go index 6bba730fb6..ec3fda1e39 100644 --- a/pkg/core/native/native_test/management_test.go +++ b/pkg/core/native/native_test/management_test.go @@ -171,7 +171,7 @@ func TestManagement_NativeDeployUpdateNotifications(t *testing.T) { switch name { case nativenames.Gas: expected = append(expected, state.NotificationEvent{ - ScriptHash: nativehashes.Gas, + ScriptHash: nativehashes.GasToken, Name: "Transfer", Item: stackitem.NewArray([]stackitem.Item{ stackitem.Null{}, @@ -181,7 +181,7 @@ func TestManagement_NativeDeployUpdateNotifications(t *testing.T) { }) case nativenames.Neo: expected = append(expected, state.NotificationEvent{ - ScriptHash: nativehashes.Neo, + ScriptHash: nativehashes.NeoToken, Name: "Transfer", Item: stackitem.NewArray([]stackitem.Item{ stackitem.Null{}, @@ -191,7 +191,7 @@ func TestManagement_NativeDeployUpdateNotifications(t *testing.T) { }) } expected = append(expected, state.NotificationEvent{ - ScriptHash: nativehashes.Management, + ScriptHash: nativehashes.ContractManagement, Name: "Deploy", Item: stackitem.NewArray([]stackitem.Item{ stackitem.Make(state.CreateNativeContractHash(name)), @@ -208,7 +208,7 @@ func TestManagement_NativeDeployUpdateNotifications(t *testing.T) { expected = expected[:0] for _, name := range []string{nativenames.CryptoLib, nativenames.Neo} { expected = append(expected, state.NotificationEvent{ - ScriptHash: nativehashes.Management, + ScriptHash: nativehashes.ContractManagement, Name: "Update", Item: stackitem.NewArray([]stackitem.Item{ stackitem.Make(state.CreateNativeContractHash(name)), diff --git a/pkg/core/native/nativehashes/gen.go b/pkg/core/native/nativehashes/gen.go new file mode 100644 index 0000000000..3609385dee --- /dev/null +++ b/pkg/core/native/nativehashes/gen.go @@ -0,0 +1,70 @@ +//go:build ignore + +// This program generates hashes.go. It can be invoked by running +// go generate. +package main + +import ( + "fmt" + "log" + "os" + "text/template" + + "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" + "github.com/nspcc-dev/neo-go/pkg/core/state" +) + +// srcTmpl is a nativehashes package template. +const srcTmpl = `// Code generated by "go generate go run gen.go"; DO NOT EDIT. + +//go:generate go run gen.go + +// package nativehashes contains hashes of all native contracts in their LE and Uint160 representation. +package nativehashes + +import "github.com/nspcc-dev/neo-go/pkg/util" + +// Hashes of all native contracts. +var ( +{{- range .Natives }} + // {{ .Name }} is a hash of native {{ .Name }} contract. + {{ .Name }} = {{ .Hash }} +{{- end }} +) +` + +type ( + // Config contains parameters for the nativehashes package generation. + Config struct { + Natives []NativeInfo + } + + // NativeInfo contains information about native contract needed for + // nativehashes package generation. + NativeInfo struct { + Name string + Hash string + } +) + +// srcTemplate is a parsed nativehashes package template. +var srcTemplate = template.Must(template.New("nativehashes").Parse(srcTmpl)) + +func main() { + f, err := os.Create("hashes.go") + if err != nil { + log.Fatal(err) + } + defer f.Close() + + var cfg = Config{Natives: make([]NativeInfo, len(nativenames.All))} + for i, name := range nativenames.All { + var hash = state.CreateNativeContractHash(name) + cfg.Natives[i] = NativeInfo{ + Name: name, + Hash: fmt.Sprintf("%#v", hash), + } + } + + srcTemplate.Execute(f, cfg) +} diff --git a/pkg/core/native/nativehashes/hashes.go b/pkg/core/native/nativehashes/hashes.go index 646da83680..40d57bdc3f 100644 --- a/pkg/core/native/nativehashes/hashes.go +++ b/pkg/core/native/nativehashes/hashes.go @@ -1,34 +1,32 @@ +// Code generated by "go generate go run gen.go"; DO NOT EDIT. + +//go:generate go run gen.go + +// package nativehashes contains hashes of all native contracts in their LE and Uint160 representation. package nativehashes -import ( - "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" - "github.com/nspcc-dev/neo-go/pkg/core/state" - "github.com/nspcc-dev/neo-go/pkg/util" -) +import "github.com/nspcc-dev/neo-go/pkg/util" // Hashes of all native contracts. var ( - Management util.Uint160 - Ledger util.Uint160 - Neo util.Uint160 - Gas util.Uint160 - Policy util.Uint160 - Oracle util.Uint160 - Designation util.Uint160 - Notary util.Uint160 - CryptoLib util.Uint160 - StdLib util.Uint160 + // ContractManagement is a hash of native ContractManagement contract. + ContractManagement = util.Uint160{0xfd, 0xa3, 0xfa, 0x43, 0x46, 0xea, 0x53, 0x2a, 0x25, 0x8f, 0xc4, 0x97, 0xdd, 0xad, 0xdb, 0x64, 0x37, 0xc9, 0xfd, 0xff} + // StdLib is a hash of native StdLib contract. + StdLib = util.Uint160{0xc0, 0xef, 0x39, 0xce, 0xe0, 0xe4, 0xe9, 0x25, 0xc6, 0xc2, 0xa0, 0x6a, 0x79, 0xe1, 0x44, 0xd, 0xd8, 0x6f, 0xce, 0xac} + // CryptoLib is a hash of native CryptoLib contract. + CryptoLib = util.Uint160{0x1b, 0xf5, 0x75, 0xab, 0x11, 0x89, 0x68, 0x84, 0x13, 0x61, 0xa, 0x35, 0xa1, 0x28, 0x86, 0xcd, 0xe0, 0xb6, 0x6c, 0x72} + // LedgerContract is a hash of native LedgerContract contract. + LedgerContract = util.Uint160{0xbe, 0xf2, 0x4, 0x31, 0x40, 0x36, 0x2a, 0x77, 0xc1, 0x50, 0x99, 0xc7, 0xe6, 0x4c, 0x12, 0xf7, 0x0, 0xb6, 0x65, 0xda} + // NeoToken is a hash of native NeoToken contract. + NeoToken = util.Uint160{0xf5, 0x63, 0xea, 0x40, 0xbc, 0x28, 0x3d, 0x4d, 0xe, 0x5, 0xc4, 0x8e, 0xa3, 0x5, 0xb3, 0xf2, 0xa0, 0x73, 0x40, 0xef} + // GasToken is a hash of native GasToken contract. + GasToken = util.Uint160{0xcf, 0x76, 0xe2, 0x8b, 0xd0, 0x6, 0x2c, 0x4a, 0x47, 0x8e, 0xe3, 0x55, 0x61, 0x1, 0x13, 0x19, 0xf3, 0xcf, 0xa4, 0xd2} + // PolicyContract is a hash of native PolicyContract contract. + PolicyContract = util.Uint160{0x7b, 0xc6, 0x81, 0xc0, 0xa1, 0xf7, 0x1d, 0x54, 0x34, 0x57, 0xb6, 0x8b, 0xba, 0x8d, 0x5f, 0x9f, 0xdd, 0x4e, 0x5e, 0xcc} + // RoleManagement is a hash of native RoleManagement contract. + RoleManagement = util.Uint160{0xe2, 0x95, 0xe3, 0x91, 0x54, 0x4c, 0x17, 0x8a, 0xd9, 0x4f, 0x3, 0xec, 0x4d, 0xcd, 0xff, 0x78, 0x53, 0x4e, 0xcf, 0x49} + // OracleContract is a hash of native OracleContract contract. + OracleContract = util.Uint160{0x58, 0x87, 0x17, 0x11, 0x7e, 0xa, 0xa8, 0x10, 0x72, 0xaf, 0xab, 0x71, 0xd2, 0xdd, 0x89, 0xfe, 0x7c, 0x4b, 0x92, 0xfe} + // Notary is a hash of native Notary contract. + Notary = util.Uint160{0x3b, 0xec, 0x35, 0x31, 0x11, 0x9b, 0xba, 0xd7, 0x6d, 0xd0, 0x44, 0x92, 0xb, 0xd, 0xe6, 0xc3, 0x19, 0x4f, 0xe1, 0xc1} ) - -func init() { - Management = state.CreateNativeContractHash(nativenames.Management) - Ledger = state.CreateNativeContractHash(nativenames.Ledger) - Neo = state.CreateNativeContractHash(nativenames.Neo) - Gas = state.CreateNativeContractHash(nativenames.Gas) - Policy = state.CreateNativeContractHash(nativenames.Policy) - Oracle = state.CreateNativeContractHash(nativenames.Oracle) - Designation = state.CreateNativeContractHash(nativenames.Designation) - Notary = state.CreateNativeContractHash(nativenames.Notary) - CryptoLib = state.CreateNativeContractHash(nativenames.CryptoLib) - StdLib = state.CreateNativeContractHash(nativenames.StdLib) -} diff --git a/pkg/rpcclient/gas/gas.go b/pkg/rpcclient/gas/gas.go index 16f1bb53d6..c5dda22446 100644 --- a/pkg/rpcclient/gas/gas.go +++ b/pkg/rpcclient/gas/gas.go @@ -13,7 +13,7 @@ import ( ) // Hash stores the hash of the native GAS contract. -var Hash = nativehashes.Gas +var Hash = nativehashes.GasToken // NewReader creates a NEP-17 reader for the GAS contract. func NewReader(invoker nep17.Invoker) *nep17.TokenReader { diff --git a/pkg/rpcclient/management/management.go b/pkg/rpcclient/management/management.go index 5cc6691fe9..9e891562fb 100644 --- a/pkg/rpcclient/management/management.go +++ b/pkg/rpcclient/management/management.go @@ -75,7 +75,7 @@ type HashesIterator struct { } // Hash stores the hash of the native ContractManagement contract. -var Hash = nativehashes.Management +var Hash = nativehashes.ContractManagement // Event is the event emitted on contract deployment/update/destroy. // Even though these events are different they all have the same field inside. diff --git a/pkg/rpcclient/neo/neo.go b/pkg/rpcclient/neo/neo.go index 4d8a54aab4..872cf67a7b 100644 --- a/pkg/rpcclient/neo/neo.go +++ b/pkg/rpcclient/neo/neo.go @@ -97,7 +97,7 @@ type ValidatorIterator struct { } // Hash stores the hash of the native NEOToken contract. -var Hash = nativehashes.Neo +var Hash = nativehashes.NeoToken // NewReader creates an instance of ContractReader to get data from the NEO // contract. diff --git a/pkg/rpcclient/oracle/oracle.go b/pkg/rpcclient/oracle/oracle.go index b3727f36e4..d658fda198 100644 --- a/pkg/rpcclient/oracle/oracle.go +++ b/pkg/rpcclient/oracle/oracle.go @@ -31,7 +31,7 @@ type Actor interface { } // Hash stores the hash of the native OracleContract contract. -var Hash = nativehashes.Oracle +var Hash = nativehashes.OracleContract const priceSetter = "setPrice" diff --git a/pkg/rpcclient/policy/policy.go b/pkg/rpcclient/policy/policy.go index b2bb59f0a1..8ed642c845 100644 --- a/pkg/rpcclient/policy/policy.go +++ b/pkg/rpcclient/policy/policy.go @@ -33,7 +33,7 @@ type Actor interface { } // Hash stores the hash of the native PolicyContract contract. -var Hash = nativehashes.Policy +var Hash = nativehashes.PolicyContract const ( execFeeSetter = "setExecFeeFactor" diff --git a/pkg/rpcclient/rolemgmt/roles.go b/pkg/rpcclient/rolemgmt/roles.go index 770665c8de..e647952ff1 100644 --- a/pkg/rpcclient/rolemgmt/roles.go +++ b/pkg/rpcclient/rolemgmt/roles.go @@ -31,7 +31,7 @@ type Actor interface { } // Hash stores the hash of the native RoleManagement contract. -var Hash = nativehashes.Designation +var Hash = nativehashes.RoleManagement const designateMethod = "designateAsRole" diff --git a/pkg/services/oracle/response.go b/pkg/services/oracle/response.go index b80520f1c2..6dbf6892ea 100644 --- a/pkg/services/oracle/response.go +++ b/pkg/services/oracle/response.go @@ -119,7 +119,7 @@ func (o *Oracle) CreateResponseTx(gasForResponse int64, vub uint32, resp *transa oracleSignContract := o.getOracleSignContract() tx.Signers = []transaction.Signer{ { - Account: nativehashes.Oracle, + Account: nativehashes.OracleContract, Scopes: transaction.None, }, { @@ -174,7 +174,7 @@ func (o *Oracle) testVerify(tx *transaction.Transaction) (int64, bool, error) { ic.VM.GasLimit = o.Chain.GetMaxVerificationGAS() o.oracleInfoLock.RLock() - ic.VM.LoadScriptWithHash(o.oracleScript, nativehashes.Oracle, callflag.ReadOnly) + ic.VM.LoadScriptWithHash(o.oracleScript, nativehashes.OracleContract, callflag.ReadOnly) ic.VM.Context().Jump(o.verifyOffset) o.oracleInfoLock.RUnlock() diff --git a/pkg/services/rpcsrv/client_test.go b/pkg/services/rpcsrv/client_test.go index 7b47859f31..98e874567f 100644 --- a/pkg/services/rpcsrv/client_test.go +++ b/pkg/services/rpcsrv/client_test.go @@ -826,7 +826,7 @@ func TestCalculateNetworkFee(t *testing.T) { require.NoError(t, err) // Set fee per Conflicts attribute. - script, err := smartcontract.CreateCallScript(nativehashes.Policy, "setAttributeFee", byte(transaction.ConflictsT), conflictsFee) + script, err := smartcontract.CreateCallScript(nativehashes.PolicyContract, "setAttributeFee", byte(transaction.ConflictsT), conflictsFee) require.NoError(t, err) txSetFee := transaction.New(script, 1_0000_0000) txSetFee.ValidUntilBlock = chain.BlockHeight() + 1