*: support _initialize method in contracts

Invoke `_initialize` method on every call if present.
In NEO3 there is no entrypoint and methods are invoked by offset,
thus `Main` function is no longer required.
We still have special `Main` method in tests to simplify them.
This commit is contained in:
Evgenii Stratonikov 2020-07-24 13:40:54 +03:00
parent 466af55dea
commit 685d44dbc1
9 changed files with 156 additions and 40 deletions

View file

@ -330,6 +330,8 @@ func getTestContractState() *state.Contract {
byte(opcode.ADD), byte(opcode.RET),
byte(opcode.PUSH7), byte(opcode.RET),
byte(opcode.DROP), byte(opcode.RET),
byte(opcode.INITSSLOT), 1, byte(opcode.PUSH3), byte(opcode.STSFLD0), byte(opcode.RET),
byte(opcode.LDSFLD0), byte(opcode.ADD), byte(opcode.RET),
}
h := hash.Hash160(script)
m := manifest.NewManifest(h)
@ -354,6 +356,19 @@ func getTestContractState() *state.Contract {
Offset: 5,
ReturnType: smartcontract.VoidType,
},
{
Name: manifest.MethodInit,
Offset: 7,
ReturnType: smartcontract.VoidType,
},
{
Name: "add3",
Offset: 12,
Parameters: []manifest.Parameter{
manifest.NewParameter("addend", smartcontract.IntegerType),
},
ReturnType: smartcontract.IntegerType,
},
}
return &state.Contract{
Script: script,
@ -382,6 +397,7 @@ func TestContractCall(t *testing.T) {
perm := manifest.NewPermission(manifest.PermissionHash, h)
perm.Methods.Add("add")
perm.Methods.Add("drop")
perm.Methods.Add("add3")
m.Permissions = append(m.Permissions, *perm)
require.NoError(t, ic.DAO.PutContractState(&state.Contract{
@ -441,6 +457,20 @@ func TestContractCall(t *testing.T) {
require.NoError(t, contractCall(ic, v))
require.Error(t, v.Run())
})
t.Run("CallInitialize", func(t *testing.T) {
t.Run("Directly", runInvalid(stackitem.NewArray([]stackitem.Item{}), "_initialize", h.BytesBE()))
initVM(v)
v.Estack().PushVal(stackitem.NewArray([]stackitem.Item{stackitem.Make(5)}))
v.Estack().PushVal("add3")
v.Estack().PushVal(h.BytesBE())
require.NoError(t, contractCall(ic, v))
require.NoError(t, v.Run())
require.Equal(t, 2, v.Estack().Len())
require.Equal(t, big.NewInt(8), v.Estack().Pop().Value())
require.Equal(t, big.NewInt(42), v.Estack().Pop().Value())
})
}
func TestContractCreate(t *testing.T) {