use of snowblossom.lib.NetworkParamsRegtest in project snowblossom by snowblossomcoin.
the class ShardUtilTest method testBlockRewardSum.
/**
* This test takes a list of shards, makes a bunch of fake blocks
* for those shards, has the blocks include each other
* and then see if the total block rewards add up as they should
*/
@Test
public void testBlockRewardSum() {
LinkedList<List<Integer>> shard_lists = new LinkedList<>();
shard_lists.add(ImmutableList.of(0));
shard_lists.add(ImmutableList.of(1, 5, 6));
shard_lists.add(ImmutableList.of(3, 4, 5, 6));
shard_lists.add(ImmutableList.of(7, 8, 9, 10, 5, 6));
shard_lists.add(getGeneration(3));
// 32 shards
shard_lists.add(getGeneration(5));
// 128 shards
shard_lists.add(getGeneration(7));
// 256 shards
shard_lists.add(getGeneration(8));
// 512 shards
shard_lists.add(getGeneration(9));
// shard_lists.add(getGeneration(10)); //1024 shards
NetworkParams params = new NetworkParamsRegtest();
long blocks_per_day = 86400 / (params.getBlockTimeTarget() / 1000);
int blocks_per_year = (int) blocks_per_day * 365;
int blocks_four_years = blocks_per_year * 4;
Random rnd = new Random();
for (List<Integer> shards : shard_lists) {
// This is the real test case, do a simulation with this list of shards
ArrayList<BlockHeader.Builder> headers = new ArrayList<>();
Assert.assertTrue(ShardUtil.isProperSet(shards));
assertCompleteCoverage(shards, 10000);
long expected_total = 0;
List<Integer> height_list = new LinkedList<>();
// which may be different from the block that includes it
for (int h = blocks_four_years - 5; h <= blocks_four_years + 5; h++) {
height_list.add(h);
expected_total += PowUtil.getBlockReward(params, h);
}
System.out.println("Expected block reward: " + expected_total);
for (int s : shards) {
ArrayList<BlockHeader.Builder> shard_headers = new ArrayList<>();
// Make a fake header for each height
for (int h : height_list) {
BlockHeader.Builder b = BlockHeader.newBuilder();
b.setShardId(s);
b.setBlockHeight(h);
shard_headers.add(b);
}
// For every other shard and height, import that somewhere
for (int o : shards) {
if (s != o) {
for (int h : height_list) {
// Header to stick this block in
// These will be all out of order and crazy but it doesn't matter
// We just need each block included in each other shard chain somewhere
BlockHeader.Builder b = shard_headers.get(rnd.nextInt(shard_headers.size()));
if (!b.getShardImportMap().containsKey(o)) {
b.putShardImport(o, BlockImportList.newBuilder().build());
}
BlockImportList.Builder il = BlockImportList.newBuilder();
il.mergeFrom(b.getShardImportMap().get(o));
il.putHeightMap(h, ByteString.EMPTY);
b.putShardImport(o, il.build());
}
}
}
headers.addAll(shard_headers);
}
long found_reward = 0;
for (BlockHeader.Builder b : headers) {
found_reward += ShardUtil.getBlockReward(params, b.build());
}
// With only six digits after the decimal and a few shards going
// We start to get some truncation errors so a tiny slice of the reward
// is not issued.
// The error is limited to at most one flake per block.
// Note: even though we are losing some flakes, the calculation
// is all integer math that will always be done the exact same way
// so there is no concensus problem.
long missing = expected_total - found_reward;
System.out.println(shards.toString() + " missing " + missing + " from " + headers.size() + " blocks");
Assert.assertTrue(missing < headers.size());
}
}
use of snowblossom.lib.NetworkParamsRegtest in project snowblossom by snowblossomcoin.
the class PowUtilTest method testCalcNextTargetDecreasingFromAvg.
@Test
public void testCalcNextTargetDecreasingFromAvg() {
NetworkParams params = new NetworkParamsRegtest();
long time = System.currentTimeMillis();
// block solved fast
BlockHeader prev_header = BlockHeader.newBuilder().setTimestamp(time - params.getBlockTimeTarget() / 2).build();
BigInteger average_target = params.getMaxTarget().divide(BigInteger.valueOf(100L));
BlockSummary bs = BlockSummary.newBuilder().setHeader(prev_header).setTargetAverage(average_target.toString()).setBlocktimeAverageMs(params.getBlockTimeTarget()).build();
BigInteger target = PowUtil.calcNextTarget(bs, params, System.currentTimeMillis());
Assert.assertTrue(target.compareTo(average_target) < 0);
}
use of snowblossom.lib.NetworkParamsRegtest in project snowblossom by snowblossomcoin.
the class PowUtilTest method testStability.
@Test
public void testStability() {
NetworkParams params = new NetworkParamsRegtest();
long time = 1000000000L;
BigInteger target = params.getMaxTarget();
// per ms
double simulated_solve_rate = 100;
BlockSummary bs = BlockSummary.newBuilder().build();
for (int i = 0; i < 10000; i++) {
long last_time = time;
target = PowUtil.calcNextTarget(bs, params, time);
double tdiff = PowUtil.getDiffForTarget(target);
double work = Math.pow(2, tdiff);
long solve_time = (long) (work / simulated_solve_rate);
if (solve_time <= 0)
solve_time = 1;
time = time + solve_time;
BlockHeader header = BlockHeader.newBuilder().setTarget(BlockchainUtil.targetBigIntegerToBytes(target)).setTimestamp(time).build();
bs = BlockchainUtil.getNewSummary(header, bs, params, 1L, 600L, new LinkedList());
Assert.assertEquals(time, bs.getHeader().getTimestamp());
}
double diff = Math.abs(bs.getBlocktimeAverageMs() - params.getBlockTimeTarget());
diff = diff / params.getBlockTimeTarget();
Assert.assertTrue(String.format("Diff: %f", diff), diff < 0.02);
// increase speed
simulated_solve_rate = 5;
for (int i = 0; i < 10000; i++) {
long last_time = time;
target = PowUtil.calcNextTarget(bs, params, time);
double tdiff = PowUtil.getDiffForTarget(target);
double work = Math.pow(2, tdiff);
long solve_time = (long) (work / simulated_solve_rate);
if (solve_time <= 0)
solve_time = 1;
time = time + solve_time;
BlockHeader header = BlockHeader.newBuilder().setTarget(BlockchainUtil.targetBigIntegerToBytes(target)).setTimestamp(time).build();
bs = BlockchainUtil.getNewSummary(header, bs, params, 1L, 600L, new LinkedList());
Assert.assertEquals(time, bs.getHeader().getTimestamp());
}
diff = Math.abs(bs.getBlocktimeAverageMs() - params.getBlockTimeTarget());
diff = diff / params.getBlockTimeTarget();
Assert.assertTrue(diff < 0.02);
// Assert.fail();
}
use of snowblossom.lib.NetworkParamsRegtest in project snowblossom by snowblossomcoin.
the class PowUtilTest method testCalcNextTargetInitial.
@Test
public void testCalcNextTargetInitial() {
BlockSummary bs = BlockSummary.newBuilder().build();
NetworkParams params = new NetworkParamsRegtest();
BigInteger target = PowUtil.calcNextTarget(bs, params, System.currentTimeMillis());
Assert.assertEquals(params.getMaxTarget(), target);
}
use of snowblossom.lib.NetworkParamsRegtest in project snowblossom by snowblossomcoin.
the class WalletTest method testWalletSerialize.
@Test
public void testWalletSerialize() throws Exception {
ConfigMem config = new ConfigMem(ImmutableMap.of());
WalletDatabase.Builder builder = WalletDatabase.newBuilder();
WalletUtil.genNewKey(WalletDatabase.newBuilder().build(), builder, config, new NetworkParamsRegtest());
WalletDatabase a = builder.build();
ByteString str = a.toByteString();
WalletDatabase b = WalletDatabase.parseFrom(str);
Assert.assertEquals(a, b);
}
Aggregations