[beacon] Only change epoch when beaconchain changed to higher epoch (#1823)

* [beacon] Only change epoch when beaconchain changed to higher epoch

* [beacon] Address PR comments
pull/1829/head
Edgar Aroutiounian 5 years ago committed by GitHub
parent 47890d1656
commit 15bc0eeb31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 47
      node/node_newblock.go
  2. 35
      node/worker/worker.go
  3. 5
      shard/committee/assignment.go

@ -1,7 +1,6 @@
package node package node
import ( import (
"math/big"
"sort" "sort"
"time" "time"
@ -11,7 +10,6 @@ import (
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/shard"
"github.com/harmony-one/harmony/shard/committee"
) )
// Constants of proposing a new block // Constants of proposing a new block
@ -118,7 +116,13 @@ func (node *Node) proposeNewBlock() (*types.Block, error) {
} }
// Prepare shard state // Prepare shard state
shardState := node.Worker.ProposeShardStateWithoutBeaconSync() shardState, err := node.Worker.SuperCommitteeForNextEpoch(
node.Consensus.ShardID, node.Beaconchain(),
)
if err != nil {
return nil, err
}
// Prepare last commit signatures // Prepare last commit signatures
sig, mask, err := node.Consensus.LastCommitSig() sig, mask, err := node.Consensus.LastCommitSig()
@ -129,43 +133,6 @@ func (node *Node) proposeNewBlock() (*types.Block, error) {
return node.Worker.FinalizeNewBlock(sig, mask, node.Consensus.GetViewID(), coinbase, crossLinks, shardState) return node.Worker.FinalizeNewBlock(sig, mask, node.Consensus.GetViewID(), coinbase, crossLinks, shardState)
} }
func (node *Node) proposeShardStateWithoutBeaconSync(block *types.Block) shard.State {
if block == nil || !shard.Schedule.IsLastBlock(block.Number().Uint64()) {
return nil
}
shardState, _ := committee.WithStakingEnabled.Compute(
new(big.Int).Add(block.Header().Epoch(), common.Big1), node.chainConfig, nil,
)
return shardState
}
func (node *Node) proposeShardState(block *types.Block) error {
switch node.Consensus.ShardID {
case 0:
return node.proposeBeaconShardState(block)
default:
node.proposeLocalShardState(block)
return nil
}
}
func (node *Node) proposeBeaconShardState(block *types.Block) error {
// TODO ek - replace this with variable epoch logic.
if !shard.Schedule.IsLastBlock(block.Number().Uint64()) {
// We haven't reached the end of this epoch; don't propose yet.
return nil
}
// TODO Use ReadFromComputation
prevEpoch := new(big.Int).Sub(block.Header().Epoch(), common.Big1)
shardState, err := committee.WithStakingEnabled.ReadFromDB(
prevEpoch, node.Blockchain(),
)
if err != nil {
return err
}
return block.AddShardState(shardState)
}
func (node *Node) proposeLocalShardState(block *types.Block) { func (node *Node) proposeLocalShardState(block *types.Block) {
logger := block.Logger(utils.Logger()) logger := block.Logger(utils.Logger())
// TODO ek – read this from beaconchain once BC sync is fixed // TODO ek – read this from beaconchain once BC sync is fixed

@ -281,15 +281,36 @@ func (w *Worker) IncomingReceipts() []*types.CXReceiptsProof {
return w.current.incxs return w.current.incxs
} }
// ProposeShardStateWithoutBeaconSync proposes the next shard state for next epoch. // SuperCommitteeForNextEpoch assumes only called by consensus leader
func (w *Worker) ProposeShardStateWithoutBeaconSync() shard.State { func (w *Worker) SuperCommitteeForNextEpoch(
if !shard.Schedule.IsLastBlock(w.current.header.Number().Uint64()) { shardID uint32,
return nil beacon *core.BlockChain,
) (shard.State, error) {
var (
nextCommittee shard.State
oops error
)
switch shardID {
case shard.BeaconChainShardID:
if shard.Schedule.IsLastBlock(w.current.header.Number().Uint64()) {
nextCommittee, oops = committee.WithStakingEnabled.Compute(
new(big.Int).Add(w.current.header.Epoch(), common.Big1),
*w.config,
beacon,
)
} }
shardState, _ := committee.WithStakingEnabled.Compute( default:
new(big.Int).Add(w.current.header.Epoch(), common.Big1), *w.config, nil, // WARN When we first enable staking, this condition may not be robust by itself.
switch beacon.CurrentHeader().Epoch().Cmp(w.current.header.Epoch()) {
case 1:
nextCommittee, oops = committee.WithStakingEnabled.ReadFromDB(
beacon.CurrentHeader().Epoch(), beacon,
) )
return shardState }
}
return nextCommittee, oops
} }
// FinalizeNewBlock generate a new block for the next consensus round. // FinalizeNewBlock generate a new block for the next consensus round.

@ -12,7 +12,6 @@ import (
"github.com/harmony-one/harmony/internal/ctxerror" "github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/internal/params" "github.com/harmony-one/harmony/internal/params"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/shard"
staking "github.com/harmony-one/harmony/staking/types" staking "github.com/harmony-one/harmony/staking/types"
) )
@ -51,7 +50,7 @@ type Reader interface {
// StakingCandidatesReader .. // StakingCandidatesReader ..
type StakingCandidatesReader interface { type StakingCandidatesReader interface {
ValidatorInformation(addr common.Address) (*staking.Validator, error) ValidatorInformation(addr common.Address) (*staking.Validator, error)
ValidatorStakingWithDelegation(addr common.Address) numeric.Dec ValidatorStakingWithDelegation(addr common.Address) *big.Int
ValidatorCandidates() []common.Address ValidatorCandidates() []common.Address
} }
@ -203,7 +202,7 @@ func (def partialStakingEnabled) ReadPublicKeysFromDB(
return nil, nil return nil, nil
} }
// ReadPublicKeysFromChain produces publicKeys of entire supercommittee per epoch, optionally providing a // ComputePublicKeys produces publicKeys of entire supercommittee per epoch, optionally providing a
// shard specific subcommittee // shard specific subcommittee
func (def partialStakingEnabled) ComputePublicKeys( func (def partialStakingEnabled) ComputePublicKeys(
epoch *big.Int, reader ChainReader, shardID int, epoch *big.Int, reader ChainReader, shardID int,

Loading…
Cancel
Save