Merge pull request #1258 from LeoHChen/implement_is_epoch_block

Implement is epoch block
pull/1264/head
Leo Chen 5 years ago committed by GitHub
commit 98cb315a9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      core/blockchain.go
  2. 67
      core/core_test.go
  3. 72
      internal/configs/sharding/shardingconfig_test.go

@ -228,9 +228,13 @@ func (bc *BlockChain) ValidateNewBlock(block *types.Block) error {
}
// IsEpochBlock returns whether this block is the first block of an epoch.
// TODO: lc this is not used
// by checking if the previous block is the last block of the previous epoch
func IsEpochBlock(block *types.Block) bool {
return block.NumberU64()%ShardingSchedule.BlocksPerEpoch() == 0
if block.NumberU64() == 0 {
// genesis block is the first epoch block
return true
}
return ShardingSchedule.IsLastBlock(block.NumberU64() - 1)
}
// IsEpochLastBlock returns whether this block is the last block of an epoch.

@ -0,0 +1,67 @@
package core
import (
"math/big"
"testing"
"github.com/harmony-one/harmony/core/types"
shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding"
)
func TestIsEpochBlock(t *testing.T) {
block1 := types.NewBlock(&types.Header{Number: big.NewInt(10)}, nil, nil)
block2 := types.NewBlock(&types.Header{Number: big.NewInt(0)}, nil, nil)
block3 := types.NewBlock(&types.Header{Number: big.NewInt(327680)}, nil, nil)
block4 := types.NewBlock(&types.Header{Number: big.NewInt(77)}, nil, nil)
block5 := types.NewBlock(&types.Header{Number: big.NewInt(78)}, nil, nil)
block6 := types.NewBlock(&types.Header{Number: big.NewInt(188)}, nil, nil)
block7 := types.NewBlock(&types.Header{Number: big.NewInt(189)}, nil, nil)
tests := []struct {
schedule shardingconfig.Schedule
block *types.Block
expected bool
}{
{
shardingconfig.MainnetSchedule,
block1,
false,
},
{
shardingconfig.MainnetSchedule,
block2,
true,
},
{
shardingconfig.MainnetSchedule,
block3,
true,
},
{
shardingconfig.TestnetSchedule,
block4,
false,
},
{
shardingconfig.TestnetSchedule,
block5,
true,
},
{
shardingconfig.TestnetSchedule,
block6,
false,
},
{
shardingconfig.TestnetSchedule,
block7,
true,
},
}
for _, test := range tests {
ShardingSchedule = test.schedule
r := IsEpochBlock(test.block)
if r != test.expected {
t.Errorf("expected: %v, got: %v\n", test.expected, r)
}
}
}

@ -0,0 +1,72 @@
package shardingconfig
import (
"math/big"
"testing"
)
func TestMainnetInstanceForEpoch(t *testing.T) {
tests := []struct {
epoch *big.Int
instance Instance
}{
{
big.NewInt(0),
mainnetV0,
},
{
big.NewInt(1),
mainnetV1,
},
{
big.NewInt(2),
mainnetV1,
},
}
for _, test := range tests {
in := MainnetSchedule.InstanceForEpoch(test.epoch)
if in.NumShards() != test.instance.NumShards() || in.NumNodesPerShard() != test.instance.NumNodesPerShard() {
t.Errorf("can't get the right instane for epoch: %v\n", test.epoch)
}
}
}
func TestCalcEpochNumber(t *testing.T) {
tests := []struct {
block uint64
epoch *big.Int
}{
{
0,
big.NewInt(0),
},
{
1,
big.NewInt(0),
},
{
327679,
big.NewInt(0),
},
{
327680,
big.NewInt(1),
},
{
344064,
big.NewInt(2),
},
{
344063,
big.NewInt(1),
},
}
for _, test := range tests {
ep := MainnetSchedule.CalcEpochNumber(test.block)
if ep.Cmp(test.epoch) != 0 {
t.Errorf("CalcEpochNumber error: got %v, expect %v\n", ep, test.epoch)
}
}
}
Loading…
Cancel
Save