private void StartInit() { if (0 == GetBlockCount() && NodeService.Instance.PosWallet.AccountId == ProtocolSettings.Default.StandbyValidators[0]) { // do genesis var authGenesis = new ServiceBlock { Index = 1, UIndex = 1, NetworkId = _nodeConfig.Lyra.NetworkId, ShardId = "Primary", TransferFee = 1, TokenGenerationFee = 100, TradeFee = 0.1m, SvcAccountID = NodeService.Instance.PosWallet.AccountId }; authGenesis.InitializeBlock(null, NodeService.Instance.PosWallet.PrivateKey, _nodeConfig.Lyra.NetworkId, authGenesis.ShardId, NodeService.Instance.PosWallet.AccountId); authGenesis.UHash = SignableObject.CalculateHash($"{authGenesis.UIndex}|{authGenesis.Index}|{authGenesis.Hash}"); authGenesis.Authorizations = new List <AuthorizationSignature>(); authGenesis.Authorizations.Add(new AuthorizationSignature { Key = NodeService.Instance.PosWallet.AccountId, Signature = Signatures.GetSignature(NodeService.Instance.PosWallet.PrivateKey, authGenesis.Hash, NodeService.Instance.PosWallet.AccountId) }); // TODO: add more seed's auth info _store.AddBlock(authGenesis); // the first consolidate block var consBlock = new ConsolidationBlock { UIndex = 2, NetworkId = authGenesis.NetworkId, ShardId = authGenesis.ShardId, ServiceHash = authGenesis.Hash, SvcAccountID = NodeService.Instance.PosWallet.AccountId }; consBlock.InitializeBlock(authGenesis, NodeService.Instance.PosWallet.PrivateKey, _nodeConfig.Lyra.NetworkId, authGenesis.ShardId, NodeService.Instance.PosWallet.AccountId); consBlock.UHash = SignableObject.CalculateHash($"{consBlock.UIndex}|{consBlock.Index}|{consBlock.Hash}"); consBlock.Authorizations = new List <AuthorizationSignature>(); consBlock.Authorizations.Add(new AuthorizationSignature { Key = NodeService.Instance.PosWallet.AccountId, Signature = Signatures.GetSignature(NodeService.Instance.PosWallet.PrivateKey, consBlock.Hash + consBlock.ServiceHash, NodeService.Instance.PosWallet.AccountId) }); _store.AddBlock(consBlock); // tell consensus what happened InSyncing = false; var board = new BillBoard(); board.Add(NodeService.Instance.PosWallet.AccountId); // add me! LyraSystem.Singleton.Consensus.Tell(board); LyraSystem.Singleton.Consensus.Tell(new ConsensusService.BlockChainSynced()); _log.LogInformation("Service Genesis Completed."); } else { SyncBlocksFromSeeds(0); } }
/// <summary> /// if this node is seed0 then sync with seeds others (random choice the one that is in normal state) /// if this node is seed1+ then sync with seed0 /// otherwise sync with any seed node /// </summary> private void SyncBlocksFromSeeds(long ToUIndex) { InSyncing = true; Task.Run(async() => { while (true) { _log.LogInformation("BlockChain Doing Sync..."); string syncWithUrl = null; LyraRestClient client = null; long syncToUIndex = ToUIndex; for (int i = 0; i < ProtocolSettings.Default.SeedList.Length; i++) { if (NodeService.Instance.PosWallet.AccountId == ProtocolSettings.Default.StandbyValidators[i]) // self { continue; } try { var addr = ProtocolSettings.Default.SeedList[i].Split(':')[0]; var apiUrl = $"https://{addr}:4505/api/LyraNode/"; _log.LogInformation("Platform {1} Use seed node of {0}", apiUrl, Environment.OSVersion.Platform); client = await LyraRestClient.CreateAsync(NetworkID, Environment.OSVersion.Platform.ToString(), "LyraNode2", "1.0", apiUrl); var mode = await client.GetSyncState(); if (mode.ResultCode == APIResultCodes.Success && mode.Mode == ConsensusWorkingMode.Normal) { syncWithUrl = apiUrl; if (syncToUIndex == 0) { syncToUIndex = mode.NewestBlockUIndex; } break; } } catch (Exception ex) { _log.LogWarning($"Trying to sync.. {ex.Message}"); } } if (syncWithUrl == null) { // no node to sync. if (NodeService.Instance.PosWallet.AccountId == ProtocolSettings.Default.StandbyValidators[0]) { // seed0. no seed to sync. this seed must have the NORMAL blockchain var board = new BillBoard(); board.Add(NodeService.Instance.PosWallet.AccountId); // add me! LyraSystem.Singleton.Consensus.Tell(board); break; } else { _log.LogError("No seed node in normal state. Wait..."); await Task.Delay(300 * 1000); } } else { // update latest billboard var board = await client.GetBillBoardAsync(); LyraSystem.Singleton.Consensus.Tell(board); // do sync with node long startUIndex = _store.GetNewestBlockUIndex() + 1; _log.LogInformation($"BlockChain Doing sync from {startUIndex} to {syncToUIndex} from node {syncWithUrl}"); async Task <bool> DoCopyBlock() { for (long j = startUIndex; j <= syncToUIndex; j++) { var blockResult = await client.GetBlockByUIndex(j).ConfigureAwait(false); if (blockResult.ResultCode == APIResultCodes.Success) { AddBlock(blockResult.GetBlock() as TransactionBlock); startUIndex = j + 1; _log.LogInformation($"BlockChain Synced Block Number: {j}"); } else if (blockResult.ResultCode == APIResultCodes.BlockNotFound) { return(true); } else { // error _log.LogError($"Error syncing block: {blockResult.ResultCode}"); return(false); } } return(true); } var copyOK = await DoCopyBlock().ConfigureAwait(false); if (copyOK) { break; } else { await Task.Delay(5000).ConfigureAwait(false); } } } InSyncing = false; LyraSystem.Singleton.Consensus.Tell(new ConsensusService.BlockChainSynced()); _log.LogInformation("BlockChain Sync Completed."); }); }
public void LoadOptions() { try { System.Random rand = new System.Random(); // loading of options will happen here m_Engine.SetTerrain(200, 200, @"..\..\Resources\heightmap.jpg", @"..\..\Resources\sand1.jpg", 10.0f, 0.45f); for (int i = 0; i < 150; i++) { float north = (float)(rand.NextDouble() * 1900.0); float east = (float)(rand.NextDouble() * 1900.0); BillBoard.Add(east, north, 0.0f, "cactus" + i, @"..\..\Resources\cactus.dds", 1.0f, 1.0f); } for (int i = 0; i < 150; i++) { float north = (float)(rand.NextDouble() * 1900.0); float east = (float)(rand.NextDouble() * 1900.0); BillBoard.Add(east, north, 0.0f, "tree" + i, @"..\..\Resources\palmtree.dds", 6.5f, 10.0f); } GameEngine.Console.AddLine("all trees loaded"); // m_Engine.AddObject( new ParticleGenerator("Spray1", 2000, 2000, Color.Yellow, "Particle.bmp", new ParticleUpdate(Gravity))); double j = 0.0; double center_x = 1000.0; double center_z = 1000.0; double radius = 700.0; double width = 20.0; m_flag = new Cloth("flag", @"..\..\Resources\flag.jpg", 2, 2, 0.1, 1.0f); m_flag.Height = 0.6f; m_flag.North = 2.0f; m_flag.East = 0.1f; Cloth.EastWind = -3.0f; for (double i = 0.0; i < 360.0; i += 1.5) { float north = (float)(center_z + Math.Cos(i / 180.0 * Math.PI) * radius ); float east = (float)(center_x + Math.Sin(i / 180.0 * Math.PI) * radius ); BillBoard.Add(east, north, 0.0f, "redpost" + (int)(i * 2), @"..\..\Resources\redpost.dds", 0.25f, 1.0f); j += 5.0; if (j > 360.0) { j -= 360.0; } } j = 0.0; for (double i = 0.5; i < 360.0; i += 1.5) { float north = (float)(center_z + Math.Cos(i / 180.0 * Math.PI) * (radius + width) ); float east = (float)(center_x + Math.Sin(i / 180.0 * Math.PI) * (radius + width) ); BillBoard.Add(east, north, 0.0f, "bluepost" + (int)(i * 2), @"..\..\Resources\bluepost.dds", 0.25f, 1.0f); j += 5.0; if (j >= 360.0) { j -= 360.0; } } m_ownship = new Ownship(this, "car1", @"..\..\Resources\SprintRacer.x", new Vector3(0.0f, 0.8f, 0.0f), new Attitude(0.0f, (float)Math.PI, 0.0f)); m_ownship.AddChild(m_flag); SoundEffect.Volume = 0.25f; m_Engine.AddObject(m_ownship); /* Opponent opp1 = new Opponent("car2", @"..\..\Resources\SprintRacer.x", new Vector3(0.0f, 0.8f, 0.0f), * new Attitude(0.0f, (float)Math.PI, 0.0f), "knowledge.xml"); * opp1.SetLOD( 10, 3000.0f ); * m_opponents.Add( opp1 ); * m_Engine.AddObject( opp1 ); */ m_ownship.North = 298.0f; m_ownship.East = 1000.0f; m_Engine.Cam.Attach(m_ownship, new Vector3(0.0f, 0.85f, -4.5f)); m_Engine.Cam.LookAt(m_ownship); m_ownship.Heading = (float)Math.PI * 1.5f; m_ownship.SetLOD(10, 3000.0f); // Car car2 = (Car)m_Engine.GetObject("car2"); // car2.North = 295.0f; // car2.East = 1000.0f; // car2.Height = 0.0f; // car2.Heading = (float)Math.PI * 1.5f; // car2.SetLOD( 10, 300.0f ); // car2.SetUpdateMethod( new ObjectUpdate(OpponentUpdate)); GameEngine.GameLights.Ambient = Color.White; // GameEngine.GameLights light1 = GameEngine.GameLights.AddPointLight( new Vector3(130.0f,35.0f,130.0f),Color.White,"light1"); // light1.Attenuation1 = 0.11f; // GameEngine.GameLights light2 = GameEngine.GameLights.AddPointLight( new Vector3(40.0f,25.0f,40.0f),Color.White,"light2"); // light2.Attenuation1 = 0.11f; // GameEngine.GameLights light3 = GameEngine.GameLights.AddPointLight( new Vector3(130.0f,35.0f,230.0f),Color.White,"light3"); // light3.Attenuation1 = 0.11f; // GameEngine.GameLights light4 = GameEngine.GameLights.AddPointLight( new Vector3(230.0f,40.0f,230.0f),Color.White,"light4"); // light4.Attenuation1 = 0.11f; // GameEngine.GameLights light5 = GameEngine.GameLights.AddPointLight( new Vector3(70.0f,25.0f,70.0f),Color.White,"light5"); // light5.Attenuation1 = 0.11f; GameEngine.GameLights headlights = GameEngine.GameLights.AddSpotLight(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(1.0f, 0.0f, 1.0f), Color.White, "headlight"); headlights.EffectiveRange = 200.0f; headlights.Attenuation0 = 1.0f; headlights.Attenuation1 = 0.0f; headlights.InnerConeAngle = 1.0f; headlights.OuterConeAngle = 1.5f; headlights.PositionOffset = new Vector3(0.0f, 2.0f, 1.0f); headlights.DirectionOffset = new Vector3(0.0f, 0.00f, 1.0f); m_ownship.AddChild(headlights); headlights.Enabled = false; CGameEngine.FogColor = Color.Beige; CGameEngine.FogDensity = 0.5f; CGameEngine.FogEnable = true; CGameEngine.FogStart = 100.0f; CGameEngine.FogEnd = 900.0f; CGameEngine.FogTableMode = FogMode.Linear; } catch (Exception e) { GameEngine.Console.AddLine("Exception"); GameEngine.Console.AddLine(e.Message); } }