Exemple #1
0
 public object ToJson()
 {
     return(new
     {
         number = Number?.ToJson(),
         hash = Hash?.ToJson(),
         parentHash = ParentHash?.ToJson(),
         nonce = Nonce?.ToJson(),
         MixHash = MixHash?.ToJson(),
         sha3Uncles = Sha3Uncles?.ToJson(),
         logsBloom = LogsBloom?.ToJson(),
         transactionsRoot = TransactionsRoot?.ToJson(),
         stateRoot = StateRoot?.ToJson(),
         receiptsRoot = ReceiptsRoot?.ToJson(),
         miner = Miner?.ToJson(),
         difficulty = Difficulty?.ToJson(),
         totalDifficulty = TotalDifficulty?.ToJson(),
         extraData = ExtraData?.ToJson(),
         size = Size?.ToJson(),
         gasLimit = GasLimit?.ToJson(),
         gasUsed = GasUsed?.ToJson(),
         timestamp = Timestamp?.ToJson(),
         transactions = Transactions?.Select(x => x.ToJson()).ToArray() ?? TransactionHashes?.Select(x => x.ToJson()).ToArray(),
         uncles = Uncles?.Select(x => x.ToJson()).ToArray()
     });
 }
Exemple #2
0
 void OnSongenPointValueIsZero()
 {
     SongenPointValue = 0.0f;
     SongenBar.Value  = 0;
     StateRoot.BroadcastMessage("OnEndGame");
     CanvasRoot.BroadcastMessage("OnEndGame");
 }
Exemple #3
0
 private static IEnumerable <StateRoot> GetStateRoots(Stream stream)
 {
     using (BinaryReader r = new BinaryReader(stream))
     {
         uint start = r.ReadUInt32();
         uint count = r.ReadUInt32();
         if (count == 0)
         {
             yield break;
         }
         uint end = checked (start + count) - 1;
         if (end <= Blockchain.Singleton.StateHeight)
         {
             yield break;
         }
         for (uint height = start; height <= end; height++)
         {
             var len = r.ReadInt32();
             if (len > ushort.MaxValue * 3)
             {
                 yield break;
             }
             byte[] array = r.ReadBytes(len);
             if (height > Blockchain.Singleton.Height)
             {
                 yield break;
             }
             if (height > Blockchain.Singleton.StateHeight)
             {
                 StateRoot root = array.AsSerializable <StateRoot>();
                 yield return(root);
             }
         }
     }
 }
Exemple #4
0
 public StateRoot MakeStateRoot()
 {
     if (PreviousBlockStateRoot is null)
     {
         PreviousBlockStateRoot = Blockchain.Singleton.GetStateRoot(Snapshot.Height).StateRoot;
     }
     return(PreviousBlockStateRoot);
 }
Exemple #5
0
        public JObject GetStateRoot(JArray _params)
        {
            uint index = uint.Parse(_params[0].AsString());

            using var snapshot = StateStore.Singleton.GetSnapshot();
            StateRoot state_root = snapshot.GetStateRoot(index);

            if (state_root is null)
            {
                throw new RpcException(-100, "Unknown state root");
            }
            else
            {
                return(state_root.ToJson());
            }
        }
Exemple #6
0
        private void InitInGame()
        {
            CurrentState = State.InGame;
            Debug.Log("Go to InGame.");

            BGM.Play();

            AudioSource.PlayOneShot(AudioGameStart);

            StateRoot.BroadcastMessage("OnStartGame");
            CanvasRoot.BroadcastMessage("OnStartGame");

            SetMessageText("始め!");
            SetMessageScale(1.0f);
            MessageText.StartAlphaUpdate();
        }
Exemple #7
0
        private void OnGetStateRoot(uint index)
        {
            if (System is null || System.Settings.Network != Settings.Default.Network)
            {
                throw new InvalidOperationException("Network doesn't match");
            }
            using var snapshot = StateStore.Singleton.GetSnapshot();
            StateRoot state_root = snapshot.GetStateRoot(index);

            if (state_root is null)
            {
                ConsoleHelper.Warning("Unknown state root");
            }
            else
            {
                ConsoleHelper.Info(state_root.ToJson().ToString());
            }
        }
    public override int GetHashCode()
    {
        int hash = 1;

        if (StateRoot.Length != 0)
        {
            hash ^= StateRoot.GetHashCode();
        }
        if (Address.Length != 0)
        {
            hash ^= Address.GetHashCode();
        }
        if (_unknownFields != null)
        {
            hash ^= _unknownFields.GetHashCode();
        }
        return(hash);
    }
        private RelayResultReason OnNewStateRoot(StateRoot stateRoot)
        {
            if (stateRoot.Index < ExpectStateRootIndex)
            {
                return(RelayResultReason.AlreadyExists);
            }
            if (ExpectStateRootIndex + MaxRootCacheCount < stateRoot.Index)
            {
                return(RelayResultReason.OutOfMemory);
            }
            if (stateRootCache.ContainsKey(stateRoot.Index))
            {
                return(RelayResultReason.AlreadyExists);
            }
            if (stateRoot.Index > ExpectStateRootIndex)
            {
                stateRootCache.Add(stateRoot.Index, stateRoot);
                return(RelayResultReason.Succeed);
            }

            while (stateRoot.Index <= Height)
            {
                stateRootCache.Remove(stateRoot.Index);
                if (!stateRoot.Verify(currentSnapshot))
                {
                    break;
                }
                if (PersistCnStateRoot(stateRoot) == StateRootVerifyFlag.Invalid)
                {
                    break;
                }
                if (stateRoot.Index + 3 > HeaderHeight)
                {
                    system.LocalNode.Tell(new LocalNode.SendDirectly {
                        Inventory = stateRoot
                    });
                }
                if (!stateRootCache.TryGetValue(stateRoot.Index + 1, out stateRoot))
                {
                    break;
                }
            }
            return(RelayResultReason.Succeed);
        }
        private void CheckPreparations()
        {
            if (context.PreparationPayloads.Count(p => p != null) >= context.M() && context.TransactionHashes.All(p => context.Transactions.ContainsKey(p)))
            {
                ConsensusPayload payload = context.MakeCommit();
                Log($"send commit");
                context.Save();
                localNode.Tell(new LocalNode.SendDirectly {
                    Inventory = payload
                });
                // Set timer, so we will resend the commit in case of a networking issue
                ChangeTimer(TimeSpan.FromSeconds(Blockchain.SecondsPerBlock));

                StateRoot stateRoot = context.CreateStateRoot();
                Log($"relay stateRoot: height={stateRoot.Index} hash={stateRoot.Root}");
                localNode.Tell(new LocalNode.Relay {
                    Inventory = stateRoot
                });

                CheckCommits();
            }
        }
        private StateRootVerifyFlag PersistCnStateRoot(StateRoot stateRoot)
        {
            using (Snapshot snapshot = GetSnapshot())
            {
                var localState = snapshot.StateRoots.GetAndChange(stateRoot.Index);
                if (localState.StateRoot.Root == stateRoot.Root && localState.StateRoot.PreHash == stateRoot.PreHash)
                {
                    RootHashIndex rootHashIndex = snapshot.StateRootHashIndex.GetAndChange();
                    rootHashIndex.Index  = stateRoot.Index;
                    rootHashIndex.Hash   = stateRoot.Hash;
                    localState.StateRoot = stateRoot;
                    localState.Flag      = StateRootVerifyFlag.Verified;
                }
                else
                {
                    localState.Flag = StateRootVerifyFlag.Invalid;
                }
                snapshot.Commit();
                UpdateCurrentSnapshot();

                return(localState.Flag);
            }
        }
    public override int GetHashCode()
    {
        int hash = 1;

        if (Status != 0)
        {
            hash ^= Status.GetHashCode();
        }
        hash ^= entries_.GetHashCode();
        if (StateRoot.Length != 0)
        {
            hash ^= StateRoot.GetHashCode();
        }
        if (paging_ != null)
        {
            hash ^= Paging.GetHashCode();
        }
        if (_unknownFields != null)
        {
            hash ^= _unknownFields.GetHashCode();
        }
        return(hash);
    }
Exemple #13
0
 void ICloneable <StateRootState> .FromReplica(StateRootState replica)
 {
     Flag      = replica.Flag;
     StateRoot = replica.StateRoot;
 }
Exemple #14
0
 public override void Deserialize(BinaryReader reader)
 {
     base.Deserialize(reader);
     Flag      = (StateRootVerifyFlag)reader.ReadByte();
     StateRoot = reader.ReadSerializable <StateRoot>();
 }
Exemple #15
0
 public void Reset(byte viewNumber)
 {
     if (viewNumber == 0)
     {
         Block = null;
         Snapshot?.Dispose();
         Snapshot               = Blockchain.Singleton.GetSnapshot();
         PrevHash               = Snapshot.CurrentBlockHash;
         BlockIndex             = Snapshot.Height + 1;
         Validators             = Snapshot.GetValidators();
         MyIndex                = -1;
         ChangeViewPayloads     = new ConsensusPayload[Validators.Length];
         LastChangeViewPayloads = new ConsensusPayload[Validators.Length];
         CommitPayloads         = new ConsensusPayload[Validators.Length];
         if (LastSeenMessage == null)
         {
             LastSeenMessage = new Dictionary <ECPoint, int>();
             foreach (var validator in Validators)
             {
                 LastSeenMessage[validator] = -1;
             }
         }
         if (0 < Snapshot.Height && Snapshot.GetBlock(Snapshot.Height).NextConsensus != Snapshot.GetBlock(Snapshot.Height - 1)?.NextConsensus)
         {
             var old_last_seen_message = LastSeenMessage;
             LastSeenMessage = new Dictionary <ECPoint, int>();
             foreach (var validator in Validators)
             {
                 if (old_last_seen_message.TryGetValue(validator, out int value))
                 {
                     LastSeenMessage[validator] = value;
                 }
                 else
                 {
                     LastSeenMessage[validator] = -1;
                 }
             }
         }
         keyPair = null;
         for (int i = 0; i < Validators.Length; i++)
         {
             WalletAccount account = wallet?.GetAccount(Validators[i]);
             if (account?.HasKey != true)
             {
                 continue;
             }
             MyIndex = i;
             keyPair = account.GetKey();
             break;
         }
         PreviousBlockStateRoot = null;
     }
     else
     {
         for (int i = 0; i < LastChangeViewPayloads.Length; i++)
         {
             if (ChangeViewPayloads[i]?.GetDeserializedMessage <ChangeView>().NewViewNumber >= viewNumber)
             {
                 LastChangeViewPayloads[i] = ChangeViewPayloads[i];
             }
             else
             {
                 LastChangeViewPayloads[i] = null;
             }
         }
     }
     ViewNumber          = viewNumber;
     PrimaryIndex        = this.GetPrimaryIndex(viewNumber);
     Timestamp           = 0;
     TransactionHashes   = null;
     PreparationPayloads = new ConsensusPayload[Validators.Length];
     if (MyIndex >= 0)
     {
         LastSeenMessage[Validators[MyIndex]] = (int)BlockIndex;
     }
     _header = null;
 }