Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Commit

Permalink
feat: Complete asset transfers management (close #38)
Browse files Browse the repository at this point in the history
  • Loading branch information
federicozanardo committed Jan 27, 2023
1 parent 44ee9a0 commit 8145520
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 75 deletions.
2 changes: 1 addition & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void main(String[] args) {
} catch (IOException e) {
throw new RuntimeException(e);
}*/
// propertyId => 60e62f2c-f7e8-4385-8e06-41c87a006e13
// propertyId => a86d2ed2-a455-43e1-b3be-264c05d7fc20

// Set up the virtual machine handler
VirtualMachine virtualMachine = new VirtualMachine(
Expand Down
81 changes: 67 additions & 14 deletions src/main/java/storage/PropertiesStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class PropertiesStorage extends StorageSerializer<ArrayList<Property>> {
private final ReentrantLock mutex;

public PropertiesStorage() {
this.mutex = new ReentrantLock();
mutex = new ReentrantLock();
}

public void seed() throws IOException {
Expand All @@ -41,30 +41,30 @@ public void seed() throws IOException {
}

public ArrayList<Property> getFunds(String address) throws IOException {
this.mutex.lock();
mutex.lock();

this.levelDb = factory.open(new File(String.valueOf(Constants.PROPERTIES_PATH)), new Options());
ArrayList<Property> funds = this.deserialize(levelDb.get(bytes(address)));

if (funds == null) {
// Error: this contractInstanceId does not exist
this.mutex.unlock();
mutex.unlock();
return null;
}

this.mutex.unlock();
mutex.unlock();
return funds;
}

public Property getFund(String address, String singleUseSealId) throws IOException {
this.mutex.lock();
public Property getFund(String address, String propertyId) throws IOException {
mutex.lock();

this.levelDb = factory.open(new File(String.valueOf(Constants.PROPERTIES_PATH)), new Options());
ArrayList<Property> funds = this.deserialize(levelDb.get(bytes(address)));

if (funds == null) {
// Error: this contractInstanceId does not exist
this.mutex.unlock();
mutex.unlock();
return null;
}

Expand All @@ -75,7 +75,7 @@ public Property getFund(String address, String singleUseSealId) throws IOExcepti
while (i < funds.size() && !found) {
Property currentFund = funds.get(i);

if (currentFund.getId().equals(singleUseSealId)) {
if (currentFund.getId().equals(propertyId)) {
found = true;
fund = currentFund;
} else {
Expand All @@ -85,16 +85,17 @@ public Property getFund(String address, String singleUseSealId) throws IOExcepti

if (!found) {
// Error
this.mutex.unlock();
mutex.unlock();
return null;
}

this.mutex.unlock();
levelDb.close();
mutex.unlock();
return fund;
}

public void addFund(String address, Property fund) throws IOException {
this.mutex.lock();
mutex.lock();

this.levelDb = factory.open(new File(String.valueOf(Constants.PROPERTIES_PATH)), new Options());
ArrayList<Property> funds = null;
Expand All @@ -112,11 +113,11 @@ public void addFund(String address, Property fund) throws IOException {
funds.add(fund);
levelDb.put(bytes(address), this.serialize(funds));
levelDb.close();
this.mutex.unlock();
mutex.unlock();
}

public void addFunds(HashMap<String, SingleUseSeal> funds) throws IOException {
this.mutex.lock();
mutex.lock();
this.levelDb = factory.open(new File(String.valueOf(Constants.PROPERTIES_PATH)), new Options());

for (HashMap.Entry<String, SingleUseSeal> entry : funds.entrySet()) {
Expand Down Expand Up @@ -146,6 +147,58 @@ public void addFunds(HashMap<String, SingleUseSeal> funds) throws IOException {
}

levelDb.close();
this.mutex.unlock();
mutex.unlock();
}

public void makePropertySpent(
String address,
String propertyId,
String contractInstanceId,
String unlockScript
) throws Exception {
mutex.lock();

this.levelDb = factory.open(new File(String.valueOf(Constants.PROPERTIES_PATH)), new Options());
ArrayList<Property> funds = this.deserialize(levelDb.get(bytes(address)));
System.out.println("makePropertySpent: currentFunds => " + funds);

if (funds == null) {
// Error: this contractInstanceId does not exist
levelDb.close();
mutex.unlock();
throw new Exception("Impossible to find the funds given the following address => " + address);
}

int i = 0;
boolean found = false;

while (i < funds.size() && !found) {
Property currentFund = funds.get(i);

if (currentFund.getId().equals(propertyId)) {
found = true;
} else {
i++;
}
}

if (!found) {
// Error
levelDb.close();
mutex.unlock();
throw new Exception("Impossible to find the funds given the following address => " + address +
" and the propertyId => " + propertyId);
}

// Update the property
funds.get(i).setContractInstanceId(contractInstanceId);
funds.get(i).setUnlockScript(unlockScript);

// Save
levelDb.put(bytes(address), this.serialize(funds));
System.out.println("makePropertySpent: updatedFunds => " + funds);

levelDb.close();
mutex.unlock();
}
}
25 changes: 25 additions & 0 deletions src/main/java/storage/PropertyUpdateData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package storage;

public class PropertyUpdateData {
private final String propertyId;
private final String contractInstanceId;
private final String unlockScript;

public PropertyUpdateData(String propertyId, String contractInstanceId, String unlockScript) {
this.propertyId = propertyId;
this.contractInstanceId = contractInstanceId;
this.unlockScript = unlockScript;
}

public String getPropertyId() {
return propertyId;
}

public String getContractInstanceId() {
return contractInstanceId;
}

public String getUnlockScript() {
return unlockScript;
}
}
1 change: 0 additions & 1 deletion src/main/java/vm/SmartContractVirtualMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lib.datastructures.Stack;
import models.address.Address;
import models.contract.SingleUseSeal;
import storage.PropertyUpdateData;
import vm.trap.Trap;
import vm.trap.TrapErrorCodes;
import vm.types.*;
Expand Down
Loading

0 comments on commit 8145520

Please sign in to comment.