public static (IFederationManager federationManager, IFederationHistory federationHistory) CreateFederationManager(object caller, Network network, LoggerFactory loggerFactory, ISignals signals)
        {
            string dir = TestBase.CreateTestDir(caller);

            var dbreezeSerializer = new DBreezeSerializer(network.Consensus.ConsensusFactory);

            var nodeSettings = new NodeSettings(network, args: new string[] { $"-datadir={dir}" });

            Key federationKey = new Mnemonic("lava frown leave wedding virtual ghost sibling able mammal liar wide wisdom").DeriveExtKey().PrivateKey;

            new KeyTool(nodeSettings.DataFolder).SavePrivateKey(federationKey);

            var consensusManager = new Mock <IConsensusManager>();

            consensusManager.Setup(c => c.Tip).Returns(new ChainedHeader(network.GetGenesis().Header, network.GenesisHash, 0));

            var fullNode = new Mock <IFullNode>();

            fullNode.Setup(x => x.NodeService <IConsensusManager>(false)).Returns(consensusManager.Object);

            var counterChainSettings = new CounterChainSettings(nodeSettings, new CounterChainNetworkWrapper(new StraxRegTest()));

            var federationManager = new FederationManager(fullNode.Object, network, nodeSettings, signals, counterChainSettings);
            var asyncProvider     = new AsyncProvider(loggerFactory, signals);

            var chainIndexerMock = new Mock <ChainIndexer>();
            var header           = new BlockHeader();

            chainIndexerMock.Setup(x => x.Tip).Returns(new ChainedHeader(header, header.GetHash(), 0));
            var votingManager = new VotingManager(federationManager, loggerFactory,
                                                  new Mock <IPollResultExecutor>().Object, new Mock <INodeStats>().Object, nodeSettings.DataFolder, dbreezeSerializer, signals, network, null, chainIndexerMock.Object, null, nodeSettings);

            var federationHistory = new Mock <IFederationHistory>();

            federationHistory.Setup(x => x.GetFederationMemberForBlock(It.IsAny <ChainedHeader>())).Returns <ChainedHeader>((chainedHeader) =>
            {
                List <IFederationMember> members = ((PoAConsensusOptions)network.Consensus.Options).GenesisFederationMembers;
                return(members[chainedHeader.Height % members.Count]);
            });

            federationHistory.Setup(x => x.GetFederationForBlock(It.IsAny <ChainedHeader>())).Returns <ChainedHeader>((chainedHeader) =>
            {
                return(((PoAConsensusOptions)network.Consensus.Options).GenesisFederationMembers);
            });

            federationHistory.Setup(x => x.CanGetFederationForBlock(It.IsAny <ChainedHeader>())).Returns <ChainedHeader>((chainedHeader) => true);

            votingManager.Initialize(federationHistory.Object);
            fullNode.Setup(x => x.NodeService <VotingManager>(It.IsAny <bool>())).Returns(votingManager);
            federationManager.Initialize();

            return(federationManager, federationHistory.Object);
        }
Exemple #2
0
        private void InitializeCollateralChecker([CallerMemberName] string callingMethod = "")
        {
            var loggerFactory = new LoggerFactory();
            IHttpClientFactory clientFactory = new Bitcoin.Controllers.HttpClientFactory();

            Network network = CirrusNetwork.NetworksSelector.Regtest();

            this.collateralFederationMembers = new List <CollateralFederationMember>()
            {
                new CollateralFederationMember(new PubKey("036317d97f911ce899fd0a360866d19f2dca5252c7960d4652d814ab155a8342de"), false, new Money(100), "addr1"),
                new CollateralFederationMember(new PubKey("02a08d72d47b3103261163c15aa2f6b0d007e1872ad9f5fddbfbd27bdb738156e9"), false, new Money(500), "addr2"),
                new CollateralFederationMember(new PubKey("03634c79d4e8e915cfb9f7bbef57bed32d715150836b7845b1a14c93670d816ab6"), false, new Money(100_000), "addr3")
            };

            List <IFederationMember> federationMembers = (network.Consensus.Options as PoAConsensusOptions).GenesisFederationMembers;

            federationMembers.Clear();
            federationMembers.AddRange(this.collateralFederationMembers);

            var dataFolder = TestBase.CreateTestDir(callingMethod);
            FederatedPegSettings fedPegSettings = FedPegTestsHelper.CreateSettings(network, KnownNetworks.StraxRegTest, dataFolder, out NodeSettings nodeSettings);

            var counterChainSettings = new CounterChainSettings(nodeSettings, new CounterChainNetworkWrapper(Networks.Strax.Regtest()));
            var asyncMock            = new Mock <IAsyncProvider>();

            asyncMock.Setup(a => a.RegisterTask(It.IsAny <string>(), It.IsAny <Task>()));

            ISignals signals            = new Signals(loggerFactory, new DefaultSubscriptionErrorHandler(loggerFactory));
            var      dbreezeSerializer  = new DBreezeSerializer(network.Consensus.ConsensusFactory);
            var      asyncProvider      = new AsyncProvider(loggerFactory, signals);
            var      finalizedBlockRepo = new FinalizedBlockInfoRepository(new LevelDbKeyValueRepository(nodeSettings.DataFolder, dbreezeSerializer), asyncProvider);

            finalizedBlockRepo.LoadFinalizedBlockInfoAsync(network).GetAwaiter().GetResult();

            var chainIndexerMock = new Mock <ChainIndexer>();
            var header           = new BlockHeader();

            chainIndexerMock.Setup(x => x.Tip).Returns(new ChainedHeader(header, header.GetHash(), 0));
            var fullNode = new Mock <IFullNode>();

            IFederationManager federationManager = new FederationManager(fullNode.Object, network, nodeSettings, signals, counterChainSettings);
            var votingManager     = new VotingManager(federationManager, new Mock <IPollResultExecutor>().Object, new Mock <INodeStats>().Object, nodeSettings.DataFolder, dbreezeSerializer, signals, finalizedBlockRepo, network);
            var federationHistory = new FederationHistory(federationManager, votingManager);

            votingManager.Initialize(federationHistory);

            fullNode.Setup(x => x.NodeService <VotingManager>(It.IsAny <bool>())).Returns(votingManager);

            federationManager.Initialize();

            this.collateralChecker = new CollateralChecker(clientFactory, counterChainSettings, federationManager, signals, network, asyncMock.Object, (new Mock <INodeLifetime>()).Object);
        }
Exemple #3
0
        private VotingManager InitializeVotingManager(NodeSettings nodeSettings)
        {
            var dbreezeSerializer = new DBreezeSerializer(this.network.Consensus.ConsensusFactory);
            var asyncProvider     = new AsyncProvider(this.loggerFactory, this.signals);

            var chainIndexerMock = new Mock <ChainIndexer>();
            var header           = new BlockHeader();

            chainIndexerMock.Setup(x => x.Tip).Returns(new ChainedHeader(header, header.GetHash(), 0));

            var votingManager     = new VotingManager(this.federationManager, this.loggerFactory, new Mock <IPollResultExecutor>().Object, new Mock <INodeStats>().Object, nodeSettings.DataFolder, dbreezeSerializer, this.signals, this.network);
            var federationHistory = new FederationHistory(this.federationManager, this.network, votingManager);

            votingManager.Initialize(federationHistory);

            return(votingManager);
        }
        public static IFederationManager CreateFederationManager(object caller, Network network, LoggerFactory loggerFactory, ISignals signals)
        {
            string dir = TestBase.CreateTestDir(caller);

            var dbreezeSerializer = new DBreezeSerializer(network.Consensus.ConsensusFactory);

            var nodeSettings = new NodeSettings(network, args: new string[] { $"-datadir={dir}" });

            Key federationKey = new Mnemonic("lava frown leave wedding virtual ghost sibling able mammal liar wide wisdom").DeriveExtKey().PrivateKey;

            new KeyTool(nodeSettings.DataFolder).SavePrivateKey(federationKey);

            var consensusManager = new Mock <IConsensusManager>();

            consensusManager.Setup(c => c.Tip).Returns(new ChainedHeader(network.GetGenesis().Header, network.GenesisHash, 0));

            var fullNode = new Mock <IFullNode>();

            fullNode.Setup(x => x.NodeService <IConsensusManager>(false)).Returns(consensusManager.Object);

            var counterChainSettings = new CounterChainSettings(nodeSettings, new CounterChainNetworkWrapper(new StraxRegTest()));

            var federationManager  = new FederationManager(counterChainSettings, fullNode.Object, network, nodeSettings, loggerFactory, signals);
            var asyncProvider      = new AsyncProvider(loggerFactory, signals, new Mock <INodeLifetime>().Object);
            var finalizedBlockRepo = new FinalizedBlockInfoRepository(new KeyValueRepository(nodeSettings.DataFolder, dbreezeSerializer), loggerFactory, asyncProvider);

            finalizedBlockRepo.LoadFinalizedBlockInfoAsync(network).GetAwaiter().GetResult();

            var chainIndexerMock = new Mock <ChainIndexer>();
            var header           = new BlockHeader();

            chainIndexerMock.Setup(x => x.Tip).Returns(new ChainedHeader(header, header.GetHash(), 0));
            var slotsManager  = new SlotsManager(network, federationManager, chainIndexerMock.Object, loggerFactory);
            var votingManager = new VotingManager(federationManager, loggerFactory, slotsManager,
                                                  new Mock <IPollResultExecutor>().Object, new Mock <INodeStats>().Object, nodeSettings.DataFolder, dbreezeSerializer, signals, finalizedBlockRepo, network);

            votingManager.Initialize();
            fullNode.Setup(x => x.NodeService <VotingManager>(It.IsAny <bool>())).Returns(votingManager);
            federationManager.Initialize();

            return(federationManager);
        }
        private VotingManager InitializeVotingManager(NodeSettings nodeSettings)
        {
            var dbreezeSerializer  = new DBreezeSerializer(this.network.Consensus.ConsensusFactory);
            var asyncProvider      = new AsyncProvider(this.loggerFactory, this.signals, new Mock <INodeLifetime>().Object);
            var finalizedBlockRepo = new FinalizedBlockInfoRepository(new KeyValueRepository(nodeSettings.DataFolder, dbreezeSerializer), this.loggerFactory, asyncProvider);

            finalizedBlockRepo.LoadFinalizedBlockInfoAsync(this.network).GetAwaiter().GetResult();

            var chainIndexerMock = new Mock <ChainIndexer>();
            var header           = new BlockHeader();

            chainIndexerMock.Setup(x => x.Tip).Returns(new ChainedHeader(header, header.GetHash(), 0));

            var slotsManager  = new SlotsManager(this.network, this.federationManager, chainIndexerMock.Object, this.loggerFactory);
            var votingManager = new VotingManager(this.federationManager, this.loggerFactory, slotsManager, new Mock <IPollResultExecutor>().Object, new Mock <INodeStats>().Object, nodeSettings.DataFolder, dbreezeSerializer, this.signals, finalizedBlockRepo, this.network);

            votingManager.Initialize();

            return(votingManager);
        }
        public static (IFederationManager federationManager, IFederationHistory federationHistory) CreateFederationManager(object caller, Network network, LoggerFactory loggerFactory, ISignals signals)
        {
            string dir = TestBase.CreateTestDir(caller);

            var dbreezeSerializer = new DBreezeSerializer(network.Consensus.ConsensusFactory);

            var nodeSettings = new NodeSettings(network, args: new string[] { $"-datadir={dir}" });

            Key federationKey = new Mnemonic("lava frown leave wedding virtual ghost sibling able mammal liar wide wisdom").DeriveExtKey().PrivateKey;

            new KeyTool(nodeSettings.DataFolder).SavePrivateKey(federationKey);

            var consensusManager = new Mock <IConsensusManager>();

            consensusManager.Setup(c => c.Tip).Returns(new ChainedHeader(network.GetGenesis().Header, network.GenesisHash, 0));

            var fullNode = new Mock <IFullNode>();

            fullNode.Setup(x => x.NodeService <IConsensusManager>(false)).Returns(consensusManager.Object);

            var counterChainSettings = new CounterChainSettings(nodeSettings, new CounterChainNetworkWrapper(new StraxRegTest()));

            var federationManager  = new FederationManager(fullNode.Object, network, nodeSettings, signals, counterChainSettings);
            var asyncProvider      = new AsyncProvider(loggerFactory, signals);
            var finalizedBlockRepo = new FinalizedBlockInfoRepository(new LevelDbKeyValueRepository(nodeSettings.DataFolder, dbreezeSerializer), asyncProvider);

            finalizedBlockRepo.LoadFinalizedBlockInfoAsync(network).GetAwaiter().GetResult();

            var chainIndexerMock = new Mock <ChainIndexer>();
            var header           = new BlockHeader();

            chainIndexerMock.Setup(x => x.Tip).Returns(new ChainedHeader(header, header.GetHash(), 0));
            var votingManager = new VotingManager(federationManager, new Mock <IPollResultExecutor>().Object,
                                                  new Mock <INodeStats>().Object, nodeSettings.DataFolder, dbreezeSerializer, signals, finalizedBlockRepo, network);

            var federationHistory = new Mock <IFederationHistory>();

            federationHistory.Setup(x => x.GetFederationMemberForBlock(It.IsAny <ChainedHeader>())).Returns <ChainedHeader>((chainedHeader) =>
            {
                List <IFederationMember> members = ((PoAConsensusOptions)network.Consensus.Options).GenesisFederationMembers;
                return(members[chainedHeader.Height % members.Count]);
            });

            federationHistory.Setup(x => x.GetFederationMemberForBlock(It.IsAny <ChainedHeader>(), It.IsAny <List <IFederationMember> >())).Returns <ChainedHeader, List <IFederationMember> >((chainedHeader, members) =>
            {
                members = members ?? ((PoAConsensusOptions)network.Consensus.Options).GenesisFederationMembers;
                return(members[chainedHeader.Height % members.Count]);
            });

            federationHistory.Setup(x => x.GetFederationForBlock(It.IsAny <ChainedHeader>())).Returns <ChainedHeader>((chainedHeader) =>
            {
                return(((PoAConsensusOptions)network.Consensus.Options).GenesisFederationMembers);
            });

            federationHistory
            .Setup(x => x.GetFederationMemberForTimestamp(It.IsAny <uint>(), It.IsAny <PoAConsensusOptions>()))
            .Returns <uint, PoAConsensusOptions>((headerUnixTimestamp, poAConsensusOptions) =>
            {
                List <IFederationMember> federationMembers = poAConsensusOptions.GenesisFederationMembers;

                uint roundTime = (uint)(federationMembers.Count * poAConsensusOptions.TargetSpacingSeconds);

                // Time when current round started.
                uint roundStartTimestamp = (headerUnixTimestamp / roundTime) * roundTime;

                // Slot number in current round.
                int currentSlotNumber = (int)((headerUnixTimestamp - roundStartTimestamp) / poAConsensusOptions.TargetSpacingSeconds);

                return(federationMembers[currentSlotNumber]);
            });

            votingManager.Initialize(federationHistory.Object);
            fullNode.Setup(x => x.NodeService <VotingManager>(It.IsAny <bool>())).Returns(votingManager);
            federationManager.Initialize();

            return(federationManager, federationHistory.Object);
        }