mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-23 13:41:37 +00:00
Merge pull request #1145 from nspcc-dev/fix/feeperbyte
Cache `transaction.FeePerByte()`
This commit is contained in:
commit
02bc0bdc1e
3 changed files with 39 additions and 2 deletions
|
@ -64,6 +64,9 @@ type Transaction struct {
|
|||
// for correct signing/verification.
|
||||
Network netmode.Magic
|
||||
|
||||
// feePerByte is the ratio of NetworkFee and tx size, used for calculating tx priority.
|
||||
feePerByte int64
|
||||
|
||||
// Hash of the transaction (double SHA256).
|
||||
hash util.Uint256
|
||||
|
||||
|
@ -272,13 +275,22 @@ func NewTransactionFromBytes(network netmode.Magic, b []byte) (*Transaction, err
|
|||
if r.Err != nil {
|
||||
return nil, r.Err
|
||||
}
|
||||
_ = r.ReadB()
|
||||
if r.Err == nil {
|
||||
return nil, errors.New("additional data after the transaction")
|
||||
}
|
||||
tx.feePerByte = tx.NetworkFee / int64(len(b))
|
||||
return tx, nil
|
||||
}
|
||||
|
||||
// FeePerByte returns NetworkFee of the transaction divided by
|
||||
// its size
|
||||
func (t *Transaction) FeePerByte() int64 {
|
||||
return t.NetworkFee / int64(io.GetVarSize(t))
|
||||
if t.feePerByte != 0 {
|
||||
return t.feePerByte
|
||||
}
|
||||
t.feePerByte = t.NetworkFee / int64(io.GetVarSize(t))
|
||||
return t.feePerByte
|
||||
}
|
||||
|
||||
// transactionJSON is a wrapper for Transaction and
|
||||
|
|
|
@ -71,6 +71,26 @@ func TestNew(t *testing.T) {
|
|||
testserdes.EncodeDecodeBinary(t, tx, &Transaction{Network: netmode.UnitTestNet})
|
||||
}
|
||||
|
||||
func TestNewTransactionFromBytes(t *testing.T) {
|
||||
script := []byte{0x51}
|
||||
tx := New(netmode.UnitTestNet, script, 1)
|
||||
tx.NetworkFee = 123
|
||||
data, err := testserdes.EncodeBinary(tx)
|
||||
require.NoError(t, err)
|
||||
|
||||
// set cached fields
|
||||
tx.Hash()
|
||||
tx.FeePerByte()
|
||||
|
||||
tx1, err := NewTransactionFromBytes(netmode.UnitTestNet, data)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tx, tx1)
|
||||
|
||||
data = append(data, 42)
|
||||
_, err = NewTransactionFromBytes(netmode.UnitTestNet, data)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestEncodingTXWithNoScript(t *testing.T) {
|
||||
_, err := testserdes.EncodeBinary(new(Transaction))
|
||||
require.Error(t, err)
|
||||
|
|
|
@ -498,7 +498,12 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return c.GetRawTransactionVerbose(hash)
|
||||
out, err := c.GetRawTransactionVerbose(hash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out.Transaction.FeePerByte() // set fee per byte
|
||||
return out, nil
|
||||
},
|
||||
serverResponse: txMoveNeoVerbose,
|
||||
result: func(c *Client) interface{} {
|
||||
|
|
Loading…
Reference in a new issue