TransparentUpgradeableProxy.behaviour.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. const { BN, expectRevert, expectEvent, constants } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { getAddressInSlot, ImplementationSlot, AdminSlot } = require('../../helpers/erc1967');
  4. const { expectRevertCustomError } = require('../../helpers/customError');
  5. const { expect } = require('chai');
  6. const { ethers, web3 } = require('hardhat');
  7. const { impersonate } = require('../../helpers/account');
  8. const Implementation1 = artifacts.require('Implementation1');
  9. const Implementation2 = artifacts.require('Implementation2');
  10. const Implementation3 = artifacts.require('Implementation3');
  11. const Implementation4 = artifacts.require('Implementation4');
  12. const MigratableMockV1 = artifacts.require('MigratableMockV1');
  13. const MigratableMockV2 = artifacts.require('MigratableMockV2');
  14. const MigratableMockV3 = artifacts.require('MigratableMockV3');
  15. const InitializableMock = artifacts.require('InitializableMock');
  16. const DummyImplementation = artifacts.require('DummyImplementation');
  17. const ClashingImplementation = artifacts.require('ClashingImplementation');
  18. const Ownable = artifacts.require('Ownable');
  19. module.exports = function shouldBehaveLikeTransparentUpgradeableProxy(createProxy, initialOwner, accounts) {
  20. const [anotherAccount] = accounts;
  21. async function createProxyWithImpersonatedProxyAdmin(logic, initData, opts = undefined) {
  22. const proxy = await createProxy(logic, initData, opts);
  23. // Expect proxy admin to be the first and only contract created by the proxy
  24. const proxyAdminAddress = ethers.getCreateAddress({ from: proxy.address, nonce: 1 });
  25. await impersonate(proxyAdminAddress);
  26. return {
  27. proxy,
  28. proxyAdminAddress,
  29. };
  30. }
  31. before(async function () {
  32. this.implementationV0 = (await DummyImplementation.new()).address;
  33. this.implementationV1 = (await DummyImplementation.new()).address;
  34. });
  35. beforeEach(async function () {
  36. const initializeData = Buffer.from('');
  37. const { proxy, proxyAdminAddress } = await createProxyWithImpersonatedProxyAdmin(
  38. this.implementationV0,
  39. initializeData,
  40. );
  41. this.proxy = proxy;
  42. this.proxyAdminAddress = proxyAdminAddress;
  43. });
  44. describe('implementation', function () {
  45. it('returns the current implementation address', async function () {
  46. const implementationAddress = await getAddressInSlot(this.proxy, ImplementationSlot);
  47. expect(implementationAddress).to.be.equal(this.implementationV0);
  48. });
  49. it('delegates to the implementation', async function () {
  50. const dummy = new DummyImplementation(this.proxy.address);
  51. const value = await dummy.get();
  52. expect(value).to.equal(true);
  53. });
  54. });
  55. describe('proxy admin', function () {
  56. it('emits AdminChanged event during construction', async function () {
  57. await expectEvent.inConstruction(this.proxy, 'AdminChanged', {
  58. previousAdmin: ZERO_ADDRESS,
  59. newAdmin: this.proxyAdminAddress,
  60. });
  61. });
  62. it('sets the proxy admin in storage with the correct initial owner', async function () {
  63. expect(await getAddressInSlot(this.proxy, AdminSlot)).to.be.equal(this.proxyAdminAddress);
  64. const proxyAdmin = await Ownable.at(this.proxyAdminAddress);
  65. expect(await proxyAdmin.owner()).to.be.equal(initialOwner);
  66. });
  67. it('can overwrite the admin by the implementation', async function () {
  68. const dummy = new DummyImplementation(this.proxy.address);
  69. await dummy.unsafeOverrideAdmin(anotherAccount);
  70. const ERC1967AdminSlotValue = await getAddressInSlot(this.proxy, AdminSlot);
  71. expect(ERC1967AdminSlotValue).to.be.equal(anotherAccount);
  72. // Still allows previous admin to execute admin operations
  73. expect(ERC1967AdminSlotValue).to.not.equal(this.proxyAdminAddress);
  74. expectEvent(
  75. await this.proxy.upgradeToAndCall(this.implementationV1, '0x', { from: this.proxyAdminAddress }),
  76. 'Upgraded',
  77. {
  78. implementation: this.implementationV1,
  79. },
  80. );
  81. });
  82. });
  83. describe('upgradeToAndCall', function () {
  84. describe('without migrations', function () {
  85. beforeEach(async function () {
  86. this.behavior = await InitializableMock.new();
  87. });
  88. describe('when the call does not fail', function () {
  89. const initializeData = new InitializableMock('').contract.methods['initializeWithX(uint256)'](42).encodeABI();
  90. describe('when the sender is the admin', function () {
  91. const value = 1e5;
  92. beforeEach(async function () {
  93. this.receipt = await this.proxy.upgradeToAndCall(this.behavior.address, initializeData, {
  94. from: this.proxyAdminAddress,
  95. value,
  96. });
  97. });
  98. it('upgrades to the requested implementation', async function () {
  99. const implementationAddress = await getAddressInSlot(this.proxy, ImplementationSlot);
  100. expect(implementationAddress).to.be.equal(this.behavior.address);
  101. });
  102. it('emits an event', function () {
  103. expectEvent(this.receipt, 'Upgraded', { implementation: this.behavior.address });
  104. });
  105. it('calls the initializer function', async function () {
  106. const migratable = new InitializableMock(this.proxy.address);
  107. const x = await migratable.x();
  108. expect(x).to.be.bignumber.equal('42');
  109. });
  110. it('sends given value to the proxy', async function () {
  111. const balance = await web3.eth.getBalance(this.proxy.address);
  112. expect(balance.toString()).to.be.bignumber.equal(value.toString());
  113. });
  114. it('uses the storage of the proxy', async function () {
  115. // storage layout should look as follows:
  116. // - 0: Initializable storage ++ initializerRan ++ onlyInitializingRan
  117. // - 1: x
  118. const storedValue = await web3.eth.getStorageAt(this.proxy.address, 1);
  119. expect(parseInt(storedValue)).to.eq(42);
  120. });
  121. });
  122. describe('when the sender is not the admin', function () {
  123. it('reverts', async function () {
  124. await expectRevert.unspecified(
  125. this.proxy.upgradeToAndCall(this.behavior.address, initializeData, { from: anotherAccount }),
  126. );
  127. });
  128. });
  129. });
  130. describe('when the call does fail', function () {
  131. const initializeData = new InitializableMock('').contract.methods.fail().encodeABI();
  132. it('reverts', async function () {
  133. await expectRevert.unspecified(
  134. this.proxy.upgradeToAndCall(this.behavior.address, initializeData, { from: this.proxyAdminAddress }),
  135. );
  136. });
  137. });
  138. });
  139. describe('with migrations', function () {
  140. describe('when the sender is the admin', function () {
  141. const value = 1e5;
  142. describe('when upgrading to V1', function () {
  143. const v1MigrationData = new MigratableMockV1('').contract.methods.initialize(42).encodeABI();
  144. beforeEach(async function () {
  145. this.behaviorV1 = await MigratableMockV1.new();
  146. this.balancePreviousV1 = new BN(await web3.eth.getBalance(this.proxy.address));
  147. this.receipt = await this.proxy.upgradeToAndCall(this.behaviorV1.address, v1MigrationData, {
  148. from: this.proxyAdminAddress,
  149. value,
  150. });
  151. });
  152. it('upgrades to the requested version and emits an event', async function () {
  153. const implementation = await getAddressInSlot(this.proxy, ImplementationSlot);
  154. expect(implementation).to.be.equal(this.behaviorV1.address);
  155. expectEvent(this.receipt, 'Upgraded', { implementation: this.behaviorV1.address });
  156. });
  157. it("calls the 'initialize' function and sends given value to the proxy", async function () {
  158. const migratable = new MigratableMockV1(this.proxy.address);
  159. const x = await migratable.x();
  160. expect(x).to.be.bignumber.equal('42');
  161. const balance = await web3.eth.getBalance(this.proxy.address);
  162. expect(new BN(balance)).to.be.bignumber.equal(this.balancePreviousV1.addn(value));
  163. });
  164. describe('when upgrading to V2', function () {
  165. const v2MigrationData = new MigratableMockV2('').contract.methods.migrate(10, 42).encodeABI();
  166. beforeEach(async function () {
  167. this.behaviorV2 = await MigratableMockV2.new();
  168. this.balancePreviousV2 = new BN(await web3.eth.getBalance(this.proxy.address));
  169. this.receipt = await this.proxy.upgradeToAndCall(this.behaviorV2.address, v2MigrationData, {
  170. from: this.proxyAdminAddress,
  171. value,
  172. });
  173. });
  174. it('upgrades to the requested version and emits an event', async function () {
  175. const implementation = await getAddressInSlot(this.proxy, ImplementationSlot);
  176. expect(implementation).to.be.equal(this.behaviorV2.address);
  177. expectEvent(this.receipt, 'Upgraded', { implementation: this.behaviorV2.address });
  178. });
  179. it("calls the 'migrate' function and sends given value to the proxy", async function () {
  180. const migratable = new MigratableMockV2(this.proxy.address);
  181. const x = await migratable.x();
  182. expect(x).to.be.bignumber.equal('10');
  183. const y = await migratable.y();
  184. expect(y).to.be.bignumber.equal('42');
  185. const balance = new BN(await web3.eth.getBalance(this.proxy.address));
  186. expect(balance).to.be.bignumber.equal(this.balancePreviousV2.addn(value));
  187. });
  188. describe('when upgrading to V3', function () {
  189. const v3MigrationData = new MigratableMockV3('').contract.methods['migrate()']().encodeABI();
  190. beforeEach(async function () {
  191. this.behaviorV3 = await MigratableMockV3.new();
  192. this.balancePreviousV3 = new BN(await web3.eth.getBalance(this.proxy.address));
  193. this.receipt = await this.proxy.upgradeToAndCall(this.behaviorV3.address, v3MigrationData, {
  194. from: this.proxyAdminAddress,
  195. value,
  196. });
  197. });
  198. it('upgrades to the requested version and emits an event', async function () {
  199. const implementation = await getAddressInSlot(this.proxy, ImplementationSlot);
  200. expect(implementation).to.be.equal(this.behaviorV3.address);
  201. expectEvent(this.receipt, 'Upgraded', { implementation: this.behaviorV3.address });
  202. });
  203. it("calls the 'migrate' function and sends given value to the proxy", async function () {
  204. const migratable = new MigratableMockV3(this.proxy.address);
  205. const x = await migratable.x();
  206. expect(x).to.be.bignumber.equal('42');
  207. const y = await migratable.y();
  208. expect(y).to.be.bignumber.equal('10');
  209. const balance = new BN(await web3.eth.getBalance(this.proxy.address));
  210. expect(balance).to.be.bignumber.equal(this.balancePreviousV3.addn(value));
  211. });
  212. });
  213. });
  214. });
  215. });
  216. describe('when the sender is not the admin', function () {
  217. const from = anotherAccount;
  218. it('reverts', async function () {
  219. const behaviorV1 = await MigratableMockV1.new();
  220. const v1MigrationData = new MigratableMockV1('').contract.methods.initialize(42).encodeABI();
  221. await expectRevert.unspecified(this.proxy.upgradeToAndCall(behaviorV1.address, v1MigrationData, { from }));
  222. });
  223. });
  224. });
  225. });
  226. describe('transparent proxy', function () {
  227. beforeEach('creating proxy', async function () {
  228. const initializeData = Buffer.from('');
  229. this.clashingImplV0 = (await ClashingImplementation.new()).address;
  230. this.clashingImplV1 = (await ClashingImplementation.new()).address;
  231. const { proxy, proxyAdminAddress } = await createProxyWithImpersonatedProxyAdmin(
  232. this.clashingImplV0,
  233. initializeData,
  234. );
  235. this.proxy = proxy;
  236. this.proxyAdminAddress = proxyAdminAddress;
  237. this.clashing = new ClashingImplementation(this.proxy.address);
  238. });
  239. it('proxy admin cannot call delegated functions', async function () {
  240. await expectRevertCustomError(
  241. this.clashing.delegatedFunction({ from: this.proxyAdminAddress }),
  242. 'ProxyDeniedAdminAccess',
  243. [],
  244. );
  245. });
  246. describe('when function names clash', function () {
  247. it('executes the proxy function if the sender is the admin', async function () {
  248. const receipt = await this.proxy.upgradeToAndCall(this.clashingImplV1, '0x', {
  249. from: this.proxyAdminAddress,
  250. });
  251. expectEvent(receipt, 'Upgraded', { implementation: this.clashingImplV1 });
  252. });
  253. it('delegates the call to implementation when sender is not the admin', async function () {
  254. const receipt = await this.proxy.upgradeToAndCall(this.clashingImplV1, '0x', {
  255. from: anotherAccount,
  256. });
  257. expectEvent.notEmitted(receipt, 'Upgraded');
  258. expectEvent.inTransaction(receipt.tx, this.clashing, 'ClashingImplementationCall');
  259. });
  260. });
  261. });
  262. describe('regression', () => {
  263. const initializeData = Buffer.from('');
  264. it('should add new function', async () => {
  265. const instance1 = await Implementation1.new();
  266. const { proxy, proxyAdminAddress } = await createProxyWithImpersonatedProxyAdmin(
  267. instance1.address,
  268. initializeData,
  269. );
  270. const proxyInstance1 = new Implementation1(proxy.address);
  271. await proxyInstance1.setValue(42);
  272. const instance2 = await Implementation2.new();
  273. await proxy.upgradeToAndCall(instance2.address, '0x', { from: proxyAdminAddress });
  274. const proxyInstance2 = new Implementation2(proxy.address);
  275. const res = await proxyInstance2.getValue();
  276. expect(res.toString()).to.eq('42');
  277. });
  278. it('should remove function', async () => {
  279. const instance2 = await Implementation2.new();
  280. const { proxy, proxyAdminAddress } = await createProxyWithImpersonatedProxyAdmin(
  281. instance2.address,
  282. initializeData,
  283. );
  284. const proxyInstance2 = new Implementation2(proxy.address);
  285. await proxyInstance2.setValue(42);
  286. const res = await proxyInstance2.getValue();
  287. expect(res.toString()).to.eq('42');
  288. const instance1 = await Implementation1.new();
  289. await proxy.upgradeToAndCall(instance1.address, '0x', { from: proxyAdminAddress });
  290. const proxyInstance1 = new Implementation2(proxy.address);
  291. await expectRevert.unspecified(proxyInstance1.getValue());
  292. });
  293. it('should change function signature', async () => {
  294. const instance1 = await Implementation1.new();
  295. const { proxy, proxyAdminAddress } = await createProxyWithImpersonatedProxyAdmin(
  296. instance1.address,
  297. initializeData,
  298. );
  299. const proxyInstance1 = new Implementation1(proxy.address);
  300. await proxyInstance1.setValue(42);
  301. const instance3 = await Implementation3.new();
  302. await proxy.upgradeToAndCall(instance3.address, '0x', { from: proxyAdminAddress });
  303. const proxyInstance3 = new Implementation3(proxy.address);
  304. const res = await proxyInstance3.getValue(8);
  305. expect(res.toString()).to.eq('50');
  306. });
  307. it('should add fallback function', async () => {
  308. const initializeData = Buffer.from('');
  309. const instance1 = await Implementation1.new();
  310. const { proxy, proxyAdminAddress } = await createProxyWithImpersonatedProxyAdmin(
  311. instance1.address,
  312. initializeData,
  313. );
  314. const instance4 = await Implementation4.new();
  315. await proxy.upgradeToAndCall(instance4.address, '0x', { from: proxyAdminAddress });
  316. const proxyInstance4 = new Implementation4(proxy.address);
  317. const data = '0x';
  318. await web3.eth.sendTransaction({ to: proxy.address, from: anotherAccount, data });
  319. const res = await proxyInstance4.getValue();
  320. expect(res.toString()).to.eq('1');
  321. });
  322. it('should remove fallback function', async () => {
  323. const instance4 = await Implementation4.new();
  324. const { proxy, proxyAdminAddress } = await createProxyWithImpersonatedProxyAdmin(
  325. instance4.address,
  326. initializeData,
  327. );
  328. const instance2 = await Implementation2.new();
  329. await proxy.upgradeToAndCall(instance2.address, '0x', { from: proxyAdminAddress });
  330. const data = '0x';
  331. await expectRevert.unspecified(web3.eth.sendTransaction({ to: proxy.address, from: anotherAccount, data }));
  332. const proxyInstance2 = new Implementation2(proxy.address);
  333. const res = await proxyInstance2.getValue();
  334. expect(res.toString()).to.eq('0');
  335. });
  336. });
  337. };