-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement pending state for airdrop purposes (#13566)
Signed-off-by: ibankov <[email protected]> Co-authored-by: Valentin Tronkov <[email protected]>
- Loading branch information
1 parent
222940d
commit 12f0f1d
Showing
17 changed files
with
700 additions
and
6 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
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
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
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
70 changes: 70 additions & 0 deletions
70
...e-impl/src/main/java/com/hedera/node/app/service/token/impl/ReadableAirdropStoreImpl.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,70 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.hedera.node.app.service.token.impl; | ||
|
||
import static com.hedera.node.app.service.token.impl.schemas.V0500TokenSchema.AIRDROPS_KEY; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.hedera.hapi.node.base.PendingAirdropId; | ||
import com.hedera.hapi.node.base.PendingAirdropValue; | ||
import com.hedera.node.app.service.token.ReadableAccountStore; | ||
import com.hedera.node.app.service.token.ReadableAirdropStore; | ||
import com.swirlds.state.spi.ReadableKVState; | ||
import com.swirlds.state.spi.ReadableStates; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import edu.umd.cs.findbugs.annotations.Nullable; | ||
|
||
/** | ||
* Default implementation of {@link ReadableAccountStore} | ||
*/ | ||
public class ReadableAirdropStoreImpl implements ReadableAirdropStore { | ||
/** The underlying data storage class that holds the airdrop data. */ | ||
private final ReadableKVState<PendingAirdropId, PendingAirdropValue> readableAirdropState; | ||
|
||
/** | ||
* Create a new {@link ReadableAirdropStoreImpl} instance. | ||
* | ||
* @param states The state to use. | ||
*/ | ||
public ReadableAirdropStoreImpl(@NonNull final ReadableStates states) { | ||
requireNonNull(states); | ||
this.readableAirdropState = states.get(AIRDROPS_KEY); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public boolean exists(@NonNull final PendingAirdropId airdropId) { | ||
return readableAirdropState.contains(requireNonNull(airdropId)); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
@Nullable | ||
public PendingAirdropValue get(@NonNull final PendingAirdropId airdropId) { | ||
requireNonNull(airdropId); | ||
if (airdropId.hasNonFungibleToken()) { | ||
return null; | ||
} | ||
return readableAirdropState.get(airdropId); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public long sizeOfState() { | ||
return readableAirdropState.size(); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
...rvice-impl/src/main/java/com/hedera/node/app/service/token/impl/WritableAirdropStore.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,114 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.hedera.node.app.service.token.impl; | ||
|
||
import static com.hedera.node.app.service.token.impl.schemas.V0500TokenSchema.AIRDROPS_KEY; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.hedera.hapi.node.base.PendingAirdropId; | ||
import com.hedera.hapi.node.base.PendingAirdropValue; | ||
import com.hedera.node.app.spi.metrics.StoreMetricsService; | ||
import com.hedera.node.config.data.TokensConfig; | ||
import com.swirlds.config.api.Configuration; | ||
import com.swirlds.state.spi.WritableKVState; | ||
import com.swirlds.state.spi.WritableStates; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import edu.umd.cs.findbugs.annotations.Nullable; | ||
|
||
/** | ||
* Provides write methods for modifying underlying data storage mechanisms for | ||
* working with Pending Airdrops. | ||
* | ||
* <p>This class is not exported from the module. It is an internal implementation detail. | ||
* This class is not complete, it will be extended with other methods like remove, update etc., | ||
*/ | ||
public class WritableAirdropStore extends ReadableAirdropStoreImpl { | ||
/** | ||
* The underlying data storage class that holds the Pending Airdrops data. | ||
*/ | ||
private final WritableKVState<PendingAirdropId, PendingAirdropValue> airdropState; | ||
|
||
/** | ||
* Create a new {@link WritableAirdropStore} instance. | ||
* | ||
* @param states The state to use. | ||
*/ | ||
public WritableAirdropStore( | ||
@NonNull final WritableStates states, | ||
@NonNull final Configuration configuration, | ||
@NonNull final StoreMetricsService storeMetricsService) { | ||
super(states); | ||
airdropState = states.get(AIRDROPS_KEY); | ||
|
||
final long maxCapacity = configuration.getConfigData(TokensConfig.class).maxAllowedAirdorps(); | ||
final var storeMetrics = storeMetricsService.get(StoreMetricsService.StoreType.AIRDROPS, maxCapacity); | ||
airdropState.setMetrics(storeMetrics); | ||
} | ||
|
||
/** | ||
* Persists a new {@link PendingAirdropId} with given {@link PendingAirdropValue} into the state, | ||
* as well as exporting its ID to the transaction receipt. If there is existing | ||
* airdrop with the same id we add the value to the existing drop. | ||
* | ||
* @param airdropId - the airdropId to be persisted. | ||
* @param airdropValue - the value for the given airdropId to be persisted. | ||
*/ | ||
public void put(@NonNull final PendingAirdropId airdropId, @NonNull final PendingAirdropValue airdropValue) { | ||
requireNonNull(airdropId); | ||
requireNonNull(airdropValue); | ||
|
||
if (!airdropState.contains(airdropId)) { | ||
airdropState.put(airdropId, airdropValue); | ||
return; | ||
} | ||
|
||
if (airdropId.hasFungibleTokenType()) { | ||
var existingAirdropValue = | ||
requireNonNull(airdropState.get(airdropId)).amount(); | ||
var newValue = airdropValue.amount() + existingAirdropValue; | ||
var newAirdropValue = | ||
PendingAirdropValue.newBuilder().amount(newValue).build(); | ||
airdropState.put(airdropId, newAirdropValue); | ||
} | ||
} | ||
|
||
/** | ||
* Removes a {@link PendingAirdropId} from the state | ||
* | ||
* @param airdropId the {@code PendingAirdropId} to be removed | ||
*/ | ||
public void remove(@NonNull final PendingAirdropId airdropId) { | ||
airdropState.remove(requireNonNull(airdropId)); | ||
} | ||
|
||
/** | ||
* Returns the {@link PendingAirdropValue} with the given airdrop id. If the airdrop contains only NFT return {@code null}. | ||
* If no such airdrop exists, returns {@code null} | ||
* | ||
* @param airdropId - the id of the airdrop, which value should be retrieved | ||
* @return the fungible airdrop value, or {@code null} if no such | ||
* airdrop exists | ||
*/ | ||
@Nullable | ||
public PendingAirdropValue getForModify(@NonNull final PendingAirdropId airdropId) { | ||
requireNonNull(airdropId); | ||
if (airdropId.hasNonFungibleToken()) { | ||
return null; | ||
} | ||
return airdropState.getForModify(airdropId); | ||
} | ||
} |
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
Oops, something went wrong.