Coverage Summary for Class: ShowStateInfo (co.rsk.cli.tools)

Class Class, % Method, % Line, %
ShowStateInfo 0% (0/1) 0% (0/4) 0% (0/44)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 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 package co.rsk.cli.tools; 19  20 import co.rsk.RskContext; 21 import co.rsk.trie.NodeReference; 22 import co.rsk.trie.Trie; 23 import co.rsk.trie.TrieStore; 24 import org.ethereum.core.Block; 25 import org.ethereum.db.BlockStore; 26 import org.ethereum.util.ByteUtil; 27  28 import java.io.PrintStream; 29 import java.util.Optional; 30  31 /** 32  * The entry point for show state info CLI util 33  * This is an experimental/unsupported tool 34  */ 35 public class ShowStateInfo { 36  private static int nnodes; 37  private static int nvalues; 38  private static int nbytes; 39  40  public static void main(String[] args) { 41  RskContext ctx = new RskContext(args); 42  BlockStore blockStore = ctx.getBlockStore(); 43  TrieStore trieStore = ctx.getTrieStore(); 44  45  execute(args, blockStore, trieStore, System.out); 46  } 47  48  public static void execute(String[] args, BlockStore blockStore, TrieStore trieStore, PrintStream writer) { 49  Block block; 50  51  if ("best".equals(args[0])) { 52  block = blockStore.getBestBlock(); 53  } 54  else { 55  block = blockStore.getChainBlockByNumber(Long.parseLong(args[0])); 56  } 57  58  writer.println("Block number: " + block.getNumber()); 59  writer.println("Block hash: " + ByteUtil.toHexString(block.getHash().getBytes())); 60  writer.println("Block parent hash: " + ByteUtil.toHexString(block.getParentHash().getBytes())); 61  writer.println("Block root hash: " + ByteUtil.toHexString(block.getStateRoot())); 62  63  Optional<Trie> otrie = trieStore.retrieve(block.getStateRoot()); 64  65  if (otrie.isPresent()) { 66  Trie trie = otrie.get(); 67  68  processTrie(trie); 69  } 70  71  writer.println("Trie nodes: " + nnodes); 72  writer.println("Trie long values: " + nvalues); 73  writer.println("Trie MB: " + (double)nbytes / (1024*1024)); 74  } 75  76  private static void processTrie(Trie trie) { 77  nnodes++; 78  nbytes += trie.getMessageLength(); 79  80  NodeReference leftReference = trie.getLeft(); 81  82  if (!leftReference.isEmpty()) { 83  Optional<Trie> left = leftReference.getNode(); 84  85  if (left.isPresent()) { 86  Trie leftTrie = left.get(); 87  88  if (!leftReference.isEmbeddable()) { 89  processTrie(leftTrie); 90  } 91  92  if (leftTrie.hasLongValue()) { 93  nvalues++; 94  nbytes += leftTrie.getValue().length; 95  } 96  } 97  } 98  99  NodeReference rightReference = trie.getRight(); 100  101  if (!rightReference.isEmpty()) { 102  Optional<Trie> right = rightReference.getNode(); 103  104  if (right.isPresent()) { 105  Trie rightTrie = right.get(); 106  107  if (!rightReference.isEmbeddable()) { 108  processTrie(rightTrie); 109  } 110  111  if (rightTrie.hasLongValue()) { 112  nvalues++; 113  nbytes += rightTrie.getValue().length; 114  } 115  } 116  } 117  118  if (trie.hasLongValue()) { 119  nvalues++; 120  nbytes += trie.getValue().length; 121  } 122  } 123 }