Skip to content

Commit

Permalink
added PlatformTxnRecord
Browse files Browse the repository at this point in the history
Signed-off-by: abhishek-hedera <[email protected]>
  • Loading branch information
abhishek-hedera committed May 27, 2021
1 parent 915dacc commit 2596ae3
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import com.swirlds.common.merkle.MerkleNode;
import com.swirlds.common.merkle.utility.AbstractNaryMerkleInternal;
import com.swirlds.fcmap.FCMap;
import com.typesafe.config.ConfigException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* ‍
*/

import com.google.protobuf.InvalidProtocolBufferException;
import com.hedera.services.context.ServicesContext;
import com.hedera.services.legacy.crypto.SignatureStatus;
import com.hedera.services.sigs.sourcing.ScopedSigBytesProvider;
Expand Down
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;
}
}
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));
}
}

0 comments on commit 2596ae3

Please sign in to comment.