2020-03-19 15:52:37 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
2020-09-25 09:40:57 +00:00
|
|
|
"strings"
|
2020-09-23 08:48:31 +00:00
|
|
|
|
2020-03-19 15:52:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-12-13 18:36:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
2020-06-17 10:57:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-03-19 15:52:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-06-17 10:57:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2020-03-19 15:52:37 +00:00
|
|
|
)
|
|
|
|
|
2020-11-19 10:00:46 +00:00
|
|
|
// reservedContractID represents the upper bound of the reserved IDs for native contracts.
|
|
|
|
const reservedContractID = -100
|
|
|
|
|
2020-03-19 15:52:37 +00:00
|
|
|
// Contracts is a set of registered native contracts.
|
|
|
|
type Contracts struct {
|
2020-12-08 15:28:00 +00:00
|
|
|
Management *Management
|
|
|
|
NEO *NEO
|
|
|
|
GAS *GAS
|
|
|
|
Policy *Policy
|
|
|
|
Oracle *Oracle
|
|
|
|
Designate *Designate
|
|
|
|
Notary *Notary
|
|
|
|
Contracts []interop.Contract
|
2020-06-17 10:57:54 +00:00
|
|
|
// persistScript is vm script which executes "onPersist" method of every native contract.
|
|
|
|
persistScript []byte
|
2020-09-23 08:48:31 +00:00
|
|
|
// postPersistScript is vm script which executes "postPersist" method of every native contract.
|
|
|
|
postPersistScript []byte
|
2020-03-19 15:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ByHash returns native contract with the specified hash.
|
2020-04-22 20:00:18 +00:00
|
|
|
func (cs *Contracts) ByHash(h util.Uint160) interop.Contract {
|
2020-03-19 15:52:37 +00:00
|
|
|
for _, ctr := range cs.Contracts {
|
|
|
|
if ctr.Metadata().Hash.Equals(h) {
|
|
|
|
return ctr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:40:57 +00:00
|
|
|
// ByName returns native contract with the specified name.
|
|
|
|
func (cs *Contracts) ByName(name string) interop.Contract {
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
for _, ctr := range cs.Contracts {
|
|
|
|
if strings.ToLower(ctr.Metadata().Name) == name {
|
|
|
|
return ctr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 10:00:46 +00:00
|
|
|
// NewContracts returns new set of native contracts with new GAS, NEO, Policy, Oracle,
|
|
|
|
// Designate and (optional) Notary contracts.
|
2020-11-18 08:59:34 +00:00
|
|
|
func NewContracts(p2pSigExtensionsEnabled bool) *Contracts {
|
2020-04-22 20:00:18 +00:00
|
|
|
cs := new(Contracts)
|
|
|
|
|
2020-12-08 15:28:00 +00:00
|
|
|
mgmt := newManagement()
|
|
|
|
cs.Management = mgmt
|
|
|
|
cs.Contracts = append(cs.Contracts, mgmt)
|
|
|
|
|
2020-10-02 10:50:56 +00:00
|
|
|
gas := newGAS()
|
|
|
|
neo := newNEO()
|
2020-04-22 20:00:18 +00:00
|
|
|
neo.GAS = gas
|
|
|
|
gas.NEO = neo
|
|
|
|
|
|
|
|
cs.GAS = gas
|
|
|
|
cs.NEO = neo
|
|
|
|
cs.Contracts = append(cs.Contracts, neo)
|
2020-12-13 18:36:06 +00:00
|
|
|
cs.Contracts = append(cs.Contracts, gas)
|
2020-06-15 18:13:32 +00:00
|
|
|
|
|
|
|
policy := newPolicy()
|
|
|
|
cs.Policy = policy
|
|
|
|
cs.Contracts = append(cs.Contracts, policy)
|
2020-09-18 13:26:36 +00:00
|
|
|
|
|
|
|
oracle := newOracle()
|
|
|
|
oracle.GAS = gas
|
|
|
|
oracle.NEO = neo
|
|
|
|
cs.Oracle = oracle
|
|
|
|
cs.Contracts = append(cs.Contracts, oracle)
|
2020-10-01 15:17:09 +00:00
|
|
|
|
2020-11-18 08:59:34 +00:00
|
|
|
desig := newDesignate(p2pSigExtensionsEnabled)
|
2020-10-01 15:17:09 +00:00
|
|
|
desig.NEO = neo
|
|
|
|
cs.Designate = desig
|
|
|
|
cs.Oracle.Desig = desig
|
|
|
|
cs.Contracts = append(cs.Contracts, desig)
|
|
|
|
|
2020-11-19 10:00:46 +00:00
|
|
|
if p2pSigExtensionsEnabled {
|
|
|
|
notary := newNotary()
|
|
|
|
notary.GAS = gas
|
|
|
|
notary.Desig = desig
|
|
|
|
cs.Notary = notary
|
|
|
|
cs.Contracts = append(cs.Contracts, notary)
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:00:18 +00:00
|
|
|
return cs
|
2020-06-17 10:57:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 18:36:06 +00:00
|
|
|
// GetPersistScript returns VM script calling "onPersist" syscall for native contracts.
|
2020-06-17 10:57:54 +00:00
|
|
|
func (cs *Contracts) GetPersistScript() []byte {
|
|
|
|
if cs.persistScript != nil {
|
|
|
|
return cs.persistScript
|
|
|
|
}
|
|
|
|
w := io.NewBufBinWriter()
|
2020-12-13 18:36:06 +00:00
|
|
|
emit.Syscall(w.BinWriter, interopnames.SystemContractNativeOnPersist)
|
2020-06-17 10:57:54 +00:00
|
|
|
cs.persistScript = w.Bytes()
|
|
|
|
return cs.persistScript
|
2020-03-19 15:52:37 +00:00
|
|
|
}
|
2020-09-23 08:48:31 +00:00
|
|
|
|
2020-12-13 18:36:06 +00:00
|
|
|
// GetPostPersistScript returns VM script calling "postPersist" syscall for native contracts.
|
2020-09-23 08:48:31 +00:00
|
|
|
func (cs *Contracts) GetPostPersistScript() []byte {
|
|
|
|
if cs.postPersistScript != nil {
|
|
|
|
return cs.postPersistScript
|
|
|
|
}
|
|
|
|
w := io.NewBufBinWriter()
|
2020-12-13 18:36:06 +00:00
|
|
|
emit.Syscall(w.BinWriter, interopnames.SystemContractNativePostPersist)
|
2020-09-23 08:48:31 +00:00
|
|
|
cs.postPersistScript = w.Bytes()
|
|
|
|
return cs.postPersistScript
|
|
|
|
}
|