smartcontract: rewrite trigger types for NEO3

This commit is contained in:
Evgenii Stratonikov 2020-06-10 10:59:10 +03:00
parent a3b876d55c
commit b12add5a78
4 changed files with 32 additions and 51 deletions

View file

@ -272,7 +272,7 @@ func checkStorageContext(ic *interop.Context, stc *StorageContext) error {
// storageDelete deletes stored key-value pair. // storageDelete deletes stored key-value pair.
func storageDelete(ic *interop.Context, v *vm.VM) error { func storageDelete(ic *interop.Context, v *vm.VM) error {
if ic.Trigger != trigger.Application && ic.Trigger != trigger.ApplicationR { if ic.Trigger != trigger.Application {
return errors.New("can't delete when the trigger is not application") return errors.New("can't delete when the trigger is not application")
} }
stcInterface := v.Estack().Pop().Value() stcInterface := v.Estack().Pop().Value()
@ -337,7 +337,7 @@ func storageGetReadOnlyContext(ic *interop.Context, v *vm.VM) error {
} }
func putWithContextAndFlags(ic *interop.Context, stc *StorageContext, key []byte, value []byte, isConst bool) error { func putWithContextAndFlags(ic *interop.Context, stc *StorageContext, key []byte, value []byte, isConst bool) error {
if ic.Trigger != trigger.Application && ic.Trigger != trigger.ApplicationR { if ic.Trigger != trigger.Application {
return errors.New("can't delete when the trigger is not application") return errors.New("can't delete when the trigger is not application")
} }
if len(key) > MaxStorageKeyLen { if len(key) > MaxStorageKeyLen {

View file

@ -1,41 +1,29 @@
package trigger package trigger
//go:generate stringer -type=Type //go:generate stringer -type=Type -output=trigger_type_string.go
// Type represents trigger type used in C# reference node: https://github.com/neo-project/neo/blob/c64748ecbac3baeb8045b16af0d518398a6ced24/neo/SmartContract/TriggerType.cs#L3 // Type represents trigger type used in C# reference node: https://github.com/neo-project/neo/blob/c64748ecbac3baeb8045b16af0d518398a6ced24/neo/SmartContract/TriggerType.cs#L3
type Type byte type Type byte
// Viable list of supported trigger type constants. // Viable list of supported trigger type constants.
const ( const (
// System is trigger type that indicates that script is being invoke internally by the system.
System Type = 0x01
// The verification trigger indicates that the contract is being invoked as a verification function. // The verification trigger indicates that the contract is being invoked as a verification function.
// The verification function can accept multiple parameters, and should return a boolean value that indicates the validity of the transaction or block. // The verification function can accept multiple parameters, and should return a boolean value that indicates the validity of the transaction or block.
// The entry point of the contract will be invoked if the contract is triggered by Verification: // The entry point of the contract will be invoked if the contract is triggered by Verification:
// main(...); // main(...);
// The entry point of the contract must be able to handle this type of invocation. // The entry point of the contract must be able to handle this type of invocation.
Verification Type = 0x00 Verification Type = 0x20
// The verificationR trigger indicates that the contract is being invoked as a verification function because it is specified as a target of an output of the transaction.
// The verification function accepts no parameter, and should return a boolean value that indicates the validity of the transaction.
// The entry point of the contract will be invoked if the contract is triggered by VerificationR:
// main("receiving", new object[0]);
// The receiving function should have the following signature:
// public bool receiving()
// The receiving function will be invoked automatically when a contract is receiving assets from a transfer.
VerificationR Type = 0x01
// The application trigger indicates that the contract is being invoked as an application function. // The application trigger indicates that the contract is being invoked as an application function.
// The application function can accept multiple parameters, change the states of the blockchain, and return any type of value. // The application function can accept multiple parameters, change the states of the blockchain, and return any type of value.
// The contract can have any form of entry point, but we recommend that all contracts should have the following entry point: // The contract can have any form of entry point, but we recommend that all contracts should have the following entry point:
// public byte[] main(string operation, params object[] args) // public byte[] main(string operation, params object[] args)
// The functions can be invoked by creating an InvocationTransaction. // The functions can be invoked by creating an InvocationTransaction.
Application Type = 0x10 Application Type = 0x40
// The ApplicationR trigger indicates that the default function received of the contract is being invoked because it is specified as a target of an output of the transaction. // All represents any trigger type.
// The received function accepts no parameter, changes the states of the blockchain, and returns any type of value. All = System | Verification | Application
// The entry point of the contract will be invoked if the contract is triggered by ApplicationR:
// main("received", new object[0]);
// The received function should have the following signature:
// public byte[] received()
// The received function will be invoked automatically when a contract is receiving assets from a transfer.
ApplicationR Type = 0x11
) )

View file

@ -1,4 +1,4 @@
// Code generated by "stringer -type=Type"; DO NOT EDIT. // Code generated by "stringer -type=Type -output=trigger_type_string.go"; DO NOT EDIT.
package trigger package trigger
@ -8,29 +8,25 @@ func _() {
// An "invalid array index" compiler error signifies that the constant values have changed. // An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again. // Re-run the stringer command to generate them again.
var x [1]struct{} var x [1]struct{}
_ = x[Verification-0] _ = x[System-1]
_ = x[VerificationR-1] _ = x[Verification-32]
_ = x[Application-16] _ = x[Application-64]
_ = x[ApplicationR-17]
} }
const ( const (
_Type_name_0 = "VerificationVerificationR" _Type_name_0 = "System"
_Type_name_1 = "ApplicationApplicationR" _Type_name_1 = "Verification"
) _Type_name_2 = "Application"
var (
_Type_index_0 = [...]uint8{0, 12, 25}
_Type_index_1 = [...]uint8{0, 11, 23}
) )
func (i Type) String() string { func (i Type) String() string {
switch { switch {
case i <= 1: case i == 1:
return _Type_name_0[_Type_index_0[i]:_Type_index_0[i+1]] return _Type_name_0
case 16 <= i && i <= 17: case i == 32:
i -= 16 return _Type_name_1
return _Type_name_1[_Type_index_1[i]:_Type_index_1[i+1]] case i == 64:
return _Type_name_2
default: default:
return "Type(" + strconv.FormatInt(int64(i), 10) + ")" return "Type(" + strconv.FormatInt(int64(i), 10) + ")"
} }

View file

@ -8,10 +8,9 @@ import (
func TestStringer(t *testing.T) { func TestStringer(t *testing.T) {
tests := map[Type]string{ tests := map[Type]string{
Application: "Application", System: "System",
ApplicationR: "ApplicationR", Application: "Application",
Verification: "Verification", Verification: "Verification",
VerificationR: "VerificationR",
} }
for o, s := range tests { for o, s := range tests {
assert.Equal(t, s, o.String()) assert.Equal(t, s, o.String())
@ -20,10 +19,9 @@ func TestStringer(t *testing.T) {
func TestEncodeBynary(t *testing.T) { func TestEncodeBynary(t *testing.T) {
tests := map[Type]byte{ tests := map[Type]byte{
Verification: 0x00, System: 0x01,
VerificationR: 0x01, Verification: 0x20,
Application: 0x10, Application: 0x40,
ApplicationR: 0x11,
} }
for o, b := range tests { for o, b := range tests {
assert.Equal(t, b, byte(o)) assert.Equal(t, b, byte(o))
@ -32,10 +30,9 @@ func TestEncodeBynary(t *testing.T) {
func TestDecodeBynary(t *testing.T) { func TestDecodeBynary(t *testing.T) {
tests := map[Type]byte{ tests := map[Type]byte{
Verification: 0x00, System: 0x01,
VerificationR: 0x01, Verification: 0x20,
Application: 0x10, Application: 0x40,
ApplicationR: 0x11,
} }
for o, b := range tests { for o, b := range tests {
assert.Equal(t, o, Type(b)) assert.Equal(t, o, Type(b))