-
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.
Signed-off-by: abhishek-hedera <[email protected]>
- Loading branch information
1 parent
915dacc
commit 2596ae3
Showing
4 changed files
with
160 additions
and
2 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
58 changes: 58 additions & 0 deletions
58
hedera-node/src/main/java/com/hedera/services/utils/PlatformTxnRecord.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,58 @@ | ||
package com.hedera.services.utils; | ||
|
||
/*- | ||
* | ||
* Hedera Services Node | ||
* | ||
* Copyright (C) 2018 - 2021 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. | ||
* | ||
*/ | ||
|
||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import com.swirlds.common.Transaction; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class PlatformTxnRecord { | ||
|
||
private Cache<Transaction, PlatformTxnAccessor> cache; | ||
private long timeToExpire; | ||
|
||
public PlatformTxnRecord(long timeToExpire) { | ||
this.timeToExpire = timeToExpire; | ||
cache = CacheBuilder.newBuilder() | ||
.expireAfterWrite(timeToExpire, TimeUnit.SECONDS) | ||
.build(); | ||
} | ||
|
||
public PlatformTxnAccessor getPlatformTxnAccessor(Transaction platformTxn){ | ||
if(platformTxn == null) | ||
return null; | ||
|
||
return cache.getIfPresent(platformTxn); | ||
|
||
} | ||
|
||
public void addTransaction(Transaction transaction) throws InvalidProtocolBufferException { | ||
PlatformTxnAccessor accessor = new PlatformTxnAccessor(transaction); | ||
cache.put(transaction, accessor); | ||
} | ||
|
||
public long getTimeToExpire() { | ||
return timeToExpire; | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
hedera-node/src/test/java/com/hedera/services/utils/PlatformTxnRecordTest.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,102 @@ | ||
package com.hedera.services.utils; | ||
|
||
/*- | ||
* | ||
* Hedera Services Node | ||
* | ||
* Copyright (C) 2018 - 2021 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. | ||
* | ||
*/ | ||
|
||
|
||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import com.swirlds.common.Transaction; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.hedera.services.utils.SleepingPause.SLEEPING_PAUSE; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.mock; | ||
|
||
public class PlatformTxnRecordTest { | ||
private Transaction platformTxn; | ||
private PlatformTxnRecord subject; | ||
|
||
@BeforeEach | ||
private void setup(){ | ||
platformTxn = mock(Transaction.class); | ||
given(platformTxn.getContents()).willReturn( | ||
com.hederahashgraph.api.proto.java.Transaction.getDefaultInstance().toByteArray()); | ||
|
||
subject = new PlatformTxnRecord(1L); | ||
} | ||
|
||
@Test | ||
public void getPlatformTxnAccessorWillReturnNullWhenEmptyTransaction() { | ||
// when | ||
assertNull(subject.getPlatformTxnAccessor(null)); | ||
} | ||
|
||
@Test | ||
void getPlatformTxnAccessorWillReturnNullWhenTransactionsAreDifferent() throws InvalidProtocolBufferException{ | ||
// setup | ||
Transaction transactionA = new Transaction("abc".getBytes()); | ||
|
||
// when | ||
subject.addTransaction(platformTxn); | ||
|
||
// then | ||
assertNull(subject.getPlatformTxnAccessor(transactionA)); | ||
} | ||
|
||
@Test | ||
void addTransactionWillThrowExceptionOnInvalidTxn() { | ||
// setup | ||
Transaction transactionA = new Transaction("abc".getBytes()); | ||
|
||
// when | ||
assertThrows(InvalidProtocolBufferException.class, () -> subject.addTransaction(transactionA)); | ||
} | ||
|
||
@Test | ||
void getPlatformTxnAccessorReturnsSameTransaction() throws InvalidProtocolBufferException{ | ||
// when | ||
subject.addTransaction(platformTxn); | ||
|
||
//then | ||
assertEquals(platformTxn, subject.getPlatformTxnAccessor(platformTxn).getPlatformTxn()); | ||
} | ||
|
||
@Test | ||
void defaultTimeToExpireIsSame(){ | ||
// when | ||
assertEquals(1, subject.getTimeToExpire()); | ||
} | ||
|
||
@Test | ||
void getPlatformTxnAccessorWillReturnNullAfterExpiry() throws InvalidProtocolBufferException { | ||
// when | ||
subject.addTransaction(platformTxn); | ||
|
||
// then | ||
SLEEPING_PAUSE.forMs(50L); | ||
assertEquals(platformTxn, subject.getPlatformTxnAccessor(platformTxn).getPlatformTxn()); | ||
SLEEPING_PAUSE.forMs(1000L); | ||
assertNull(subject.getPlatformTxnAccessor(platformTxn)); | ||
} | ||
} |