*: add GenesisTransaction extension to the protocol configuration

Provide a script that should be deployed in the genesis block.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
Anna Shaleva 2023-10-19 15:29:41 +03:00
parent 065bd3f0be
commit 8cc32a91b6
8 changed files with 178 additions and 13 deletions

View file

@ -1,6 +1,7 @@
package core
import (
"fmt"
"time"
"github.com/nspcc-dev/neo-go/pkg/config"
@ -15,7 +16,7 @@ import (
// CreateGenesisBlock creates a genesis block based on the given configuration.
func CreateGenesisBlock(cfg config.ProtocolConfiguration) (*block.Block, error) {
validators, err := validatorsFromConfig(cfg)
validators, committee, err := validatorsFromConfig(cfg)
if err != nil {
return nil, err
}
@ -25,6 +26,49 @@ func CreateGenesisBlock(cfg config.ProtocolConfiguration) (*block.Block, error)
return nil, err
}
txs := []*transaction.Transaction{}
if cfg.Genesis.Transaction != nil {
committeeH, err := getCommitteeAddress(committee)
if err != nil {
return nil, fmt.Errorf("failed to calculate committee address: %w", err)
}
tx := cfg.Genesis.Transaction
signers := []transaction.Signer{
{
Account: nextConsensus,
Scopes: transaction.CalledByEntry,
},
}
scripts := []transaction.Witness{
{
InvocationScript: []byte{},
VerificationScript: []byte{byte(opcode.PUSH1)},
},
}
if !committeeH.Equals(nextConsensus) {
signers = append(signers, []transaction.Signer{
{
Account: committeeH,
Scopes: transaction.CalledByEntry,
},
}...)
scripts = append(scripts, []transaction.Witness{
{
InvocationScript: []byte{},
VerificationScript: []byte{byte(opcode.PUSH1)},
},
}...)
}
txs = append(txs, &transaction.Transaction{
SystemFee: tx.SystemFee,
ValidUntilBlock: 1,
Script: tx.Script,
Signers: signers,
Scripts: scripts,
})
}
base := block.Header{
Version: 0,
PrevHash: util.Uint256{},
@ -41,19 +85,19 @@ func CreateGenesisBlock(cfg config.ProtocolConfiguration) (*block.Block, error)
b := &block.Block{
Header: base,
Transactions: []*transaction.Transaction{},
Transactions: txs,
}
b.RebuildMerkleRoot()
return b, nil
}
func validatorsFromConfig(cfg config.ProtocolConfiguration) ([]*keys.PublicKey, error) {
func validatorsFromConfig(cfg config.ProtocolConfiguration) ([]*keys.PublicKey, []*keys.PublicKey, error) {
vs, err := keys.NewPublicKeysFromStrings(cfg.StandbyCommittee)
if err != nil {
return nil, err
return nil, nil, err
}
return vs[:cfg.GetNumOfCNs(0)], nil
return vs.Copy()[:cfg.GetNumOfCNs(0)], vs, nil
}
func getNextConsensusAddress(validators []*keys.PublicKey) (val util.Uint160, err error) {
@ -64,6 +108,14 @@ func getNextConsensusAddress(validators []*keys.PublicKey) (val util.Uint160, er
return hash.Hash160(raw), nil
}
func getCommitteeAddress(committee []*keys.PublicKey) (val util.Uint160, err error) {
raw, err := smartcontract.CreateMajorityMultiSigRedeemScript(committee)
if err != nil {
return val, err
}
return hash.Hash160(raw), nil
}
// hashSliceReverse reverses the given slice of util.Uint256.
func hashSliceReverse(dest []util.Uint256) {
for i, j := 0, len(dest)-1; i < j; i, j = i+1, j-1 {