Skip to content

Commit

Permalink
add function descriptions and comments to helpers in stream sync
Browse files Browse the repository at this point in the history
  • Loading branch information
GheisMohammadi committed Oct 24, 2024
1 parent 1af798a commit 6f68f89
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions api/service/stagedstreamsync/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,30 @@ import (
sttypes "github.com/harmony-one/harmony/p2p/stream/types"
)

// marshalData serializes a uint64 block number into a big-endian byte slice.
func marshalData(blockNumber uint64) []byte {
return encodeBigEndian(blockNumber)
}

// unmarshalData deserializes a byte slice into a uint64 block number.
func unmarshalData(data []byte) (uint64, error) {
if len(data) == 0 {
return 0, nil
}
if len(data) < 8 {
return 0, fmt.Errorf("value must be at least 8 bytes, got %d", len(data))
return 0, fmt.Errorf("invalid data length: expected at least 8 bytes, got %d", len(data))
}
return binary.BigEndian.Uint64(data[:8]), nil
}

// encodeBigEndian encodes a uint64 value into an 8-byte big-endian slice.
func encodeBigEndian(n uint64) []byte {
var v [8]byte
binary.BigEndian.PutUint64(v[:], n)
return v[:]
}

// divideCeil performs ceiling division of two integers.
func divideCeil(x, y int) int {
fVal := float64(x) / float64(y)
return int(math.Ceil(fVal))
Expand Down Expand Up @@ -58,6 +62,7 @@ func computeBlockNumberByMaxVote(votes map[sttypes.StreamID]uint64) uint64 {
return res
}

// checkGetBlockByHashesResult checks the integrity of blocks received by their hashes.
func checkGetBlockByHashesResult(blocks []*types.Block, hashes []common.Hash) error {
if len(blocks) != len(hashes) {
return ErrUnexpectedNumberOfBlockHashes
Expand All @@ -67,34 +72,36 @@ func checkGetBlockByHashesResult(blocks []*types.Block, hashes []common.Hash) er
return ErrNilBlock
}
if block.Hash() != hashes[i] {
return fmt.Errorf("unexpected block hash: %x / %x", block.Hash(), hashes[i])
return fmt.Errorf("unexpected block hash: got %x, expected %x", block.Hash(), hashes[i])
}
}
return nil
}

// getBlockByMaxVote returns the block that has the most votes based on their hashes.
func getBlockByMaxVote(blocks []*types.Block) (*types.Block, error) {
hashesVote := make(map[common.Hash]int)
maxVote := int(-1)
maxVotedBlockIndex := int(0)
var maxVotedBlock *types.Block
maxVote := -1

for i, block := range blocks {
for _, block := range blocks {
if block == nil {
continue
}
if _, exist := hashesVote[block.Header().Hash()]; !exist {
hashesVote[block.Header().Hash()] = 0
hash := block.Header().Hash()
if _, exist := hashesVote[hash]; !exist {
hashesVote[hash] = 0
}
hashesVote[block.Header().Hash()]++
if hashesVote[block.Header().Hash()] > maxVote {
maxVote = hashesVote[block.Header().Hash()]
maxVotedBlockIndex = i
hashesVote[hash]++
if hashesVote[hash] > maxVote {
maxVote = hashesVote[hash]
maxVotedBlock = block
}
}
if maxVote < 0 {
return nil, ErrInvalidBlockBytes
}
return blocks[maxVotedBlockIndex], nil
return maxVotedBlock, nil
}

// countHashMaxVote counts the votes for each hash in the map, respecting the whitelist.
Expand Down Expand Up @@ -138,6 +145,7 @@ func countHashMaxVote(m map[sttypes.StreamID]common.Hash, whitelist map[sttypes.
return res, nextWl
}

// ByteCount returns a human-readable string representation of a byte count.
func ByteCount(b uint64) string {
const unit = 1024
if b < unit {
Expand All @@ -148,6 +156,5 @@ func ByteCount(b uint64) string {
div *= unit
exp++
}
return fmt.Sprintf("%.1f%cB",
float64(b)/float64(div), "KMGTPE"[exp])
return fmt.Sprintf("%.1f%cB", float64(b)/float64(div), "KMGTPE"[exp])
}

0 comments on commit 6f68f89

Please sign in to comment.