Exemple #1
0
        public async Task TestTryAddNewContract()
        {
            RunnerConfig.Instance.SdkDir = _mock.SdkDir;
            var runner = new SmartContractRunner();

            var reg = new SmartContractRegistration
            {
                Category      = 0,
                ContractBytes = ByteString.CopyFrom(_mock.ContractCode),
                ContractHash  = Hash.FromRawBytes(_mock.ContractCode)
            };

            var resC = runner.ExtractMetadata(typeof(TestContractC));

            // Structure of the test data
            var groundTruthResC = new Dictionary <string, FunctionMetadataTemplate>(
                new[] // function metadata map for contract
            {
                new KeyValuePair <string, FunctionMetadataTemplate>(
                    "${this}.Func0",                 //     local function name
                    new FunctionMetadataTemplate(    //     local function metadata
                        new HashSet <string>(),      //         calling set of this function metadata
                        new HashSet <Resource>(new[] //         local resource set of this function metadata
                {
                    new Resource("${this}.resource4",
                                 DataAccessMode.AccountSpecific)    //             resource of the local resource set
                }))),

                new KeyValuePair <string, FunctionMetadataTemplate>(
                    "${this}.Func1",
                    new FunctionMetadataTemplate(
                        new HashSet <string>(),
                        new HashSet <Resource>(new[]
                {
                    new Resource("${this}.resource5",
                                 DataAccessMode.ReadOnlyAccountSharing)
                })))
            });

            var groundTruthTemplateC = new ContractMetadataTemplate(typeof(TestContractC).FullName, groundTruthResC,
                                                                    new Dictionary <string, Address>());

            Assert.Equal(groundTruthTemplateC, resC, new ContractMetadataTemplateEqualityComparer());

            var resB = runner.ExtractMetadata(typeof(TestContractB));

            var groundTruthResB = new Dictionary <string, FunctionMetadataTemplate>(new[]
            {
                new KeyValuePair <string, FunctionMetadataTemplate>(
                    "${this}.Func0",
                    new FunctionMetadataTemplate(
                        new HashSet <string>(new[] { "${ContractC}.Func1" }),
                        new HashSet <Resource>(new[]
                                               { new Resource("${this}.resource2", DataAccessMode.AccountSpecific) }))),

                new KeyValuePair <string, FunctionMetadataTemplate>(
                    "${this}.Func1",
                    new FunctionMetadataTemplate(
                        new HashSet <string>(),
                        new HashSet <Resource>(new[]
                                               { new Resource("${this}.resource3", DataAccessMode.ReadOnlyAccountSharing) })))
            });

            var refB = new Dictionary <string, Address>(new []
            {
                new KeyValuePair <string, Address>("ContractC", Address.FromRawBytes(ByteArrayHelpers.FromHexString("0x456745674567456745674567456745674567"))),
            });

            var groundTruthTemplateB = new ContractMetadataTemplate(typeof(TestContractB).FullName, groundTruthResB,
                                                                    refB);

            Assert.Equal(groundTruthTemplateB, resB, new ContractMetadataTemplateEqualityComparer());

            var resA = runner.ExtractMetadata(typeof(TestContractA));

            var groundTruthResA = new Dictionary <string, FunctionMetadataTemplate>(new[]
            {
                new KeyValuePair <string, FunctionMetadataTemplate>(
                    "${this}.Func0(int)",
                    new FunctionMetadataTemplate(
                        new HashSet <string>(), new HashSet <Resource>())),

                new KeyValuePair <string, FunctionMetadataTemplate>(
                    "${this}.Func0",
                    new FunctionMetadataTemplate(
                        new HashSet <string>(new[] { "${this}.Func1" }),
                        new HashSet <Resource>(new[]
                {
                    new Resource("${this}.resource0", DataAccessMode.AccountSpecific)
                }))),

                new KeyValuePair <string, FunctionMetadataTemplate>(
                    "${this}.Func1",
                    new FunctionMetadataTemplate(
                        new HashSet <string>(new[] { "${this}.Func2" }),
                        new HashSet <Resource>(new[]
                {
                    new Resource("${this}.resource1", DataAccessMode.ReadOnlyAccountSharing)
                }))),

                new KeyValuePair <string, FunctionMetadataTemplate>(
                    "${this}.Func2",
                    new FunctionMetadataTemplate(
                        new HashSet <string>(),
                        new HashSet <Resource>(new[]
                {
                    new Resource("${this}.resource1", DataAccessMode.ReadOnlyAccountSharing),
                    new Resource("${this}.resource2", DataAccessMode.ReadWriteAccountSharing)
                }))),

                new KeyValuePair <string, FunctionMetadataTemplate>(
                    "${this}.Func3",
                    new FunctionMetadataTemplate(
                        new HashSet <string>(new[] { "${_contractB}.Func0", "${this}.Func0", "${ContractC}.Func0" }),
                        new HashSet <Resource>(new[]
                {
                    new Resource("${this}.resource1", DataAccessMode.ReadOnlyAccountSharing)
                }))),

                new KeyValuePair <string, FunctionMetadataTemplate>(
                    "${this}.Func4",
                    new FunctionMetadataTemplate(
                        new HashSet <string>(new[] { "${this}.Func2", "${this}.Func2" }),
                        new HashSet <Resource>())),

                new KeyValuePair <string, FunctionMetadataTemplate>(
                    "${this}.Func5",
                    new FunctionMetadataTemplate(
                        new HashSet <string>(new[] { "${_contractB}.Func1", "${this}.Func3" }),
                        new HashSet <Resource>())),
            });

            var refA = new Dictionary <string, Address>(new []
            {
                new KeyValuePair <string, Address>("ContractC", Address.FromRawBytes(ByteArrayHelpers.FromHexString("0x456745674567456745674567456745674567"))),
                new KeyValuePair <string, Address>("_contractB", Address.FromRawBytes(ByteArrayHelpers.FromHexString("0x123412341234123412341234123412341234"))),
            });

            var groundTruthTemplateA = new ContractMetadataTemplate(typeof(TestContractA).FullName, groundTruthResA,
                                                                    refA);

            Assert.Equal(groundTruthTemplateA, resA, new ContractMetadataTemplateEqualityComparer());

            //test fail cases
            await TestFailCases(runner);
        }
Exemple #2
0
        public async Task TestFailCases(SmartContractRunner runner)
        {
            var groundTruthMap =
                new Dictionary <string, Dictionary <string, FunctionMetadataTemplate> >();

            var exception = await Assert
                            .ThrowsAsync <FunctionMetadataException>(() => Task.FromResult(runner.ExtractMetadata(typeof(TestContractD))));

            Assert.True(exception.Message.Contains("Duplicate name of field attributes in contract"));

            exception = await Assert
                        .ThrowsAsync <FunctionMetadataException>(() => Task.FromResult(runner.ExtractMetadata(typeof(TestContractE))));

            Assert.True(
                exception.Message.Contains("Duplicate name of smart contract reference attributes in contract "));

            exception = await Assert
                        .ThrowsAsync <FunctionMetadataException>(() => Task.FromResult(runner.ExtractMetadata(typeof(TestContractF))));

            Assert.True(exception.Message.Contains("Unknown reference local field ${this}.resource1"));

            exception = await Assert
                        .ThrowsAsync <FunctionMetadataException>(() => Task.FromResult(runner.ExtractMetadata(typeof(TestContractG))));

            Assert.True(exception.Message.Contains("Duplicate name of function attribute"));

            exception = await Assert
                        .ThrowsAsync <FunctionMetadataException>(() => Task.FromResult(runner.ExtractMetadata(typeof(TestContractH))));

            Assert.True(exception.Message.Contains("contains unknown reference to it's own function"));

            exception = await Assert
                        .ThrowsAsync <FunctionMetadataException>(() => Task.FromResult(runner.ExtractMetadata(typeof(TestContractI))));

            Assert.True(exception.Message.Contains("contains unknown local member reference to other contract"));

            exception = await Assert
                        .ThrowsAsync <FunctionMetadataException>(() => Task.FromResult(runner.ExtractMetadata(typeof(TestContractJ))));

            Assert.True(exception.Message.Contains("is Non-DAG thus nothing take effect"));
        }