Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,17 @@ type Proposal struct {
}

// NewProposal creates a new proposal
func NewProposal(t ProposalType, blockNum uint64) Proposal {
func NewProposal(t ProposalType, blockNum uint64, stackTrace ...string) Proposal {
if len(stackTrace) > 0 {
return Proposal{
Type: t,
Caller: stackTrace[0],
blockNum: blockNum,
}
}
return Proposal{
Type: t,
Caller: utils.GetCallStackInfo(2),
Caller: utils.GetStackTrace(2),
blockNum: blockNum,
}
}
Expand Down
6 changes: 5 additions & 1 deletion consensus/consensus_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ func (consensus *Consensus) updateConsensusInformation(reason string) Mode {
if consensus.Blockchain().Config().IsTwoSeconds(nextEpoch) {
consensus.BlockPeriod = 2 * time.Second
}
if consensus.Blockchain().Config().IsOneSecond(nextEpoch) {
consensus.BlockPeriod = 1 * time.Second
}

isFirstTimeStaking := consensus.Blockchain().Config().IsStaking(nextEpoch) &&
curHeader.IsLastBlockInEpoch() && !consensus.Blockchain().Config().IsStaking(curEpoch)
Expand Down Expand Up @@ -493,11 +496,12 @@ func (consensus *Consensus) updateConsensusInformation(reason string) Mode {
// If the leader changed and I myself become the leader
if (oldLeader != nil && consensus.getLeaderPubKey() != nil &&
!consensus.getLeaderPubKey().Object.IsEqual(oldLeader.Object)) && consensus.isLeader() {
trace := utils.GetStackTrace(1)
go func() {
consensus.GetLogger().Info().
Str("myKey", myPubKeys.SerializeToHexStr()).
Msg("[UpdateConsensusInformation] I am the New Leader")
consensus.ReadySignal(NewProposal(SyncProposal, curHeader.NumberU64()+1), "updateConsensusInformation", "leader changed and I am the new leader")
consensus.ReadySignal(NewProposal(SyncProposal, curHeader.NumberU64()+1, trace), "updateConsensusInformation", "leader changed and I am the new leader")
}()
}
return Normal
Expand Down
12 changes: 9 additions & 3 deletions consensus/consensus_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ func (consensus *Consensus) _finalCommit(isLeader bool) {
}

block.SetCurrentCommitSig(commitSigAndBitmap)
// commitBlock internally calls setupForNewConsensus, which updates leader key
err = consensus.commitBlock(block, FBFTMsg)

if err != nil || consensus.BlockNum()-beforeCatchupNum != 1 {
Expand Down Expand Up @@ -289,10 +290,11 @@ func (consensus *Consensus) _finalCommit(isLeader bool) {
if isLeader {
if block.IsLastBlockInEpoch() {
// No pipelining
if !consensus.isRotation(block.Epoch()) {
trace := utils.GetStackTrace(1)
if consensus.isMyKey(consensus.getLeaderPubKey()) && !consensus.isRotation(block.Epoch()) {
go func() {
consensus.getLogger().Info().Msg("[finalCommit] sending block proposal signal")
consensus.ReadySignal(NewProposal(SyncProposal, block.NumberU64()+1), "finalCommit", "I am leader and it's the last block in epoch")
consensus.ReadySignal(NewProposal(SyncProposal, block.NumberU64()+1, trace), "finalCommit", "I am leader and it's the last block in epoch")
}()
}
} else {
Expand Down Expand Up @@ -727,6 +729,9 @@ func (consensus *Consensus) commitBlock(blk *types.Block, committedMsg *FBFTMess
// rotateLeader rotates the leader to the next leader in the committee.
// This function must be called with enabled leader rotation.
func (consensus *Consensus) rotateLeader(epoch *big.Int, defaultKey *bls.PublicKeyWrapper) *bls.PublicKeyWrapper {
if !consensus.isRotation(epoch) {
return defaultKey
}
var (
bc = consensus.Blockchain()
leader = consensus.getLeaderPubKey()
Expand Down Expand Up @@ -883,9 +888,10 @@ func (consensus *Consensus) setupForNewConsensus(blk *types.Block, committedMsg
if consensus.isLeader() && newLeader && !wasLeader {
// leader changed
blockPeriod := consensus.BlockPeriod
trace := utils.GetStackTrace(1)
go func() {
<-time.After(blockPeriod)
consensus.ReadySignal(NewProposal(SyncProposal, blk.NumberU64()+1), "setupForNewConsensus", "I am the new leader")
consensus.ReadySignal(NewProposal(SyncProposal, blk.NumberU64()+1, trace), "setupForNewConsensus", "I am the new leader")
}()
}
}
Expand Down
7 changes: 6 additions & 1 deletion internal/chain/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,14 @@ func getDefaultStakingReward(bc engine.ChainReader, epoch *big.Int, blockNum uin
if bc.Config().IsTwoSeconds(epoch) {
defaultReward = stakingReward.TwoSecStakedBlocks
}
if bc.Config().IsOneSecond(epoch) {
defaultReward = stakingReward.OneSecStakedBlock
}
} else {
// Mainnet (other nets):
if bc.Config().IsHIP30(epoch) {
if bc.Config().IsOneSecond(epoch) {
defaultReward = stakingReward.OneSecStakedBlock
} else if bc.Config().IsHIP30(epoch) {
defaultReward = stakingReward.HIP30StakedBlocks
} else if bc.Config().IsTwoSeconds(epoch) {
defaultReward = stakingReward.TwoSecStakedBlocks
Expand Down
2 changes: 2 additions & 0 deletions internal/configs/sharding/localnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const (

func (ls localnetSchedule) InstanceForEpoch(epoch *big.Int) Instance {
switch {
case params.LocalnetChainConfig.IsOneSecond(epoch):
return localnetV4
case params.LocalnetChainConfig.IsHIP30(epoch):
return localnetV4
case params.LocalnetChainConfig.IsFeeCollectEpoch(epoch):
Expand Down
9 changes: 9 additions & 0 deletions internal/params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ var (
MaxRateEpoch: EpochTBD,
DevnetExternalEpoch: EpochTBD,
TestnetExternalEpoch: EpochTBD,
IsOneSecondEpoch: big.NewInt(4),
}

// AllProtocolChanges ...
Expand Down Expand Up @@ -374,6 +375,7 @@ var (
big.NewInt(0),
big.NewInt(0),
big.NewInt(0),
big.NewInt(0),
}

// TestChainConfig ...
Expand Down Expand Up @@ -425,6 +427,7 @@ var (
big.NewInt(0), // MaxRateEpoch
big.NewInt(0),
big.NewInt(0),
big.NewInt(0),
}

// TestRules ...
Expand Down Expand Up @@ -606,6 +609,8 @@ type ChainConfig struct {
// vote power feature https://github.com/harmony-one/harmony/pull/4683
// if crosslink are not sent for an entire epoch signed and toSign will be 0 and 0. when that happen, next epoch there will no shard 1 validator elected in the committee.
HIP32Epoch *big.Int `json:"hip32-epoch,omitempty"`

IsOneSecondEpoch *big.Int `json:"is-one-second-epoch,omitempty"`
}

// String implements the fmt.Stringer interface.
Expand Down Expand Up @@ -731,6 +736,10 @@ func (c *ChainConfig) IsTwoSeconds(epoch *big.Int) bool {
return isForked(c.TwoSecondsEpoch, epoch)
}

func (c *ChainConfig) IsOneSecond(epoch *big.Int) bool {
return isForked(c.IsOneSecondEpoch, epoch)
}

// IsSixtyPercent determines whether it is the epoch to reduce internal voting power to 60%
func (c *ChainConfig) IsSixtyPercent(epoch *big.Int) bool {
return isForked(c.SixtyPercentEpoch, epoch)
Expand Down
16 changes: 16 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,19 @@ func FatalError(err error) {
func FatalErrMsg(err error, format string, args ...interface{}) {
FatalError(errors.WithMessagef(err, format, args...))
}

func GetStackTrace(i int) string {
stackBuf := strings.Builder{}

for ; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
stackBuf.WriteString(file)
stackBuf.WriteString(":")
stackBuf.WriteString(fmt.Sprint(line))
stackBuf.WriteString(" ")
}
return stackBuf.String()
}
7 changes: 7 additions & 0 deletions internal/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,10 @@ func TestIsPrivateIP(t *testing.T) {
}
}
}

func TestGetStackTrace(t *testing.T) {
stack := GetStackTrace()
if len(stack) == 0 {
t.Errorf("GetStackTrace failed")
}
}
28 changes: 25 additions & 3 deletions node/harmony/node_syncing.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,29 @@ func (p *LocalSyncingPeerProvider) SyncingPeers(shardID uint32) (peers []p2p.Pee
return nil, errors.Errorf(
"shard ID %d out of range 0..%d", shardID, p.numShards-1)
}
shards := [][]string{
{"6000", "6004", "6008", "6012", "6016", "6020", "6100", "6104", "6108", "6112", "6116", "6120"},
{"6002", "6006", "6010", "6014", "6018", "6022", "6102", "6106", "6110", "6114", "6118", "6122"},
shards := [][]string{}
if true {
shards = [][]string{
{
"6000",
"6004",
"6008",
"6120",
},
{
"6002",
"6006",
"6010",
"6122",
},
}
} else {
shards = [][]string{
{"6000", "6004", "6008", "6012", "6016", "6020", "6100", "6104", "6108", "6112", "6116", "6120"},
{"6002", "6006", "6010", "6014", "6018", "6022", "6102", "6106", "6110", "6114", "6118", "6122"},
}
}

selfport := fmt.Sprint(p.selfPort)
for _, port := range shards[shardID] {
if port == selfport {
Expand Down Expand Up @@ -317,6 +336,9 @@ func (node *Node) doSync(syncInstance ISync, syncingPeerProvider SyncingPeerProv
}
// TODO: treat fake maximum height
if isSynchronized, _, _ := syncInstance.GetParsedSyncStatusDoubleChecked(); !isSynchronized {
if consensus.IsLeader() {
return
}
node.IsSynchronized.UnSet()
if willJoinConsensus {
consensus.BlocksNotSynchronized("node.doSync")
Expand Down
5 changes: 5 additions & 0 deletions staking/reward/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ var (
big.NewInt(14*denominations.Nano), big.NewInt(denominations.Nano),
))

// OneSecStakedBlock is half of HIP30
OneSecStakedBlock = numeric.NewDecFromBigInt(new(big.Int).Mul(
big.NewInt(7*denominations.Nano), big.NewInt(denominations.Nano),
))

// TotalInitialTokens is the total amount of tokens (in ONE) at block 0 of the network.
// This should be set/change on the node's init according to the core.GenesisSpec.
TotalInitialTokens = numeric.Dec{Int: big.NewInt(0)}
Expand Down