Skip to content

Commit

Permalink
remove some redundant condition check for last snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricfung committed Dec 30, 2024
1 parent d785da1 commit adb9f2c
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 37 deletions.
4 changes: 2 additions & 2 deletions kernel/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (ab ActionBuffer) Poll() *CosiAction {
}

func (chain *Chain) loadIdentity() *CNode {
now := uint64(clock.Now().UnixNano())
now := clock.NowUnixNano()
for _, n := range chain.node.NodesListWithoutState(now, false) {
if chain.ChainId == n.IdForNetwork {
return n
Expand Down Expand Up @@ -219,7 +219,7 @@ func (chain *Chain) loadState() error {
state.RoundHistory = loadRoundHistoryForNode(chain.persistStore, final)
cache.Timestamp = final.Start + config.SnapshotRoundGap

allNodes := chain.node.NodesListWithoutState(uint64(clock.Now().UnixNano()), false)
allNodes := chain.node.NodesListWithoutState(clock.NowUnixNano(), false)
for _, cn := range allNodes {
if chain.ChainId == cn.IdForNetwork {
continue
Expand Down
4 changes: 2 additions & 2 deletions kernel/cosi.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (chain *Chain) checkActionSanity(m *CosiAction) error {
if s.Signature != nil || s.Timestamp != 0 {
return fmt.Errorf("only empty snapshot can be announced")
}
s.Timestamp = uint64(clock.Now().UnixNano())
s.Timestamp = clock.NowUnixNano()
case CosiActionSelfCommitment, CosiActionSelfFullCommitment, CosiActionSelfResponse:
if chain.ChainId != chain.node.IdForNetwork {
return fmt.Errorf("self action aggregation chain %s %s", chain.ChainId, chain.node.IdForNetwork)
Expand Down Expand Up @@ -236,7 +236,7 @@ func (chain *Chain) checkActionSanity(m *CosiAction) error {
return fmt.Errorf("invalid snapshot hash %s %s", m.SnapshotHash, s.Hash)
}
threshold := config.SnapshotRoundGap * config.SnapshotReferenceThreshold
if s.Timestamp > uint64(clock.Now().UnixNano())+threshold {
if s.Timestamp > clock.NowUnixNano()+threshold {
return fmt.Errorf("future snapshot timestamp %d", s.Timestamp)
}
if s.Timestamp+threshold*2 < chain.node.GraphTimestamp {
Expand Down
2 changes: 1 addition & 1 deletion kernel/custodian.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func (node *Node) validateCustodianUpdateNodes(s *common.Snapshot, tx *common.VersionedTransaction, finalized bool) error {
timestamp := s.Timestamp
if s.Timestamp == 0 && s.NodeId == node.IdForNetwork {
timestamp = uint64(clock.Now().UnixNano())
timestamp = clock.NowUnixNano()
}
eid := node.electSnapshotNode(common.TransactionTypeCustodianUpdateNodes, timestamp)
if eid != s.NodeId {
Expand Down
10 changes: 5 additions & 5 deletions kernel/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (node *Node) tryToSendRemoveTransaction() error {
func (node *Node) validateNodeRemoveSnapshot(s *common.Snapshot, tx *common.VersionedTransaction, finalized bool) error {
timestamp := s.Timestamp
if s.Timestamp == 0 && s.NodeId == node.IdForNetwork {
timestamp = uint64(clock.Now().UnixNano())
timestamp = clock.NowUnixNano()
}
eid := node.electSnapshotNode(common.TransactionTypeNodeRemove, timestamp)
if eid != s.NodeId {
Expand Down Expand Up @@ -309,7 +309,7 @@ func (chain *Chain) buildNodeAcceptTransaction(timestamp uint64, finalized bool)
}

func (chain *Chain) tryToSendAcceptTransaction() error {
now := uint64(clock.Now().UnixNano())
now := clock.NowUnixNano()
ver, err := chain.buildNodeAcceptTransaction(now, false)
if err != nil {
return err
Expand Down Expand Up @@ -337,7 +337,7 @@ func (chain *Chain) tryToSendAcceptTransaction() error {
func (node *Node) validateNodeAcceptSnapshot(s *common.Snapshot, tx *common.VersionedTransaction, finalized bool) error {
timestamp := s.Timestamp
if timestamp == 0 && s.NodeId == node.IdForNetwork {
timestamp = uint64(clock.Now().UnixNano())
timestamp = clock.NowUnixNano()
}
if s.RoundNumber != 0 {
return fmt.Errorf("invalid snapshot round %d", s.RoundNumber)
Expand Down Expand Up @@ -490,7 +490,7 @@ func (node *Node) getInitialExternalReference(s *common.Snapshot) (*FinalRound,
func (node *Node) validateNodePledgeSnapshot(s *common.Snapshot, tx *common.VersionedTransaction) error {
timestamp, totalNodes := s.Timestamp, 0
if s.Timestamp == 0 && s.NodeId == node.IdForNetwork {
timestamp = uint64(clock.Now().UnixNano())
timestamp = clock.NowUnixNano()
}
eid := node.electSnapshotNode(common.TransactionTypeNodePledge, timestamp)
if eid != s.NodeId {
Expand Down Expand Up @@ -544,7 +544,7 @@ func (node *Node) validateNodePledgeSnapshot(s *common.Snapshot, tx *common.Vers
func (node *Node) validateNodeCancelSnapshot(s *common.Snapshot, tx *common.VersionedTransaction, finalized bool) error {
timestamp := s.Timestamp
if s.Timestamp == 0 && s.NodeId == node.IdForNetwork {
timestamp = uint64(clock.Now().UnixNano())
timestamp = clock.NowUnixNano()
}
if timestamp < node.Epoch {
return fmt.Errorf("invalid snapshot timestamp %d %d", node.Epoch, timestamp)
Expand Down
2 changes: 1 addition & 1 deletion kernel/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func (chain *Chain) checkReferenceSanity(ec *Chain, external *common.Round, roun
}

cr, fr := ec.State.CacheRound, ec.State.FinalRound
if now := uint64(clock.Now().UnixNano()); fr.Start > now {
if now := clock.NowUnixNano(); fr.Start > now {
return fmt.Errorf("external hint round timestamp too future %d %d",
fr.Start, clock.Now().UnixNano())
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func mintMultiBatchesSize(old, batch uint64) common.Integer {
func (node *Node) validateMintSnapshot(snap *common.Snapshot, tx *common.VersionedTransaction) error {
timestamp := snap.Timestamp
if snap.Timestamp == 0 && snap.NodeId == node.IdForNetwork {
timestamp = uint64(clock.Now().UnixNano())
timestamp = clock.NowUnixNano()
}
eid := node.electSnapshotNode(common.TransactionTypeMint, timestamp)
if eid != snap.NodeId {
Expand Down
8 changes: 4 additions & 4 deletions kernel/mint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestUniversalMintTransaction(t *testing.T) {
round: 1,
}} {
clock.MockDiff(tr.diff)
timestamp := uint64(clock.Now().UnixNano())
timestamp := clock.NowUnixNano()
for i := 0; i < 2; i++ {
snapshots := testBuildMintSnapshots(signers, tr.round, timestamp)
err = node.persistStore.WriteRoundWork(node.IdForNetwork, tr.round, snapshots, true)
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestUniversalMintTransaction(t *testing.T) {
}
}

timestamp := uint64(clock.Now().UnixNano())
timestamp := clock.NowUnixNano()
cur := &common.CustodianUpdateRequest{Custodian: &custodian}
versioned = node.buildUniversalMintTransaction(cur, timestamp, false)
require.NotNil(versioned)
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestMintWorks(t *testing.T) {
require.Equal(uint64(0), offset)

signers := append(node.genesisNodes, node.IdForNetwork)
timestamp := uint64(clock.Now().UnixNano())
timestamp := clock.NowUnixNano()
leaders := len(signers)*2/3 + 1
for i := 0; i < 2; i++ {
snapshots := testBuildMintSnapshots(signers[1:], 0, timestamp)
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestMintWorks(t *testing.T) {
require.Equal(uint64(0), offset)
}

timestamp = uint64(clock.Now().UnixNano())
timestamp = clock.NowUnixNano()
snapshots := testBuildMintSnapshots(signers[1:], 1, timestamp)
err = node.persistStore.WriteRoundWork(node.IdForNetwork, 1, snapshots[:98], true)
require.Nil(err)
Expand Down
18 changes: 9 additions & 9 deletions kernel/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (node *Node) ListWorkingAcceptedNodes(timestamp uint64) []*CNode {
}

func (node *Node) GetAcceptedOrPledgingNode(id crypto.Hash) *CNode {
nodes := node.NodesListWithoutState(uint64(clock.Now().UnixNano()), false)
nodes := node.NodesListWithoutState(clock.NowUnixNano(), false)
for _, cn := range nodes {
if cn.IdForNetwork == id && (cn.State == common.NodeStateAccepted || cn.State == common.NodeStatePledging) {
return cn
Expand Down Expand Up @@ -300,7 +300,7 @@ func (node *Node) ConsensusThreshold(timestamp uint64, final bool) int {
}

func (node *Node) LoadConsensusNodes() error {
threshold := uint64(clock.Now().UnixNano()) * 2
threshold := clock.NowUnixNano() * 2
nodes := node.persistStore.ReadAllNodes(threshold, true)
sort.Slice(nodes, func(i, j int) bool {
if nodes[i].Timestamp < nodes[j].Timestamp {
Expand Down Expand Up @@ -479,7 +479,7 @@ func (node *Node) CachePutTransaction(peerId crypto.Hash, tx *common.VersionedTr

func (node *Node) ReadAllNodesWithoutState() []crypto.Hash {
var all []crypto.Hash
nodes := node.NodesListWithoutState(uint64(clock.Now().UnixNano()), false)
nodes := node.NodesListWithoutState(clock.NowUnixNano(), false)
for _, cn := range nodes {
all = append(all, cn.IdForNetwork)
}
Expand All @@ -496,7 +496,7 @@ func (node *Node) ReadSnapshotsForNodeRound(nodeIdWithNetwork crypto.Hash, round

func (node *Node) sendGraphToConcensusNodesAndPeers() {
for {
nodes := node.NodesListWithoutState(uint64(clock.Now().UnixNano()), true)
nodes := node.NodesListWithoutState(clock.NowUnixNano(), true)
neighbors := node.Peer.Neighbors()
peers := make(map[crypto.Hash]bool)
for _, cn := range nodes {
Expand Down Expand Up @@ -536,8 +536,8 @@ func (node *Node) CheckBroadcastedToPeers() bool {
}

final, count := node.chain.State.FinalRound.Number, 1
threshold := node.ConsensusThreshold(uint64(clock.Now().UnixNano()), false)
nodes := node.NodesListWithoutState(uint64(clock.Now().UnixNano()), true)
threshold := node.ConsensusThreshold(clock.NowUnixNano(), false)
nodes := node.NodesListWithoutState(clock.NowUnixNano(), true)
for _, cn := range nodes {
remote := spm[cn.IdForNetwork]
if remote == nil {
Expand All @@ -556,11 +556,11 @@ func (node *Node) CheckCatchUpWithPeers() bool {
return false
}

threshold := node.ConsensusThreshold(uint64(clock.Now().UnixNano()), false)
threshold := node.ConsensusThreshold(clock.NowUnixNano(), false)
cache, updated := node.chain.State.CacheRound, 1
final := node.chain.State.FinalRound.Number

nodes := node.NodesListWithoutState(uint64(clock.Now().UnixNano()), true)
nodes := node.NodesListWithoutState(clock.NowUnixNano(), true)
for _, cn := range nodes {
remote := spm[cn.IdForNetwork]
if remote == nil {
Expand Down Expand Up @@ -588,7 +588,7 @@ func (node *Node) CheckCatchUpWithPeers() bool {
cf.Hash, remote.Hash)
return false
}
if now := uint64(clock.Now().UnixNano()); cf.Start+config.SnapshotRoundGap*100 > now {
if now := clock.NowUnixNano(); cf.Start+config.SnapshotRoundGap*100 > now {
logger.Verbosef("CheckCatchUpWithPeers local start(%d)+%d > now(%d)\n",
cf.Start, config.SnapshotRoundGap*100, now)
return false
Expand Down
8 changes: 4 additions & 4 deletions kernel/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (node *Node) QueueTransaction(tx *common.VersionedTransaction) (string, err
return old.PayloadHash().String(), node.persistStore.CachePutTransaction(tx)
}

err = tx.Validate(node.persistStore, uint64(clock.Now().UnixNano()), false)
err = tx.Validate(node.persistStore, clock.NowUnixNano(), false)
if err != nil {
return "", err
}
Expand All @@ -56,7 +56,7 @@ func (node *Node) loopCacheQueue() {
continue
}

allNodes := node.ListWorkingAcceptedNodes(uint64(clock.Now().UnixNano()))
allNodes := node.ListWorkingAcceptedNodes(clock.NowUnixNano())
if len(allNodes) <= 0 {
continue
}
Expand Down Expand Up @@ -131,7 +131,7 @@ func (node *Node) filterLeadingNodes(all []*CNode) ([]*CNode, map[crypto.Hash]bo
defer node.chains.RUnlock()

threshold := 5 * uint64(time.Minute)
now := uint64(clock.Now().UnixNano())
now := clock.NowUnixNano()

leading := make([]*CNode, 0)
filter := make(map[crypto.Hash]bool)
Expand Down Expand Up @@ -172,7 +172,7 @@ func (node *Node) QueueState() (uint64, uint64, map[string][2]uint64) {

var caches, finals uint64
state := make(map[string][2]uint64)
accepted := node.NodesListWithoutState(uint64(clock.Now().UnixNano()), true)
accepted := node.NodesListWithoutState(clock.NowUnixNano(), true)
for _, cn := range accepted {
chain := node.chains.m[cn.IdForNetwork]
sa := [2]uint64{
Expand Down
2 changes: 1 addition & 1 deletion kernel/round.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type FinalRound struct {
}

func (node *Node) LoadAllChainsAndGraphTimestamp(store storage.Store, networkId crypto.Hash) error {
nodes := node.NodesListWithoutState(uint64(clock.Now().UnixNano()), false)
nodes := node.NodesListWithoutState(clock.NowUnixNano(), false)
for _, cn := range nodes {
chain := node.getOrCreateChain(cn.IdForNetwork)
if chain.State == nil {
Expand Down
4 changes: 2 additions & 2 deletions kernel/self_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func TestDetermineBestRound(t *testing.T) {
require.NotNil(node)

chain := node.BootChain(node.IdForNetwork)
best := chain.determineBestRound(uint64(clock.Now().UnixNano()))
best := chain.determineBestRound(clock.NowUnixNano())
require.Nil(best)

chain = node.BootChain(node.genesisNodes[0])
best = chain.determineBestRound(uint64(clock.Now().UnixNano()))
best = chain.determineBestRound(clock.NowUnixNano())
require.NotNil(best)
}
2 changes: 1 addition & 1 deletion kernel/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const (
// loss, and the payee will get back the whole pledge. so the punishment to
// a removing or slashing node is only drastically mint decline.
func (node *Node) GetRemovingOrSlashingNode(id crypto.Hash) *CNode {
now := uint64(clock.Now().UnixNano())
now := clock.NowUnixNano()
now, ready := prepareNodeRemovalTime(now, node.Epoch)
if !ready {
return nil
Expand Down
6 changes: 2 additions & 4 deletions kernel/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (node *Node) WitnessSnapshot(s *common.SnapshotWithTopologicalOrder) *Snaps
sig := node.Signer.PrivateSpendKey.Sign(msg)
return &SnapshotWitness{
Signature: &sig,
Timestamp: uint64(clock.Now().UnixNano()),
Timestamp: clock.NowUnixNano(),
}
}

Expand Down Expand Up @@ -101,11 +101,9 @@ func (topo *TopologicalSequence) TopoStats(node *Node) {
func (node *Node) getTopologyCounter(store storage.Store) *TopologicalSequence {
s, _ := store.LastSnapshot()
topo := &TopologicalSequence{
seq: s.TopologicalOrder,
filter: make(map[crypto.Hash]bool),
}
if s != nil {
topo.seq = s.TopologicalOrder
}
topo.point = topo.seq
go topo.TopoStats(node)
return topo
Expand Down
3 changes: 3 additions & 0 deletions storage/badger_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func (s *BadgerStore) LoadGenesis(rounds []*common.Round, snapshots []*common.Sn

cs := snapshots[len(snapshots)-1]
ct := transactions[len(snapshots)-1]
if cs.TopologicalOrder+1 != uint64(len(snapshots)) {
panic(cs.TopologicalOrder)
}
err = writeConsensusSnapshot(txn, cs.Snapshot, ct, nil)
if err != nil {
return err
Expand Down

0 comments on commit adb9f2c

Please sign in to comment.