use of com.hedera.services.bdd.spec.HapiSpecOperation in project hedera-services by hashgraph.
the class ValidationScenarios method appropriateQuery.
private static HapiSpecOperation appropriateQuery(SysFilesDownScenario sys, long fileNum) {
String fid = String.format("0.0.%d", fileNum);
SysFileSerde<String> serde = SYS_FILE_SERDES.get(fileNum);
String fqn = params.getTargetNetwork() + "-" + serde.preferredFileName();
String loc = "files/" + fqn;
UnaryOperator<byte[]> preCompare = (fileNum == 121 || fileNum == 122) ? ValidationScenarios::asOrdered : bytes -> bytes;
if (SysFilesDownScenario.COMPARE_EVAL_MODE.equals(sys.getEvalMode())) {
String actualLoc = "files/actual-" + fqn;
try {
byte[] expected = serde.toRawFile(readString(Paths.get(loc)));
return getFileContents(fid).payingWith(SCENARIO_PAYER_NAME).saveReadableTo(serde::fromRawFile, actualLoc).hasContents(spec -> expected).afterBytesTransform(preCompare);
} catch (IOException e) {
throw new IllegalStateException("Cannot read comparison file @ '" + loc + "'!", e);
}
} else if (SysFilesDownScenario.SNAPSHOT_EVAL_MODE.equals(sys.getEvalMode())) {
return getFileContents(fid).payingWith(SCENARIO_PAYER_NAME).saveReadableTo(serde::fromRawFile, loc);
} else {
throw new IllegalArgumentException("No such sys files eval mode '" + sys.getEvalMode() + "'!");
}
}
use of com.hedera.services.bdd.spec.HapiSpecOperation in project hedera-services by hashgraph.
the class SysFilesUpdate method updateTargetFromHumanReadable.
private HapiApiSpec updateTargetFromHumanReadable() {
byte[] bytes = new byte[0];
String nodesOverride = envNodes();
System.out.println("Targeting " + nodesOverride);
String loc = humanReadableFileNames.get(target);
String readableFile = qualifiedPath(nodesOverride, loc, false);
try {
if (BOOK_FILES.contains(target)) {
AddressBookPojo pojoBook = mapper.readValue(new File(readableFile), AddressBookPojo.class);
var addressBook = NodeAddressBook.newBuilder();
pojoBook.getEntries().stream().flatMap(updateConverters.get(target)).forEach(addressBook::addNodeAddress);
bytes = addressBook.build().toByteArray();
} else if (target == SystemFile.EXCHANGE_RATES) {
var pojoRates = mapper.readValue(new File(readableFile), ExchangeRatesPojo.class);
bytes = pojoRates.toProto().toByteArray();
} else if (target == SystemFile.FEE_SCHEDULE) {
final var literal = java.nio.file.Files.readString(Paths.get(readableFile));
bytes = FeesJsonToProtoSerde.parseFeeScheduleFromJson(literal).toByteArray();
} else {
var jutilConfig = new Properties();
jutilConfig.load(java.nio.file.Files.newInputStream(Paths.get(readableFile)));
ServicesConfigurationList.Builder protoConfig = ServicesConfigurationList.newBuilder();
jutilConfig.stringPropertyNames().stream().sorted(JutilPropsToSvcCfgBytes.LEGACY_THROTTLES_FIRST_ORDER).forEach(prop -> protoConfig.addNameValue(Setting.newBuilder().setName(prop).setValue(jutilConfig.getProperty(prop))));
bytes = protoConfig.build().toByteArray();
}
} catch (Exception e) {
System.out.println(String.format("File '%s' should contain a human-readable %s representation!", readableFile, target.toString()));
e.printStackTrace();
System.exit(1);
}
final byte[] toUpload = bytes;
return customHapiSpec(String.format("Update-%s-FromHumanReadable", target)).withProperties(Map.of("nodes", nodesOverride, "default.payer", defaultPayerOverride, "startupAccounts.path", startupAccountsPathOverride)).given(withOpContext((spec, opLog) -> {
var sysFileOcKeystore = SpecUtils.asOcKeystore(new File(DEFAULT_SYSFILE_PEM_LOC), KeyFactory.PEM_PASSPHRASE);
var sysFileKey = Key.newBuilder().setEd25519(ByteString.copyFrom(com.swirlds.common.CommonUtils.unhex(sysFileOcKeystore.getPublicKeyAbyteStr()))).build();
opLog.info("Will ensure default public key :: " + sysFileKey + " (hex = " + sysFileOcKeystore.getPublicKeyAbyteStr() + ")");
spec.registry().saveKey(DEFAULT_SYSFILE_KEY, sysFileKey);
spec.keys().incorporate(DEFAULT_SYSFILE_KEY, sysFileOcKeystore);
}), keyFromPem(DEFAULT_SYSFILE_PEM_LOC).name("insurance").simpleWacl().passphrase(KeyFactory.PEM_PASSPHRASE)).when().then(withOpContext((spec, opLog) -> {
if (toUpload.length < (6 * 1024)) {
var singleOp = fileUpdate(registryNames.get(target)).payingWith(DEFAULT_PAYER).fee(feeToOffer()).contents(toUpload).signedBy(DEFAULT_PAYER, DEFAULT_SYSFILE_KEY);
CustomSpecAssert.allRunFor(spec, singleOp);
} else {
int n = 0;
while (n < toUpload.length) {
int thisChunkSize = Math.min(toUpload.length - n, 4096);
byte[] thisChunk = Arrays.copyOfRange(toUpload, n, n + thisChunkSize);
HapiSpecOperation subOp;
if (n == 0) {
subOp = fileUpdate(registryNames.get(target)).fee(feeToOffer()).wacl("insurance").contents(thisChunk).signedBy(DEFAULT_PAYER, DEFAULT_SYSFILE_KEY).hasKnownStatusFrom(SUCCESS, FEE_SCHEDULE_FILE_PART_UPLOADED);
} else {
subOp = fileAppend(registryNames.get(target)).fee(feeToOffer()).content(thisChunk).signedBy(DEFAULT_PAYER, DEFAULT_SYSFILE_KEY).hasKnownStatusFrom(SUCCESS, FEE_SCHEDULE_FILE_PART_UPLOADED);
}
CustomSpecAssert.allRunFor(spec, subOp);
n += thisChunkSize;
}
}
}));
}
Aggregations