-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement contract upgrades compatibility in ContractKit (#8308)
### Description - Adds version logic to ContractKit base wrapper to support generic version gatekeeping ### Other changes - Modularize and simplify protocol build scripts to only generate artifacts when necessary - Modify contractkit build:gen script to support targeting contract releases ### Tested - Tests added for base wrapper version compatibility ### Related issues - Fixes #8018 ### Backwards compatibility Yes ### Documentation None
- Loading branch information
Showing
9 changed files
with
153 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const semverGte = require('semver/functions/gte') | ||
|
||
export class ContractVersion { | ||
constructor( | ||
public readonly storage: number | string, | ||
public readonly major: number | string, | ||
public readonly minor: number | string, | ||
public readonly patch: number | string | ||
) {} | ||
private toSemver = () => `${this.storage}.${this.major}.${this.minor}` | ||
isAtLeast = (other: ContractVersion) => semverGte(this.toSemver(), other.toSemver()) | ||
toString = () => this.toSemver().concat(`.${this.patch}`) | ||
toRaw = () => [this.storage, this.major, this.minor, this.patch] | ||
static fromRaw = (raw: ReturnType<ContractVersion['toRaw']>) => | ||
new ContractVersion(raw[0], raw[1], raw[2], raw[3]) | ||
} | ||
|
||
export const newContractVersion = (storage: number, major: number, minor: number, patch: number) => | ||
new ContractVersion(storage, major, minor, patch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { NULL_ADDRESS } from '@celo/base' | ||
import { CeloTxObject } from '@celo/connect' | ||
import Web3 from 'web3' | ||
import { | ||
ICeloVersionedContract, | ||
newICeloVersionedContract, | ||
} from '../generated/ICeloVersionedContract' | ||
import { newKitFromWeb3 } from '../kit' | ||
import { ContractVersion, newContractVersion } from '../versions' | ||
import { BaseWrapper } from './BaseWrapper' | ||
|
||
const web3 = new Web3('http://localhost:8545') | ||
const mockContract = newICeloVersionedContract(web3, NULL_ADDRESS) | ||
const mockVersion = newContractVersion(1, 1, 1, 1) | ||
// @ts-ignore | ||
mockContract.methods.getVersionNumber = (): CeloTxObject<any> => ({ | ||
call: async () => mockVersion.toRaw(), | ||
}) | ||
|
||
class TestWrapper extends BaseWrapper<ICeloVersionedContract> { | ||
constructor() { | ||
super(newKitFromWeb3(web3), mockContract) | ||
} | ||
|
||
async protectedFunction(v: ContractVersion) { | ||
await this.onlyVersionOrGreater(v) | ||
} | ||
} | ||
|
||
describe('TestWrapper', () => { | ||
const tw = new TestWrapper() | ||
|
||
describe(`#onlyVersionOrGreater (actual = ${mockVersion})`, () => { | ||
const throwTests = [ | ||
newContractVersion(2, 0, 0, 0), | ||
newContractVersion(1, 2, 0, 0), | ||
newContractVersion(1, 1, 2, 0), | ||
] | ||
const resolveTests = [newContractVersion(1, 1, 1, 2), newContractVersion(1, 0, 0, 0)] | ||
|
||
throwTests.forEach((v) => { | ||
it(`should throw with incompatible version ${v}`, async () => { | ||
await expect(tw.protectedFunction(v)).rejects.toThrow( | ||
`Bytecode version ${mockVersion} is not compatible with ${v} yet` | ||
) | ||
}) | ||
}) | ||
|
||
resolveTests.forEach((v) => { | ||
it(`should resolve with compatible version ${v}`, async () => { | ||
await expect(tw.protectedFunction(v)).resolves.not.toThrow() | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters