Skip to content

Commit

Permalink
fix(deps): CDB-2128 Address nonce errors
Browse files Browse the repository at this point in the history
Per ethers-io/ethers.js#3812, nonce may be
incorrectly incremented, which can produce off by one errors which
we are currently seeing in QA and Clay
  • Loading branch information
dbcfd committed Apr 13, 2023
1 parent e11e0d1 commit e724438
Show file tree
Hide file tree
Showing 12 changed files with 559 additions and 522 deletions.
363 changes: 195 additions & 168 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@
"@types/lodash.camelcase": "^4.3.7",
"@types/lodash.snakecase": "^4.1.7",
"await-semaphore": "^0.1.3",
"body-parser": "^1.20.2",
"cartonne": "^2.0.1",
"conditional-type-checks": "^1.0.6",
"cors": "^2.8.5",
"dag-jose": "^4.0.0",
"dotenv": "^16.0.3",
"ethers": "~5.7.2",
"ethers": "^6.2.3",
"express": "^4.18.1",
"fp-ts": "^2.13.1",
"http-status-codes": "^2.2.0",
Expand All @@ -75,9 +76,11 @@
"lodash.clonedeep": "^4.5.0",
"lodash.snakecase": "^4.1.1",
"lru-cache": "^7.17.0",
"merge-options": "^3.0.4",
"morgan": "^1.10.0",
"multiformats": "^11.0.1",
"os-utils": "^0.0.14",
"prom-client": "^14.2.0",
"pg": "^8.8.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.5.2",
Expand Down
6 changes: 5 additions & 1 deletion src/__tests__/ceramic_integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ process.env.NODE_ENV = 'test'
const randomNumber = Math.floor(Math.random() * 10000)
const TOPIC = `/ceramic/local/${randomNumber}`

// Workaround from https://stackoverflow.com/a/72416352/599991
import dns from 'node:dns'
dns.setDefaultResultOrder('ipv4first')

/**
* Create an IPFS instance
*/
Expand Down Expand Up @@ -90,7 +94,7 @@ async function makeCeramicCore(
anchorServiceUrl,
// TODO CDB-2317 Remove `indexing` config when Ceramic Core allows that
indexing: {
db: "TODO",
db: 'TODO',
allowQueriesBeforeHistoricalSync: false,
disableComposedb: true,
enableHistoricalSync: false,
Expand Down
73 changes: 38 additions & 35 deletions src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,53 @@ function buildExpressMiddleware() {
* Notice that the absense of a did header or body bypasses any checks below
* this app will still work if the logice above is not in place.
*/
return function(req: Request, _res: Response, next: NextFunction) {
if (req.headers) {
if (req.headers['did'] && req.body) {
if (Object.keys(req.body).length > 0) {
const digest = buildBodyDigest(req.headers['content-type'], req.body)
if (req.headers['digest'] == digest) {
return next()
} else {
throw Error('Body digest verification failed')
}
}
}
return function (req: Request, _res: Response, next: NextFunction) {
if (req.headers) {
if (req.headers['did'] && req.body) {
if (Object.keys(req.body).length > 0) {
const digest = buildBodyDigest(req.headers['content-type'], req.body)
if (req.headers['digest'] == digest) {
return next()
} else {
throw Error('Body digest verification failed')
}
}
return next()
}
}
return next()
}
}

function buildBodyDigest(contentType: string | undefined, body: any): string | undefined {
if (!body) return
if (!body) return

let hash: Uint8Array | undefined
let hash: Uint8Array | undefined

if (contentType) {
if (contentType.includes('application/vnd.ipld.car')) {
const carFactory = new CARFactory()
carFactory.codecs.add(DAG_JOSE)
console.log('Will build a car file from req.body', body)
try {
console.log('Will build a car file from req.body (as utf8 string)', u8a.toString(body, 'base64'))
} catch(e) {
console.log('Couldn\'t convert req.body to string: ', e)
}
const car = carFactory.fromBytes(body)
if (!car.roots[0]) throw Error('Missing CAR root')
return car.roots[0].toString()
} else if (contentType.includes('application/json')) {
hash = sha256.hash(u8a.fromString(JSON.stringify(body)))
if (contentType) {
if (contentType.includes('application/vnd.ipld.car')) {
const carFactory = new CARFactory()
carFactory.codecs.add(DAG_JOSE)
console.log('Will build a car file from req.body', body)
try {
console.log(
'Will build a car file from req.body (as utf8 string)',
u8a.toString(body, 'base64')
)
} catch (e) {
console.log("Couldn't convert req.body to string: ", e)
}
}

if (!hash) {
// Default to hashing stringified body
const car = carFactory.fromBytes(body)
if (!car.roots[0]) throw Error('Missing CAR root')
return car.roots[0].toString()
} else if (contentType.includes('application/json')) {
hash = sha256.hash(u8a.fromString(JSON.stringify(body)))
}
}

return `0x${u8a.toString(hash, 'base16')}`
if (!hash) {
// Default to hashing stringified body
hash = sha256.hash(u8a.fromString(JSON.stringify(body)))
}

return `0x${u8a.toString(hash, 'base16')}`
}
6 changes: 3 additions & 3 deletions src/models/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ export class Transaction {
chain: string
txHash: string
blockNumber: number
blockTimestamp: number
blockTimestamp: Date

constructor(chain: string, txHash: string, blockNumber: number, blockTimestamp: number) {
constructor(chain: string, txHash: string, blockNumber: number, blockDate: Date) {
this.chain = chain
this.txHash = txHash
this.blockNumber = blockNumber
this.blockTimestamp = blockTimestamp
this.blockTimestamp = blockDate
}
}
6 changes: 4 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ export class CeramicAnchorServer extends Server {
super(true)

this.app.set('trust proxy', true)
this.app.use(bodyParser.raw({inflate: true, type: 'application/vnd.ipld.car'}))
this.app.use(bodyParser.raw({ inflate: true, type: 'application/vnd.ipld.car' }))
this.app.use(bodyParser.json({ type: 'application/json' }))
this.app.use(bodyParser.urlencoded({ extended: true, type: 'application/x-www-form-urlencoded' }))
this.app.use(
bodyParser.urlencoded({ extended: true, type: 'application/x-www-form-urlencoded' })
)
this.app.use(expressLoggers)
if (config.requireAuth == true) {
this.app.use(auth)
Expand Down
2 changes: 1 addition & 1 deletion src/services/anchor-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export class AnchorService {
if (this.includeBlockInfoInAnchorProof) {
ipfsAnchorProof = {
blockNumber: tx.blockNumber,
blockTimestamp: tx.blockTimestamp,
blockTimestamp: tx.blockTimestamp.getUTCDate(),
...ipfsAnchorProof,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

exports[`ETH service connected to ganache v0 should send CID to local ganache server 1`] = `
Transaction {
"blockNumber": 3,
"blockTimestamp": 2020-04-13T13:20:08.000Z,
"chain": "eip155:1337",
"txHash": "0xf7f830ef89d55af5900d48db106d5bcfbc045aec70b29cde019d2cd1e58a7827",
"txHash": "0xd6e8a8fab6097270c8a52a85db47ef8c2ba1b9b60db76435f5d8a8a3a1d598be",
}
`;

Expand All @@ -20,18 +22,9 @@ exports[`ETH service with mock wallet insufficient funds error 1`] = `
{
"data": "0x0f017112205d7fcd1a0999befdb062e6762c1f0f902f729b98304a2ef539412f53360d3d6a",
"from": "abcd1234",
"gasLimit": {
"hex": "0x2710",
"type": "BigNumber",
},
"maxFeePerGas": {
"hex": "0x0834",
"type": "BigNumber",
},
"maxPriorityFeePerGas": {
"hex": "0x044c",
"type": "BigNumber",
},
"gasLimit": 10000n,
"maxFeePerGas": 2431n,
"maxPriorityFeePerGas": 1210n,
"nonce": 5,
"to": "abcd1234",
}
Expand All @@ -41,18 +34,9 @@ exports[`ETH service with mock wallet insufficient funds error 2`] = `
{
"data": "0x0f017112205d7fcd1a0999befdb062e6762c1f0f902f729b98304a2ef539412f53360d3d6a",
"from": "abcd1234",
"gasLimit": {
"hex": "0x2710",
"type": "BigNumber",
},
"maxFeePerGas": {
"hex": "0x0834",
"type": "BigNumber",
},
"maxPriorityFeePerGas": {
"hex": "0x044c",
"type": "BigNumber",
},
"gasLimit": 10000n,
"maxFeePerGas": 2431n,
"maxPriorityFeePerGas": 1210n,
"nonce": 5,
"to": "abcd1234",
}
Expand All @@ -70,7 +54,7 @@ exports[`ETH service with mock wallet single transaction attempt 1`] = `
exports[`ETH service with mock wallet single transaction attempt 2`] = `
Transaction {
"blockNumber": 54321,
"blockTimestamp": 54321000,
"blockTimestamp": 2000-02-01T00:00:00.000Z,
"chain": "eip155:1337",
"txHash": "0x12345abcde",
}
Expand All @@ -79,14 +63,8 @@ Transaction {
exports[`ETH service with mock wallet single transaction attempt 3`] = `
{
"data": "0x987654321",
"gasLimit": {
"hex": "0x2710",
"type": "BigNumber",
},
"gasPrice": {
"hex": "0x03e8",
"type": "BigNumber",
},
"gasLimit": 10000n,
"gasPrice": 1000n,
"nonce": 5,
"to": "abcd1234",
}
Expand All @@ -96,18 +74,9 @@ exports[`ETH service with mock wallet successful mocked transaction 1`] = `
{
"data": "0x0f017112205d7fcd1a0999befdb062e6762c1f0f902f729b98304a2ef539412f53360d3d6a",
"from": "abcd1234",
"gasLimit": {
"hex": "0x2710",
"type": "BigNumber",
},
"maxFeePerGas": {
"hex": "0x07d0",
"type": "BigNumber",
},
"maxPriorityFeePerGas": {
"hex": "0x03e8",
"type": "BigNumber",
},
"gasLimit": 10000n,
"maxFeePerGas": 2310n,
"maxPriorityFeePerGas": 1100n,
"nonce": 5,
"to": "abcd1234",
}
Expand All @@ -117,18 +86,9 @@ exports[`setGasPrice EIP1559 transaction 1`] = `
{
"data": "0x0f017112205d7fcd1a0999befdb062e6762c1f0f902f729b98304a2ef539412f53360d3d6a",
"from": "abcd1234",
"gasLimit": {
"hex": "0x0a",
"type": "BigNumber",
},
"maxFeePerGas": {
"hex": "0x07d0",
"type": "BigNumber",
},
"maxPriorityFeePerGas": {
"hex": "0x03e8",
"type": "BigNumber",
},
"gasLimit": 10n,
"maxFeePerGas": 2310n,
"maxPriorityFeePerGas": 1100n,
"nonce": undefined,
"to": "abcd1234",
}
Expand All @@ -138,18 +98,9 @@ exports[`setGasPrice EIP1559 transaction 2`] = `
{
"data": "0x0f017112205d7fcd1a0999befdb062e6762c1f0f902f729b98304a2ef539412f53360d3d6a",
"from": "abcd1234",
"gasLimit": {
"hex": "0x0a",
"type": "BigNumber",
},
"maxFeePerGas": {
"hex": "0x0834",
"type": "BigNumber",
},
"maxPriorityFeePerGas": {
"hex": "0x044c",
"type": "BigNumber",
},
"gasLimit": 10n,
"maxFeePerGas": 2431n,
"maxPriorityFeePerGas": 1210n,
"nonce": undefined,
"to": "abcd1234",
}
Expand All @@ -159,18 +110,9 @@ exports[`setGasPrice EIP1559 transaction 3`] = `
{
"data": "0x0f017112205d7fcd1a0999befdb062e6762c1f0f902f729b98304a2ef539412f53360d3d6a",
"from": "abcd1234",
"gasLimit": {
"hex": "0x0a",
"type": "BigNumber",
},
"maxFeePerGas": {
"hex": "0x08a2",
"type": "BigNumber",
},
"maxPriorityFeePerGas": {
"hex": "0x04ba",
"type": "BigNumber",
},
"gasLimit": 10n,
"maxFeePerGas": 2564n,
"maxPriorityFeePerGas": 1331n,
"nonce": undefined,
"to": "abcd1234",
}
Expand All @@ -180,14 +122,8 @@ exports[`setGasPrice legacy transaction 1`] = `
{
"data": "0x0f017112205d7fcd1a0999befdb062e6762c1f0f902f729b98304a2ef539412f53360d3d6a",
"from": "abcd1234",
"gasLimit": {
"hex": "0x0a",
"type": "BigNumber",
},
"gasPrice": {
"hex": "0x03e8",
"type": "BigNumber",
},
"gasLimit": 10n,
"gasPrice": 1100n,
"nonce": undefined,
"to": "abcd1234",
}
Expand All @@ -197,14 +133,8 @@ exports[`setGasPrice legacy transaction 2`] = `
{
"data": "0x0f017112205d7fcd1a0999befdb062e6762c1f0f902f729b98304a2ef539412f53360d3d6a",
"from": "abcd1234",
"gasLimit": {
"hex": "0x0a",
"type": "BigNumber",
},
"gasPrice": {
"hex": "0x044c",
"type": "BigNumber",
},
"gasLimit": 10n,
"gasPrice": 1210n,
"nonce": undefined,
"to": "abcd1234",
}
Expand All @@ -214,14 +144,8 @@ exports[`setGasPrice legacy transaction 3`] = `
{
"data": "0x0f017112205d7fcd1a0999befdb062e6762c1f0f902f729b98304a2ef539412f53360d3d6a",
"from": "abcd1234",
"gasLimit": {
"hex": "0x0a",
"type": "BigNumber",
},
"gasPrice": {
"hex": "0x04ba",
"type": "BigNumber",
},
"gasLimit": 10n,
"gasPrice": 1331n,
"nonce": undefined,
"to": "abcd1234",
}
Expand Down
Loading

0 comments on commit e724438

Please sign in to comment.