-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add clock package and block validation
- Loading branch information
Showing
6 changed files
with
245 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package clock | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
// TODO this is the bare min, not wasting time to get killed in review. | ||
|
||
// Clock is a thing | ||
type Clock interface { | ||
Now() time.Time | ||
} | ||
|
||
// BlockClock is a thing | ||
type BlockClock struct{} | ||
|
||
// NewBlockClock is a thing | ||
func NewBlockClock() *BlockClock { | ||
return &BlockClock{} | ||
} | ||
|
||
// Now is a thing | ||
func (bc *BlockClock) Now() time.Time { | ||
return time.Now() | ||
} | ||
|
||
// MockClock is a thing | ||
type MockClock struct { | ||
nowMu sync.Mutex | ||
now time.Time | ||
} | ||
|
||
// NewMockClock is a thing | ||
func NewMockClock(n time.Time) *MockClock { | ||
return &MockClock{ | ||
now: n, | ||
} | ||
} | ||
|
||
// Now is a thing | ||
func (mc *MockClock) Now() time.Time { | ||
mc.nowMu.Lock() | ||
defer mc.nowMu.Unlock() | ||
return mc.now | ||
} | ||
|
||
// Set is a thing | ||
func (mc *MockClock) Set(t time.Time) { | ||
mc.nowMu.Lock() | ||
defer mc.nowMu.Unlock() | ||
mc.now = t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package consensus_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ipfs/go-cid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/filecoin-project/go-filecoin/address" | ||
"github.com/filecoin-project/go-filecoin/clock" | ||
"github.com/filecoin-project/go-filecoin/consensus" | ||
tf "github.com/filecoin-project/go-filecoin/testhelpers/testflags" | ||
"github.com/filecoin-project/go-filecoin/types" | ||
) | ||
|
||
// to block time converts a time to block timestamp | ||
func tbt(t time.Time) types.Uint64 { | ||
return types.Uint64(t.Unix()) | ||
} | ||
|
||
func TestBlockValidSemantic(t *testing.T) { | ||
tf.UnitTest(t) | ||
|
||
blockTime := consensus.DefaultBlockTime | ||
ts := time.Now() | ||
mclock := clock.NewMockClock(ts) | ||
ctx := context.Background() | ||
|
||
validator := consensus.NewDefaultBlockValidator(blockTime, mclock) | ||
//panic(fmt.Sprintf("ts: %d, addedTo: %d", tbt(ts), tbt(ts.Add(blockTime)))) | ||
|
||
t.Run("reject block with same height as parents", func(t *testing.T) { | ||
c := &types.Block{Height: 1, Timestamp: tbt(ts)} | ||
p := &types.Block{Height: 1, Timestamp: tbt(ts)} | ||
parents, err := types.NewTipSet(p) | ||
require.NoError(t, err) | ||
|
||
err = validator.ValidateSemantic(ctx, c, &parents) | ||
assert.Equal(t, consensus.ErrInvalidHeight, err) | ||
}) | ||
|
||
t.Run("reject block mined too soon after parent", func(t *testing.T) { | ||
c := &types.Block{Height: 2, Timestamp: tbt(ts)} | ||
p := &types.Block{Height: 1, Timestamp: tbt(ts)} | ||
parents, err := types.NewTipSet(p) | ||
require.NoError(t, err) | ||
|
||
err = validator.ValidateSemantic(ctx, c, &parents) | ||
assert.Equal(t, consensus.ErrTooSoon, err) | ||
}) | ||
|
||
t.Run("reject block mined too soon after parent with one null block", func(t *testing.T) { | ||
c := &types.Block{Height: 3, Timestamp: tbt(ts)} | ||
p := &types.Block{Height: 1, Timestamp: tbt(ts)} | ||
parents, err := types.NewTipSet(p) | ||
require.NoError(t, err) | ||
|
||
err = validator.ValidateSemantic(ctx, c, &parents) | ||
assert.Equal(t, consensus.ErrTooSoon, err) | ||
}) | ||
|
||
t.Run("accept block mined one block time after parent", func(t *testing.T) { | ||
c := &types.Block{Height: 2, Timestamp: tbt(ts.Add(blockTime))} | ||
p := &types.Block{Height: 1, Timestamp: tbt(ts)} | ||
parents, err := types.NewTipSet(p) | ||
require.NoError(t, err) | ||
|
||
err = validator.ValidateSemantic(ctx, c, &parents) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("accept block mined one block time after parent with one null block", func(t *testing.T) { | ||
c := &types.Block{Height: 3, Timestamp: tbt(ts.Add(2 * blockTime))} | ||
p := &types.Block{Height: 1, Timestamp: tbt(ts)} | ||
parents, err := types.NewTipSet(p) | ||
require.NoError(t, err) | ||
|
||
err = validator.ValidateSemantic(ctx, c, &parents) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
} | ||
|
||
func TestBlockValidSyntax(t *testing.T) { | ||
tf.UnitTest(t) | ||
|
||
blockTime := consensus.DefaultBlockTime | ||
|
||
loc, _ := time.LoadLocation("America/New_York") | ||
ts := time.Date(2019, time.April, 1, 0, 0, 0, 0, loc) | ||
mclock := clock.NewMockClock(ts) | ||
|
||
ctx := context.Background() | ||
|
||
validator := consensus.NewDefaultBlockValidator(blockTime, mclock) | ||
|
||
t.Run("reject block generated in future", func(t *testing.T) { | ||
blk := &types.Block{ | ||
Timestamp: types.Uint64(ts.Add(time.Second * 1).Unix()), | ||
} | ||
assert.Error(t, validator.ValidateSyntax(ctx, blk)) | ||
}) | ||
|
||
t.Run("reject block with undef StateRoot", func(t *testing.T) { | ||
blk := &types.Block{ | ||
StateRoot: cid.Undef, | ||
} | ||
assert.Error(t, validator.ValidateSyntax(ctx, blk)) | ||
}) | ||
|
||
t.Run("reject block with undef miner address", func(t *testing.T) { | ||
blk := &types.Block{ | ||
Miner: address.Undef, | ||
} | ||
assert.Error(t, validator.ValidateSyntax(ctx, blk)) | ||
}) | ||
|
||
t.Run("reject block with empty ticket", func(t *testing.T) { | ||
blk := &types.Block{ | ||
Ticket: []byte{}, | ||
} | ||
assert.Error(t, validator.ValidateSyntax(ctx, blk)) | ||
}) | ||
|
||
// Impossible due to implementation | ||
/* | ||
t.Run("reject block with negative parent weight", func(t *testing.T) { | ||
blk := &types.Block{ | ||
ParentWeight: -1, | ||
} | ||
assert.Error(t, validator.ValidateSyntax(ctx, blk)) | ||
}) | ||
t.Run("reject block with negative height", func(t *testing.T) { | ||
blk := &types.Block{ | ||
Height: -1, | ||
} | ||
assert.Error(t, validator.ValidateSyntax(ctx, blk)) | ||
}) | ||
*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters