github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/consensus/neatcon/types/genesis.go (about)

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