11package org .tron .core .db ;
22
3+ import com .google .common .collect .Maps ;
34import com .google .protobuf .ByteString ;
45import java .io .File ;
6+ import java .util .Map ;
57import java .util .stream .Collectors ;
8+ import java .util .stream .IntStream ;
69import lombok .extern .slf4j .Slf4j ;
710import org .junit .AfterClass ;
811import org .junit .Assert ;
1215import org .tron .common .utils .ByteArray ;
1316import org .tron .common .utils .FileUtil ;
1417import org .tron .common .utils .Sha256Hash ;
18+ import org .tron .common .utils .Utils ;
1519import org .tron .core .Constant ;
20+ import org .tron .core .capsule .AccountCapsule ;
1621import org .tron .core .capsule .BlockCapsule ;
22+ import org .tron .core .capsule .TransactionCapsule ;
1723import org .tron .core .capsule .WitnessCapsule ;
1824import org .tron .core .config .args .Args ;
1925import org .tron .core .exception .ContractExeException ;
2026import org .tron .core .exception .ContractValidateException ;
2127import org .tron .core .exception .UnLinkedBlockException ;
2228import org .tron .core .exception .ValidateScheduleException ;
2329import org .tron .core .exception .ValidateSignatureException ;
30+ import org .tron .core .witness .WitnessController ;
31+ import org .tron .protos .Contract .TransferContract ;
32+ import org .tron .protos .Protocol .Account ;
33+ import org .tron .protos .Protocol .Transaction .Contract .ContractType ;
2434
2535@ Slf4j
2636public class ManagerTest {
@@ -56,53 +66,53 @@ public static void removeDb() {
5666 dbManager .destory ();
5767 }
5868
59- // @Test
60- // public void setBlockReference()
61- // throws ContractExeException, UnLinkedBlockException, ValidateScheduleException, ContractValidateException, ValidateSignatureException {
62- //
63- // BlockCapsule blockCapsule = new BlockCapsule(1, dbManager.getGenesisBlockId().getByteString(),
64- // 0,
65- // ByteString.copyFrom(
66- // ECKey.fromPrivate(ByteArray
67- // .fromHexString(Args.getInstance().getLocalWitnesses().getPrivateKey()))
68- // .getAddress()));
69- // blockCapsule.setMerkleRoot();
70- // blockCapsule.sign(
71- // ByteArray.fromHexString(Args.getInstance().getLocalWitnesses().getPrivateKey()));
72- //
73- // TransferContract tc = TransferContract.newBuilder().setAmount(10)
74- // .setOwnerAddress(ByteString.copyFromUtf8("aaa"))
75- // .setToAddress(ByteString.copyFromUtf8("bbb")).build();
76- // TransactionCapsule trx = new TransactionCapsule(tc, ContractType.TransferContract);
77- //
78- // if (dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() == 0) {
79- // dbManager.pushBlock(blockCapsule);
80- // Assert.assertEquals(1, dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber());
81- // dbManager.setBlockReference(trx);
82- // Assert.assertEquals(1, trx.getInstance().getRawData().getRefBlockNum());
83- // }
84- // while (dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() > 0) {
85- // dbManager.eraseBlock();
86- // }
87- // try {
88- // dbManager.pushBlock(blockCapsule);
89- // Assert.assertEquals(1, dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber());
90- // } catch (ValidateSignatureException e) {
91- // e.printStackTrace();
92- // } catch (ContractValidateException e) {
93- // e.printStackTrace();
94- // } catch (ContractExeException e) {
95- // e.printStackTrace();
96- // } catch (UnLinkedBlockException e) {
97- // e.printStackTrace();
98- // } catch (ValidateScheduleException e) {
99- // e.printStackTrace();
100- // }
101- // Assert.assertEquals(1, dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber());
102- // dbManager.setBlockReference(trx);
103- // Assert.assertEquals(1, trx.getInstance().getRawData().getRefBlockNum());
104- //
105- // }
69+ @ Test
70+ public void setBlockReference ()
71+ throws ContractExeException , UnLinkedBlockException , ValidateScheduleException , ContractValidateException , ValidateSignatureException {
72+
73+ BlockCapsule blockCapsule = new BlockCapsule (1 , dbManager .getGenesisBlockId ().getByteString (),
74+ 0 ,
75+ ByteString .copyFrom (
76+ ECKey .fromPrivate (ByteArray
77+ .fromHexString (Args .getInstance ().getLocalWitnesses ().getPrivateKey ()))
78+ .getAddress ()));
79+ blockCapsule .setMerkleRoot ();
80+ blockCapsule .sign (
81+ ByteArray .fromHexString (Args .getInstance ().getLocalWitnesses ().getPrivateKey ()));
82+
83+ TransferContract tc = TransferContract .newBuilder ().setAmount (10 )
84+ .setOwnerAddress (ByteString .copyFromUtf8 ("aaa" ))
85+ .setToAddress (ByteString .copyFromUtf8 ("bbb" )).build ();
86+ TransactionCapsule trx = new TransactionCapsule (tc , ContractType .TransferContract );
87+
88+ if (dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderNumber () == 0 ) {
89+ dbManager .pushBlock (blockCapsule );
90+ Assert .assertEquals (1 , dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderNumber ());
91+ dbManager .setBlockReference (trx );
92+ Assert .assertEquals (1 , trx .getInstance ().getRawData ().getRefBlockNum ());
93+ }
94+ while (dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderNumber () > 0 ) {
95+ dbManager .eraseBlock ();
96+ }
97+ try {
98+ dbManager .pushBlock (blockCapsule );
99+ Assert .assertEquals (1 , dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderNumber ());
100+ } catch (ValidateSignatureException e ) {
101+ e .printStackTrace ();
102+ } catch (ContractValidateException e ) {
103+ e .printStackTrace ();
104+ } catch (ContractExeException e ) {
105+ e .printStackTrace ();
106+ } catch (UnLinkedBlockException e ) {
107+ e .printStackTrace ();
108+ } catch (ValidateScheduleException e ) {
109+ e .printStackTrace ();
110+ }
111+ Assert .assertEquals (1 , dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderNumber ());
112+ dbManager .setBlockReference (trx );
113+ Assert .assertEquals (1 , trx .getInstance ().getRawData ().getRefBlockNum ());
114+
115+ }
106116
107117 @ Test
108118 public void pushBlock () {
@@ -136,7 +146,7 @@ public void pushBlock() {
136146 }
137147
138148
139- // @Test
149+ // @Test
140150 public void updateWits () {
141151 int sizePrv = dbManager .getWitnesses ().size ();
142152 dbManager .getWitnesses ().forEach (witnessCapsule -> {
@@ -176,91 +186,22 @@ public void fork() throws ValidateSignatureException, ContractValidateException,
176186 ContractExeException , UnLinkedBlockException , ValidateScheduleException {
177187 Args .setParam (new String []{"--witness" }, Constant .TEST_CONF );
178188 long size = dbManager .getBlockStore ().dbSource .allKeys ().size ();
179- // String key = "f31db24bfbd1a2ef19beddca0a0fa37632eded9ac666a05d3bd925f01dde1f62";
180- // byte[] privateKey = ByteArray.fromHexString(key);
181- // final ECKey ecKey = ECKey.fromPrivate(privateKey);
182- // byte[] address = ecKey.getAddress();
183- // WitnessCapsule witnessCapsule = new WitnessCapsule(ByteString.copyFrom(address));
184- // dbManager.addWitness(witnessCapsule);
185- // dbManager.addWitness(witnessCapsule);
186- // dbManager.addWitness(witnessCapsule);
187- // IntStream.range(0, 1).forEach(i -> {
188- // try {
189- // dbManager.generateBlock(new WitnessCapsule(ByteString.copyFrom(
190- // ECKey.fromPrivate(ByteArray
191- // .fromHexString(Args.getInstance().getLocalWitnesses().getPrivateKey()))
192- // .getAddress())), System.currentTimeMillis(), privateKey);
193- // } catch (Exception e) {
194- // logger.debug(e.getMessage(), e);
195- // }
196- // });
189+
190+ Map <ByteString , String > addressToProvateKeys = addTestWitnessAndAccount ();
197191
198192 long num = dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderNumber ();
199- BlockCapsule blockCapsule0 = new BlockCapsule (num + 1 ,
200- dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderHash (),
201- dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderTimestamp () + 1 ,
202- ByteString .copyFrom (
203- ECKey .fromPrivate (ByteArray
204- .fromHexString (Args .getInstance ().getLocalWitnesses ().getPrivateKey ()))
205- .getAddress ()));
206- blockCapsule0 .generatedByMyself = true ;
207- blockCapsule0 .setMerkleRoot ();
208- blockCapsule0 .sign (
209- ByteArray .fromHexString (Args .getInstance ().getLocalWitnesses ().getPrivateKey ()));
193+ BlockCapsule blockCapsule0 = createTestBlockCapsule (num + 1 ,
194+ dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderHash (), addressToProvateKeys );
210195
211- BlockCapsule blockCapsule1 = new BlockCapsule (num + 1 ,
212- dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderHash (),
213- System .currentTimeMillis (),
214- ByteString .copyFrom (
215- ECKey .fromPrivate (ByteArray
216- .fromHexString (Args .getInstance ().getLocalWitnesses ().getPrivateKey ()))
217- .getAddress ()));
218- blockCapsule1 .generatedByMyself = true ;
219- blockCapsule1 .setMerkleRoot ();
220- blockCapsule1 .sign (
221- ByteArray .fromHexString (Args .getInstance ().getLocalWitnesses ().getPrivateKey ()));
196+ BlockCapsule blockCapsule1 = createTestBlockCapsule (num + 1 ,
197+ dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderHash (), addressToProvateKeys );
222198
223- BlockCapsule blockCapsule2 = new BlockCapsule (num + 2 ,
224- blockCapsule1 .getBlockId ().getByteString (),
225- System .currentTimeMillis (),
226- ByteString .copyFrom (
227- ECKey .fromPrivate (ByteArray
228- .fromHexString (Args .getInstance ().getLocalWitnesses ().getPrivateKey ()))
229- .getAddress ()));
230- blockCapsule2 .generatedByMyself = true ;
231- blockCapsule2 .setMerkleRoot ();
232- blockCapsule2 .sign (
233- ByteArray .fromHexString (Args .getInstance ().getLocalWitnesses ().getPrivateKey ()));
199+ BlockCapsule blockCapsule2 = createTestBlockCapsule (num + 2 ,
200+ blockCapsule1 .getBlockId ().getByteString (), addressToProvateKeys );
234201
235- logger .error ("localWitness address:" + ByteString .copyFrom (
236- ECKey .fromPrivate (ByteArray
237- .fromHexString (Args .getInstance ().getLocalWitnesses ().getPrivateKey ()))
238- .getAddress ()));
239- logger .error ("*******is exists1:" + dbManager .getWitnessStore ().get (ByteString .copyFrom (
240- ECKey .fromPrivate (ByteArray
241- .fromHexString (Args .getInstance ().getLocalWitnesses ().getPrivateKey ()))
242- .getAddress ()).toByteArray ()));
243- logger .error ("*******is exists2:" + dbManager .getWitnessStore ().get (
244- blockCapsule0 .getInstance ().getBlockHeader ().getRawData ().getWitnessAddress ()
245- .toByteArray ()));
246- logger .error ("*******is exists3:" + dbManager .getWitnessStore ().get (
247- blockCapsule1 .getInstance ().getBlockHeader ().getRawData ().getWitnessAddress ()
248- .toByteArray ()));
249- logger .error ("*******is exists4:" + dbManager .getWitnessStore ().get (
250- blockCapsule2 .getInstance ().getBlockHeader ().getRawData ().getWitnessAddress ()
251- .toByteArray ()));
252-
253- logger .error ("******0*******" + "block0 id:" + blockCapsule0 .getBlockId ());
254- logger .error ("******1*******" + "block1 id:" + blockCapsule1 .getBlockId ());
255- logger .error ("******2*******" + "block2 id:" + blockCapsule2 .getBlockId ());
256202 dbManager .pushBlock (blockCapsule0 );
257203 dbManager .pushBlock (blockCapsule1 );
258204 dbManager .pushBlock (blockCapsule2 );
259- logger .error ("******in blockStore block size:"
260- + dbManager .getBlockStore ().dbSource .allKeys ().size ());
261- logger .error ("******in blockStore block:"
262- + dbManager .getBlockStore ().dbSource .allKeys ().stream ().map (ByteArray ::toHexString )
263- .collect (Collectors .toList ()));
264205
265206 Assert .assertNotNull (dbManager .getBlockStore ().get (blockCapsule1 .getBlockId ().getBytes ()));
266207 Assert .assertNotNull (dbManager .getBlockStore ().get (blockCapsule2 .getBlockId ().getBytes ()));
@@ -281,4 +222,38 @@ public void fork() throws ValidateSignatureException, ContractValidateException,
281222 Assert .assertEquals (dbManager .getHead ().getBlockId ().getByteString (),
282223 dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderHash ());
283224 }
225+
226+ private Map <ByteString , String > addTestWitnessAndAccount () {
227+ dbManager .getWitnesses ().clear ();
228+ return IntStream .range (0 , 2 ).mapToObj (i -> {
229+ ECKey ecKey = new ECKey (Utils .getRandom ());
230+ String privateKey = ByteArray .toHexString (ecKey .getPrivKey ().toByteArray ());
231+ ByteString address = ByteString .copyFrom (ecKey .getAddress ());
232+
233+ WitnessCapsule witnessCapsule = new WitnessCapsule (address );
234+ dbManager .getWitnessStore ().put (address .toByteArray (), witnessCapsule );
235+ dbManager .getWitnessController ().addWitness (witnessCapsule );
236+
237+ AccountCapsule accountCapsule =
238+ new AccountCapsule (Account .newBuilder ().setAddress (address ).build ());
239+ dbManager .getAccountStore ().put (address .toByteArray (), accountCapsule );
240+
241+ return Maps .immutableEntry (address , privateKey );
242+ })
243+ .collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
244+ }
245+
246+ private BlockCapsule createTestBlockCapsule (long number , ByteString hash ,
247+ Map <ByteString , String > addressToProvateKeys ) {
248+ long time = System .currentTimeMillis ();
249+ WitnessController witnessController = dbManager .getWitnessController ();
250+ ByteString witnessAddress =
251+ witnessController .getScheduledWitness (witnessController .getSlotAtTime (time ));
252+ BlockCapsule blockCapsule = new BlockCapsule (number , hash , time , witnessAddress );
253+ blockCapsule .generatedByMyself = true ;
254+ blockCapsule .setMerkleRoot ();
255+ blockCapsule .sign (ByteArray .fromHexString (addressToProvateKeys .get (witnessAddress )));
256+ return blockCapsule ;
257+ }
258+
284259}
0 commit comments