-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added check on veichle identification number uniqueness #4
Open
paolocappelletti
wants to merge
3
commits into
dev
Choose a base branch
from
vin_unique_check
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+208
−31
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 68 additions & 8 deletions
76
src/main/java/io/horizen/lambo/CarRegistryApplicationState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,99 @@ | ||
package io.horizen.lambo; | ||
|
||
import com.google.inject.Inject; | ||
import com.horizen.block.SidechainBlock; | ||
import com.horizen.box.Box; | ||
import com.horizen.proposition.Proposition; | ||
import com.horizen.state.ApplicationState; | ||
import com.horizen.state.SidechainStateReader; | ||
import com.horizen.transaction.BoxTransaction; | ||
import io.horizen.lambo.car.box.CarBox; | ||
import io.horizen.lambo.car.box.CarSellOrderBox; | ||
import io.horizen.lambo.car.services.CarInfoDBService; | ||
import io.horizen.lambo.car.transaction.CarDeclarationTransaction; | ||
import scala.util.Success; | ||
import scala.util.Try; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import scala.collection.JavaConverters; | ||
|
||
// There is no custom logic for Car registry State now. | ||
// TODO: prevent the declaration of CarBoxes which car information already exists in the previously added CarBoxes or CarSellOrderBoxes. | ||
public class CarRegistryApplicationState implements ApplicationState { | ||
|
||
private CarInfoDBService carInfoDbService; | ||
|
||
@Inject | ||
public CarRegistryApplicationState(CarInfoDBService carInfoDbService) { | ||
this.carInfoDbService = carInfoDbService; | ||
} | ||
|
||
@Override | ||
public boolean validate(SidechainStateReader stateReader, SidechainBlock block) { | ||
//We check that there are no multiple transactions declaring the same VIN inside the block | ||
Set<String> vinList = new HashSet<>(); | ||
for (BoxTransaction<Proposition, Box<Proposition>> t : JavaConverters.seqAsJavaList(block.transactions())){ | ||
if (CarDeclarationTransaction.class.isInstance(t)){ | ||
for (String currentVin : carInfoDbService.extractVinFromBoxes(t.newBoxes())){ | ||
if (vinList.contains(currentVin)){ | ||
return false; | ||
}else{ | ||
vinList.add(currentVin); | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean validate(SidechainStateReader stateReader, BoxTransaction<Proposition, Box<Proposition>> transaction) { | ||
// TODO: here we expect to go though all CarDeclarationTransactions and verify that each CarBox reflects to unique Car. | ||
// we go though all CarDeclarationTransactions and verify that each CarBox reflects to unique Car. | ||
if (CarDeclarationTransaction.class.isInstance(transaction)){ | ||
Set<String> vinList = carInfoDbService.extractVinFromBoxes(transaction.newBoxes()); | ||
for (String vin : vinList) { | ||
if (! carInfoDbService.validateVin(vin, Optional.empty())){ | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public Try<ApplicationState> onApplyChanges(SidechainStateReader stateReader, byte[] version, List<Box<Proposition>> newBoxes, List<byte[]> boxIdsToRemove) { | ||
// TODO: here we expect to update Car info database. The data from it will be used during validation. | ||
public Try<ApplicationState> onApplyChanges(SidechainStateReader stateReader, | ||
byte[] version, | ||
List<Box<Proposition>> newBoxes, List<byte[]> boxIdsToRemove) { | ||
//we update the Car info database. The data from it will be used during validation. | ||
|
||
//collect the vin to be added: the ones declared in new boxes | ||
Set<String> vinToAdd = carInfoDbService.extractVinFromBoxes(newBoxes); | ||
//collect the vin to be removed: the ones contained in the removed boxes that are not present in the prevoius list | ||
Set<String> vinToRemove = new HashSet<>(); | ||
for (byte[] boxId : boxIdsToRemove) { | ||
stateReader.getClosedBox(boxId).ifPresent( box -> { | ||
if (box instanceof CarBox){ | ||
String vin = ((CarBox)box).getVin(); | ||
if (!vinToAdd.contains(vin)){ | ||
vinToRemove.add(vin); | ||
} | ||
} else if (box instanceof CarSellOrderBox){ | ||
String vin = ((CarSellOrderBox)box).getVin(); | ||
if (!vinToAdd.contains(vin)){ | ||
vinToRemove.add(vin); | ||
} | ||
} | ||
} | ||
); | ||
} | ||
carInfoDbService.updateVin(version, vinToAdd, vinToRemove); | ||
return new Success<>(this); | ||
} | ||
|
||
|
||
@Override | ||
public Try<ApplicationState> onRollback(byte[] version) { | ||
// TODO: rollback car info database to certain point. | ||
carInfoDbService.rollback(version); | ||
return new Success<>(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/main/java/io/horizen/lambo/car/services/CarInfoDBService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package io.horizen.lambo.car.services; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.name.Named; | ||
import com.horizen.box.Box; | ||
import com.horizen.node.NodeMemoryPool; | ||
import com.horizen.proposition.Proposition; | ||
import com.horizen.storage.Storage; | ||
import com.horizen.transaction.BoxTransaction; | ||
import com.horizen.utils.ByteArrayWrapper; | ||
import com.horizen.utils.Pair; | ||
import io.horizen.lambo.car.box.CarBox; | ||
import io.horizen.lambo.car.box.CarSellOrderBox; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import scorex.crypto.hash.Blake2b256; | ||
import java.util.*; | ||
|
||
/** | ||
* This service manages a local db with the list of all veichle identification numbers (vin) declared on the chain. | ||
* The vin could be present inside two type of boxes: CarBox and CarSellOrderBox. | ||
*/ | ||
public class CarInfoDBService { | ||
|
||
private Storage carInfoStorage; | ||
protected Logger log = LoggerFactory.getLogger(CarInfoDBService.class.getName()); | ||
|
||
@Inject | ||
public CarInfoDBService(@Named("CarInfoStorage") Storage carInfoStorage){ | ||
this.carInfoStorage = carInfoStorage; | ||
} | ||
|
||
public void updateVin(byte[] version, Set<String> vinToAdd, Set<String> vinToRemove){ | ||
log.debug("carInfoStorage updateVin"); | ||
log.debug(" vinToAdd "+vinToAdd.size()); | ||
log.debug(" vinToRemove "+vinToRemove.size()); | ||
List<Pair<ByteArrayWrapper, ByteArrayWrapper>> toUpdate = new ArrayList<>(vinToAdd.size()); | ||
List<ByteArrayWrapper> toRemove = new ArrayList<>(vinToRemove.size()); | ||
vinToAdd.forEach(ele -> { | ||
toUpdate.add(buildDBElement(ele)); | ||
}); | ||
vinToRemove.forEach(ele -> { | ||
toRemove.add(buildDBElement(ele).getKey()); | ||
}); | ||
carInfoStorage.update(new ByteArrayWrapper(version), toUpdate, toRemove); | ||
log.debug("carInfoStorage now contains: "+carInfoStorage.getAll().size()+" elements"); | ||
} | ||
|
||
|
||
/** | ||
* Validate the given vehicle identification number against the db list and (optionally) the mempool transactions. | ||
* @param vin the vehicle identification number to check | ||
* @param memoryPool if not null, the vin is checked also against the mempool transactions | ||
* @return true if the vin is valid (not already declared) | ||
*/ | ||
public boolean validateVin(String vin, Optional<NodeMemoryPool> memoryPool){ | ||
if (carInfoStorage.get(buildDBElement(vin).getKey()).isPresent()){ | ||
return false; | ||
} | ||
//in the vin is not found, and the mempool was provided, we check also there | ||
if (memoryPool.isPresent()) { | ||
for (BoxTransaction<Proposition, Box<Proposition>> transaction : memoryPool.get().getTransactions()) { | ||
Set<String> vinInMempool = extractVinFromBoxes(transaction.newBoxes()); | ||
if (vinInMempool.contains(vin)){ | ||
return false; | ||
} | ||
} | ||
} | ||
//if we arrive here, the vin is valid | ||
return true; | ||
} | ||
|
||
public void rollback(byte[] version) { | ||
carInfoStorage.rollback(new ByteArrayWrapper(version)); | ||
} | ||
|
||
/** | ||
* Extracts the list of vehicle identification numbers (vin) declared in the given box list. | ||
* The vin could be present inside two type of boxes: CarBox and CarSellOrderBox | ||
*/ | ||
public Set<String> extractVinFromBoxes(List<Box<Proposition>> boxes){ | ||
Set<String> vinList = new HashSet<String>(); | ||
for (Box<Proposition> currentBox : boxes) { | ||
if (CarBox.class.isAssignableFrom(currentBox.getClass())){ | ||
String vin = CarBox.parseBytes(currentBox.bytes()).getVin(); | ||
vinList.add(vin); | ||
} else if (CarSellOrderBox.class.isAssignableFrom(currentBox.getClass())){ | ||
String vin = CarSellOrderBox.parseBytes(currentBox.bytes()).getVin(); | ||
vinList.add(vin); | ||
} | ||
} | ||
return vinList; | ||
} | ||
|
||
|
||
|
||
private Pair<ByteArrayWrapper, ByteArrayWrapper> buildDBElement(String vin){ | ||
//we hash the vin to be sure the key has a fixed size of 32, which is the default of iohk.iodb used as underline storage | ||
ByteArrayWrapper keyWrapper = new ByteArrayWrapper(Blake2b256.hash(vin)); | ||
//the value is not important (we need just a key set, each key is a vin hash) | ||
ByteArrayWrapper valueWrapper = new ByteArrayWrapper(new byte[1]); | ||
return new Pair<>(keyWrapper, valueWrapper); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CarInfoDBService must be controlled only by CarRegistryApplicationState class. Other way it is possible to change the state of CarInfoDBService directly in
CarApi
.I guess that it is done in this way, because there is no way to get access to the application state instance inside the CarApi, because there is no interface method inside the
SidechainNodeView
, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PS: in the SDK we are going to add a possibility to get ApplicationState and ApplicationWallet instances from the SidechainNodeView
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes the reason was that one!
wating for the method will be available in SDK I could otherwise inject ApplicationState in the constructor
what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep it as is and post the issue to the lambo repo.
After the changes in the SDK we will change here as well.
Note: it's important, that
SidechainNodeView
should provide the access to the "read-only" part of theCarRegistryApplicationState
, otherwise we will have the same issue as now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok! I add the issue