2019-09-16 09:33:53 +00:00
|
|
|
package io
|
2019-02-20 17:39:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2019-09-16 12:58:26 +00:00
|
|
|
// This structure is used to calculate the wire size of the serializable
|
|
|
|
// structure. It's an io.Writer that doesn't do any real writes, but instead
|
|
|
|
// just counts the number of bytes to be written.
|
|
|
|
type counterWriter struct {
|
|
|
|
counter int
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Write implements the io.Writer interface.
|
2019-09-16 12:58:26 +00:00
|
|
|
func (cw *counterWriter) Write(p []byte) (int, error) {
|
|
|
|
n := len(p)
|
|
|
|
cw.counter += n
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// getVarIntSize returns the size in number of bytes of a variable integer.
|
2019-02-20 17:39:32 +00:00
|
|
|
// (reference: GetVarSize(int value), https://github.com/neo-project/neo/blob/master/neo/IO/Helper.cs)
|
2019-09-20 16:08:58 +00:00
|
|
|
func getVarIntSize(value int) int {
|
2019-02-20 17:39:32 +00:00
|
|
|
var size uintptr
|
|
|
|
|
|
|
|
if value < 0xFD {
|
|
|
|
size = 1 // unit8
|
|
|
|
} else if value <= 0xFFFF {
|
|
|
|
size = 3 // byte + uint16
|
|
|
|
} else {
|
|
|
|
size = 5 // byte + uint32
|
|
|
|
}
|
|
|
|
return int(size)
|
|
|
|
}
|
|
|
|
|
2019-09-20 16:10:13 +00:00
|
|
|
// GetVarSize returns the number of bytes in a serialized variable. It supports ints/uints (estimating
|
2022-12-07 13:51:03 +00:00
|
|
|
// them with variable-length encoding that is used in Neo), strings, pointers to Serializable structures,
|
2019-09-20 16:10:13 +00:00
|
|
|
// slices and arrays of ints/uints or Serializable structures. It's similar to GetVarSize<T>(this T[] value)
|
|
|
|
// used in C#, but differs in that it also supports things like Uint160 or Uint256.
|
2023-04-03 10:34:24 +00:00
|
|
|
func GetVarSize(value any) int {
|
2019-02-20 17:39:32 +00:00
|
|
|
v := reflect.ValueOf(value)
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.String:
|
2019-09-20 17:01:56 +00:00
|
|
|
valueSize := len([]byte(v.String()))
|
|
|
|
return getVarIntSize(valueSize) + valueSize
|
2019-02-20 17:39:32 +00:00
|
|
|
case reflect.Int,
|
|
|
|
reflect.Int8,
|
|
|
|
reflect.Int16,
|
|
|
|
reflect.Int32,
|
|
|
|
reflect.Int64:
|
2019-09-20 16:08:58 +00:00
|
|
|
return getVarIntSize(int(v.Int()))
|
2019-02-20 17:39:32 +00:00
|
|
|
case reflect.Uint,
|
|
|
|
reflect.Uint8,
|
|
|
|
reflect.Uint16,
|
|
|
|
reflect.Uint32,
|
|
|
|
reflect.Uint64:
|
2019-09-20 16:08:58 +00:00
|
|
|
return getVarIntSize(int(v.Uint()))
|
2019-09-16 12:58:26 +00:00
|
|
|
case reflect.Ptr:
|
|
|
|
vser, ok := v.Interface().(Serializable)
|
|
|
|
if !ok {
|
2021-05-12 15:29:39 +00:00
|
|
|
panic("unable to calculate GetVarSize for a non-Serializable pointer")
|
2019-09-16 12:58:26 +00:00
|
|
|
}
|
|
|
|
cw := counterWriter{}
|
|
|
|
w := NewBinWriterFromIO(&cw)
|
2019-09-16 16:31:49 +00:00
|
|
|
vser.EncodeBinary(w)
|
|
|
|
if w.Err != nil {
|
|
|
|
panic(fmt.Sprintf("error serializing %s: %s", reflect.TypeOf(value), w.Err.Error()))
|
2019-09-16 12:58:26 +00:00
|
|
|
}
|
|
|
|
return cw.counter
|
2019-02-20 17:39:32 +00:00
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
valueLength := v.Len()
|
|
|
|
valueSize := 0
|
|
|
|
|
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
|
|
|
if valueLength != 0 {
|
2024-11-15 19:28:53 +00:00
|
|
|
switch v.Index(0).Interface().(type) {
|
2019-09-16 09:33:53 +00:00
|
|
|
case Serializable:
|
2024-08-30 18:41:02 +00:00
|
|
|
for i := range valueLength {
|
2019-09-16 12:58:26 +00:00
|
|
|
valueSize += GetVarSize(v.Index(i).Interface())
|
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
|
|
|
}
|
|
|
|
case uint8, int8:
|
|
|
|
valueSize = valueLength
|
|
|
|
case uint16, int16:
|
|
|
|
valueSize = valueLength * 2
|
|
|
|
case uint32, int32:
|
|
|
|
valueSize = valueLength * 4
|
|
|
|
case uint64, int64:
|
|
|
|
valueSize = valueLength * 8
|
2019-02-20 17:39:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 16:08:58 +00:00
|
|
|
return getVarIntSize(valueLength) + valueSize
|
2019-02-20 17:39:32 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unable to calculate GetVarSize, %s", reflect.TypeOf(value)))
|
|
|
|
}
|
|
|
|
}
|