7 type Cache[Peer, BlockKey, Hash comparable] struct {
11 blocks map[BlockKey]map[Peer]Hash
14 type Block[Key any] struct {
19 func (me *Cache[Peer, BlockKey, Hash]) Init() {
20 me.blocks = make(map[BlockKey]map[Peer]Hash)
23 func (me *Cache[Peer, BlockKey, Hash]) RecordBlock(peer Peer, key BlockKey, data []byte) {
26 defer me.lock.Unlock()
27 peers := me.blocks[key]
29 peers = make(map[Peer]Hash)
30 me.blocks[key] = peers
35 func (me *Cache[Peer, BlockKey, Hash]) CheckBlock(key BlockKey, data []byte) (bad []Peer) {
36 correct := me.Hash(data)
38 defer me.lock.RUnlock()
39 for peer, hash := range me.blocks[key] {
41 bad = append(bad, peer)
47 func (me *Cache[Peer, BlockKey, Hash]) ForgetBlock(key BlockKey) {
49 defer me.lock.Unlock()
50 delete(me.blocks, key)