github.com/braveheart12/insolar-09-08-19@v0.8.7/network/servicenetwork/integr_pulsar.go (about) 1 /* 2 * The Clear BSD License 3 * 4 * Copyright (c) 2019 Insolar Technologies 5 * 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met: 9 * 10 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 11 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 12 * Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 13 * 14 * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 15 * 16 */ 17 18 package servicenetwork 19 20 import ( 21 "context" 22 "time" 23 24 "github.com/insolar/insolar/component" 25 "github.com/insolar/insolar/configuration" 26 "github.com/insolar/insolar/core" 27 "github.com/insolar/insolar/network/pulsenetwork" 28 "github.com/insolar/insolar/network/transport" 29 "github.com/insolar/insolar/network/transport/relay" 30 "github.com/insolar/insolar/pulsar/entropygenerator" 31 "github.com/pkg/errors" 32 ) 33 34 type TestPulsar interface { 35 Start(ctx context.Context, bootstrapHosts []string) error 36 component.Stopper 37 } 38 39 func NewTestPulsar(pulseTimeMs, requestsTimeoutMs, pulseDelta int32) (TestPulsar, error) { 40 transportCfg := configuration.Transport{ 41 Protocol: "TCP", 42 Address: "127.0.0.1:0", 43 BehindNAT: false, 44 } 45 tp, err := transport.NewTransport(transportCfg, relay.NewProxy()) 46 if err != nil { 47 return nil, errors.Wrap(err, "Failed to create distributor transport") 48 } 49 return &testPulsar{ 50 transport: tp, 51 generator: &entropygenerator.StandardEntropyGenerator{}, 52 pulseTimeMs: pulseTimeMs, 53 reqTimeoutMs: requestsTimeoutMs, 54 pulseDelta: pulseDelta, 55 cancellationToken: make(chan struct{}), 56 }, nil 57 } 58 59 type testPulsar struct { 60 transport transport.Transport 61 distributor core.PulseDistributor 62 generator entropygenerator.EntropyGenerator 63 cm *component.Manager 64 65 pulseTimeMs int32 66 reqTimeoutMs int32 67 pulseDelta int32 68 69 cancellationToken chan struct{} 70 } 71 72 func (tp *testPulsar) Start(ctx context.Context, bootstrapHosts []string) error { 73 var err error 74 distributorCfg := configuration.PulseDistributor{ 75 BootstrapHosts: bootstrapHosts, 76 PingRequestTimeout: tp.reqTimeoutMs, 77 RandomHostsRequestTimeout: tp.reqTimeoutMs, 78 PulseRequestTimeout: tp.reqTimeoutMs, 79 RandomNodesCount: 1, 80 } 81 tp.distributor, err = pulsenetwork.NewDistributor(distributorCfg) 82 if err != nil { 83 return errors.Wrap(err, "Failed to create pulse distributor") 84 } 85 86 tp.cm = &component.Manager{} 87 tp.cm.Inject(tp.transport, tp.distributor) 88 89 if err = tp.cm.Init(ctx); err != nil { 90 return errors.Wrap(err, "Failed to init test pulsar components") 91 } 92 if err = tp.cm.Start(ctx); err != nil { 93 return errors.Wrap(err, "Failed to start test pulsar components") 94 } 95 96 go tp.distribute(ctx) 97 return nil 98 } 99 100 func (tp *testPulsar) distribute(ctx context.Context) { 101 timeNow := time.Now() 102 pulseNumber := core.CalculatePulseNumber(timeNow) 103 pulse := core.Pulse{ 104 PulseNumber: pulseNumber, 105 Entropy: tp.generator.GenerateEntropy(), 106 NextPulseNumber: pulseNumber + core.PulseNumber(tp.pulseDelta), 107 PrevPulseNumber: pulseNumber - core.PulseNumber(tp.pulseDelta), 108 EpochPulseNumber: 1, 109 OriginID: [16]byte{206, 41, 229, 190, 7, 240, 162, 155, 121, 245, 207, 56, 161, 67, 189, 0}, 110 PulseTimestamp: timeNow.Unix(), 111 } 112 113 for { 114 select { 115 case <-time.After(time.Duration(tp.pulseTimeMs) * time.Millisecond): 116 go tp.distributor.Distribute(ctx, pulse) 117 pulse = tp.incrementPulse(pulse) 118 case <-tp.cancellationToken: 119 return 120 } 121 } 122 } 123 124 func (tp *testPulsar) incrementPulse(pulse core.Pulse) core.Pulse { 125 newPulse := pulse.PulseNumber + core.PulseNumber(tp.pulseDelta) 126 return core.Pulse{ 127 PulseNumber: newPulse, 128 Entropy: tp.generator.GenerateEntropy(), 129 NextPulseNumber: newPulse + core.PulseNumber(tp.pulseDelta), 130 PrevPulseNumber: pulse.PulseNumber, 131 EpochPulseNumber: pulse.EpochPulseNumber, 132 OriginID: pulse.OriginID, 133 PulseTimestamp: time.Now().Unix(), 134 } 135 } 136 137 func (tp *testPulsar) Stop(ctx context.Context) error { 138 if err := tp.cm.Stop(ctx); err != nil { 139 return errors.Wrap(err, "Failed to stop test pulsar components") 140 } 141 close(tp.cancellationToken) 142 return nil 143 }