Coverage Summary for Class: StateRootTranslator (co.rsk.db)

Class Class, % Method, % Line, %
StateRootTranslator 100% (1/1) 33.3% (1/3) 25% (4/16)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2019 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.db; 19  20 import co.rsk.crypto.Keccak256; 21 import org.ethereum.datasource.KeyValueDataSource; 22  23 import java.util.Map; 24  25 public class StateRootTranslator { 26  27  private KeyValueDataSource stateRootDB; 28  private Map<Keccak256, Keccak256> stateRootCache; 29  30  public StateRootTranslator(KeyValueDataSource stateRootDB, Map<Keccak256, Keccak256> stateRootCache) { 31  this.stateRootDB = stateRootDB; 32  this.stateRootCache = stateRootCache; 33  } 34  35  public synchronized Keccak256 get(Keccak256 oldStateRoot) { 36  Keccak256 stateRoot = this.stateRootCache.get(oldStateRoot); 37  38  if (stateRoot != null) { 39  return stateRoot; 40  } 41  42  byte[] stateRootBytes = stateRootDB.get(oldStateRoot.getBytes()); 43  if (stateRootBytes == null) { 44  return null; 45  } 46  47  Keccak256 newStateRoot = new Keccak256(stateRootBytes); 48  this.stateRootCache.put(oldStateRoot, newStateRoot); 49  return newStateRoot; 50  } 51  52  public void put(Keccak256 oldStateRoot, Keccak256 newStateRoot) { 53  this.stateRootCache.put(oldStateRoot, newStateRoot); 54  this.stateRootDB.put(oldStateRoot.getBytes(), newStateRoot.getBytes()); 55  } 56 }