Skip to content

Commit 27cba55

Browse files
authored
Merge pull request #2567 from rsksmart/vovchyk/rc631/logging-impt
chore(logging): refactor and improve logging
2 parents 7d979a1 + f582ac4 commit 27cba55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+931
-129
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of RskJ
3+
* Copyright (C) 2024 RSK Labs Ltd.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
package co.rsk.core.types.bytes;
20+
21+
/**
22+
* {@link ByteSequence} is the most basic interface that represents read-only sequence of <code>byte</code> values.
23+
*/
24+
public interface ByteSequence {
25+
26+
/**
27+
* Returns the length of this byte sequence.
28+
*
29+
* @return the length of the sequence of bytes represented by this
30+
* object.
31+
*/
32+
int length();
33+
34+
/**
35+
* Returns the {@code byte} value at the
36+
* specified index. An index ranges from {@code 0} to
37+
* {@code length() - 1}. The first {@code byte} value of the sequence
38+
* is at index {@code 0}, the next at index {@code 1},
39+
* and so on, as for array indexing.
40+
*
41+
* @param index the index of the {@code byte} value.
42+
* @return the {@code byte} value at the specified index of this array.
43+
* The first {@code byte} value is at index {@code 0}.
44+
* @exception IndexOutOfBoundsException if the {@code index}
45+
* argument is negative or not less than the length of this
46+
* array.
47+
*/
48+
byte byteAt(int index);
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* This file is part of RskJ
3+
* Copyright (C) 2024 RSK Labs Ltd.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
package co.rsk.core.types.bytes;
20+
21+
import org.ethereum.util.ByteUtil;
22+
import org.ethereum.util.FastByteComparisons;
23+
24+
import javax.annotation.Nonnull;
25+
import javax.annotation.Nullable;
26+
import java.util.Arrays;
27+
import java.util.Objects;
28+
29+
/**
30+
* A {@link Bytes} is a readable sequence of <code>byte</code> values. This
31+
* interface provides uniform, read-only access to many different kinds of
32+
* <code>byte</code> sequences.
33+
*
34+
* <p> This interface does not refine the general contracts of the {@link
35+
* java.lang.Object#equals(java.lang.Object) equals} and {@link
36+
* java.lang.Object#hashCode() hashCode} methods. The result of comparing two
37+
* objects that implement <tt>Bytes</tt> is therefore, in general,
38+
* undefined. Each object may be implemented by a different class, and there
39+
* is no guarantee that each class will be capable of testing its instances
40+
* for equality with those of the other. It is therefore inappropriate to use
41+
* arbitrary <tt>Bytes</tt> instances as elements in a set or as keys in
42+
* a map. </p>
43+
*/
44+
public interface Bytes extends BytesSlice {
45+
46+
/**
47+
* Returns an instance of the {@link Bytes} interface, which represents {@code unsafeByteArray}.
48+
*
49+
* @return the instance of the {@link Bytes} interface that wraps a provided byte array.
50+
*/
51+
static Bytes of(@Nullable byte[] unsafeByteArray) {
52+
if (unsafeByteArray == null) {
53+
return null;
54+
}
55+
return new BytesImpl(unsafeByteArray);
56+
}
57+
58+
/**
59+
* A helper method for printing "nullable" byte arrays.
60+
*
61+
* @return {@code valueIfNull}, if {@code byteArray} is {@code null}. Otherwise - {@code Bytes.of(byteArray).toPrintableString()}.
62+
*/
63+
static String toPrintableString(@Nullable byte[] byteArray, @Nullable String valueIfNull) {
64+
if (byteArray == null) {
65+
return valueIfNull;
66+
}
67+
return of(byteArray).toPrintableString();
68+
}
69+
70+
/**
71+
* A helper method for printing "nullable" byte arrays.
72+
*
73+
* @return {@code "<null>"}, if {@code byteArray} is {@code null}. Otherwise - {@code Bytes.of(byteArray).toPrintableString()}.
74+
*/
75+
@Nonnull
76+
static String toPrintableString(@Nullable byte[] byteArray) {
77+
return toPrintableString(byteArray, "<null>");
78+
}
79+
80+
/**
81+
* A helper method for extracting "unsafe" underlying byte array from the {@code bytes} instance.
82+
*
83+
* @return {@code null}, if {@code bytes} is {@code null}. Otherwise - {@code bytes.asUnsafeByteArray()}.
84+
*/
85+
@Nullable
86+
static byte[] asUnsafeByteArray(@Nullable Bytes bytes) {
87+
if (bytes == null) {
88+
return null;
89+
}
90+
return bytes.asUnsafeByteArray();
91+
}
92+
93+
static boolean equalBytes(Bytes b1, Bytes b2) {
94+
if (b1 == null && b2 == null) {
95+
return true;
96+
}
97+
if (b1 == null || b2 == null) {
98+
return false;
99+
}
100+
return FastByteComparisons.equalBytes(b1.asUnsafeByteArray(), b2.asUnsafeByteArray());
101+
}
102+
103+
/**
104+
* Returns an underlying byte array, which is backing this instance. Any mutations that are being done with the bytes
105+
* of returned array will have direct impact on the byte array that is being wrapped by this instance.
106+
*
107+
* @return the wrapped by this instance byte array.
108+
*/
109+
byte[] asUnsafeByteArray();
110+
}
111+
112+
/**
113+
* The {@link BytesImpl} class represents a read-only sequence of <code>byte</code> values.
114+
* <p>
115+
* Instances of the {@link BytesImpl} class are constant; their values cannot be changed after they
116+
* are created via the methods that this class provides. But a {@code byteArray} instance itself provided in the constructor
117+
* is mutable and can be modified outside the class. It's generally a bad idea to mutate a byte array that's being wrapped
118+
* by an instance of this class, as the idea is to make a byte sequence immutable, which is not the case with the Java
119+
* built-in {@code byte[]} type.
120+
* <p>
121+
* Because {@link BytesImpl} objects are immutable they can be safely used by multiple Java threads, if the wrapped array
122+
* is not being referenced and modified outside.
123+
*/
124+
class BytesImpl implements Bytes {
125+
126+
private final byte[] byteArray;
127+
128+
BytesImpl(@Nonnull byte[] unsafeByteArray) {
129+
this.byteArray = Objects.requireNonNull(unsafeByteArray);
130+
}
131+
132+
@Override
133+
public int length() {
134+
return byteArray.length;
135+
}
136+
137+
@Override
138+
public byte byteAt(int index) {
139+
return byteArray[index];
140+
}
141+
142+
@Override
143+
public byte[] copyArrayOfRange(int from, int to) {
144+
return Arrays.copyOfRange(byteArray, from, to);
145+
}
146+
147+
@Override
148+
public String toHexString() {
149+
return ByteUtil.toHexString(byteArray);
150+
}
151+
152+
@Override
153+
public String toHexString(int off, int length) {
154+
return ByteUtil.toHexString(byteArray, off, length);
155+
}
156+
157+
@Nonnull
158+
@Override
159+
public byte[] asUnsafeByteArray() {
160+
return byteArray;
161+
}
162+
163+
@Override
164+
public String toString() {
165+
return toPrintableString();
166+
}
167+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* This file is part of RskJ
3+
* Copyright (C) 2024 RSK Labs Ltd.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
package co.rsk.core.types.bytes;
20+
21+
/**
22+
* A {@link BytesSlice} is a subsequence of bytes backed by another broader byte sequence.
23+
*/
24+
public interface BytesSlice extends HexPrintableBytes {
25+
26+
/**
27+
* Copies the specified range of the specified array into a new array.
28+
* The initial index of the range (<tt>from</tt>) must lie between zero
29+
* and <tt>original.length</tt>, inclusive. The value at
30+
* <tt>original[from]</tt> is placed into the initial element of the copy
31+
* (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
32+
* Values from subsequent elements in the original array are placed into
33+
* subsequent elements in the copy. The final index of the range
34+
* (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
35+
* may be greater than <tt>original.length</tt>, in which case
36+
* <tt>(byte)0</tt> is placed in all elements of the copy whose index is
37+
* greater than or equal to <tt>original.length - from</tt>. The length
38+
* of the returned array will be <tt>to - from</tt>.
39+
*
40+
* @param from the initial index of the range to be copied, inclusive
41+
* @param to the final index of the range to be copied, exclusive.
42+
* (This index may lie outside the array.)
43+
* @return a new array containing the specified range from the original array,
44+
* truncated or padded with zeros to obtain the required length
45+
* @throws ArrayIndexOutOfBoundsException if {@code from < 0}
46+
* or {@code from > original.length}
47+
* @throws IllegalArgumentException if <tt>from &gt; to</tt>
48+
* @throws NullPointerException if <tt>original</tt> is null
49+
*/
50+
byte[] copyArrayOfRange(int from, int to);
51+
52+
default byte[] copyArray() {
53+
return copyArrayOfRange(0, length());
54+
}
55+
56+
default Bytes copyBytesOfRange(int from, int to) {
57+
return Bytes.of(copyArrayOfRange(from, to));
58+
}
59+
60+
default Bytes copyBytes() {
61+
return Bytes.of(copyArrayOfRange(0, length()));
62+
}
63+
64+
default BytesSlice slice(int from, int to) {
65+
return new BytesSliceImpl(this, from, to);
66+
}
67+
}
68+
69+
class BytesSliceImpl implements BytesSlice {
70+
71+
private final BytesSlice originBytes;
72+
73+
private final int from;
74+
private final int to;
75+
76+
BytesSliceImpl(BytesSlice originBytes, int from, int to) {
77+
this.originBytes = originBytes;
78+
79+
if (from < 0) {
80+
throw new IndexOutOfBoundsException(from + " < " + 0);
81+
}
82+
if (from > to) {
83+
throw new IndexOutOfBoundsException(from + " > " + to);
84+
}
85+
if (to > originBytes.length()) {
86+
throw new IndexOutOfBoundsException(to + " > " + "length");
87+
}
88+
89+
this.from = from;
90+
this.to = to;
91+
}
92+
93+
@Override
94+
public int length() {
95+
return to - from;
96+
}
97+
98+
@Override
99+
public byte byteAt(int index) {
100+
if (index < 0 || index >= length()) {
101+
throw new IndexOutOfBoundsException("invalid index: " + index);
102+
}
103+
return originBytes.byteAt(from + index);
104+
}
105+
106+
@Override
107+
public byte[] copyArrayOfRange(int from, int to) {
108+
if (from < 0 || from > to || to > length()) {
109+
throw new IndexOutOfBoundsException("invalid 'from' and/or 'to': [" + from + ";" + to + ")");
110+
}
111+
return originBytes.copyArrayOfRange(this.from + from, this.from + to);
112+
}
113+
114+
@Override
115+
public String toHexString(int off, int length) {
116+
if (off < 0 || length < 0 || off + length > length()) {
117+
throw new IndexOutOfBoundsException("invalid 'off' and/or 'length': " + off + "; " + length);
118+
}
119+
return originBytes.toHexString(from + off, length);
120+
}
121+
122+
@Override
123+
public String toHexString() {
124+
return toHexString(0, length());
125+
}
126+
127+
@Override
128+
public String toString() {
129+
return toPrintableString();
130+
}
131+
}

0 commit comments

Comments
 (0)