TransparentUpgradeableProxy.behaviour.js 17 KB

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