github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/consensus/ipbft/types/genesis.go (about)

     1  package types
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"github.com/intfoundation/intchain/common/hexutil"
     8  	"math/big"
     9  	"time"
    10  
    11  	. "github.com/intfoundation/go-common"
    12  	"github.com/intfoundation/go-crypto"
    13  	"github.com/intfoundation/intchain/common"
    14  )
    15  
    16  //------------------------------------------------------------
    17  // we store the gendoc in the db
    18  
    19  var GenDocKey = []byte("GenDocKey")
    20  
    21  //------------------------------------------------------------
    22  // core types for a genesis definition
    23  
    24  var CONSENSUS_IPBFT string = "ipbft"
    25  
    26  type GenesisValidator struct {
    27  	EthAccount     common.Address `json:"address"`
    28  	PubKey         crypto.PubKey  `json:"pub_key"`
    29  	Amount         *big.Int       `json:"amount"`
    30  	Name           string         `json:"name"`
    31  	RemainingEpoch uint64         `json:"epoch"`
    32  }
    33  
    34  type GenesisCandidate struct {
    35  	EthAccount common.Address `json:"address"`
    36  	PubKey     crypto.PubKey  `json:"pub_key"`
    37  }
    38  
    39  type OneEpochDoc struct {
    40  	Number         uint64             `json:"number"`
    41  	RewardPerBlock *big.Int           `json:"reward_per_block"`
    42  	StartBlock     uint64             `json:"start_block"`
    43  	EndBlock       uint64             `json:"end_block"`
    44  	Status         int                `json:"status"`
    45  	Validators     []GenesisValidator `json:"validators"`
    46  	//Candidates     []GenesisCandidate `json:"candidates"`
    47  }
    48  
    49  type RewardSchemeDoc struct {
    50  	TotalReward        *big.Int `json:"total_reward"`
    51  	RewardFirstYear    *big.Int `json:"reward_first_year"`
    52  	EpochNumberPerYear uint64   `json:"epoch_no_per_year"`
    53  	TotalYear          uint64   `json:"total_year"`
    54  }
    55  
    56  type GenesisDoc struct {
    57  	ChainID      string          `json:"chain_id"`
    58  	Consensus    string          `json:"consensus"`
    59  	GenesisTime  time.Time       `json:"genesis_time"`
    60  	RewardScheme RewardSchemeDoc `json:"reward_scheme"`
    61  	CurrentEpoch OneEpochDoc     `json:"current_epoch"`
    62  }
    63  
    64  // Utility method for saving GenensisDoc as JSON file.
    65  func (genDoc *GenesisDoc) SaveAs(file string) error {
    66  	genDocBytes, err := json.MarshalIndent(genDoc, "", "\t")
    67  	if err != nil {
    68  		fmt.Println(err)
    69  	}
    70  
    71  	return WriteFile(file, genDocBytes, 0644)
    72  }
    73  
    74  //------------------------------------------------------------
    75  // Make genesis state from file
    76  
    77  func GenesisDocFromJSON(jsonBlob []byte) (genDoc *GenesisDoc, err error) {
    78  	err = json.Unmarshal(jsonBlob, &genDoc)
    79  	return
    80  }
    81  
    82  var MainnetGenesisJSON string = `{
    83  	"chain_id": "intchain",
    84  	"consensus": "ipbft",
    85  	"genesis_time": "2021-08-18T18:18:04.710873+08:00",
    86  	"reward_scheme": {
    87  		"total_reward": "0x52b7d2dcc80cd2e4000000",
    88  		"reward_first_year": "0x108b2a2c28029094000000",
    89  		"epoch_no_per_year": "0x111c",
    90  		"total_year": "0x5"
    91  	},
    92  	"current_epoch": {
    93  		"number": "0x0",
    94  		"reward_per_block": "0x1a675944a9a10838",
    95  		"start_block": "0x0",
    96  		"end_block": "0x960",
    97  		"validators": [
    98  			{
    99  				"address": "0xe00026f798c36ed24cc946474f92494de800c6c9",
   100  				"pub_key": "0x71F9A459488833990A81D131C32C92EB7ED8B00C1EFB1B9FB7EE78FF133535BC089B8F1BB61512E27E6D85B8C4469903730CB18A963353F6262FE2B5DD0222E80B28CC05B70EEAF8F59289EEC0964D4C9570AAB44991C69CF5DEBBBD4FFA8AC871F1097DC19855E69DD43463F5BC2EAD65F9690F9A9D28109D64659432FDC301",
   101  				"amount": "0xd3c21bcecceda1000000",
   102  				"name": "",
   103  				"epoch": "0x0"
   104  			}
   105  		]
   106  	}
   107  }`
   108  
   109  var TestnetGenesisJSON string = `{
   110  	"chain_id": "testnet",
   111  	"consensus": "ipbft",
   112  	"genesis_time": "2021-08-16T10:34:51.240259+08:00",
   113  	"reward_scheme": {
   114  		"total_reward": "0x52b7d2dcc80cd2e4000000",
   115  		"reward_first_year": "0x108b2a2c28029094000000",
   116  		"epoch_no_per_year": "0x111c",
   117  		"total_year": "0x5"
   118  	},
   119  	"current_epoch": {
   120  		"number": "0x0",
   121  		"reward_per_block": "0x1a675944a9a10838",
   122  		"start_block": "0x0",
   123  		"end_block": "0x960",
   124  		"validators": [
   125  			{
   126  				"address": "0x0d3a15f869e4c50d59154ad1bc1b11702673b394",
   127  				"pub_key": "0x6741E21BCCE8CE483F58FF61EF6AE33EAFE66B90A32EA4EA6610789722C06898472F201E291B0DD58C085915EDD398FF4E1551C57AB79DAF1D3D37D7C6839FF955E7BD6AA9F25795107771C074B28360293A14B60A74311B97E03A0095EE9DA3168D3D91991859C88C12713B46006DE93DE26AF4136EFCF0391B1F1C4025B0A3",
   128  				"amount": "0xd3c21bcecceda1000000",
   129  				"name": "",
   130  				"epoch": "0x0"
   131  			}
   132  		]
   133  	}
   134  }
   135  `
   136  
   137  func (ep OneEpochDoc) MarshalJSON() ([]byte, error) {
   138  	type hexEpoch struct {
   139  		Number         hexutil.Uint64     `json:"number"`
   140  		RewardPerBlock *hexutil.Big       `json:"reward_per_block"`
   141  		StartBlock     hexutil.Uint64     `json:"start_block"`
   142  		EndBlock       hexutil.Uint64     `json:"end_block"`
   143  		Validators     []GenesisValidator `json:"validators"`
   144  	}
   145  	var enc hexEpoch
   146  	enc.Number = hexutil.Uint64(ep.Number)
   147  	enc.RewardPerBlock = (*hexutil.Big)(ep.RewardPerBlock)
   148  	enc.StartBlock = hexutil.Uint64(ep.StartBlock)
   149  	enc.EndBlock = hexutil.Uint64(ep.EndBlock)
   150  	if ep.Validators != nil {
   151  		enc.Validators = ep.Validators
   152  	}
   153  	return json.Marshal(&enc)
   154  }
   155  
   156  func (ep *OneEpochDoc) UnmarshalJSON(input []byte) error {
   157  	type hexEpoch struct {
   158  		Number         hexutil.Uint64     `json:"number"`
   159  		RewardPerBlock *hexutil.Big       `json:"reward_per_block"`
   160  		StartBlock     hexutil.Uint64     `json:"start_block"`
   161  		EndBlock       hexutil.Uint64     `json:"end_block"`
   162  		Validators     []GenesisValidator `json:"validators"`
   163  	}
   164  	var dec hexEpoch
   165  	if err := json.Unmarshal(input, &dec); err != nil {
   166  		return err
   167  	}
   168  	ep.Number = uint64(dec.Number)
   169  	ep.RewardPerBlock = (*big.Int)(dec.RewardPerBlock)
   170  	ep.StartBlock = uint64(dec.StartBlock)
   171  	ep.EndBlock = uint64(dec.EndBlock)
   172  	if dec.Validators == nil {
   173  		return errors.New("missing required field 'validators' for Genesis/epoch")
   174  	}
   175  	ep.Validators = dec.Validators
   176  	return nil
   177  }
   178  
   179  func (gv GenesisValidator) MarshalJSON() ([]byte, error) {
   180  	type hexValidator struct {
   181  		Address        common.Address `json:"address"`
   182  		PubKey         string         `json:"pub_key"`
   183  		Amount         *hexutil.Big   `json:"amount"`
   184  		Name           string         `json:"name"`
   185  		RemainingEpoch hexutil.Uint64 `json:"epoch"`
   186  	}
   187  	var enc hexValidator
   188  	enc.Address = gv.EthAccount
   189  	enc.PubKey = gv.PubKey.KeyString()
   190  	enc.Amount = (*hexutil.Big)(gv.Amount)
   191  	enc.Name = gv.Name
   192  	enc.RemainingEpoch = hexutil.Uint64(gv.RemainingEpoch)
   193  
   194  	return json.Marshal(&enc)
   195  }
   196  
   197  func (gv *GenesisValidator) UnmarshalJSON(input []byte) error {
   198  	type hexValidator struct {
   199  		Address        common.Address `json:"address"`
   200  		PubKey         string         `json:"pub_key"`
   201  		Amount         *hexutil.Big   `json:"amount"`
   202  		Name           string         `json:"name"`
   203  		RemainingEpoch hexutil.Uint64 `json:"epoch"`
   204  	}
   205  	var dec hexValidator
   206  	if err := json.Unmarshal(input, &dec); err != nil {
   207  		return err
   208  	}
   209  	gv.EthAccount = dec.Address
   210  
   211  	pubkeyBytes := common.FromHex(dec.PubKey)
   212  	if dec.PubKey == "" || len(pubkeyBytes) != 128 {
   213  		return errors.New("wrong format of required field 'pub_key' for Genesis/epoch/validators")
   214  	}
   215  	var blsPK crypto.BLSPubKey
   216  	copy(blsPK[:], pubkeyBytes)
   217  	gv.PubKey = blsPK
   218  
   219  	if dec.Amount == nil {
   220  		return errors.New("missing required field 'amount' for Genesis/epoch/validators")
   221  	}
   222  	gv.Amount = (*big.Int)(dec.Amount)
   223  	gv.Name = dec.Name
   224  	gv.RemainingEpoch = uint64(dec.RemainingEpoch)
   225  	return nil
   226  }
   227  
   228  func (rs RewardSchemeDoc) MarshalJSON() ([]byte, error) {
   229  	type hexRewardScheme struct {
   230  		TotalReward        *hexutil.Big   `json:"total_reward"`
   231  		RewardFirstYear    *hexutil.Big   `json:"reward_first_year"`
   232  		EpochNumberPerYear hexutil.Uint64 `json:"epoch_no_per_year"`
   233  		TotalYear          hexutil.Uint64 `json:"total_year"`
   234  	}
   235  	var enc hexRewardScheme
   236  	enc.TotalReward = (*hexutil.Big)(rs.TotalReward)
   237  	enc.RewardFirstYear = (*hexutil.Big)(rs.RewardFirstYear)
   238  	enc.EpochNumberPerYear = hexutil.Uint64(rs.EpochNumberPerYear)
   239  	enc.TotalYear = hexutil.Uint64(rs.TotalYear)
   240  
   241  	return json.Marshal(&enc)
   242  }
   243  
   244  func (rs *RewardSchemeDoc) UnmarshalJSON(input []byte) error {
   245  	type hexRewardScheme struct {
   246  		TotalReward        *hexutil.Big   `json:"total_reward"`
   247  		RewardFirstYear    *hexutil.Big   `json:"reward_first_year"`
   248  		EpochNumberPerYear hexutil.Uint64 `json:"epoch_no_per_year"`
   249  		TotalYear          hexutil.Uint64 `json:"total_year"`
   250  	}
   251  	var dec hexRewardScheme
   252  	if err := json.Unmarshal(input, &dec); err != nil {
   253  		return err
   254  	}
   255  	if dec.TotalReward == nil {
   256  		return errors.New("missing required field 'total_reward' for Genesis/reward_scheme")
   257  	}
   258  	rs.TotalReward = (*big.Int)(dec.TotalReward)
   259  	if dec.RewardFirstYear == nil {
   260  		return errors.New("missing required field 'reward_first_year' for Genesis/reward_scheme")
   261  	}
   262  	rs.RewardFirstYear = (*big.Int)(dec.RewardFirstYear)
   263  
   264  	rs.EpochNumberPerYear = uint64(dec.EpochNumberPerYear)
   265  	rs.TotalYear = uint64(dec.TotalYear)
   266  
   267  	return nil
   268  }