github.com/ethereum/go-ethereum@v1.14.3/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_deneb.json",
    38  			version:         "deneb",
    39  			wantSlot:        8631513,
    40  			wantBlockNumber: 19431837,
    41  			wantBlockHash:   common.HexToHash("0x4cf7d9108fc01b50023ab7cab9b372a96068fddcadec551630393b65acb1f34c"),
    42  		},
    43  		{
    44  			file:            "block_capella.json",
    45  			version:         "capella",
    46  			wantSlot:        7378495,
    47  			wantBlockNumber: 18189758,
    48  			wantBlockHash:   common.HexToHash("0x802acf5c350f4252e31d83c431fcb259470250fa0edf49e8391cfee014239820"),
    49  		},
    50  	}
    51  
    52  	for _, test := range tests {
    53  		t.Run(test.file, func(t *testing.T) {
    54  			data, err := os.ReadFile(filepath.Join("testdata", test.file))
    55  			if err != nil {
    56  				t.Fatal(err)
    57  			}
    58  			beaconBlock, err := BlockFromJSON(test.version, data)
    59  			if err != nil {
    60  				t.Fatal(err)
    61  			}
    62  			if beaconBlock.Slot() != test.wantSlot {
    63  				t.Errorf("wrong slot number %d", beaconBlock.Slot())
    64  			}
    65  			execBlock, err := beaconBlock.ExecutionPayload()
    66  			if err != nil {
    67  				t.Fatalf("payload extraction failed: %v", err)
    68  			}
    69  			if execBlock.NumberU64() != test.wantBlockNumber {
    70  				t.Errorf("wrong block number: %v", execBlock.NumberU64())
    71  			}
    72  			if execBlock.Hash() != test.wantBlockHash {
    73  				t.Errorf("wrong block hash: %v", execBlock.Hash())
    74  			}
    75  		})
    76  	}
    77  }