neoneo-go/pkg/core/transaction/invocation.go
Roman Khimov 5bf00db2c9 io: move BinReader/BinWriter there, redo Serializable with it
The logic here is that we'll have all binary encoding/decoding done via our io
package, which simplifies error handling. This functionality doesn't belong to
util, so it's moved.

This also expands BufBinWriter with Reset() method to fit the needs of core
package.
2019-09-16 23:39:51 +03:00

61 lines
1.3 KiB
Go

package transaction
import (
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/util"
)
// 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.
Gas util.Fixed8
Version uint8
}
// NewInvocationTX returns a new invocation transaction.
func NewInvocationTX(script []byte) *Transaction {
return &Transaction{
Type: InvocationType,
Version: 1,
Data: &InvocationTX{
Script: script,
},
Attributes: []*Attribute{},
Inputs: []*Input{},
Outputs: []*Output{},
Scripts: []*Witness{},
}
}
// DecodeBinary implements the Payload interface.
func (tx *InvocationTX) DecodeBinary(br *io.BinReader) error {
tx.Script = br.ReadBytes()
if tx.Version >= 1 {
br.ReadLE(&tx.Gas)
} else {
tx.Gas = util.Fixed8FromInt64(0)
}
return br.Err
}
// EncodeBinary implements the Payload interface.
func (tx *InvocationTX) EncodeBinary(bw *io.BinWriter) error {
bw.WriteBytes(tx.Script)
if tx.Version >= 1 {
bw.WriteLE(tx.Gas)
}
return bw.Err
}
// Size returns serialized binary size for this transaction.
func (tx *InvocationTX) Size() int {
sz := util.GetVarSize(tx.Script)
if tx.Version >= 1 {
sz += tx.Gas.Size()
}
return sz
}