github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/mainchain/genesis/genesis.go (about)

     1  package genesis
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"encoding/json"
     7  
     8  	"encoding/hex"
     9  
    10  	"github.com/sixexorg/magnetic-ring/common"
    11  	"github.com/sixexorg/magnetic-ring/config"
    12  	"github.com/sixexorg/magnetic-ring/core/mainchain/types"
    13  	"github.com/sixexorg/magnetic-ring/store/mainchain/states"
    14  	"github.com/sixexorg/magnetic-ring/store/mainchain/storages"
    15  )
    16  
    17  type Genesis struct {
    18  	Config *config.GenesisConfig
    19  }
    20  
    21  var genesis *Genesis
    22  
    23  func InitGenesis(conf *config.GenesisConfig) (*Genesis, error) {
    24  	genesis = &Genesis{
    25  		Config: conf,
    26  	}
    27  	return genesis, nil
    28  }
    29  func GetGenesisBlockState() (states.AccountStates, types.Transactions) {
    30  	return genesis.genFirst()
    31  }
    32  
    33  type VbftBlockInfo struct {
    34  	VrfValue []byte `json:"vrf_value"`
    35  	VrfProof []byte `json:"vrf_proof"`
    36  }
    37  
    38  //privatekey must input from cmd
    39  func (g *Genesis) GenesisBlock(ledger *storages.LedgerStoreImp) error {
    40  	ass, txs := g.genFirst()
    41  	//todo hello world msg
    42  	block := &types.Block{
    43  		Header: &types.Header{
    44  			Height:        1,
    45  			Version:       0x01,
    46  			PrevBlockHash: common.Hash{},
    47  			LeagueRoot:    common.Hash{},
    48  			ReceiptsRoot:  common.Hash{},
    49  			StateRoot:     ass.GetHashRoot(),
    50  			TxRoot:        txs.GetHashRoot(),
    51  			Timestamp:     g.Config.Timestamp,
    52  		},
    53  		Transactions: txs,
    54  	}
    55  	blkInfo := &VbftBlockInfo{}
    56  	blkInfo.VrfValue = []byte("1c9810aa9822e511d5804a9c4db9dd08497c31087b0daafa34d768a3253441fa20515e2f30f81741102af0ca3cefc4818fef16adb825fbaa8cad78647f3afb590e")
    57  	blkInfo.VrfProof = []byte("c57741f934042cb8d8b087b44b161db56fc3ffd4ffb675d36cd09f83935be853d8729f3f5298d12d6fd28d45dde515a4b9d7f67682d182ba5118abf451ff1988")
    58  	block.Header.ConsensusPayload, _ = json.Marshal(blkInfo)
    59  	blockInfo := &storages.BlockInfo{
    60  		Block:         block,
    61  		AccountStates: ass,
    62  		GasDestroy:    big.NewInt(0),
    63  	}
    64  	err := ledger.SaveAll(blockInfo)
    65  	return err
    66  }
    67  
    68  func (g *Genesis) genFirst() (states.AccountStates, types.Transactions) {
    69  	official, _ := common.ToAddress(g.Config.Official)
    70  	ass := make(states.AccountStates, 0, 1+len(g.Config.Stars))
    71  	txs := make(types.Transactions, 0, len(g.Config.Stars))
    72  	as := &states.AccountState{
    73  		Address: official,
    74  		Height:  1,
    75  		Data: &states.Account{
    76  			Nonce:       1,
    77  			Balance:     big.NewInt(0).SetUint64(g.Config.crystal),
    78  			EnergyBalance: big.NewInt(0).SetUint64(g.Config.Energy),
    79  		},
    80  	}
    81  	ass = append(ass, as)
    82  	for _, v := range g.Config.Stars {
    83  
    84  		addr, err := common.ToAddress(v.Account)
    85  		if err != nil {
    86  			panic(err)
    87  		}
    88  		asNode := &states.AccountState{
    89  			Address: addr,
    90  			Height:  1,
    91  			Data: &states.Account{
    92  				Nonce:   1,
    93  				Balance: big.NewInt(0).SetUint64(v.crystal),
    94  			},
    95  		}
    96  		ass = append(ass, asNode)
    97  
    98  		pubuf, err := hex.DecodeString(v.Nodekey)
    99  		if err != nil {
   100  			continue
   101  		}
   102  		tx := &types.Transaction{
   103  
   104  			Version: types.TxVersion,
   105  			TxType:  types.AuthX,
   106  			TxData: &types.TxData{
   107  				Nonce:   1,
   108  				From:    addr,
   109  				NodePub: pubuf,
   110  				Fee:     big.NewInt(0),
   111  			},
   112  		}
   113  		txs = append(txs, tx)
   114  	}
   115  	return ass, txs
   116  }