public void Repository_CommitAndRollbackTest() { ISource <byte[], byte[]> stateDB = new NoDeleteSource <byte[], byte[]>(new MemoryDictionarySource()); ContractStateRoot repository = new ContractStateRoot(stateDB); IContractState txTrack = repository.StartTracking(); txTrack.CreateAccount(testAddress); txTrack.SetStorageValue(testAddress, dog, cat); txTrack.Commit(); repository.Commit(); byte[] root1 = repository.Root; IContractState txTrack2 = repository.StartTracking(); txTrack2.SetStorageValue(testAddress, dog, fish); txTrack2.Rollback(); IContractState txTrack3 = repository.StartTracking(); txTrack3.SetStorageValue(testAddress, dodecahedron, bird); txTrack3.Commit(); repository.Commit(); byte[] upToDateRoot = repository.Root; Assert.Equal(cat, repository.GetStorageValue(testAddress, dog)); Assert.Equal(bird, repository.GetStorageValue(testAddress, dodecahedron)); IContractState snapshot = repository.GetSnapshotTo(root1); repository.SyncToRoot(root1); Assert.Equal(cat, snapshot.GetStorageValue(testAddress, dog)); Assert.Null(snapshot.GetStorageValue(testAddress, dodecahedron)); }
public void VM_CreateContract_WithParameters() { //Get the contract execution code------------------------ SmartContractCompilationResult compilationResult = SmartContractCompiler.CompileFile("SmartContracts/Auction.cs"); Assert.True(compilationResult.Success); byte[] contractExecutionCode = compilationResult.Compilation; //------------------------------------------------------- //Set the calldata for the transaction---------- var methodParameters = new object[] { (ulong)5 }; var callData = new CreateData((Gas)5000000, contractExecutionCode, methodParameters); Money value = Money.Zero; //------------------------------------------------------- var repository = new ContractStateRoot(new NoDeleteSource <byte[], byte[]>(new MemoryDictionarySource())); IContractState track = repository.StartTracking(); var gasMeter = new GasMeter(callData.GasLimit); var transactionContext = new TransactionContext( txHash: uint256.One, blockHeight: 1, coinbase: TestAddress.ToUint160(this.network), sender: TestAddress.ToUint160(this.network), amount: value ); VmExecutionResult result = this.vm.Create(gasMeter, repository, callData, transactionContext); track.Commit(); var endBlockValue = track.GetStorageValue(result.NewContractAddress, Encoding.UTF8.GetBytes("EndBlock")); Assert.Equal(6, BitConverter.ToInt16(endBlockValue, 0)); Assert.Equal(TestAddress.ToUint160(this.network).ToBytes(), track.GetStorageValue(result.NewContractAddress, Encoding.UTF8.GetBytes("Owner"))); }
public void VM_ExecuteContract_WithParameters() { //Get the contract execution code------------------------ SmartContractCompilationResult compilationResult = SmartContractCompiler.CompileFile("SmartContracts/StorageTestWithParameters.cs"); Assert.True(compilationResult.Success); byte[] contractExecutionCode = compilationResult.Compilation; //------------------------------------------------------- //Set the calldata for the transaction---------- var methodParameters = new object[] { (short)5 }; var callData = new CallData((Gas)5000000, new uint160(1), "StoreData", methodParameters); Money value = Money.Zero; //------------------------------------------------------- var repository = new ContractStateRoot(new NoDeleteSource <byte[], byte[]>(new MemoryDictionarySource())); IContractState track = repository.StartTracking(); var gasMeter = new GasMeter(callData.GasLimit); uint160 address = TestAddress.ToUint160(this.network); var transactionContext = new TransactionContext(uint256.One, 1, address, address, value); repository.SetCode(callData.ContractAddress, contractExecutionCode); repository.SetContractType(callData.ContractAddress, "StorageTestWithParameters"); VmExecutionResult result = this.vm.ExecuteMethod(gasMeter, repository, callData, transactionContext); track.Commit(); Assert.Equal(5, BitConverter.ToInt16(track.GetStorageValue(callData.ContractAddress, Encoding.UTF8.GetBytes("orders")), 0)); Assert.Equal(5, BitConverter.ToInt16(repository.GetStorageValue(callData.ContractAddress, Encoding.UTF8.GetBytes("orders")), 0)); }