Skip to content
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

Update System.Contract.Call #1647

Merged
merged 3 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cli/smartcontract/smart_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
"github.com/nspcc-dev/neo-go/pkg/util"
Expand Down Expand Up @@ -821,7 +822,9 @@ func contractDeploy(ctx *cli.Context) error {
return cli.NewExitError(fmt.Errorf("failed to get management contract's hash: %w", err), 1)
}
buf := io.NewBufBinWriter()
emit.AppCallWithOperationAndArgs(buf.BinWriter, mgmtHash, "deploy", f, manifestBytes)
emit.AppCall(buf.BinWriter, mgmtHash, "deploy",
callflag.ReadStates|callflag.WriteStates|callflag.AllowNotify,
f, manifestBytes)
if buf.Err != nil {
return cli.NewExitError(fmt.Errorf("failed to create deployment script: %w", buf.Err), 1)
}
Expand Down
Binary file modified cli/testdata/chain50x2.acc
Binary file not shown.
2 changes: 1 addition & 1 deletion cli/testdata/deploy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Fail() {
func Update(script, manifest []byte) {
ctx := storage.GetReadOnlyContext()
mgmt := storage.Get(ctx, mgmtKey).(interop.Hash160)
contract.Call(mgmt, "update", script, manifest)
contract.Call(mgmt, "update", contract.All, script, manifest)
}

// GetValue returns stored value.
Expand Down
5 changes: 3 additions & 2 deletions cli/wallet/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
Expand Down Expand Up @@ -105,7 +106,7 @@ func handleCandidate(ctx *cli.Context, method string) error {
return err
}
w := io.NewBufBinWriter()
emit.AppCallWithOperationAndArgs(w.BinWriter, neoContractHash, method, acc.PrivateKey().PublicKey().Bytes())
emit.AppCall(w.BinWriter, neoContractHash, method, callflag.WriteStates, acc.PrivateKey().PublicKey().Bytes())
emit.Opcodes(w.BinWriter, opcode.ASSERT)
tx, err := c.CreateTxFromScript(w.Bytes(), acc, -1, int64(gas), transaction.Signer{
Account: acc.Contract.ScriptHash(),
Expand Down Expand Up @@ -167,7 +168,7 @@ func handleVote(ctx *cli.Context) error {
return cli.NewExitError(err, 1)
}
w := io.NewBufBinWriter()
emit.AppCallWithOperationAndArgs(w.BinWriter, neoContractHash, "vote", addr.BytesBE(), pubArg)
emit.AppCall(w.BinWriter, neoContractHash, "vote", callflag.WriteStates, addr.BytesBE(), pubArg)
emit.Opcodes(w.BinWriter, opcode.ASSERT)

tx, err := c.CreateTxFromScript(w.Bytes(), acc, -1, int64(gas), transaction.Signer{
Expand Down
6 changes: 3 additions & 3 deletions examples/timer/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Migrate(script []byte, manifest []byte) bool {
return false
}
mgmt := storage.Get(ctx, mgmtKey).(interop.Hash160)
contract.Call(mgmt, "update", script, manifest)
contract.Call(mgmt, "update", contract.All, script, manifest)
runtime.Log("Contract updated.")
return true
}
Expand All @@ -58,7 +58,7 @@ func Tick() bool {
ticksLeft = ticksLeft.(int) - 1
if ticksLeft == 0 {
runtime.Log("Fired!")
return contract.Call(runtime.GetExecutingScriptHash(), "selfDestroy").(bool)
return contract.Call(runtime.GetExecutingScriptHash(), "selfDestroy", contract.All).(bool)
}
storage.Put(ctx, ticksKey, ticksLeft)
i := binary.Itoa(ticksLeft.(int), 10)
Expand All @@ -73,7 +73,7 @@ func SelfDestroy() bool {
return false
}
mgmt := storage.Get(ctx, mgmtKey).(interop.Hash160)
contract.Call(mgmt, "destroy")
contract.Call(mgmt, "destroy", contract.All)
runtime.Log("Destroyed.")
return true
}
5 changes: 3 additions & 2 deletions internal/testchain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
Expand All @@ -28,7 +29,7 @@ var (
func NewTransferFromOwner(bc blockchainer.Blockchainer, contractHash, to util.Uint160, amount int64,
nonce, validUntil uint32) (*transaction.Transaction, error) {
w := io.NewBufBinWriter()
emit.AppCallWithOperationAndArgs(w.BinWriter, contractHash, "transfer", ownerHash, to, amount, nil)
emit.AppCall(w.BinWriter, contractHash, "transfer", callflag.All, ownerHash, to, amount, nil)
emit.Opcodes(w.BinWriter, opcode.ASSERT)
if w.Err != nil {
return nil, w.Err
Expand Down Expand Up @@ -76,7 +77,7 @@ func NewDeployTx(bc blockchainer.Blockchainer, name string, sender util.Uint160,
return nil, util.Uint160{}, err
}
buf := io.NewBufBinWriter()
emit.AppCallWithOperationAndArgs(buf.BinWriter, bc.ManagementContractHash(), "deploy", neb, rawManifest)
emit.AppCall(buf.BinWriter, bc.ManagementContractHash(), "deploy", callflag.All, neb, rawManifest)
if buf.Err != nil {
return nil, util.Uint160{}, buf.Err
}
Expand Down
9 changes: 1 addition & 8 deletions pkg/compiler/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -866,14 +866,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
emit.Opcodes(c.prog.BinWriter, opcode.PACK)
numArgs -= varSize - 1
}
// CallFlag in CallEx interop should be the last argument
// but this can't be reflected in signature due to varargs.
// It is first in compiler interop though, thus we just need to reverse 1 values less.
if f != nil && isSyscall(f) && f.pkg.Name() == "contract" && f.name == "CallEx" {
c.emitReverse(numArgs - 1)
} else {
c.emitReverse(numArgs)
}
c.emitReverse(numArgs)
}

// Check builtin first to avoid nil pointer on funcScope!
Expand Down
18 changes: 9 additions & 9 deletions pkg/compiler/interop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
cinterop "github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util"
Expand Down Expand Up @@ -120,7 +120,7 @@ func spawnVM(t *testing.T, ic *interop.Context, src string) *vm.VM {
require.NoError(t, err)
v := core.SpawnVM(ic)
invokeMethod(t, testMainIdent, b, v, di)
v.LoadScriptWithFlags(b, smartcontract.All)
v.LoadScriptWithFlags(b, callflag.All)
return v
}

Expand Down Expand Up @@ -150,7 +150,7 @@ func TestAppCall(t *testing.T) {
return a + n
}
func CallInner() int {
return contract.Call(%s, "get42").(int)
return contract.Call(%s, "get42", contract.All).(int)
}`
srcInner = fmt.Sprintf(srcInner,
fmt.Sprintf("%#v", cinterop.Hash160(barH.BytesBE())))
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestAppCall(t *testing.T) {
func Main() []byte {
x := []byte{1, 2}
y := []byte{3, 4}
result := contract.Call([]byte(scriptHash), "append", x, y)
result := contract.Call([]byte(scriptHash), "append", contract.All, x, y)
return result.([]byte)
}
`
Expand All @@ -241,7 +241,7 @@ func TestAppCall(t *testing.T) {
x := []byte{1, 2}
y := []byte{3, 4}
var addr = []byte(` + fmt.Sprintf("%#v", string(ih.BytesBE())) + `)
result := contract.Call(addr, "append", x, y)
result := contract.Call(addr, "append", contract.All, x, y)
return result.([]byte)
}
`
Expand All @@ -257,7 +257,7 @@ func TestAppCall(t *testing.T) {
import "github.com/nspcc-dev/neo-go/pkg/interop/contract"
func Main() int {
var addr = []byte(` + fmt.Sprintf("%#v", string(ih.BytesBE())) + `)
result := contract.Call(addr, "add3", 39)
result := contract.Call(addr, "add3", contract.All, 39)
return result.(int)
}`

Expand All @@ -272,7 +272,7 @@ func TestAppCall(t *testing.T) {
import ee "github.com/nspcc-dev/neo-go/pkg/interop/contract"
func Main() int {
var addr = []byte(` + fmt.Sprintf("%#v", string(ih.BytesBE())) + `)
result := ee.Call(addr, "add3", 39)
result := ee.Call(addr, "add3", ee.All, 39)
return result.(int)
}`
v := spawnVM(t, ic, src)
Expand All @@ -288,7 +288,7 @@ func getAppCallScript(h string) string {
func Main() []byte {
x := []byte{1, 2}
y := []byte{3, 4}
result := contract.Call(` + h + `, "append", x, y)
result := contract.Call(` + h + `, "append", contract.All, x, y)
return result.([]byte)
}
`
Expand All @@ -298,7 +298,7 @@ func getCallExScript(h string, flags string) string {
return `package foo
import "github.com/nspcc-dev/neo-go/pkg/interop/contract"
func Main() int {
result := contract.CallEx(` + flags + `, ` + h + `, "callInner")
result := contract.Call(` + h + `, "callInner", ` + flags + `)
return result.(int)
}`
}
Expand Down
1 change: 0 additions & 1 deletion pkg/compiler/syscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var syscalls = map[string]map[string]string{
},
"contract": {
"Call": interopnames.SystemContractCall,
"CallEx": interopnames.SystemContractCallEx,
"CreateStandardAccount": interopnames.SystemContractCreateStandardAccount,
"IsStandard": interopnames.SystemContractIsStandard,
"GetCallFlags": interopnames.SystemContractGetCallFlags,
Expand Down
18 changes: 9 additions & 9 deletions pkg/compiler/syscall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import (
"testing"

"github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// Checks that changes in `smartcontract` are reflected in compiler interop package.
func TestCallFlags(t *testing.T) {
require.EqualValues(t, contract.ReadStates, smartcontract.ReadStates)
require.EqualValues(t, contract.WriteStates, smartcontract.WriteStates)
require.EqualValues(t, contract.AllowCall, smartcontract.AllowCall)
require.EqualValues(t, contract.AllowNotify, smartcontract.AllowNotify)
require.EqualValues(t, contract.States, smartcontract.States)
require.EqualValues(t, contract.ReadOnly, smartcontract.ReadOnly)
require.EqualValues(t, contract.All, smartcontract.All)
require.EqualValues(t, contract.NoneFlag, smartcontract.NoneFlag)
require.EqualValues(t, contract.ReadStates, callflag.ReadStates)
require.EqualValues(t, contract.WriteStates, callflag.WriteStates)
require.EqualValues(t, contract.AllowCall, callflag.AllowCall)
require.EqualValues(t, contract.AllowNotify, callflag.AllowNotify)
require.EqualValues(t, contract.States, callflag.States)
require.EqualValues(t, contract.ReadOnly, callflag.ReadOnly)
require.EqualValues(t, contract.All, callflag.All)
require.EqualValues(t, contract.NoneFlag, callflag.NoneFlag)
}

func TestStoragePutGet(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/compiler/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/compiler"
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
Expand Down Expand Up @@ -85,7 +85,7 @@ func invokeMethod(t *testing.T, method string, script []byte, v *vm.VM, di *comp
}
}
require.True(t, mainOffset >= 0)
v.LoadScriptWithFlags(script, smartcontract.All)
v.LoadScriptWithFlags(script, callflag.All)
v.Jump(v.Context(), mainOffset)
if initOffset >= 0 {
v.Call(v.Context(), initOffset)
Expand Down
9 changes: 5 additions & 4 deletions pkg/consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
Expand Down Expand Up @@ -54,10 +55,10 @@ func initServiceNextConsensus(t *testing.T, newAcc *wallet.Account, offset uint3

// Transfer funds to new validator.
w := io.NewBufBinWriter()
emit.AppCallWithOperationAndArgs(w.BinWriter, bc.GoverningTokenHash(), "transfer",
emit.AppCall(w.BinWriter, bc.GoverningTokenHash(), "transfer", callflag.All,
acc.Contract.ScriptHash().BytesBE(), newPriv.GetScriptHash().BytesBE(), int64(native.NEOTotalSupply), nil)
emit.Opcodes(w.BinWriter, opcode.ASSERT)
emit.AppCallWithOperationAndArgs(w.BinWriter, bc.UtilityTokenHash(), "transfer",
emit.AppCall(w.BinWriter, bc.UtilityTokenHash(), "transfer", callflag.All,
acc.Contract.ScriptHash().BytesBE(), newPriv.GetScriptHash().BytesBE(), int64(1_000_000_000), nil)
emit.Opcodes(w.BinWriter, opcode.ASSERT)
require.NoError(t, w.Err)
Expand All @@ -74,7 +75,7 @@ func initServiceNextConsensus(t *testing.T, newAcc *wallet.Account, offset uint3

// Register new candidate.
w.Reset()
emit.AppCallWithOperationAndArgs(w.BinWriter, bc.GoverningTokenHash(), "registerCandidate", newPriv.PublicKey().Bytes())
emit.AppCall(w.BinWriter, bc.GoverningTokenHash(), "registerCandidate", callflag.All, newPriv.PublicKey().Bytes())
require.NoError(t, w.Err)

tx = transaction.New(netmode.UnitTestNet, w.Bytes(), 20_000_000)
Expand All @@ -92,7 +93,7 @@ func initServiceNextConsensus(t *testing.T, newAcc *wallet.Account, offset uint3

// Vote for new candidate.
w.Reset()
emit.AppCallWithOperationAndArgs(w.BinWriter, bc.GoverningTokenHash(), "vote",
emit.AppCall(w.BinWriter, bc.GoverningTokenHash(), "vote", callflag.All,
newPriv.GetScriptHash(), newPriv.PublicKey().Bytes())
emit.Opcodes(w.BinWriter, opcode.ASSERT)
require.NoError(t, w.Err)
Expand Down
12 changes: 5 additions & 7 deletions pkg/core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -624,7 +623,7 @@ func (bc *Blockchain) storeBlock(block *block.Block, txpool *mempool.Pool) error

systemInterop := bc.newInteropContext(trigger.Application, cache, block, tx)
v := systemInterop.SpawnVM()
v.LoadScriptWithFlags(tx.Script, smartcontract.All)
v.LoadScriptWithFlags(tx.Script, callflag.All)
v.SetPriceGetter(bc.getPrice)
v.GasLimit = tx.SystemFee

Expand Down Expand Up @@ -765,7 +764,7 @@ func (bc *Blockchain) storeBlock(block *block.Block, txpool *mempool.Pool) error
func (bc *Blockchain) runPersist(script []byte, block *block.Block, cache *dao.Cached, trig trigger.Type) (*state.AppExecResult, error) {
systemInterop := bc.newInteropContext(trig, cache, block, nil)
v := systemInterop.SpawnVM()
v.LoadScriptWithFlags(script, smartcontract.WriteStates|smartcontract.AllowCall)
v.LoadScriptWithFlags(script, callflag.WriteStates|callflag.AllowCall)
v.SetPriceGetter(bc.getPrice)
if err := v.Run(); err != nil {
return nil, fmt.Errorf("VM has failed: %w", err)
Expand Down Expand Up @@ -1659,7 +1658,7 @@ func (bc *Blockchain) initVerificationVM(ic *interop.Context, hash util.Uint160,
if bc.contracts.ByHash(hash) != nil {
return ErrNativeContractWitness
}
v.LoadScriptWithFlags(witness.VerificationScript, smartcontract.NoneFlag)
v.LoadScriptWithFlags(witness.VerificationScript, callflag.NoneFlag)
} else {
cs, err := ic.GetContract(hash)
if err != nil {
Expand All @@ -1670,12 +1669,11 @@ func (bc *Blockchain) initVerificationVM(ic *interop.Context, hash util.Uint160,
return ErrInvalidVerificationContract
}
initMD := cs.Manifest.ABI.GetMethod(manifest.MethodInit)
v.LoadScriptWithHash(cs.NEF.Script, hash, smartcontract.ReadStates)
v.LoadScriptWithHash(cs.NEF.Script, hash, callflag.ReadStates)
v.Jump(v.Context(), md.Offset)

if cs.ID <= 0 {
w := io.NewBufBinWriter()
emit.Opcodes(w.BinWriter, opcode.DEPTH, opcode.PACK)
emit.String(w.BinWriter, manifest.MethodVerify)
if w.Err != nil {
return w.Err
Expand Down
7 changes: 4 additions & 3 deletions pkg/core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm"
Expand Down Expand Up @@ -272,12 +273,12 @@ func TestVerifyTx(t *testing.T) {
if sc.Equals(gasHash) {
amount = 1_000_000_000
}
emit.AppCallWithOperationAndArgs(w.BinWriter, sc, "transfer",
emit.AppCall(w.BinWriter, sc, "transfer", callflag.All,
neoOwner, a.Contract.ScriptHash(), amount, nil)
emit.Opcodes(w.BinWriter, opcode.ASSERT)
}
}
emit.AppCallWithOperationAndArgs(w.BinWriter, gasHash, "transfer",
emit.AppCall(w.BinWriter, gasHash, "transfer", callflag.All,
neoOwner, testchain.CommitteeScriptHash(), int64(1_000_000_000), nil)
emit.Opcodes(w.BinWriter, opcode.ASSERT)
require.NoError(t, w.Err)
Expand Down Expand Up @@ -973,7 +974,7 @@ func TestVerifyTx(t *testing.T) {
transaction.NotaryServiceFeePerKey + // fee for Notary attribute
fee.Opcode(bc.GetBaseExecFee(), // Notary verification script
opcode.PUSHDATA1, opcode.RET, // invocation script
opcode.DEPTH, opcode.PACK, opcode.PUSHDATA1, opcode.RET, // arguments for native verification call
opcode.PUSHDATA1, opcode.RET, // arguments for native verification call
opcode.PUSHDATA1, opcode.SYSCALL, opcode.RET) + // Neo.Native.Call
native.NotaryVerificationPrice // Notary witness verification price
tx.Scripts = []transaction.Witness{
Expand Down
Loading