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:
parent
d0620b24ec
commit
7808762ba0
4 changed files with 35 additions and 15 deletions
|
@ -130,7 +130,12 @@ func (t *Transaction) GetAttributes(typ AttrType) []Attribute {
|
|||
|
||||
// decodeHashableFields decodes the fields that are used for signing the
|
||||
// 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.Nonce = br.ReadU32LE()
|
||||
t.SystemFee = int64(br.ReadU64LE())
|
||||
|
@ -164,10 +169,14 @@ func (t *Transaction) decodeHashableFields(br *io.BinReader) {
|
|||
if br.Err == nil {
|
||||
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) {
|
||||
t.decodeHashableFields(br)
|
||||
func (t *Transaction) decodeBinaryNoSize(br *io.BinReader, buf []byte) {
|
||||
t.decodeHashableFields(br, buf)
|
||||
if br.Err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -186,14 +195,14 @@ func (t *Transaction) decodeBinaryNoSize(br *io.BinReader) {
|
|||
|
||||
// Create the hash of the transaction at decode, so we dont need
|
||||
// to do it anymore.
|
||||
if br.Err == nil {
|
||||
if br.Err == nil && buf == nil {
|
||||
br.Err = t.createHash()
|
||||
}
|
||||
}
|
||||
|
||||
// DecodeBinary implements Serializable interface.
|
||||
func (t *Transaction) DecodeBinary(br *io.BinReader) {
|
||||
t.decodeBinaryNoSize(br)
|
||||
t.decodeBinaryNoSize(br, nil)
|
||||
|
||||
if br.Err == nil {
|
||||
_ = t.Size()
|
||||
|
@ -258,18 +267,15 @@ func (t *Transaction) createHash() error {
|
|||
// DecodeHashableFields decodes a part of transaction which should be hashed.
|
||||
func (t *Transaction) DecodeHashableFields(buf []byte) error {
|
||||
r := io.NewBinReaderFromBuf(buf)
|
||||
t.decodeHashableFields(r)
|
||||
t.decodeHashableFields(r, buf)
|
||||
if r.Err != nil {
|
||||
return r.Err
|
||||
}
|
||||
// Ensure all the data was read.
|
||||
_ = r.ReadB()
|
||||
if r.Err == nil {
|
||||
if r.Len() != 0 {
|
||||
return errors.New("additional data after the signed part")
|
||||
}
|
||||
t.Scripts = make([]Witness, 0)
|
||||
|
||||
t.hash = hash.Sha256(buf)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -287,12 +293,11 @@ func (t *Transaction) Bytes() []byte {
|
|||
func NewTransactionFromBytes(b []byte) (*Transaction, error) {
|
||||
tx := &Transaction{}
|
||||
r := io.NewBinReaderFromBuf(b)
|
||||
tx.decodeBinaryNoSize(r)
|
||||
tx.decodeBinaryNoSize(r, b)
|
||||
if r.Err != nil {
|
||||
return nil, r.Err
|
||||
}
|
||||
_ = r.ReadB()
|
||||
if r.Err == nil {
|
||||
if r.Len() != 0 {
|
||||
return nil, errors.New("additional data after the transaction")
|
||||
}
|
||||
tx.size = len(b)
|
||||
|
|
|
@ -97,6 +97,11 @@ func TestNewTransactionFromBytes(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
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)
|
||||
_, err = NewTransactionFromBytes(data)
|
||||
require.Error(t, err)
|
||||
|
|
|
@ -31,6 +31,17 @@ func NewBinReaderFromBuf(b []byte) *BinReader {
|
|||
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
|
||||
// io.Reader. On read failures it returns zero.
|
||||
func (r *BinReader) ReadU64LE() uint64 {
|
||||
|
|
|
@ -29,8 +29,7 @@ func NewP2PNotaryRequestFromBytes(b []byte) (*P2PNotaryRequest, error) {
|
|||
if br.Err != nil {
|
||||
return nil, br.Err
|
||||
}
|
||||
_ = br.ReadB()
|
||||
if br.Err == nil {
|
||||
if br.Len() != 0 {
|
||||
return nil, errors.New("additional data after the payload")
|
||||
}
|
||||
return req, nil
|
||||
|
|
Loading…
Reference in a new issue