2018-03-09 15:55:25 +00:00
|
|
|
package core
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
import (
|
2020-08-03 14:07:24 +00:00
|
|
|
"errors"
|
2018-03-25 10:45:54 +00:00
|
|
|
"time"
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2020-03-25 15:30:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2018-03-17 11:53:21 +00:00
|
|
|
)
|
2018-03-09 15:55:25 +00:00
|
|
|
|
2020-02-14 14:44:46 +00:00
|
|
|
var (
|
|
|
|
// governingTokenTX represents transaction that is used to create
|
|
|
|
// governing (NEO) token. It's a part of the genesis block.
|
|
|
|
governingTokenTX transaction.Transaction
|
|
|
|
|
|
|
|
// utilityTokenTX represents transaction that is used to create
|
|
|
|
// utility (GAS) token. It's a part of the genesis block. It's mostly
|
|
|
|
// useful for its hash that represents GAS asset ID.
|
|
|
|
utilityTokenTX transaction.Transaction
|
|
|
|
)
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// createGenesisBlock creates a genesis block based on the given configuration.
|
2020-01-14 12:32:07 +00:00
|
|
|
func createGenesisBlock(cfg config.ProtocolConfiguration) (*block.Block, error) {
|
2020-06-23 15:15:55 +00:00
|
|
|
validators, err := validatorsFromConfig(cfg)
|
2018-03-25 10:45:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nextConsensus, err := getNextConsensusAddress(validators)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-01 13:44:47 +00:00
|
|
|
base := block.Header{
|
2018-03-25 10:45:54 +00:00
|
|
|
Version: 0,
|
|
|
|
PrevHash: util.Uint256{},
|
2020-06-05 10:05:50 +00:00
|
|
|
Timestamp: uint64(time.Date(2016, 7, 15, 15, 8, 21, 0, time.UTC).Unix()) * 1000, // Milliseconds.
|
2018-03-25 10:45:54 +00:00
|
|
|
Index: 0,
|
|
|
|
NextConsensus: nextConsensus,
|
2019-12-09 14:14:10 +00:00
|
|
|
Script: transaction.Witness{
|
2018-03-25 10:45:54 +00:00
|
|
|
InvocationScript: []byte{},
|
2020-06-05 10:04:20 +00:00
|
|
|
VerificationScript: []byte{byte(opcode.PUSH1)},
|
2018-03-25 10:45:54 +00:00
|
|
|
},
|
2020-11-23 13:24:47 +00:00
|
|
|
StateRootEnabled: cfg.StateRootInHeader,
|
|
|
|
Network: cfg.Magic,
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 12:32:07 +00:00
|
|
|
b := &block.Block{
|
2021-03-01 13:44:47 +00:00
|
|
|
Header: base,
|
2020-12-08 15:28:00 +00:00
|
|
|
Transactions: []*transaction.Transaction{},
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
2020-09-15 15:38:15 +00:00
|
|
|
b.RebuildMerkleRoot()
|
2018-03-25 10:45:54 +00:00
|
|
|
|
2020-01-14 12:32:07 +00:00
|
|
|
return b, nil
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 15:15:55 +00:00
|
|
|
func validatorsFromConfig(cfg config.ProtocolConfiguration) ([]*keys.PublicKey, error) {
|
2020-08-05 08:30:14 +00:00
|
|
|
vs, err := committeeFromConfig(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return vs[:cfg.ValidatorsCount], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func committeeFromConfig(cfg config.ProtocolConfiguration) ([]*keys.PublicKey, error) {
|
2020-08-03 14:07:24 +00:00
|
|
|
if len(cfg.StandbyCommittee) < cfg.ValidatorsCount {
|
|
|
|
return nil, errors.New("validators count can be less than the size of StandbyCommittee")
|
|
|
|
}
|
2020-08-05 08:30:14 +00:00
|
|
|
validators := make([]*keys.PublicKey, len(cfg.StandbyCommittee))
|
2020-08-03 14:07:24 +00:00
|
|
|
for i := range validators {
|
|
|
|
pubKey, err := keys.NewPublicKeyFromString(cfg.StandbyCommittee[i])
|
2018-03-25 10:45:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
validators[i] = pubKey
|
|
|
|
}
|
|
|
|
return validators, nil
|
2018-03-10 12:04:06 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 13:29:42 +00:00
|
|
|
func getNextConsensusAddress(validators []*keys.PublicKey) (val util.Uint160, err error) {
|
2020-08-10 15:58:11 +00:00
|
|
|
raw, err := smartcontract.CreateDefaultMultiSigRedeemScript(validators)
|
2018-03-25 10:45:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return val, err
|
|
|
|
}
|
2019-08-23 15:50:45 +00:00
|
|
|
return hash.Hash160(raw), nil
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
// headerSliceReverse reverses the given slice of *Header.
|
2020-01-14 12:32:07 +00:00
|
|
|
func headerSliceReverse(dest []*block.Header) {
|
2018-03-17 11:53:21 +00:00
|
|
|
for i, j := 0, len(dest)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
dest[i], dest[j] = dest[j], dest[i]
|
|
|
|
}
|
|
|
|
}
|