Skip to content

Commit

Permalink
Fixed out-of-bounds difficulty in getBlock, which can affect PoA netw…
Browse files Browse the repository at this point in the history
…orks (#711).
  • Loading branch information
ricmoo committed Jan 21, 2020
1 parent 1ff5f52 commit 251882c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 20 deletions.
40 changes: 21 additions & 19 deletions packages/providers/src.ts/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ export class Formatter {
}

getDefaultFormats(): Formats {
let formats: Formats = <Formats>({ });
const formats: Formats = <Formats>({ });

let address = this.address.bind(this);
let bigNumber = this.bigNumber.bind(this);
let blockTag = this.blockTag.bind(this);
let data = this.data.bind(this);
let hash = this.hash.bind(this);
let hex = this.hex.bind(this);
let number = this.number.bind(this);
const address = this.address.bind(this);
const bigNumber = this.bigNumber.bind(this);
const blockTag = this.blockTag.bind(this);
const data = this.data.bind(this);
const hash = this.hash.bind(this);
const hex = this.hex.bind(this);
const number = this.number.bind(this);

let strictData = (v: any) => { return this.data(v, true); };
const strictData = (v: any) => { return this.data(v, true); };

formats.transaction = {
hash: hash,
Expand Down Expand Up @@ -195,7 +195,7 @@ export class Formatter {
}

data(value: any, strict?: boolean): string {
let result = this.hex(value, strict);
const result = this.hex(value, strict);
if ((result.length % 2) !== 0) {
throw new Error("invalid data; odd-length - " + value);
}
Expand All @@ -210,7 +210,7 @@ export class Formatter {

callAddress(value: any): string {
if (!isHexString(value, 32)) { return null; }
let address = getAddress(hexDataSlice(value, 12));
const address = getAddress(hexDataSlice(value, 12));
return (address === AddressZero) ? null: address;
}

Expand All @@ -237,7 +237,7 @@ export class Formatter {

// Requires a hash, optionally requires 0x prefix; returns prefixed lowercase hash.
hash(value: any, strict?: boolean): string {
let result = this.hex(value, strict);
const result = this.hex(value, strict);
if (hexDataLength(result) !== 32) {
return logger.throwArgumentError("invalid hash", "value", value);
}
Expand All @@ -246,7 +246,9 @@ export class Formatter {

// Returns the difficulty as a number, or if too large (i.e. PoA network) null
difficulty(value: any): number {
let v = BigNumber.from(value);
if (value == null) { return null; }

const v = BigNumber.from(value);

try {
return v.toNumber();
Expand Down Expand Up @@ -328,7 +330,7 @@ export class Formatter {
}
*/

let result: TransactionResponse = Formatter.check(this.formats.transaction, transaction);
const result: TransactionResponse = Formatter.check(this.formats.transaction, transaction);

if (transaction.chainId != null) {
let chainId = transaction.chainId;
Expand Down Expand Up @@ -382,7 +384,7 @@ export class Formatter {
//let status = transactionReceipt.status;
//let root = transactionReceipt.root;

let result: TransactionReceipt = Formatter.check(this.formats.receipt, value);
const result: TransactionReceipt = Formatter.check(this.formats.receipt, value);
result.logs.forEach((entry, index) => {
if (entry.transactionLogIndex == null) {
entry.transactionLogIndex = index;
Expand Down Expand Up @@ -414,10 +416,10 @@ export class Formatter {
}

static check(format: { [ name: string ]: FormatFunc }, object: any): any {
let result: any = {};
for (let key in format) {
const result: any = {};
for (const key in format) {
try {
let value = format[key](object[key]);
const value = format[key](object[key]);
if (value !== undefined) { result[key] = value; }
} catch (error) {
error.checkKey = key;
Expand Down Expand Up @@ -449,7 +451,7 @@ export class Formatter {
return (function(array: any): Array<any> {
if (!Array.isArray(array)) { throw new Error("not an array"); }

let result: any = [];
const result: any = [];

array.forEach(function(value) {
result.push(format(value));
Expand Down
37 changes: 36 additions & 1 deletion packages/tests/src.ts/test-providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const blockchainData: any = {
balance: bnify("997787946734641021")
},
block3: {
hash: "0xf0ec9bf41b99a6bd1f6cd29f91302f71a1a82d14634d2e207edea4b7962f3676",
hash: "0xf0ec9bf41b99a6bd1f6cd29f91302f71a1a82d14634d2e207edea4b7962f3676",
parentHash: "0xf110ecd84454f116e2222378e7bca81ac3e59be0dac96d7ec56d5ef1c3bc1d64",
number: 3,
timestamp: 1488459452,
Expand All @@ -131,6 +131,25 @@ const blockchainData: any = {
extraData: "0xd5830105048650617269747986312e31352e31826c69",
transactions: []
},
// See: #711
block16265864: {
hash: "0xd92891a6eeaed4892289edf9bd5ebff261da5c6a51f7131cc1a481c6f4d1aa75",
parentHash: "0xcc769a02513be1df80eee7d3a5cb87f14f37baee03c13f3e3ad1e7bdcaf7dac3",
number: 16265864,
timestamp: 1579621004,
difficulty: null,
gasLimit: bnify("0x989680"),
gasUsed: bnify("0x0705bf"),
miner: "0x596e8221A30bFe6e7eFF67Fee664A01C73BA3C56",
extraData: "0xde830206088f5061726974792d457468657265756d86312e34302e30826c69",
transactions: [
"0x20e6760fa1297fb06c8c20e6ed99581e0ba964d51167ea3c8ff580bfcb10bfc3",
"0x0ce7eba48b1bbdee05823b79ae24e741f3f290d0abfef8ae9adf32db108b7dd6",
"0x1fa2baafa844bf4853e4abbbf49532bf570210d589dc626dbf7ebc4832bdfa5d",
"0xdb5d1fa54d30a4b6aee0b242a2c68ea52d3dd28703f69e6e30871827850aa2fa",
"0xcc898db85d7d2493d4778faf640be32a4a3b7f5f987257bdc0009ce75a18eeaa"
]
},
},
rinkeby: {
balance: {
Expand Down Expand Up @@ -327,6 +346,22 @@ function testProvider(providerName: string, networkName: string) {
});
});

// Kovan Test Case: See #711
if (blockchainData[networkName].block16265864) {
it("fetches block #16265864", function() {
this.timeout(20000);
let test = blockchainData[networkName].block16265864;
return provider.getBlock(16265864).then((block) => {
for (let key in test) {
equals("Block " + key, (<any>block)[key], test[key]);
}
}, (error) => {
console.log("Error", error);
assert.ok(false);
});
});
}

it("fetches address balance", function() {
// @TODO: These tests could be fiddled with if someone sends ether to our address
// We should set up a contract on each network like:
Expand Down

0 comments on commit 251882c

Please sign in to comment.