2021-08-03 18:35:25 +03:00
|
|
|
// Copyright 2021 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2023-05-11 10:19:42 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2024-01-23 16:51:58 +03:00
|
|
|
"github.com/holiman/uint256"
|
2021-08-03 18:35:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func filledStateDB() *StateDB {
|
2023-05-11 10:19:42 +03:00
|
|
|
state, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2021-08-03 18:35:25 +03:00
|
|
|
|
|
|
|
// Create an account and check if the retrieved balance is correct
|
|
|
|
addr := common.HexToAddress("0xaffeaffeaffeaffeaffeaffeaffeaffeaffeaffe")
|
|
|
|
skey := common.HexToHash("aaa")
|
|
|
|
sval := common.HexToHash("bbb")
|
|
|
|
|
2024-01-23 16:51:58 +03:00
|
|
|
state.SetBalance(addr, uint256.NewInt(42)) // Change the account trie
|
|
|
|
state.SetCode(addr, []byte("hello")) // Change an external metadata
|
|
|
|
state.SetState(addr, skey, sval) // Change the storage trie
|
2021-08-03 18:35:25 +03:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
sk := common.BigToHash(big.NewInt(int64(i)))
|
|
|
|
state.SetState(addr, sk, sk) // Change the storage trie
|
|
|
|
}
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2023-08-23 12:46:08 +03:00
|
|
|
func prefetchGuaranteed(prefetcher *triePrefetcher, owner common.Hash, root common.Hash, addr common.Address, keys [][]byte) {
|
|
|
|
prefetcher.prefetch(owner, root, addr, keys)
|
[R4R]: Redesign triePrefetcher to make it thread safe (#972)
* Redesign triePrefetcher to make it thread safe
There are 2 types of triePrefetcher instances:
1.New created triePrefetcher: it is key to do trie prefetch to speed up validation phase.
2.Copied triePrefetcher: it only copy the prefetched trie information, actually it won't do
prefetch at all, the copied tries are all kept in p.fetches.
Here we try to improve the new created one, to make it concurrent safe, while the copied one's
behavior stay unchanged(its logic is very simple).
As commented in triePrefetcher struct, its APIs are not thread safe. So callers should make sure
the created triePrefetcher should be used within a single routine.
As we are trying to improve triePrefetcher, we would use it concurrently, so it is necessary to
redesign it for concurrent access.
The design is simple:
** start a mainLoop to do all the work, APIs just send channel message.
Others:
** remove the metrics copy, since it is useless for copied triePrefetcher
** for trie(), only get subfetcher through channel to reduce the workload of mainloop
* some code enhancement for triePrefetcher redesign
* some fixup: rename, temporary trie chan for concurrent safe.
* fix review comments
* add some protection in case the trie prefetcher is already stopped
* fix review comments
** make close concurrent safe
** fix potential deadlock
* replace channel by RWMutex for a few triePrefetcher APIs
For APIs like: trie(), copy(), used(), it is simpler and more efficient to
use a RWMutex instead of channel communicaton.
Since the mainLoop would be busy handling trie request, while these trie request
can be processed in parallism.
We would only keep prefetch and close within the mainLoop, since they could update
the fetchers
* add lock for subfecter.used access to make it concurrent safe
* no need to create channel for copied triePrefetcher
* fix trie_prefetcher_test.go
trie prefetcher’s behavior has changed, prefetch() won't create subfetcher immediately.
it is reasonable, but break the UT, to fix the failed UT
2022-07-07 05:00:09 +03:00
|
|
|
for {
|
|
|
|
if len(prefetcher.prefetchChan) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 18:35:25 +03:00
|
|
|
func TestCopyAndClose(t *testing.T) {
|
|
|
|
db := filledStateDB()
|
2022-07-29 09:21:37 +03:00
|
|
|
prefetcher := newTriePrefetcher(db.db, db.originalRoot, common.Hash{}, "")
|
2021-08-03 18:35:25 +03:00
|
|
|
skey := common.HexToHash("aaa")
|
2023-08-23 12:46:08 +03:00
|
|
|
prefetchGuaranteed(prefetcher, common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
|
|
|
prefetchGuaranteed(prefetcher, common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
2021-08-03 18:35:25 +03:00
|
|
|
time.Sleep(1 * time.Second)
|
2022-06-06 18:14:55 +03:00
|
|
|
a := prefetcher.trie(common.Hash{}, db.originalRoot)
|
2023-08-23 12:46:08 +03:00
|
|
|
prefetchGuaranteed(prefetcher, common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
2022-06-06 18:14:55 +03:00
|
|
|
b := prefetcher.trie(common.Hash{}, db.originalRoot)
|
2021-08-03 18:35:25 +03:00
|
|
|
cpy := prefetcher.copy()
|
2023-08-23 12:46:08 +03:00
|
|
|
prefetchGuaranteed(cpy, common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
|
|
|
prefetchGuaranteed(cpy, common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
2022-06-06 18:14:55 +03:00
|
|
|
c := cpy.trie(common.Hash{}, db.originalRoot)
|
2021-08-03 18:35:25 +03:00
|
|
|
prefetcher.close()
|
|
|
|
cpy2 := cpy.copy()
|
2023-08-23 12:46:08 +03:00
|
|
|
prefetchGuaranteed(cpy2, common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
2022-06-06 18:14:55 +03:00
|
|
|
d := cpy2.trie(common.Hash{}, db.originalRoot)
|
2021-08-03 18:35:25 +03:00
|
|
|
cpy.close()
|
|
|
|
cpy2.close()
|
|
|
|
if a.Hash() != b.Hash() || a.Hash() != c.Hash() || a.Hash() != d.Hash() {
|
|
|
|
t.Fatalf("Invalid trie, hashes should be equal: %v %v %v %v", a.Hash(), b.Hash(), c.Hash(), d.Hash())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUseAfterClose(t *testing.T) {
|
|
|
|
db := filledStateDB()
|
2022-07-29 09:21:37 +03:00
|
|
|
prefetcher := newTriePrefetcher(db.db, db.originalRoot, common.Hash{}, "")
|
2021-08-03 18:35:25 +03:00
|
|
|
skey := common.HexToHash("aaa")
|
2023-08-23 12:46:08 +03:00
|
|
|
prefetchGuaranteed(prefetcher, common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
2022-06-06 18:14:55 +03:00
|
|
|
a := prefetcher.trie(common.Hash{}, db.originalRoot)
|
2021-08-03 18:35:25 +03:00
|
|
|
prefetcher.close()
|
2022-06-06 18:14:55 +03:00
|
|
|
b := prefetcher.trie(common.Hash{}, db.originalRoot)
|
2021-08-03 18:35:25 +03:00
|
|
|
if a == nil {
|
|
|
|
t.Fatal("Prefetching before close should not return nil")
|
|
|
|
}
|
|
|
|
if b != nil {
|
|
|
|
t.Fatal("Trie after close should return nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopyClose(t *testing.T) {
|
|
|
|
db := filledStateDB()
|
2022-07-29 09:21:37 +03:00
|
|
|
prefetcher := newTriePrefetcher(db.db, db.originalRoot, common.Hash{}, "")
|
2021-08-03 18:35:25 +03:00
|
|
|
skey := common.HexToHash("aaa")
|
2023-08-23 12:46:08 +03:00
|
|
|
prefetchGuaranteed(prefetcher, common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
2021-08-03 18:35:25 +03:00
|
|
|
cpy := prefetcher.copy()
|
2022-06-06 18:14:55 +03:00
|
|
|
a := prefetcher.trie(common.Hash{}, db.originalRoot)
|
|
|
|
b := cpy.trie(common.Hash{}, db.originalRoot)
|
2021-08-03 18:35:25 +03:00
|
|
|
prefetcher.close()
|
2022-06-06 18:14:55 +03:00
|
|
|
c := prefetcher.trie(common.Hash{}, db.originalRoot)
|
|
|
|
d := cpy.trie(common.Hash{}, db.originalRoot)
|
2021-08-03 18:35:25 +03:00
|
|
|
if a == nil {
|
|
|
|
t.Fatal("Prefetching before close should not return nil")
|
|
|
|
}
|
|
|
|
if b == nil {
|
|
|
|
t.Fatal("Copy trie should return nil")
|
|
|
|
}
|
|
|
|
if c != nil {
|
|
|
|
t.Fatal("Trie after close should return nil")
|
|
|
|
}
|
|
|
|
if d == nil {
|
|
|
|
t.Fatal("Copy trie should not return nil")
|
|
|
|
}
|
|
|
|
}
|