Skip to content

Commit

Permalink
AKI-509: Bugfix for block encoding/decoding inconsistency:
Browse files Browse the repository at this point in the history
 - The bug: the seal method in StakingBlock was not updating the encoding with the sealer signature.
 - The fix: removed the lazy initialization of the encoding to avoid similar issues caused by other updates.
  • Loading branch information
AlexandraRoatis committed Nov 4, 2019
1 parent 2fc8dc8 commit 674e4b5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 150 deletions.
3 changes: 0 additions & 3 deletions modAionImpl/src/org/aion/zero/impl/types/AbstractBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public boolean isEqual(Block block) {

public abstract String getShortDescr();

abstract void parseRLP();

/**
* check if param block is son of this block
*
Expand All @@ -59,7 +57,6 @@ public byte[] getEncodedBody() {
}

List<byte[]> getBodyElements() {
parseRLP();
byte[] transactions = getTransactionsEncoded();
List<byte[]> body = new ArrayList<>();
body.add(transactions);
Expand Down
87 changes: 16 additions & 71 deletions modAionImpl/src/org/aion/zero/impl/types/AionBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
public class AionBlock extends AbstractBlock {

/* Private */
private byte[] rlpEncoded;
private volatile boolean parsed = false;
private A0BlockHeader header;

/* Constructors */
Expand All @@ -35,15 +33,13 @@ public AionBlock(AionBlock block) {
}
this.header = A0BlockHeader.Builder.newInstance().withHeader(block.getHeader()).build();
this.transactionsList.addAll(block.getTransactionsList());
this.parsed = true;
}

public AionBlock(byte[] rawData) {
if (rawData == null) {
throw new NullPointerException("RlpEncoded data is null");
}

this.rlpEncoded = rawData;
parseRLP(rawData);
}

/**
Expand Down Expand Up @@ -86,7 +82,6 @@ public AionBlock(A0BlockHeader header, List<AionTransaction> transactionsList) {
this.header = header;
this.transactionsList.clear();
this.transactionsList.addAll(transactionsList);
this.parsed = true;
}

@VisibleForTesting
Expand Down Expand Up @@ -131,7 +126,6 @@ public AionBlock(
this.header = builder.build();
this.transactionsList.clear();
this.transactionsList.addAll(transactionsList);
this.parsed = true;
}

/**
Expand Down Expand Up @@ -191,30 +185,19 @@ protected AionBlock(
.withDefaultSolution();

this.header = builder.build();
this.parsed = true;
}

void parseRLP() {
if (this.parsed) {
return;
}

synchronized (this) {
if (this.parsed) return;
private void parseRLP(byte[] rawData) {
RLPList params = RLP.decode2(rawData);
RLPList block = (RLPList) params.get(0);

RLPList params = RLP.decode2(rlpEncoded);
RLPList block = (RLPList) params.get(0);

// Parse Header
RLPList header = (RLPList) block.get(0);
this.header = A0BlockHeader.Builder.newInstance().withRlpList(header).build();

// Parse Transactions
RLPList txTransactions = (RLPList) block.get(1);
this.parseTxs(this.header.getTxTrieRoot(), txTransactions);
// Parse Header
RLPList header = (RLPList) block.get(0);
this.header = A0BlockHeader.Builder.newInstance().withRlpList(header).build();

this.parsed = true;
}
// Parse Transactions
RLPList txTransactions = (RLPList) block.get(1);
this.parseTxs(this.header.getTxTrieRoot(), txTransactions);
}

@Override
Expand All @@ -223,61 +206,51 @@ public int size() {
}

public A0BlockHeader getHeader() {
parseRLP();
return this.header;
}

@Override
public byte[] getHash() {
parseRLP();
return this.header.getHash();
}

@Override
public byte[] getParentHash() {
parseRLP();
return this.header.getParentHash();
}

@Override
public AionAddress getCoinbase() {
parseRLP();
return this.header.getCoinbase();
}

@Override
public byte[] getStateRoot() {
parseRLP();
return this.header.getStateRoot();
}

@Override
public byte[] getTxTrieRoot() {
parseRLP();
return this.header.getTxTrieRoot();
}

@Override
public byte[] getReceiptsRoot() {
parseRLP();
return this.header.getReceiptsRoot();
}

@Override
public byte[] getLogBloom() {
parseRLP();
return this.header.getLogsBloom();
}

@Override
public byte[] getDifficulty() {
parseRLP();
return this.header.getDifficulty();
}

@Override
public BigInteger getDifficultyBI() {
parseRLP();
return this.header.getDifficultyBI();
}

Expand All @@ -289,46 +262,29 @@ public void updateHeader(BlockHeader _header) {
}

header = (A0BlockHeader) _header;

if (rlpEncoded == null) {
getEncoded();
}

byte[] headerEncoded = header.getEncoded();

List<byte[]> block = getBodyElements();
block.add(0, headerEncoded);
byte[][] elements = block.toArray(new byte[block.size()][]);

rlpEncoded = RLP.encodeList(elements);
}

@Override
public long getTimestamp() {
parseRLP();
return this.header.getTimestamp();
}

@Override
public long getNumber() {
parseRLP();
return this.header.getNumber();
}

@Override
public byte[] getExtraData() {
parseRLP();
return this.header.getExtraData();
}

public byte[] getNonce() {
parseRLP();
return this.header.getNonce();
}

@Override
public List<AionTransaction> getTransactionsList() {
parseRLP();
return transactionsList;
}

Expand Down Expand Up @@ -398,8 +354,6 @@ public void updateTransactionAndState(
@Override
public String toString() {
StringBuilder toStringBuff = new StringBuilder();
parseRLP();

toStringBuff.setLength(0);
toStringBuff.append("BlockData [ ");
toStringBuff.append("hash=").append(ByteUtil.toHexString(this.getHash())).append("\n");
Expand Down Expand Up @@ -477,26 +431,22 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Arrays.hashCode(rlpEncoded);
return Arrays.hashCode(getEncoded());
}

@Override
public byte[] getEncoded() {
if (rlpEncoded == null) {
byte[] header = this.header.getEncoded();
byte[] header = this.header.getEncoded();

List<byte[]> block = getBodyElements();
block.add(0, header);
byte[][] elements = block.toArray(new byte[block.size()][]);
List<byte[]> block = getBodyElements();
block.add(0, header);
byte[][] elements = block.toArray(new byte[block.size()][]);

this.rlpEncoded = RLP.encodeList(elements);
}
return rlpEncoded;
return RLP.encodeList(elements);
}

@Override
public String getShortHash() {
parseRLP();
return Hex.toHexString(getHash()).substring(0, 6);
}

Expand All @@ -514,13 +464,11 @@ public String getShortDescr() {

@Override
public long getNrgConsumed() {
parseRLP();
return this.header.getEnergyConsumed();
}

@Override
public long getNrgLimit() {
parseRLP();
return this.header.getEnergyLimit();
}

Expand All @@ -529,7 +477,6 @@ public static AionBlock createBlockFromNetwork(A0BlockHeader header, byte[] body

AionBlock block = new AionBlock();
block.header = header;
block.parsed = true;

RLPList items = (RLPList) RLP.decode2(body).get(0);
RLPList transactions = (RLPList) items.get(0);
Expand Down Expand Up @@ -568,7 +515,6 @@ public static AionBlock fromRLP(byte[] rlpEncoded, boolean isUnsafe) {

AionBlock block = new AionBlock();
block.header = header;
block.parsed = true;

// Parse Transactions
RLPList transactions = (RLPList) blockRLP.get(1);
Expand Down Expand Up @@ -616,7 +562,6 @@ public static AionBlock fromRLPList(RLPList rlpEncoded) {

AionBlock block = new AionBlock();
block.header = header;
block.parsed = true;

// Parse Transactions
RLPList transactions = (RLPList) rlpEncoded.get(1);
Expand Down
Loading

0 comments on commit 674e4b5

Please sign in to comment.