From 2e396d792860f94deb9bb1f464cab9b5c4bcdd98 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Tue, 2 Jul 2024 10:10:24 +0800 Subject: [PATCH 1/5] Problem: redundant copy in check validate --- CHANGELOG.md | 1 + versiondb/tsrocksdb/iterator.go | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a1e723cb8..2b083f3c80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ * [#1488](https://github.com/crypto-org-chain/cronos/pull/1488) Enable optimistic execution. * [#1490](https://github.com/crypto-org-chain/cronos/pull/1490) Update cometbft to v0.38.8. * [#1491](https://github.com/crypto-org-chain/cronos/pull/1491) Free slice data in HasAtVersion. +* [#1498](https://github.com/crypto-org-chain/cronos/pull/1498) Check validity with extractSliceBytes to avoid copying slice data. ### Bug Fixes diff --git a/versiondb/tsrocksdb/iterator.go b/versiondb/tsrocksdb/iterator.go index 6a2ac7d588..523b804d07 100644 --- a/versiondb/tsrocksdb/iterator.go +++ b/versiondb/tsrocksdb/iterator.go @@ -23,7 +23,7 @@ func newRocksDBIterator(source *grocksdb.Iterator, prefix, start, end []byte, is } else { source.Seek(end) if source.Valid() { - eoakey := moveSliceToBytes(source.Key()) // end or after key + eoakey := extractSliceBytes(source.Key()) // end or after key if bytes.Compare(end, eoakey) <= 0 { source.Prev() } @@ -75,7 +75,7 @@ func (itr *rocksDBIterator) Valid() bool { // If key is end or past it, invalid. start := itr.start end := itr.end - key := moveSliceToBytes(itr.source.Key()) + key := extractSliceBytes(itr.source.Key()) if itr.isReverse { if start != nil && bytes.Compare(key, start) < 0 { itr.isInvalid = true @@ -143,3 +143,13 @@ func moveSliceToBytes(s *grocksdb.Slice) []byte { copy(v, s.Data()) return v } + +// extractSliceBytes returns the underlying []byte from the given *Slice +// without copying the data. Use this function with caution as the *Slice +// should not be used after calling this function, as it is not marked as freed. +func extractSliceBytes(s *grocksdb.Slice) []byte { + if !s.Exists() { + return nil + } + return s.Data() +} From 1335397c45dd29a6df059d22f4e053365d0df5e0 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Tue, 2 Jul 2024 10:50:31 +0800 Subject: [PATCH 2/5] free key --- versiondb/tsrocksdb/iterator.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/versiondb/tsrocksdb/iterator.go b/versiondb/tsrocksdb/iterator.go index 523b804d07..8d12be1e0f 100644 --- a/versiondb/tsrocksdb/iterator.go +++ b/versiondb/tsrocksdb/iterator.go @@ -23,8 +23,11 @@ func newRocksDBIterator(source *grocksdb.Iterator, prefix, start, end []byte, is } else { source.Seek(end) if source.Valid() { - eoakey := extractSliceBytes(source.Key()) // end or after key - if bytes.Compare(end, eoakey) <= 0 { + key := source.Key() + eoakey := extractSliceBytes(key) // end or after key + diff := bytes.Compare(end, eoakey) + key.Free() + if diff <= 0 { source.Prev() } } else { @@ -75,7 +78,9 @@ func (itr *rocksDBIterator) Valid() bool { // If key is end or past it, invalid. start := itr.start end := itr.end - key := extractSliceBytes(itr.source.Key()) + srcKey := itr.source.Key() + key := extractSliceBytes(srcKey) + defer srcKey.Free() if itr.isReverse { if start != nil && bytes.Compare(key, start) < 0 { itr.isInvalid = true @@ -87,7 +92,6 @@ func (itr *rocksDBIterator) Valid() bool { return false } } - // It's valid. return true } From f46cfc12c6a7491d3e96aa0c74068385dd0a5583 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Tue, 2 Jul 2024 11:09:57 +0800 Subject: [PATCH 3/5] update doc --- CHANGELOG.md | 63 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b083f3c80..35314afaff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,33 +6,75 @@ * [#1377](https://github.com/crypto-org-chain/cronos/pull/1377) Upgrade sdk to 0.50, and integrate block-stm parallel tx execution. * [#1394](https://github.com/crypto-org-chain/cronos/pull/1394) Add icahost wirings but disable in parameters. -* [#1407](https://github.com/crypto-org-chain/cronos/pull/1407) Add end-to-end encryption module. * [#1414](https://github.com/crypto-org-chain/cronos/pull/1414) Integrate new evm tx format. * [#1458](https://github.com/crypto-org-chain/cronos/pull/1458) Adjust require gas for recvPacket when ReceiverChainIsSource. ### Improvements -* (test) [#1380](https://github.com/crypto-org-chain/cronos/pull/1380) Upgrade cosmovisor to 1.5.0 in integration test. -* (versiondb) [#1379](https://github.com/crypto-org-chain/cronos/pull/1379) Flush versiondb when graceful shutdown, make rocksdb upgrade smooth. * (store) [#1378](https://github.com/crypto-org-chain/cronos/pull/1378) Upgrade rocksdb to `v8.11.3`. * (versiondb) [#1387](https://github.com/crypto-org-chain/cronos/pull/1387) Add dedicated config section for versiondb, prepare for sdk 0.50 integration. -* (e2ee)[#1413](https://github.com/crypto-org-chain/cronos/pull/1413) Add custom keyring implementation for e2ee module. -* (e2ee)[#1415](https://github.com/crypto-org-chain/cronos/pull/1415) Add batch keys query for e2ee module. -* (e2ee)[#1421](https://github.com/crypto-org-chain/cronos/pull/1421) Validate e2ee key when register. * (store) [#1448](https://github.com/crypto-org-chain/cronos/pull/1448) Upgrade rocksdb to `v9.1.1`. * [#1431](https://github.com/crypto-org-chain/cronos/pull/1431) Integrate testground to run benchmark on cluster. * [#1464](https://github.com/crypto-org-chain/cronos/pull/1464) Update cosmos-sdk to `v0.50.7`. -* (rpc) [#1467](https://github.com/crypto-org-chain/cronos/pull/1467) Avoid unnecessary tx decode in tx listener. * [#1484](https://github.com/crypto-org-chain/cronos/pull/1484), [#1487](https://github.com/crypto-org-chain/cronos/pull/1487) Respect gas that is wanted to be returned by the ante handler. * [#1488](https://github.com/crypto-org-chain/cronos/pull/1488) Enable optimistic execution. * [#1490](https://github.com/crypto-org-chain/cronos/pull/1490) Update cometbft to v0.38.8. -* [#1491](https://github.com/crypto-org-chain/cronos/pull/1491) Free slice data in HasAtVersion. -* [#1498](https://github.com/crypto-org-chain/cronos/pull/1498) Check validity with extractSliceBytes to avoid copying slice data. +* (versiondb) [#1491](https://github.com/crypto-org-chain/cronos/pull/1491) Free slice data in HasAtVersion. +* (versiondb) [#1498](https://github.com/crypto-org-chain/cronos/pull/1498) Check validity with extractSliceBytes to avoid copying slice data. + + +*Jun 18, 2024* + +## v1.3.0-rc2 + +### Improvements + +* (rpc) [#1467](https://github.com/crypto-org-chain/cronos/pull/1467) Avoid unnecessary tx decode in tx listener. + +### Bug Fixes + +* [#1466](https://github.com/crypto-org-chain/cronos/pull/1466) Fix handling of pending transactions related APIs. + +*May 21, 2024* + +## v1.3.0-rc1 + +### State Machine Breaking + +* [#1407](https://github.com/crypto-org-chain/cronos/pull/1407) Add end-to-end encryption module. + +### Improvements + +* [#1413](https://github.com/crypto-org-chain/cronos/pull/1413) Add custom keyring implementation for e2ee module. +* (e2ee)[#1415](https://github.com/crypto-org-chain/cronos/pull/1415) Add batch keys query for e2ee module. +* (e2ee)[#1421](https://github.com/crypto-org-chain/cronos/pull/1421) Validate e2ee key when register. +* [#1437](https://github.com/crypto-org-chain/cronos/pull/1437) Update cometbft and cosmos-sdk dependencies. + +### Bug Fixes + +* (rpc) [#1444](https://github.com/crypto-org-chain/cronos/pull/1444) Avoid nil pointer error when query blocks before feemarket module gets enabled. +* [#1439](https://github.com/crypto-org-chain/cronos/pull/1439) Add back default prepare proposal logic. + +*May 3, 2024* + +## v1.2.2 + +### Bug Fixes + +* (rpc) [#1416](https://github.com/crypto-org-chain/cronos/pull/1416) Fix parsed logs from old events. + +*April 22, 2024* + +## v1.2.1 + +### Improvements + +* (test) [#1380](https://github.com/crypto-org-chain/cronos/pull/1380) Upgrade cosmovisor to 1.5.0 in integration test. +* (versiondb) [#1379](https://github.com/crypto-org-chain/cronos/pull/1379) Flush versiondb when graceful shutdown, make rocksdb upgrade smooth. ### Bug Fixes * (rpc) [#1397](https://github.com/crypto-org-chain/cronos/pull/1397) Avoid panic on invalid elasticity_multiplier. -* (rpc) [#1466](https://github.com/crypto-org-chain/cronos/pull/1466) Fix handling of pending transactions related APIs. ### Features @@ -41,6 +83,7 @@ *April 8, 2024* +## v1.2.0 ## v1.2.0-rc1 ### Bug Fixes From 5bebb98c6fed8ea201ad62be9a87954d2678ad86 Mon Sep 17 00:00:00 2001 From: huangyi Date: Tue, 2 Jul 2024 12:17:27 +0800 Subject: [PATCH 4/5] cleanup --- versiondb/tsrocksdb/iterator.go | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/versiondb/tsrocksdb/iterator.go b/versiondb/tsrocksdb/iterator.go index 8d12be1e0f..02065a9980 100644 --- a/versiondb/tsrocksdb/iterator.go +++ b/versiondb/tsrocksdb/iterator.go @@ -23,11 +23,9 @@ func newRocksDBIterator(source *grocksdb.Iterator, prefix, start, end []byte, is } else { source.Seek(end) if source.Valid() { - key := source.Key() - eoakey := extractSliceBytes(key) // end or after key - diff := bytes.Compare(end, eoakey) - key.Free() - if diff <= 0 { + eoakey := source.Key() // end or after key + defer eoakey.Free() + if bytes.Compare(end, eoakey.Data()) <= 0 { source.Prev() } } else { @@ -78,20 +76,20 @@ func (itr *rocksDBIterator) Valid() bool { // If key is end or past it, invalid. start := itr.start end := itr.end - srcKey := itr.source.Key() - key := extractSliceBytes(srcKey) - defer srcKey.Free() + key := itr.source.Key() + defer key.Free() if itr.isReverse { - if start != nil && bytes.Compare(key, start) < 0 { + if start != nil && bytes.Compare(key.Data(), start) < 0 { itr.isInvalid = true return false } } else { - if end != nil && bytes.Compare(end, key) <= 0 { + if end != nil && bytes.Compare(end, key.Data()) <= 0 { itr.isInvalid = true return false } } + // It's valid. return true } @@ -147,13 +145,3 @@ func moveSliceToBytes(s *grocksdb.Slice) []byte { copy(v, s.Data()) return v } - -// extractSliceBytes returns the underlying []byte from the given *Slice -// without copying the data. Use this function with caution as the *Slice -// should not be used after calling this function, as it is not marked as freed. -func extractSliceBytes(s *grocksdb.Slice) []byte { - if !s.Exists() { - return nil - } - return s.Data() -} From 50d5a4a0612f1d1a0c07185d968c3387c51ab399 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Tue, 2 Jul 2024 14:59:07 +0800 Subject: [PATCH 5/5] update doc --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35314afaff..123a71a3eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ * [#1488](https://github.com/crypto-org-chain/cronos/pull/1488) Enable optimistic execution. * [#1490](https://github.com/crypto-org-chain/cronos/pull/1490) Update cometbft to v0.38.8. * (versiondb) [#1491](https://github.com/crypto-org-chain/cronos/pull/1491) Free slice data in HasAtVersion. -* (versiondb) [#1498](https://github.com/crypto-org-chain/cronos/pull/1498) Check validity with extractSliceBytes to avoid copying slice data. +* (versiondb) [#1498](https://github.com/crypto-org-chain/cronos/pull/1498) Reduce scope of copying slice data in iterator. *Jun 18, 2024*