org.ethereum.vm.DataWord Java Examples

The following examples show how to use org.ethereum.vm.DataWord. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContractDetails.java    From ethereumj with MIT License 6 votes vote down vote up
public byte[] getEncoded() {

		if (rlpEncoded == null) {

			int size = storageKeys == null ? 0 : storageKeys.size();

			byte[][] keys = new byte[size][];
			byte[][] values = new byte[size][];

			for (int i = 0; i < size; ++i) {
				DataWord key = storageKeys.get(i);
				keys[i] = RLP.encodeElement(key.getData());
			}
			for (int i = 0; i < size; ++i) {
				DataWord value = storageValues.get(i);
				values[i] = RLP.encodeElement(value.getNoLeadZeroesData());
			}

			byte[] rlpKeysList = RLP.encodeList(keys);
			byte[] rlpValuesList = RLP.encodeList(values);
			byte[] rlpCode = RLP.encodeElement(code);

			this.rlpEncoded = RLP.encodeList(rlpKeysList, rlpValuesList, rlpCode);
		}
		return rlpEncoded;
	}
 
Example #2
Source File: StateExplorerWindow.java    From ethereumj with MIT License 6 votes vote down vote up
private String accountDetailsString(byte[] account, StateDataTableModel dataModel){	
	String ret = "";
	// 1) print account address
	ret = "Account: " + Hex.toHexString(account) + "\n";
	
	//2) print state 
	AccountState state = WorldManager.getInstance().getRepository().getAccountState(account);
	if(state != null)
		ret += state.toString() + "\n";
	
	//3) print storage
	ContractDetails contractDetails = WorldManager.getInstance().getRepository().getContractDetails(account);
	if(contractDetails != null) {
		Map<DataWord, DataWord> accountStorage = contractDetails.getStorage();
		dataModel.setData(accountStorage);
	}
	
	//4) code print
	byte[] code = WorldManager.getInstance().getRepository().getCode(account);
	if(code != null) {
		ret += "\n\nCode:\n";
		ret += Program.stringify(code, 0, "");
	}
	
	return ret;
}
 
Example #3
Source File: Heap.java    From nuls-v2 with MIT License 6 votes vote down vote up
public Object getArrayChunkFromState(ObjectRef arrayRef, String arrayKey) {
    if (this.repository == null) {
        return null;
    }
    DataWord dataWord = this.repository.getStorageValue(this.address, new DataWord(arrayKey));
    if (dataWord == null) {
        return null;
    }
    byte[] value = dataWord.getNoLeadZeroesData();
    Class clazz = arrayRef.getVariableType().getPrimitiveTypeClass();
    if (!arrayRef.getVariableType().getComponentType().isPrimitive()) {
        clazz = ObjectRef.class;
    }
    Object object = JsonUtils.decodeArray(new String(value), clazz, classNames);
    return object;
}
 
Example #4
Source File: RepositoryTest.java    From ethereumj with MIT License 6 votes vote down vote up
@Test // storage set/get
public void test10() {

    byte[] addr = Hex.decode("cd2a3d9f938e13cd947ec05abc7fe734df8dd826");
    byte[] code = Hex.decode("00");
    Repository repository = new RepositoryImpl();

    try {
        repository.createAccount(addr);
        repository.saveCode(addr, code);
        byte[] keyBytes = Hex.decode("cd2a3d9f938e13cd947ec05abc7fe734df8dd826");
        DataWord key = new DataWord(keyBytes);
        byte[] valueBytes = Hex.decode("0F4240");
        DataWord value = new DataWord(valueBytes);
        repository.addStorageRow(addr, key, value);
        DataWord fetchedValue = repository.getStorageValue(addr, key);
        assertEquals(value, fetchedValue);
    } finally {
        repository.close();
    }
}
 
Example #5
Source File: RepositoryImpl.java    From nuls with MIT License 6 votes vote down vote up
@Override
public synchronized RepositoryImpl startTracking() {
    Source<byte[], AccountState> trackAccountStateCache = new WriteCache.BytesKey<>(accountStateCache,
            WriteCache.CacheType.SIMPLE);
    Source<byte[], byte[]> trackCodeCache = new WriteCache.BytesKey<>(codeCache, WriteCache.CacheType.SIMPLE);
    MultiCache<CachedSource<DataWord, DataWord>> trackStorageCache = new MultiCache(storageCache) {
        @Override
        protected CachedSource create(byte[] key, CachedSource srcCache) {
            return new WriteCache<>(srcCache, WriteCache.CacheType.SIMPLE);
        }
    };

    RepositoryImpl ret = new RepositoryImpl(trackAccountStateCache, trackCodeCache, trackStorageCache);
    ret.parent = this;
    return ret;
}
 
Example #6
Source File: RepositoryImpl.java    From nuls-v2 with MIT License 6 votes vote down vote up
@Override
public synchronized RepositoryImpl startTracking() {
    Source<byte[], AccountState> trackAccountStateCache = new WriteCache.BytesKey<>(accountStateCache,
            WriteCache.CacheType.SIMPLE);
    Source<byte[], byte[]> trackCodeCache = new WriteCache.BytesKey<>(codeCache, WriteCache.CacheType.SIMPLE);
    MultiCache<CachedSource<DataWord, DataWord>> trackStorageCache = new MultiCache(storageCache) {
        @Override
        protected CachedSource create(byte[] key, CachedSource srcCache) {
            return new WriteCache<>(srcCache, WriteCache.CacheType.SIMPLE);
        }
    };

    RepositoryImpl ret = new RepositoryImpl(trackAccountStateCache, trackCodeCache, trackStorageCache);
    ret.parent = this;
    return ret;
}
 
Example #7
Source File: Heap.java    From nuls with MIT License 6 votes vote down vote up
public Object getArrayChunkFromState(ObjectRef arrayRef, String arrayKey) {
    if (this.repository == null) {
        return null;
    }
    DataWord dataWord = this.repository.getStorageValue(this.address, new DataWord(arrayKey));
    if (dataWord == null) {
        return null;
    }
    byte[] value = dataWord.getNoLeadZeroesData();
    Class clazz = arrayRef.getVariableType().getPrimitiveTypeClass();
    if (!arrayRef.getVariableType().getComponentType().isPrimitive()) {
        clazz = ObjectRef.class;
    }
    Object object = JsonUtils.decodeArray(new String(value), clazz, classNames);
    return object;
}
 
Example #8
Source File: JSONHelper.java    From ethereumj with MIT License 6 votes vote down vote up
public static void dumpState(ObjectNode statesNode, String address, AccountState state, ContractDetails details) {  	

        List<DataWord> storageKeys = new ArrayList<>(details.getStorage().keySet());
		Collections.sort((List<DataWord>) storageKeys);

        ObjectNode account = statesNode.objectNode();
        ObjectNode storage = statesNode.objectNode();
                
        for (DataWord key : storageKeys) {
        	storage.put("0x" + Hex.toHexString(key.getData()),
					"0x" + Hex.toHexString(details.getStorage().get(key).getNoLeadZeroesData()));
        }
        account.put("balance", state.getBalance() == null ? "0" : state.getBalance().toString());
//        account.put("codeHash", details.getCodeHash() == null ? "0x" : "0x" + Hex.toHexString(details.getCodeHash()));
        account.put("code", details.getCode() == null ? "0x" : "0x" + Hex.toHexString(details.getCode()));
        account.put("nonce", state.getNonce() == null ? "0" : state.getNonce().toString());
        account.put("storage", storage);
        account.put("storage_root", state.getStateRoot() == null ? "" : Hex.toHexString(state.getStateRoot()));
        
        statesNode.put(address, account);
    }
 
Example #9
Source File: RepositoryImpl.java    From ethereumj with MIT License 6 votes vote down vote up
public void addStorageRow(byte[] addr, DataWord key, DataWord value) {

        if (key == null) return;
        AccountState      state = getAccountState(addr);
        ContractDetails   details = getContractDetails(addr);

        if (state == null || details == null) return;
        details.put(key, value);

        byte[] storageHash = details.getStorageHash();
        state.setStateRoot(storageHash);

        if (logger.isDebugEnabled())
            logger.debug("Storage key/value saved:\n account:\t [{}]\n key:\t\t [{}]\n value:\t\t [{}]\n new hash:\t [{}]",
                    Hex.toHexString(addr),
                    Hex.toHexString(key.getNoLeadZeroesData()),
                    Hex.toHexString(value.getNoLeadZeroesData()),
                    Hex.toHexString(storageHash));

        accountStateDB.update(addr, state.getEncoded());
        contractDetailsDB.put(addr, details.getEncoded());
    }
 
Example #10
Source File: Op.java    From ethereumj with MIT License 5 votes vote down vote up
public void saveStack(Stack<DataWord> stack) {

        this.stack = new ArrayList<>();

        for(DataWord element : stack){
            this.stack.add(0, Hex.toHexString(element.getNoLeadZeroesData()));
        }
    }
 
Example #11
Source File: Op.java    From ethereumj with MIT License 5 votes vote down vote up
public void saveStorageMap(Map<DataWord, DataWord> storage) {

        this.storage = new HashMap<>();
        List<DataWord> keys = new ArrayList<>(storage.keySet());
        Collections.sort(keys);
        for (DataWord key : keys) {
            DataWord value = storage.get(key);
            this.storage.put(Hex.toHexString(key.getNoLeadZeroesData()),
                    Hex.toHexString(value.getNoLeadZeroesData()));
        }
    }
 
Example #12
Source File: Serializers.java    From nuls-v2 with MIT License 5 votes vote down vote up
@Override
public DataWord deserialize(byte[] stream) {
    if (stream == null || stream.length == 0) {
        return null;
    }
    byte[] dataDecoded = RLP.decode2(stream).get(0).getRLPData();
    return DataWord.of(dataDecoded);
}
 
Example #13
Source File: RepositoryImpl.java    From nuls-v2 with MIT License 5 votes vote down vote up
@Override
public synchronized void addStorageRow(byte[] addr, DataWord key, DataWord value) {
    getOrCreateAccountState(addr);

    Source<DataWord, DataWord> contractStorage = storageCache.get(addr);
    contractStorage.put(key, value.isZero() ? null : value);
}
 
Example #14
Source File: ContractDetails.java    From ethereumj with MIT License 5 votes vote down vote up
public Map<DataWord, DataWord> getStorage() {
      Map<DataWord, DataWord> storage = new HashMap<>();
for (int i = 0; storageKeys != null && i < storageKeys.size(); ++i) {
	storage.put(storageKeys.get(i), storageValues.get(i));
}
      return Collections.unmodifiableMap(storage);
  }
 
Example #15
Source File: Serializers.java    From nuls with MIT License 5 votes vote down vote up
@Override
public DataWord deserialize(byte[] stream) {
    if (stream == null || stream.length == 0) {
        return null;
    }
    byte[] dataDecoded = RLP.decode2(stream).get(0).getRLPData();
    return DataWord.of(dataDecoded);
}
 
Example #16
Source File: StateExplorerWindow.java    From ethereumj with MIT License 5 votes vote down vote up
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
	DataWord key = (DataWord) this.data.keySet().toArray()[rowIndex];
	
	if(columnIndex == 0) {
		return getDataWithEncoding(key.getData(), keyEncodingType);
	}
	else {
		DataWord value = this.data.get(key);
		return getDataWithEncoding(value.getData(), valueEncodingType);
	}
}
 
Example #17
Source File: Heap.java    From nuls-v2 with MIT License 5 votes vote down vote up
public Map<String, Object> getFieldsFromState(ObjectRef objectRef) {
    if (this.repository == null) {
        return null;
    }
    String key = JsonUtils.encode(objectRef, classNames);
    DataWord dataWord = this.repository.getStorageValue(this.address, new DataWord(key));
    if (dataWord == null) {
        return null;
    }
    byte[] value = dataWord.getNoLeadZeroesData();
    Map<String, Object> map = (Map<String, Object>) JsonUtils.decode(new String(value), classNames);
    return map;
}
 
Example #18
Source File: ContractDetails.java    From ethereumj with MIT License 5 votes vote down vote up
public DataWord get(DataWord key) {

		if (storageKeys.size() == 0)
			return null;

		int foundIndex = storageKeys.indexOf(key);
		if (foundIndex != -1)
			return storageValues.get(foundIndex);
		else
			return null;
	}
 
Example #19
Source File: RepositoryImpl.java    From nuls with MIT License 5 votes vote down vote up
@Override
public synchronized void addStorageRow(byte[] addr, DataWord key, DataWord value) {
    getOrCreateAccountState(addr);

    Source<DataWord, DataWord> contractStorage = storageCache.get(addr);
    contractStorage.put(key, value.isZero() ? null : value);
}
 
Example #20
Source File: Heap.java    From nuls with MIT License 5 votes vote down vote up
public Map<String, Object> getFieldsFromState(ObjectRef objectRef) {
    if (this.repository == null) {
        return null;
    }
    String key = JsonUtils.encode(objectRef, classNames);
    DataWord dataWord = this.repository.getStorageValue(this.address, new DataWord(key));
    if (dataWord == null) {
        return null;
    }
    byte[] value = dataWord.getNoLeadZeroesData();
    Map<String, Object> map = (Map<String, Object>) JsonUtils.decode(new String(value), classNames);
    return map;
}
 
Example #21
Source File: RepositoryImpl.java    From nuls-v2 with MIT License 4 votes vote down vote up
@Override
public Set<DataWord> getStorageKeys(byte[] addr) {
    throw new RuntimeException("Not supported");
}
 
Example #22
Source File: RepositoryImpl.java    From nuls with MIT License 4 votes vote down vote up
@Override
public Set<DataWord> getStorageKeys(byte[] addr) {
    throw new RuntimeException("Not supported");
}
 
Example #23
Source File: RepositoryImpl.java    From nuls-v2 with MIT License 4 votes vote down vote up
@Override
public synchronized DataWord getStorageValue(byte[] addr, DataWord key) {
    AccountState accountState = getAccountState(addr);
    return accountState == null ? null : storageCache.get(addr).get(key);
}
 
Example #24
Source File: RepositoryImpl.java    From nuls with MIT License 4 votes vote down vote up
@Override
public void setStorage(List<DataWord> storageKeys, List<DataWord> storageValues) {
    throw new RuntimeException("Not supported");
}
 
Example #25
Source File: RepositoryImpl.java    From nuls with MIT License 4 votes vote down vote up
@Override
public Map<DataWord, DataWord> getStorage() {
    throw new RuntimeException("Not supported");
}
 
Example #26
Source File: RepositoryImpl.java    From nuls with MIT License 4 votes vote down vote up
@Override
public DataWord get(DataWord key) {
    return RepositoryImpl.this.getStorageValue(address, key);
}
 
Example #27
Source File: RepositoryImpl.java    From nuls with MIT License 4 votes vote down vote up
@Override
public synchronized DataWord getStorageValue(byte[] addr, DataWord key) {
    AccountState accountState = getAccountState(addr);
    return accountState == null ? null : storageCache.get(addr).get(key);
}
 
Example #28
Source File: RepositoryImpl.java    From nuls with MIT License 4 votes vote down vote up
@Override
public void put(DataWord key, DataWord value) {
    RepositoryImpl.this.addStorageRow(address, key, value);
}
 
Example #29
Source File: RepositoryImpl.java    From nuls with MIT License 4 votes vote down vote up
@Override
public Map<DataWord, DataWord> getStorage(@Nullable Collection<DataWord> keys) {
    throw new RuntimeException("Not supported");
}
 
Example #30
Source File: RepositoryWrapper.java    From nuls with MIT License 4 votes vote down vote up
@Override
public void addStorageRow(byte[] addr, DataWord key, DataWord value) {
    getRepository().addStorageRow(addr, key, value);
}