Ejemplo n.º 1
0
        public void ApplicationEngineReusedStorage_PartialReuse()
        {
            var key      = new byte[] { (byte)OpCode.PUSH1 };
            var oldValue = new byte[] { (byte)OpCode.PUSH1 };
            var value    = new byte[] { (byte)OpCode.PUSH1, (byte)OpCode.PUSH1 };

            byte[] script = CreatePutScript(key, value);

            ContractState contractState = TestUtils.GetContract(script);

            StorageKey  skey  = TestUtils.GetStorageKey(contractState.Id, key);
            StorageItem sItem = TestUtils.GetStorageItem(oldValue);

            var snapshot = TestBlockchain.GetTestSnapshot();

            snapshot.Add(skey, sItem);
            snapshot.AddContract(script.ToScriptHash(), contractState);

            using ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
            Debugger debugger = new(ae);

            ae.LoadScript(script);
            debugger.StepInto();
            debugger.StepInto();
            debugger.StepInto();
            var setupPrice = ae.GasConsumed;

            debugger.StepInto();
            debugger.StepInto();
            (ae.GasConsumed - setupPrice).Should().Be((1 + (oldValue.Length / 4) + value.Length - oldValue.Length) * ae.StoragePrice + (1 << 15) * 30);
        }
        public void TestFromJson()
        {
            var    snapshot = TestBlockchain.GetTestSnapshot();
            Action action   = () => ContractParametersContext.Parse("{\"type\":\"wrongType\",\"data\":\"00000000007c97764845172d827d3c863743293931a691271a0000000000000000000000000000000000000000000100\",\"items\":{\"0x1bd5c777ec35768892bd3daab60fb7a1cb905066\":{\"script\":\"21026ff03b949241ce1dadd43519e6960e0a85b41a69a05c328103aa2bce1594ca1650680a906ad4\",\"parameters\":[{\"type\":\"Signature\",\"value\":\"01\"}]}}}", snapshot);

            action.Should().Throw <FormatException>();
        }
Ejemplo n.º 3
0
        public void TestNotify()
        {
            var snapshot = TestBlockchain.GetTestSnapshot();

            using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings);
            engine.LoadScript(System.Array.Empty <byte>());
            ApplicationEngine.Notify += Test_Notify1;
            const string notifyEvent = "TestEvent";

            engine.SendNotification(UInt160.Zero, notifyEvent, new Array());
            eventName.Should().Be(notifyEvent);

            ApplicationEngine.Notify += Test_Notify2;
            engine.SendNotification(UInt160.Zero, notifyEvent, new Array());
            eventName.Should().Be(null);

            eventName = notifyEvent;
            ApplicationEngine.Notify -= Test_Notify1;
            engine.SendNotification(UInt160.Zero, notifyEvent, new Array());
            eventName.Should().Be(null);

            ApplicationEngine.Notify -= Test_Notify2;
            engine.SendNotification(UInt160.Zero, notifyEvent, new Array());
            eventName.Should().Be(null);
        }
Ejemplo n.º 4
0
        public void TestStorage_Find()
        {
            var snapshot = TestBlockchain.GetTestSnapshot();
            var state    = TestUtils.GetContract();

            var storageItem = new StorageItem
            {
                Value = new byte[] { 0x01, 0x02, 0x03, 0x04 }
            };
            var storageKey = new StorageKey
            {
                Id  = state.Id,
                Key = new byte[] { 0x01 }
            };

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

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

            var iterator = engine.Find(new StorageContext
            {
                Id         = state.Id,
                IsReadOnly = false
            }, new byte[] { 0x01 }, FindOptions.ValuesOnly);

            iterator.Next();
            var ele = iterator.Value();

            ele.GetSpan().ToHexString().Should().Be(storageItem.Value.ToHexString());
        }
Ejemplo n.º 5
0
        public void System_Runtime_GasLeft()
        {
            var snapshot = TestBlockchain.GetTestSnapshot();

            using (var script = new ScriptBuilder())
            {
                script.Emit(OpCode.NOP);
                script.EmitSysCall(ApplicationEngine.System_Runtime_GasLeft);
                script.Emit(OpCode.NOP);
                script.EmitSysCall(ApplicationEngine.System_Runtime_GasLeft);
                script.Emit(OpCode.NOP);
                script.Emit(OpCode.NOP);
                script.Emit(OpCode.NOP);
                script.EmitSysCall(ApplicationEngine.System_Runtime_GasLeft);

                // Execute

                var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, gas: 100_000_000);
                engine.LoadScript(script.ToArray());
                Assert.AreEqual(engine.Execute(), VMState.HALT);

                // Check the results

                CollectionAssert.AreEqual
                (
                    engine.ResultStack.Select(u => (int)u.GetInteger()).ToArray(),
                    new int[] { 99_999_490, 99_998_980, 99_998_410 }
Ejemplo n.º 6
0
        public void TestContract_Update_Invalid()
        {
            var nefFile = new NefFile()
            {
                Script   = new byte[] { 0x01 },
                Compiler = "",
                Tokens   = System.Array.Empty <MethodToken>()
            };

            nefFile.CheckSum = NefFile.ComputeChecksum(nefFile);

            var snapshot = TestBlockchain.GetTestSnapshot();

            Assert.ThrowsException <InvalidOperationException>(() => snapshot.UpdateContract(null, null, new byte[] { 0x01 }));
            Assert.ThrowsException <InvalidOperationException>(() => snapshot.UpdateContract(null, nefFile.ToArray(), null));
            Assert.ThrowsException <ArgumentException>(() => snapshot.UpdateContract(null, null, null));

            nefFile = new NefFile()
            {
                Script   = new byte[0],
                Compiler = "",
                Tokens   = System.Array.Empty <MethodToken>()
            };
            nefFile.CheckSum = NefFile.ComputeChecksum(nefFile);

            Assert.ThrowsException <InvalidOperationException>(() => snapshot.UpdateContract(null, nefFile.ToArray(), new byte[] { 0x01 }));
            Assert.ThrowsException <InvalidOperationException>(() => snapshot.UpdateContract(null, nefFile.ToArray(), new byte[0]));
        }
Ejemplo n.º 7
0
        public void TestDuplicateOracle()
        {
            // Fake balance
            var snapshot = TestBlockchain.GetTestSnapshot();

            ApplicationEngine engine  = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings, gas: long.MaxValue);
            BigInteger        balance = NativeContract.GAS.BalanceOf(snapshot, UInt160.Zero);

            NativeContract.GAS.Burn(engine, UInt160.Zero, balance);
            NativeContract.GAS.Mint(engine, UInt160.Zero, 8, false);

            // Test
            TransactionVerificationContext verificationContext = new TransactionVerificationContext();
            var tx = CreateTransactionWithFee(1, 2);

            tx.Attributes = new TransactionAttribute[] { new OracleResponse()
                                                         {
                                                             Code = OracleResponseCode.ConsensusUnreachable, Id = 1, Result = new byte[0]
                                                         } };
            verificationContext.CheckTransaction(tx, snapshot).Should().BeTrue();
            verificationContext.AddTransaction(tx);

            tx            = CreateTransactionWithFee(2, 1);
            tx.Attributes = new TransactionAttribute[] { new OracleResponse()
                                                         {
                                                             Code = OracleResponseCode.ConsensusUnreachable, Id = 1, Result = new byte[0]
                                                         } };
            verificationContext.CheckTransaction(tx, snapshot).Should().BeFalse();
        }
Ejemplo n.º 8
0
        public void TestContract_Destroy()
        {
            var snapshot    = TestBlockchain.GetTestSnapshot();
            var state       = TestUtils.GetContract();
            var scriptHash  = UInt160.Parse("0xcb9f3b7c6fb1cf2c13a40637c189bdd066a272b4");
            var storageItem = new StorageItem
            {
                Value = new byte[] { 0x01, 0x02, 0x03, 0x04 }
            };

            var storageKey = new StorageKey
            {
                Id  = 0x43000000,
                Key = new byte[] { 0x01 }
            };

            snapshot.AddContract(scriptHash, state);
            snapshot.Add(storageKey, storageItem);
            snapshot.DestroyContract(scriptHash);
            snapshot.Find(BitConverter.GetBytes(0x43000000)).Any().Should().BeFalse();

            //storages are removed
            state = TestUtils.GetContract();
            snapshot.AddContract(scriptHash, state);
            snapshot.DestroyContract(scriptHash);
            snapshot.Find(BitConverter.GetBytes(0x43000000)).Any().Should().BeFalse();
        }
Ejemplo n.º 9
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));
        }
Ejemplo n.º 10
0
        public void TestStorage_Delete()
        {
            var engine     = GetEngine(false, true);
            var snapshot   = TestBlockchain.GetTestSnapshot();
            var state      = TestUtils.GetContract();
            var storageKey = new StorageKey
            {
                Id  = 0x42000000,
                Key = new byte[] { 0x01 }
            };
            var storageItem = new StorageItem
            {
                Value = new byte[] { 0x01, 0x02, 0x03, 0x04 }
            };

            snapshot.AddContract(state.Hash, state);
            snapshot.Add(storageKey, storageItem);
            engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
            engine.LoadScript(new byte[] { 0x01 });
            var key            = new byte[] { 0x01 };
            var storageContext = new StorageContext
            {
                Id         = state.Id,
                IsReadOnly = false
            };

            engine.Delete(storageContext, key);

            //context is readonly
            storageContext.IsReadOnly = true;
            Assert.ThrowsException <ArgumentException>(() => engine.Delete(storageContext, key));
        }
Ejemplo n.º 11
0
        public void TestStorage_Put()
        {
            var engine = GetEngine(false, true);

            //CheckStorageContext fail
            var key            = new byte[] { 0x01 };
            var value          = new byte[] { 0x02 };
            var state          = TestUtils.GetContract();
            var storageContext = new StorageContext
            {
                Id         = state.Id,
                IsReadOnly = false
            };

            engine.Put(storageContext, key, value);

            //key.Length > MaxStorageKeySize
            key   = new byte[ApplicationEngine.MaxStorageKeySize + 1];
            value = new byte[] { 0x02 };
            Assert.ThrowsException <ArgumentException>(() => engine.Put(storageContext, key, value));

            //value.Length > MaxStorageValueSize
            key   = new byte[] { 0x01 };
            value = new byte[ushort.MaxValue + 1];
            Assert.ThrowsException <ArgumentException>(() => engine.Put(storageContext, key, value));

            //context.IsReadOnly
            key   = new byte[] { 0x01 };
            value = new byte[] { 0x02 };
            storageContext.IsReadOnly = true;
            Assert.ThrowsException <ArgumentException>(() => engine.Put(storageContext, key, value));

            //storage value is constant
            var snapshot = TestBlockchain.GetTestSnapshot();

            var storageKey = new StorageKey
            {
                Id  = state.Id,
                Key = new byte[] { 0x01 }
            };
            var storageItem = new StorageItem
            {
                Value = new byte[] { 0x01, 0x02, 0x03, 0x04 }
            };

            snapshot.AddContract(state.Hash, state);
            snapshot.Add(storageKey, storageItem);
            engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
            engine.LoadScript(new byte[] { 0x01 });
            key   = new byte[] { 0x01 };
            value = new byte[] { 0x02 };
            storageContext.IsReadOnly = false;
            engine.Put(storageContext, key, value);

            //value length == 0
            key   = new byte[] { 0x01 };
            value = System.Array.Empty <byte>();
            engine.Put(storageContext, key, value);
        }
Ejemplo n.º 12
0
        public void TestParse()
        {
            var snapshot = TestBlockchain.GetTestSnapshot();
            var ret      = ContractParametersContext.Parse("{\"type\":\"Neo.Network.P2P.Payloads.Transaction\",\"hex\":\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFmUJDLobcPtqo9vZKIdjXsd8fVGwEAARI=\",\"items\":{\"0xbecaad15c0ea585211faf99738a4354014f177f2\":{\"script\":\"IQJv8DuUkkHOHa3UNRnmlg4KhbQaaaBcMoEDqivOFZTKFmh0dHaq\",\"parameters\":[{\"type\":\"Signature\",\"value\":\"AQ==\"}]}}}", snapshot);

            ret.ScriptHashes[0].ToString().Should().Be("0x1bd5c777ec35768892bd3daab60fb7a1cb905066");
            ((Transaction)ret.Verifiable).Script.ToHexString().Should().Be(new byte[] { 18 }.ToHexString());
        }
Ejemplo n.º 13
0
        public void TestGetComplete()
        {
            var         snapshot = TestBlockchain.GetTestSnapshot();
            Transaction tx       = TestUtils.GetTransaction(UInt160.Parse("0x1bd5c777ec35768892bd3daab60fb7a1cb905066"));
            var         context  = new ContractParametersContext(snapshot, tx);

            context.Completed.Should().BeFalse();
        }
Ejemplo n.º 14
0
        public void TestParse()
        {
            var snapshot = TestBlockchain.GetTestSnapshot();
            var ret      = ContractParametersContext.Parse("{\"type\":\"Neo.Network.P2P.Payloads.Transaction\",\"data\":\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFmUJDLobcPtqo9vZKIdjXsd8fVGwEAARI=\",\"items\":{\"0xbecaad15c0ea585211faf99738a4354014f177f2\":{\"script\":\"IQJv8DuUkkHOHa3UNRnmlg4KhbQaaaBcMoEDqivOFZTKFmh0dHaq\",\"parameters\":[{\"type\":\"Signature\",\"value\":\"AQ==\"}],\"signatures\":{\"03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c\":\"AQ==\"}}},\"network\":" + ProtocolSettings.Default.Network + "}", snapshot);

            ret.ScriptHashes[0].ToString().Should().Be("0x1bd5c777ec35768892bd3daab60fb7a1cb905066");
            ((Transaction)ret.Verifiable).Script.ToHexString().Should().Be(new byte[] { 18 }.ToHexString());
        }
Ejemplo n.º 15
0
        public void Json_Serialize()
        {
            var snapshot = TestBlockchain.GetTestSnapshot();

            // Good

            using (var script = new ScriptBuilder())
            {
                script.EmitDynamicCall(NativeContract.StdLib.Hash, "jsonSerialize", 5);
                script.EmitDynamicCall(NativeContract.StdLib.Hash, "jsonSerialize", true);
                script.EmitDynamicCall(NativeContract.StdLib.Hash, "jsonSerialize", "test");
                script.EmitDynamicCall(NativeContract.StdLib.Hash, "jsonSerialize", new object[] { null });
                script.EmitDynamicCall(NativeContract.StdLib.Hash, "jsonSerialize", new ContractParameter(ContractParameterType.Map)
                {
                    Value = new List <KeyValuePair <ContractParameter, ContractParameter> >()
                    {
                        { new KeyValuePair <ContractParameter, ContractParameter>(
                              new ContractParameter(ContractParameterType.String)
                            {
                                Value = "key"
                            },
                              new ContractParameter(ContractParameterType.String)
                            {
                                Value = "value"
                            }) }
                    }
                });

                using (var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings))
                {
                    engine.LoadScript(script.ToArray());

                    Assert.AreEqual(engine.Execute(), VMState.HALT);
                    Assert.AreEqual(5, engine.ResultStack.Count);

                    Assert.IsTrue(engine.ResultStack.Pop <ByteString>().GetString() == "{\"key\":\"value\"}");
                    Assert.IsTrue(engine.ResultStack.Pop <ByteString>().GetString() == "null");
                    Assert.IsTrue(engine.ResultStack.Pop <ByteString>().GetString() == "\"test\"");
                    Assert.IsTrue(engine.ResultStack.Pop <ByteString>().GetString() == "1");
                    Assert.IsTrue(engine.ResultStack.Pop <ByteString>().GetString() == "5");
                }
            }

            // Error

            using (var script = new ScriptBuilder())
            {
                script.EmitDynamicCall(NativeContract.StdLib.Hash, "jsonSerialize");

                using (var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings))
                {
                    engine.LoadScript(script.ToArray());

                    Assert.AreEqual(engine.Execute(), VMState.FAULT);
                    Assert.AreEqual(0, engine.ResultStack.Count);
                }
            }
        }
Ejemplo n.º 16
0
        public void TestAddSignature()
        {
            var         snapshot     = TestBlockchain.GetTestSnapshot();
            var         singleSender = UInt160.Parse("0x4bc1b25796f4a13fa3cc7538168d86f7d3bc5356");
            Transaction tx           = TestUtils.GetTransaction(singleSender);

            //singleSign

            var context = new ContractParametersContext(snapshot, tx);

            context.AddSignature(contract, key.PublicKey, new byte[] { 0x01 }).Should().BeTrue();

            var contract1 = Contract.CreateSignatureContract(key.PublicKey);

            contract1.ParameterList = new ContractParameterType[0];
            context = new ContractParametersContext(snapshot, tx);
            context.AddSignature(contract1, key.PublicKey, new byte[] { 0x01 }).Should().BeFalse();

            contract1.ParameterList = new[] { ContractParameterType.Signature, ContractParameterType.Signature };
            Action action1 = () => context.AddSignature(contract1, key.PublicKey, new byte[] { 0x01 });

            action1.Should().Throw <NotSupportedException>();

            //multiSign

            byte[] privateKey2 = new byte[] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02 };
            var key2 = new KeyPair(privateKey2);
            var multiSignContract = Contract.CreateMultiSigContract(2,
                                                                    new ECPoint[]
            {
                key.PublicKey,
                key2.PublicKey
            });
            var multiSender = UInt160.Parse("0x7eae53e544e3c1507bfa7892d4793b8784be4c31");

            tx      = TestUtils.GetTransaction(multiSender);
            context = new ContractParametersContext(snapshot, tx);
            context.AddSignature(multiSignContract, key.PublicKey, new byte[] { 0x01 }).Should().BeTrue();
            context.AddSignature(multiSignContract, key2.PublicKey, new byte[] { 0x01 }).Should().BeTrue();

            tx      = TestUtils.GetTransaction(singleSender);
            context = new ContractParametersContext(snapshot, tx);
            context.AddSignature(multiSignContract, key.PublicKey, new byte[] { 0x01 }).Should().BeFalse();

            tx      = TestUtils.GetTransaction(multiSender);
            context = new ContractParametersContext(snapshot, tx);
            byte[] privateKey3 = new byte[] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x03 };
            var key3 = new KeyPair(privateKey3);

            context.AddSignature(multiSignContract, key3.PublicKey, new byte[] { 0x01 }).Should().BeFalse();
        }
Ejemplo n.º 17
0
        public void TestSetup()
        {
            TestBlockchain.InitializeMockNeoSystem();

            _snapshot        = TestBlockchain.GetTestSnapshot();
            _persistingBlock = new Block {
                Header = new Header()
            };
        }
Ejemplo n.º 18
0
 public void TestSetup()
 {
     _snapshot        = TestBlockchain.GetTestSnapshot();
     _persistingBlock = new Block
     {
         Header       = new Header(),
         Transactions = Array.Empty <Transaction>()
     };
 }
Ejemplo n.º 19
0
        public void TestAddSignature()
        {
            var         snapshot     = TestBlockchain.GetTestSnapshot();
            var         singleSender = UInt160.Parse("0x902e0d38da5e513b6d07c1c55b85e77d3dce8063");
            Transaction tx           = TestUtils.GetTransaction(singleSender);

            //singleSign

            var context = new ContractParametersContext(snapshot, tx);

            context.AddSignature(contract, key.PublicKey, new byte[] { 0x01 }).Should().BeTrue();

            var contract1 = Contract.CreateSignatureContract(key.PublicKey);

            contract1.ParameterList = Array.Empty <ContractParameterType>();
            context = new ContractParametersContext(snapshot, tx);
            context.AddSignature(contract1, key.PublicKey, new byte[] { 0x01 }).Should().BeFalse();

            contract1.ParameterList = new[] { ContractParameterType.Signature, ContractParameterType.Signature };
            Action action1 = () => context.AddSignature(contract1, key.PublicKey, new byte[] { 0x01 });

            action1.Should().Throw <NotSupportedException>();

            //multiSign

            byte[] privateKey2 = new byte[] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02 };
            var key2 = new KeyPair(privateKey2);
            var multiSignContract = Contract.CreateMultiSigContract(2,
                                                                    new ECPoint[]
            {
                key.PublicKey,
                key2.PublicKey
            });
            var multiSender = UInt160.Parse("0xf76b51bc6605ac3cfcd188173af0930507f51210");

            tx      = TestUtils.GetTransaction(multiSender);
            context = new ContractParametersContext(snapshot, tx);
            context.AddSignature(multiSignContract, key.PublicKey, new byte[] { 0x01 }).Should().BeTrue();
            context.AddSignature(multiSignContract, key2.PublicKey, new byte[] { 0x01 }).Should().BeTrue();

            tx      = TestUtils.GetTransaction(singleSender);
            context = new ContractParametersContext(snapshot, tx);
            context.AddSignature(multiSignContract, key.PublicKey, new byte[] { 0x01 }).Should().BeFalse();

            tx      = TestUtils.GetTransaction(multiSender);
            context = new ContractParametersContext(snapshot, tx);
            byte[] privateKey3 = new byte[] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x03 };
            var key3 = new KeyPair(privateKey3);

            context.AddSignature(multiSignContract, key3.PublicKey, new byte[] { 0x01 }).Should().BeFalse();
        }
Ejemplo n.º 20
0
        public void TestAddSignature()
        {
            var         snapshot     = TestBlockchain.GetTestSnapshot();
            var         singleSender = UInt160.Parse("0x282646ee0afa5508bb999318f35074b84a17c9f0");
            Transaction tx           = TestUtils.GetTransaction(singleSender);

            //singleSign

            var context = new ContractParametersContext(snapshot, tx);

            context.AddSignature(contract, key.PublicKey, new byte[] { 0x01 }).Should().BeTrue();

            var contract1 = Contract.CreateSignatureContract(key.PublicKey);

            contract1.ParameterList = new ContractParameterType[0];
            context = new ContractParametersContext(snapshot, tx);
            context.AddSignature(contract1, key.PublicKey, new byte[] { 0x01 }).Should().BeFalse();

            contract1.ParameterList = new[] { ContractParameterType.Signature, ContractParameterType.Signature };
            Action action1 = () => context.AddSignature(contract1, key.PublicKey, new byte[] { 0x01 });

            action1.Should().Throw <NotSupportedException>();

            //multiSign

            byte[] privateKey2 = new byte[] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02 };
            var key2 = new KeyPair(privateKey2);
            var multiSignContract = Contract.CreateMultiSigContract(2,
                                                                    new ECPoint[]
            {
                key.PublicKey,
                key2.PublicKey
            });
            var multiSender = UInt160.Parse("0x3593816cc1085a6328fea2b899c24d78cd0ba372");

            tx      = TestUtils.GetTransaction(multiSender);
            context = new ContractParametersContext(snapshot, tx);
            context.AddSignature(multiSignContract, key.PublicKey, new byte[] { 0x01 }).Should().BeTrue();
            context.AddSignature(multiSignContract, key2.PublicKey, new byte[] { 0x01 }).Should().BeTrue();

            tx      = TestUtils.GetTransaction(singleSender);
            context = new ContractParametersContext(snapshot, tx);
            context.AddSignature(multiSignContract, key.PublicKey, new byte[] { 0x01 }).Should().BeFalse();

            tx      = TestUtils.GetTransaction(multiSender);
            context = new ContractParametersContext(snapshot, tx);
            byte[] privateKey3 = new byte[] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                              0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x03 };
            var key3 = new KeyPair(privateKey3);

            context.AddSignature(multiSignContract, key3.PublicKey, new byte[] { 0x01 }).Should().BeFalse();
        }
Ejemplo n.º 21
0
        public void TestSetup()
        {
            _snapshot = TestBlockchain.GetTestSnapshot();

            ApplicationEngine engine = ApplicationEngine.Create(TriggerType.OnPersist, null, _snapshot, new Block {
                Header = new Header()
            }, settings: TestBlockchain.TheNeoSystem.Settings, gas: 0);

            NativeContract.ContractManagement.OnPersist(engine);
        }
Ejemplo n.º 22
0
        public void TestConstructorWithNonexistAssetId()
        {
            var    snapshot = TestBlockchain.GetTestSnapshot();
            Action action   = () =>
            {
                var descriptor = new Neo.Wallets.AssetDescriptor(snapshot, UInt160.Parse("01ff00ff00ff00ff00ff00ff00ff00ff00ff00a4"));
            };

            action.Should().Throw <ArgumentException>();
        }
Ejemplo n.º 23
0
        public void TestContract_Update()
        {
            var snapshot = TestBlockchain.GetTestSnapshot();
            var nef      = new NefFile()
            {
                Script   = new[] { (byte)OpCode.RET },
                Compiler = "",
                Tokens   = System.Array.Empty <MethodToken>()
            };

            nef.CheckSum = NefFile.ComputeChecksum(nef);
            Assert.ThrowsException <InvalidOperationException>(() => snapshot.UpdateContract(null, nef.ToArray(), new byte[0]));

            var manifest = TestUtils.CreateDefaultManifest();

            byte[]  privkey = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                0x01,  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
            KeyPair key    = new KeyPair(privkey);
            ECPoint pubkey = key.PublicKey;
            var     state  = TestUtils.GetContract();

            byte[] signature = Crypto.Sign(state.Hash.ToArray(), privkey, pubkey.EncodePoint(false).Skip(1).ToArray());
            manifest.Groups = new ContractGroup[]
            {
                new ContractGroup()
                {
                    PubKey    = pubkey,
                    Signature = signature
                }
            };

            var storageItem = new StorageItem
            {
                Value      = new byte[] { 0x01 },
                IsConstant = false
            };

            var storageKey = new StorageKey
            {
                Id  = state.Id,
                Key = new byte[] { 0x01 }
            };

            snapshot.AddContract(state.Hash, state);
            snapshot.Add(storageKey, storageItem);
            state.UpdateCounter.Should().Be(0);
            snapshot.UpdateContract(state.Hash, nef.ToArray(), manifest.ToJson().ToByteArray(false));
            var ret = NativeContract.ContractManagement.GetContract(snapshot, state.Hash);

            snapshot.Find(BitConverter.GetBytes(state.Id)).ToList().Count().Should().Be(1);
            ret.UpdateCounter.Should().Be(1);
            ret.Id.Should().Be(state.Id);
            ret.Manifest.ToJson().ToString().Should().Be(manifest.ToJson().ToString());
            ret.Script.ToHexString().Should().Be(nef.Script.ToHexString().ToString());
        }
Ejemplo n.º 24
0
        public void Check_GAS()
        {
            var snapshot   = TestBlockchain.GetTestSnapshot();
            var descriptor = new Neo.Wallets.AssetDescriptor(snapshot, ProtocolSettings.Default, NativeContract.GAS.Hash);

            descriptor.AssetId.Should().Be(NativeContract.GAS.Hash);
            descriptor.AssetName.Should().Be(nameof(GasToken));
            descriptor.ToString().Should().Be(nameof(GasToken));
            descriptor.Symbol.Should().Be("GAS");
            descriptor.Decimals.Should().Be(8);
        }
Ejemplo n.º 25
0
        public void TestToString()
        {
            var         snapshot = TestBlockchain.GetTestSnapshot();
            Transaction tx       = TestUtils.GetTransaction(UInt160.Parse("0x1bd5c777ec35768892bd3daab60fb7a1cb905066"));
            var         context  = new ContractParametersContext(snapshot, tx, ProtocolSettings.Default.Network);

            context.Add(contract, 0, new byte[] { 0x01 });
            string str = context.ToString();

            str.Should().Be(@"{""type"":""Neo.Network.P2P.Payloads.Transaction"",""hash"":""0x602c1fa1c08b041e4e6b87aa9a9f9c643166cd34bdd5215a3dd85778c59cce88"",""data"":""AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFmUJDLobcPtqo9vZKIdjXsd8fVGwEAARI="",""items"":{},""network"":" + ProtocolSettings.Default.Network + "}");
        }
Ejemplo n.º 26
0
        public void Check_NEO()
        {
            var snapshot   = TestBlockchain.GetTestSnapshot();
            var descriptor = new Neo.Wallets.AssetDescriptor(snapshot, NativeContract.NEO.Hash);

            descriptor.AssetId.Should().Be(NativeContract.NEO.Hash);
            descriptor.AssetName.Should().Be(nameof(NeoToken));
            descriptor.ToString().Should().Be(nameof(NeoToken));
            descriptor.Symbol.Should().Be("NEO");
            descriptor.Decimals.Should().Be(0);
        }
Ejemplo n.º 27
0
        public void TestSetup()
        {
            TestBlockchain.InitializeMockNeoSystem();
            _snapshot = TestBlockchain.GetTestSnapshot();

            ApplicationEngine engine = ApplicationEngine.Create(TriggerType.OnPersist, null, _snapshot, new Block {
                Header = new Header()
            }, 0);

            NativeContract.ContractManagement.OnPersist(engine);
        }
Ejemplo n.º 28
0
        public void TestToString()
        {
            var         snapshot = TestBlockchain.GetTestSnapshot();
            Transaction tx       = TestUtils.GetTransaction(UInt160.Parse("0x1bd5c777ec35768892bd3daab60fb7a1cb905066"));
            var         context  = new ContractParametersContext(snapshot, tx);

            context.Add(contract, 0, new byte[] { 0x01 });
            string str = context.ToString();

            str.Should().Be(@"{""type"":""Neo.Network.P2P.Payloads.Transaction"",""hex"":""AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFmUJDLobcPtqo9vZKIdjXsd8fVGwEAARI="",""items"":{}}");
        }
Ejemplo n.º 29
0
        public void TestCreateDummyBlock()
        {
            var snapshot = TestBlockchain.GetTestSnapshot();

            byte[]            SyscallSystemRuntimeCheckWitnessHash = new byte[] { 0x68, 0xf8, 0x27, 0xec, 0x8c };
            ApplicationEngine engine = ApplicationEngine.Run(SyscallSystemRuntimeCheckWitnessHash, snapshot);

            engine.PersistingBlock.Version.Should().Be(0);
            engine.PersistingBlock.PrevHash.Should().Be(TestBlockchain.TheNeoSystem.GenesisBlock.Hash);
            engine.PersistingBlock.MerkleRoot.Should().Be(new UInt256());
        }
Ejemplo n.º 30
0
        public void Check_Vauth()
        {
            var snapshot   = TestBlockchain.GetTestSnapshot();
            var descriptor = new Vauth.Wallets.AssetDescriptor(snapshot, ProtocolSettings.Default, NativeContract.Vauth.Hash);

            descriptor.AssetId.Should().Be(NativeContract.Vauth.Hash);
            descriptor.AssetName.Should().Be(nameof(VauthToken));
            descriptor.ToString().Should().Be(nameof(VauthToken));
            descriptor.Symbol.Should().Be("Vauth");
            descriptor.Decimals.Should().Be(0);
        }