Skip to content

Commit

Permalink
hack for last round of removed node
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricfung committed Sep 27, 2022
1 parent 827899c commit 7962576
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
7 changes: 6 additions & 1 deletion kernel/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type Chain struct {
}

func (node *Node) buildChain(chainId crypto.Hash) *Chain {
logger.Printf("node.buildChain(%s)", chainId)

chain := &Chain{
node: node,
ChainId: chainId,
Expand All @@ -88,7 +90,10 @@ func (node *Node) buildChain(chainId crypto.Hash) *Chain {
panic(err)
}
if node.GetRemovedOrCancelledNode(chainId) != nil {
return chain
// FIXME
// return chain
// this comment because we can't ensure the last round of a removed node yet
// thus will cause inconsistence when calculate mint works
}

go chain.AggregateMintWork()
Expand Down
45 changes: 29 additions & 16 deletions kernel/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kernel

import (
"encoding/hex"
"errors"
"fmt"
"time"

Expand All @@ -10,6 +11,7 @@ import (
"github.com/MixinNetwork/mixin/crypto"
"github.com/MixinNetwork/mixin/kernel/internal/clock"
"github.com/MixinNetwork/mixin/logger"
"github.com/dgraph-io/badger/v3"
)

const (
Expand Down Expand Up @@ -47,38 +49,49 @@ func (chain *Chain) AggregateMintWork() {
logger.Printf("AggregateMintWork(%s) begin with %d\n", chain.ChainId, round)

fork := uint64(SnapshotRoundDayLeapForkHack.UnixNano())
period := time.Duration(config.SnapshotRoundGap / 2)
if chain.node.GetRemovedOrCancelledNode(chain.ChainId) != nil {
period = time.Duration(chain.node.custom.Node.KernelOprationPeriod/2) * time.Second
}
for chain.running {
if cs := chain.State; cs == nil {
logger.Printf("AggregateMintWork(%s) no state yet\n", chain.ChainId)
time.Sleep(time.Duration(chain.node.custom.Node.KernelOprationPeriod/2) * time.Second)
continue
}
frn := chain.State.FinalRound.Number
if frn < round {
time.Sleep(time.Duration(config.SnapshotRoundGap / 2))
continue
crn := chain.State.CacheRound.Number
if crn < round {
panic(fmt.Errorf("AggregateMintWork(%s) waiting %d %d", chain.ChainId, crn, round))
}

snapshots, err := chain.persistStore.ReadSnapshotWorksForNodeRound(chain.ChainId, round)
if err != nil {
logger.Verbosef("AggregateMintWork(%s) ERROR ReadSnapshotsForNodeRound %s\n", chain.ChainId, err.Error())
time.Sleep(100 * time.Millisecond)
continue
}
if len(snapshots) == 0 {
panic(fmt.Errorf("AggregateMintWork(%s) empty round %d", chain.ChainId, round))
time.Sleep(period)
continue
}
if chain.node.networkId.String() == config.MainnetId && snapshots[0].Timestamp < fork {
snapshots = nil
for chain.running {
if chain.node.networkId.String() == config.MainnetId && snapshots[0].Timestamp < fork {
snapshots = nil
}
err = chain.persistStore.WriteRoundWork(chain.ChainId, round, snapshots)
if err == nil {
break
}
if errors.Is(err, badger.ErrConflict) {
logger.Verbosef("AggregateMintWork(%s) ERROR WriteRoundWork %s\n", chain.ChainId, err.Error())
time.Sleep(100 * time.Millisecond)
continue
}
panic(err)
}

err = chain.persistStore.WriteRoundWork(chain.ChainId, round, snapshots)
if err != nil {
logger.Verbosef("AggregateMintWork(%s) ERROR WriteRoundWork %s\n", chain.ChainId, err.Error())
time.Sleep(100 * time.Millisecond)
continue
if round < crn {
round = round + 1
} else {
time.Sleep(period)
}
round = round + 1
}

logger.Printf("AggregateMintWork(%s) end with %d\n", chain.ChainId, round)
Expand Down
3 changes: 3 additions & 0 deletions kernel/round.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/crypto"
"github.com/MixinNetwork/mixin/kernel/internal/clock"
"github.com/MixinNetwork/mixin/logger"
"github.com/MixinNetwork/mixin/storage"
)

Expand All @@ -32,6 +33,8 @@ type FinalRound struct {

func (node *Node) LoadAllChains(store storage.Store, networkId crypto.Hash) error {
nodes := node.NodesListWithoutState(uint64(clock.Now().UnixNano()), false)
logger.Printf("node.LoadAllChains(%s) => %d", networkId, len(nodes))

for _, cn := range nodes {
if cn.State == common.NodeStatePledging || cn.State == common.NodeStateCancelled {
continue
Expand Down

0 comments on commit 7962576

Please sign in to comment.