Skip to content

Commit

Permalink
fix some lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricfung committed Oct 7, 2024
1 parent 5340562 commit d7caee3
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 47 deletions.
87 changes: 62 additions & 25 deletions kernel/cosi.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ type CosiVerifier struct {
}

func (node *Node) cosiAcceptedNodesListShuffle(ts uint64) []*CNode {
var nodes []*CNode
for _, n := range node.NodesListWithoutState(ts, true) {
nodes = append(nodes, n)
}
var nodes []*CNode // copy the nodes list to avoid conflicts
nodes = append(nodes, node.NodesListWithoutState(ts, true)...)
for i := len(nodes) - 1; i > 0; i-- {
j := int(ts % uint64(i+1))
nodes[i], nodes[j] = nodes[j], nodes[i]
Expand All @@ -89,9 +87,9 @@ func (chain *Chain) cosiHook(m *CosiAction) (bool, error) {
if m.finalized || !m.WantTx || m.PeerId == chain.node.IdForNetwork {
return m.finalized, nil
}
logger.Debugf("cosiHook finalized snapshot without transaction %s %s %s\n",
m.PeerId, m.SnapshotHash, m.Snapshot.SoleTransaction())
chain.node.Peer.SendTransactionRequestMessage(m.PeerId, m.Snapshot.SoleTransaction())
err = chain.node.Peer.SendTransactionRequestMessage(m.PeerId, m.Snapshot.SoleTransaction())
logger.Debugf("cosiHook finalized snapshot without transaction %s %s %s %v\n",
m.PeerId, m.SnapshotHash, m.Snapshot.SoleTransaction(), err)
return m.finalized, nil
}

Expand Down Expand Up @@ -310,7 +308,7 @@ func (chain *Chain) prepareAnnouncement(m *CosiAction) (bool, error) {
logger.Verbosef("cosiSendAnnouncement new best external %s:%d:%d => %s:%d:%d\n",
external.NodeId, external.Number, external.Timestamp, best.NodeId, best.Number, best.Start)
references := &common.RoundLink{Self: final.Hash, External: best.Hash}
err := chain.updateEmptyHeadRoundAndPersist(m, final, cache, references, s.Timestamp, true)
err := chain.updateEmptyHeadRoundAndPersist(final, cache, references, s.Timestamp, true)
if err != nil {
logger.Verbosef("ERROR cosiSendAnnouncement updateEmptyHeadRoundAndPersist failed %s %s %v\n",
m.PeerId, s.Hash, err)
Expand Down Expand Up @@ -339,6 +337,9 @@ func (chain *Chain) prepareAnnouncement(m *CosiAction) (bool, error) {
chain.CosiVerifiers = make(map[crypto.Hash]*CosiVerifier)
}
cache.Timestamp = s.Timestamp
if final.Number+1 != cache.Number {
panic(final.Number)
}

if len(cache.Snapshots) > 0 {
cft := cache.Snapshots[0].Timestamp
Expand Down Expand Up @@ -410,7 +411,10 @@ func (chain *Chain) cosiSendAnnouncement(m *CosiAction) error {
Snapshot: s,
WantTx: true,
}
chain.AppendCosiAction(cam)
err = chain.AppendCosiAction(cam)
if err != nil {
logger.Verbosef("cosiSendAnnouncement AppendCosiAction(%s, %s) ERROR %v\n", peerId, s.Hash, err)
}
}
return nil
}
Expand Down Expand Up @@ -521,7 +525,10 @@ func (chain *Chain) cosiHandleFullChallenge(m *CosiAction) error {
Signature: m.Signature,
Transaction: m.Transaction,
}
chain.AppendCosiAction(ccm)
err = chain.AppendCosiAction(ccm)
if err != nil {
logger.Verbosef("cosiHandleFullChallenge AppendCosiAction(%s) ERROR %v\n", s.Hash, err)
}
return nil
}

Expand Down Expand Up @@ -552,7 +559,7 @@ func (chain *Chain) checkAnnouncementOrChallenge(m *CosiAction) (bool, error) {
return false, nil
}
if s.RoundNumber == cache.Number && !s.References.Equal(cache.References) {
err := chain.updateEmptyHeadRoundAndPersist(m, final, cache, s.References, s.Timestamp, true)
err := chain.updateEmptyHeadRoundAndPersist(final, cache, s.References, s.Timestamp, true)
if err != nil {
logger.Verbosef("checkAnnouncementOrChallenge %s %v updateEmptyHeadRoundAndPersist %v\n",
m.PeerId, m.Snapshot, err)
Expand All @@ -574,6 +581,9 @@ func (chain *Chain) checkAnnouncementOrChallenge(m *CosiAction) (bool, error) {
cache, final = nc, nf
chain.CosiVerifiers = make(map[crypto.Hash]*CosiVerifier)
}
if final.Number+1 != cache.Number {
panic(final.Number)
}

if err := cache.ValidateSnapshot(s); err != nil {
logger.Verbosef("checkAnnouncementOrChallenge %s %v ValidateSnapshot %s\n",
Expand Down Expand Up @@ -646,7 +656,10 @@ func (chain *Chain) cosiHandleResponse(m *CosiAction) error {
return nil
}

s.Signature.AggregateResponse(publics, agg.Responses, m.SnapshotHash, false)
err = s.Signature.AggregateResponse(publics, agg.Responses, m.SnapshotHash, false)
if err != nil {
panic(err)
}
signers, finalized := chain.node.cacheVerifyCosi(m.SnapshotHash, s.Signature, cids, publics, base)
if !finalized {
logger.Verbosef("cosiHandleResponse %v AGGREGATE ERROR\n", m)
Expand Down Expand Up @@ -679,7 +692,10 @@ func (chain *Chain) cosiHandleResponse(m *CosiAction) error {
return nil
}

chain.AddSnapshot(final, cache, s, signers)
err = chain.AddSnapshot(final, cache, s, signers)
if err != nil {
panic(err)
}
}

nodes := chain.node.cosiAcceptedNodesListShuffle(s.Timestamp)
Expand Down Expand Up @@ -777,7 +793,7 @@ func (chain *Chain) cosiHandleFinalization(m *CosiAction) error {

cache, final := chain.StateCopy()
if !s.References.Equal(cache.References) {
err := chain.updateEmptyHeadRoundAndPersist(m, final, cache, s.References, s.Timestamp, false)
err := chain.updateEmptyHeadRoundAndPersist(final, cache, s.References, s.Timestamp, false)
if err != nil {
logger.Debugf("ERROR cosiHandleFinalization updateEmptyHeadRoundAndPersist failed %s %s %v\n",
m.PeerId, s.Hash, err)
Expand All @@ -789,7 +805,10 @@ func (chain *Chain) cosiHandleFinalization(m *CosiAction) error {
logger.Verbosef("ERROR cosiHandleFinalization ValidateSnapshot %s %v %v\n", m.PeerId, s, err)
return nil
}
chain.AddSnapshot(final, cache, s, signers)
err = chain.AddSnapshot(final, cache, s, signers)
if err != nil {
panic(err)
}
m.finalized = true
return chain.node.reloadConsensusState(s, tx)
}
Expand Down Expand Up @@ -909,7 +928,10 @@ func (node *Node) CosiQueueExternalCommitments(peerId crypto.Hash, commitments [
Action: CosiActionExternalCommitments,
Commitments: commitments,
}
node.chain.AppendCosiAction(m)
err := node.chain.AppendCosiAction(m)
if err != nil {
logger.Verbosef("CosiQueueExternalCommitments(%v) => %v\n", m, err)
}
return nil
}

Expand All @@ -935,7 +957,10 @@ func (node *Node) CosiQueueExternalAnnouncement(peerId crypto.Hash, s *common.Sn
Commitment: commitment,
SnapshotHash: s.Hash,
}
chain.AppendCosiAction(m)
err := chain.AppendCosiAction(m)
if err != nil {
logger.Verbosef("CosiQueueExternalAnnouncement(%v) => %v\n", m, err)
}
return nil
}

Expand All @@ -958,7 +983,10 @@ func (node *Node) CosiAggregateSelfCommitments(peerId crypto.Hash, snap crypto.H
Commitment: commitment,
WantTx: wantTx,
}
node.chain.AppendCosiAction(m)
err := node.chain.AppendCosiAction(m)
if err != nil {
logger.Verbosef("CosiAggregateSelfCommitments(%v) => %v\n", m, err)
}
return nil
}

Expand All @@ -977,7 +1005,10 @@ func (node *Node) CosiQueueExternalChallenge(peerId crypto.Hash, snap crypto.Has
Signature: cosi,
Transaction: ver,
}
chain.AppendCosiAction(m)
err := chain.AppendCosiAction(m)
if err != nil {
logger.Verbosef("CosiQueueExternalChallenge(%v) => %v\n", m, err)
}
return nil
}

Expand All @@ -1000,7 +1031,10 @@ func (node *Node) CosiQueueExternalFullChallenge(peerId crypto.Hash, s *common.S
Signature: cosi,
Transaction: ver,
}
chain.AppendCosiAction(m)
err := chain.AppendCosiAction(m)
if err != nil {
logger.Verbosef("CosiQueueExternalFullChallenge(%v) => %v\n", m, err)
}
return nil
}

Expand All @@ -1017,7 +1051,10 @@ func (node *Node) CosiAggregateSelfResponses(peerId crypto.Hash, snap crypto.Has
SnapshotHash: snap,
Response: response,
}
node.chain.AppendCosiAction(m)
err := node.chain.AppendCosiAction(m)
if err != nil {
logger.Verbosef("CosiAggregateSelfResponses(%v) => %v\n", m, err)
}
return nil
}

Expand All @@ -1037,9 +1074,9 @@ func (node *Node) VerifyAndQueueAppendSnapshotFinalization(peerId crypto.Hash, s
if err != nil {
logger.Verbosef("VerifyAndQueueAppendSnapshotFinalization(%s, %s) check tx error %s\n", peerId, s.Hash, err)
} else if tx == nil {
logger.Verbosef("VerifyAndQueueAppendSnapshotFinalization(%s, %s) SendTransactionRequestMessage %s\n",
peerId, s.Hash, s.SoleTransaction())
node.Peer.SendTransactionRequestMessage(peerId, s.SoleTransaction())
err = node.Peer.SendTransactionRequestMessage(peerId, s.SoleTransaction())
logger.Verbosef("VerifyAndQueueAppendSnapshotFinalization(%s, %s) SendTransactionRequestMessage %s %v\n",
peerId, s.Hash, s.SoleTransaction(), err)
} else if finalized == s.Hash.String() {
return nil
}
Expand All @@ -1059,5 +1096,5 @@ func (node *Node) VerifyAndQueueAppendSnapshotFinalization(peerId crypto.Hash, s
logger.Verbosef("VerifyAndQueueAppendSnapshotFinalization(%s, %s) chain error %s\n",
peerId, s.Hash, err)
}
return err
return nil
}
14 changes: 7 additions & 7 deletions kernel/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (node *Node) validateNodeRemoveSnapshot(s *common.Snapshot, tx *common.Vers
return nil
}

func (chain *Chain) checkNodeAcceptPossibility(timestamp uint64, s *common.Snapshot, finalized bool) error {
func (chain *Chain) checkNodeAcceptPossibility(timestamp uint64, finalized bool) error {
ci, epoch := chain.ConsensusInfo, chain.node.Epoch
if chain.State != nil {
return fmt.Errorf("invalid graph round %s %d", chain.ChainId, chain.State.CacheRound.Number)
Expand Down Expand Up @@ -263,8 +263,8 @@ func (chain *Chain) checkNodeAcceptPossibility(timestamp uint64, s *common.Snaps
return nil
}

func (chain *Chain) buildNodeAcceptTransaction(timestamp uint64, s *common.Snapshot, finalized bool) (*common.VersionedTransaction, error) {
err := chain.checkNodeAcceptPossibility(timestamp, s, finalized)
func (chain *Chain) buildNodeAcceptTransaction(timestamp uint64, finalized bool) (*common.VersionedTransaction, error) {
err := chain.checkNodeAcceptPossibility(timestamp, finalized)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -300,7 +300,7 @@ func (chain *Chain) buildNodeAcceptTransaction(timestamp uint64, s *common.Snaps

func (chain *Chain) tryToSendAcceptTransaction() error {
now := uint64(clock.Now().UnixNano())
ver, err := chain.buildNodeAcceptTransaction(now, nil, false)
ver, err := chain.buildNodeAcceptTransaction(now, false)
if err != nil {
return err
}
Expand All @@ -319,8 +319,8 @@ func (chain *Chain) tryToSendAcceptTransaction() error {
NodeId: chain.ChainId,
}
s.AddSoleTransaction(ver.PayloadHash())
chain.AppendSelfEmpty(s)
logger.Println("tryToSendAcceptTransaction", ver.PayloadHash(), hex.EncodeToString(ver.Marshal()))
err = chain.AppendSelfEmpty(s)
logger.Println("tryToSendAcceptTransaction", ver.PayloadHash(), hex.EncodeToString(ver.Marshal()), err)
return nil
}

Expand All @@ -334,7 +334,7 @@ func (node *Node) validateNodeAcceptSnapshot(s *common.Snapshot, tx *common.Vers
}

chain := node.getOrCreateChain(s.NodeId)
ver, err := chain.buildNodeAcceptTransaction(timestamp, s, finalized)
ver, err := chain.buildNodeAcceptTransaction(timestamp, finalized)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (chain *Chain) validateNewRound(cache *CacheRound, references *common.Round
return final, false, nil
}

func (chain *Chain) updateEmptyHeadRoundAndPersist(m *CosiAction, final *FinalRound, cache *CacheRound, references *common.RoundLink, timestamp uint64, strict bool) error {
func (chain *Chain) updateEmptyHeadRoundAndPersist(final *FinalRound, cache *CacheRound, references *common.RoundLink, timestamp uint64, strict bool) error {
if len(cache.Snapshots) != 0 {
return fmt.Errorf("malformated head round references not empty")
}
Expand Down
5 changes: 4 additions & 1 deletion kernel/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,10 @@ func (node *Node) sendGraphToConcensusNodesAndPeers() {
peers[p.IdForNetwork] = true
}
for id := range peers {
node.Peer.SendGraphMessage(id)
err := node.Peer.SendGraphMessage(id)
if err != nil {
logger.Debugf("SendGraphMessage(%s) => %v\n", id, err)
}
}
time.Sleep(time.Duration(config.SnapshotRoundGap / 2))
}
Expand Down
22 changes: 11 additions & 11 deletions kernel/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,10 @@ func (node *Node) QueueTransaction(tx *common.VersionedTransaction) (string, err
return tx.PayloadHash().String(), err
}

func (node *Node) loopCacheQueue() error {
func (node *Node) loopCacheQueue() {
defer close(node.cqc)

for {
if node.waitOrDone(time.Duration(config.SnapshotRoundGap)) {
return nil
}
for !node.waitOrDone(time.Duration(config.SnapshotRoundGap)) {
caches, finals, _ := node.QueueState()
if caches > 1000 || finals > 500 {
logger.Printf("LoopCacheQueue QueueState too big %d %d\n", caches, finals)
Expand All @@ -63,11 +60,16 @@ func (node *Node) loopCacheQueue() error {
if len(allNodes) <= 0 {
continue
}
leadingNodes, leadingFilter := node.filterLeadingNodes(allNodes)

txs, err := node.persistStore.CacheRetrieveTransactions(100)
if err != nil {
logger.Printf("LoopCacheQueue CacheRetrieveTransactions ERROR %s\n", err)
continue
}

var stale []crypto.Hash
filter := make(map[crypto.Hash]bool)
txs, err := node.persistStore.CacheRetrieveTransactions(100)
leadingNodes, leadingFilter := node.filterLeadingNodes(allNodes)
for _, tx := range txs {
hash := tx.PayloadHash()
if filter[hash] {
Expand Down Expand Up @@ -102,9 +104,6 @@ func (node *Node) loopCacheQueue() error {
}
}
}
if err != nil {
logger.Printf("LoopCacheQueue CacheRetrieveTransactions ERROR %s\n", err)
}
err = node.persistStore.CacheRemoveTransactions(stale)
if err != nil {
logger.Printf("LoopCacheQueue CacheRemoveTransactions ERROR %s\n", err)
Expand All @@ -122,7 +121,8 @@ func (node *Node) sendTransactionToNode(hash, nbor crypto.Hash) {
NodeId: node.IdForNetwork,
}
s.AddSoleTransaction(hash)
node.chain.AppendSelfEmpty(s)
err := node.chain.AppendSelfEmpty(s)
logger.Debugf("queue.AppendSelfEmpty(%v) => %v", s, err)
}
}

Expand Down
5 changes: 4 additions & 1 deletion p2p/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ func (me *Peer) syncHeadRoundToRemote(local, remote map[crypto.Hash]*SyncPoint,
for i := remoteFinal; i <= remoteFinal+config.SnapshotReferenceThreshold+2; i++ {
ss, _ := me.cacheReadSnapshotsForNodeRound(nodeId, i)
for _, s := range ss {
me.SendSnapshotFinalizationMessage(p.IdForNetwork, s.Snapshot)
err := me.SendSnapshotFinalizationMessage(p.IdForNetwork, s.Snapshot)
if err != nil {
logger.Verbosef("network.sync SendSnapshotFinalizationMessage %s %v\n", p.IdForNetwork, err)
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion rpc/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func testConsensus(t *testing.T, extrenalRelayers bool) {
require.Nil(err)
require.NotNil(store)
if i == 0 {
kernel.TestMockDiff(epoch.Sub(time.Now()))
kernel.TestMockDiff(time.Until(epoch))
}
node, err := kernel.SetupNode(custom, store, cache, gns)
require.Nil(err)
Expand Down

0 comments on commit d7caee3

Please sign in to comment.