2019-11-28 16:06:09 +00:00
|
|
|
package state
|
2018-04-16 20:15:30 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-30 16:52:16 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-04-16 20:15:30 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
2019-11-28 16:06:09 +00:00
|
|
|
// Contract holds information about a smart contract in the NEO blockchain.
|
|
|
|
type Contract struct {
|
2019-10-10 14:56:58 +00:00
|
|
|
Script []byte
|
|
|
|
ParamList []smartcontract.ParamType
|
|
|
|
ReturnType smartcontract.ParamType
|
|
|
|
Properties smartcontract.PropertyState
|
|
|
|
Name string
|
|
|
|
CodeVersion string
|
|
|
|
Author string
|
|
|
|
Email string
|
|
|
|
Description string
|
2018-04-16 20:15:30 +00:00
|
|
|
|
|
|
|
scriptHash util.Uint160
|
|
|
|
}
|
2019-09-30 16:52:16 +00:00
|
|
|
|
|
|
|
// DecodeBinary implements Serializable interface.
|
2019-11-28 16:06:09 +00:00
|
|
|
func (cs *Contract) DecodeBinary(br *io.BinReader) {
|
2019-12-06 15:37:37 +00:00
|
|
|
cs.Script = br.ReadVarBytes()
|
2019-11-14 08:14:06 +00:00
|
|
|
br.ReadArray(&cs.ParamList)
|
2019-10-11 15:21:34 +00:00
|
|
|
br.ReadLE(&cs.ReturnType)
|
|
|
|
br.ReadLE(&cs.Properties)
|
|
|
|
cs.Name = br.ReadString()
|
|
|
|
cs.CodeVersion = br.ReadString()
|
|
|
|
cs.Author = br.ReadString()
|
|
|
|
cs.Email = br.ReadString()
|
|
|
|
cs.Description = br.ReadString()
|
|
|
|
cs.createHash()
|
2019-09-30 16:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements Serializable interface.
|
2019-11-28 16:06:09 +00:00
|
|
|
func (cs *Contract) EncodeBinary(bw *io.BinWriter) {
|
2019-11-22 10:34:06 +00:00
|
|
|
bw.WriteVarBytes(cs.Script)
|
2019-11-14 08:14:06 +00:00
|
|
|
bw.WriteArray(cs.ParamList)
|
2019-10-11 15:21:34 +00:00
|
|
|
bw.WriteLE(cs.ReturnType)
|
|
|
|
bw.WriteLE(cs.Properties)
|
|
|
|
bw.WriteString(cs.Name)
|
|
|
|
bw.WriteString(cs.CodeVersion)
|
|
|
|
bw.WriteString(cs.Author)
|
|
|
|
bw.WriteString(cs.Email)
|
|
|
|
bw.WriteString(cs.Description)
|
2019-09-30 16:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ScriptHash returns a contract script hash.
|
2019-11-28 16:06:09 +00:00
|
|
|
func (cs *Contract) ScriptHash() util.Uint160 {
|
2019-10-11 15:21:34 +00:00
|
|
|
if cs.scriptHash.Equals(util.Uint160{}) {
|
|
|
|
cs.createHash()
|
2019-09-30 16:52:16 +00:00
|
|
|
}
|
2019-10-11 15:21:34 +00:00
|
|
|
return cs.scriptHash
|
2019-09-30 16:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// createHash creates contract script hash.
|
2019-11-28 16:06:09 +00:00
|
|
|
func (cs *Contract) createHash() {
|
2019-10-11 15:21:34 +00:00
|
|
|
cs.scriptHash = hash.Hash160(cs.Script)
|
2019-09-30 16:52:16 +00:00
|
|
|
}
|
2019-10-10 14:56:58 +00:00
|
|
|
|
|
|
|
// HasStorage checks whether the contract has storage property set.
|
2019-11-28 16:06:09 +00:00
|
|
|
func (cs *Contract) HasStorage() bool {
|
2019-10-10 14:56:58 +00:00
|
|
|
return (cs.Properties & smartcontract.HasStorage) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasDynamicInvoke checks whether the contract has dynamic invoke property set.
|
2019-11-28 16:06:09 +00:00
|
|
|
func (cs *Contract) HasDynamicInvoke() bool {
|
2019-10-10 14:56:58 +00:00
|
|
|
return (cs.Properties & smartcontract.HasDynamicInvoke) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsPayable checks whether the contract has payable property set.
|
2019-11-28 16:06:09 +00:00
|
|
|
func (cs *Contract) IsPayable() bool {
|
2019-10-10 14:56:58 +00:00
|
|
|
return (cs.Properties & smartcontract.IsPayable) != 0
|
|
|
|
}
|