From 90c3248c7998810881807081c082de0511613cd7 Mon Sep 17 00:00:00 2001 From: Tsachi Herman Date: Wed, 9 Feb 2022 14:20:50 -0500 Subject: [PATCH 1/2] fix performance regression. --- data/bookkeeping/txn_merkle.go | 6 +++--- data/bookkeeping/txn_merkle_test.go | 14 +++++++------- test/e2e-go/features/transactions/proof_test.go | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/data/bookkeeping/txn_merkle.go b/data/bookkeeping/txn_merkle.go index 4f6317fc88..9a806708e3 100644 --- a/data/bookkeeping/txn_merkle.go +++ b/data/bookkeeping/txn_merkle.go @@ -70,7 +70,7 @@ type txnMerkleElem struct { stib transactions.SignedTxnInBlock } -func txnMerkleToRaw(txid []byte, stib []byte) (buf []byte) { +func txnMerkleToRaw(txid [crypto.DigestSize]byte, stib [crypto.DigestSize]byte) (buf []byte) { buf = make([]byte, 2*crypto.DigestSize) copy(buf[:], txid[:]) copy(buf[crypto.DigestSize:], stib[:]) @@ -84,7 +84,7 @@ func (tme *txnMerkleElem) ToBeHashed() (protocol.HashID, []byte) { txid := tme.txn.ID() stib := tme.stib.Hash() - return protocol.TxnMerkleLeaf, txnMerkleToRaw(txid[:], stib[:]) + return protocol.TxnMerkleLeaf, txnMerkleToRaw(txid, stib) } // Hash implements an optimized version of crypto.HashObj(tme). @@ -99,6 +99,6 @@ func (tme *txnMerkleElem) HashRepresentation() []byte { var buf [len(protocol.TxnMerkleLeaf) + 2*crypto.DigestSize]byte s := buf[:0] s = append(s, protocol.TxnMerkleLeaf...) - s = append(s, txnMerkleToRaw(txid[:], stib[:])...) + s = append(s, txnMerkleToRaw(txid, stib)...) return s } diff --git a/data/bookkeeping/txn_merkle_test.go b/data/bookkeeping/txn_merkle_test.go index dfd6939adf..2dabd07bd0 100644 --- a/data/bookkeeping/txn_merkle_test.go +++ b/data/bookkeeping/txn_merkle_test.go @@ -143,23 +143,23 @@ func BenchmarkTxnRoots(b *testing.B) { _ = r } +func txnMerkleToRawAppend(txid [crypto.DigestSize]byte, stib [crypto.DigestSize]byte) []byte { + buf := make([]byte, 0, 2*crypto.DigestSize) + buf = append(buf, txid[:]...) + return append(buf, stib[:]...) +} func BenchmarkTxnMerkleToRaw(b *testing.B) { digest1 := crypto.Hash([]byte{1, 2, 3}) digest2 := crypto.Hash([]byte{4, 5, 6}) - txnMerkleToRawAppend := func(txid []byte, stib []byte) []byte { - buf := make([]byte, 0, 2*crypto.DigestSize) - buf = append(buf, txid...) - return append(buf, stib...) - } b.Run("copy", func(b *testing.B) { for i := 0; i < b.N; i++ { - txnMerkleToRaw(digest1[:], digest2[:]) + txnMerkleToRaw(digest1, digest2) } }) b.Run("append", func(b *testing.B) { for i := 0; i < b.N; i++ { - txnMerkleToRawAppend(digest1[:], digest2[:]) + txnMerkleToRawAppend(digest1, digest2) } }) } diff --git a/test/e2e-go/features/transactions/proof_test.go b/test/e2e-go/features/transactions/proof_test.go index 618dc3b227..ecf1bf81d5 100644 --- a/test/e2e-go/features/transactions/proof_test.go +++ b/test/e2e-go/features/transactions/proof_test.go @@ -34,7 +34,7 @@ type TxnMerkleElemRaw struct { Stib []byte // hash value of transactions.SignedTxnInBlock } -func txnMerkleToRaw(txid []byte, stib []byte) (buf []byte) { +func txnMerkleToRaw(txid [crypto.DigestSize]byte, stib [crypto.DigestSize]byte) (buf []byte) { buf = make([]byte, 2*crypto.DigestSize) copy(buf[:], txid[:]) copy(buf[crypto.DigestSize:], stib[:]) From aea93748d24833e451d4f64ffd6094f4469b8f74 Mon Sep 17 00:00:00 2001 From: Tsachi Herman Date: Wed, 9 Feb 2022 15:00:06 -0500 Subject: [PATCH 2/2] fix broken test --- test/e2e-go/features/transactions/proof_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/e2e-go/features/transactions/proof_test.go b/test/e2e-go/features/transactions/proof_test.go index ecf1bf81d5..d38d874d55 100644 --- a/test/e2e-go/features/transactions/proof_test.go +++ b/test/e2e-go/features/transactions/proof_test.go @@ -30,8 +30,8 @@ import ( // TxnMerkleElemRaw this struct helps creates a hashable struct from the bytes type TxnMerkleElemRaw struct { - Txn []byte // txn id - Stib []byte // hash value of transactions.SignedTxnInBlock + Txn crypto.Digest // txn id + Stib crypto.Digest // hash value of transactions.SignedTxnInBlock } func txnMerkleToRaw(txid [crypto.DigestSize]byte, stib [crypto.DigestSize]byte) (buf []byte) { @@ -121,7 +121,8 @@ func TestTxnMerkleProof(t *testing.T) { blk, err := client.BookkeepingBlock(confirmedTx.ConfirmedRound) a.NoError(err) - element := TxnMerkleElemRaw{Txn: txid[:], Stib: proofresp.Stibhash} + element := TxnMerkleElemRaw{Txn: crypto.Digest(txid)} + copy(element.Stib[:], proofresp.Stibhash[:]) elems := make(map[uint64]crypto.Hashable)