TransparentUpgradeableProxy.behaviour.js 18 KB

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