Пример #1
0
        public void Test_SignRoundTrip()
        {
            var privateKey = "0xD95D6DB65F3E2223703C5D8E205D98E3E6B470F067B0F94F6C6BF73D4301CE48".HexToBytes();
            var publicKey  = Crypto.ComputePublicKey(privateKey, true);
            var address    = "0x6Bc32575ACb8754886dC283c2c8ac54B1Bd93195".HexToBytes();

            CollectionAssert.AreEqual(address, Crypto.ComputeAddress(publicKey));

            var       startTs = TimeUtils.CurrentTimeMillis();
            const int n       = 100;

            for (var it = 0; it < n; ++it)
            {
                // var message = "0xdeadbeef" + it.ToString("X4");
                var message =
                    "0xec808504a817c800825208948e7b7262e0fa4616566591d51f998f16a79fb547880de0b6b3a764000080018080";
                var digest = message.HexToBytes();
                // using old chain id
                var signature = Crypto.Sign(digest, privateKey, false);
                Assert.IsTrue(Crypto.VerifySignature(digest, signature, publicKey, false));
                var recoveredPubkey = Crypto.RecoverSignature(digest, signature, false);
                Assert.AreEqual(recoveredPubkey, publicKey);

                // using new chain id
                signature = Crypto.Sign(digest, privateKey, true);
                Assert.IsTrue(Crypto.VerifySignature(digest, signature, publicKey, true));
                recoveredPubkey = Crypto.RecoverSignature(digest, signature, true);
                Assert.AreEqual(recoveredPubkey, publicKey);
            }

            var endTs = TimeUtils.CurrentTimeMillis();

            Console.WriteLine($"Full sign + recover time: {endTs - startTs}ms");
            Console.WriteLine($"Per 1 iteration: {(double) (endTs - startTs) / n}ms");
        }
Пример #2
0
        public void VerifyTransaction(TransactionReceipt acceptedTransaction, ECDSAPublicKey publicKey, bool useNewChainId)
        {
            var address = _crypto.ComputeAddress(publicKey.EncodeCompressed()).ToUInt160();

            _publicKeyCache.Add(address, publicKey);
            VerifyTransaction(acceptedTransaction, useNewChainId);
        }
Пример #3
0
 public static UInt160 PublicKeyToAddress(byte[] publicKey)
 {
     return(Crypto.ComputeAddress(publicKey).ToUInt160());
 }
Пример #4
0
        public JObject GetValidatorInfo(string publicKeyStr)
        {
            var publicKey      = publicKeyStr.HexToBytes();
            var addressUint160 = Crypto.ComputeAddress(publicKey).ToUInt160();

            var balance = _stateManager.CurrentSnapshot.Balances.GetBalance(addressUint160);

            var stake   = _systemContractReader.GetStake(addressUint160).ToMoney();
            var penalty = _systemContractReader.GetPenalty(addressUint160).ToMoney();

            var isNextValidator     = _systemContractReader.IsNextValidator(publicKey);
            var isAbleToBeValidator = _systemContractReader.IsAbleToBeValidator(addressUint160);
            var isPreviousValidator = _systemContractReader.IsPreviousValidator(publicKey);
            var isCurrentValidator  = _stateManager.CurrentSnapshot.Validators
                                      .GetValidatorsPublicKeys().Any(pk =>
                                                                     pk.Buffer.ToByteArray().SequenceEqual(publicKey));

            var isAbleToBeStaker = balance.ToWei() > StakingContract.TokenUnitsInRoll;
            var isStaker         = !_systemContractReader.GetStake(addressUint160).IsZero();

            bool stakeDelegated = !isStaker && isCurrentValidator;

            string state;

            if (isCurrentValidator)
            {
                state = "Validator";
            }
            else if (isNextValidator)
            {
                state = "NextValidator";
            }
            else if (isAbleToBeValidator)
            {
                state = "AbleToBeValidator";
            }
            else if (isPreviousValidator)
            {
                state = "PreviousValidator";
            }
            else if (isAbleToBeStaker)
            {
                state = "AbleToBeStaker";
            }
            else
            {
                state = "Newbie";
            }

            return(new JObject
            {
                ["address"] = addressUint160.ToHex(),
                ["publicKey"] = publicKey.ToHex(),
                ["balance"] = balance.ToString(),
                ["stake"] = stake.ToString(),
                ["penalty"] = penalty.ToString(),
                ["state"] = state,
                ["stakeDelegated"] = stakeDelegated.ToString(),
                ["staker"] = isStaker
            });
        }
Пример #5
0
 public static UInt160 GetAddress(this ECDSAPublicKey key)
 {
     return(Crypto.ComputeAddress(key.EncodeCompressed()).ToUInt160());
 }
Пример #6
0
        public void Start(RunOptions options)
        {
            _blockManager       = _container.Resolve <IBlockManager>();
            _configManager      = _container.Resolve <IConfigManager>();
            _stateManager       = _container.Resolve <IStateManager>();
            _transactionBuilder = _container.Resolve <ITransactionBuilder>();
            _transactionPool    = _container.Resolve <ITransactionPool>();
            _transactionSigner  = _container.Resolve <ITransactionSigner>();
            _wallet             = _container.Resolve <IPrivateWallet>();

            var keyPair = _wallet.EcdsaKeyPair;

            Console.WriteLine("-------------------------------");
            Console.WriteLine("Private Key: " + keyPair.PrivateKey.Encode().ToHex());
            Console.WriteLine("Public Key: " + keyPair.PublicKey.EncodeCompressed().ToHex());
            Console.WriteLine("Address: " + Crypto.ComputeAddress(keyPair.PublicKey.EncodeCompressed()).ToHex());
            Console.WriteLine("-------------------------------");

            if (_blockManager.TryBuildGenesisBlock())
            {
                Console.WriteLine("Generated genesis block");
            }

            var genesisBlock = _stateManager.LastApprovedSnapshot.Blocks.GetBlockByHeight(0);

            Console.WriteLine("Genesis Block: " + genesisBlock !.Hash.ToHex());
            Console.WriteLine($" + prevBlockHash: {genesisBlock.Header.PrevBlockHash.ToHex()}");
            Console.WriteLine($" + merkleRoot: {genesisBlock.Header.MerkleRoot.ToHex()}");
            Console.WriteLine($" + nonce: {genesisBlock.Header.Nonce}");
            Console.WriteLine($" + transactionHashes: {genesisBlock.TransactionHashes.Count}");
            foreach (var s in genesisBlock.TransactionHashes)
            {
                Console.WriteLine($" + - {s.ToHex()}");
            }
            Console.WriteLine($" + hash: {genesisBlock.Hash.ToHex()}");

            var address1 = "0xe3c7a20ee19c0107b9121087bcba18eb4dcb8576".HexToUInt160();
            var address2 = "0x6bc32575acb8754886dc283c2c8ac54b1bd93195".HexToUInt160();

            Console.WriteLine("-------------------------------");
            // Console.WriteLine("Current block header height: " + blockchainContext.CurrentBlockHeight);
            Console.WriteLine("-------------------------------");
            Console.WriteLine("Balance of LA 0x3e: " +
                              _stateManager.LastApprovedSnapshot.Balances.GetBalance(address1));
            Console.WriteLine("Balance of LA 0x6b: " +
                              _stateManager.LastApprovedSnapshot.Balances.GetBalance(address2));
            Console.WriteLine("-------------------------------");

            Logger.LogInformation($"Setting chainId");
            var chainId    = _configManager.GetConfig <NetworkConfig>("network")?.ChainId ?? 42;
            var newChainId = _configManager.GetConfig <NetworkConfig>("network")?.NewChainId ?? chainId;

            TransactionUtils.SetChainId((int)chainId, newChainId);

            // _BenchTxProcessing(_transactionBuilder, _transactionSigner, keyPair);
            // _BenchOneTxInBlock(_transactionBuilder, _transactionSigner, keyPair);

            Console.WriteLine("---------------START - TX POOL BENCHMARK----------------");
            _Bench_Tx_Pool(_transactionBuilder, _transactionSigner, keyPair);
            Console.WriteLine("---------------END - TX POOL BENCHMARK----------------");

            Console.WriteLine("---------------START - BLOCK EMULATE BENCHMARK----------------");
            _Bench_Emulate_Block(_transactionBuilder, _transactionSigner, keyPair);
            Console.WriteLine("---------------END - BLOCK EMULATE BENCHMARK----------------");

            Console.WriteLine("---------------START - BLOCK EMULATE + EXECUTE BENCHMARK----------------");
            _Bench_Emulate_Execute_Tx(_transactionBuilder, _transactionSigner, keyPair);
            Console.WriteLine("---------------END - BLOCK EMULATE + EXECUTE BENCHMARK----------------");

            Console.WriteLine("---------------START - MULTIPLE BLOCKS EMULATE + EXECUTE BENCHMARK----------------");
            _Bench_Execute_Blocks(_transactionBuilder, _transactionSigner, keyPair);
            Console.WriteLine("---------------END - MULTIPLE BLOCKS EMULATE + EXECUTE BENCHMARK----------------");

            Environment.Exit(0);
        }