2019-09-30 22:38:07 +00:00
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2019-10-01 21:34:53 +00:00
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-02-23 07:49:15 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
2019-10-14 11:53:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-09-30 22:38:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTsCache(t *testing.T) {
|
2020-09-07 12:43:06 +00:00
|
|
|
tsc := newTSCache(50, &tsCacheAPIFailOnStorageCall{t: t})
|
2019-09-30 22:38:07 +00:00
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
h := abi.ChainEpoch(75)
|
2019-09-30 22:38:07 +00:00
|
|
|
|
2019-10-14 11:53:20 +00:00
|
|
|
a, _ := address.NewFromString("t00")
|
|
|
|
|
2019-09-30 22:38:07 +00:00
|
|
|
add := func() {
|
|
|
|
ts, err := types.NewTipSet([]*types.BlockHeader{{
|
2019-10-14 11:53:20 +00:00
|
|
|
Miner: a,
|
2019-10-01 21:34:53 +00:00
|
|
|
Height: h,
|
|
|
|
ParentStateRoot: dummyCid,
|
|
|
|
Messages: dummyCid,
|
|
|
|
ParentMessageReceipts: dummyCid,
|
2020-02-26 09:05:22 +00:00
|
|
|
BlockSig: &crypto.Signature{Type: crypto.SigTypeBLS},
|
2020-03-21 22:25:00 +00:00
|
|
|
BLSAggregate: &crypto.Signature{Type: crypto.SigTypeBLS},
|
2019-09-30 22:38:07 +00:00
|
|
|
}})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := tsc.add(ts); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
h++
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 9000; i++ {
|
|
|
|
if i%90 > 60 {
|
2020-09-07 12:43:06 +00:00
|
|
|
best, err := tsc.best()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err, "; i:", i)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := tsc.revert(best); err != nil {
|
2019-09-30 22:38:07 +00:00
|
|
|
t.Fatal(err, "; i:", i)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h--
|
|
|
|
} else {
|
|
|
|
add()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-10-04 22:43:04 +00:00
|
|
|
|
2020-09-07 12:43:06 +00:00
|
|
|
type tsCacheAPIFailOnStorageCall struct {
|
|
|
|
t *testing.T
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *tsCacheAPIFailOnStorageCall) ChainGetTipSetByHeight(ctx context.Context, epoch abi.ChainEpoch, key types.TipSetKey) (*types.TipSet, error) {
|
|
|
|
tc.t.Fatal("storage call")
|
|
|
|
return &types.TipSet{}, nil
|
|
|
|
}
|
|
|
|
func (tc *tsCacheAPIFailOnStorageCall) ChainHead(ctx context.Context) (*types.TipSet, error) {
|
|
|
|
tc.t.Fatal("storage call")
|
|
|
|
return &types.TipSet{}, nil
|
|
|
|
}
|
|
|
|
|
2019-10-04 22:43:04 +00:00
|
|
|
func TestTsCacheNulls(t *testing.T) {
|
2020-09-07 12:43:06 +00:00
|
|
|
tsc := newTSCache(50, &tsCacheAPIFailOnStorageCall{t: t})
|
2019-10-04 22:43:04 +00:00
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
h := abi.ChainEpoch(75)
|
2019-10-04 22:43:04 +00:00
|
|
|
|
2019-10-14 11:53:20 +00:00
|
|
|
a, _ := address.NewFromString("t00")
|
2019-10-04 22:43:04 +00:00
|
|
|
add := func() {
|
|
|
|
ts, err := types.NewTipSet([]*types.BlockHeader{{
|
2019-10-14 11:53:20 +00:00
|
|
|
Miner: a,
|
2019-10-04 22:43:04 +00:00
|
|
|
Height: h,
|
|
|
|
ParentStateRoot: dummyCid,
|
|
|
|
Messages: dummyCid,
|
|
|
|
ParentMessageReceipts: dummyCid,
|
2020-02-26 09:05:22 +00:00
|
|
|
BlockSig: &crypto.Signature{Type: crypto.SigTypeBLS},
|
2020-03-21 22:25:00 +00:00
|
|
|
BLSAggregate: &crypto.Signature{Type: crypto.SigTypeBLS},
|
2019-10-04 22:43:04 +00:00
|
|
|
}})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := tsc.add(ts); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
h++
|
|
|
|
}
|
|
|
|
|
|
|
|
add()
|
|
|
|
add()
|
|
|
|
add()
|
|
|
|
h += 5
|
|
|
|
|
|
|
|
add()
|
|
|
|
add()
|
|
|
|
|
2020-09-07 12:43:06 +00:00
|
|
|
best, err := tsc.best()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, h-1, best.Height())
|
2019-10-04 22:43:04 +00:00
|
|
|
|
|
|
|
ts, err := tsc.get(h - 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, h-1, ts.Height())
|
|
|
|
|
|
|
|
ts, err = tsc.get(h - 2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, h-2, ts.Height())
|
|
|
|
|
|
|
|
ts, err = tsc.get(h - 3)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, ts)
|
|
|
|
|
|
|
|
ts, err = tsc.get(h - 8)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, h-8, ts.Height())
|
|
|
|
|
2020-09-07 12:43:06 +00:00
|
|
|
best, err = tsc.best()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, tsc.revert(best))
|
|
|
|
|
|
|
|
best, err = tsc.best()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, tsc.revert(best))
|
|
|
|
|
|
|
|
best, err = tsc.best()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, h-8, best.Height())
|
2019-10-04 23:26:36 +00:00
|
|
|
|
|
|
|
h += 50
|
|
|
|
add()
|
|
|
|
|
|
|
|
ts, err = tsc.get(h - 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, h-1, ts.Height())
|
2019-10-04 22:43:04 +00:00
|
|
|
}
|
2020-09-07 12:43:06 +00:00
|
|
|
|
|
|
|
type tsCacheAPIStorageCallCounter struct {
|
|
|
|
t *testing.T
|
|
|
|
chainGetTipSetByHeight int
|
|
|
|
chainHead int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *tsCacheAPIStorageCallCounter) ChainGetTipSetByHeight(ctx context.Context, epoch abi.ChainEpoch, key types.TipSetKey) (*types.TipSet, error) {
|
|
|
|
tc.chainGetTipSetByHeight++
|
|
|
|
return &types.TipSet{}, nil
|
|
|
|
}
|
|
|
|
func (tc *tsCacheAPIStorageCallCounter) ChainHead(ctx context.Context) (*types.TipSet, error) {
|
|
|
|
tc.chainHead++
|
|
|
|
return &types.TipSet{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTsCacheEmpty(t *testing.T) {
|
|
|
|
// Calling best on an empty cache should just call out to the chain API
|
|
|
|
callCounter := &tsCacheAPIStorageCallCounter{t: t}
|
|
|
|
tsc := newTSCache(50, callCounter)
|
|
|
|
_, err := tsc.best()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, callCounter.chainHead)
|
|
|
|
}
|