Skip to content

Commit

Permalink
Fixes for validator set update for the initial dynasty
Browse files Browse the repository at this point in the history
  • Loading branch information
jieyilong committed Mar 21, 2023
1 parent b0d8032 commit d249b60
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 30 deletions.
1 change: 1 addition & 0 deletions interchain/witness/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ type ChainWitness interface {
Wait()
GetMainchainBlockHeight() (*big.Int, error)
GetValidatorSetByDynasty(dynasty *big.Int) (*score.ValidatorSet, error)
GetSubchainRegistrationHeight() (*big.Int, error)
GetInterChainEventCache() *siu.InterChainEventCache
}
78 changes: 48 additions & 30 deletions interchain/witness/metachain_witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,36 +574,38 @@ func (mw *MetachainWitness) updateValidatorSetCache(dynasty *big.Int) (*score.Va
// queryBlockHeight = big.NewInt(0).Add(queryBlockHeight, big.NewInt(1)) // increment by one to make sure the query block height falls into the dynasty
// vs, err := mw.chainRegistrarOnMainchain.GetValidatorSet(nil, mw.subchainID, queryBlockHeight)

adjustedDynasty := dynasty
registrationMainchainHeight, ok, err := mw.chainRegistrarOnMainchain.GetSubchainRegistrationHeight(nil, mw.subchainID)
if !ok {
errMsg := fmt.Sprintf("subchain with ID %v does not exist", mw.subchainID)
logger.Warnf("updateValidatorSetCache: %v", errMsg)
return nil, fmt.Errorf(errMsg)
}

if err != nil {
logger.Warnf("updateValidatorSetCache: %v", err)
return nil, err
}

registrationDynasty := scom.CalculateDynasty(registrationMainchainHeight)
if adjustedDynasty.Cmp(registrationDynasty) == 0 {
// Special handling for the initial dynasty query on the **Mainchain**:
// The initial set of validators hardcoded in the genesis snapshot should be
// in charge during the initial dyansty. However, if we directly query the
// initial dynasty, we would obtain an empty set since the validators registered
// on the mainchain takes charge only when the next dynasty begins. Hence, for
// the initial dyansty, we query the next dynasty instead, which should return
// a validator set that matches with the validator set hardcoded in the
// genesis snapshot.
adjustedDynasty = big.NewInt(0).Add(adjustedDynasty, big.NewInt(1))

logger.Debugf("Subchain %v registration main chain height: %v, registration dynasty: %v, dynasty: %v, adjustedDynasty: %v",
mw.subchainID, registrationMainchainHeight, registrationDynasty, dynasty, adjustedDynasty)
}

vs, err := mw.chainRegistrarOnMainchain.GetValidatorSet(nil, mw.subchainID, adjustedDynasty)
// adjustedDynasty := dynasty
// registrationMainchainHeight, ok, err := mw.chainRegistrarOnMainchain.GetSubchainRegistrationHeight(nil, mw.subchainID)
// if !ok {
// errMsg := fmt.Sprintf("subchain with ID %v does not exist", mw.subchainID)
// logger.Warnf("updateValidatorSetCache: %v", errMsg)
// return nil, fmt.Errorf(errMsg)
// }

// if err != nil {
// logger.Warnf("updateValidatorSetCache: %v", err)
// return nil, err
// }

// registrationDynasty := scom.CalculateDynasty(registrationMainchainHeight)
// if adjustedDynasty.Cmp(registrationDynasty) == 0 {
// // Special handling for the initial dynasty query on the **Mainchain**:
// // The initial set of validators hardcoded in the genesis snapshot should be
// // in charge during the initial dyansty. However, if we directly query the
// // initial dynasty, we would obtain an empty set since the validators registered
// // on the mainchain takes charge only when the next dynasty begins. Hence, for
// // the initial dyansty, we query the next dynasty instead, which should return
// // a validator set that matches with the validator set hardcoded in the
// // genesis snapshot.
// adjustedDynasty = big.NewInt(0).Add(adjustedDynasty, big.NewInt(1))

// logger.Debugf("Subchain %v registration main chain height: %v, registration dynasty: %v, dynasty: %v, adjustedDynasty: %v",
// mw.subchainID, registrationMainchainHeight, registrationDynasty, dynasty, adjustedDynasty)
// }

// vs, err := mw.chainRegistrarOnMainchain.GetValidatorSet(nil, mw.subchainID, adjustedDynasty)

vs, err := mw.chainRegistrarOnMainchain.GetValidatorSet(nil, mw.subchainID, dynasty)
validatorAddrs := vs.Validators
validatorStakes := vs.ShareAmounts

Expand Down Expand Up @@ -631,3 +633,19 @@ func (mw *MetachainWitness) updateValidatorSetCache(dynasty *big.Int) (*score.Va
func (mw *MetachainWitness) GetInterChainEventCache() *siu.InterChainEventCache {
return mw.interChainEventCache
}

func (mw *MetachainWitness) GetSubchainRegistrationHeight() (*big.Int, error) {
registrationMainchainHeight, ok, err := mw.chainRegistrarOnMainchain.GetSubchainRegistrationHeight(nil, mw.subchainID)
if !ok {
errMsg := fmt.Sprintf("subchain with ID %v does not exist", mw.subchainID)
logger.Warnf("GetSubchainRegistrationHeight: %v", errMsg)
return nil, fmt.Errorf(errMsg)
}

if err != nil {
logger.Warnf("GetSubchainRegistrationHeight: %v", err)
return nil, err
}

return registrationMainchainHeight, nil
}
13 changes: 13 additions & 0 deletions ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,19 @@ func (ledger *Ledger) getNewDynastyAndValidatorSet(view *slst.StoreView) (enteri
return false, nil, nil
}

registrationMainchainHeight, err := ledger.metachainWitness.GetSubchainRegistrationHeight()
if err != nil {
logger.Warnf("Failed to get subchain registration height: %v", err)
return false, nil, nil
}

registrationDynasty := scom.CalculateDynasty(registrationMainchainHeight)
if currentDynasty.Cmp(registrationDynasty) == 0 {
// For the initial dynasty, i.e. the dynasty during which the subchain was registered, instead of querying
// the validator set from the main chain, we trust the validator set in the snapshot
return false, nil, nil
}

witnessedDynasty := scom.CalculateDynasty(mainchainBlockHeight)
witnessedValidatorSet, err := ledger.metachainWitness.GetValidatorSetByDynasty(witnessedDynasty)
if err != nil {
Expand Down

0 comments on commit d249b60

Please sign in to comment.