Feature 4160 beacon crosslink signal. (#4169)
* Sending crosslinks from beacon to non-beacon chains. * Sending crosslinks from beacon to non-beacon chains. * Broadcast. * Fix comment. * Crosslink signal. * Crosslink signal. * Add comment to run tests. * Fix comments. * Fix comments. * Fix review comments. * Fix comments. * Fix comments. Co-authored-by: Konstantin <k.potapov@softpro.com>pull/4193/head
parent
9ac7866d05
commit
7a8e5d468c
@ -0,0 +1,61 @@ |
||||
package crosslink_sending |
||||
|
||||
import ( |
||||
"github.com/harmony-one/harmony/core" |
||||
"github.com/harmony-one/harmony/shard" |
||||
) |
||||
|
||||
type broadcast interface { |
||||
BroadcastCrosslinkHeartbeatSignalFromBeaconToShards() |
||||
BroadcastCrossLinkFromShardsToBeacon() |
||||
} |
||||
|
||||
type Service struct { |
||||
node broadcast |
||||
bc *core.BlockChain |
||||
ch chan core.ChainEvent |
||||
closeCh chan struct{} |
||||
beacon bool |
||||
} |
||||
|
||||
func New(node broadcast, bc *core.BlockChain) *Service { |
||||
return &Service{ |
||||
node: node, |
||||
bc: bc, |
||||
ch: make(chan core.ChainEvent, 1), |
||||
closeCh: make(chan struct{}), |
||||
beacon: bc.ShardID() == shard.BeaconChainShardID, |
||||
} |
||||
} |
||||
|
||||
// Start starts service.
|
||||
func (s *Service) Start() error { |
||||
s.bc.SubscribeChainEvent(s.ch) |
||||
go s.run() |
||||
return nil |
||||
} |
||||
|
||||
func (s *Service) run() { |
||||
for { |
||||
select { |
||||
case _, ok := <-s.ch: |
||||
if !ok { |
||||
return |
||||
} |
||||
if s.beacon { |
||||
go s.node.BroadcastCrosslinkHeartbeatSignalFromBeaconToShards() |
||||
} else { |
||||
// TODO: this should be uncommented for beacon sync, no need to have it now.
|
||||
//go s.node.BroadcastCrossLinkFromShardsToBeacon()
|
||||
} |
||||
case <-s.closeCh: |
||||
return |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Stop stops service.
|
||||
func (s *Service) Stop() error { |
||||
close(s.closeCh) |
||||
return nil |
||||
} |
@ -0,0 +1,9 @@ |
||||
package types |
||||
|
||||
type CrosslinkHeartbeat struct { |
||||
ShardID uint32 |
||||
LatestContinuousBlockNum uint64 |
||||
Epoch uint64 |
||||
PublicKey []byte |
||||
Signature []byte |
||||
} |
@ -0,0 +1,48 @@ |
||||
package types |
||||
|
||||
import ( |
||||
"math/big" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
func TestCrosslinks_Sorting(t *testing.T) { |
||||
bb := CrossLinks{ |
||||
{ |
||||
BlockNumberF: big.NewInt(1), |
||||
ViewIDF: big.NewInt(0), |
||||
}, |
||||
{ |
||||
BlockNumberF: big.NewInt(1), |
||||
ViewIDF: big.NewInt(1), |
||||
}, |
||||
{ |
||||
BlockNumberF: big.NewInt(1), |
||||
ViewIDF: big.NewInt(4), |
||||
}, |
||||
{ |
||||
BlockNumberF: big.NewInt(1), |
||||
ViewIDF: big.NewInt(3), |
||||
}, |
||||
{ |
||||
BlockNumberF: big.NewInt(1), |
||||
ViewIDF: big.NewInt(2), |
||||
}, |
||||
} |
||||
bb.Sort() |
||||
|
||||
for i, v := range bb { |
||||
require.EqualValues(t, i, v.ViewID().Uint64()) |
||||
} |
||||
|
||||
require.True(t, bb.IsSorted()) |
||||
} |
||||
|
||||
func TestBigNumberInequality(t *testing.T) { |
||||
type A struct { |
||||
X int |
||||
} |
||||
require.False(t, big.NewInt(1) == big.NewInt(1)) |
||||
require.False(t, &A{} == &A{}) |
||||
} |
Loading…
Reference in new issue