fix: remove legacy code

This commit is contained in:
Tyera Eulberg 2020-01-08 13:59:58 -07:00 committed by Michael Vines
parent aea0e83a40
commit 4a547b0057
6 changed files with 189 additions and 237 deletions

View File

@ -30,17 +30,12 @@ type RpcResponseAndContext<T> = {
* @private
*/
function jsonRpcResultAndContext(resultDescription: any) {
return struct.union([
// those same methods return results with context in v0.21+ servers
jsonRpcResult({
context: struct({
slot: 'number',
}),
value: resultDescription,
return jsonRpcResult({
context: struct({
slot: 'number',
}),
// selected methods return "bare" results in pre-v0.21 servers
jsonRpcResult(resultDescription),
]);
value: resultDescription,
});
}
/**
@ -375,19 +370,6 @@ const GetClusterNodes = jsonRpcResult(
}),
]),
);
/**
* @ignore
*/
const GetClusterNodes_015 = jsonRpcResult(
struct.list([
struct({
id: 'string',
gossip: 'string',
tpu: struct.union(['null', 'string']),
rpc: struct.union(['null', 'string']),
}),
]),
);
/**
* Expected JSON RPC response for the "getVoteAccounts" message
@ -515,20 +497,6 @@ const GetRecentBlockhashAndContextRpcResult = jsonRpcResultAndContext([
}),
]);
/**
* @ignore
*/
const GetRecentBlockhash_016 = jsonRpcResult([
'string',
struct({
lamportsPerSignature: 'number',
maxLamportsPerSignature: 'number',
minLamportsPerSignature: 'number',
targetLamportsPerSignature: 'number',
targetSignaturesPerSlot: 'number',
}),
]);
/**
* Expected JSON RPC response for the "requestAirdrop" message
*/
@ -722,21 +690,7 @@ export class Connection {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
const isV021 =
typeof res.result.context !== 'undefined' &&
typeof res.result.value !== 'undefined';
if (isV021) {
return res.result;
} else {
return {
context: {
slot: NaN,
},
value: res.result,
};
}
return res.result;
}
async getBalance(
publicKey: PublicKey,
@ -764,18 +718,11 @@ export class Connection {
}
assert(typeof res.result !== 'undefined');
const isV021 =
typeof res.result.context !== 'undefined' &&
typeof res.result.value !== 'undefined';
const slot = isV021 ? res.result.context.slot : NaN;
const resultValue = isV021 ? res.result.value : res.result;
if (!resultValue) {
if (!res.result.value) {
throw new Error('Invalid request');
}
const {executable, owner, lamports, data} = resultValue;
const {executable, owner, lamports, data} = res.result.value;
const value = {
executable,
owner: new PublicKey(owner),
@ -785,7 +732,7 @@ export class Connection {
return {
context: {
slot,
slot: res.result.context.slot,
},
value,
};
@ -845,21 +792,7 @@ export class Connection {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
const isV021 =
typeof res.result.context !== 'undefined' &&
typeof res.result.value !== 'undefined';
if (isV021) {
return res.result;
} else {
return {
context: {
slot: NaN,
},
value: res.result,
};
}
return res.result;
}
async confirmTransaction(
signature: TransactionSignature,
@ -878,23 +811,6 @@ export class Connection {
async getClusterNodes(): Promise<Array<ContactInfo>> {
const unsafeRes = await this._rpcRequest('getClusterNodes', []);
// Legacy v0.15 response. TODO: Remove in August 2019
try {
const res_015 = GetClusterNodes_015(unsafeRes);
if (res_015.error) {
console.log('no', res_015.error);
throw new Error(res_015.error.message);
}
return res_015.result.map(node => {
node.pubkey = node.id;
node.id = undefined;
return node;
});
} catch (e) {
// Not legacy format
}
// End Legacy v0.15 response
const res = GetClusterNodes(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
@ -1063,46 +979,12 @@ export class Connection {
const args = this._argsWithCommitment([], commitment);
const unsafeRes = await this._rpcRequest('getRecentBlockhash', args);
// Legacy v0.16 response. TODO: Remove in September 2019
try {
const res_016 = GetRecentBlockhash_016(unsafeRes);
if (res_016.error) {
throw new Error(res_016.error.message);
}
const [blockhash, feeCalculator] = res_016.result;
feeCalculator.burnPercent = 0;
return {
context: {
slot: NaN,
},
value: [blockhash, feeCalculator],
};
} catch (e) {
// Not legacy format
}
// End Legacy v0.16 response
const res = GetRecentBlockhashAndContextRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
const isV021 =
typeof res.result.context !== 'undefined' &&
typeof res.result.value !== 'undefined';
if (isV021) {
return res.result;
} else {
return {
context: {
slot: NaN,
},
value: res.result,
};
}
return res.result;
}
async getRecentBlockhash(
commitment: ?Commitment,
@ -1167,23 +1049,17 @@ export class Connection {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
const isV021 =
typeof res.result.context !== 'undefined' &&
typeof res.result.value !== 'undefined';
const slot = isV021 ? res.result.context.slot : NaN;
const resultValue = isV021 ? res.result.value : res.result;
if (!resultValue) {
if (!res.result.value) {
throw new Error('Invalid request');
}
const value = NonceAccount.fromAccountData(Buffer.from(resultValue.data));
const value = NonceAccount.fromAccountData(
Buffer.from(res.result.value.data),
);
return {
context: {
slot,
slot: res.result.context.slot,
},
value,
};

View File

@ -217,7 +217,8 @@ export class Transaction {
allKeys.sort(function(x, y) {
const checkSigner = x.isSigner === y.isSigner ? 0 : x.isSigner ? -1 : 1;
const checkWritable = x.isWritable === y.isWritable ? 0 : x.isWritable ? -1 : 1;
const checkWritable =
x.isWritable === y.isWritable ? 0 : x.isWritable ? -1 : 1;
return checkSigner || checkWritable;
});

View File

@ -123,7 +123,12 @@ test('get balance', async () => {
},
{
error: null,
result: 0,
result: {
context: {
slot: 11,
},
value: 0,
},
},
]);
@ -518,7 +523,12 @@ test('request airdrop', async () => {
},
{
error: null,
result: 42,
result: {
context: {
slot: 11,
},
value: 42,
},
},
]);
@ -537,43 +547,48 @@ test('request airdrop', async () => {
{
error: null,
result: {
owner: [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
lamports: 42,
data: [],
executable: false,
context: {
slot: 11,
},
value: {
owner: [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
lamports: 42,
data: [],
executable: false,
},
},
},
]);
@ -609,7 +624,12 @@ test('request airdrop - max commitment', async () => {
},
{
error: null,
result: 40,
result: {
context: {
slot: 11,
},
value: 40,
},
},
]);
@ -647,7 +667,12 @@ test('transaction', async () => {
},
{
error: null,
result: 100010,
result: {
context: {
slot: 11,
},
value: 100010,
},
},
]);
await connection.requestAirdrop(accountFrom.publicKey, 100010);
@ -673,7 +698,12 @@ test('transaction', async () => {
},
{
error: null,
result: 21,
result: {
context: {
slot: 11,
},
value: 21,
},
},
]);
await connection.requestAirdrop(accountTo.publicKey, 21);
@ -710,7 +740,12 @@ test('transaction', async () => {
},
{
error: null,
result: true,
result: {
context: {
slot: 11,
},
value: true,
},
},
]);
@ -751,7 +786,12 @@ test('transaction', async () => {
},
{
error: null,
result: 2,
result: {
context: {
slot: 11,
},
value: 2,
},
},
]);
@ -768,7 +808,12 @@ test('transaction', async () => {
},
{
error: null,
result: 31,
result: {
context: {
slot: 11,
},
value: 31,
},
},
]);
expect(await connection.getBalance(accountTo.publicKey)).toBe(31);

View File

@ -20,17 +20,22 @@ export function mockGetRecentBlockhash(commitment: ?Commitment) {
},
{
error: null,
result: [
recentBlockhash.publicKey.toBase58(),
{
lamportsPerSignature: 42,
burnPercent: 50,
maxLamportsPerSignature: 42,
minLamportsPerSignature: 42,
targetLamportsPerSignature: 42,
targetSignaturesPerSlot: 42,
result: {
context: {
slot: 11,
},
],
value: [
recentBlockhash.publicKey.toBase58(),
{
lamportsPerSignature: 42,
burnPercent: 50,
maxLamportsPerSignature: 42,
minLamportsPerSignature: 42,
targetLamportsPerSignature: 42,
targetSignaturesPerSlot: 42,
},
],
},
},
]);
}

View File

@ -61,7 +61,12 @@ test('create and query nonce account', async () => {
},
{
error: null,
result: minimumAmount * 2,
result: {
context: {
slot: 11,
},
value: minimumAmount * 2,
},
},
]);
@ -104,43 +109,48 @@ test('create and query nonce account', async () => {
{
error: null,
result: {
owner: [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
lamports: minimumAmount,
data: [...expectedData],
executable: false,
context: {
slot: 11,
},
value: {
owner: [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
lamports: minimumAmount,
data: [...expectedData],
executable: false,
},
},
},
]);

View File

@ -98,7 +98,12 @@ test('transaction-payer', async () => {
},
{
error: null,
result: true,
result: {
context: {
slot: 11,
},
value: true,
},
},
]);
@ -139,7 +144,12 @@ test('transaction-payer', async () => {
},
{
error: null,
result: 99,
result: {
context: {
slot: 11,
},
value: 99,
},
},
]);
@ -158,7 +168,12 @@ test('transaction-payer', async () => {
},
{
error: null,
result: 2,
result: {
context: {
slot: 11,
},
value: 2,
},
},
]);
expect(await connection.getBalance(accountFrom.publicKey)).toBe(2);