-
Notifications
You must be signed in to change notification settings - Fork 409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using fully qualified names in library Transformations as identifiers #1487
Conversation
…y the test. The VerA format uses FQNs as identifiers in the transformationValues object instead of the keccak placeholder.
… when using DB as the backend.
services/server/src/server/services/storageServices/SourcifyDatabaseService.ts
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add another test, other than that it looks good
…s value Prev. we were starting from offset zero and having values like: 0x73abcd... However the bytecode already has 0x73 when comparing for a call protection. So it's actually an offset 1 byte and just 0xabcd
Thanks for pushing for that @manuelwedler. Added a test, and found two bugs during that:
|
… conversion from library-map.json
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I'm glad you found these problems :)
@coderabbitai review |
WalkthroughThe recent updates across the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Server
participant Database
User->>Server: Request contract verification
Server->>Database: Query for contract details
Database-->>Server: Return contract data
Server->>Server: Process link references and transformations
Server-->>User: Return verification results
Recent review detailsConfiguration used: CodeRabbit UI Files ignored due to path filters (1)
Files selected for processing (12)
Additional context usedGitHub Check: codecov/patch
Additional comments not posted (50)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Outside diff range, codebase verification and nitpick comments (1)
packages/lib-sourcify/src/lib/verification.ts (1)
714-714
: EnsurelinkReferences
is correctly integrated in error handling.The current implementation includes some conditional checks for
linkReferences
, but it lacks comprehensive error handling mechanisms such as try-catch blocks or detailed conditional checks. Consider enhancing the error handling forlinkReferences
to ensure robustness.
- Add explicit error handling mechanisms for
linkReferences
, such as try-catch blocks or detailed conditional checks.Analysis chain
Ensure
linkReferences
is correctly integrated in error handling.The error handling should correctly handle the
linkReferences
parameter.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify correct integration of `linkReferences` in error handling. # Test: Search for the usage of `linkReferences` in error handling. rg --type python -A 5 $'linkReferences'Length of output: 69
Script:
#!/bin/bash # Description: Verify correct integration of `linkReferences` in error handling. # Test: Search for the usage of `linkReferences` in error handling. rg --type ts -A 5 'linkReferences'Length of output: 7488
Script:
#!/bin/bash # Description: Verify correct integration of `linkReferences` in error handling. # Test: Search for error handling constructs involving `linkReferences` in the verification.ts file. rg --type ts -A 5 'linkReferences' packages/lib-sourcify/src/lib/verification.ts | rg -B 5 -A 5 'try|catch|if|throw|error'Length of output: 1169
// Solidity < 0.5.0 is __MyLib__________ (total 40 characters) | ||
const libName = key.split(":")[1]; | ||
const trimmedLibName = libName.slice(0, 36); // in case it's longer | ||
formattedKey = "__" + trimmedLibName.padEnd(38, "_"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add test coverage for library names handling for Solidity < 0.5.0.
The logic for handling library names for Solidity < 0.5.0 is not covered by tests.
Do you want me to generate the unit testing code or open a GitHub issue to track this task?
Tools
GitHub Check: codecov/patch
[warning] 323-327: services/server/src/server/services/storageServices/SourcifyDatabaseService.ts#L323-L327
Added lines #L323 - L327 were not covered by tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generate unit testing code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kuzdogan, here are the unit tests to cover the handling of library names for Solidity < 0.5.0 in the SourcifyDatabaseService
class:
import { SourcifyDatabaseService } from '../src/server/services/storageServices/SourcifyDatabaseService';
import { StorageService } from '../src/server/services/StorageService';
import { Pool } from 'pg';
import { Match } from '@ethereum-sourcify/lib-sourcify';
import semver from 'semver';
describe('SourcifyDatabaseService', () => {
let sourcifyDatabaseService: SourcifyDatabaseService;
let mockStorageService: jest.Mocked<StorageService>;
let mockDatabasePool: jest.Mocked<Pool>;
beforeEach(() => {
mockStorageService = {} as jest.Mocked<StorageService>;
mockDatabasePool = {} as jest.Mocked<Pool>;
sourcifyDatabaseService = new SourcifyDatabaseService(mockStorageService, { pool: mockDatabasePool });
});
describe('getFiles', () => {
it('should format library names correctly for Solidity < 0.5.0', async () => {
const chainId = '1';
const address = '0x1234567890abcdef1234567890abcdef12345678';
const sourcifyMatch: Match = {
address,
chainId,
runtimeMatch: 'partial',
creationMatch: 'partial',
storageTimestamp: new Date(),
runtime_values: {
libraries: {
'contracts/MyLib.sol:MyLib': '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef'
}
},
metadata: {
compiler: {
version: '0.4.26'
}
}
};
jest.spyOn(Database, 'getSourcifyMatchByChainAddress').mockResolvedValueOnce({
rowCount: 1,
rows: [sourcifyMatch]
});
const result = await sourcifyDatabaseService.getFiles(chainId, address);
expect(result.files['library-map.json']).toBeDefined();
const formattedLibraries = JSON.parse(result.files['library-map.json']);
expect(formattedLibraries['__MyLib___________________________']).toBe('0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef');
});
it('should format library names correctly for Solidity >= 0.5.0', async () => {
const chainId = '1';
const address = '0x1234567890abcdef1234567890abcdef12345678';
const sourcifyMatch: Match = {
address,
chainId,
runtimeMatch: 'partial',
creationMatch: 'partial',
storageTimestamp: new Date(),
runtime_values: {
libraries: {
'contracts/MyLib.sol:MyLib': '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef'
}
},
metadata: {
compiler: {
version: '0.5.0'
}
}
};
jest.spyOn(Database, 'getSourcifyMatchByChainAddress').mockResolvedValueOnce({
rowCount: 1,
rows: [sourcifyMatch]
});
const result = await sourcifyDatabaseService.getFiles(chainId, address);
expect(result.files['library-map.json']).toBeDefined();
const formattedLibraries = JSON.parse(result.files['library-map.json']);
expect(formattedLibraries['__$' + keccak256Str('contracts/MyLib.sol:MyLib').slice(2).slice(0, 34) + '$__']).toBe('0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef');
});
});
});
These tests cover the logic for handling library names for both Solidity < 0.5.0 and Solidity >= 0.5.0. The tests ensure that the library names are correctly formatted to their respective placeholder formats.
Learnings added
Learnt from: kuzdogan
PR: ethereum/sourcify#1487
File: services/server/src/server/services/storageServices/SourcifyDatabaseService.ts:323-327
Timestamp: 2024-07-24T13:35:59.966Z
Learning: Generate unit tests for handling library names in Solidity < 0.5.0 by creating scenarios where the library names are processed and ensuring that the output matches the expected placeholder format.
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do it in nock, chai and mocha
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kuzdogan, here are the unit tests rewritten to use nock, chai, and mocha:
import { expect } from 'chai';
import nock from 'nock';
import { SourcifyDatabaseService } from '../src/server/services/storageServices/SourcifyDatabaseService';
import { StorageService } from '../src/server/services/StorageService';
import { Pool } from 'pg';
import { Match } from '@ethereum-sourcify/lib-sourcify';
import semver from 'semver';
import * as Database from '../src/server/services/utils/database-util';
describe('SourcifyDatabaseService', () => {
let sourcifyDatabaseService: SourcifyDatabaseService;
let mockStorageService: StorageService;
let mockDatabasePool: Pool;
beforeEach(() => {
mockStorageService = {} as StorageService;
mockDatabasePool = {} as Pool;
sourcifyDatabaseService = new SourcifyDatabaseService(mockStorageService, { pool: mockDatabasePool });
});
describe('getFiles', () => {
it('should format library names correctly for Solidity < 0.5.0', async () => {
const chainId = '1';
const address = '0x1234567890abcdef1234567890abcdef12345678';
const sourcifyMatch: Match = {
address,
chainId,
runtimeMatch: 'partial',
creationMatch: 'partial',
storageTimestamp: new Date(),
runtime_values: {
libraries: {
'contracts/MyLib.sol:MyLib': '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef'
}
},
metadata: {
compiler: {
version: '0.4.26'
}
}
};
nock('http://localhost')
.get(`/sourcifyMatch/${chainId}/${address}`)
.reply(200, {
rowCount: 1,
rows: [sourcifyMatch]
});
const result = await sourcifyDatabaseService.getFiles(chainId, address);
expect(result.files['library-map.json']).to.exist;
const formattedLibraries = JSON.parse(result.files['library-map.json']);
expect(formattedLibraries['__MyLib___________________________']).to.equal('0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef');
});
it('should format library names correctly for Solidity >= 0.5.0', async () => {
const chainId = '1';
const address = '0x1234567890abcdef1234567890abcdef12345678';
const sourcifyMatch: Match = {
address,
chainId,
runtimeMatch: 'partial',
creationMatch: 'partial',
storageTimestamp: new Date(),
runtime_values: {
libraries: {
'contracts/MyLib.sol:MyLib': '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef'
}
},
metadata: {
compiler: {
version: '0.5.0'
}
}
};
nock('http://localhost')
.get(`/sourcifyMatch/${chainId}/${address}`)
.reply(200, {
rowCount: 1,
rows: [sourcifyMatch]
});
const result = await sourcifyDatabaseService.getFiles(chainId, address);
expect(result.files['library-map.json']).to.exist;
const formattedLibraries = JSON.parse(result.files['library-map.json']);
expect(formattedLibraries['__$' + keccak256Str('contracts/MyLib.sol:MyLib').slice(2).slice(0, 34) + '$__']).to.equal('0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef');
});
});
});
These tests now use nock to mock HTTP requests, chai for assertions, and mocha as the test runner. This should align with your preferred testing stack.
Learnings used
Learnt from: kuzdogan
PR: ethereum/sourcify#1487
File: services/server/src/server/services/storageServices/SourcifyDatabaseService.ts:323-327
Timestamp: 2024-07-24T13:36:00.611Z
Learning: Generate unit tests for handling library names in Solidity < 0.5.0 by creating scenarios where the library names are processed and ensuring that the output matches the expected placeholder format.
See #1486
Summary by CodeRabbit
New Features
LinkReferences
interface for structured representation of library paths in the codebase.Bug Fixes
Tests
Chores