From 4b933f88a73971f74966e5f1a8eaa4aa7edaa0d3 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 11 May 2021 17:40:03 +0300 Subject: [PATCH] core: simplify interop functions We now have the only interop table (system interops). --- pkg/compiler/syscall_test.go | 6 ++---- pkg/core/blockchain.go | 2 +- pkg/core/interop/context.go | 18 ++++++++--------- pkg/core/interop/crypto/ecdsa_test.go | 2 +- pkg/core/interop/crypto/interop.go | 10 +++------- pkg/core/interops.go | 24 +++++++---------------- pkg/smartcontract/context/context_test.go | 3 +-- 7 files changed, 23 insertions(+), 42 deletions(-) diff --git a/pkg/compiler/syscall_test.go b/pkg/compiler/syscall_test.go index 863c31917..86adb3306 100644 --- a/pkg/compiler/syscall_test.go +++ b/pkg/compiler/syscall_test.go @@ -94,10 +94,8 @@ func TestSyscallExecution(t *testing.T) { ic := &interop.Context{} core.SpawnVM(ic) // set Functions field for _, fs := range ic.Functions { - for i := range fs { - // It will be set in test and we want to fail if calling invalid syscall. - fs[i].Func = nil - } + // It will be set in test and we want to fail if calling invalid syscall. + fs.Func = nil } for goName, tc := range interops { t.Run(goName, func(t *testing.T) { diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index fe9be92e5..26556f68a 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -1860,7 +1860,7 @@ func hashAndIndexToBytes(h util.Uint256, index uint32) []byte { func (bc *Blockchain) newInteropContext(trigger trigger.Type, d dao.DAO, block *block.Block, tx *transaction.Transaction) *interop.Context { ic := interop.NewContext(trigger, bc, d, bc.contracts.Management.GetContract, bc.contracts.Contracts, block, tx, bc.log) - ic.Functions = [][]interop.Function{systemInterops, neoInterops} + ic.Functions = systemInterops switch { case tx != nil: ic.Container = tx diff --git a/pkg/core/interop/context.go b/pkg/core/interop/context.go index e973a68a2..4379a0fb2 100644 --- a/pkg/core/interop/context.go +++ b/pkg/core/interop/context.go @@ -44,7 +44,7 @@ type Context struct { Notifications []state.NotificationEvent Log *zap.Logger VM *vm.VM - Functions [][]Function + Functions []Function getContract func(dao.DAO, util.Uint160) (*state.Contract, error) } @@ -64,8 +64,8 @@ func NewContext(trigger trigger.Type, bc blockchainer.Blockchainer, d dao.DAO, DAO: dao, Notifications: nes, Log: log, - // Functions is a slice of slices of interops sorted by ID. - Functions: [][]Function{}, + // Functions is a slice of interops sorted by ID. + Functions: []Function{}, getContract: getContract, } } @@ -229,13 +229,11 @@ func (ic *Context) GetContract(hash util.Uint160) (*state.Contract, error) { // GetFunction returns metadata for interop with the specified id. func (ic *Context) GetFunction(id uint32) *Function { - for _, slice := range ic.Functions { - n := sort.Search(len(slice), func(i int) bool { - return slice[i].ID >= id - }) - if n < len(slice) && slice[n].ID == id { - return &slice[n] - } + n := sort.Search(len(ic.Functions), func(i int) bool { + return ic.Functions[i].ID >= id + }) + if n < len(ic.Functions) && ic.Functions[n].ID == id { + return &ic.Functions[n] } return nil } diff --git a/pkg/core/interop/crypto/ecdsa_test.go b/pkg/core/interop/crypto/ecdsa_test.go index a8d0bf610..c5ca3ab32 100644 --- a/pkg/core/interop/crypto/ecdsa_test.go +++ b/pkg/core/interop/crypto/ecdsa_test.go @@ -71,8 +71,8 @@ func initCheckMultisigVMNoArgs(container *transaction.Transaction) *vm.VM { Network: uint32(netmode.UnitTestNet), Trigger: trigger.Verification, Container: container, + Functions: Interops, } - Register(ic) v := ic.SpawnVM() v.LoadScript(buf) return v diff --git a/pkg/core/interop/crypto/interop.go b/pkg/core/interop/crypto/interop.go index 0c969c481..93ee72e47 100644 --- a/pkg/core/interop/crypto/interop.go +++ b/pkg/core/interop/crypto/interop.go @@ -10,16 +10,12 @@ var ( neoCryptoCheckSigID = interopnames.ToID([]byte(interopnames.SystemCryptoCheckSig)) ) -var cryptoInterops = []interop.Function{ +// Interops represents sorted crypto-related interop functions. +var Interops = []interop.Function{ {ID: neoCryptoCheckMultisigID, Func: ECDSASecp256r1CheckMultisig}, {ID: neoCryptoCheckSigID, Func: ECDSASecp256r1CheckSig}, } func init() { - interop.Sort(cryptoInterops) -} - -// Register adds crypto interops to ic. -func Register(ic *interop.Context) { - ic.Functions = append(ic.Functions, cryptoInterops) + interop.Sort(Interops) } diff --git a/pkg/core/interops.go b/pkg/core/interops.go index b2dedc516..209b49f77 100644 --- a/pkg/core/interops.go +++ b/pkg/core/interops.go @@ -24,7 +24,7 @@ import ( // up for current blockchain. func SpawnVM(ic *interop.Context) *vm.VM { vm := ic.SpawnVM() - ic.Functions = [][]interop.Function{systemInterops, neoInterops} + ic.Functions = systemInterops return vm } @@ -38,6 +38,8 @@ var systemInterops = []interop.Function{ {Name: interopnames.SystemContractGetCallFlags, Func: contractGetCallFlags, Price: 1 << 10}, {Name: interopnames.SystemContractNativeOnPersist, Func: native.OnPersist, Price: 0, RequiredFlags: callflag.States}, {Name: interopnames.SystemContractNativePostPersist, Func: native.PostPersist, Price: 0, RequiredFlags: callflag.States}, + {Name: interopnames.SystemCryptoCheckMultisig, Func: crypto.ECDSASecp256r1CheckMultisig, Price: 0, ParamCount: 2}, + {Name: interopnames.SystemCryptoCheckSig, Func: crypto.ECDSASecp256r1CheckSig, Price: fee.ECDSAVerifyPrice, ParamCount: 2}, {Name: interopnames.SystemIteratorNext, Func: iterator.Next, Price: 1 << 15, ParamCount: 1}, {Name: interopnames.SystemIteratorValue, Func: iterator.Value, Price: 1 << 4, ParamCount: 1}, {Name: interopnames.SystemRuntimeBurnGas, Func: runtime.BurnGas, Price: 1 << 4, ParamCount: 1}, @@ -73,22 +75,10 @@ var systemInterops = []interop.Function{ RequiredFlags: callflag.ReadStates, ParamCount: 1}, } -var neoInterops = []interop.Function{ - {Name: interopnames.SystemCryptoCheckMultisig, Func: crypto.ECDSASecp256r1CheckMultisig, Price: 0, ParamCount: 2}, - {Name: interopnames.SystemCryptoCheckSig, Func: crypto.ECDSASecp256r1CheckSig, Price: fee.ECDSAVerifyPrice, ParamCount: 2}, -} - -// initIDinInteropsSlice initializes IDs from names in one given -// Function slice and then sorts it. -func initIDinInteropsSlice(iops []interop.Function) { - for i := range iops { - iops[i].ID = interopnames.ToID([]byte(iops[i].Name)) - } - interop.Sort(iops) -} - // init initializes IDs in the global interop slices. func init() { - initIDinInteropsSlice(systemInterops) - initIDinInteropsSlice(neoInterops) + for i := range systemInterops { + systemInterops[i].ID = interopnames.ToID([]byte(systemInterops[i].Name)) + } + interop.Sort(systemInterops) } diff --git a/pkg/smartcontract/context/context_test.go b/pkg/smartcontract/context/context_test.go index a2db41f53..fe0603671 100644 --- a/pkg/smartcontract/context/context_test.go +++ b/pkg/smartcontract/context/context_test.go @@ -118,8 +118,7 @@ func TestParameterContext_AddSignatureMultisig(t *testing.T) { } func newTestVM(w *transaction.Witness, tx *transaction.Transaction) *vm.VM { - ic := &interop.Context{Network: uint32(netmode.UnitTestNet), Container: tx} - crypto.Register(ic) + ic := &interop.Context{Network: uint32(netmode.UnitTestNet), Container: tx, Functions: crypto.Interops} v := ic.SpawnVM() v.LoadScript(w.VerificationScript) v.LoadScript(w.InvocationScript)