github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/consensus/raft/util.go (about)

     1  package raft
     2  
     3  import (
     4  	"encoding/binary"
     5  	"encoding/json"
     6  	"fmt"
     7  	"github.com/bigzoro/my_simplechain/crypto"
     8  	"github.com/bigzoro/my_simplechain/log"
     9  	uuid "github.com/satori/go.uuid"
    10  	"io"
    11  	"io/ioutil"
    12  	"os"
    13  	"path/filepath"
    14  	"runtime"
    15  )
    16  
    17  type raftConfig struct {
    18  	RaftId uint16 `json:"raftId"`
    19  }
    20  
    21  const raftFile = "raftConfig.json"
    22  
    23  // TODO: this is just copied over from cmd/utils/cmd.go. dedupe
    24  // Fatalf formats a message to standard error and exits the program.
    25  // The message is also printed to standard output if standard error
    26  // is redirected to a different file.
    27  func Fatalf(format string, args ...interface{}) {
    28  	w := io.MultiWriter(os.Stdout, os.Stderr)
    29  	if runtime.GOOS == "windows" {
    30  		// The SameFile check below doesn't work on Windows.
    31  		// stdout is unlikely to get redirected though, so just print there.
    32  		w = os.Stdout
    33  	} else {
    34  		outf, _ := os.Stdout.Stat()
    35  		errf, _ := os.Stderr.Stat()
    36  		if outf != nil && errf != nil && os.SameFile(outf, errf) {
    37  			w = os.Stderr
    38  		}
    39  	}
    40  	fmt.Fprintf(w, "Fatal: "+format+"\n", args...)
    41  	os.Exit(1)
    42  }
    43  
    44  func GetRaftIdFromEnodeId(enode []byte) uint16 {
    45  	enodeHash := crypto.Keccak256(enode)
    46  	return binary.BigEndian.Uint16(enodeHash[30:])
    47  }
    48  
    49  func GetRandomRaftIdFromEnodeId(enode []byte) (uint16, error) {
    50  	packageUUID := uuid.NewV4()
    51  	enode = append(enode, []byte(packageUUID.String())...)
    52  	return GetRaftIdFromEnodeId(enode), nil
    53  }
    54  
    55  func ReadGenRaftConfigJson(enode []byte, path string) (uint16, error) {
    56  	var rc raftConfig
    57  	fileBytes, err := ioutil.ReadFile(filepath.Join(path, raftFile))
    58  	if err != nil {
    59  		raftId, err := GetRandomRaftIdFromEnodeId(enode)
    60  		if err != nil {
    61  			log.Error(err.Error())
    62  			return 0, err
    63  		}
    64  		rc.RaftId = raftId
    65  	} else {
    66  		err = json.Unmarshal(fileBytes, &rc)
    67  		if err != nil {
    68  			log.Error(err.Error())
    69  			return 0, err
    70  		}
    71  	}
    72  	data, err := json.Marshal(rc)
    73  	if err != nil {
    74  		log.Error("mashral raft config failed")
    75  		return 0, err
    76  	}
    77  	return rc.RaftId, ioutil.WriteFile(filepath.Join(path, raftFile), data, 0700)
    78  }
    79  
    80  func GetRaftConfigJson(path string) (uint16, error) {
    81  	fileBytes, err := ioutil.ReadFile(filepath.Join(path, raftFile))
    82  	if err != nil {
    83  		return 0, err
    84  	}
    85  	var rc = raftConfig{}
    86  	err = json.Unmarshal(fileBytes, &rc)
    87  	if err != nil {
    88  		return 0, err
    89  	}
    90  	return rc.RaftId, nil
    91  }
    92  
    93  func GenUpdateRaftConfigJson(enode []byte, path string) (uint16, error) {
    94  	var rc = raftConfig{}
    95  	raftId, err := GetRandomRaftIdFromEnodeId(enode)
    96  	if err != nil {
    97  		log.Error(err.Error())
    98  		return 0, err
    99  	}
   100  	rc.RaftId = raftId
   101  	data, err := json.Marshal(rc)
   102  	if err != nil {
   103  		log.Error("mashral raft config failed")
   104  		return 0, err
   105  	}
   106  	return raftId, ioutil.WriteFile(filepath.Join(path, raftFile), data, 0700)
   107  }