gitee.com/liu-zhao234568/cntest@v1.0.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  	"bytes"
    21  	"encoding/json"
    22  	"io/ioutil"
    23  	"reflect"
    24  	"strings"
    25  	"testing"
    26  
    27  	"gitee.com/liu-zhao234568/cntest/core"
    28  	"github.com/davecgh/go-spew/spew"
    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  				t.Logf("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  	enc, err := json.MarshalIndent(spec, "", "  ")
    85  	if err != nil {
    86  		t.Fatalf("failed encoding chainspec: %v", err)
    87  	}
    88  	expBlob, err := ioutil.ReadFile("testdata/stureby_parity.json")
    89  	if err != nil {
    90  		t.Fatalf("could not read file: %v", err)
    91  	}
    92  	if !bytes.Equal(expBlob, enc) {
    93  		t.Fatalf("chainspec mismatch")
    94  	}
    95  }