Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

[PAN-2935] Add nonce handling to GenesisState #1728

Merged
merged 3 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class GenesisAllocation {
private final String address;
private final JsonObject data;

public GenesisAllocation(final String address, final JsonObject data) {
GenesisAllocation(final String address, final JsonObject data) {
this.address = address;
this.data = data;
}
Expand All @@ -37,6 +37,10 @@ public String getCode() {
return data.getString("code");
}

public String getNonce() {
return data.getString("nonce", "0");
}

public String getVersion() {
return data.getString("version");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private static void writeAccountsTo(
genesisAccounts.forEach(
genesisAccount -> {
final MutableAccount account = updater.getOrCreate(genesisAccount.address);
account.setNonce(genesisAccount.nonce);
account.setBalance(genesisAccount.balance);
account.setCode(genesisAccount.code);
account.setVersion(genesisAccount.version);
Expand Down Expand Up @@ -194,16 +195,15 @@ private static Stream<GenesisAccount> parseAllocations(final GenesisConfigFile g
}

private static long parseNonce(final GenesisConfigFile genesis) {
return withNiceErrorMessage(
"nonce",
genesis.getNonce(),
value -> {
String nonce = value.toLowerCase(Locale.US);
if (nonce.startsWith("0x")) {
nonce = nonce.substring(2);
}
return Long.parseUnsignedLong(nonce, 16);
});
return withNiceErrorMessage("nonce", genesis.getNonce(), GenesisState::parseUnsignedLong);
}

private static long parseUnsignedLong(final String value) {
String nonce = value.toLowerCase(Locale.US);
if (nonce.startsWith("0x")) {
nonce = nonce.substring(2);
}
return Long.parseUnsignedLong(nonce, 16);
}

@Override
Expand All @@ -216,6 +216,7 @@ public String toString() {

private static final class GenesisAccount {

final long nonce;
final Address address;
final Wei balance;
final Map<UInt256, UInt256> storage;
Expand All @@ -224,6 +225,7 @@ private static final class GenesisAccount {

static GenesisAccount fromAllocation(final GenesisAllocation allocation) {
return new GenesisAccount(
allocation.getNonce(),
allocation.getAddress(),
allocation.getBalance(),
allocation.getStorage(),
Expand All @@ -232,11 +234,13 @@ static GenesisAccount fromAllocation(final GenesisAllocation allocation) {
}

private GenesisAccount(
final String hexNonce,
final String hexAddress,
final String balance,
final Map<String, Object> storage,
final String hexCode,
final String version) {
this.nonce = withNiceErrorMessage("nonce", hexNonce, GenesisState::parseUnsignedLong);
this.address = withNiceErrorMessage("address", hexAddress, Address::fromHexString);
this.balance = withNiceErrorMessage("balance", balance, this::parseBalance);
this.code = hexCode != null ? BytesValue.fromHexString(hexCode) : null;
Expand Down Expand Up @@ -270,6 +274,7 @@ private Map<UInt256, UInt256> parseStorage(final Map<String, Object> storage) {
public String toString() {
return MoreObjects.toStringHelper(this)
.add("address", address)
.add("nonce", nonce)
.add("balance", balance)
.add("storage", storage)
.add("code", code)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ public void createFromJsonWithVersion() throws Exception {
"genesis4.json", "0x3224ddae856381f5fb67492b4561ecbc0cb1e9e50e6cf3238f6e049fe95a8604", 1);
}

@Test
public void createFromJsonWithNonce() throws Exception {
final GenesisState genesisState =
GenesisState.fromJson(
Resources.toString(
GenesisStateTest.class.getResource("genesisNonce.json"), Charsets.UTF_8),
MainnetProtocolSchedule.create());
final BlockHeader header = genesisState.getBlock().getHeader();
assertThat(header.getHash())
.isEqualTo(
Hash.fromHexString(
"0x36750291f1a8429aeb553a790dc2d149d04dbba0ca4cfc7fd5eb12d478117c9f"));
}

@Test
public void encodeOlympicBlock() throws Exception {
final GenesisState genesisState =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"config" : {
"ethash" : {
"fixeddifficulty" : 0
},
"homesteadBlock" : 0,
"eip150Block" : 0,
"eip158Block" : 0,
"byzantiumBlock" : 0,
"constantinopleBlock" : 0
},
"coinbase" : "0x8888f1f195afa192cfee860698584c030f4c9db1",
"difficulty" : "0x020000",
"extraData" : "0x42",
"gasLimit" : "0x2fefd8",
"mixHash" : "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"nonce" : "0x0102030405060708",
"timestamp" : "0x54c98c81",
"alloc" : {
"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b" : {
"balance" : "0x0de0b6b3a7640000",
"code" : "0x",
"nonce" : "0x00",
"storage" : { }
},
"0xec0e71ad0a90ffe1909d27dac207f7680abba42d" : {
"balance" : "0x01",
"code" : "0x6010ff",
"nonce" : "0x03",
"storage" : { }
}
}
}