2019-12-04 09:27:04 +00:00
package trigger
2020-11-12 14:49:28 +00:00
import (
"fmt"
"strings"
)
2020-09-03 16:58:50 +00:00
2020-06-10 07:59:10 +00:00
//go:generate stringer -type=Type -output=trigger_type_string.go
2020-02-26 13:44:54 +00:00
2022-04-20 18:30:09 +00:00
// Type represents a trigger type used in C# reference node: https://github.com/neo-project/neo/blob/c64748ecbac3baeb8045b16af0d518398a6ced24/neo/SmartContract/TriggerType.cs#L3
2020-02-26 13:44:54 +00:00
type Type byte
// Viable list of supported trigger type constants.
2019-12-04 09:27:04 +00:00
const (
2022-04-20 18:30:09 +00:00
// OnPersist is a trigger type that indicates that the script is being invoked
2020-10-29 16:14:49 +00:00
// internally by the system during block persistence (before transaction
// processing).
OnPersist Type = 0x01
2022-04-20 18:30:09 +00:00
// PostPersist is a trigger type that indicates that the script is being invoked
2020-10-29 16:14:49 +00:00
// by the system after block persistence (transcation processing) has
// finished.
PostPersist Type = 0x02
2020-06-10 07:59:10 +00:00
2019-12-04 09:27:04 +00:00
// The verification trigger indicates that the contract is being invoked as a verification function.
2022-04-20 18:30:09 +00:00
// The verification function can accept multiple parameters and should return a boolean value that indicates the validity of the transaction or block.
2019-12-04 09:27:04 +00:00
// 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.
2020-06-10 07:59:10 +00:00
Verification Type = 0x20
2019-12-04 09:27:04 +00:00
// 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.
2020-06-10 07:59:10 +00:00
Application Type = 0x40
2019-12-04 09:27:04 +00:00
2020-06-10 07:59:10 +00:00
// All represents any trigger type.
2020-10-29 16:14:49 +00:00
All Type = OnPersist | PostPersist | Verification | Application
2019-12-04 09:27:04 +00:00
)
2020-09-03 16:58:50 +00:00
2022-04-20 18:30:09 +00:00
// FromString converts a string to the trigger Type.
2020-09-03 16:58:50 +00:00
func FromString ( str string ) ( Type , error ) {
2020-10-29 16:14:49 +00:00
triggers := [ ] Type { OnPersist , PostPersist , Verification , Application , All }
2020-11-12 14:49:28 +00:00
str = strings . ToLower ( str )
2020-09-03 16:58:50 +00:00
for _ , t := range triggers {
2020-11-12 14:49:28 +00:00
if strings . ToLower ( t . String ( ) ) == str {
2020-09-03 16:58:50 +00:00
return t , nil
}
}
return 0 , fmt . Errorf ( "unknown trigger type: %s" , str )
}