avoid unnecessary copy

This commit is contained in:
maskpp 2024-06-18 10:09:13 +08:00 committed by Gary Rong
parent 27008408a5
commit a2fc0b0d07
3 changed files with 25 additions and 24 deletions

View file

@ -332,11 +332,7 @@ func ServiceGetAccountRangeQuery(chain *core.BlockChain, req *GetAccountRangePac
return nil, nil
}
}
var proofs [][]byte
for _, blob := range proof.List() {
proofs = append(proofs, blob)
}
return accounts, proofs
return accounts, proof.ByteList()
}
func ServiceGetStorageRangesQuery(chain *core.BlockChain, req *GetStorageRangesPacket) ([][]*StorageData, [][]byte) {
@ -438,7 +434,7 @@ func ServiceGetStorageRangesQuery(chain *core.BlockChain, req *GetStorageRangesP
return nil, nil
}
}
for _, blob := range proof.List() {
for _, blob := range proof.ByteList() {
proofs = append(proofs, blob)
}
// Proof terminates the reply as proofs are only added if a node

View file

@ -286,10 +286,7 @@ func createAccountRequestResponse(t *testPeer, root common.Hash, origin common.H
t.logger.Error("Could not prove last item", "error", err)
}
}
for _, blob := range proof.List() {
proofs = append(proofs, blob)
}
return keys, vals, proofs
return keys, vals, proof.ByteList()
}
// defaultStorageRequestHandler is a well-behaving storage request handler
@ -371,7 +368,7 @@ func createStorageRequestResponse(t *testPeer, root common.Hash, accounts []comm
t.logger.Error("Could not prove last item", "error", err)
}
}
for _, blob := range proof.List() {
for _, blob := range proof.ByteList() {
proofs = append(proofs, blob)
}
break
@ -430,7 +427,7 @@ func createStorageRequestResponseAlwaysProve(t *testPeer, root common.Hash, acco
t.logger.Error("Could not prove last item", "error", err)
}
}
for _, blob := range proof.List() {
for _, blob := range proof.ByteList() {
proofs = append(proofs, blob)
}
break
@ -586,9 +583,8 @@ func testSyncBloatedProof(t *testing.T, scheme string) {
source.accountRequestHandler = func(t *testPeer, requestId uint64, root common.Hash, origin common.Hash, limit common.Hash, cap uint64) error {
var (
proofs [][]byte
keys []common.Hash
vals [][]byte
keys []common.Hash
vals [][]byte
)
// The values
for _, entry := range t.accountValues {
@ -618,10 +614,7 @@ func testSyncBloatedProof(t *testing.T, scheme string) {
keys = append(keys[:1], keys[2:]...)
vals = append(vals[:1], vals[2:]...)
}
for _, blob := range proof.List() {
proofs = append(proofs, blob)
}
if err := t.remote.OnAccounts(t, requestId, keys, vals, proofs); err != nil {
if err := t.remote.OnAccounts(t, requestId, keys, vals, proof.ByteList()); err != nil {
t.logger.Info("remote error on delivery (as expected)", "error", err)
t.term()
// This is actually correct, signal to exit the test successfully

View file

@ -102,14 +102,26 @@ func (db *ProofSet) DataSize() int {
return db.dataSize
}
// List converts the node set to a ProofList
func (db *ProofSet) List() ProofList {
// ByteList converts the node set to a [][]byte
func (db *ProofSet) ByteList() [][]byte {
db.lock.RLock()
defer db.lock.RUnlock()
var values ProofList
for _, key := range db.order {
values = append(values, db.nodes[key])
var values = make([][]byte, len(db.order))
for i, key := range db.order {
values[i] = db.nodes[key]
}
return values
}
// ProofList converts the node set to a ProofList
func (db *ProofSet) ProofList() ProofList {
db.lock.RLock()
defer db.lock.RUnlock()
var values = make(ProofList, len(db.order))
for i, key := range db.order {
values[i] = db.nodes[key]
}
return values
}