2020-03-29 21:32:01 +08:00
|
|
|
package genesis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/meshplus/bitxhub-kit/bytesutil"
|
|
|
|
"github.com/meshplus/bitxhub-kit/types"
|
2020-05-07 14:11:02 +08:00
|
|
|
"github.com/meshplus/bitxhub-model/pb"
|
2020-03-29 21:32:01 +08:00
|
|
|
"github.com/meshplus/bitxhub/internal/ledger"
|
|
|
|
"github.com/meshplus/bitxhub/internal/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
roleAddr = types.Bytes2Address(bytesutil.LeftPadBytes([]byte{13}, 20))
|
|
|
|
)
|
|
|
|
|
|
|
|
// Initialize initialize block
|
2020-05-07 14:11:02 +08:00
|
|
|
func Initialize(config *repo.Config, lg ledger.Ledger) error {
|
2020-03-29 21:32:01 +08:00
|
|
|
for _, addr := range config.Addresses {
|
2020-05-07 14:11:02 +08:00
|
|
|
lg.SetBalance(types.String2Address(addr), 100000000)
|
2020-03-29 21:32:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
body, err := json.Marshal(config.Genesis.Addresses)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-07 14:11:02 +08:00
|
|
|
lg.SetState(roleAddr, []byte("admin-roles"), body)
|
2020-03-29 21:32:01 +08:00
|
|
|
|
2020-05-07 14:11:02 +08:00
|
|
|
accounts, journal := lg.FlushDirtyDataAndComputeJournal()
|
2020-03-29 21:32:01 +08:00
|
|
|
block := &pb.Block{
|
|
|
|
BlockHeader: &pb.BlockHeader{
|
|
|
|
Number: 1,
|
2020-05-07 14:11:02 +08:00
|
|
|
StateRoot: journal.ChangedHash,
|
2020-03-29 21:32:01 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
block.BlockHash = block.Hash()
|
2020-05-07 14:11:02 +08:00
|
|
|
blockData := &ledger.BlockData{
|
|
|
|
Block: block,
|
|
|
|
Receipts: nil,
|
|
|
|
Accounts: accounts,
|
|
|
|
Journal: journal,
|
|
|
|
}
|
|
|
|
|
|
|
|
lg.PersistBlockData(blockData)
|
2020-03-29 21:32:01 +08:00
|
|
|
|
2020-05-07 14:11:02 +08:00
|
|
|
return nil
|
2020-03-29 21:32:01 +08:00
|
|
|
}
|