Exemple #1
0
 public void AddRequest(BlockConsensusRequest request, MinerWallet wallet)
 {
     lock (SyncRoot)
     {
         if (request.PrevHash != PrevHash)
         {
             return;
         }
         if (request.Miner == my_pubkey)
         {
             return;
         }
         if (NoncePieces.ContainsKey(request.Miner))
         {
             return;
         }
         byte[] aeskey = wallet.GetAesKey(request.Miner);
         byte[] piece  = request.NoncePieces[my_pubkey];
         using (AesManaged aes = new AesManaged())
             using (ICryptoTransform decryptor = aes.CreateDecryptor(aeskey, request.IV))
             {
                 piece = decryptor.TransformFinalBlock(piece, 0, piece.Length);
             }
         Array.Clear(aeskey, 0, aeskey.Length);
         NoncePieces.Add(request.Miner, new List <FiniteFieldPoint>());
         NoncePieces[request.Miner].Add(FiniteFieldPoint.DeserializeFrom(piece));
         NonceHashes.Add(request.Miner, request.NonceHash);
         TransactionHashes.UnionWith(request.TransactionHashes);
         action_request.CheckPredicate();
     }
 }
Exemple #2
0
        public void Reset()
        {
            if (PrevHash == Blockchain.Default.CurrentBlockHash)
            {
                return;
            }
            Random rand = new Random();

            byte[] nonce = new byte[32];
            rand.NextBytes(nonce);
            lock (SyncRoot)
            {
                PrevHash = Blockchain.Default.CurrentBlockHash;
                Miners   = Blockchain.Default.GetMiners();
                Nonces.Clear();
                Nonces.Add(my_pubkey, new UInt256(nonce));
                NonceHashes.Clear();
                NonceHashes.Add(my_pubkey, new UInt256(nonce.Sha256()));
                NoncePieces.Clear();
                TransactionHashes.Clear();
                TransactionHashes.UnionWith(Blockchain.Default.GetMemoryPool().Select(p => p.Hash));
                index_me = Array.IndexOf(Miners, my_pubkey);
                _request = null;
                if (action_request != null)
                {
                    action_request.Reset();
                }
            }
        }
 public BlockConsensusRequest CreateRequest(MinerWallet wallet)
 {
     lock (SyncRoot)
     {
         if (!Valid) throw new InvalidOperationException();
         if (request == null)
         {
             request = new BlockConsensusRequest
             {
                 PrevHash = PrevHash,
                 Miner = my_pubkey,
                 NonceHash = NonceHashes[my_pubkey],
                 TransactionHashes = TransactionHashes.ToArray()
             };
             SplitSecret secret = SecretSharing.Split(Nonces[my_pubkey].ToArray(), (Miners.Length - 1) / 2 + 1);
             for (int i = 0; i < Miners.Length; i++)
             {
                 if (Miners[i].Equals(my_pubkey)) continue;
                 byte[] aeskey = wallet.GetAesKey(Miners[i]);
                 byte[] iv = aeskey.Take(16).ToArray();
                 byte[] piece = secret.GetShare(i + 1).ToArray();
                 using (AesManaged aes = new AesManaged())
                 using (ICryptoTransform encryptor = aes.CreateEncryptor(aeskey, iv))
                 {
                     piece = encryptor.TransformFinalBlock(piece, 0, piece.Length);
                 }
                 Array.Clear(aeskey, 0, aeskey.Length);
                 Array.Clear(iv, 0, iv.Length);
                 request.NoncePieces.Add(Miners[i], piece);
             }
         }
         return request;
     }
 }
Exemple #4
0
 private async Task SendConsensusRequestAsync()
 {
     if (!context.Valid)
     {
         return;
     }
     BlockConsensusRequest request = context.CreateRequest(wallet);
     //TODO: 签名
     //request.Script = wallet.Sign(request);
     await localnode.RelayAsync(request);
 }
Exemple #5
0
        private void LocalNode_NewInventory(object sender, Inventory inventory)
        {
            if (inventory.InventoryType != InventoryType.ConsRequest)
            {
                return;
            }
            BlockConsensusRequest request = (BlockConsensusRequest)inventory;

            if (!request.Verify())
            {
                return;
            }
            context.AddRequest(request, wallet);
        }
Exemple #6
0
 public BlockConsensusRequest CreateRequest(MinerWallet wallet)
 {
     lock (SyncRoot)
     {
         if (!Valid)
         {
             throw new InvalidOperationException();
         }
         if (_request == null)
         {
             _request = new BlockConsensusRequest
             {
                 PrevHash          = PrevHash,
                 Miner             = my_pubkey,
                 IV                = new byte[16],
                 NonceHash         = NonceHashes[my_pubkey],
                 TransactionHashes = TransactionHashes.ToArray()
             };
             Random rand = new Random();
             rand.NextBytes(_request.IV);
             SplitSecret secret = SecretSharing.Split(Nonces[my_pubkey].ToArray(), (Miners.Length - 1) / 2 + 1);
             for (int i = 0; i < Miners.Length; i++)
             {
                 if (Miners[i].Equals(my_pubkey))
                 {
                     continue;
                 }
                 byte[] aeskey = wallet.GetAesKey(Miners[i]);
                 byte[] piece  = secret.GetShare(i + 1).ToArray();
                 using (AesManaged aes = new AesManaged())
                     using (ICryptoTransform encryptor = aes.CreateEncryptor(aeskey, _request.IV))
                     {
                         piece = encryptor.TransformFinalBlock(piece, 0, piece.Length);
                     }
                 Array.Clear(aeskey, 0, aeskey.Length);
                 _request.NoncePieces.Add(Miners[i], piece);
             }
         }
         return(_request);
     }
 }
 public void AddRequest(BlockConsensusRequest request, MinerWallet wallet)
 {
     lock (SyncRoot)
     {
         if (request.PrevHash != PrevHash) return;
         if (request.Miner == my_pubkey) return;
         if (NoncePieces.ContainsKey(request.Miner)) return;
         byte[] aeskey = wallet.GetAesKey(request.Miner);
         byte[] piece = request.NoncePieces[my_pubkey];
         using (AesManaged aes = new AesManaged())
         using (ICryptoTransform decryptor = aes.CreateDecryptor(aeskey, request.IV))
         {
             piece = decryptor.TransformFinalBlock(piece, 0, piece.Length);
         }
         Array.Clear(aeskey, 0, aeskey.Length);
         NoncePieces.Add(request.Miner, new List<FiniteFieldPoint>());
         NoncePieces[request.Miner].Add(FiniteFieldPoint.DeserializeFrom(piece));
         NonceHashes.Add(request.Miner, request.NonceHash);
         TransactionHashes.UnionWith(request.TransactionHashes);
         action_request.CheckPredicate();
     }
 }
 public void Reset()
 {
     if (PrevHash == Blockchain.Default.CurrentBlockHash)
         return;
     Random rand = new Random();
     byte[] nonce = new byte[32];
     rand.NextBytes(nonce);
     lock (SyncRoot)
     {
         PrevHash = Blockchain.Default.CurrentBlockHash;
         Miners = Blockchain.Default.GetMiners();
         Nonces.Clear();
         Nonces.Add(my_pubkey, new UInt256(nonce));
         NonceHashes.Clear();
         NonceHashes.Add(my_pubkey, new UInt256(nonce.Sha256()));
         NoncePieces.Clear();
         TransactionHashes.Clear();
         TransactionHashes.UnionWith(Blockchain.Default.GetMemoryPool().Select(p => p.Hash));
         index_me = Array.IndexOf(Miners, my_pubkey);
         _request = null;
         if (action_request != null)
             action_request.Reset();
     }
 }