smartcontract: turn trigger types into Type

1) Turn trigger types from byte constants into Type
2) Add auto-generated stringer for future purposes
This commit is contained in:
Anna Shaleva 2020-02-26 16:44:54 +03:00
parent b3621d4a86
commit 7d46404e2d
6 changed files with 98 additions and 11 deletions

View file

@ -1,13 +1,18 @@
package trigger
// Trigger typed used in C# reference node: https://github.com/neo-project/neo/blob/c64748ecbac3baeb8045b16af0d518398a6ced24/neo/SmartContract/TriggerType.cs#L3
//go:generate stringer -type=Type
// 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
// Viable list of supported trigger type constants.
const (
// 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 entry point of the contract will be invoked if the contract is triggered by Verification:
// main(...);
// The entry point of the contract must be able to handle this type of invocation.
Verification = 0x00
Verification Type = 0x00
// 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.
@ -16,14 +21,14 @@ const (
// 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 = 0x01
VerificationR Type = 0x01
// 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 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)
// The functions can be invoked by creating an InvocationTransaction.
Application = 0x10
Application Type = 0x10
// 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.
// The received function accepts no parameter, changes the states of the blockchain, and returns any type of value.
@ -32,5 +37,5 @@ const (
// 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 = 0x11
ApplicationR Type = 0x11
)

View file

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

View file

@ -0,0 +1,43 @@
package trigger
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestStringer(t *testing.T) {
tests := map[Type]string{
Application: "Application",
ApplicationR: "ApplicationR",
Verification: "Verification",
VerificationR: "VerificationR",
}
for o, s := range tests {
assert.Equal(t, s, o.String())
}
}
func TestEncodeBynary(t *testing.T) {
tests := map[Type]byte{
Verification: 0x00,
VerificationR: 0x01,
Application: 0x10,
ApplicationR: 0x11,
}
for o, b := range tests {
assert.Equal(t, b, byte(o))
}
}
func TestDecodeBynary(t *testing.T) {
tests := map[Type]byte{
Verification: 0x00,
VerificationR: 0x01,
Application: 0x10,
ApplicationR: 0x11,
}
for o, b := range tests {
assert.Equal(t, o, Type(b))
}
}