Search in sources :

Example 1 with RequestTrieData

use of org.aion.zero.impl.sync.msg.RequestTrieData in project aion by aionnetwork.

the class RequestTrieDataHandler method receive.

@Override
public void receive(int peerId, String displayId, final byte[] message) {
    if (message == null || message.length == 0) {
        this.log.debug("<req-trie empty message from peer={}>", displayId);
        return;
    }
    RequestTrieData request = RequestTrieData.decode(message);
    if (request != null) {
        DatabaseType dbType = request.getDbType();
        ByteArrayWrapper key = ByteArrayWrapper.wrap(request.getNodeKey());
        int limit = request.getLimit();
        if (log.isDebugEnabled()) {
            this.log.debug("<req-trie from-db={} key={} peer={}>", dbType, key, displayId);
        }
        byte[] value = null;
        try {
            // retrieve from blockchain depending on db type
            value = chain.getTrieNode(key.toBytes(), dbType);
        } catch (Exception e) {
            this.log.error("<req-trie value retrieval failed>", e);
        }
        if (value != null) {
            ResponseTrieData response;
            if (limit == 1) {
                // generate response without referenced nodes
                response = new ResponseTrieData(key, value, dbType);
            } else {
                // check for internal limit on the request
                if (limit == 0) {
                    limit = TRIE_DATA_REQUEST_MAXIMUM_BATCH_SIZE;
                } else {
                    // the first value counts towards the limit
                    limit = Math.min(limit - 1, TRIE_DATA_REQUEST_MAXIMUM_BATCH_SIZE);
                }
                Map<ByteArrayWrapper, byte[]> referencedNodes = Collections.emptyMap();
                try {
                    // determine if the node can be expanded
                    referencedNodes = chain.getReferencedTrieNodes(value, limit, dbType);
                } catch (Exception e) {
                    this.log.error("<req-trie reference retrieval failed>", e);
                }
                // generate response with referenced nodes
                response = new ResponseTrieData(key, value, referencedNodes, dbType);
            }
            // reply to request
            this.p2p.send(peerId, displayId, response);
        }
    } else {
        this.log.error("<req-trie decode-error msg-bytes={} peer={}>", message.length, displayId);
        if (log.isTraceEnabled()) {
            this.log.trace("<req-trie decode-error for msg={} peer={}>", Arrays.toString(message), displayId);
        }
    }
}
Also used : DatabaseType(org.aion.zero.impl.sync.DatabaseType) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ResponseTrieData(org.aion.zero.impl.sync.msg.ResponseTrieData) RequestTrieData(org.aion.zero.impl.sync.msg.RequestTrieData)

Aggregations

ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)1 DatabaseType (org.aion.zero.impl.sync.DatabaseType)1 RequestTrieData (org.aion.zero.impl.sync.msg.RequestTrieData)1 ResponseTrieData (org.aion.zero.impl.sync.msg.ResponseTrieData)1