2019-11-28 16:06:09 +00:00
|
|
|
package state
|
2018-04-16 20:15:30 +00:00
|
|
|
|
|
|
|
import (
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-06-09 09:12:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-11-18 20:10:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2018-04-16 20:15:30 +00:00
|
|
|
)
|
|
|
|
|
2019-11-28 16:06:09 +00:00
|
|
|
// Contract holds information about a smart contract in the NEO blockchain.
|
|
|
|
type Contract struct {
|
2020-11-18 20:10:48 +00:00
|
|
|
ID int32 `json:"id"`
|
|
|
|
UpdateCounter uint16 `json:"updatecounter"`
|
|
|
|
Hash util.Uint160 `json:"hash"`
|
|
|
|
Script []byte `json:"script"`
|
|
|
|
Manifest manifest.Manifest `json:"manifest"`
|
2018-04-16 20:15:30 +00:00
|
|
|
}
|
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) {
|
2020-06-09 09:12:56 +00:00
|
|
|
cs.ID = int32(br.ReadU32LE())
|
2020-11-18 20:10:48 +00:00
|
|
|
cs.UpdateCounter = br.ReadU16LE()
|
|
|
|
cs.Hash.DecodeBinary(br)
|
2019-12-06 15:37:37 +00:00
|
|
|
cs.Script = br.ReadVarBytes()
|
2020-06-09 09:12:56 +00:00
|
|
|
cs.Manifest.DecodeBinary(br)
|
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) {
|
2020-06-09 09:12:56 +00:00
|
|
|
bw.WriteU32LE(uint32(cs.ID))
|
2020-11-18 20:10:48 +00:00
|
|
|
bw.WriteU16LE(cs.UpdateCounter)
|
|
|
|
cs.Hash.EncodeBinary(bw)
|
2019-11-22 10:34:06 +00:00
|
|
|
bw.WriteVarBytes(cs.Script)
|
2020-06-09 09:12:56 +00:00
|
|
|
cs.Manifest.EncodeBinary(bw)
|
2019-09-30 16:52:16 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 20:10:48 +00:00
|
|
|
// CreateContractHash creates deployed contract hash from transaction sender
|
|
|
|
// and contract script.
|
|
|
|
func CreateContractHash(sender util.Uint160, script []byte) util.Uint160 {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.ABORT)
|
|
|
|
emit.Bytes(w.BinWriter, sender.BytesBE())
|
|
|
|
emit.Bytes(w.BinWriter, script)
|
|
|
|
if w.Err != nil {
|
|
|
|
panic(w.Err)
|
2020-06-09 09:12:56 +00:00
|
|
|
}
|
2020-11-18 20:10:48 +00:00
|
|
|
return hash.Hash160(w.Bytes())
|
2019-10-10 14:56:58 +00:00
|
|
|
}
|