TransparentUpgradeableProxy.behaviour.js 17 KB

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