github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/cmd/puppeth/genesis_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU 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  // go-ethereum 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 General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"io/ioutil"
    23  	"reflect"
    24  	"strings"
    25  	"testing"
    26  
    27  	"github.com/davecgh/go-spew/spew"
    28  
    29  	"github.com/scroll-tech/go-ethereum/core"
    30  )
    31  
    32  // Tests the go-ethereum to Aleth chainspec conversion for the Stureby testnet.
    33  func TestAlethSturebyConverter(t *testing.T) {
    34  	blob, err := ioutil.ReadFile("testdata/stureby_geth.json")
    35  	if err != nil {
    36  		t.Fatalf("could not read file: %v", err)
    37  	}
    38  	var genesis core.Genesis
    39  	if err := json.Unmarshal(blob, &genesis); err != nil {
    40  		t.Fatalf("failed parsing genesis: %v", err)
    41  	}
    42  	spec, err := newAlethGenesisSpec("stureby", &genesis)
    43  	if err != nil {
    44  		t.Fatalf("failed creating chainspec: %v", err)
    45  	}
    46  
    47  	expBlob, err := ioutil.ReadFile("testdata/stureby_aleth.json")
    48  	if err != nil {
    49  		t.Fatalf("could not read file: %v", err)
    50  	}
    51  	expspec := &alethGenesisSpec{}
    52  	if err := json.Unmarshal(expBlob, expspec); err != nil {
    53  		t.Fatalf("failed parsing genesis: %v", err)
    54  	}
    55  	if !reflect.DeepEqual(expspec, spec) {
    56  		t.Errorf("chainspec mismatch")
    57  		c := spew.ConfigState{
    58  			DisablePointerAddresses: true,
    59  			SortKeys:                true,
    60  		}
    61  		exp := strings.Split(c.Sdump(expspec), "\n")
    62  		got := strings.Split(c.Sdump(spec), "\n")
    63  		for i := 0; i < len(exp) && i < len(got); i++ {
    64  			if exp[i] != got[i] {
    65  				t.Logf("got: %v\nexp: %v\n", exp[i], got[i])
    66  			}
    67  		}
    68  	}
    69  }
    70  
    71  // Tests the go-ethereum to Parity chainspec conversion for the Stureby testnet.
    72  func TestParitySturebyConverter(t *testing.T) {
    73  	blob, err := ioutil.ReadFile("testdata/stureby_geth.json")
    74  	if err != nil {
    75  		t.Fatalf("could not read file: %v", err)
    76  	}
    77  	var genesis core.Genesis
    78  	if err := json.Unmarshal(blob, &genesis); err != nil {
    79  		t.Fatalf("failed parsing genesis: %v", err)
    80  	}
    81  	spec, err := newParityChainSpec("stureby", &genesis, []string{})
    82  	if err != nil {
    83  		t.Fatalf("failed creating chainspec: %v", err)
    84  	}
    85  	enc, err := json.MarshalIndent(spec, "", "  ")
    86  	if err != nil {
    87  		t.Fatalf("failed encoding chainspec: %v", err)
    88  	}
    89  	expBlob, err := ioutil.ReadFile("testdata/stureby_parity.json")
    90  	if err != nil {
    91  		t.Fatalf("could not read file: %v", err)
    92  	}
    93  	if !bytes.Equal(expBlob, enc) {
    94  		t.Fatalf("chainspec mismatch")
    95  	}
    96  }