2018-04-16 20:15:30 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2019-09-30 16:52:16 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
|
|
|
"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-09-30 16:52:16 +00:00
|
|
|
// Contracts is a mapping between scripthash and ContractState.
|
|
|
|
type Contracts map[util.Uint160]*ContractState
|
|
|
|
|
2018-04-16 20:15:30 +00:00
|
|
|
// ContractState holds information about a smart contract in the NEO blockchain.
|
|
|
|
type ContractState 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
|
|
|
|
|
|
|
// commit flushes all contracts to the given storage.Batch.
|
2019-10-16 14:29:21 +00:00
|
|
|
func (a Contracts) commit(store storage.Store) error {
|
|
|
|
for _, contract := range a {
|
|
|
|
if err := putContractStateIntoStore(store, contract); err != nil {
|
|
|
|
return err
|
2019-09-30 16:52:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements Serializable interface.
|
2019-10-11 15:21:34 +00:00
|
|
|
func (cs *ContractState) DecodeBinary(br *io.BinReader) {
|
|
|
|
cs.Script = br.ReadBytes()
|
2019-09-30 16:52:16 +00:00
|
|
|
paramBytes := br.ReadBytes()
|
2019-10-11 15:21:34 +00:00
|
|
|
cs.ParamList = make([]smartcontract.ParamType, len(paramBytes))
|
2019-09-30 16:52:16 +00:00
|
|
|
for k := range paramBytes {
|
2019-10-11 15:21:34 +00:00
|
|
|
cs.ParamList[k] = smartcontract.ParamType(paramBytes[k])
|
2019-09-30 16:52:16 +00:00
|
|
|
}
|
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-10-11 15:21:34 +00:00
|
|
|
func (cs *ContractState) EncodeBinary(bw *io.BinWriter) {
|
|
|
|
bw.WriteBytes(cs.Script)
|
|
|
|
bw.WriteVarUint(uint64(len(cs.ParamList)))
|
|
|
|
for k := range cs.ParamList {
|
|
|
|
bw.WriteLE(cs.ParamList[k])
|
2019-09-30 16:52:16 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-10-11 10:11:54 +00:00
|
|
|
// putContractStateIntoStore puts given contract state into the given store.
|
|
|
|
func putContractStateIntoStore(s storage.Store, cs *ContractState) error {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
cs.EncodeBinary(buf.BinWriter)
|
|
|
|
if buf.Err != nil {
|
|
|
|
return buf.Err
|
|
|
|
}
|
|
|
|
key := storage.AppendPrefix(storage.STContract, cs.ScriptHash().Bytes())
|
|
|
|
return s.Put(key, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteContractStateInStore deletes given contract state in the given store.
|
|
|
|
func deleteContractStateInStore(s storage.Store, hash util.Uint160) error {
|
|
|
|
key := storage.AppendPrefix(storage.STContract, hash.Bytes())
|
|
|
|
return s.Delete(key)
|
|
|
|
}
|
|
|
|
|
2019-09-30 16:52:16 +00:00
|
|
|
// ScriptHash returns a contract script hash.
|
2019-10-11 15:21:34 +00:00
|
|
|
func (cs *ContractState) ScriptHash() util.Uint160 {
|
|
|
|
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-10-11 15:21:34 +00:00
|
|
|
func (cs *ContractState) createHash() {
|
|
|
|
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.
|
|
|
|
func (cs *ContractState) HasStorage() bool {
|
|
|
|
return (cs.Properties & smartcontract.HasStorage) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasDynamicInvoke checks whether the contract has dynamic invoke property set.
|
|
|
|
func (cs *ContractState) HasDynamicInvoke() bool {
|
|
|
|
return (cs.Properties & smartcontract.HasDynamicInvoke) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsPayable checks whether the contract has payable property set.
|
|
|
|
func (cs *ContractState) IsPayable() bool {
|
|
|
|
return (cs.Properties & smartcontract.IsPayable) != 0
|
|
|
|
}
|