add a simple semaphore to batch verify seals

This commit is contained in:
Jeromy 2020-06-05 10:47:49 -07:00
parent 60101fd97b
commit 51f1712564

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
goruntime "runtime"
"sync" "sync"
"github.com/filecoin-project/go-address" "github.com/filecoin-project/go-address"
@ -243,6 +244,8 @@ func (ss *syscallShim) VerifySignature(sig crypto.Signature, addr address.Addres
func (ss *syscallShim) BatchVerifySeals(inp map[address.Address][]abi.SealVerifyInfo) (map[address.Address][]bool, error) { func (ss *syscallShim) BatchVerifySeals(inp map[address.Address][]abi.SealVerifyInfo) (map[address.Address][]bool, error) {
out := make(map[address.Address][]bool) out := make(map[address.Address][]bool)
sema := make(chan struct{}, goruntime.NumCPU())
var wg sync.WaitGroup var wg sync.WaitGroup
for addr, seals := range inp { for addr, seals := range inp {
results := make([]bool, len(seals)) results := make([]bool, len(seals))
@ -252,12 +255,16 @@ func (ss *syscallShim) BatchVerifySeals(inp map[address.Address][]abi.SealVerify
wg.Add(1) wg.Add(1)
go func(ma address.Address, ix int, svi abi.SealVerifyInfo, res []bool) { go func(ma address.Address, ix int, svi abi.SealVerifyInfo, res []bool) {
defer wg.Done() defer wg.Done()
sema <- struct{}{}
if err := ss.VerifySeal(svi); err != nil { if err := ss.VerifySeal(svi); err != nil {
log.Warnw("seal verify in batch failed", "miner", ma, "index", ix, "err", err) log.Warnw("seal verify in batch failed", "miner", ma, "index", ix, "err", err)
res[ix] = false res[ix] = false
} else { } else {
res[ix] = true res[ix] = true
} }
<-sema
}(addr, i, s, results) }(addr, i, s, results)
} }
} }