mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-01-09 21:19:05 +00:00
transaction: specify hash/size behavior better
Signed-off-by: Roman Khimov <roman@nspcc.ru>
This commit is contained in:
parent
c2b4d32224
commit
7af2ab92d2
1 changed files with 19 additions and 6 deletions
|
@ -103,7 +103,11 @@ func New(script []byte, gas int64) *Transaction {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hash returns the hash of the transaction.
|
// Hash returns the hash of the transaction which is based on serialized
|
||||||
|
// representation of its fields. Notice that this hash is cached internally
|
||||||
|
// in [Transaction] for efficiency, so once you call this method it will not
|
||||||
|
// change even if you change any structure fields. If you need to update the
|
||||||
|
// hash use encoding/decoding or [Transaction.Copy].
|
||||||
func (t *Transaction) Hash() util.Uint256 {
|
func (t *Transaction) Hash() util.Uint256 {
|
||||||
if !t.hashed {
|
if !t.hashed {
|
||||||
if t.createHash() != nil {
|
if t.createHash() != nil {
|
||||||
|
@ -208,7 +212,9 @@ func (t *Transaction) decodeBinaryNoSize(br *io.BinReader, buf []byte) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeBinary implements the Serializable interface.
|
// DecodeBinary implements the [io.Serializable] interface. It also
|
||||||
|
// computes and caches transaction hash and size (see [Transaction.Hash] and
|
||||||
|
// [Transaction.Size]).
|
||||||
func (t *Transaction) DecodeBinary(br *io.BinReader) {
|
func (t *Transaction) DecodeBinary(br *io.BinReader) {
|
||||||
t.decodeBinaryNoSize(br, nil)
|
t.decodeBinaryNoSize(br, nil)
|
||||||
|
|
||||||
|
@ -294,7 +300,9 @@ func (t *Transaction) Bytes() []byte {
|
||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTransactionFromBytes decodes byte array into *Transaction.
|
// NewTransactionFromBytes decodes byte array into [*Transaction]. It also
|
||||||
|
// computes and caches transaction hash and size (see [Transaction.Hash] and
|
||||||
|
// [Transaction.Size]).
|
||||||
func NewTransactionFromBytes(b []byte) (*Transaction, error) {
|
func NewTransactionFromBytes(b []byte) (*Transaction, error) {
|
||||||
tx := &Transaction{}
|
tx := &Transaction{}
|
||||||
r := io.NewBinReaderFromBuf(b)
|
r := io.NewBinReaderFromBuf(b)
|
||||||
|
@ -315,7 +323,10 @@ func (t *Transaction) FeePerByte() int64 {
|
||||||
return t.NetworkFee / int64(t.Size())
|
return t.NetworkFee / int64(t.Size())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size returns size of the serialized transaction.
|
// Size returns size of the serialized transaction. This value is cached
|
||||||
|
// in the [Transaction], so once you obtain it no changes to fields will be
|
||||||
|
// reflected in value returned from this method, use encoding/decoding or
|
||||||
|
// [Transaction.Copy] if needed.
|
||||||
func (t *Transaction) Size() int {
|
func (t *Transaction) Size() int {
|
||||||
if t.size == 0 {
|
if t.size == 0 {
|
||||||
t.size = io.GetVarSize(t)
|
t.size = io.GetVarSize(t)
|
||||||
|
@ -469,8 +480,10 @@ func (t *Transaction) ToStackItem() stackitem.Item {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy creates a deep copy of the Transaction, including all slice fields. Cached values like
|
// Copy creates a deep copy of the Transaction, including all slice fields.
|
||||||
// 'hashed' and 'size' are reset to ensure the copy can be modified independently of the original.
|
// Cached values like hash and size are reset to ensure the copy can be
|
||||||
|
// modified independently of the original (see [Transaction.Hash] and
|
||||||
|
// [Transaction.Size]).
|
||||||
func (t *Transaction) Copy() *Transaction {
|
func (t *Transaction) Copy() *Transaction {
|
||||||
if t == nil {
|
if t == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in a new issue