my-cloud / ruthenium

Golang implementation of the Ruthenium protocol
The Unlicense
11 stars 1 forks source link

Blocks requests are not parallelized #131

Closed JeremyPansier closed 1 year ago

JeremyPansier commented 1 year ago

In blockchain.go, neighbor.GetBlocks() calls must be done in goroutines

func (blockchain *Blockchain) Verify() {
    // Select valid blocks
    neighbors := blockchain.synchronizer.Neighbors()
    blockchain.mutex.RLock()
    blockResponsesByTarget := make(map[string][]*neighborhood.BlockResponse)
    blocksByTarget := make(map[string][]*Block)
    var selectedTargets []string
    for _, neighbor := range neighbors {
        neighborBlocks, err := neighbor.GetBlocks()
        target := neighbor.Target()
        blockResponsesByTarget[target] = neighborBlocks
        if err == nil {
            var validBlocks []*Block
            validBlocks, err = blockchain.getValidBlocks(neighborBlocks)
            if err != nil || validBlocks == nil {
                blockchain.logger.Debug(fmt.Errorf("failed to verify blocks for neighbor %s: %w", target, err).Error())
            } else {
                blocksByTarget[target] = validBlocks
                selectedTargets = append(selectedTargets, target)
            }
        }
    }