|
18 | 18 |
|
19 | 19 | import static com.hedera.services.bdd.junit.TestTags.SMART_CONTRACT;
|
20 | 20 | import static com.hedera.services.bdd.spec.HapiSpec.defaultHapiSpec;
|
| 21 | +import static com.hedera.services.bdd.spec.assertions.AccountInfoAsserts.approxChangeFromSnapshot; |
| 22 | +import static com.hedera.services.bdd.spec.queries.QueryVerbs.getAccountBalance; |
21 | 23 | import static com.hedera.services.bdd.spec.queries.QueryVerbs.getContractBytecode;
|
22 | 24 | import static com.hedera.services.bdd.spec.transactions.TxnVerbs.contractCreate;
|
| 25 | +import static com.hedera.services.bdd.spec.transactions.TxnVerbs.cryptoCreate; |
23 | 26 | import static com.hedera.services.bdd.spec.transactions.TxnVerbs.uploadInitCode;
|
24 | 27 | import static com.hedera.services.bdd.spec.utilops.CustomSpecAssert.allRunFor;
|
| 28 | +import static com.hedera.services.bdd.spec.utilops.UtilVerbs.balanceSnapshot; |
| 29 | +import static com.hedera.services.bdd.spec.utilops.UtilVerbs.sleepFor; |
| 30 | +import static com.hedera.services.bdd.spec.utilops.UtilVerbs.sourcing; |
25 | 31 | import static com.hedera.services.bdd.spec.utilops.UtilVerbs.withOpContext;
|
26 | 32 | import static com.hedera.services.bdd.suites.contract.Utils.getResourcePath;
|
27 | 33 |
|
|
35 | 41 | import java.io.File;
|
36 | 42 | import java.util.Arrays;
|
37 | 43 | import java.util.List;
|
| 44 | +import java.util.concurrent.atomic.AtomicLong; |
38 | 45 | import org.apache.logging.log4j.LogManager;
|
39 | 46 | import org.apache.logging.log4j.Logger;
|
40 | 47 | import org.bouncycastle.util.encoders.Hex;
|
@@ -71,21 +78,47 @@ public boolean canRunConcurrent() {
|
71 | 78 | @HapiTest
|
72 | 79 | final HapiSpec getByteCodeWorks() {
|
73 | 80 | final var contract = "EmptyConstructor";
|
| 81 | + final var canonicalUsdFee = 0.05; |
| 82 | + final var canonicalQueryFeeAtActiveRate = new AtomicLong(); |
74 | 83 | return HapiSpec.defaultHapiSpec("GetByteCodeWorks")
|
75 |
| - .given(uploadInitCode(contract), contractCreate(contract)) |
76 |
| - .when() |
77 |
| - .then(withOpContext((spec, opLog) -> { |
78 |
| - final var getBytecode = getContractBytecode(contract).saveResultTo("contractByteCode"); |
79 |
| - allRunFor(spec, getBytecode); |
| 84 | + .given( |
| 85 | + cryptoCreate(CIVILIAN_PAYER).balance(ONE_HUNDRED_HBARS), |
| 86 | + uploadInitCode(contract), |
| 87 | + contractCreate(contract)) |
| 88 | + .when(balanceSnapshot("beforeQuery", CIVILIAN_PAYER)) |
| 89 | + .then( |
| 90 | + withOpContext((spec, opLog) -> { |
| 91 | + final var getBytecode = getContractBytecode(contract) |
| 92 | + .payingWith(CIVILIAN_PAYER) |
| 93 | + .saveResultTo("contractByteCode") |
| 94 | + .exposingBytecodeTo(bytes -> { |
| 95 | + canonicalQueryFeeAtActiveRate.set( |
| 96 | + spec.ratesProvider().toTbWithActiveRates((long) |
| 97 | + (canonicalUsdFee * 100 * TINY_PARTS_PER_WHOLE))); |
| 98 | + log.info( |
| 99 | + "Canoncal tinybar cost at active rate: {}", |
| 100 | + canonicalQueryFeeAtActiveRate.get()); |
| 101 | + }); |
| 102 | + allRunFor(spec, getBytecode); |
80 | 103 |
|
81 |
| - @SuppressWarnings("UnstableApiUsage") |
82 |
| - final var originalBytecode = |
83 |
| - Hex.decode(Files.toByteArray(new File(getResourcePath(contract, ".bin")))); |
84 |
| - final var actualBytecode = spec.registry().getBytes("contractByteCode"); |
85 |
| - // The original bytecode is modified on deployment |
86 |
| - final var expectedBytecode = Arrays.copyOfRange(originalBytecode, 29, originalBytecode.length); |
87 |
| - Assertions.assertArrayEquals(expectedBytecode, actualBytecode); |
88 |
| - })); |
| 104 | + @SuppressWarnings("UnstableApiUsage") |
| 105 | + final var originalBytecode = |
| 106 | + Hex.decode(Files.toByteArray(new File(getResourcePath(contract, ".bin")))); |
| 107 | + final var actualBytecode = spec.registry().getBytes("contractByteCode"); |
| 108 | + // The original bytecode is modified on deployment |
| 109 | + final var expectedBytecode = |
| 110 | + Arrays.copyOfRange(originalBytecode, 29, originalBytecode.length); |
| 111 | + Assertions.assertArrayEquals(expectedBytecode, actualBytecode); |
| 112 | + }), |
| 113 | + // Wait for the query payment transaction to be handled |
| 114 | + sleepFor(5_000), |
| 115 | + sourcing(() -> getAccountBalance(CIVILIAN_PAYER) |
| 116 | + .hasTinyBars( |
| 117 | + // Just sanity-check a fee within 50% of the canonical fee to be safe |
| 118 | + approxChangeFromSnapshot( |
| 119 | + "beforeQuery", |
| 120 | + -canonicalQueryFeeAtActiveRate.get(), |
| 121 | + canonicalQueryFeeAtActiveRate.get() / 2)))); |
89 | 122 | }
|
90 | 123 |
|
91 | 124 | @HapiTest
|
|
0 commit comments