github.com/ethereum/go-ethereum@v1.16.1/beacon/types/beacon_block_test.go (about)

     1  // Copyright 2024 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 types
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  )
    26  
    27  func TestBlockFromJSON(t *testing.T) {
    28  	type blocktest struct {
    29  		file            string
    30  		version         string
    31  		wantSlot        uint64
    32  		wantBlockNumber uint64
    33  		wantBlockHash   common.Hash
    34  	}
    35  	tests := []blocktest{
    36  		{
    37  			file:            "block_electra_withdrawals.json",
    38  			version:         "electra",
    39  			wantSlot:        151850,
    40  			wantBlockNumber: 141654,
    41  			wantBlockHash:   common.HexToHash("0xf6730485a38be5ada3e110990a2c7adaabd2e8d4a49782134f1a8bfbc246a5d7"),
    42  		},
    43  		{
    44  			file:            "block_electra_deposits.json",
    45  			version:         "electra",
    46  			wantSlot:        151016,
    47  			wantBlockNumber: 140858,
    48  			wantBlockHash:   common.HexToHash("0x1f2637170986346c7993d5adbadbebbf4c9ed89c6a4d2dff653db99c8c168076"),
    49  		},
    50  		{
    51  			file:            "block_electra_consolidations.json",
    52  			version:         "electra",
    53  			wantSlot:        151717,
    54  			wantBlockNumber: 141529,
    55  			wantBlockHash:   common.HexToHash("0xc8807f7a1f96b0a073ff27065776dd21eff6b7e64079c60bffd33f690efbb330"),
    56  		},
    57  		{
    58  			file:            "block_deneb.json",
    59  			version:         "deneb",
    60  			wantSlot:        8631513,
    61  			wantBlockNumber: 19431837,
    62  			wantBlockHash:   common.HexToHash("0x4cf7d9108fc01b50023ab7cab9b372a96068fddcadec551630393b65acb1f34c"),
    63  		},
    64  		{
    65  			file:            "block_capella.json",
    66  			version:         "capella",
    67  			wantSlot:        7378495,
    68  			wantBlockNumber: 18189758,
    69  			wantBlockHash:   common.HexToHash("0x802acf5c350f4252e31d83c431fcb259470250fa0edf49e8391cfee014239820"),
    70  		},
    71  	}
    72  
    73  	for _, test := range tests {
    74  		t.Run(test.file, func(t *testing.T) {
    75  			data, err := os.ReadFile(filepath.Join("testdata", test.file))
    76  			if err != nil {
    77  				t.Fatal(err)
    78  			}
    79  			beaconBlock, err := BlockFromJSON(test.version, data)
    80  			if err != nil {
    81  				t.Fatal(err)
    82  			}
    83  			if beaconBlock.Slot() != test.wantSlot {
    84  				t.Errorf("wrong slot number %d", beaconBlock.Slot())
    85  			}
    86  			execBlock, err := beaconBlock.ExecutionPayload()
    87  			if err != nil {
    88  				t.Fatalf("payload extraction failed: %v", err)
    89  			}
    90  			if execBlock.NumberU64() != test.wantBlockNumber {
    91  				t.Errorf("wrong block number: %v", execBlock.NumberU64())
    92  			}
    93  			if execBlock.Hash() != test.wantBlockHash {
    94  				t.Errorf("wrong block hash: %v", execBlock.Hash())
    95  			}
    96  		})
    97  	}
    98  }