示例#1
0
 public ICOContractTests()
 {
     this.mContractLogger      = new Mock <IContractLogger>();
     this.mContractState       = new Mock <ISmartContractState>();
     this.mTransactionExecutor = new Mock <IInternalTransactionExecutor>();
     this.persistentState      = new InMemoryState();
     this.network = new Mock <Network>();
     this.mBlock  = new Mock <IBlock>();
     this.mContractState.Setup(s => s.Block).Returns(this.mBlock.Object);
     this.mContractState.Setup(s => s.PersistentState).Returns(this.persistentState);
     this.mContractState.Setup(s => s.ContractLogger).Returns(this.mContractLogger.Object);
     this.mContractState.Setup(s => s.InternalTransactionExecutor).Returns(this.mTransactionExecutor.Object);
     this.serializer = new Serializer(new ContractPrimitiveSerializer(this.network.Object));
     this.mContractState.Setup(s => s.Serializer).Returns(this.serializer);
     this.sender         = "0x0000000000000000000000000000000000000001".HexToAddress();
     this.owner          = "0x0000000000000000000000000000000000000002".HexToAddress();
     this.investor       = "0x0000000000000000000000000000000000000003".HexToAddress();
     this.identity       = "0x0000000000000000000000000000000000000004".HexToAddress();
     this.contract       = "0x0000000000000000000000000000000000000005".HexToAddress();
     this.tokenContract  = "0x0000000000000000000000000000000000000006".HexToAddress();
     this.kycContract    = "0x0000000000000000000000000000000000000007".HexToAddress();
     this.mapperContract = "0x0000000000000000000000000000000000000008".HexToAddress();
     this.createSuccess  = CreateResult.Succeeded(this.tokenContract);
     this.name           = "Test Token";
     this.symbol         = "TST";
     this.totalSupply    = 100 * Satoshis;
     this.persistentState.IsContractResult = true;
 }
        public void Create_GasRemaining_Error()
        {
            ulong amount     = 100UL;
            var   parameters = new object[] { };
            var   gasLimit   = (Gas)100_000;

            var fixture = new InternalExecutorTestFixture();

            fixture.SetGasMeterLimitBelow(gasLimit);

            var internalExecutor = new InternalExecutor(
                fixture.LoggerFactory,
                fixture.Network,
                fixture.State.Object,
                fixture.StateProcessor.Object);

            ICreateResult result = internalExecutor.Create <string>(fixture.SmartContractState, amount, parameters, gasLimit);

            fixture.State.Verify(s => s.Snapshot(), Times.Never);

            fixture.StateProcessor.Verify(sp =>
                                          sp.Apply(fixture.Snapshot, It.IsAny <InternalCreateMessage>()), Times.Never);

            fixture.State.Verify(s => s.TransitionTo(fixture.Snapshot), Times.Never);

            fixture.GasMeter.Verify(g => g.Spend(It.IsAny <Gas>()), Times.Never);

            Assert.False(result.Success);
            Assert.Equal(default(Address), result.NewContractAddress);
        }
        public void Create_StateTransition_Error()
        {
            ulong amount     = 100UL;
            var   parameters = new object[] { };
            var   gasLimit   = (Gas)100_000;

            var fixture = new InternalExecutorTestFixture();

            fixture.SetGasMeterLimitAbove(gasLimit);

            StateTransitionResult stateTransitionResult = StateTransitionResult.Fail((Gas)1000, new ContractErrorMessage("Error"));

            fixture.StateProcessor
            .Setup(sp => sp.Apply(It.IsAny <IState>(), It.IsAny <InternalCreateMessage>()))
            .Returns(stateTransitionResult);

            var internalExecutor = new InternalExecutor(
                fixture.LoggerFactory,
                fixture.Network,
                fixture.State.Object,
                fixture.StateProcessor.Object);

            ICreateResult result = internalExecutor.Create <string>(fixture.SmartContractState, amount, parameters, gasLimit);

            fixture.State.Verify(s => s.Snapshot(), Times.Once);

            fixture.StateProcessor.Verify(sp =>
                                          sp.Apply(fixture.Snapshot, It.Is <InternalCreateMessage>(m =>
                                                                                                   m.Amount == amount &&
                                                                                                   m.Parameters == parameters &&
                                                                                                   m.GasLimit == gasLimit &&
                                                                                                   m.From == fixture.FromAddress &&
                                                                                                   m.Type == typeof(string).Name
                                                                                                   )));

            fixture.State.Verify(s => s.TransitionTo(fixture.Snapshot), Times.Never);

            fixture.GasMeter.Verify(g => g.Spend(stateTransitionResult.GasConsumed));

            Assert.False(result.Success);
            Assert.Equal(default(Address), result.NewContractAddress);
        }
示例#4
0
        public void Create_StateTransition_Success()
        {
            ulong amount     = 100UL;
            var   parameters = new object[] { };
            var   gasLimit   = (RuntimeObserver.Gas)SmartContractFormatLogic.GasLimitMaximum;

            var fixture = new InternalExecutorTestFixture();

            fixture.SetGasMeterLimitAbove(gasLimit);

            StateTransitionResult stateTransitionResult = StateTransitionResult.Ok((RuntimeObserver.Gas) 1000, uint160.One, new object());

            fixture.StateProcessor
            .Setup(sp => sp.Apply(It.IsAny <IState>(), It.IsAny <InternalCreateMessage>()))
            .Returns(stateTransitionResult);

            var internalExecutor = new InternalExecutor(
                fixture.GasMeter.Object,
                fixture.State.Object,
                fixture.StateProcessor.Object);

            ICreateResult result = internalExecutor.Create <string>(fixture.SmartContractState, amount, parameters, gasLimit);

            fixture.State.Verify(s => s.Snapshot(), Times.Once);

            fixture.StateProcessor.Verify(sp =>
                                          sp.Apply(fixture.Snapshot, It.Is <InternalCreateMessage>(m =>
                                                                                                   m.Amount == amount &&
                                                                                                   m.Parameters == parameters &&
                                                                                                   m.GasLimit == gasLimit &&
                                                                                                   m.From == fixture.FromAddress &&
                                                                                                   m.Type == typeof(string).Name
                                                                                                   )));

            fixture.State.Verify(s => s.TransitionTo(fixture.Snapshot));

            fixture.GasMeter.Verify(g => g.Spend(stateTransitionResult.GasConsumed));

            Assert.True(result.Success);
            Assert.Equal(stateTransitionResult.Success.ContractAddress.ToAddress(), result.NewContractAddress);
        }