github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/eth/protocols/snap/sort_test.go (about)

     1  // Copyright 2022 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package snap
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/electroneum/electroneum-sc/common"
    25  	"github.com/electroneum/electroneum-sc/trie"
    26  )
    27  
    28  func hexToNibbles(s string) []byte {
    29  	if len(s) >= 2 && s[0] == '0' && s[1] == 'x' {
    30  		s = s[2:]
    31  	}
    32  	var s2 []byte
    33  	for _, ch := range []byte(s) {
    34  		s2 = append(s2, '0')
    35  		s2 = append(s2, ch)
    36  	}
    37  	return common.Hex2Bytes(string(s2))
    38  }
    39  
    40  func TestRequestSorting(t *testing.T) {
    41  	//   - Path 0x9  -> {0x19}
    42  	//   - Path 0x99 -> {0x0099}
    43  	//   - Path 0x01234567890123456789012345678901012345678901234567890123456789019  -> {0x0123456789012345678901234567890101234567890123456789012345678901, 0x19}
    44  	//   - Path 0x012345678901234567890123456789010123456789012345678901234567890199 -> {0x0123456789012345678901234567890101234567890123456789012345678901, 0x0099}
    45  	var f = func(path string) (trie.SyncPath, TrieNodePathSet, common.Hash) {
    46  		data := hexToNibbles(path)
    47  		sp := trie.NewSyncPath(data)
    48  		tnps := TrieNodePathSet([][]byte(sp))
    49  		hash := common.Hash{}
    50  		return sp, tnps, hash
    51  	}
    52  	var (
    53  		hashes   []common.Hash
    54  		paths    []trie.SyncPath
    55  		pathsets []TrieNodePathSet
    56  	)
    57  	for _, x := range []string{
    58  		"0x9",
    59  		"0x012345678901234567890123456789010123456789012345678901234567890195",
    60  		"0x012345678901234567890123456789010123456789012345678901234567890197",
    61  		"0x012345678901234567890123456789010123456789012345678901234567890196",
    62  		"0x99",
    63  		"0x012345678901234567890123456789010123456789012345678901234567890199",
    64  		"0x01234567890123456789012345678901012345678901234567890123456789019",
    65  		"0x0123456789012345678901234567890101234567890123456789012345678901",
    66  		"0x01234567890123456789012345678901012345678901234567890123456789010",
    67  		"0x01234567890123456789012345678901012345678901234567890123456789011",
    68  	} {
    69  		sp, _, hash := f(x)
    70  		hashes = append(hashes, hash)
    71  		paths = append(paths, sp)
    72  	}
    73  	_, paths, pathsets = sortByAccountPath(hashes, paths)
    74  	{
    75  		var b = new(bytes.Buffer)
    76  		for i := 0; i < len(paths); i++ {
    77  			fmt.Fprintf(b, "\n%d. paths %x", i, paths[i])
    78  		}
    79  		want := `
    80  0. paths [0099]
    81  1. paths [0123456789012345678901234567890101234567890123456789012345678901 00]
    82  2. paths [0123456789012345678901234567890101234567890123456789012345678901 0095]
    83  3. paths [0123456789012345678901234567890101234567890123456789012345678901 0096]
    84  4. paths [0123456789012345678901234567890101234567890123456789012345678901 0097]
    85  5. paths [0123456789012345678901234567890101234567890123456789012345678901 0099]
    86  6. paths [0123456789012345678901234567890101234567890123456789012345678901 10]
    87  7. paths [0123456789012345678901234567890101234567890123456789012345678901 11]
    88  8. paths [0123456789012345678901234567890101234567890123456789012345678901 19]
    89  9. paths [19]`
    90  		if have := b.String(); have != want {
    91  			t.Errorf("have:%v\nwant:%v\n", have, want)
    92  		}
    93  	}
    94  	{
    95  		var b = new(bytes.Buffer)
    96  		for i := 0; i < len(pathsets); i++ {
    97  			fmt.Fprintf(b, "\n%d. pathset %x", i, pathsets[i])
    98  		}
    99  		want := `
   100  0. pathset [0099]
   101  1. pathset [0123456789012345678901234567890101234567890123456789012345678901 00 0095 0096 0097 0099 10 11 19]
   102  2. pathset [19]`
   103  		if have := b.String(); have != want {
   104  			t.Errorf("have:%v\nwant:%v\n", have, want)
   105  		}
   106  	}
   107  }