forked from hyperledger/besu
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactors into reusable BlobCache, and rekeys on versioned hash
Signed-off-by: Justin Florentine <[email protected]>
- Loading branch information
Showing
6 changed files
with
132 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/BlobCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright Hyperledger Besu contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.hyperledger.besu.ethereum.eth.transactions; | ||
|
||
import org.hyperledger.besu.datatypes.BlobsWithCommitments; | ||
import org.hyperledger.besu.datatypes.VersionedHash; | ||
import org.hyperledger.besu.ethereum.core.Transaction; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class BlobCache { | ||
private final Cache<VersionedHash, BlobsWithCommitments.BlobQuad> cache; | ||
private static final Logger LOG = LoggerFactory.getLogger(BlobCache.class); | ||
|
||
public BlobCache() { | ||
// TODO: needs size limit, ttl policy and eviction on finalization policy - cache size should | ||
// max out around 6 * blocks since final | ||
|
||
this.cache = Caffeine.newBuilder().build(); | ||
} | ||
|
||
public void cacheBlobs(final Transaction t) { | ||
if (t.getType().supportsBlob()) { | ||
var bwc = t.getBlobsWithCommitments(); | ||
if (bwc.isPresent()) { | ||
bwc.get().getBlobQuads().stream() | ||
.forEach(blobQuad -> this.cache.put(blobQuad.versionedHash(), blobQuad)); | ||
} else { | ||
LOG.debug("transaction is missing blobs, cannot cache"); | ||
} | ||
} | ||
} | ||
|
||
public Optional<Transaction> restoreBlob(final Transaction transaction) { | ||
if (transaction.getType().supportsBlob()) { | ||
Optional<List<VersionedHash>> maybeHashes = transaction.getVersionedHashes(); | ||
if (maybeHashes.isPresent()) { | ||
if (!maybeHashes.get().isEmpty()) { | ||
Transaction.Builder txBuilder = Transaction.builder(); | ||
txBuilder.copiedFrom(transaction); | ||
List<BlobsWithCommitments.BlobQuad> blobQuads = | ||
maybeHashes.get().stream().map(cache::getIfPresent).toList(); | ||
final BlobsWithCommitments bwc = new BlobsWithCommitments(blobQuads); | ||
if (blobQuads.stream() | ||
.map(BlobsWithCommitments.BlobQuad::versionedHash) | ||
.toList() | ||
.containsAll(transaction.getVersionedHashes().get())) { | ||
txBuilder.blobsWithCommitments(bwc); | ||
return Optional.of(txBuilder.build()); | ||
} else { | ||
LOG.debug("did not find all versioned hashes to restore from cache"); | ||
return Optional.empty(); | ||
} | ||
} else { | ||
LOG.debug("can't restore blobs for transaction with empty list of versioned hashes"); | ||
return Optional.empty(); | ||
} | ||
} else { | ||
LOG.debug("can't restore blobs for transaction without list of versioned hashes"); | ||
return Optional.empty(); | ||
} | ||
|
||
} else { | ||
LOG.debug( | ||
"can't restore blobs for non-blob transaction of type " + transaction.getType().name()); | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters