Skip to content

Commit

Permalink
eth/protocols/snap: update
Browse files Browse the repository at this point in the history
  • Loading branch information
rjl493456442 committed Jul 5, 2022
1 parent 7cfb11a commit 7b92f2a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 39 deletions.
27 changes: 9 additions & 18 deletions eth/protocols/snap/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/trie"
)

func hexToNibbles(s string) []byte {
Expand All @@ -38,23 +37,17 @@ func hexToNibbles(s string) []byte {
}

func TestRequestSorting(t *testing.T) {

// - Path 0x9 -> {0x19}
// - Path 0x99 -> {0x0099}
// - Path 0x01234567890123456789012345678901012345678901234567890123456789019 -> {0x0123456789012345678901234567890101234567890123456789012345678901, 0x19}
// - Path 0x012345678901234567890123456789010123456789012345678901234567890199 -> {0x0123456789012345678901234567890101234567890123456789012345678901, 0x0099}
var f = func(path string) (trie.SyncPath, TrieNodePathSet, common.Hash) {
var f = func(path string) string {
data := hexToNibbles(path)
sp := trie.NewSyncPath(data)
tnps := TrieNodePathSet([][]byte(sp))
hash := common.Hash{}
return sp, tnps, hash
return string(data)
}
var (
keys []string
hashes []common.Hash
paths []trie.SyncPath
pathsets []TrieNodePathSet
hashes []common.Hash
paths []string
)
for _, x := range []string{
"0x9",
Expand All @@ -68,16 +61,14 @@ func TestRequestSorting(t *testing.T) {
"0x01234567890123456789012345678901012345678901234567890123456789010",
"0x01234567890123456789012345678901012345678901234567890123456789011",
} {
sp, _, hash := f(x)
keys = append(keys, "")
hashes = append(hashes, hash)
paths = append(paths, sp)
paths = append(paths, f(x))
hashes = append(hashes, common.Hash{})
}
_, _, paths, pathsets = sortByAccountPath(keys, hashes, paths)
_, _, syncPaths, pathsets := sortByAccountPath(paths, hashes)
{
var b = new(bytes.Buffer)
for i := 0; i < len(paths); i++ {
fmt.Fprintf(b, "\n%d. paths %x", i, paths[i])
for i := 0; i < len(syncPaths); i++ {
fmt.Fprintf(b, "\n%d. paths %x", i, syncPaths[i])
}
want := `
0. paths [0099]
Expand Down
42 changes: 21 additions & 21 deletions eth/protocols/snap/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,8 @@ type trienodeHealRequest struct {
timeout *time.Timer // Timer to track delivery timeout
stale chan struct{} // Channel to signal the request was dropped

paths []string // Trie node paths for identifying trie node
hashes []common.Hash // Trie node hashes to validate responses
syncPaths []trie.SyncPath // Trie node sync paths requested for rescheduling
paths []string // Trie node paths for identifying trie node
hashes []common.Hash // Trie node hashes to validate responses

task *healTask // Task which this request is filling (only access fields through the runloop!!)
}
Expand Down Expand Up @@ -1323,35 +1322,32 @@ func (s *Syncer) assignTrienodeHealTasks(success chan *trienodeHealResponse, fai
cap = maxTrieRequestCount
}
var (
hashes = make([]common.Hash, 0, cap)
paths = make([]string, 0, cap)
syncPaths = make([]trie.SyncPath, 0, cap)
pathsets = make([]TrieNodePathSet, 0, cap)
hashes = make([]common.Hash, 0, cap)
paths = make([]string, 0, cap)
pathsets = make([]TrieNodePathSet, 0, cap)
)
for path, hash := range s.healer.trieTasks {
delete(s.healer.trieTasks, path)

paths = append(paths, path)
hashes = append(hashes, hash)
syncPaths = append(syncPaths, trie.NewSyncPath([]byte(path)))
if len(paths) >= cap {
break
}
}
// Group requests by account hash
paths, hashes, syncPaths, pathsets = sortByAccountPath(paths, hashes, syncPaths)
paths, hashes, _, pathsets = sortByAccountPath(paths, hashes)
req := &trienodeHealRequest{
peer: idle,
id: reqid,
time: time.Now(),
deliver: success,
revert: fail,
cancel: cancel,
stale: make(chan struct{}),
paths: paths,
hashes: hashes,
syncPaths: syncPaths,
task: s.healer,
peer: idle,
id: reqid,
time: time.Now(),
deliver: success,
revert: fail,
cancel: cancel,
stale: make(chan struct{}),
paths: paths,
hashes: hashes,
task: s.healer,
}
req.timeout = time.AfterFunc(s.rates.TargetTimeout(), func() {
peer.Log().Debug("Trienode heal request timed out", "reqid", reqid)
Expand Down Expand Up @@ -2981,7 +2977,11 @@ func (t *healRequestSort) Merge() []TrieNodePathSet {

// sortByAccountPath takes hashes and paths, and sorts them. After that, it generates
// the TrieNodePaths and merges paths which belongs to the same account path.
func sortByAccountPath(paths []string, hashes []common.Hash, syncPaths []trie.SyncPath) ([]string, []common.Hash, []trie.SyncPath, []TrieNodePathSet) {
func sortByAccountPath(paths []string, hashes []common.Hash) ([]string, []common.Hash, []trie.SyncPath, []TrieNodePathSet) {
var syncPaths []trie.SyncPath
for _, path := range paths {
syncPaths = append(syncPaths, trie.NewSyncPath([]byte(path)))
}
n := &healRequestSort{paths, hashes, syncPaths}
sort.Sort(n)
pathsets := n.Merge()
Expand Down

0 comments on commit 7b92f2a

Please sign in to comment.