github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/config/genesis_extensions.go (about)

     1  package config
     2  
     3  import (
     4  	"encoding/base64"
     5  	"fmt"
     6  
     7  	"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
     8  	"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
     9  )
    10  
    11  // Genesis represents a set of genesis block settings including the extensions
    12  // enabled in the genesis block or during native contracts initialization.
    13  type Genesis struct {
    14  	// Roles contains the set of roles that should be designated during native
    15  	// Designation contract initialization. It is NeoGo extension and must be
    16  	// disabled on the public Neo N3 networks.
    17  	Roles map[noderoles.Role]keys.PublicKeys
    18  	// Transaction contains transaction script that should be deployed in the
    19  	// genesis block. It is NeoGo extension and must be disabled on the public
    20  	// Neo N3 networks.
    21  	Transaction *GenesisTransaction
    22  }
    23  
    24  // GenesisTransaction is a placeholder for script that should be included into genesis
    25  // block as a transaction script with the given system fee. Provided
    26  // system fee value will be taken from the standby validators account which is
    27  // added to the list of Signers as a sender with CalledByEntry scope.
    28  type GenesisTransaction struct {
    29  	Script    []byte
    30  	SystemFee int64
    31  }
    32  
    33  type (
    34  	// genesisAux is an auxiliary structure for Genesis YAML marshalling.
    35  	genesisAux struct {
    36  		Roles       map[string]keys.PublicKeys `yaml:"Roles"`
    37  		Transaction *genesisTransactionAux     `yaml:"Transaction"`
    38  	}
    39  	// genesisTransactionAux is an auxiliary structure for GenesisTransaction YAML
    40  	// marshalling.
    41  	genesisTransactionAux struct {
    42  		Script    string `yaml:"Script"`
    43  		SystemFee int64  `yaml:"SystemFee"`
    44  	}
    45  )
    46  
    47  // MarshalYAML implements the YAML marshaler interface.
    48  func (e Genesis) MarshalYAML() (any, error) {
    49  	var aux genesisAux
    50  	aux.Roles = make(map[string]keys.PublicKeys, len(e.Roles))
    51  	for r, ks := range e.Roles {
    52  		aux.Roles[r.String()] = ks
    53  	}
    54  	if e.Transaction != nil {
    55  		aux.Transaction = &genesisTransactionAux{
    56  			Script:    base64.StdEncoding.EncodeToString(e.Transaction.Script),
    57  			SystemFee: e.Transaction.SystemFee,
    58  		}
    59  	}
    60  	return aux, nil
    61  }
    62  
    63  // UnmarshalYAML implements the YAML unmarshaler interface.
    64  func (e *Genesis) UnmarshalYAML(unmarshal func(any) error) error {
    65  	var aux genesisAux
    66  	if err := unmarshal(&aux); err != nil {
    67  		return err
    68  	}
    69  
    70  	e.Roles = make(map[noderoles.Role]keys.PublicKeys)
    71  	for s, ks := range aux.Roles {
    72  		r, ok := noderoles.FromString(s)
    73  		if !ok {
    74  			return fmt.Errorf("unknown node role: %s", s)
    75  		}
    76  		e.Roles[r] = ks
    77  	}
    78  
    79  	if aux.Transaction != nil {
    80  		script, err := base64.StdEncoding.DecodeString(aux.Transaction.Script)
    81  		if err != nil {
    82  			return fmt.Errorf("failed to decode script of genesis transaction: %w", err)
    83  		}
    84  		e.Transaction = &GenesisTransaction{
    85  			Script:    script,
    86  			SystemFee: aux.Transaction.SystemFee,
    87  		}
    88  	}
    89  
    90  	return nil
    91  }