Skip to content

Commit

Permalink
Merge pull request #146 from wormholes-org/perf_Random11Coefficient
Browse files Browse the repository at this point in the history
Perf random11 coefficient
  • Loading branch information
wormholeslab authored Apr 11, 2023
2 parents dea11e7 + 5885786 commit 0d6260f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
59 changes: 55 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ type BlockChain struct {

stakerPool *types.StakerList
bytesStakersCh chan BytesStakerList
coefficients map[uint64]map[common.Address]uint8
}

type BytesStakerList struct {
Expand Down Expand Up @@ -272,6 +273,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
vmConfig: vmConfig,
stakerPool: new(types.StakerList),
bytesStakersCh: make(chan BytesStakerList, 100),
coefficients: make(map[uint64]map[common.Address]uint8, 0),
}
bc.validator = NewBlockValidator(chainConfig, bc, engine)
bc.prefetcher = newStatePrefetcher(chainConfig, bc, engine)
Expand Down Expand Up @@ -505,6 +507,37 @@ func (bc *BlockChain) WriteStakersToDB() {
}
}

func (bc *BlockChain) DeleteExpiredCoefficents() {
expired := 3
var minBlockNumber uint64 = 0
for {
if len(bc.coefficients) <= expired {
break
}
for k, _ := range bc.coefficients {
if minBlockNumber == 0 {
minBlockNumber = k
} else {
if minBlockNumber > k {
minBlockNumber = k
}
}
}
delete(bc.coefficients, minBlockNumber)
}
}

func (bc *BlockChain) DeleteCoefficients() {
var deleteItems []uint64
for k, _ := range bc.coefficients {
deleteItems = append(deleteItems, k)
}

for _, v := range deleteItems {
delete(bc.coefficients, v)
}
}

// GetStakerPool return bc.stakerPool
func (bc *BlockChain) GetStakerPool() *types.StakerList {
return bc.stakerPool
Expand Down Expand Up @@ -1665,6 +1698,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
return NonStatTy, err
}
log.Info("caver|validator-before", "no", block.Header().Number, "len", validatorPool.Len(), "state.PledgedTokenPool", len(state.PledgedTokenPool))
validatorsCoe := make(map[common.Address]uint8)
if len(state.PledgedTokenPool) > 0 {
for _, v := range state.PledgedTokenPool {
if v.Flag {
Expand All @@ -1679,8 +1713,11 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
// Recalculate the weight, which needs to be calculated after the list is determined
for _, account := range validatorPool.Validators {
coefficient := state.GetValidatorCoefficient(account.Addr)
validatorsCoe[account.Addr] = coefficient
validatorPool.CalculateAddressRangeV2(account.Addr, account.Balance, big.NewInt(int64(coefficient)))
}
bc.coefficients[block.NumberU64()] = validatorsCoe
bc.DeleteExpiredCoefficents()

bc.WriteValidatorPool(block.Header(), validatorPool)
log.Info("caver|validator-after", "no", block.Header().Number, "len", validatorPool.Len(), "state.PledgedTokenPool", len(state.PledgedTokenPool))
Expand Down Expand Up @@ -1723,6 +1760,8 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
if err := bc.reorg(currentBlock, block); err != nil {
return NonStatTy, err
}
// handle the fork problem
bc.DeleteCoefficients()
}
status = CanonStatTy
} else {
Expand Down Expand Up @@ -2962,8 +3001,14 @@ func (bc *BlockChain) Random11ValidatorFromPool(header *types.Header) (*types.Va

// Get all validator weights
var weights []uint8
for _, v := range validatorList.Validators {
weights = append(weights, db.GetCoefficient(v.Addr))
if validatorsCoe, ok := bc.coefficients[header.Number.Uint64()]; ok {
for _, v := range validatorList.Validators {
weights = append(weights, validatorsCoe[v.Addr])
}
} else {
for _, v := range validatorList.Validators {
weights = append(weights, db.GetCoefficient(v.Addr))
}
}

var validators []common.Address
Expand Down Expand Up @@ -3028,8 +3073,14 @@ func (bc *BlockChain) Random11ValidatorWithOutProxy(header *types.Header) (*type

// Get all validator weights
var weights []uint8
for _, v := range validatorList.Validators {
weights = append(weights, db.GetCoefficient(v.Addr))
if validatorsCoe, ok := bc.coefficients[header.Number.Uint64()]; ok {
for _, v := range validatorList.Validators {
weights = append(weights, validatorsCoe[v.Addr])
}
} else {
for _, v := range validatorList.Validators {
weights = append(weights, db.GetCoefficient(v.Addr))
}
}

var validators []common.Address
Expand Down
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
const (
VersionMajor = 0 // Major version component of the current release
VersionMinor = 13 // Minor version component of the current release
VersionPatch = 0 // Patch version component of the current release
VersionPatch = 1 // Patch version component of the current release
VersionMeta = "stable" // Version metadata to append to the version string
)

Expand Down
2 changes: 1 addition & 1 deletion wormholes_install.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
#check docker cmd
echo "Script version Number: v0.13.0"
echo "Script version Number: v0.13.1"
which docker >/dev/null 2>&1
if [ $? -ne 0 ] ; then
echo "docker not found, please install first!"
Expand Down

0 comments on commit 0d6260f

Please sign in to comment.