org.ethereum.datasource.Serializers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ethereumj-core Show documentation
Show all versions of ethereumj-core Show documentation
Java implementation of the Ethereum protocol adapted to use for Hedera Smart Contract Service
The newest version!
/*
* Copyright (c) [2016] [ ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see .
*/
package org.ethereum.datasource;
import org.ethereum.core.AccountState;
import org.ethereum.core.BlockHeader;
import org.ethereum.util.RLP;
import org.ethereum.util.Value;
import org.ethereum.vm.DataWord;
/**
* Collection of common Serializers
* Created by Anton Nashatyrev on 08.11.2016.
*/
public class Serializers {
/**
* No conversion
*/
public static class Identity implements Serializer {
@Override
public T serialize(T object) {
return object;
}
@Override
public T deserialize(T stream) {
return stream;
}
}
/**
* Serializes/Deserializes AccountState instances from the State Trie (part of Ethereum spec)
*/
public final static Serializer AccountStateSerializer = new Serializer() {
@Override
public byte[] serialize(AccountState object) {
return object.getEncoded();
}
@Override
public AccountState deserialize(byte[] stream) {
return stream == null || stream.length == 0 ? null : new AccountState(stream);
}
};
/**
* Contract storage key serializer
*/
public final static Serializer StorageKeySerializer = new Serializer() {
@Override
public byte[] serialize(DataWord object) {
return object.getData();
}
@Override
public DataWord deserialize(byte[] stream) {
return DataWord.of(stream);
}
};
/**
* Contract storage value serializer (part of Ethereum spec)
*/
public final static Serializer StorageValueSerializer = new Serializer() {
@Override
public byte[] serialize(DataWord object) {
return RLP.encodeElement(object.getNoLeadZeroesData());
}
@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);
}
};
/**
* Trie node serializer (part of Ethereum spec)
*/
public final static Serializer TrieNodeSerializer = new Serializer() {
@Override
public byte[] serialize(Value object) {
return object.asBytes();
}
@Override
public Value deserialize(byte[] stream) {
return new Value(stream);
}
};
/**
* Trie node serializer (part of Ethereum spec)
*/
public final static Serializer BlockHeaderSerializer = new Serializer() {
@Override
public byte[] serialize(BlockHeader object) {
return object == null ? null : object.getEncoded();
}
@Override
public BlockHeader deserialize(byte[] stream) {
return stream == null ? null : new BlockHeader(stream);
}
};
/**
* AS IS serializer (doesn't change anything)
*/
public final static Serializer AsIsSerializer = new Serializer() {
@Override
public byte[] serialize(byte[] object) {
return object;
}
@Override
public byte[] deserialize(byte[] stream) {
return stream;
}
};
}