transaction: avoid reencoding and reading what can't be read

name               old time/op    new time/op    delta
DecodeFromBytes-8    1.79µs ± 2%    1.46µs ± 4%  -18.44%  (p=0.000 n=10+10)

name               old alloc/op   new alloc/op   delta
DecodeFromBytes-8      800B ± 0%      624B ± 0%  -22.00%  (p=0.000 n=10+10)

name               old allocs/op  new allocs/op  delta
DecodeFromBytes-8      10.0 ± 0%       8.0 ± 0%  -20.00%  (p=0.000 n=10+10)
This commit is contained in:
Roman Khimov 2021-08-20 22:08:09 +03:00
parent d0620b24ec
commit 7808762ba0
4 changed files with 35 additions and 15 deletions

View file

@ -130,7 +130,12 @@ func (t *Transaction) GetAttributes(typ AttrType) []Attribute {
// decodeHashableFields decodes the fields that are used for signing the // decodeHashableFields decodes the fields that are used for signing the
// transaction, which are all fields except the scripts. // transaction, which are all fields except the scripts.
func (t *Transaction) decodeHashableFields(br *io.BinReader) { func (t *Transaction) decodeHashableFields(br *io.BinReader, buf []byte) {
var start, end int
if buf != nil {
start = len(buf) - br.Len()
}
t.Version = uint8(br.ReadB()) t.Version = uint8(br.ReadB())
t.Nonce = br.ReadU32LE() t.Nonce = br.ReadU32LE()
t.SystemFee = int64(br.ReadU64LE()) t.SystemFee = int64(br.ReadU64LE())
@ -164,10 +169,14 @@ func (t *Transaction) decodeHashableFields(br *io.BinReader) {
if br.Err == nil { if br.Err == nil {
br.Err = t.isValid() br.Err = t.isValid()
} }
if buf != nil {
end = len(buf) - br.Len()
t.hash = hash.Sha256(buf[start:end])
}
} }
func (t *Transaction) decodeBinaryNoSize(br *io.BinReader) { func (t *Transaction) decodeBinaryNoSize(br *io.BinReader, buf []byte) {
t.decodeHashableFields(br) t.decodeHashableFields(br, buf)
if br.Err != nil { if br.Err != nil {
return return
} }
@ -186,14 +195,14 @@ func (t *Transaction) decodeBinaryNoSize(br *io.BinReader) {
// Create the hash of the transaction at decode, so we dont need // Create the hash of the transaction at decode, so we dont need
// to do it anymore. // to do it anymore.
if br.Err == nil { if br.Err == nil && buf == nil {
br.Err = t.createHash() br.Err = t.createHash()
} }
} }
// DecodeBinary implements Serializable interface. // DecodeBinary implements Serializable interface.
func (t *Transaction) DecodeBinary(br *io.BinReader) { func (t *Transaction) DecodeBinary(br *io.BinReader) {
t.decodeBinaryNoSize(br) t.decodeBinaryNoSize(br, nil)
if br.Err == nil { if br.Err == nil {
_ = t.Size() _ = t.Size()
@ -258,18 +267,15 @@ func (t *Transaction) createHash() error {
// DecodeHashableFields decodes a part of transaction which should be hashed. // DecodeHashableFields decodes a part of transaction which should be hashed.
func (t *Transaction) DecodeHashableFields(buf []byte) error { func (t *Transaction) DecodeHashableFields(buf []byte) error {
r := io.NewBinReaderFromBuf(buf) r := io.NewBinReaderFromBuf(buf)
t.decodeHashableFields(r) t.decodeHashableFields(r, buf)
if r.Err != nil { if r.Err != nil {
return r.Err return r.Err
} }
// Ensure all the data was read. // Ensure all the data was read.
_ = r.ReadB() if r.Len() != 0 {
if r.Err == nil {
return errors.New("additional data after the signed part") return errors.New("additional data after the signed part")
} }
t.Scripts = make([]Witness, 0) t.Scripts = make([]Witness, 0)
t.hash = hash.Sha256(buf)
return nil return nil
} }
@ -287,12 +293,11 @@ func (t *Transaction) Bytes() []byte {
func NewTransactionFromBytes(b []byte) (*Transaction, error) { func NewTransactionFromBytes(b []byte) (*Transaction, error) {
tx := &Transaction{} tx := &Transaction{}
r := io.NewBinReaderFromBuf(b) r := io.NewBinReaderFromBuf(b)
tx.decodeBinaryNoSize(r) tx.decodeBinaryNoSize(r, b)
if r.Err != nil { if r.Err != nil {
return nil, r.Err return nil, r.Err
} }
_ = r.ReadB() if r.Len() != 0 {
if r.Err == nil {
return nil, errors.New("additional data after the transaction") return nil, errors.New("additional data after the transaction")
} }
tx.size = len(b) tx.size = len(b)

View file

@ -97,6 +97,11 @@ func TestNewTransactionFromBytes(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tx, tx1) require.Equal(t, tx, tx1)
tx2 := new(Transaction)
err = testserdes.DecodeBinary(data, tx2)
require.NoError(t, err)
require.Equal(t, tx1, tx2)
data = append(data, 42) data = append(data, 42)
_, err = NewTransactionFromBytes(data) _, err = NewTransactionFromBytes(data)
require.Error(t, err) require.Error(t, err)

View file

@ -31,6 +31,17 @@ func NewBinReaderFromBuf(b []byte) *BinReader {
return NewBinReaderFromIO(r) return NewBinReaderFromIO(r)
} }
// Len returns the number of bytes of the unread portion of the buffer if
// reading from bytes.Reader or -1 otherwise.
func (r *BinReader) Len() int {
var res = -1
byteReader, ok := r.r.(*bytes.Reader)
if ok {
res = byteReader.Len()
}
return res
}
// ReadU64LE reads a little-endian encoded uint64 value from the underlying // ReadU64LE reads a little-endian encoded uint64 value from the underlying
// io.Reader. On read failures it returns zero. // io.Reader. On read failures it returns zero.
func (r *BinReader) ReadU64LE() uint64 { func (r *BinReader) ReadU64LE() uint64 {

View file

@ -29,8 +29,7 @@ func NewP2PNotaryRequestFromBytes(b []byte) (*P2PNotaryRequest, error) {
if br.Err != nil { if br.Err != nil {
return nil, br.Err return nil, br.Err
} }
_ = br.ReadB() if br.Len() != 0 {
if br.Err == nil {
return nil, errors.New("additional data after the payload") return nil, errors.New("additional data after the payload")
} }
return req, nil return req, nil