Merge pull request #2508 from filecoin-project/feat/epoch-boundary-mitigation

mitigate epoch boundary attacks by randomizing cutoff
This commit is contained in:
Łukasz Magiera 2020-07-21 23:11:37 +02:00 committed by GitHub
commit 30c1608380
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,8 @@ package miner
import ( import (
"bytes" "bytes"
"context" "context"
"crypto/rand"
"encoding/binary"
"fmt" "fmt"
big2 "math/big" big2 "math/big"
"sort" "sort"
@ -33,6 +35,14 @@ var log = logging.Logger("miner")
// returns a callback reporting whether we mined a blocks in this round // returns a callback reporting whether we mined a blocks in this round
type waitFunc func(ctx context.Context, baseTime uint64) (func(bool, error), error) type waitFunc func(ctx context.Context, baseTime uint64) (func(bool, error), error)
func randTimeOffset(width time.Duration) time.Duration {
buf := make([]byte, 8)
rand.Reader.Read(buf)
val := time.Duration(binary.BigEndian.Uint64(buf) % uint64(width))
return val - (width / 2)
}
func NewMiner(api api.FullNode, epp gen.WinningPoStProver, addr address.Address) *Miner { func NewMiner(api api.FullNode, epp gen.WinningPoStProver, addr address.Address) *Miner {
arc, err := lru.NewARC(10000) arc, err := lru.NewARC(10000)
if err != nil { if err != nil {
@ -46,7 +56,11 @@ func NewMiner(api api.FullNode, epp gen.WinningPoStProver, addr address.Address)
waitFunc: func(ctx context.Context, baseTime uint64) (func(bool, error), error) { waitFunc: func(ctx context.Context, baseTime uint64) (func(bool, error), error) {
// Wait around for half the block time in case other parents come in // Wait around for half the block time in case other parents come in
deadline := baseTime + build.PropagationDelaySecs deadline := baseTime + build.PropagationDelaySecs
build.Clock.Sleep(build.Clock.Until(time.Unix(int64(deadline), 0))) baseT := time.Unix(int64(deadline), 0)
baseT = baseT.Add(randTimeOffset(time.Second))
build.Clock.Sleep(build.Clock.Until(baseT))
return func(bool, error) {}, nil return func(bool, error) {}, nil
}, },