github.com/evdatsion/aphelion-dpos-bft@v0.32.1/privval/file_deprecated.go (about)

     1  package privval
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	"github.com/evdatsion/aphelion-dpos-bft/crypto"
     8  	cmn "github.com/evdatsion/aphelion-dpos-bft/libs/common"
     9  	"github.com/evdatsion/aphelion-dpos-bft/types"
    10  )
    11  
    12  // OldFilePV is the old version of the FilePV, pre v0.28.0.
    13  // Deprecated: Use FilePV instead.
    14  type OldFilePV struct {
    15  	Address       types.Address  `json:"address"`
    16  	PubKey        crypto.PubKey  `json:"pub_key"`
    17  	LastHeight    int64          `json:"last_height"`
    18  	LastRound     int            `json:"last_round"`
    19  	LastStep      int8           `json:"last_step"`
    20  	LastSignature []byte         `json:"last_signature,omitempty"`
    21  	LastSignBytes cmn.HexBytes   `json:"last_signbytes,omitempty"`
    22  	PrivKey       crypto.PrivKey `json:"priv_key"`
    23  
    24  	filePath string
    25  }
    26  
    27  // LoadOldFilePV loads an OldFilePV from the filePath.
    28  func LoadOldFilePV(filePath string) (*OldFilePV, error) {
    29  	pvJSONBytes, err := ioutil.ReadFile(filePath)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	pv := &OldFilePV{}
    34  	err = cdc.UnmarshalJSON(pvJSONBytes, &pv)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	// overwrite pubkey and address for convenience
    40  	pv.PubKey = pv.PrivKey.PubKey()
    41  	pv.Address = pv.PubKey.Address()
    42  
    43  	pv.filePath = filePath
    44  	return pv, nil
    45  }
    46  
    47  // Upgrade convets the OldFilePV to the new FilePV, separating the immutable and mutable components,
    48  // and persisting them to the keyFilePath and stateFilePath, respectively.
    49  // It renames the original file by adding ".bak".
    50  func (oldFilePV *OldFilePV) Upgrade(keyFilePath, stateFilePath string) *FilePV {
    51  	privKey := oldFilePV.PrivKey
    52  	pvKey := FilePVKey{
    53  		PrivKey:  privKey,
    54  		PubKey:   privKey.PubKey(),
    55  		Address:  privKey.PubKey().Address(),
    56  		filePath: keyFilePath,
    57  	}
    58  
    59  	pvState := FilePVLastSignState{
    60  		Height:    oldFilePV.LastHeight,
    61  		Round:     oldFilePV.LastRound,
    62  		Step:      oldFilePV.LastStep,
    63  		Signature: oldFilePV.LastSignature,
    64  		SignBytes: oldFilePV.LastSignBytes,
    65  		filePath:  stateFilePath,
    66  	}
    67  
    68  	// Save the new PV files
    69  	pv := &FilePV{
    70  		Key:           pvKey,
    71  		LastSignState: pvState,
    72  	}
    73  	pv.Save()
    74  
    75  	// Rename the old PV file
    76  	err := os.Rename(oldFilePV.filePath, oldFilePV.filePath+".bak")
    77  	if err != nil {
    78  		panic(err)
    79  	}
    80  	return pv
    81  }