예제 #1
0
        private static bool Transaction_GetReferences(IBlockchainProvider provider, ExecutionEngine engine)
        {
            var tx = GetInteropFromStack <Transaction>(engine);

            if (tx == null)
            {
                return(false);
            }

            var items = new List <StackItem>();

            var references = new List <Transaction.Output>();

            foreach (var input in tx.inputs)
            {
                var other_tx = provider.GetTransaction(input.prevHash);
                references.Add(other_tx.outputs[input.prevIndex]);
            }

            foreach (var reference in references)
            {
                items.Add(new VM.Types.InteropInterface(reference));
            }

            var result = new VM.Types.Array(items.ToArray <StackItem>());

            engine.CurrentContext.EvaluationStack.Push(result);

            return(true);
        }
예제 #2
0
파일: UT_Helper.cs 프로젝트: yongjiema/neo
        public void TestToParameter()
        {
            StackItem byteItem = "00e057eb481b".HexToBytes();

            Assert.AreEqual(30000000000000L, (long)new BigInteger(byteItem.ToParameter().Value as byte[]));

            StackItem boolItem = false;

            Assert.AreEqual(false, (bool)boolItem.ToParameter().Value);

            StackItem intItem = new BigInteger(1000);

            Assert.AreEqual(1000, (BigInteger)intItem.ToParameter().Value);

            StackItem interopItem = new VM.Types.InteropInterface <string>("test");

            Assert.AreEqual(null, interopItem.ToParameter().Value);

            StackItem arrayItem = new VM.Types.Array(new[] { byteItem, boolItem, intItem, interopItem });

            Assert.AreEqual(1000, (BigInteger)(arrayItem.ToParameter().Value as List <ContractParameter>)[2].Value);

            StackItem mapItem = new VM.Types.Map(new Dictionary <StackItem, StackItem>(new[] { new KeyValuePair <StackItem, StackItem>(byteItem, intItem) }));

            Assert.AreEqual(1000, (BigInteger)(mapItem.ToParameter().Value as List <KeyValuePair <ContractParameter, ContractParameter> >)[0].Value.Value);
        }
예제 #3
0
        public void TestContract_CallEx()
        {
            var snapshot = Blockchain.Singleton.GetSnapshot();

            string method = "method";
            var    args   = new VM.Types.Array {
                0, 1
            };
            var state = TestUtils.GetContract(method, args.Count);

            state.Manifest.Features = ContractFeatures.HasStorage;
            snapshot.Contracts.Add(state.ScriptHash, state);


            foreach (var flags in new CallFlags[] { CallFlags.None, CallFlags.AllowCall, CallFlags.AllowModifyStates, CallFlags.All })
            {
                var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);
                engine.LoadScript(new byte[] { 0x01 });

                engine.CallContractEx(state.ScriptHash, method, args, CallFlags.All);
                engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[0]);
                engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[1]);

                // Contract doesn't exists
                Assert.ThrowsException <InvalidOperationException>(() => engine.CallContractEx(UInt160.Zero, method, args, CallFlags.All));

                // Call with rights
                engine.CallContractEx(state.ScriptHash, method, args, flags);
                engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[0]);
                engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[1]);
            }
        }
예제 #4
0
        public void TestContract_Call()
        {
            var    snapshot = TestBlockchain.GetTestSnapshot();
            string method   = "method";
            var    args     = new VM.Types.Array {
                0, 1
            };
            var state = TestUtils.GetContract(method, args.Count);

            snapshot.AddContract(state.Hash, state);
            var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);

            engine.LoadScript(new byte[] { 0x01 });

            engine.CallContract(state.Hash, method, CallFlags.All, args);
            engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[0]);
            engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[1]);

            state.Manifest.Permissions[0].Methods = WildcardContainer <string> .Create("a");

            Assert.ThrowsException <InvalidOperationException>(() => engine.CallContract(state.Hash, method, CallFlags.All, args));

            state.Manifest.Permissions[0].Methods = WildcardContainer <string> .CreateWildcard();

            engine.CallContract(state.Hash, method, CallFlags.All, args);

            Assert.ThrowsException <InvalidOperationException>(() => engine.CallContract(UInt160.Zero, method, CallFlags.All, args));
        }
예제 #5
0
        public bool GetTransactions(ExecutionEngine engine)
        {
            var obj   = engine.EvaluationStack.Pop();
            var block = ((VM.Types.InteropInterface)obj).GetInterface <Block>();

            if (block == null)
            {
                return(false);
            }

            // returns Transaction[]

            var txs = new StackItem[block._transactions.Count];

            int index = 0;

            foreach (var tx in block.Transactions)
            {
                txs[index] = new VM.Types.InteropInterface(tx);
                index++;
            }

            var array = new VM.Types.Array(txs);

            throw new NotImplementedException();
        }
예제 #6
0
        public void Serialize_EmptyArray()
        {
            var entry = new VM.Types.Array();
            var json  = JsonSerializer.Serialize(entry).ToString();

            Assert.AreEqual(json, "[]");
        }
예제 #7
0
        public static bool GetReferences(ExecutionEngine engine)
        {
            var obj = engine.EvaluationStack.Pop() as VM.Types.InteropInterface;

            if (obj == null)
            {
                return(false);
            }

            var debugger = obj.GetInterface <NeoDebugger>();

            var transactions = new List <StackItem>();

            foreach (var entry in debugger.transaction)
            {
                var tx = new TransactionOutput()
                {
                    ammount = entry.ammount, id = entry.id
                };
                transactions.Add(new VM.Types.InteropInterface(tx));
            }

            var outputs = new VM.Types.Array(transactions.ToArray <StackItem>());

            engine.EvaluationStack.Push(outputs);

            return(true);
        }
예제 #8
0
        public void HashCodeTest()
        {
            StackItem itemA = "NEO";
            StackItem itemB = "NEO";
            StackItem itemC = "SmartEconomy";

            Assert.IsTrue(itemA.GetHashCode() == itemB.GetHashCode());
            Assert.IsTrue(itemA.GetHashCode() != itemC.GetHashCode());

            itemA = new VM.Types.Buffer(1);
            itemB = new VM.Types.Buffer(1);

            Assert.IsTrue(itemA.GetHashCode() != itemB.GetHashCode());

            itemA = true;
            itemB = true;
            itemC = false;

            Assert.IsTrue(itemA.GetHashCode() == itemB.GetHashCode());
            Assert.IsTrue(itemA.GetHashCode() != itemC.GetHashCode());

            itemA = 1;
            itemB = 1;
            itemC = 123;

            Assert.IsTrue(itemA.GetHashCode() == itemB.GetHashCode());
            Assert.IsTrue(itemA.GetHashCode() != itemC.GetHashCode());

            itemA = new Null();
            itemB = new Null();

            Assert.IsTrue(itemA.GetHashCode() == itemB.GetHashCode());

            itemA = new VM.Types.Array();

            Assert.ThrowsException <System.NotSupportedException>(() => itemA.GetHashCode());

            itemA = new Struct();

            Assert.ThrowsException <System.NotSupportedException>(() => itemA.GetHashCode());

            itemA = new Map();

            Assert.ThrowsException <System.NotSupportedException>(() => itemA.GetHashCode());

            itemA = new InteropInterface(123);
            itemB = new InteropInterface(123);

            Assert.IsTrue(itemA.GetHashCode() == itemB.GetHashCode());

            var script = new Script(new byte[0]);

            itemA = new Pointer(script, 123);
            itemB = new Pointer(script, 123);
            itemC = new Pointer(script, 1234);

            Assert.IsTrue(itemA.GetHashCode() == itemB.GetHashCode());
            Assert.IsTrue(itemA.GetHashCode() != itemC.GetHashCode());
        }
예제 #9
0
        public void Serialize_Number()
        {
            var entry = new VM.Types.Array {
                1, 9007199254740992
            };

            Assert.ThrowsException <InvalidOperationException>(() => JsonSerializer.Serialize(entry));
        }
예제 #10
0
파일: UT_Helper.cs 프로젝트: jz263/neo
        private void TestToParaMeter2VMArray()
        {
            VM.Types.Array    item      = new VM.Types.Array();
            ContractParameter parameter = VM.Helper.ToParameter(item);

            Assert.AreEqual(ContractParameterType.Array, parameter.Type);
            Assert.AreEqual(0, ((List <ContractParameter>)parameter.Value).Count);
        }
예제 #11
0
        public void TestSerialize()
        {
            byte[] result1 = BinarySerializer.Serialize(new byte[5], MaxItemSize);
            byte[] expectedArray1 = new byte[] {
                        0x28, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00
                    };
            Assert.AreEqual(Encoding.Default.GetString(expectedArray1), Encoding.Default.GetString(result1));

            byte[] result2 = BinarySerializer.Serialize(true, MaxItemSize);
            byte[] expectedArray2 = new byte[] {
                        0x20, 0x01
                    };
            Assert.AreEqual(Encoding.Default.GetString(expectedArray2), Encoding.Default.GetString(result2));

            byte[] result3 = BinarySerializer.Serialize(1, MaxItemSize);
            byte[] expectedArray3 = new byte[] {
                        0x21, 0x01, 0x01
                    };
            Assert.AreEqual(Encoding.Default.GetString(expectedArray3), Encoding.Default.GetString(result3));

            StackItem stackItem4 = new InteropInterface(new object());
            Action action4 = () => BinarySerializer.Serialize(stackItem4, MaxItemSize);
            action4.Should().Throw<NotSupportedException>();

            List<StackItem> list6 = new List<StackItem> { 1 };
            StackItem stackItem62 = new VM.Types.Array(list6);
            byte[] result6 = BinarySerializer.Serialize(stackItem62, MaxItemSize);
            byte[] expectedArray6 = new byte[] {
                        0x40,0x01,0x21,0x01,0x01
                    };
            Assert.AreEqual(Encoding.Default.GetString(expectedArray6), Encoding.Default.GetString(result6));

            List<StackItem> list7 = new List<StackItem> { 1 };
            StackItem stackItem72 = new Struct(list7);
            byte[] result7 = BinarySerializer.Serialize(stackItem72, MaxItemSize);
            byte[] expectedArray7 = new byte[] {
                        0x41,0x01,0x21,0x01,0x01
                    };
            Assert.AreEqual(Encoding.Default.GetString(expectedArray7), Encoding.Default.GetString(result7));

            StackItem stackItem82 = new Map { [2] = 1 };
            byte[] result8 = BinarySerializer.Serialize(stackItem82, MaxItemSize);
            byte[] expectedArray8 = new byte[] {
                        0x48,0x01,0x21,0x01,0x02,0x21,0x01,0x01
                    };
            Assert.AreEqual(Encoding.Default.GetString(expectedArray8), Encoding.Default.GetString(result8));

            Map stackItem91 = new Map();
            stackItem91[1] = stackItem91;
            Action action9 = () => BinarySerializer.Serialize(stackItem91, MaxItemSize);
            action9.Should().Throw<NotSupportedException>();

            VM.Types.Array stackItem10 = new VM.Types.Array();
            stackItem10.Add(stackItem10);
            Action action10 = () => BinarySerializer.Serialize(stackItem10, MaxItemSize);
            action10.Should().Throw<NotSupportedException>();
        }
예제 #12
0
        public void Serialize_Number()
        {
            var entry = new VM.Types.Array {
                1, 9007199254740992
            };
            var json = JsonSerializer.Serialize(entry).ToString();

            Assert.AreEqual(json, "[1,\"9007199254740992\"]");
        }
예제 #13
0
        public void Serialize_Array_Bool_Str_Num()
        {
            var entry = new VM.Types.Array {
                true, "test", 123
            };

            var json = JsonSerializer.Serialize(entry).ToString();

            Assert.AreEqual(json, "[true,\"test\",123]");
        }
예제 #14
0
        public void GetHashData()
        {
            var snapshot = Blockchain.Singleton.GetSnapshot().Clone();
            var engine   = ApplicationEngine.Create(TriggerType.Application, null, snapshot);

            Assert.ThrowsException <ArgumentException>(() => new MethodCallback(engine, UInt160.Zero, "_test"));

            var contract = new ContractState()
            {
                Manifest = new ContractManifest()
                {
                    Permissions = new ContractPermission[0],
                    Groups      = new ContractGroup[0],
                    Trusts      = WildcardContainer <UInt160> .Create(),
                    Abi         = new ContractAbi()
                    {
                        Methods = new ContractMethodDescriptor[]
                        {
                            new ContractMethodDescriptor()
                            {
                                Name = "test", Parameters = new ContractParameterDefinition[0]
                            }
                        },
                        Events = new ContractEventDescriptor[0],
                    },
                },
                Script = new byte[] { 1, 2, 3 },
                Hash   = new byte[] { 1, 2, 3 }.ToScriptHash()
            };

            engine.LoadScript(contract.Script);
            engine.Snapshot.AddContract(contract.Hash, contract);

            Assert.ThrowsException <InvalidOperationException>(() => new MethodCallback(engine, contract.Hash, "test"));

            contract.Manifest.Permissions = new ContractPermission[] {
                new ContractPermission()
                {
                    Contract = ContractPermissionDescriptor.Create(contract.Hash),
                    Methods  = WildcardContainer <string> .Create("test")
                }
            };
            var data = new MethodCallback(engine, contract.Hash, "test");

            Assert.AreEqual(0, engine.CurrentContext.EvaluationStack.Count);
            var array = new VM.Types.Array();

            data.LoadContext(engine, array);

            Assert.AreEqual(3, engine.CurrentContext.EvaluationStack.Count);
            Assert.AreEqual("9bc4860bb936abf262d7a51f74b4304833fee3b2", engine.Pop <VM.Types.ByteString>().GetSpan().ToHexString());
            Assert.AreEqual("test", engine.Pop <VM.Types.ByteString>().GetString());
            Assert.IsTrue(engine.Pop() == array);
        }
예제 #15
0
        public void TestContract_CallEx()
        {
            var snapshot = Blockchain.Singleton.GetSnapshot();

            var state = TestUtils.GetContract("method");

            state.Manifest.Features = ContractFeatures.HasStorage;
            snapshot.Contracts.Add(state.ScriptHash, state);

            byte[] method = Encoding.UTF8.GetBytes("method");
            var    args   = new VM.Types.Array {
                0, 1
            };

            foreach (var flags in new CallFlags[] { CallFlags.None, CallFlags.AllowCall, CallFlags.AllowModifyStates, CallFlags.All })
            {
                var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0);
                engine.LoadScript(new byte[] { 0x01 });

                engine.CurrentContext.EvaluationStack.Push((int)CallFlags.All);
                engine.CurrentContext.EvaluationStack.Push(args);
                engine.CurrentContext.EvaluationStack.Push(method);
                engine.CurrentContext.EvaluationStack.Push(state.ScriptHash.ToArray());
                InteropService.Invoke(engine, InteropService.Contract.CallEx).Should().BeTrue();
                engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[0]);
                engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[1]);

                // Contract doesn't exists

                engine.CurrentContext.EvaluationStack.Push((int)CallFlags.All);
                engine.CurrentContext.EvaluationStack.Push(args);
                engine.CurrentContext.EvaluationStack.Push(method);
                engine.CurrentContext.EvaluationStack.Push(UInt160.Zero.ToArray());
                InteropService.Invoke(engine, InteropService.Contract.CallEx).Should().BeFalse();

                // Call with rights

                engine.CurrentContext.EvaluationStack.Push((int)flags);
                engine.CurrentContext.EvaluationStack.Push(args);
                engine.CurrentContext.EvaluationStack.Push(method);
                engine.CurrentContext.EvaluationStack.Push(state.ScriptHash.ToArray());
                InteropService.Invoke(engine, InteropService.Contract.CallEx).Should().BeTrue();
                engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[0]);
                engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[1]);

                // Check rights

                engine.CurrentContext.EvaluationStack.Push((int)CallFlags.All);
                engine.CurrentContext.EvaluationStack.Push(args);
                engine.CurrentContext.EvaluationStack.Push(method);
                engine.CurrentContext.EvaluationStack.Push(state.ScriptHash.ToArray());
                InteropService.Invoke(engine, InteropService.Contract.CallEx).Should().Be(flags.HasFlag(CallFlags.AllowCall));
            }
        }
예제 #16
0
        public void Serialize_Array_OfArray()
        {
            var entry = new VM.Types.Array
            {
                new VM.Types.Array {
                    true, "test1", 123
                },
                new VM.Types.Array {
                    true, "test2", 321
                }
            };

            var json = JsonSerializer.Serialize(entry).ToString();

            Assert.AreEqual(json, "[[true,\"test1\",123],[true,\"test2\",321]]");
        }
예제 #17
0
        public void TestDeserializeStackItem()
        {
            StackItem stackItem1 = new ByteArray(new byte[5]);
            byte[] byteArray1 = BinarySerializer.Serialize(stackItem1, MaxItemSize);
            StackItem result1 = BinarySerializer.Deserialize(byteArray1, 2048, (uint)byteArray1.Length);
            Assert.AreEqual(stackItem1, result1);

            StackItem stackItem2 = new VM.Types.Boolean(true);
            byte[] byteArray2 = BinarySerializer.Serialize(stackItem2, MaxItemSize);
            StackItem result2 = BinarySerializer.Deserialize(byteArray2, 2048, (uint)byteArray2.Length);
            Assert.AreEqual(stackItem2, result2);

            StackItem stackItem3 = new Integer(1);
            byte[] byteArray3 = BinarySerializer.Serialize(stackItem3, MaxItemSize);
            StackItem result3 = BinarySerializer.Deserialize(byteArray3, 2048, (uint)byteArray3.Length);
            Assert.AreEqual(stackItem3, result3);

            byte[] byteArray4 = BinarySerializer.Serialize(1, MaxItemSize);
            byteArray4[0] = 0x40;
            Action action4 = () => BinarySerializer.Deserialize(byteArray4, 2048, (uint)byteArray4.Length);
            action4.Should().Throw<FormatException>();

            List<StackItem> list5 = new List<StackItem> { 1 };
            StackItem stackItem52 = new VM.Types.Array(list5);
            byte[] byteArray5 = BinarySerializer.Serialize(stackItem52, MaxItemSize);
            StackItem result5 = BinarySerializer.Deserialize(byteArray5, 2048, (uint)byteArray5.Length);
            Assert.AreEqual(((VM.Types.Array)stackItem52).Count, ((VM.Types.Array)result5).Count);
            Assert.AreEqual(((VM.Types.Array)stackItem52).GetEnumerator().Current, ((VM.Types.Array)result5).GetEnumerator().Current);

            List<StackItem> list6 = new List<StackItem> { 1 };
            StackItem stackItem62 = new Struct(list6);
            byte[] byteArray6 = BinarySerializer.Serialize(stackItem62, MaxItemSize);
            StackItem result6 = BinarySerializer.Deserialize(byteArray6, 2048, (uint)byteArray6.Length);
            Assert.AreEqual(((Struct)stackItem62).Count, ((Struct)result6).Count);
            Assert.AreEqual(((Struct)stackItem62).GetEnumerator().Current, ((Struct)result6).GetEnumerator().Current);

            StackItem stackItem72 = new Map { [2] = 1 };
            byte[] byteArray7 = BinarySerializer.Serialize(stackItem72, MaxItemSize);
            StackItem result7 = BinarySerializer.Deserialize(byteArray7, 2048, (uint)byteArray7.Length);
            Assert.AreEqual(((Map)stackItem72).Count, ((Map)result7).Count);
            CollectionAssert.AreEqual(((Map)stackItem72).Keys.ToArray(), ((Map)result7).Keys.ToArray());
            CollectionAssert.AreEqual(((Map)stackItem72).Values.ToArray(), ((Map)result7).Values.ToArray());
        }
예제 #18
0
        public void TestContract_Call()
        {
            var snapshot = Blockchain.Singleton.GetSnapshot();
            var state    = TestUtils.GetContract("method");

            state.Manifest.Features = ContractFeatures.HasStorage;
            byte[] method = Encoding.UTF8.GetBytes("method");
            var    args   = new VM.Types.Array {
                0, 1
            };

            snapshot.Contracts.Add(state.ScriptHash, state);
            var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0);

            engine.LoadScript(new byte[] { 0x01 });

            engine.CurrentContext.EvaluationStack.Push(args);
            engine.CurrentContext.EvaluationStack.Push(method);
            engine.CurrentContext.EvaluationStack.Push(state.ScriptHash.ToArray());
            InteropService.Invoke(engine, InteropService.Contract.Call).Should().BeTrue();
            engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[0]);
            engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[1]);

            state.Manifest.Permissions[0].Methods = WildcardContainer <string> .Create("a");

            engine.CurrentContext.EvaluationStack.Push(args);
            engine.CurrentContext.EvaluationStack.Push(method);
            engine.CurrentContext.EvaluationStack.Push(state.ScriptHash.ToArray());
            InteropService.Invoke(engine, InteropService.Contract.Call).Should().BeFalse();
            state.Manifest.Permissions[0].Methods = WildcardContainer <string> .CreateWildcard();

            engine.CurrentContext.EvaluationStack.Push(args);
            engine.CurrentContext.EvaluationStack.Push(method);
            engine.CurrentContext.EvaluationStack.Push(state.ScriptHash.ToArray());
            InteropService.Invoke(engine, InteropService.Contract.Call).Should().BeTrue();

            engine.CurrentContext.EvaluationStack.Push(args);
            engine.CurrentContext.EvaluationStack.Push(method);
            engine.CurrentContext.EvaluationStack.Push(UInt160.Zero.ToArray());
            InteropService.Invoke(engine, InteropService.Contract.Call).Should().BeFalse();
        }
예제 #19
0
        public static bool GetInputs(ExecutionEngine engine)
        {
            var tx = GetTransactionFromStack(engine);

            if (tx == null)
            {
                return(false);
            }

            var transactions = new List <StackItem>();

            foreach (var entry in tx.inputs)
            {
                transactions.Add(new VM.Types.InteropInterface(entry));
            }

            var inputs = new VM.Types.Array(transactions.ToArray <StackItem>());

            engine.EvaluationStack.Push(inputs);

            return(true);
        }
예제 #20
0
        private static bool Transaction_GetInputs(ExecutionEngine engine)
        {
            var tx = GetInteropFromStack <Transaction>(engine);

            if (tx == null)
            {
                return(false);
            }

            var items = new List <StackItem>();

            foreach (var input in tx.inputs)
            {
                items.Add(new VM.Types.InteropInterface(input));
            }

            var result = new VM.Types.Array(items.ToArray <StackItem>());

            engine.CurrentContext.EvaluationStack.Push(result);

            return(true);
        }
예제 #21
0
        public bool GetTransactions(ExecutionEngine engine)
        {
            var obj   = engine.EvaluationStack.Pop();
            var block = obj.GetInterface <Block>();

            if (block == null)
            {
                return(false);
            }

            // returns Transaction[]

            var txs = new StackItem[block.transactions.Count];

            for (int i = 0; i < block.transactions.Count; i++)
            {
                var tx = block.transactions[i];
                txs[i] = new VM.Types.InteropInterface(tx);
            }

            var array = new VM.Types.Array(txs);

            throw new NotImplementedException();
        }
예제 #22
0
        public static bool GetInputs(ExecutionEngine engine)
        {
            var obj = engine.EvaluationStack.Pop() as VM.Types.InteropInterface;

            if (obj == null)
            {
                return(false);
            }

            var tx = obj.GetInterface <Transaction>();

            var transactions = new List <StackItem>();

            foreach (var entry in tx.inputs)
            {
                transactions.Add(new VM.Types.InteropInterface(entry));
            }

            var outputs = new VM.Types.Array(transactions.ToArray <StackItem>());

            engine.EvaluationStack.Push(outputs);

            return(true);
        }
예제 #23
0
 public void SendTestNotification(UInt160 hash, string eventName, VM.Types.Array state)
 {
     typeof(ApplicationEngine).GetMethod("SendNotification", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
     .Invoke(this, new object[] { hash, eventName, state });
 }
예제 #24
0
        protected override StackItem Main(ApplicationEngine engine, string operation, VM.Types.Array args)
        {
            switch (operation)
            {
            case "getMaxTransactionsPerBlock":
                return(GetMaxTransactionsPerBlock(engine.Snapshot));

            case "getMaxLowPriorityTransactionsPerBlock":
                return(GetMaxLowPriorityTransactionsPerBlock(engine.Snapshot));

            case "getMaxLowPriorityTransactionSize":
                return(GetMaxLowPriorityTransactionSize(engine.Snapshot));

            case "getFeePerByte":
                return(GetFeePerByte(engine.Snapshot));

            case "getBlockedAccounts":
                return(GetBlockedAccounts(engine.Snapshot).Select(p => (StackItem)p.ToArray()).ToArray());

            case "setMaxTransactionsPerBlock":
                return(SetMaxTransactionsPerBlock(engine, (uint)args[0].GetBigInteger()));

            case "setMaxLowPriorityTransactionsPerBlock":
                return(SetMaxLowPriorityTransactionsPerBlock(engine, (uint)args[0].GetBigInteger()));

            case "setMaxLowPriorityTransactionSize":
                return(SetMaxLowPriorityTransactionSize(engine, (uint)args[0].GetBigInteger()));

            case "setFeePerByte":
                return(SetFeePerByte(engine, (long)args[0].GetBigInteger()));

            case "blockAccount":
                return(BlockAccount(engine, new UInt160(args[0].GetByteArray())));

            case "unblockAccount":
                return(UnblockAccount(engine, new UInt160(args[0].GetByteArray())));

            default:
                return(base.Main(engine, operation, args));
            }
        }
예제 #25
0
        private void HandleNotification(Snapshot snapshot, Transaction transaction, UInt160 scriptHash,
                                        VM.Types.Array stateItems,
                                        Dictionary <Nep5BalanceKey, Nep5Balance> nep5BalancesChanged, ref ushort transferIndex)
        {
            if (stateItems.Count == 0)
            {
                return;
            }
            // Event name should be encoded as a byte array.
            if (!(stateItems[0] is VM.Types.ByteArray))
            {
                return;
            }
            var eventName = Encoding.UTF8.GetString(stateItems[0].GetByteArray());

            if (_shouldTrackNonStandardMintTokensEvent && eventName == "mintTokens")
            {
                if (stateItems.Count < 4)
                {
                    return;
                }
                // This is not an official standard but at least one token uses it, and so it is needed for proper
                // balance tracking to support all tokens in use.
                if (!(stateItems[2] is VM.Types.ByteArray))
                {
                    return;
                }
                byte[] mintToBytes = stateItems[2].GetByteArray();
                if (mintToBytes.Length != 20)
                {
                    return;
                }
                var mintTo = new UInt160(mintToBytes);

                var mintAmountItem = stateItems[3];
                if (!(mintAmountItem is VM.Types.ByteArray || mintAmountItem is VM.Types.Integer))
                {
                    return;
                }

                var toKey = new Nep5BalanceKey(mintTo, scriptHash);
                if (!nep5BalancesChanged.ContainsKey(toKey))
                {
                    nep5BalancesChanged.Add(toKey, new Nep5Balance());
                }
                RecordTransferHistory(snapshot, scriptHash, UInt160.Zero, mintTo, mintAmountItem.GetBigInteger(), transaction.Hash, ref transferIndex);
                return;
            }
            if (eventName != "transfer")
            {
                return;
            }
            if (stateItems.Count < 4)
            {
                return;
            }

            if (!(stateItems[1] is null) && !(stateItems[1] is VM.Types.ByteArray))
            {
                return;
            }
            if (!(stateItems[2] is null) && !(stateItems[2] is VM.Types.ByteArray))
            {
                return;
            }
            var amountItem = stateItems[3];

            if (!(amountItem is VM.Types.ByteArray || amountItem is VM.Types.Integer))
            {
                return;
            }
            byte[] fromBytes = stateItems[1]?.GetByteArray();
            if (fromBytes?.Length != 20)
            {
                fromBytes = null;
            }
            byte[] toBytes = stateItems[2]?.GetByteArray();
            if (toBytes?.Length != 20)
            {
                toBytes = null;
            }
            if (fromBytes == null && toBytes == null)
            {
                return;
            }
            var from = new UInt160(fromBytes);
            var to   = new UInt160(toBytes);

            if (fromBytes != null)
            {
                var fromKey = new Nep5BalanceKey(from, scriptHash);
                if (!nep5BalancesChanged.ContainsKey(fromKey))
                {
                    nep5BalancesChanged.Add(fromKey, new Nep5Balance());
                }
            }

            if (toBytes != null)
            {
                var toKey = new Nep5BalanceKey(to, scriptHash);
                if (!nep5BalancesChanged.ContainsKey(toKey))
                {
                    nep5BalancesChanged.Add(toKey, new Nep5Balance());
                }
            }
            RecordTransferHistory(snapshot, scriptHash, from, to, amountItem.GetBigInteger(), transaction.Hash, ref transferIndex);
        }
예제 #26
0
        private void HandleNotification(Snapshot snapshot, Transaction transaction, UInt160 scriptHash,
                                        VM.Types.Array stateItems,
                                        Dictionary <Nep5BalanceKey, Nep5Balance> nep5BalancesChanged, ref ushort transferIndex)
        {
            if (stateItems.Count == 0)
            {
                return;
            }
            // Event name should be encoded as a byte array.
            if (!(stateItems[0] is VM.Types.ByteArray))
            {
                return;
            }
            var eventName = Encoding.UTF8.GetString(stateItems[0].GetByteArray());

            if (eventName != "Transfer")
            {
                return;
            }
            if (stateItems.Count < 4)
            {
                return;
            }

            if (!(stateItems[1] is null) && !(stateItems[1] is VM.Types.ByteArray))
            {
                return;
            }
            if (!(stateItems[2] is null) && !(stateItems[2] is VM.Types.ByteArray))
            {
                return;
            }
            var amountItem = stateItems[3];

            if (!(amountItem is VM.Types.ByteArray || amountItem is VM.Types.Integer))
            {
                return;
            }
            byte[] fromBytes = stateItems[1]?.GetByteArray();
            if (fromBytes?.Length != 20)
            {
                fromBytes = null;
            }
            byte[] toBytes = stateItems[2]?.GetByteArray();
            if (toBytes?.Length != 20)
            {
                toBytes = null;
            }
            if (fromBytes == null && toBytes == null)
            {
                return;
            }
            var from = new UInt160(fromBytes);
            var to   = new UInt160(toBytes);

            if (fromBytes != null)
            {
                var fromKey = new Nep5BalanceKey(from, scriptHash);
                if (!nep5BalancesChanged.ContainsKey(fromKey))
                {
                    nep5BalancesChanged.Add(fromKey, new Nep5Balance());
                }
            }

            if (toBytes != null)
            {
                var toKey = new Nep5BalanceKey(to, scriptHash);
                if (!nep5BalancesChanged.ContainsKey(toKey))
                {
                    nep5BalancesChanged.Add(toKey, new Nep5Balance());
                }
            }
            RecordTransferHistory(snapshot, scriptHash, from, to, amountItem.GetBigInteger(), transaction.Hash, ref transferIndex);
        }
예제 #27
0
 public void VMTypes(
     VM.Types.Boolean p1, VM.Types.Integer p2, VM.Types.ByteString p3, VM.Types.Buffer p4,
     VM.Types.Array p5, VM.Types.Struct p6, VM.Types.Map p7, VM.Types.StackItem p8
     )
 {
 }
예제 #28
0
        private void HandleNotification(DataCache snapshot, IVerifiable scriptContainer, UInt160 scriptHash, string eventName,
                                        VM.Types.Array stateItems,
                                        Dictionary <Nep17BalanceKey, Nep17Balance> nep17BalancesChanged, ref ushort transferIndex)
        {
            if (stateItems.Count == 0)
            {
                return;
            }
            if (eventName != "Transfer")
            {
                return;
            }
            if (stateItems.Count < 3)
            {
                return;
            }

            if (!(stateItems[0].IsNull) && !(stateItems[0] is VM.Types.ByteString))
            {
                return;
            }
            if (!(stateItems[1].IsNull) && !(stateItems[1] is VM.Types.ByteString))
            {
                return;
            }
            var amountItem = stateItems[2];

            if (!(amountItem is VM.Types.ByteString || amountItem is VM.Types.Integer))
            {
                return;
            }
            byte[] fromBytes = stateItems[0].IsNull ? null : stateItems[0].GetSpan().ToArray();
            if (fromBytes != null && fromBytes.Length != UInt160.Length)
            {
                return;
            }
            byte[] toBytes = stateItems[1].IsNull ? null : stateItems[1].GetSpan().ToArray();
            if (toBytes != null && toBytes.Length != UInt160.Length)
            {
                return;
            }
            if (fromBytes == null && toBytes == null)
            {
                return;
            }

            var from = UInt160.Zero;
            var to   = UInt160.Zero;

            if (fromBytes != null)
            {
                from = new UInt160(fromBytes);
                var fromKey = new Nep17BalanceKey(from, scriptHash);
                if (!nep17BalancesChanged.ContainsKey(fromKey))
                {
                    nep17BalancesChanged.Add(fromKey, new Nep17Balance());
                }
            }

            if (toBytes != null)
            {
                to = new UInt160(toBytes);
                var toKey = new Nep17BalanceKey(to, scriptHash);
                if (!nep17BalancesChanged.ContainsKey(toKey))
                {
                    nep17BalancesChanged.Add(toKey, new Nep17Balance());
                }
            }
            if (scriptContainer is Transaction transaction)
            {
                RecordTransferHistory(snapshot, scriptHash, from, to, amountItem.GetInteger(), transaction.Hash, ref transferIndex);
            }
        }
예제 #29
0
        public static StackItem ToStackItem(this object obj)
        {
            StackItem item;

            if (obj == null)
            {
                item = new byte[0];
            }
            else
            {
                var type = obj.GetType();
                if (obj is BigInteger)
                {
                    item = (BigInteger)obj;
                }
                else
                if (obj is byte)
                {
                    item = new BigInteger((byte)obj);
                }
                else
                if (obj is sbyte)
                {
                    item = new BigInteger((sbyte)obj);
                }
                else
                if (obj is int)
                {
                    item = new BigInteger((int)obj);
                }
                else
                if (obj is short)
                {
                    item = new BigInteger((short)obj);
                }
                else
                if (obj is ushort)
                {
                    item = new BigInteger((ushort)obj);
                }
                else
                if (obj is uint)
                {
                    item = new BigInteger((uint)obj);
                }
                else
                if (obj is long)
                {
                    item = new BigInteger((long)obj);
                }
                else
                if (obj is ulong)
                {
                    item = new BigInteger((ulong)obj);
                }
                else
                if (obj is byte[])
                {
                    item = (byte[])obj;
                }
                else
                if (obj is bool)
                {
                    item = (bool)obj;
                }
                else
                if (obj is string)
                {
                    item = (byte[])System.Text.Encoding.UTF8.GetBytes((string)obj);
                }
                else
                if (type.IsArray)
                {
                    var items = new List <StackItem>();
                    var array = (Array)obj;

                    foreach (var val in array)
                    {
                        items.Add(val.ToStackItem());
                    }

                    item = new VM.Types.Array(items);
                }
                else
                if (type.IsValueType && !type.IsEnum)
                {
                    var items = new List <StackItem> ();

                    var fields = type.GetFields();
                    foreach (var field in fields)
                    {
                        object val = field.GetValue(obj);
                        items.Add(val.ToStackItem());
                    }

                    item = new VM.Types.Struct(items);
                }
                else
                {
                    throw new Exception("something bad happened");
                }
            }

            return(item);
        }
예제 #30
0
 public new StackItem TotalSupply(ApplicationEngine engine, VM.Types.Array args)
 {
     return(base.TotalSupply(engine, args));
 }