github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/trie/verkle_test.go (about)

     1  // Copyright 2023 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 trie
    18  
    19  import (
    20  	"bytes"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/core/rawdb"
    26  	"github.com/ethereum/go-ethereum/core/types"
    27  	"github.com/ethereum/go-ethereum/trie/utils"
    28  	"github.com/holiman/uint256"
    29  )
    30  
    31  var (
    32  	accounts = map[common.Address]*types.StateAccount{
    33  		{1}: {
    34  			Nonce:    100,
    35  			Balance:  uint256.NewInt(100),
    36  			CodeHash: common.Hash{0x1}.Bytes(),
    37  		},
    38  		{2}: {
    39  			Nonce:    200,
    40  			Balance:  uint256.NewInt(200),
    41  			CodeHash: common.Hash{0x2}.Bytes(),
    42  		},
    43  	}
    44  	storages = map[common.Address]map[common.Hash][]byte{
    45  		{1}: {
    46  			common.Hash{10}: []byte{10},
    47  			common.Hash{11}: []byte{11},
    48  			common.MaxHash:  []byte{0xff},
    49  		},
    50  		{2}: {
    51  			common.Hash{20}: []byte{20},
    52  			common.Hash{21}: []byte{21},
    53  			common.MaxHash:  []byte{0xff},
    54  		},
    55  	}
    56  )
    57  
    58  func TestVerkleTreeReadWrite(t *testing.T) {
    59  	db := newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.PathScheme)
    60  	tr, _ := NewVerkleTrie(types.EmptyVerkleHash, db, utils.NewPointCache(100))
    61  
    62  	for addr, acct := range accounts {
    63  		if err := tr.UpdateAccount(addr, acct); err != nil {
    64  			t.Fatalf("Failed to update account, %v", err)
    65  		}
    66  		for key, val := range storages[addr] {
    67  			if err := tr.UpdateStorage(addr, key.Bytes(), val); err != nil {
    68  				t.Fatalf("Failed to update account, %v", err)
    69  			}
    70  		}
    71  	}
    72  
    73  	for addr, acct := range accounts {
    74  		stored, err := tr.GetAccount(addr)
    75  		if err != nil {
    76  			t.Fatalf("Failed to get account, %v", err)
    77  		}
    78  		if !reflect.DeepEqual(stored, acct) {
    79  			t.Fatal("account is not matched")
    80  		}
    81  		for key, val := range storages[addr] {
    82  			stored, err := tr.GetStorage(addr, key.Bytes())
    83  			if err != nil {
    84  				t.Fatalf("Failed to get storage, %v", err)
    85  			}
    86  			if !bytes.Equal(stored, val) {
    87  				t.Fatal("storage is not matched")
    88  			}
    89  		}
    90  	}
    91  }