lotus/chain/sub/bcast/consistent_test.go

224 lines
5.5 KiB
Go
Raw Normal View History

2022-09-12 17:03:06 +00:00
package bcast_test
import (
2022-09-13 10:33:28 +00:00
"context"
2022-09-12 17:03:06 +00:00
"crypto/rand"
2022-09-13 10:33:28 +00:00
"fmt"
mrand "math/rand"
"strconv"
"sync"
2022-09-12 17:03:06 +00:00
"testing"
2022-09-13 10:33:28 +00:00
"time"
2022-09-12 17:03:06 +00:00
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"
2022-09-13 10:33:28 +00:00
"github.com/filecoin-project/go-address"
2022-09-12 17:03:06 +00:00
"github.com/filecoin-project/go-state-types/abi"
2022-09-13 10:33:28 +00:00
"github.com/filecoin-project/lotus/chain/sub/bcast"
2022-09-12 17:03:06 +00:00
"github.com/filecoin-project/lotus/chain/types"
)
2022-09-16 07:22:37 +00:00
const TEST_DELAY = 1 * time.Second
2022-09-13 10:33:28 +00:00
2022-09-12 17:03:06 +00:00
func TestSimpleDelivery(t *testing.T) {
2022-09-13 10:33:28 +00:00
cb := bcast.NewConsistentBCast(TEST_DELAY)
// Check that we wait for delivery.
start := time.Now()
testSimpleDelivery(t, cb, 100, 5)
since := time.Since(start)
2022-09-16 07:22:37 +00:00
require.GreaterOrEqual(t, since, TEST_DELAY)
2022-09-12 17:03:06 +00:00
}
2022-09-13 10:33:28 +00:00
func testSimpleDelivery(t *testing.T, cb *bcast.ConsistentBCast, epoch abi.ChainEpoch, numBlocks int) {
ctx := context.Background()
wg := new(sync.WaitGroup)
errs := make([]error, 0)
wg.Add(numBlocks)
for i := 0; i < numBlocks; i++ {
go func(i int) {
defer wg.Done()
2022-09-13 10:33:28 +00:00
// Add a random delay in block reception
r := mrand.Intn(200)
time.Sleep(time.Duration(r) * time.Millisecond)
blk := newBlock(t, epoch, randomProof(t), []byte("test"+strconv.Itoa(i)))
cb.RcvBlock(ctx, blk)
err := cb.WaitForDelivery(blk.Header)
if err != nil {
errs = append(errs, err)
}
}(i)
}
wg.Wait()
for _, v := range errs {
t.Fatalf("error in delivery: %s", v)
}
}
func TestSeveralEpochs(t *testing.T) {
cb := bcast.NewConsistentBCast(TEST_DELAY)
2023-03-16 16:17:59 +00:00
numEpochs := 6
2022-09-13 10:33:28 +00:00
wg := new(sync.WaitGroup)
wg.Add(numEpochs)
for i := 0; i < numEpochs; i++ {
go func(i int) {
defer wg.Done()
2022-09-13 10:33:28 +00:00
// Add a random delay between epochs
r := mrand.Intn(500)
2022-09-16 07:22:37 +00:00
time.Sleep(time.Duration(i)*TEST_DELAY + time.Duration(r)*time.Millisecond)
2022-09-13 10:33:28 +00:00
rNumBlocks := mrand.Intn(5)
flip, err := flipCoin(0.7)
require.NoError(t, err)
t.Logf("Running epoch %d with %d with equivocation=%v", i, rNumBlocks, !flip)
if flip {
testSimpleDelivery(t, cb, abi.ChainEpoch(i), rNumBlocks)
} else {
testEquivocation(t, cb, abi.ChainEpoch(i), rNumBlocks)
}
cb.GarbageCollect(abi.ChainEpoch(i))
2022-09-13 10:33:28 +00:00
}(i)
}
wg.Wait()
2023-03-16 16:17:59 +00:00
require.Equal(t, cb.Len(), numEpochs)
2022-09-13 10:33:28 +00:00
}
// bias is expected to be 0-1
func flipCoin(bias float32) (bool, error) {
if bias > 1 || bias < 0 {
return false, fmt.Errorf("wrong bias. expected (0,1)")
}
r := mrand.Intn(100)
return r < int(bias*100), nil
}
func testEquivocation(t *testing.T, cb *bcast.ConsistentBCast, epoch abi.ChainEpoch, numBlocks int) {
ctx := context.Background()
wg := new(sync.WaitGroup)
errs := make([]error, 0)
wg.Add(numBlocks + 1)
for i := 0; i < numBlocks; i++ {
proof := randomProof(t)
// Valid blocks
go func(i int, proof []byte) {
defer wg.Done()
2022-09-13 10:33:28 +00:00
r := mrand.Intn(200)
time.Sleep(time.Duration(r) * time.Millisecond)
blk := newBlock(t, epoch, proof, []byte("valid"+strconv.Itoa(i)))
2022-09-13 10:33:28 +00:00
cb.RcvBlock(ctx, blk)
err := cb.WaitForDelivery(blk.Header)
if err != nil {
errs = append(errs, err)
}
}(i, proof)
// Equivocation for the last block
if i == numBlocks-1 {
// Attempting equivocation
go func(i int, proof []byte) {
defer wg.Done()
2022-09-13 10:33:28 +00:00
// Use the same proof and the same epoch
blk := newBlock(t, epoch, proof, []byte("invalid"+strconv.Itoa(i)))
2022-09-13 10:33:28 +00:00
cb.RcvBlock(ctx, blk)
err := cb.WaitForDelivery(blk.Header)
// Equivocation detected
require.Error(t, err)
}(i, proof)
}
}
wg.Wait()
// The equivocated block arrived too late, so
// we delivered all the valid blocks.
require.Len(t, errs, 1)
}
func TestEquivocation(t *testing.T) {
cb := bcast.NewConsistentBCast(TEST_DELAY)
testEquivocation(t, cb, 100, 5)
}
func TestFailedEquivocation(t *testing.T) {
cb := bcast.NewConsistentBCast(TEST_DELAY)
ctx := context.Background()
numBlocks := 5
wg := new(sync.WaitGroup)
errs := make([]error, 0)
wg.Add(numBlocks + 1)
for i := 0; i < numBlocks; i++ {
proof := randomProof(t)
// Valid blocks
go func(i int, proof []byte) {
defer wg.Done()
2022-09-13 10:33:28 +00:00
r := mrand.Intn(200)
time.Sleep(time.Duration(r) * time.Millisecond)
blk := newBlock(t, 100, proof, []byte("valid"+strconv.Itoa(i)))
cb.RcvBlock(ctx, blk)
err := cb.WaitForDelivery(blk.Header)
if err != nil {
errs = append(errs, err)
}
}(i, proof)
// Equivocation for the last block
if i == numBlocks-1 {
// Attempting equivocation
go func(i int, proof []byte) {
defer wg.Done()
2022-09-13 10:33:28 +00:00
// The equivocated block arrives late
2022-09-16 07:22:37 +00:00
time.Sleep(2 * TEST_DELAY)
2022-09-13 10:33:28 +00:00
// Use the same proof and the same epoch
blk := newBlock(t, 100, proof, []byte("invalid"+strconv.Itoa(i)))
cb.RcvBlock(ctx, blk)
err := cb.WaitForDelivery(blk.Header)
// Equivocation detected
require.Error(t, err)
}(i, proof)
}
}
wg.Wait()
// The equivocated block arrived too late, so
// we delivered all the valid blocks.
require.Len(t, errs, 0)
}
func randomProof(t *testing.T) []byte {
2022-09-12 17:03:06 +00:00
proof := make([]byte, 10)
_, err := rand.Read(proof)
2022-09-13 10:33:28 +00:00
if err != nil {
t.Fatal(err)
}
return proof
}
func newBlock(t *testing.T, epoch abi.ChainEpoch, proof []byte, mCidSeed []byte) *types.BlockMsg {
h, err := multihash.Sum(mCidSeed, multihash.SHA2_256, -1)
if err != nil {
t.Fatal(err)
}
testCid := cid.NewCidV0(h)
addr, err := address.NewIDAddress(10)
if err != nil {
2022-09-12 17:03:06 +00:00
t.Fatal(err)
}
bh := &types.BlockHeader{
2022-09-13 10:33:28 +00:00
Miner: addr,
ParentStateRoot: testCid,
ParentMessageReceipts: testCid,
2022-09-12 17:03:06 +00:00
Ticket: &types.Ticket{
2022-09-13 10:33:28 +00:00
VRFProof: proof,
2022-09-12 17:03:06 +00:00
},
2022-09-13 10:33:28 +00:00
Height: epoch,
Messages: testCid,
2022-09-12 17:03:06 +00:00
}
return &types.BlockMsg{
Header: bh,
}
}