2018-02-04 19:54:51 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
"bytes"
|
2018-02-04 19:54:51 +00:00
|
|
|
"encoding/hex"
|
2018-03-23 20:36:59 +00:00
|
|
|
"encoding/json"
|
2018-02-04 19:54:51 +00:00
|
|
|
"fmt"
|
2018-05-09 05:20:16 +00:00
|
|
|
"strings"
|
2019-11-14 08:07:23 +00:00
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2021-07-18 12:55:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
2018-02-04 19:54:51 +00:00
|
|
|
)
|
|
|
|
|
2019-11-12 09:58:11 +00:00
|
|
|
// Uint256Size is the size of Uint256 in bytes.
|
|
|
|
const Uint256Size = 32
|
2018-02-04 19:54:51 +00:00
|
|
|
|
|
|
|
// Uint256 is a 32 byte long unsigned integer.
|
2019-11-12 09:58:11 +00:00
|
|
|
type Uint256 [Uint256Size]uint8
|
2018-02-04 19:54:51 +00:00
|
|
|
|
2019-11-27 09:23:18 +00:00
|
|
|
// Uint256DecodeStringLE attempts to decode the given string (in LE representation) into an Uint256.
|
|
|
|
func Uint256DecodeStringLE(s string) (u Uint256, err error) {
|
2019-11-12 09:58:11 +00:00
|
|
|
if len(s) != Uint256Size*2 {
|
|
|
|
return u, fmt.Errorf("expected string size of %d got %d", Uint256Size*2, len(s))
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
b, err := hex.DecodeString(s)
|
|
|
|
if err != nil {
|
2018-03-02 15:24:09 +00:00
|
|
|
return u, err
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
2019-11-27 09:23:18 +00:00
|
|
|
return Uint256DecodeBytesLE(b)
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 13:17:04 +00:00
|
|
|
// Uint256DecodeStringBE attempts to decode the given string (in BE representation)
|
|
|
|
// into an Uint256.
|
|
|
|
func Uint256DecodeStringBE(s string) (u Uint256, err error) {
|
|
|
|
if len(s) != Uint256Size*2 {
|
|
|
|
return u, fmt.Errorf("expected string size of %d got %d", Uint256Size*2, len(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := hex.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Uint256DecodeBytesBE(b)
|
|
|
|
}
|
|
|
|
|
2019-11-27 09:23:18 +00:00
|
|
|
// Uint256DecodeBytesBE attempts to decode the given string (in BE representation) into an Uint256.
|
|
|
|
func Uint256DecodeBytesBE(b []byte) (u Uint256, err error) {
|
2019-11-12 09:58:11 +00:00
|
|
|
if len(b) != Uint256Size {
|
|
|
|
return u, fmt.Errorf("expected []byte of size %d got %d", Uint256Size, len(b))
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
2019-01-25 11:20:35 +00:00
|
|
|
copy(u[:], b)
|
2018-03-02 15:24:09 +00:00
|
|
|
return u, nil
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 09:23:18 +00:00
|
|
|
// Uint256DecodeBytesLE attempts to decode the given string (in LE representation) into an Uint256.
|
|
|
|
func Uint256DecodeBytesLE(b []byte) (u Uint256, err error) {
|
2021-07-18 12:55:37 +00:00
|
|
|
b = slice.CopyReverse(b)
|
2019-11-27 09:23:18 +00:00
|
|
|
return Uint256DecodeBytesBE(b)
|
2019-08-23 14:02:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 09:23:18 +00:00
|
|
|
// BytesBE returns a byte slice representation of u.
|
|
|
|
func (u Uint256) BytesBE() []byte {
|
2018-11-26 15:56:45 +00:00
|
|
|
return u[:]
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// Reverse reverses the Uint256 object.
|
2019-08-22 17:33:58 +00:00
|
|
|
func (u Uint256) Reverse() Uint256 {
|
2019-11-27 09:23:18 +00:00
|
|
|
res, _ := Uint256DecodeBytesLE(u.BytesBE())
|
2019-08-22 17:33:58 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-11-27 09:23:18 +00:00
|
|
|
// BytesLE return a little-endian byte representation of u.
|
|
|
|
func (u Uint256) BytesLE() []byte {
|
2021-07-18 12:55:37 +00:00
|
|
|
return slice.CopyReverse(u.BytesBE())
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Equals returns true if both Uint256 values are the same.
|
|
|
|
func (u Uint256) Equals(other Uint256) bool {
|
2018-11-26 15:56:45 +00:00
|
|
|
return u == other
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// String implements the stringer interface.
|
|
|
|
func (u Uint256) String() string {
|
2019-11-27 09:23:18 +00:00
|
|
|
return u.StringBE()
|
2019-08-22 17:33:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 09:23:18 +00:00
|
|
|
// StringBE produces string representation of Uint256 with BE byte order.
|
|
|
|
func (u Uint256) StringBE() string {
|
|
|
|
return hex.EncodeToString(u.BytesBE())
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringLE produces string representation of Uint256 with LE byte order.
|
|
|
|
func (u Uint256) StringLE() string {
|
|
|
|
return hex.EncodeToString(u.BytesLE())
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
2018-03-23 20:36:59 +00:00
|
|
|
|
2018-05-09 05:20:16 +00:00
|
|
|
// UnmarshalJSON implements the json unmarshaller interface.
|
|
|
|
func (u *Uint256) UnmarshalJSON(data []byte) (err error) {
|
|
|
|
var js string
|
|
|
|
if err = json.Unmarshal(data, &js); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-19 13:22:33 +00:00
|
|
|
js = strings.TrimPrefix(js, "0x")
|
2019-11-27 09:23:18 +00:00
|
|
|
*u, err = Uint256DecodeStringLE(js)
|
2018-05-09 05:20:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
// MarshalJSON implements the json marshaller interface.
|
|
|
|
func (u Uint256) MarshalJSON() ([]byte, error) {
|
2019-11-27 09:23:18 +00:00
|
|
|
return []byte(`"0x` + u.StringLE() + `"`), nil
|
2018-03-23 20:36:59 +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
|
|
|
|
|
|
|
// CompareTo compares two Uint256 with each other. Possible output: 1, -1, 0
|
|
|
|
// 1 implies u > other.
|
|
|
|
// -1 implies u < other.
|
|
|
|
// 0 implies u = other.
|
|
|
|
func (u Uint256) CompareTo(other Uint256) int { return bytes.Compare(u[:], other[:]) }
|
2019-11-14 08:07:23 +00:00
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable interface.
|
2019-12-09 15:25:15 +00:00
|
|
|
func (u *Uint256) EncodeBinary(w *io.BinWriter) {
|
2019-12-06 15:22:21 +00:00
|
|
|
w.WriteBytes(u[:])
|
2019-11-14 08:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
|
|
|
func (u *Uint256) DecodeBinary(r *io.BinReader) {
|
2019-12-06 15:37:46 +00:00
|
|
|
r.ReadBytes(u[:])
|
2019-11-14 08:07:23 +00:00
|
|
|
}
|