GSNContext.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const { BN, constants, expectEvent, expectRevert } = require('openzeppelin-test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const gsn = require('@openzeppelin/gsn-helpers');
  4. const GSNContextMock = artifacts.require('GSNContextMock');
  5. const ContextMockCaller = artifacts.require('ContextMockCaller');
  6. const { shouldBehaveLikeRegularContext } = require('./Context.behavior');
  7. contract('GSNContext', function ([_, deployer, sender, newRelayHub]) {
  8. beforeEach(async function () {
  9. this.context = await GSNContextMock.new();
  10. this.caller = await ContextMockCaller.new();
  11. });
  12. describe('get/set RelayHub', function () {
  13. const singletonRelayHub = '0xD216153c06E857cD7f72665E0aF1d7D82172F494';
  14. it('initially returns the singleton instance address', async function () {
  15. expect(await this.context.getRelayHub()).to.equal(singletonRelayHub);
  16. });
  17. it('can be upgraded to a new RelayHub', async function () {
  18. const { logs } = await this.context.upgradeRelayHub(newRelayHub);
  19. expectEvent.inLogs(logs, 'RelayHubChanged', { oldRelayHub: singletonRelayHub, newRelayHub });
  20. });
  21. it('cannot upgrade to the same RelayHub', async function () {
  22. await expectRevert(
  23. this.context.upgradeRelayHub(singletonRelayHub),
  24. 'GSNContext: new RelayHub is the current one'
  25. );
  26. });
  27. it('cannot upgrade to the zero address', async function () {
  28. await expectRevert(this.context.upgradeRelayHub(ZERO_ADDRESS), 'GSNContext: new RelayHub is the zero address');
  29. });
  30. context('with new RelayHub', function () {
  31. beforeEach(async function () {
  32. await this.context.upgradeRelayHub(newRelayHub);
  33. });
  34. it('returns the new instance address', async function () {
  35. expect(await this.context.getRelayHub()).to.equal(newRelayHub);
  36. });
  37. });
  38. });
  39. context('when called directly', function () {
  40. shouldBehaveLikeRegularContext(sender);
  41. });
  42. context('when receiving a relayed call', function () {
  43. beforeEach(async function () {
  44. await gsn.fundRecipient(web3, { recipient: this.context.address });
  45. });
  46. describe('msgSender', function () {
  47. it('returns the relayed transaction original sender', async function () {
  48. const { tx } = await this.context.msgSender({ from: sender, useGSN: true });
  49. await expectEvent.inTransaction(tx, GSNContextMock, 'Sender', { sender });
  50. });
  51. });
  52. describe('msgData', function () {
  53. it('returns the relayed transaction original data', async function () {
  54. const integerValue = new BN('42');
  55. const stringValue = 'OpenZeppelin';
  56. const callData = this.context.contract.methods.msgData(integerValue.toString(), stringValue).encodeABI();
  57. // The provider doesn't properly estimate gas for a relayed call, so we need to manually set a higher value
  58. const { tx } = await this.context.msgData(integerValue, stringValue, { gas: 1000000, useGSN: true });
  59. await expectEvent.inTransaction(tx, GSNContextMock, 'Data', { data: callData, integerValue, stringValue });
  60. });
  61. });
  62. });
  63. });