diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-10-26 13:22:29 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-26 13:22:29 +0800 |
commit | 721d36f2720b0cbf648cbffe40fd05c9a60061e4 (patch) | |
tree | 05d643695b20cc9f430869cf2105c177856625bc /simulation | |
parent | ef0df0e6e27acd3852f2e0efdccf0798d5fc63ad (diff) | |
download | dexon-consensus-721d36f2720b0cbf648cbffe40fd05c9a60061e4.tar dexon-consensus-721d36f2720b0cbf648cbffe40fd05c9a60061e4.tar.gz dexon-consensus-721d36f2720b0cbf648cbffe40fd05c9a60061e4.tar.bz2 dexon-consensus-721d36f2720b0cbf648cbffe40fd05c9a60061e4.tar.lz dexon-consensus-721d36f2720b0cbf648cbffe40fd05c9a60061e4.tar.xz dexon-consensus-721d36f2720b0cbf648cbffe40fd05c9a60061e4.tar.zst dexon-consensus-721d36f2720b0cbf648cbffe40fd05c9a60061e4.zip |
core: Pull block (#263)
Diffstat (limited to 'simulation')
-rw-r--r-- | simulation/network.go | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/simulation/network.go b/simulation/network.go index 5cd97b6..86a70b9 100644 --- a/simulation/network.go +++ b/simulation/network.go @@ -88,6 +88,7 @@ type network struct { toNode chan interface{} sentRandomness map[common.Hash]struct{} sentAgreement map[common.Hash]struct{} + blockCache map[common.Hash]*types.Block } // newNetwork setup network stuffs for nodes, which provides an @@ -105,6 +106,7 @@ func newNetwork(pubKey crypto.PublicKey, cfg config.Networking) (n *network) { toConsensus: make(chan interface{}, 1000), sentRandomness: make(map[common.Hash]struct{}), sentAgreement: make(map[common.Hash]struct{}), + blockCache: make(map[common.Hash]*types.Block), } n.ctx, n.ctxCancel = context.WithCancel(context.Background()) // Construct transport layer. @@ -123,6 +125,20 @@ func newNetwork(pubKey crypto.PublicKey, cfg config.Networking) (n *network) { return } +// PullBlock implements core.Network interface. +func (n *network) PullBlocks(hashes common.Hashes) { + go func() { + for _, hash := range hashes { + // TODO(jimmy-dexon): request block from network instead of cache. + if block, exist := n.blockCache[hash]; exist { + n.toConsensus <- block + continue + } + panic(fmt.Errorf("unknown block %s requested", hash)) + } + }() +} + // BroadcastVote implements core.Network interface. func (n *network) BroadcastVote(vote *types.Vote) { if err := n.trans.Broadcast(vote); err != nil { @@ -240,6 +256,15 @@ func (n *network) run() { // The dispatcher declararion: // to consensus or node, that's the question. disp := func(e *test.TransportEnvelope) { + if block, ok := e.Msg.(*types.Block); ok { + if len(n.blockCache) > 500 { + for k := range n.blockCache { + delete(n.blockCache, k) + break + } + } + n.blockCache[block.Hash] = block + } switch e.Msg.(type) { case *types.Block, *types.Vote, *types.AgreementResult, *types.BlockRandomnessResult, |