Skip to content

Commit

Permalink
native: autogenerate nativehashes package
Browse files Browse the repository at this point in the history
Ref.
#3402 (comment).

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
  • Loading branch information
AnnaShaleva committed May 14, 2024
1 parent 02ecbeb commit 0aecdde
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 43 deletions.
2 changes: 1 addition & 1 deletion pkg/compiler/interop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/blockchain_neotest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pkg/core/native/native_test/management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand All @@ -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{},
Expand All @@ -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)),
Expand All @@ -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)),
Expand Down
70 changes: 70 additions & 0 deletions pkg/core/native/nativehashes/gen.go
Original file line number Diff line number Diff line change
@@ -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)
}
54 changes: 26 additions & 28 deletions pkg/core/native/nativehashes/hashes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/rpcclient/gas/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/rpcclient/management/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/rpcclient/neo/neo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/rpcclient/oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion pkg/rpcclient/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion pkg/rpcclient/rolemgmt/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
4 changes: 2 additions & 2 deletions pkg/services/oracle/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
{
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion pkg/services/rpcsrv/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0aecdde

Please sign in to comment.