github.com/XinFinOrg/xdcchain@v1.1.0/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  	"encoding/json"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"reflect"
    24  	"strings"
    25  	"testing"
    26  
    27  	"github.com/davecgh/go-spew/spew"
    28  	"github.com/ethereum/go-ethereum/core"
    29  )
    30  
    31  // Tests the go-ethereum to Aleth chainspec conversion for the Stureby testnet.
    32  func TestAlethSturebyConverter(t *testing.T) {
    33  	blob, err := ioutil.ReadFile("testdata/stureby_geth.json")
    34  	if err != nil {
    35  		t.Fatalf("could not read file: %v", err)
    36  	}
    37  	var genesis core.Genesis
    38  	if err := json.Unmarshal(blob, &genesis); err != nil {
    39  		t.Fatalf("failed parsing genesis: %v", err)
    40  	}
    41  	spec, err := newAlethGenesisSpec("stureby", &genesis)
    42  	if err != nil {
    43  		t.Fatalf("failed creating chainspec: %v", err)
    44  	}
    45  
    46  	expBlob, err := ioutil.ReadFile("testdata/stureby_aleth.json")
    47  	if err != nil {
    48  		t.Fatalf("could not read file: %v", err)
    49  	}
    50  	expspec := &alethGenesisSpec{}
    51  	if err := json.Unmarshal(expBlob, expspec); err != nil {
    52  		t.Fatalf("failed parsing genesis: %v", err)
    53  	}
    54  	if !reflect.DeepEqual(expspec, spec) {
    55  		t.Errorf("chainspec mismatch")
    56  		c := spew.ConfigState{
    57  			DisablePointerAddresses: true,
    58  			SortKeys:                true,
    59  		}
    60  		exp := strings.Split(c.Sdump(expspec), "\n")
    61  		got := strings.Split(c.Sdump(spec), "\n")
    62  		for i := 0; i < len(exp) && i < len(got); i++ {
    63  			if exp[i] != got[i] {
    64  				fmt.Printf("got: %v\nexp: %v\n", exp[i], got[i])
    65  			}
    66  		}
    67  	}
    68  }
    69  
    70  // Tests the go-ethereum to Parity chainspec conversion for the Stureby testnet.
    71  func TestParitySturebyConverter(t *testing.T) {
    72  	blob, err := ioutil.ReadFile("testdata/stureby_geth.json")
    73  	if err != nil {
    74  		t.Fatalf("could not read file: %v", err)
    75  	}
    76  	var genesis core.Genesis
    77  	if err := json.Unmarshal(blob, &genesis); err != nil {
    78  		t.Fatalf("failed parsing genesis: %v", err)
    79  	}
    80  	spec, err := newParityChainSpec("Stureby", &genesis, []string{})
    81  	if err != nil {
    82  		t.Fatalf("failed creating chainspec: %v", err)
    83  	}
    84  
    85  	expBlob, err := ioutil.ReadFile("testdata/stureby_parity.json")
    86  	if err != nil {
    87  		t.Fatalf("could not read file: %v", err)
    88  	}
    89  	expspec := &parityChainSpec{}
    90  	if err := json.Unmarshal(expBlob, expspec); err != nil {
    91  		t.Fatalf("failed parsing genesis: %v", err)
    92  	}
    93  	expspec.Nodes = []string{}
    94  
    95  	if !reflect.DeepEqual(expspec, spec) {
    96  		t.Errorf("chainspec mismatch")
    97  		c := spew.ConfigState{
    98  			DisablePointerAddresses: true,
    99  			SortKeys:                true,
   100  		}
   101  		exp := strings.Split(c.Sdump(expspec), "\n")
   102  		got := strings.Split(c.Sdump(spec), "\n")
   103  		for i := 0; i < len(exp) && i < len(got); i++ {
   104  			if exp[i] != got[i] {
   105  				fmt.Printf("got: %v\nexp: %v\n", exp[i], got[i])
   106  			}
   107  		}
   108  	}
   109  }