2018-03-04 13:56:49 +00:00
|
|
|
package transaction
|
|
|
|
|
|
|
|
import (
|
2020-03-04 10:04:11 +00:00
|
|
|
"errors"
|
2020-04-10 10:41:49 +00:00
|
|
|
"math/rand"
|
2020-03-04 10:04:11 +00:00
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2018-03-04 13:56:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// InvocationTX represents a invocation transaction and is used to
|
|
|
|
// deploy smart contract to the NEO blockchain.
|
|
|
|
type InvocationTX struct {
|
|
|
|
// Script output of the smart contract.
|
|
|
|
Script []byte
|
|
|
|
|
|
|
|
// Gas cost of the smart contract.
|
2019-09-09 08:23:27 +00:00
|
|
|
Gas util.Fixed8
|
2019-08-30 07:44:59 +00:00
|
|
|
Version uint8
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewInvocationTX returns a new invocation transaction.
|
2019-11-19 17:23:14 +00:00
|
|
|
func NewInvocationTX(script []byte, gas util.Fixed8) *Transaction {
|
2018-03-04 13:56:49 +00:00
|
|
|
return &Transaction{
|
|
|
|
Type: InvocationType,
|
|
|
|
Version: 1,
|
2020-04-10 10:41:49 +00:00
|
|
|
Nonce: rand.Uint32(),
|
2018-03-04 13:56:49 +00:00
|
|
|
Data: &InvocationTX{
|
2019-10-23 08:49:43 +00:00
|
|
|
Script: script,
|
2019-11-19 17:23:14 +00:00
|
|
|
Gas: gas,
|
2019-10-23 08:49:43 +00:00
|
|
|
Version: 1,
|
2018-03-04 13:56:49 +00:00
|
|
|
},
|
2019-12-09 14:14:10 +00:00
|
|
|
Attributes: []Attribute{},
|
|
|
|
Inputs: []Input{},
|
|
|
|
Outputs: []Output{},
|
|
|
|
Scripts: []Witness{},
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// DecodeBinary implements Serializable interface.
|
|
|
|
func (tx *InvocationTX) DecodeBinary(br *io.BinReader) {
|
2019-12-06 15:37:37 +00:00
|
|
|
tx.Script = br.ReadVarBytes()
|
2020-03-04 10:04:11 +00:00
|
|
|
if br.Err == nil && len(tx.Script) == 0 {
|
|
|
|
br.Err = errors.New("no script")
|
|
|
|
return
|
|
|
|
}
|
2019-09-09 08:23:27 +00:00
|
|
|
if tx.Version >= 1 {
|
2019-12-12 15:52:23 +00:00
|
|
|
tx.Gas.DecodeBinary(br)
|
2020-03-04 10:04:11 +00:00
|
|
|
if br.Err == nil && tx.Gas.LessThan(0) {
|
|
|
|
br.Err = errors.New("negative gas")
|
|
|
|
return
|
|
|
|
}
|
2019-08-30 07:44:59 +00:00
|
|
|
} else {
|
|
|
|
tx.Gas = util.Fixed8FromInt64(0)
|
|
|
|
}
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// EncodeBinary implements Serializable interface.
|
|
|
|
func (tx *InvocationTX) EncodeBinary(bw *io.BinWriter) {
|
2019-11-22 10:34:06 +00:00
|
|
|
bw.WriteVarBytes(tx.Script)
|
2019-09-09 08:23:27 +00:00
|
|
|
if tx.Version >= 1 {
|
2019-12-12 15:52:23 +00:00
|
|
|
tx.Gas.EncodeBinary(bw)
|
2019-08-30 07:44:59 +00:00
|
|
|
}
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|