Skip to content

Commit f149514

Browse files
committed
#54 step-2(implements AccountState and unit test case)
1 parent 17a0ecd commit f149514

5 files changed

Lines changed: 510 additions & 14 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2020-2030 The XdagJ Developers
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package io.xdag.evm.client;
25+
26+
import java.util.List;
27+
import java.util.Set;
28+
import java.util.function.Function;
29+
30+
import org.apache.tuweni.bytes.Bytes;
31+
32+
import cn.hutool.core.lang.Pair;
33+
import io.xdag.db.KVSource;
34+
import io.xdag.db.rocksdb.RocksdbKVSource;
35+
36+
public class RocksdbProxy implements KVSource<Bytes, Bytes> {
37+
38+
private RocksdbKVSource rocksdbKV;
39+
40+
public RocksdbProxy(RocksdbKVSource rocksdbKV) {
41+
this.rocksdbKV = rocksdbKV;
42+
}
43+
44+
@Override
45+
public String getName() {
46+
return rocksdbKV.getName();
47+
}
48+
49+
@Override
50+
public void setName(String name) {
51+
rocksdbKV.setName(name);
52+
}
53+
54+
@Override
55+
public boolean isAlive() {
56+
return rocksdbKV.isAlive();
57+
}
58+
59+
@Override
60+
public void init() {
61+
rocksdbKV.init();
62+
}
63+
64+
@Override
65+
public void close() {
66+
rocksdbKV.close();
67+
}
68+
69+
@Override
70+
public void reset() {
71+
rocksdbKV.reset();
72+
}
73+
74+
@Override
75+
public void put(Bytes key, Bytes val) {
76+
rocksdbKV.put(key.toArray(), val.toArray());
77+
}
78+
79+
@Override
80+
public Bytes get(Bytes key) {
81+
byte[] value = rocksdbKV.get(key.toArray());
82+
return value == null? null:Bytes.wrap(value);
83+
}
84+
85+
@Override
86+
public void delete(Bytes key) {
87+
rocksdbKV.delete(key.toArray());
88+
}
89+
90+
@Override
91+
public Set<byte[]> keys() throws RuntimeException {
92+
return rocksdbKV.keys();
93+
}
94+
95+
@Override
96+
public List<Bytes> prefixKeyLookup(byte[] key) {
97+
return null;
98+
}
99+
100+
@Override
101+
public void fetchPrefix(byte[] key, Function<Pair<Bytes, Bytes>, Boolean> func) {
102+
}
103+
104+
@Override
105+
public List<Bytes> prefixValueLookup(byte[] key) {
106+
return null;
107+
}
108+
}

‎src/main/java/io/xdag/state/Account.java‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@
2323
*/
2424
package io.xdag.state;
2525

26+
import org.apache.tuweni.bytes.Bytes;
27+
2628
import io.xdag.core.SimpleEncoder;
2729
import io.xdag.core.XAmount;
2830
import io.xdag.utils.SimpleDecoder;
29-
import org.bouncycastle.util.encoders.Hex;
30-
31-
import java.util.Arrays;
3231

3332
public class Account {
3433

35-
private final byte[] address;
34+
private final Bytes address;
3635
private XAmount available;
3736
private XAmount locked;
3837
private long nonce;
@@ -41,7 +40,7 @@ public class Account {
4140
* Creates an account instance.
4241
*
4342
*/
44-
public Account(byte[] address, XAmount available, XAmount locked, long nonce) {
43+
public Account(Bytes address, XAmount available, XAmount locked, long nonce) {
4544
this.address = address;
4645
this.available = available;
4746
this.locked = locked;
@@ -52,33 +51,32 @@ public Account(byte[] address, XAmount available, XAmount locked, long nonce) {
5251
* Serializes this account into byte array.
5352
*
5453
*/
55-
public byte[] toBytes() {
54+
public Bytes toBytes() {
5655
SimpleEncoder enc = new SimpleEncoder();
5756
enc.writeXAmount(available);
5857
enc.writeXAmount(locked);
5958
enc.writeLong(nonce);
60-
61-
return enc.toBytes();
59+
return Bytes.wrap(enc.toBytes());
6260
}
6361

6462
/**
6563
* Parses an account from byte array.
6664
*
6765
*/
68-
public static Account fromBytes(byte[] address, byte[] bytes) {
69-
SimpleDecoder dec = new SimpleDecoder(bytes);
66+
public static Account fromBytes(Bytes address, Bytes bytes) {
67+
SimpleDecoder dec = new SimpleDecoder(bytes.toArray());
7068
XAmount available = dec.readXAmount();
7169
XAmount locked = dec.readXAmount();
7270
long nonce = dec.readLong();
7371

74-
return new Account(address, available, locked, nonce);
72+
return new Account(Bytes.wrap(address), available, locked, nonce);
7573
}
7674

7775
/**
7876
* Returns the address of this account.
7977
*
8078
*/
81-
public byte[] getAddress() {
79+
public Bytes getAddress() {
8280
return address;
8381
}
8482

@@ -132,7 +130,7 @@ void setNonce(long nonce) {
132130

133131
@Override
134132
public String toString() {
135-
return "Account [address=" + Arrays.toString(Hex.encode(address)) + ", available=" + available + ", locked=" + locked
133+
return "Account [address=" + address.toHexString() + ", available=" + available + ", locked=" + locked
136134
+ ", nonce=" + nonce + "]";
137135
}
138136

‎src/main/java/io/xdag/state/AccountState.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public interface AccountState {
8181
* Remove a key value pair from the storage if exists.
8282
*
8383
*/
84-
void removeStorage(byte[] address, byte[] key);
84+
void removeStorage(Bytes address, Bytes key);
8585

8686
/**
8787
* Makes a snapshot and starts tracking further updates.

0 commit comments

Comments
 (0)