forked from TrueCloudLab/neoneo-go
aa4bc1b6e8
* block partial persist * replaced refactored files with old one. * removed gokit/log from deps * Tweaks to not overburden remote nodes with getheaders/getblocks * Changed Transporter interface to not take the server as argument due to a cause of race warning from the compiler * started server test suite * more test + return errors from message handlers * removed --race from build * Little improvements.
209 lines
4.7 KiB
Go
209 lines
4.7 KiB
Go
package transaction
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/binary"
|
|
"io"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
)
|
|
|
|
// Transaction is a process recorded in the NEO blockchain.
|
|
type Transaction struct {
|
|
// The type of the transaction.
|
|
Type TXType
|
|
|
|
// The trading version which is currently 0.
|
|
Version uint8
|
|
|
|
// Data specific to the type of the transaction.
|
|
// This is always a pointer to a <Type>Transaction.
|
|
Data TXer
|
|
|
|
// Transaction attributes.
|
|
Attributes []*Attribute
|
|
|
|
// The inputs of the transaction.
|
|
Inputs []*Input
|
|
|
|
// The outputs of the transaction.
|
|
Outputs []*Output
|
|
|
|
// The scripts that comes with this transaction.
|
|
// Scripts exist out of the verification script
|
|
// and invocation script.
|
|
Scripts []*Witness
|
|
|
|
// hash of the transaction
|
|
hash util.Uint256
|
|
}
|
|
|
|
// Hash return the hash of the transaction.
|
|
func (t *Transaction) Hash() util.Uint256 {
|
|
return t.hash
|
|
}
|
|
|
|
// AddOutput adds the given output to the transaction outputs.
|
|
func (t *Transaction) AddOutput(out *Output) {
|
|
t.Outputs = append(t.Outputs, out)
|
|
}
|
|
|
|
// AddInput adds the given input to the transaction inputs.
|
|
func (t *Transaction) AddInput(in *Input) {
|
|
t.Inputs = append(t.Inputs, in)
|
|
}
|
|
|
|
// DecodeBinary implements the payload interface.
|
|
func (t *Transaction) DecodeBinary(r io.Reader) error {
|
|
if err := binary.Read(r, binary.LittleEndian, &t.Type); err != nil {
|
|
return err
|
|
}
|
|
if err := binary.Read(r, binary.LittleEndian, &t.Version); err != nil {
|
|
return err
|
|
}
|
|
if err := t.decodeData(r); err != nil {
|
|
return err
|
|
}
|
|
|
|
lenAttrs := util.ReadVarUint(r)
|
|
t.Attributes = make([]*Attribute, lenAttrs)
|
|
for i := 0; i < int(lenAttrs); i++ {
|
|
t.Attributes[i] = &Attribute{}
|
|
if err := t.Attributes[i].DecodeBinary(r); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
lenInputs := util.ReadVarUint(r)
|
|
t.Inputs = make([]*Input, lenInputs)
|
|
for i := 0; i < int(lenInputs); i++ {
|
|
t.Inputs[i] = &Input{}
|
|
if err := t.Inputs[i].DecodeBinary(r); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
lenOutputs := util.ReadVarUint(r)
|
|
t.Outputs = make([]*Output, lenOutputs)
|
|
for i := 0; i < int(lenOutputs); i++ {
|
|
t.Outputs[i] = &Output{}
|
|
if err := t.Outputs[i].DecodeBinary(r); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
lenScripts := util.ReadVarUint(r)
|
|
t.Scripts = make([]*Witness, lenScripts)
|
|
for i := 0; i < int(lenScripts); i++ {
|
|
t.Scripts[i] = &Witness{}
|
|
if err := t.Scripts[i].DecodeBinary(r); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Create the hash of the transaction at decode, so we dont need
|
|
// to do it anymore.
|
|
hash, err := t.createHash()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
t.hash = hash
|
|
|
|
return nil
|
|
}
|
|
|
|
func (t *Transaction) decodeData(r io.Reader) error {
|
|
switch t.Type {
|
|
case InvocationType:
|
|
t.Data = &InvocationTX{}
|
|
return t.Data.(*InvocationTX).DecodeBinary(r)
|
|
case MinerType:
|
|
t.Data = &MinerTX{}
|
|
return t.Data.(*MinerTX).DecodeBinary(r)
|
|
case ClaimType:
|
|
t.Data = &ClaimTX{}
|
|
return t.Data.(*ClaimTX).DecodeBinary(r)
|
|
case ContractType:
|
|
t.Data = &ContractTX{}
|
|
return t.Data.(*ContractTX).DecodeBinary(r)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EncodeBinary implements the payload interface.
|
|
func (t *Transaction) EncodeBinary(w io.Writer) error {
|
|
if err := t.EncodeBinaryUnsigned(w); err != nil {
|
|
return err
|
|
}
|
|
if err := util.WriteVarUint(w, uint64(len(t.Scripts))); err != nil {
|
|
return err
|
|
}
|
|
for _, s := range t.Scripts {
|
|
if err := s.EncodeBinary(w); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EncodeBinaryUnsigned will only encode the fields that are not used for
|
|
// signing the transaction, which are all fields except the scripts.
|
|
func (t *Transaction) EncodeBinaryUnsigned(w io.Writer) error {
|
|
if err := binary.Write(w, binary.LittleEndian, t.Type); err != nil {
|
|
return err
|
|
}
|
|
if err := binary.Write(w, binary.LittleEndian, t.Version); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Underlying TXer.
|
|
if err := t.Data.EncodeBinary(w); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Attributes
|
|
if err := util.WriteVarUint(w, uint64(len(t.Attributes))); err != nil {
|
|
return err
|
|
}
|
|
for _, attr := range t.Attributes {
|
|
if err := attr.EncodeBinary(w); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Inputs
|
|
if err := util.WriteVarUint(w, uint64(len(t.Inputs))); err != nil {
|
|
return err
|
|
}
|
|
for _, in := range t.Inputs {
|
|
if err := in.EncodeBinary(w); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Outputs
|
|
if err := util.WriteVarUint(w, uint64(len(t.Outputs))); err != nil {
|
|
return err
|
|
}
|
|
for _, out := range t.Outputs {
|
|
if err := out.EncodeBinary(w); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (t *Transaction) createHash() (hash util.Uint256, err error) {
|
|
buf := new(bytes.Buffer)
|
|
if err = t.EncodeBinaryUnsigned(buf); err != nil {
|
|
return
|
|
}
|
|
sha := sha256.New()
|
|
sha.Write(buf.Bytes())
|
|
b := sha.Sum(nil)
|
|
sha.Reset()
|
|
sha.Write(b)
|
|
b = sha.Sum(nil)
|
|
return util.Uint256DecodeBytes(util.ArrayReverse(b))
|
|
}
|