Java Code Examples for org.apache.tuweni.bytes.Bytes#of()

The following examples show how to use org.apache.tuweni.bytes.Bytes#of() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractMerklePatriciaTrieTest.java    From besu with Apache License 2.0 6 votes vote down vote up
@Test
public void getValueWithProof_forExistingValues() {
  final Bytes key1 = Bytes.of(0xfe, 1);
  final Bytes key2 = Bytes.of(0xfe, 2);
  final Bytes key3 = Bytes.of(0xfe, 3);

  final String value1 = "value1";
  trie.put(key1, value1);

  final String value2 = "value2";
  trie.put(key2, value2);

  final String value3 = "value3";
  trie.put(key3, value3);

  final Proof<String> valueWithProof = trie.getValueWithProof(key1);
  assertThat(valueWithProof.getProofRelatedNodes()).hasSize(2);
  assertThat(valueWithProof.getValue()).contains(value1);

  List<Node<Bytes>> nodes =
      TrieNodeDecoder.decodeNodes(valueWithProof.getProofRelatedNodes().get(1));

  assertThat(new String(nodes.get(1).getValue().get().toArray(), UTF_8)).isEqualTo(value1);
  assertThat(new String(nodes.get(2).getValue().get().toArray(), UTF_8)).isEqualTo(value2);
}
 
Example 2
Source File: LengthPrefixedEncodingTest.java    From teku with Apache License 2.0 5 votes vote down vote up
@Test
public void decodePayload_shouldReadPayloadWhenExtraDataIsAppended() throws RpcException {
  final StatusMessage originalMessage = StatusMessage.createPreGenesisStatus();
  final Bytes encoded = encoding.encodePayload(originalMessage);
  final Bytes extraData = Bytes.of(1, 2, 3, 4);
  final ByteBuf input = inputByteBuffer(encoded, extraData);
  RpcByteBufDecoder<StatusMessage> decoder = encoding.createDecoder(StatusMessage.class);
  final Optional<StatusMessage> result = decoder.decodeOneMessage(input);
  decoder.complete();
  assertThat(result).contains(originalMessage);
  assertThat(input.readableBytes()).isEqualTo(4);
  input.release();
  assertThat(input.refCnt()).isEqualTo(0);
}
 
Example 3
Source File: MerklePatriciaTrieJavaTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void testDeletesEntryUpdateWithNull() throws Exception {
  final Bytes key = Bytes.of(1);

  trie.putAsync(key, "value1").join();
  trie.putAsync(key, null).join();
  assertNull(trie.getAsync(key).get());
}
 
Example 4
Source File: StoredMerklePatriciaTrieJavaTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void testCanReloadTrieFromHash() throws Exception {
  final Bytes key1 = Bytes.of(1, 5, 8, 9);
  final Bytes key2 = Bytes.of(1, 6, 1, 2);
  final Bytes key3 = Bytes.of(1, 6, 1, 3);

  trie.putAsync(key1, "value1").join();
  final Bytes32 hash1 = trie.rootHash();

  trie.putAsync(key2, "value2").join();
  trie.putAsync(key3, "value3").join();
  final Bytes32 hash2 = trie.rootHash();
  assertNotEquals(hash2, hash1);

  trie.putAsync(key1, "value4").join();
  final Bytes32 hash3 = trie.rootHash();
  assertNotEquals(hash3, hash1);
  assertNotEquals(hash3, hash2);

  assertEquals("value4", trie.getAsync(key1).get());

  trie = StoredMerklePatriciaTrie.storingStrings(merkleStorage, hash1);
  assertEquals("value1", trie.getAsync(key1).get());
  assertNull(trie.getAsync(key2).get());
  assertNull(trie.getAsync(key3).get());

  trie = StoredMerklePatriciaTrie.storingStrings(merkleStorage, hash2);
  assertEquals("value1", trie.getAsync(key1).get());
  assertEquals("value2", trie.getAsync(key2).get());
  assertEquals("value3", trie.getAsync(key3).get());

  trie = StoredMerklePatriciaTrie.storingStrings(merkleStorage, hash3);
  assertEquals("value4", trie.getAsync(key1).get());
  assertEquals("value2", trie.getAsync(key2).get());
  assertEquals("value3", trie.getAsync(key3).get());
}
 
Example 5
Source File: AbstractTaskQueueTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void clear_emptyQueueWithOutstandingTasks() throws Exception {
  try (final T queue = createQueue()) {
    final Bytes one = Bytes.of(1);

    // Add and then remove task
    queue.add(one);
    final Task<Bytes> task = queue.remove();
    assertThat(task.getData()).isEqualTo(one);
    assertThat(queue.isEmpty()).isTrue();
    assertThat(queue.allTasksCompleted()).isFalse();

    // Clear queue and check state
    queue.clear();
    assertThat(queue.size()).isEqualTo(0);
    assertThat(queue.isEmpty()).isTrue();
    assertThat(queue.allTasksCompleted()).isTrue();
    assertThat(queue.remove()).isNull();

    // Marking old task as failed should not requeue task
    task.markFailed();
    assertThat(queue.size()).isEqualTo(0);
    assertThat(queue.isEmpty()).isTrue();
    assertThat(queue.allTasksCompleted()).isTrue();
    assertThat(queue.remove()).isNull();
  }
}
 
Example 6
Source File: AbstractTaskQueueTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void markTaskCompleted() throws Exception {
  try (final T queue = createQueue()) {
    final Bytes value = Bytes.of(1);

    assertThat(queue.isEmpty()).isTrue();
    assertThat(queue.allTasksCompleted()).isTrue();

    queue.add(value);

    assertThat(queue.isEmpty()).isFalse();
    assertThat(queue.allTasksCompleted()).isFalse();

    final Task<Bytes> task = queue.remove();
    assertThat(task).isNotNull();
    assertThat(task.getData()).isEqualTo(value);
    assertThat(queue.isEmpty()).isTrue();
    assertThat(queue.allTasksCompleted()).isFalse();

    task.markCompleted();
    assertThat(queue.isEmpty()).isTrue();
    assertThat(queue.allTasksCompleted()).isTrue();

    // Subsequent mark failed should do nothing
    task.markFailed();
    assertThat(queue.isEmpty()).isTrue();
    assertThat(queue.allTasksCompleted()).isTrue();
  }
}
 
Example 7
Source File: MerklePatriciaTrieJavaTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void testReplaceSingleValue() throws Exception {
  final Bytes key = Bytes.of(1);

  trie.putAsync(key, "value1").join();
  assertEquals("value1", trie.getAsync(key).get());

  trie.putAsync(key, "value2").join();
  assertEquals("value2", trie.getAsync(key).get());
}
 
Example 8
Source File: MerklePatriciaTrieJavaTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void testHashChangesWhenValueChanged() throws Exception {
  final Bytes key1 = Bytes.of(1, 5, 8, 9);
  final Bytes key2 = Bytes.of(1, 6, 1, 2);
  final Bytes key3 = Bytes.of(1, 6, 1, 3);

  trie.putAsync(key1, "value1").join();
  final Bytes32 hash1 = trie.rootHash();

  trie.putAsync(key2, "value2").join();
  trie.putAsync(key3, "value3").join();
  final Bytes32 hash2 = trie.rootHash();

  assertNotEquals(hash2, hash1);

  trie.putAsync(key1, "value4").join();
  final Bytes32 hash3 = trie.rootHash();

  assertNotEquals(hash3, hash1);
  assertNotEquals(hash3, hash2);

  trie.putAsync(key1, "value1").join();
  assertEquals(hash2, trie.rootHash());

  trie.removeAsync(key2).join();
  trie.removeAsync(key3).join();
  assertEquals(hash1, trie.rootHash());
}
 
Example 9
Source File: AbstractMerklePatriciaTrieTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void replaceSingleValue() {
  final Bytes key = Bytes.of(1);
  final String value1 = "value1";
  trie.put(key, value1);
  assertThat(trie.get(key)).isEqualTo(Optional.of(value1));

  final String value2 = "value2";
  trie.put(key, value2);
  assertThat(trie.get(key)).isEqualTo(Optional.of(value2));
}
 
Example 10
Source File: AbstractMerklePatriciaTrieTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void readPastBranch() {
  final Bytes key1 = Bytes.of(12);
  final Bytes key2 = Bytes.of(12, 54);

  final String value1 = "value1";
  trie.put(key1, value1);
  final String value2 = "value2";
  trie.put(key2, value2);

  final Bytes key3 = Bytes.of(3);
  assertFalse(trie.get(key3).isPresent());
}
 
Example 11
Source File: AbstractMerklePatriciaTrieTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void extendAndBranch() {
  final Bytes key1 = Bytes.of(1, 5, 9);
  final Bytes key2 = Bytes.of(1, 5, 2);

  final String value1 = "value1";
  trie.put(key1, value1);

  final String value2 = "value2";
  trie.put(key2, value2);

  assertThat(trie.get(key1)).isEqualTo(Optional.of(value1));
  assertThat(trie.get(key2)).isEqualTo(Optional.of(value2));
  assertFalse(trie.get(Bytes.of(1, 4)).isPresent());
}
 
Example 12
Source File: MerklePatriciaTrieJavaTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void testReadPastBranch() throws Exception {
  final Bytes key1 = Bytes.of(12);
  final Bytes key2 = Bytes.of(12, 54);
  final Bytes key3 = Bytes.of(3);

  trie.putAsync(key1, "value1").join();
  trie.putAsync(key2, "value2").join();
  assertNull(trie.getAsync(key3).get());
}
 
Example 13
Source File: NewBlockMessageTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void readFromMessageWithWrongCodeThrows() {
  final RawMessage rawMsg = new RawMessage(EthPV62.BLOCK_HEADERS, Bytes.of(0));

  assertThatExceptionOfType(IllegalArgumentException.class)
      .isThrownBy(() -> NewBlockMessage.readFrom(rawMsg));
}
 
Example 14
Source File: StoredMerklePatriciaTrieJavaTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void testReadPastLeaf() throws Exception {
  final Bytes key1 = Bytes.of(1);
  final Bytes key2 = Bytes.of(1, 3);
  trie.putAsync(key1, "value").join();
  assertNull(trie.getAsync(key2).get());
}
 
Example 15
Source File: MerklePatriciaTrieJavaTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void testSplitBranchExtension() throws Exception {
  final Bytes key1 = Bytes.of(1, 5, 9);
  final Bytes key2 = Bytes.of(1, 5, 2);
  final Bytes key3 = Bytes.of(1, 9, 1);

  trie.putAsync(key1, "value1").join();
  trie.putAsync(key2, "value2").join();
  trie.putAsync(key3, "value3").join();
  assertEquals("value1", trie.getAsync(key1).get());
  assertEquals("value2", trie.getAsync(key2).get());
  assertEquals("value3", trie.getAsync(key3).get());
}
 
Example 16
Source File: StoredMerklePatriciaTrieJavaTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void testReplaceSingleValue() throws Exception {
  final Bytes key = Bytes.of(1);

  trie.putAsync(key, "value1").join();
  assertEquals("value1", trie.getAsync(key).get());

  trie.putAsync(key, "value2").join();
  assertEquals("value2", trie.getAsync(key).get());
}
 
Example 17
Source File: StoredMerklePatriciaTrieJavaTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void testDeletesEntryUpdateWithNull() throws Exception {
  final Bytes key = Bytes.of(1);

  trie.putAsync(key, "value1").join();
  trie.putAsync(key, null).join();
  assertNull(trie.getAsync(key).get());
}
 
Example 18
Source File: AbstractMerklePatriciaTrieTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void branchWithValue() {
  final Bytes key1 = Bytes.of(5);
  final Bytes key2 = Bytes.EMPTY;

  final String value1 = "value1";
  trie.put(key1, value1);

  final String value2 = "value2";
  trie.put(key2, value2);

  assertThat(trie.get(key1)).isEqualTo(Optional.of(value1));
  assertThat(trie.get(key2)).isEqualTo(Optional.of(value2));
}
 
Example 19
Source File: MerklePatriciaTrieJavaTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void testHashChangesWhenSingleValueReplaced() throws Exception {
  final Bytes key = Bytes.of(1);

  trie.putAsync(key, "value1").join();
  final Bytes32 hash1 = trie.rootHash();

  trie.putAsync(key, "value2").join();
  final Bytes32 hash2 = trie.rootHash();

  assertNotEquals(hash2, hash1);

  trie.putAsync(key, "value1").join();
  assertEquals(hash1, trie.rootHash());
}
 
Example 20
Source File: TransactionTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void supportVMoreThanOneByte() {
  Transaction tx = new Transaction(
      UInt256.valueOf(0),
      Wei.valueOf(BigInteger.valueOf(5L)),
      Gas.valueOf(10L),
      Address.fromBytes(Bytes.fromHexString("0x0102030405060708091011121314151617181920")),
      Wei.valueOf(10L),
      Bytes.of(1, 2, 3, 4),
      SECP256K1.KeyPair.random(),
      16 * 16 * 3);
  Bytes bytes = tx.toBytes();
  Transaction read = Transaction.fromBytes(bytes);
  assertEquals(16 * 16 * 3, (int) read.getChainId());
}