public void Test20DBreeze() { DBreezeEngine engine = new DBreezeEngine(DbreezeTestLocation); using (DBreeze.Transactions.Transaction t = engine.GetTransaction()) { t.RemoveAllKeys(DbreezeTestDb, true); t.Commit(); } ISource <byte[], byte[]> stateDB = new NoDeleteSource <byte[], byte[]>(new DBreezeByteStore(engine, DbreezeTestDb)); ContractStateRoot repository = new ContractStateRoot(stateDB); byte[] root = repository.Root; uint160 cowAddress = new uint160(cow); uint160 horseAddress = new uint160(horse); IContractState track2 = repository.StartTracking(); //repository track2.SetStorageValue(cowAddress, cowKey1, cowVal1); track2.SetStorageValue(horseAddress, horseKey1, horseVal1); track2.Commit(); repository.Commit(); byte[] root2 = repository.Root; track2 = repository.StartTracking(); //repository track2.SetStorageValue(cowAddress, cowKey2, cowVal0); track2.SetStorageValue(horseAddress, horseKey2, horseVal0); track2.Commit(); repository.Commit(); byte[] root3 = repository.Root; IContractState snapshot = new ContractStateRoot(stateDB, root); Assert.Null(snapshot.GetStorageValue(cowAddress, cowKey1)); Assert.Null(snapshot.GetStorageValue(cowAddress, cowKey2)); Assert.Null(snapshot.GetStorageValue(horseAddress, horseKey1)); Assert.Null(snapshot.GetStorageValue(horseAddress, horseKey2)); snapshot = new ContractStateRoot(stateDB, root2); Assert.Equal(cowVal1, snapshot.GetStorageValue(cowAddress, cowKey1)); Assert.Null(snapshot.GetStorageValue(cowAddress, cowKey2)); Assert.Equal(horseVal1, snapshot.GetStorageValue(horseAddress, horseKey1)); Assert.Null(snapshot.GetStorageValue(horseAddress, horseKey2)); snapshot = new ContractStateRoot(stateDB, root3); Assert.Equal(cowVal1, snapshot.GetStorageValue(cowAddress, cowKey1)); Assert.Equal(cowVal0, snapshot.GetStorageValue(cowAddress, cowKey2)); Assert.Equal(horseVal1, snapshot.GetStorageValue(horseAddress, horseKey1)); Assert.Equal(horseVal0, snapshot.GetStorageValue(horseAddress, horseKey2)); }
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 Test20() { ISource <byte[], byte[]> stateDB = new NoDeleteSource <byte[], byte[]>(new MemoryDictionarySource()); ContractStateRoot repository = new ContractStateRoot(stateDB); byte[] root = repository.Root; uint160 cowAddress = new uint160(cow); uint160 horseAddress = new uint160(horse); IContractState track2 = repository.StartTracking(); //repository track2.SetStorageValue(cowAddress, cowKey1, cowVal1); track2.SetStorageValue(horseAddress, horseKey1, horseVal1); track2.Commit(); repository.Commit(); byte[] root2 = repository.Root; track2 = repository.StartTracking(); //repository track2.SetStorageValue(cowAddress, cowKey2, cowVal0); track2.SetStorageValue(horseAddress, horseKey2, horseVal0); track2.Commit(); repository.Commit(); byte[] root3 = repository.Root; IContractState snapshot = new ContractStateRoot(stateDB, root); Assert.Null(snapshot.GetStorageValue(cowAddress, cowKey1)); Assert.Null(snapshot.GetStorageValue(cowAddress, cowKey2)); Assert.Null(snapshot.GetStorageValue(horseAddress, horseKey1)); Assert.Null(snapshot.GetStorageValue(horseAddress, horseKey2)); snapshot = new ContractStateRoot(stateDB, root2); Assert.Equal(cowVal1, snapshot.GetStorageValue(cowAddress, cowKey1)); Assert.Null(snapshot.GetStorageValue(cowAddress, cowKey2)); Assert.Equal(horseVal1, snapshot.GetStorageValue(horseAddress, horseKey1)); Assert.Null(snapshot.GetStorageValue(horseAddress, horseKey2)); snapshot = new ContractStateRoot(stateDB, root3); Assert.Equal(cowVal1, snapshot.GetStorageValue(cowAddress, cowKey1)); Assert.Equal(cowVal0, snapshot.GetStorageValue(cowAddress, cowKey2)); Assert.Equal(horseVal1, snapshot.GetStorageValue(horseAddress, horseKey1)); Assert.Equal(horseVal0, snapshot.GetStorageValue(horseAddress, horseKey2)); }
public void Repository_CommitPushesToUnderlyingSource() { 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); Assert.Null(repository.GetStorageValue(testAddress, dog)); txTrack.Commit(); Assert.Equal(cat, repository.GetStorageValue(testAddress, dog)); }
public void Test4() { MemoryDictionarySource source = new MemoryDictionarySource(); ContractStateRoot root = new ContractStateRoot(source); IContractState repository = root.StartTracking(); repository.SetStorageValue(new uint160(cow), cowKey, cowValue); repository.SetStorageValue(new uint160(horse), horseKey, horseValue); repository.Commit(); Assert.Equal(cowValue, root.GetStorageValue(new uint160(cow), cowKey)); Assert.Equal(horseValue, root.GetStorageValue(new uint160(horse), horseKey)); }
private StateTransitionResult ApplyCall(CallMessage message, byte[] contractCode) { if (this.GasRemaining < message.GasLimit || this.GasRemaining < GasPriceList.BaseCost) { return(StateTransitionResult.Fail((Gas)0, StateTransitionErrorKind.InsufficientGas)); } var gasMeter = new GasMeter(message.GasLimit); gasMeter.Spend((Gas)GasPriceList.BaseCost); if (message.Method.Name == null) { return(StateTransitionResult.Fail(gasMeter.GasConsumed, StateTransitionErrorKind.NoMethodName)); } StateSnapshot stateSnapshot = this.TakeSnapshot(); IContractState state = this.CreateIntermediateState(); string type = state.GetContractType(message.To); ISmartContractState smartContractState = this.CreateSmartContractState(gasMeter, message.To, message, state); VmExecutionResult result = this.Vm.ExecuteMethod(smartContractState, message.Method, contractCode, type); this.GasRemaining -= gasMeter.GasConsumed; bool revert = result.ExecutionException != null; if (revert) { this.Rollback(stateSnapshot); return(StateTransitionResult.Fail( gasMeter.GasConsumed, result.ExecutionException)); } state.Commit(); this.GasRemaining -= gasMeter.GasConsumed; return(StateTransitionResult.Ok( gasMeter.GasConsumed, message.To, result.Result )); }
private StateTransitionResult ApplyCreate(object[] parameters, byte[] code, BaseMessage message, string type = null) { if (this.GasRemaining < message.GasLimit || this.GasRemaining < GasPriceList.BaseCost) { return(StateTransitionResult.Fail((Gas)0, StateTransitionErrorKind.InsufficientGas)); } StateSnapshot stateSnapshot = this.TakeSnapshot(); var gasMeter = new GasMeter(message.GasLimit); gasMeter.Spend((Gas)GasPriceList.BaseCost); uint160 address = this.GetNewAddress(); // Begin tracking the new intermediate state. We need to keep this reference around // for the scope of the transaction, so we can commit it later. IContractState state = this.CreateIntermediateState(); state.CreateAccount(address); ISmartContractState smartContractState = this.CreateSmartContractState(gasMeter, address, message, state); VmExecutionResult result = this.Vm.Create(state, smartContractState, code, parameters, type); this.GasRemaining -= gasMeter.GasConsumed; bool revert = result.ExecutionException != null; if (revert) { this.Rollback(stateSnapshot); return(StateTransitionResult.Fail( gasMeter.GasConsumed, result.ExecutionException)); } state.Commit(); return(StateTransitionResult.Ok( gasMeter.GasConsumed, address, result.Result )); }
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)); }