Refactor ./src/enums (#73)

* rewrote enums in TS
* changed folder name to src/definitions
This commit is contained in:
Nathan Nichols
2020-06-30 14:29:29 -07:00
parent ede5b254e3
commit cd0eb2b623
14 changed files with 189 additions and 177 deletions

View File

@@ -1,49 +0,0 @@
# Definitions
## Types
TODO
## Ledger Entry Types
TODO
## Fields
These are Serialization Fields (`sf`) [defined in rippled's SField.cpp](https://github.com/ripple/rippled/blob/develop/src/ripple/protocol/impl/SField.cpp).
### Key
The key is the string defined in the rippled source code, such as "LedgerEntry", "Transaction", etc.
### nth
nth is the `index` used to make an [`SField` field code](https://github.com/ripple/rippled/blob/eaff9a0e6aec0ad077f118501791c7684debcfd5/src/ripple/protocol/SField.h#L95-L98).
### isVLEncoded
If true, the field is Variable Length encoded. The variable-length encoded fields are `STI_VL`/`Blob`, `STI_ACCOUNT`/`AccountID`, and `STI_VECTOR256`/`Vector256`.
### isSerialized
Fields are serialized if they are not [one of these](https://github.com/ripple/rippled/blob/eaff9a0e6aec0ad077f118501791c7684debcfd5/src/ripple/protocol/impl/SField.cpp#L71-L78) or if they are not an SField.
- https://github.com/ripple/ripple-binary-codec/blob/14e76e68ead7e4bcd83c942dbdc9064d5a66869b/src/enums/definitions.json#L832
- https://github.com/ripple/rippled/search?utf8=%E2%9C%93&q=taker_gets_funded&type=
### isSigningField
True unless the field is [specified with `SField::notSigning`](https://github.com/ripple/rippled/blob/eaff9a0e6aec0ad077f118501791c7684debcfd5/src/ripple/protocol/impl/SField.cpp#L198).
## Transaction Results
See:
- https://github.com/ripple/rippled/blob/develop/src/ripple/protocol/TER.h
- https://xrpl.org/transaction-results.html
TODO: Write a script to read rippled's source file and generate the necessary mapping.
## Transaction Types
See https://github.com/ripple/rippled/blob/develop/src/ripple/protocol/TxFormats.h

File diff suppressed because it is too large Load Diff

View File

@@ -1,138 +0,0 @@
import { makeClass } from "./../utils/make-class";
const assert = require("assert");
const _ = require("lodash");
const { parseBytes, serializeUIntN } = require("./../utils/bytes-utils");
const enums = require("./definitions.json");
function transformWith(func, obj) {
return _.transform(obj, func);
}
function biMap(obj, valueKey) {
return _.transform(obj, (result, value, key) => {
result[key] = value;
result[value[valueKey]] = value;
});
}
const EnumType = makeClass(
{
EnumType(definition) {
_.assign(this, definition);
// At minimum
assert(this.bytes instanceof Uint8Array);
assert(typeof this.ordinal === "number");
assert(typeof this.name === "string");
},
toString() {
return this.name;
},
toJSON() {
return this.name;
},
toBytesSink(sink) {
sink.put(this.bytes);
},
statics: {
ordinalByteWidth: 1,
fromParser(parser) {
return this.from(parser.readUIntN(this.ordinalByteWidth));
},
from(val) {
const ret = val instanceof this ? val : this[val];
if (!ret) {
throw new Error(
`${val} is not a valid name or ordinal for ${this.enumName}`
);
}
return ret;
},
valuesByName() {
return _.transform(this.initVals, (result, ordinal, name) => {
const bytes = serializeUIntN(ordinal, this.ordinalByteWidth);
const type = new this({ name, ordinal, bytes });
result[name] = type;
});
},
init() {
const mapped = this.valuesByName();
_.assign(this, biMap(mapped, "ordinal"));
this.values = _.values(mapped);
return this;
},
},
},
undefined
);
function makeEnum(name, definition) {
return makeClass(
{
inherits: EnumType,
statics: _.assign(definition, { enumName: name }),
},
undefined
);
}
function makeEnums(to, definition, name) {
to[name] = makeEnum(name, definition);
}
const Enums = transformWith(makeEnums, {
Type: {
initVals: enums.TYPES,
},
LedgerEntryType: {
initVals: enums.LEDGER_ENTRY_TYPES,
ordinalByteWidth: 2,
},
TransactionType: {
initVals: enums.TRANSACTION_TYPES,
ordinalByteWidth: 2,
},
TransactionResult: {
initVals: enums.TRANSACTION_RESULTS,
ordinalByteWidth: 1,
},
});
Enums.Field = makeClass(
{
inherits: EnumType,
statics: {
enumName: "Field",
initVals: enums.FIELDS,
valuesByName() {
const fields = _.map(this.initVals, ([name, definition]) => {
const type = Enums.Type[definition.type];
const bytes = this.header(type.ordinal, definition.nth);
const ordinal = (type.ordinal << 16) | definition.nth;
const extra = { ordinal, name, type, bytes };
return new this(_.assign(definition, extra));
});
return _.keyBy(fields, "name");
},
header(type, nth) {
const name = nth;
const header = <any>[];
const push = header.push.bind(header);
if (type < 16) {
if (name < 16) {
push((type << 4) | name);
} else {
push(type << 4, name);
}
} else if (name < 16) {
push(name, type);
} else {
push(0, type, name);
}
return parseBytes(header, Uint8Array);
},
},
},
undefined
);
export { Enums };

View File

@@ -1,134 +0,0 @@
/**
* Quick script to re-number values
*/
const input = {
temBAD_SEND_XRP_PATHS: -283,
temBAD_SEQUENCE: -282,
temBAD_SIGNATURE: -281,
temBAD_SRC_ACCOUNT: -280,
temBAD_TRANSFER_RATE: -279,
temDST_IS_SRC: -278,
temDST_NEEDED: -277,
temINVALID: -276,
temINVALID_FLAG: -275,
temREDUNDANT: -274,
temRIPPLE_EMPTY: -273,
temDISABLED: -272,
temBAD_SIGNER: -271,
temBAD_QUORUM: -270,
temBAD_WEIGHT: -269,
temBAD_TICK_SIZE: -268,
temINVALID_ACCOUNT_ID: -267,
temCANNOT_PREAUTH_SELF: -266,
temUNCERTAIN: -265,
temUNKNOWN: -264,
tefFAILURE: -199,
tefALREADY: -198,
tefBAD_ADD_AUTH: -197,
tefBAD_AUTH: -196,
tefBAD_LEDGER: -195,
tefCREATED: -194,
tefEXCEPTION: -193,
tefINTERNAL: -192,
tefNO_AUTH_REQUIRED: -191,
tefPAST_SEQ: -190,
tefWRONG_PRIOR: -189,
tefMASTER_DISABLED: -188,
tefMAX_LEDGER: -187,
tefBAD_SIGNATURE: -186,
tefBAD_QUORUM: -185,
tefNOT_MULTI_SIGNING: -184,
tefBAD_AUTH_MASTER: -183,
tefINVARIANT_FAILED: -182,
tefTOO_BIG: -181,
terRETRY: -99,
terFUNDS_SPENT: -98,
terINSUF_FEE_B: -97,
terNO_ACCOUNT: -96,
terNO_AUTH: -95,
terNO_LINE: -94,
terOWNERS: -93,
terPRE_SEQ: -92,
terLAST: -91,
terNO_RIPPLE: -90,
terQUEUED: -89,
tesSUCCESS: 0,
tecCLAIM: 100,
tecPATH_PARTIAL: 101,
tecUNFUNDED_ADD: 102,
tecUNFUNDED_OFFER: 103,
tecUNFUNDED_PAYMENT: 104,
tecFAILED_PROCESSING: 105,
tecDIR_FULL: 121,
tecINSUF_RESERVE_LINE: 122,
tecINSUF_RESERVE_OFFER: 123,
tecNO_DST: 124,
tecNO_DST_INSUF_XRP: 125,
tecNO_LINE_INSUF_RESERVE: 126,
tecNO_LINE_REDUNDANT: 127,
tecPATH_DRY: 128,
tecUNFUNDED: 129,
tecNO_ALTERNATIVE_KEY: 130,
tecNO_REGULAR_KEY: 131,
tecOWNERS: 132,
tecNO_ISSUER: 133,
tecNO_AUTH: 134,
tecNO_LINE: 135,
tecINSUFF_FEE: 136,
tecFROZEN: 137,
tecNO_TARGET: 138,
tecNO_PERMISSION: 139,
tecNO_ENTRY: 140,
tecINSUFFICIENT_RESERVE: 141,
tecNEED_MASTER_KEY: 142,
tecDST_TAG_NEEDED: 143,
tecINTERNAL: 144,
tecOVERSIZE: 145,
tecCRYPTOCONDITION_ERROR: 146,
tecINVARIANT_FAILED: 147,
tecEXPIRED: 148,
tecDUPLICATE: 149,
tecKILLED: 150,
tecHAS_OBLIGATIONS: 151,
tecTOO_SOON: 152,
};
let startingFromTemBADSENDXRPPATHS = -284;
let startingFromTefFAILURE = -199;
let startingFromTerRETRY = -99;
const tesSUCCESS = 0;
let startingFromTecCLAIM = 100;
const startingFromTecDIRFULL = 121;
let previousKey = "tem";
Object.keys(input).forEach((key) => {
if (key.substring(0, 3) !== previousKey.substring(0, 3)) {
console.log();
previousKey = key;
}
if (key.substring(0, 3) === "tem") {
console.log(` "${key}": ${startingFromTemBADSENDXRPPATHS++},`);
} else if (key.substring(0, 3) === "tef") {
console.log(` "${key}": ${startingFromTefFAILURE++},`);
} else if (key.substring(0, 3) === "ter") {
console.log(` "${key}": ${startingFromTerRETRY++},`);
} else if (key.substring(0, 3) === "tes") {
console.log(` "${key}": ${tesSUCCESS},`);
} else if (key.substring(0, 3) === "tec") {
if (key === "tecDIR_FULL") {
startingFromTecCLAIM = startingFromTecDIRFULL;
}
console.log(` "${key}": ${startingFromTecCLAIM++},`);
}
});