2018-03-04 13:56:49 +00:00
|
|
|
package transaction
|
2018-01-31 08:27:08 +00:00
|
|
|
|
2018-01-31 10:47:54 +00:00
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2018-03-30 06:15:03 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2018-01-31 10:47:54 +00:00
|
|
|
"io"
|
2018-02-04 19:54:51 +00:00
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2018-01-31 10:47:54 +00:00
|
|
|
)
|
2018-01-31 08:27:08 +00:00
|
|
|
|
2018-03-04 13:56:49 +00:00
|
|
|
// Witness contains 2 scripts.
|
2018-01-31 08:27:08 +00:00
|
|
|
type Witness struct {
|
2018-03-30 06:15:03 +00:00
|
|
|
InvocationScript []byte
|
|
|
|
VerificationScript []byte
|
2018-01-31 08:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements the payload interface.
|
2018-03-30 06:15:03 +00:00
|
|
|
func (w *Witness) DecodeBinary(r io.Reader) error {
|
2018-02-04 19:54:51 +00:00
|
|
|
lenb := util.ReadVarUint(r)
|
2018-03-30 06:15:03 +00:00
|
|
|
w.InvocationScript = make([]byte, lenb)
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, w.InvocationScript); err != nil {
|
2018-03-04 13:56:49 +00:00
|
|
|
return err
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
lenb = util.ReadVarUint(r)
|
2018-03-30 06:15:03 +00:00
|
|
|
w.VerificationScript = make([]byte, lenb)
|
|
|
|
return binary.Read(r, binary.LittleEndian, w.VerificationScript)
|
2018-01-31 08:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements the payload interface.
|
2018-03-30 06:15:03 +00:00
|
|
|
func (w *Witness) EncodeBinary(writer io.Writer) error {
|
|
|
|
if err := util.WriteVarUint(writer, uint64(len(w.InvocationScript))); err != nil {
|
2018-03-17 11:53:21 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-03-30 06:15:03 +00:00
|
|
|
if err := binary.Write(writer, binary.LittleEndian, w.InvocationScript); err != nil {
|
2018-02-07 14:16:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-03-30 06:15:03 +00:00
|
|
|
if err := util.WriteVarUint(writer, uint64(len(w.VerificationScript))); err != nil {
|
2018-03-17 11:53:21 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-03-30 06:15:03 +00:00
|
|
|
return binary.Write(writer, binary.LittleEndian, w.VerificationScript)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json marshaller interface.
|
|
|
|
func (w *Witness) MarshalJSON() ([]byte, error) {
|
2018-11-26 15:56:45 +00:00
|
|
|
data := map[string]string{
|
2018-03-30 06:15:03 +00:00
|
|
|
"invocation": hex.EncodeToString(w.InvocationScript),
|
|
|
|
"verification": hex.EncodeToString(w.VerificationScript),
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(data)
|
2018-01-31 08:27:08 +00:00
|
|
|
}
|
2019-02-20 17:39:32 +00:00
|
|
|
|
|
|
|
// Size returns the size in bytes of the Witness.
|
|
|
|
func (w *Witness) Size() int {
|
|
|
|
return util.GetVarSize(w.InvocationScript) + util.GetVarSize(w.VerificationScript)
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// ScriptHash returns the hash of the VerificationScript.
|
|
|
|
func (w Witness) ScriptHash() (util.Uint160, error) {
|
|
|
|
return util.Uint160FromScript(w.VerificationScript)
|
|
|
|
}
|