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

Class Class, % Method, % Line, %
ExportState 0% (0/1) 0% (0/4) 0% (0/32)


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 export state CLI tool 33  * This is an experimental/unsupported tool 34  */ 35 public class ExportState { 36  public static void main(String[] args) { 37  RskContext ctx = new RskContext(args); 38  BlockStore blockStore = ctx.getBlockStore(); 39  TrieStore trieStore = ctx.getTrieStore(); 40  41  execute(args, blockStore, trieStore, System.out); 42  } 43  44  public static void execute(String[] args, BlockStore blockStore, TrieStore trieStore, PrintStream writer) { 45  long blockNumber = Long.parseLong(args[0]); 46  47  Block block = blockStore.getChainBlockByNumber(blockNumber); 48  49  Optional<Trie> otrie = trieStore.retrieve(block.getStateRoot()); 50  51  if (!otrie.isPresent()) { 52  return; 53  } 54  55  Trie trie = otrie.get(); 56  57  processTrie(trie, writer); 58  } 59  60  private static void processTrie(Trie trie, PrintStream writer) { 61  writer.println(ByteUtil.toHexString(trie.toMessage())); 62  63  NodeReference leftReference = trie.getLeft(); 64  65  if (!leftReference.isEmpty()) { 66  Optional<Trie> left = leftReference.getNode(); 67  68  if (left.isPresent()) { 69  Trie leftTrie = left.get(); 70  71  if (!leftReference.isEmbeddable()) { 72  processTrie(leftTrie, writer); 73  } 74  75  if (leftTrie.hasLongValue()) { 76  writer.println(ByteUtil.toHexString(leftTrie.getValue())); 77  } 78  } 79  } 80  81  NodeReference rightReference = trie.getRight(); 82  83  if (!rightReference.isEmpty()) { 84  Optional<Trie> right = rightReference.getNode(); 85  86  if (right.isPresent()) { 87  Trie rightTrie = right.get(); 88  89  if (!rightReference.isEmbeddable()) { 90  processTrie(rightTrie, writer); 91  } 92  93  if (rightTrie.hasLongValue()) { 94  writer.println(ByteUtil.toHexString(rightTrie.getValue())); 95  } 96  } 97  } 98  99  if (trie.hasLongValue()) { 100  writer.println(ByteUtil.toHexString(trie.getValue())); 101  } 102  } 103 }