2018-03-04 13:56:49 +00:00
|
|
|
package transaction
|
|
|
|
|
|
|
|
import (
|
2018-03-14 09:36:59 +00:00
|
|
|
"bytes"
|
2018-03-04 13:56:49 +00:00
|
|
|
"io"
|
|
|
|
|
2019-08-23 15:50:45 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
2018-03-04 13:56:49 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2018-03-17 11:53:21 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-03-04 13:56:49 +00:00
|
|
|
)
|
|
|
|
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
const (
|
|
|
|
// MaxTransactionSize is the upper limit size in bytes that a transaction can reach. It is
|
|
|
|
// set to be 102400.
|
|
|
|
MaxTransactionSize = 102400
|
|
|
|
)
|
|
|
|
|
2018-03-04 13:56:49 +00:00
|
|
|
// Transaction is a process recorded in the NEO blockchain.
|
|
|
|
type Transaction struct {
|
|
|
|
// The type of the transaction.
|
2018-03-23 20:36:59 +00:00
|
|
|
Type TXType `json:"type"`
|
2018-03-04 13:56:49 +00:00
|
|
|
|
|
|
|
// The trading version which is currently 0.
|
2018-03-30 06:15:03 +00:00
|
|
|
Version uint8 `json:"version"`
|
2018-03-04 13:56:49 +00:00
|
|
|
|
|
|
|
// Data specific to the type of the transaction.
|
|
|
|
// This is always a pointer to a <Type>Transaction.
|
2018-03-23 20:36:59 +00:00
|
|
|
Data TXer `json:"-"`
|
2018-03-04 13:56:49 +00:00
|
|
|
|
|
|
|
// Transaction attributes.
|
2018-03-23 20:36:59 +00:00
|
|
|
Attributes []*Attribute `json:"attributes"`
|
2018-03-04 13:56:49 +00:00
|
|
|
|
|
|
|
// The inputs of the transaction.
|
2018-03-23 20:36:59 +00:00
|
|
|
Inputs []*Input `json:"vin"`
|
2018-03-04 13:56:49 +00:00
|
|
|
|
|
|
|
// The outputs of the transaction.
|
2018-03-23 20:36:59 +00:00
|
|
|
Outputs []*Output `json:"vout"`
|
2018-03-04 13:56:49 +00:00
|
|
|
|
|
|
|
// The scripts that comes with this transaction.
|
|
|
|
// Scripts exist out of the verification script
|
|
|
|
// and invocation script.
|
2018-03-23 20:36:59 +00:00
|
|
|
Scripts []*Witness `json:"scripts"`
|
2018-03-14 09:36:59 +00:00
|
|
|
|
|
|
|
// hash of the transaction
|
|
|
|
hash util.Uint256
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// Trimmed indicates this is a transaction from trimmed
|
|
|
|
// data.
|
2018-03-23 20:36:59 +00:00
|
|
|
Trimmed bool `json:"-"`
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTrimmedTX returns a trimmed transaction with only its hash
|
|
|
|
// and Trimmed to true.
|
|
|
|
func NewTrimmedTX(hash util.Uint256) *Transaction {
|
|
|
|
return &Transaction{
|
|
|
|
hash: hash,
|
|
|
|
Trimmed: true,
|
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hash return the hash of the transaction.
|
|
|
|
func (t *Transaction) Hash() util.Uint256 {
|
2018-03-25 10:45:54 +00:00
|
|
|
if t.hash.Equals(util.Uint256{}) {
|
|
|
|
t.createHash()
|
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
return t.hash
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2019-08-28 16:27:06 +00:00
|
|
|
br := util.BinReader{R: r}
|
|
|
|
br.ReadLE(&t.Type)
|
|
|
|
br.ReadLE(&t.Version)
|
|
|
|
if br.Err != nil {
|
|
|
|
return br.Err
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
if err := t.decodeData(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
lenAttrs := br.ReadVarUint()
|
2018-03-04 13:56:49 +00:00
|
|
|
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 {
|
2018-04-09 16:58:09 +00:00
|
|
|
log.Warnf("failed to decode TX %s", t.hash)
|
2018-03-04 13:56:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
lenInputs := br.ReadVarUint()
|
2018-03-04 13:56:49 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
lenOutputs := br.ReadVarUint()
|
2018-03-04 13:56:49 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
lenScripts := br.ReadVarUint()
|
2018-03-04 13:56:49 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
if br.Err != nil {
|
|
|
|
return br.Err
|
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
// Create the hash of the transaction at decode, so we dont need
|
|
|
|
// to do it anymore.
|
2018-03-25 10:45:54 +00:00
|
|
|
return t.createHash()
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transaction) decodeData(r io.Reader) error {
|
|
|
|
switch t.Type {
|
|
|
|
case InvocationType:
|
2019-08-30 07:44:59 +00:00
|
|
|
t.Data = &InvocationTX{Version: t.Version}
|
2018-03-04 13:56:49 +00:00
|
|
|
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)
|
2018-03-14 09:36:59 +00:00
|
|
|
case ContractType:
|
|
|
|
t.Data = &ContractTX{}
|
|
|
|
return t.Data.(*ContractTX).DecodeBinary(r)
|
2018-03-17 11:53:21 +00:00
|
|
|
case RegisterType:
|
|
|
|
t.Data = &RegisterTX{}
|
|
|
|
return t.Data.(*RegisterTX).DecodeBinary(r)
|
|
|
|
case IssueType:
|
|
|
|
t.Data = &IssueTX{}
|
|
|
|
return t.Data.(*IssueTX).DecodeBinary(r)
|
2018-03-21 16:11:04 +00:00
|
|
|
case EnrollmentType:
|
|
|
|
t.Data = &EnrollmentTX{}
|
|
|
|
return t.Data.(*EnrollmentTX).DecodeBinary(r)
|
2018-03-25 10:45:54 +00:00
|
|
|
case PublishType:
|
2019-08-27 16:58:50 +00:00
|
|
|
t.Data = &PublishTX{Version: t.Version}
|
2018-03-25 10:45:54 +00:00
|
|
|
return t.Data.(*PublishTX).DecodeBinary(r)
|
|
|
|
case StateType:
|
|
|
|
t.Data = &StateTX{}
|
|
|
|
return t.Data.(*StateTX).DecodeBinary(r)
|
2018-03-17 11:53:21 +00:00
|
|
|
default:
|
|
|
|
log.Warnf("invalid TX type %s", t.Type)
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements the payload interface.
|
|
|
|
func (t *Transaction) EncodeBinary(w io.Writer) error {
|
2018-03-17 11:53:21 +00:00
|
|
|
if err := t.encodeHashableFields(w); err != nil {
|
2018-03-14 09:36:59 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-08-28 16:27:06 +00:00
|
|
|
bw := util.BinWriter{W: w}
|
|
|
|
bw.WriteVarUint(uint64(len(t.Scripts)))
|
|
|
|
if bw.Err != nil {
|
|
|
|
return bw.Err
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
for _, s := range t.Scripts {
|
|
|
|
if err := s.EncodeBinary(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
// encodeHashableFields will only encode the fields that are not used for
|
2018-03-14 09:36:59 +00:00
|
|
|
// signing the transaction, which are all fields except the scripts.
|
2018-03-17 11:53:21 +00:00
|
|
|
func (t *Transaction) encodeHashableFields(w io.Writer) error {
|
2019-08-28 16:27:06 +00:00
|
|
|
bw := util.BinWriter{W: w}
|
|
|
|
|
|
|
|
bw.WriteLE(t.Type)
|
|
|
|
bw.WriteLE(t.Version)
|
|
|
|
if bw.Err != nil {
|
|
|
|
return bw.Err
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
|
|
|
|
// Underlying TXer.
|
2018-03-21 16:11:04 +00:00
|
|
|
if t.Data != nil {
|
|
|
|
if err := t.Data.EncodeBinary(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attributes
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteVarUint(uint64(len(t.Attributes)))
|
|
|
|
if bw.Err != nil {
|
|
|
|
return bw.Err
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
for _, attr := range t.Attributes {
|
|
|
|
if err := attr.EncodeBinary(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inputs
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteVarUint(uint64(len(t.Inputs)))
|
|
|
|
if bw.Err != nil {
|
|
|
|
return bw.Err
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
for _, in := range t.Inputs {
|
|
|
|
if err := in.EncodeBinary(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outputs
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteVarUint(uint64(len(t.Outputs)))
|
|
|
|
if bw.Err != nil {
|
|
|
|
return bw.Err
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
for _, out := range t.Outputs {
|
|
|
|
if err := out.EncodeBinary(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
// createHash creates the hash of the transaction.
|
|
|
|
func (t *Transaction) createHash() error {
|
2018-03-14 09:36:59 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2018-03-25 10:45:54 +00:00
|
|
|
if err := t.encodeHashableFields(buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-23 15:50:45 +00:00
|
|
|
t.hash = hash.DoubleSha256(buf.Bytes())
|
2018-03-25 10:45:54 +00:00
|
|
|
|
|
|
|
return nil
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
2018-03-21 16:11:04 +00:00
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// GroupInputsByPrevHash groups all TX inputs by their previous hash.
|
2018-03-21 16:11:04 +00:00
|
|
|
func (t *Transaction) GroupInputsByPrevHash() map[util.Uint256][]*Input {
|
|
|
|
m := make(map[util.Uint256][]*Input)
|
|
|
|
for _, in := range t.Inputs {
|
|
|
|
m[in.PrevHash] = append(m[in.PrevHash], in)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
2019-02-20 17:39:32 +00:00
|
|
|
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
// GroupOutputByAssetID groups all TX outputs by their assetID.
|
|
|
|
func (t Transaction) GroupOutputByAssetID() map[util.Uint256][]*Output {
|
|
|
|
m := make(map[util.Uint256][]*Output)
|
|
|
|
for _, out := range t.Outputs {
|
|
|
|
m[out.AssetID] = append(m[out.AssetID], out)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2019-02-20 17:39:32 +00:00
|
|
|
// Size returns the size of the transaction in term of bytes
|
|
|
|
func (t *Transaction) Size() int {
|
|
|
|
attrSize := util.GetVarSize(t.Attributes)
|
|
|
|
inputSize := util.GetVarSize(t.Inputs)
|
|
|
|
outputSize := util.GetVarSize(t.Outputs)
|
|
|
|
witnesSize := util.GetVarSize(t.Scripts)
|
|
|
|
// uint8 + uint8 + attrSize + inputSize + outputSize + witnesSize
|
2019-08-30 08:28:30 +00:00
|
|
|
return 2 + attrSize + inputSize + outputSize + witnesSize + t.Data.Size()
|
2019-02-20 17:39:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes convert the transaction to []byte
|
|
|
|
func (t *Transaction) Bytes() []byte {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := t.EncodeBinary(buf); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return buf.Bytes()
|
|
|
|
|
|
|
|
}
|