Skip to content

Commit

Permalink
Add option to provide BLS keys to validators in the genesis (#2371)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemadero authored Nov 27, 2023
1 parent 42161aa commit b9ab41a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
9 changes: 6 additions & 3 deletions genesis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/formatting/address"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
)

var (
Expand Down Expand Up @@ -58,9 +59,10 @@ func (a Allocation) Less(other Allocation) bool {
}

type Staker struct {
NodeID ids.NodeID `json:"nodeID"`
RewardAddress ids.ShortID `json:"rewardAddress"`
DelegationFee uint32 `json:"delegationFee"`
NodeID ids.NodeID `json:"nodeID"`
RewardAddress ids.ShortID `json:"rewardAddress"`
DelegationFee uint32 `json:"delegationFee"`
Signer *signer.ProofOfPossession `json:"signer,omitempty"`
}

func (s Staker) Unparse(networkID uint32) (UnparsedStaker, error) {
Expand All @@ -73,6 +75,7 @@ func (s Staker) Unparse(networkID uint32) (UnparsedStaker, error) {
NodeID: s.NodeID,
RewardAddress: avaxAddr,
DelegationFee: s.DelegationFee,
Signer: s.Signer,
}, err
}

Expand Down
1 change: 1 addition & 0 deletions genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ func FromConfig(config *Config) ([]byte, ids.ID, error) {
},
Staked: utxos,
ExactDelegationFee: &delegationFee,
Signer: staker.Signer,
},
)
}
Expand Down
9 changes: 6 additions & 3 deletions genesis/unparsed_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/formatting/address"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
)

var errInvalidETHAddress = errors.New("invalid eth address")
Expand Down Expand Up @@ -54,15 +55,17 @@ func (ua UnparsedAllocation) Parse() (Allocation, error) {
}

type UnparsedStaker struct {
NodeID ids.NodeID `json:"nodeID"`
RewardAddress string `json:"rewardAddress"`
DelegationFee uint32 `json:"delegationFee"`
NodeID ids.NodeID `json:"nodeID"`
RewardAddress string `json:"rewardAddress"`
DelegationFee uint32 `json:"delegationFee"`
Signer *signer.ProofOfPossession `json:"signer,omitempty"`
}

func (us UnparsedStaker) Parse() (Staker, error) {
s := Staker{
NodeID: us.NodeID,
DelegationFee: us.DelegationFee,
Signer: us.Signer,
}

_, _, avaxAddrBytes, err := address.Parse(us.RewardAddress)
Expand Down
45 changes: 32 additions & 13 deletions vms/platformvm/api/static_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ type PermissionlessValidator struct {
// GenesisPermissionlessValidator should to be used for genesis validators only.
type GenesisPermissionlessValidator struct {
GenesisValidator
RewardOwner *Owner `json:"rewardOwner,omitempty"`
DelegationFee json.Float32 `json:"delegationFee"`
ExactDelegationFee *json.Uint32 `json:"exactDelegationFee,omitempty"`
Staked []UTXO `json:"staked,omitempty"`
RewardOwner *Owner `json:"rewardOwner,omitempty"`
DelegationFee json.Float32 `json:"delegationFee"`
ExactDelegationFee *json.Uint32 `json:"exactDelegationFee,omitempty"`
Staked []UTXO `json:"staked,omitempty"`
Signer *signer.ProofOfPossession `json:"signer,omitempty"`
}

// PermissionedValidator is the repr. of a permissioned validator sent over APIs.
Expand Down Expand Up @@ -315,21 +316,39 @@ func (*StaticService) BuildGenesis(_ *http.Request, args *BuildGenesisArgs, repl
delegationFee = uint32(*vdr.ExactDelegationFee)
}

tx := &txs.Tx{Unsigned: &txs.AddValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
var (
baseTx = txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: uint32(args.NetworkID),
BlockchainID: ids.Empty,
}},
Validator: txs.Validator{
}}
validator = txs.Validator{
NodeID: vdr.NodeID,
Start: uint64(args.Time),
End: uint64(vdr.EndTime),
Wght: weight,
},
StakeOuts: stake,
RewardsOwner: owner,
DelegationShares: delegationFee,
}}
}
tx *txs.Tx
)
if vdr.Signer == nil {
tx = &txs.Tx{Unsigned: &txs.AddValidatorTx{
BaseTx: baseTx,
Validator: validator,
StakeOuts: stake,
RewardsOwner: owner,
DelegationShares: delegationFee,
}}
} else {
tx = &txs.Tx{Unsigned: &txs.AddPermissionlessValidatorTx{
BaseTx: baseTx,
Validator: validator,
Signer: vdr.Signer,
StakeOuts: stake,
ValidatorRewardsOwner: owner,
DelegatorRewardsOwner: owner,
DelegationShares: delegationFee,
}}
}

if err := tx.Initialize(txs.GenesisCodec); err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1319,13 +1319,13 @@ func (s *state) syncGenesis(genesisBlk block.Block, genesis *genesis.Genesis) er

// Persist primary network validator set at genesis
for _, vdrTx := range genesis.Validators {
tx, ok := vdrTx.Unsigned.(*txs.AddValidatorTx)
validatorTx, ok := vdrTx.Unsigned.(txs.ValidatorTx)
if !ok {
return fmt.Errorf("expected tx type *txs.AddValidatorTx but got %T", vdrTx.Unsigned)
return fmt.Errorf("expected tx type txs.ValidatorTx but got %T", vdrTx.Unsigned)
}

stakeAmount := tx.Validator.Wght
stakeDuration := tx.Validator.Duration()
stakeAmount := validatorTx.Weight()
stakeDuration := validatorTx.EndTime().Sub(validatorTx.StartTime())
currentSupply, err := s.GetCurrentSupply(constants.PrimaryNetworkID)
if err != nil {
return err
Expand All @@ -1341,7 +1341,7 @@ func (s *state) syncGenesis(genesisBlk block.Block, genesis *genesis.Genesis) er
return err
}

staker, err := NewCurrentStaker(vdrTx.ID(), tx, potentialReward)
staker, err := NewCurrentStaker(vdrTx.ID(), validatorTx, potentialReward)
if err != nil {
return err
}
Expand Down

0 comments on commit b9ab41a

Please sign in to comment.