forked from TrueCloudLab/neoneo-go
examples: completely rework runtime example
Make it more useful, triggers are largely obsolete (2.x thing).
This commit is contained in:
parent
be7527409c
commit
fe27ae9b96
2 changed files with 52 additions and 23 deletions
|
@ -1,55 +1,81 @@
|
||||||
package runtimecontract
|
package runtimecontract
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Check if the invoker of the contract is the specified owner
|
// Check if the invoker of the contract is the specified owner
|
||||||
owner = util.FromAddress("NbrUYaZgyhSkNoRo9ugRyEMdUZxrhkNaWB")
|
owner = util.FromAddress("NbrUYaZgyhSkNoRo9ugRyEMdUZxrhkNaWB")
|
||||||
trigger byte
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// init initializes trigger before any other contract method is called
|
// init is transformed into _initialize method that is called whenever contract
|
||||||
|
// is being loaded (so you'll see this log entry with every invocation).
|
||||||
func init() {
|
func init() {
|
||||||
trigger = runtime.GetTrigger()
|
// No events and logging allowed in verification context.
|
||||||
|
if runtime.GetTrigger() != runtime.Verification {
|
||||||
|
runtime.Log("init called")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// _deploy is called after contract deployment or update, it'll be called
|
||||||
|
// in deployment transaction and if call update method of this contract.
|
||||||
func _deploy(_ interface{}, isUpdate bool) {
|
func _deploy(_ interface{}, isUpdate bool) {
|
||||||
if isUpdate {
|
if isUpdate {
|
||||||
Log("_deploy method called before contract update")
|
Log("_deploy method called after contract update")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
Log("_deploy method called before contract creation")
|
Log("_deploy method called after contract creation")
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckWitness checks owner's witness
|
// CheckWitness checks owner's witness. It returns true if invoked by the owner
|
||||||
|
// and false otherwise.
|
||||||
func CheckWitness() bool {
|
func CheckWitness() bool {
|
||||||
// Log owner upon Verification trigger
|
|
||||||
if trigger != runtime.Verification {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if runtime.CheckWitness(owner) {
|
if runtime.CheckWitness(owner) {
|
||||||
runtime.Log("Verified Owner")
|
runtime.Log("Verified Owner")
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
return true
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log logs given message
|
// Log logs given message.
|
||||||
func Log(message string) bool {
|
func Log(message string) {
|
||||||
if trigger != runtime.Application {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
runtime.Log(message)
|
runtime.Log(message)
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify notifies about given message
|
// Notify emits an event with the specified data.
|
||||||
func Notify(event interface{}) bool {
|
func Notify(event interface{}) {
|
||||||
if trigger != runtime.Application {
|
runtime.Notify("Event", event)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify method is used when contract is being used as a signer of transaction,
|
||||||
|
// it can have parameters (that then need to be present in invocation script)
|
||||||
|
// and it returns simple pass/fail result. This implementation just checks for
|
||||||
|
// owner's signature presence.
|
||||||
|
func Verify() bool {
|
||||||
|
// Technically this restriction is not needed, but you can see the difference
|
||||||
|
// between invokefunction and invokecontractverify RPC methods with it.
|
||||||
|
if runtime.GetTrigger() != runtime.Verification {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
runtime.Notify("Event", event)
|
return CheckWitness()
|
||||||
return true
|
}
|
||||||
|
|
||||||
|
// Destroy destroys the contract, only owner can do that.
|
||||||
|
func Destroy() {
|
||||||
|
if !Verify() {
|
||||||
|
panic("only owner can destroy")
|
||||||
|
}
|
||||||
|
management.Destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update updates the contract, only owner can do that. _deploy will be called
|
||||||
|
// after update.
|
||||||
|
func Update(nef, manifest []byte) {
|
||||||
|
if !Verify() {
|
||||||
|
panic("only owner can update")
|
||||||
|
}
|
||||||
|
management.Update(nef, manifest)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,3 +6,6 @@ events:
|
||||||
parameters:
|
parameters:
|
||||||
- name: event
|
- name: event
|
||||||
type: Any
|
type: Any
|
||||||
|
permissions:
|
||||||
|
- hash: fffdc93764dbaddd97c48f252a53ea4643faa3fd
|
||||||
|
methods: ["update", "destroy"]
|
||||||
|
|
Loading…
Reference in a new issue