Skip to content

Commit

Permalink
Merge pull request #1 from iotadevelopment/is-snapshot
Browse files Browse the repository at this point in the history
introduce isSnapshot flag on transactions
  • Loading branch information
Hans Moog authored Jul 23, 2018
2 parents f2b2d04 + bbd4042 commit a9eb656
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/iota/iri/Milestone.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ public void init(final SpongeFactory.Mode mode, final LedgerValidator ledgerVali
latestMilestone = milestoneViewModel.getHash();
latestMilestoneIndex = milestoneViewModel.index();
}

// mark the transaction as a snapshot
t.setIsSnapshot(tangle, true);

// set the snapshot index of the transaction (a milestone is verified by
// itself + this allows us to retrieve the matching milestone object
// efficiently)
t.setSnapshot(tangle, milestoneViewModel.index());
break;
case INCOMPLETE:
analyzedMilestoneCandidates.remove(t.getHash());
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/iota/iri/controllers/TransactionViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,17 @@ public void setSnapshot(Tangle tangle, final int index) throws Exception {
}
}

public void setIsSnapshot(Tangle tangle, final boolean isSnapshot) throws Exception {
if ( isSnapshot != transaction.isSnapshot ) {
transaction.isSnapshot = isSnapshot;
update(tangle, "isSnapshot");
}
}

public boolean getIsSnapshot() {
return transaction.isSnapshot;
}

public long getHeight() {
return transaction.height;
}
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/com/iota/iri/model/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
public class Transaction implements Persistable {
public static final int SIZE = 1604;

// bitmasks used to encode the boolean values in 1 byte
public static int IS_SOLID_BITMASK = 0b01;
public static int IS_SNAPSHOT_BITMASK = 0b10;

public byte[] bytes;

public Hash address;
Expand All @@ -36,6 +40,7 @@ public class Transaction implements Persistable {
//public boolean confirmed = false;
public boolean parsed = false;
public boolean solid = false;
public boolean isSnapshot = false;
public long height = 0;
public String sender = "";
public int snapshot;
Expand Down Expand Up @@ -81,7 +86,13 @@ public byte[] metadata() {
buffer.put(Serializer.serialize(arrivalTime));
buffer.put(Serializer.serialize(height));
//buffer.put((byte) (confirmed ? 1:0));
buffer.put((byte) (solid ? 1 : 0));

// encode booleans in 1 byte
byte flags = 0;
flags |= solid ? IS_SOLID_BITMASK : 0;
flags |= isSnapshot ? IS_SNAPSHOT_BITMASK : 0;
buffer.put(flags);

buffer.put(Serializer.serialize(snapshot));
buffer.put(sender.getBytes());
return buffer.array();
Expand Down Expand Up @@ -131,7 +142,11 @@ public void readMetadata(byte[] bytes) {
confirmed = bytes[i] == 1;
i++;
*/
solid = bytes[i] == 1;

// decode the boolean byte by checking the bitmasks
solid = (bytes[i] & IS_SOLID_BITMASK) != 0;
isSnapshot = (bytes[i] & IS_SNAPSHOT_BITMASK) != 0;

i++;
snapshot = Serializer.getInteger(bytes, i);
i += Integer.BYTES;
Expand Down

0 comments on commit a9eb656

Please sign in to comment.