Exemplo n.º 1
0
        public void TestRpcInvokeResult()
        {
            JObject json = TestUtils.RpcTestCases.Find(p => p.Name == nameof(RpcClient.InvokeFunctionAsync).ToLower()).Response.Result;
            var     item = RpcInvokeResult.FromJson(json);

            Assert.AreEqual(json.ToString(), item.ToJson().ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create an unsigned Transaction object with given parameters.
        /// </summary>
        /// <param name="script">Transaction Script</param>
        /// <param name="attributes">Transaction Attributes</param>
        /// <returns></returns>
        public TransactionManager MakeTransaction(byte[] script, TransactionAttribute[] attributes = null)
        {
            var  random = new Random();
            uint height = rpcClient.GetBlockCount() - 1;

            Tx = new Transaction
            {
                Version         = 0,
                Nonce           = (uint)random.Next(),
                Script          = script,
                Sender          = sender,
                ValidUntilBlock = height + Transaction.MaxValidUntilBlockIncrement,
                Attributes      = attributes ?? Array.Empty <TransactionAttribute>(),
                Witnesses       = Array.Empty <Witness>()
            };

            // Add witness hashes parameter to pass CheckWitness
            UInt160[]       hashes = Tx.GetScriptHashesForVerifying(null);
            RpcInvokeResult result = rpcClient.InvokeScript(script, hashes);

            Tx.SystemFee = long.Parse(result.GasConsumed);
            context      = new ContractParametersContext(Tx);
            signStore    = new List <SignItem>();

            return(this);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns the result after passing a script through the VM.
        /// This RPC call does not affect the blockchain in any way.
        /// </summary>
        public RpcInvokeResult InvokeScript(byte[] script, params UInt160[] scriptHashesForVerifying)
        {
            List <JObject> parameters = new List <JObject>
            {
                script.ToHexString()
            };

            parameters.AddRange(scriptHashesForVerifying.Select(p => (JObject)p.ToString()));
            return(RpcInvokeResult.FromJson(RpcSend("invokescript", parameters.ToArray())));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns the result after calling a smart contract at scripthash with the given operation and parameters.
        /// This RPC call does not affect the blockchain in any way.
        /// </summary>
        public RpcInvokeResult InvokeFunction(string scriptHash, string operation, RpcStack[] stacks, params UInt160[] scriptHashesForVerifying)
        {
            List <JObject> parameters = new List <JObject> {
                scriptHash, operation, stacks.Select(p => p.ToJson()).ToArray()
            };

            if (scriptHashesForVerifying.Length > 0)
            {
                parameters.Add(scriptHashesForVerifying.Select(p => (JObject)p.ToString()).ToArray());
            }
            return(RpcInvokeResult.FromJson(RpcSend("invokefunction", parameters.ToArray())));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Returns the result after calling a smart contract at scripthash with the given operation and parameters.
        /// This RPC call does not affect the blockchain in any way.
        /// </summary>
        public async Task <RpcInvokeResult> InvokeFunctionAsync(string scriptHash, string operation, RpcStack[] stacks, params Signer[] signer)
        {
            List <JObject> parameters = new() { scriptHash.AsScriptHash(), operation, stacks.Select(p => p.ToJson()).ToArray() };

            if (signer.Length > 0)
            {
                parameters.Add(signer.Select(p => p.ToJson()).ToArray());
            }
            var result = await RpcSendAsync(GetRpcName(), parameters.ToArray()).ConfigureAwait(false);

            return(RpcInvokeResult.FromJson(result));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Returns the result after passing a script through the VM.
        /// This RPC call does not affect the blockchain in any way.
        /// </summary>
        public RpcInvokeResult InvokeScript(byte[] script, params Signer[] signers)
        {
            List <JObject> parameters = new List <JObject> {
                script.ToHexString()
            };

            if (signers.Length > 0)
            {
                parameters.Add(signers.Select(p => p.ToJson()).ToArray());
            }
            return(RpcInvokeResult.FromJson(RpcSend("invokescript", parameters.ToArray())));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Returns the result after passing a script through the VM.
        /// This RPC call does not affect the blockchain in any way.
        /// </summary>
        public async Task <RpcInvokeResult> InvokeScriptAsync(byte[] script, params Signer[] signers)
        {
            List <JObject> parameters = new() { Convert.ToBase64String(script) };

            if (signers.Length > 0)
            {
                parameters.Add(signers.Select(p => p.ToJson()).ToArray());
            }
            var result = await RpcSendAsync(GetRpcName(), parameters.ToArray()).ConfigureAwait(false);

            return(RpcInvokeResult.FromJson(result));
        }
Exemplo n.º 8
0
        public static void MockInvokeScript(Mock <RpcClient> mockClient, byte[] script, params ContractParameter[] parameters)
        {
            var result = new RpcInvokeResult()
            {
                Stack       = parameters,
                GasConsumed = "100",
                Script      = script.ToHexString(),
                State       = VMState.HALT
            };

            mockClient.Setup(p => p.RpcSend("invokescript", It.Is <JObject[]>(j => j[0].AsString() == script.ToHexString())))
            .Returns(result.ToJson())
            .Verifiable();
        }
        public static void MockInvokeScript(Mock <RpcClient> mockClient, byte[] script, params ContractParameter[] parameters)
        {
            var result = new RpcInvokeResult()
            {
                Stack       = parameters.Select(p => p.ToStackItem()).ToArray(),
                GasConsumed = "100",
                Script      = Convert.ToBase64String(script),
                State       = VMState.HALT
            };

            mockClient.Setup(p => p.RpcSendAsync("invokescript", It.Is <JObject[]>(j => j[0].AsString() == Convert.ToBase64String(script))))
            .ReturnsAsync(result.ToJson())
            .Verifiable();
        }
Exemplo n.º 10
0
        /// <summary>
        /// Create an unsigned Transaction object with given parameters.
        /// </summary>
        /// <param name="script">Transaction Script</param>
        /// <param name="attributes">Transaction Attributes</param>
        /// <param name="cosigners">Transaction Cosigners</param>
        /// <param name="networkFee">Transaction NetworkFee, will set to estimate value(with only basic signatures) when networkFee is 0</param>
        /// <returns></returns>
        public TransactionManager MakeTransaction(byte[] script, TransactionAttribute[] attributes = null, Cosigner[] cosigners = null, long networkFee = 0)
        {
            var  random = new Random();
            uint height = rpcClient.GetBlockCount() - 1;

            Tx = new Transaction
            {
                Version         = 0,
                Nonce           = (uint)random.Next(),
                Script          = script,
                Sender          = sender,
                ValidUntilBlock = height + Transaction.MaxValidUntilBlockIncrement,
                Attributes      = attributes ?? new TransactionAttribute[0],
                Cosigners       = cosigners ?? new Cosigner[0],
                Witnesses       = new Witness[0]
            };

            // Add witness hashes parameter to pass CheckWitness
            UInt160[]       hashes = Tx.GetScriptHashesForVerifying(null);
            RpcInvokeResult result = rpcClient.InvokeScript(script, hashes);

            Tx.SystemFee = Math.Max(long.Parse(result.GasConsumed) - ApplicationEngine.GasFree, 0);
            if (Tx.SystemFee > 0)
            {
                long d         = (long)NativeContract.GAS.Factor;
                long remainder = Tx.SystemFee % d;
                if (remainder > 0)
                {
                    Tx.SystemFee += d - remainder;
                }
                else if (remainder < 0)
                {
                    Tx.SystemFee -= remainder;
                }
            }

            context = new ContractParametersContext(Tx);

            // set networkfee to estimate value when networkFee is 0
            Tx.NetworkFee = networkFee == 0 ? EstimateNetworkFee() : networkFee;

            var gasBalance = nep5API.BalanceOf(NativeContract.GAS.Hash, sender);

            if (gasBalance >= Tx.SystemFee + Tx.NetworkFee)
            {
                return(this);
            }
            throw new InvalidOperationException($"Insufficient GAS in address: {sender.ToAddress()}");
        }
        /// <summary>
        /// Create an unsigned Transaction object with given parameters.
        /// </summary>
        /// <param name="script">Transaction Script</param>
        /// <param name="attributes">Transaction Attributes</param>
        /// <returns></returns>
        public async Task <TransactionManager> MakeTransactionAsync(byte[] script, Signer[] signers = null, TransactionAttribute[] attributes = null)
        {
            uint blockCount = await rpcClient.GetBlockCountAsync().ConfigureAwait(false) - 1;

            RpcInvokeResult invokeResult = await rpcClient.InvokeScriptAsync(script, signers).ConfigureAwait(false);

            var tx = new Transaction
            {
                Version         = 0,
                Nonce           = (uint)new Random().Next(),
                Script          = script,
                Signers         = signers ?? Array.Empty <Signer>(),
                ValidUntilBlock = blockCount - 1 + Transaction.MaxValidUntilBlockIncrement,
                SystemFee       = invokeResult.GasConsumed,
                Attributes      = attributes ?? Array.Empty <TransactionAttribute>(),
            };

            return(new TransactionManager(tx, rpcClient));
        }
Exemplo n.º 12
0
        public Task <RpcInvokeResult> InvokeAsync(Neo.VM.Script script)
        {
            if (disposedValue)
            {
                throw new ObjectDisposedException(nameof(OfflineNode));
            }

            using ApplicationEngine engine = ApplicationEngine.Run(script, container: null, gas: 20000000L);
            var result = new RpcInvokeResult()
            {
                State       = engine.State,
                Exception   = engine.FaultException?.GetBaseException().Message ?? string.Empty,
                GasConsumed = engine.GasConsumed,
                Stack       = engine.ResultStack.ToArray(),
                Script      = string.Empty,
                Tx          = string.Empty
            };

            return(Task.FromResult(result));
        }
Exemplo n.º 13
0
        /// <summary>
        /// Create an unsigned Transaction object with given parameters.
        /// </summary>
        /// <param name="script">Transaction Script</param>
        /// <param name="attributes">Transaction Attributes</param>
        /// <param name="cosigners">Transaction Cosigners</param>
        /// <returns></returns>
        public TransactionManager MakeTransaction(byte[] script, TransactionAttribute[] attributes = null, Cosigner[] cosigners = null)
        {
            var  random = new Random();
            uint height = rpcClient.GetBlockCount() - 1;

            Tx = new Transaction
            {
                Version         = 0,
                Nonce           = (uint)random.Next(),
                Script          = script,
                Sender          = sender,
                ValidUntilBlock = height + Transaction.MaxValidUntilBlockIncrement,
                Attributes      = attributes ?? Array.Empty <TransactionAttribute>(),
                Cosigners       = cosigners ?? Array.Empty <Cosigner>(),
                Witnesses       = Array.Empty <Witness>()
            };

            // Add witness hashes parameter to pass CheckWitness
            UInt160[]       hashes = Tx.GetScriptHashesForVerifying(null);
            RpcInvokeResult result = rpcClient.InvokeScript(script, hashes);

            Tx.SystemFee = Math.Max(long.Parse(result.GasConsumed) - ApplicationEngine.GasFree, 0);
            if (Tx.SystemFee > 0)
            {
                long d         = (long)NativeContract.GAS.Factor;
                long remainder = Tx.SystemFee % d;
                if (remainder > 0)
                {
                    Tx.SystemFee += d - remainder;
                }
                else if (remainder < 0)
                {
                    Tx.SystemFee -= remainder;
                }
            }

            context   = new ContractParametersContext(Tx);
            signStore = new List <SignItem>();

            return(this);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Create an unsigned Transaction object with given parameters.
        /// </summary>
        /// <param name="script">Transaction Script</param>
        /// <param name="attributes">Transaction Attributes</param>
        /// <returns></returns>
        public TransactionManager MakeTransaction(byte[] script, Signer[] signers = null, TransactionAttribute[] attributes = null)
        {
            var  random = new Random();
            uint height = rpcClient.GetBlockCount() - 1;

            Tx = new Transaction
            {
                Version         = 0,
                Nonce           = (uint)random.Next(),
                Script          = script,
                Signers         = signers,
                ValidUntilBlock = height + Transaction.MaxValidUntilBlockIncrement,
                Attributes      = attributes ?? Array.Empty <TransactionAttribute>(),
            };

            RpcInvokeResult result = rpcClient.InvokeScript(script, signers);

            Tx.SystemFee = long.Parse(result.GasConsumed);
            context      = new ContractParametersContext(Tx);
            signStore    = new List <SignItem>();

            return(this);
        }
Exemplo n.º 15
0
 /// <summary>
 /// Returns the result after calling a smart contract at scripthash with the given operation and parameters.
 /// This RPC call does not affect the blockchain in any way.
 /// </summary>
 public RpcInvokeResult InvokeFunction(string scriptHash, string operation, RpcStack[] stacks)
 {
     return(RpcInvokeResult.FromJson(RpcSend("invokefunction", scriptHash, operation, stacks.Select(p => p.ToJson()).ToArray())));
 }
Exemplo n.º 16
0
 /// <summary>
 /// Returns the result after calling a smart contract at scripthash with the given operation and parameters.
 /// This RPC call does not affect the blockchain in any way.
 /// </summary>
 public RpcInvokeResult InvokeFunction(string address, string function, RpcStack[] stacks)
 {
     return(RpcInvokeResult.FromJson(RpcSend("invokefunction", address, function, stacks.Select(p => p.ToJson()).ToArray())));
 }
Exemplo n.º 17
0
 /// <summary>
 /// Returns the result after passing a script through the VM.
 /// This RPC call does not affect the blockchain in any way.
 /// </summary>
 public RpcInvokeResult InvokeScript(string script)
 {
     return(RpcInvokeResult.FromJson(RpcSend("invokescript", script)));
 }
Exemplo n.º 18
0
 /// <summary>
 /// Returns the result after passing a script through the VM.
 /// This RPC call does not affect the blockchain in any way.
 /// </summary>
 public RpcInvokeResult InvokeScript(byte[] script)
 {
     return(RpcInvokeResult.FromJson(RpcSend("invokescript", script.ToHexString())));
 }
        /// <summary>
        /// Create an unsigned Transaction object with given parameters.
        /// </summary>
        /// <param name="script">Transaction Script</param>
        /// <param name="attributes">Transaction Attributes</param>
        /// <returns></returns>
        public async Task <TransactionManager> MakeTransactionAsync(byte[] script, Signer[] signers = null, TransactionAttribute[] attributes = null)
        {
            RpcInvokeResult invokeResult = await rpcClient.InvokeScriptAsync(script, signers).ConfigureAwait(false);

            return(await MakeTransactionAsync(script, invokeResult.GasConsumed, signers, attributes).ConfigureAwait(false));
        }