Initial GSN support (beta) (#1844)
* Add base Context contract
* Add GSNContext and tests
* Add RelayHub deployment to tests
* Add RelayProvider integration, complete GSNContext tests
* Switch dependency to openzeppelin-gsn-provider
* Add default txfee to provider
* Add basic signing recipient
* Sign more values
* Add comment clarifying RelayHub's msg.data
* Make context constructors internal
* Rename SigningRecipient to GSNRecipientSignedData
* Add ERC20Charge recipients
* Harcode RelayHub address into GSNContext
* Fix Solidity linter errors
* Run server from binary, use gsn-helpers to fund it
* Migrate to published @openzeppelin/gsn-helpers
* Silence false-positive compiler warning
* Use GSN helper assertions
* Rename meta-tx to gsn, take out of drafts
* Merge ERC20 charge recipients into a single one
* Rename GSNRecipients to Bouncers
* Add GSNBouncerUtils to decouple the bouncers from GSNRecipient
* Add _upgradeRelayHub
* Store RelayHub address using unstructored storage
* Add IRelayHub
* Add _withdrawDeposits to GSNRecipient
* Add relayHub version to recipient
* Make _acceptRelayedCall and _declineRelayedCall easier to use
* Rename GSNBouncerUtils to GSNBouncerBase, make it IRelayRecipient
* Improve GSNBouncerBase, make pre and post sender-protected and optional
* Fix GSNBouncerERC20Fee, add tests
* Add missing GSNBouncerSignature test
* Override transferFrom in __unstable__ERC20PrimaryAdmin
* Fix gsn dependencies in package.json
* Rhub address slot reduced by 1
* Rename relay hub changed event
* Use released gsn-provider
* Run relayer with short sleep of 1s instead of 100ms
* update package-lock.json
* clear circle cache
* use optimized gsn-provider
* update to latest @openzeppelin/gsn-provider
* replace with gsn dev provider
* remove relay server
* rename arguments in approveFunction
* fix GSNBouncerSignature test
* change gsn txfee
* initialize development provider only once
* update RelayHub interface
* adapt to new IRelayHub.withdraw
* update @openzeppelin/gsn-helpers
* update relayhub singleton address
* fix helper name
* set up gsn provider for coverage too
* lint
* Revert "set up gsn provider for coverage too"
This reverts commit 8a7b5be5f9.
* remove unused code
* add gsn provider to coverage
* move truffle contract options back out
* increase gas limit for coverage
* remove unreachable code
* add more gas for GSNContext test
* fix test suite name
* rename GSNBouncerBase internal API
* remove onlyRelayHub modifier
* add explicit inheritance
* remove redundant event
* update name of bouncers error codes enums
* add basic docs page for gsn contracts
* make gsn directory all caps
* add changelog entry
* lint
* enable test run to fail in coverage
This commit is contained in:
committed by
Francisco Giordano
parent
e9cd1b5b44
commit
0ec1d761aa
42
test/GSN/Context.behavior.js
Normal file
42
test/GSN/Context.behavior.js
Normal file
@ -0,0 +1,42 @@
|
||||
const { BN, expectEvent } = require('openzeppelin-test-helpers');
|
||||
|
||||
const ContextMock = artifacts.require('ContextMock');
|
||||
|
||||
function shouldBehaveLikeRegularContext (sender) {
|
||||
describe('msgSender', function () {
|
||||
it('returns the transaction sender when called from an EOA', async function () {
|
||||
const { logs } = await this.context.msgSender({ from: sender });
|
||||
expectEvent.inLogs(logs, 'Sender', { sender });
|
||||
});
|
||||
|
||||
it('returns the transaction sender when from another contract', async function () {
|
||||
const { tx } = await this.caller.callSender(this.context.address, { from: sender });
|
||||
await expectEvent.inTransaction(tx, ContextMock, 'Sender', { sender: this.caller.address });
|
||||
});
|
||||
});
|
||||
|
||||
describe('msgData', function () {
|
||||
const integerValue = new BN('42');
|
||||
const stringValue = 'OpenZeppelin';
|
||||
|
||||
let callData;
|
||||
|
||||
beforeEach(async function () {
|
||||
callData = this.context.contract.methods.msgData(integerValue.toString(), stringValue).encodeABI();
|
||||
});
|
||||
|
||||
it('returns the transaction data when called from an EOA', async function () {
|
||||
const { logs } = await this.context.msgData(integerValue, stringValue);
|
||||
expectEvent.inLogs(logs, 'Data', { data: callData, integerValue, stringValue });
|
||||
});
|
||||
|
||||
it('returns the transaction sender when from another contract', async function () {
|
||||
const { tx } = await this.caller.callData(this.context.address, integerValue, stringValue);
|
||||
await expectEvent.inTransaction(tx, ContextMock, 'Data', { data: callData, integerValue, stringValue });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
shouldBehaveLikeRegularContext,
|
||||
};
|
||||
15
test/GSN/Context.test.js
Normal file
15
test/GSN/Context.test.js
Normal file
@ -0,0 +1,15 @@
|
||||
require('openzeppelin-test-helpers');
|
||||
|
||||
const ContextMock = artifacts.require('ContextMock');
|
||||
const ContextMockCaller = artifacts.require('ContextMockCaller');
|
||||
|
||||
const { shouldBehaveLikeRegularContext } = require('./Context.behavior');
|
||||
|
||||
contract('Context', function ([_, sender]) {
|
||||
beforeEach(async function () {
|
||||
this.context = await ContextMock.new();
|
||||
this.caller = await ContextMockCaller.new();
|
||||
});
|
||||
|
||||
shouldBehaveLikeRegularContext(sender);
|
||||
});
|
||||
69
test/GSN/GSNBouncerERC20Fee.test.js
Normal file
69
test/GSN/GSNBouncerERC20Fee.test.js
Normal file
@ -0,0 +1,69 @@
|
||||
const { BN, ether, expectEvent } = require('openzeppelin-test-helpers');
|
||||
const gsn = require('@openzeppelin/gsn-helpers');
|
||||
|
||||
const { expect } = require('chai');
|
||||
|
||||
const GSNBouncerERC20FeeMock = artifacts.require('GSNBouncerERC20FeeMock');
|
||||
const ERC20Detailed = artifacts.require('ERC20Detailed');
|
||||
const IRelayHub = artifacts.require('IRelayHub');
|
||||
|
||||
contract('GSNBouncerERC20Fee', function ([_, sender, other]) {
|
||||
const name = 'FeeToken';
|
||||
const symbol = 'FTKN';
|
||||
const decimals = new BN('18');
|
||||
|
||||
beforeEach(async function () {
|
||||
this.recipient = await GSNBouncerERC20FeeMock.new(name, symbol, decimals);
|
||||
this.token = await ERC20Detailed.at(await this.recipient.token());
|
||||
});
|
||||
|
||||
describe('token', function () {
|
||||
it('has a name', async function () {
|
||||
expect(await this.token.name()).to.equal(name);
|
||||
});
|
||||
|
||||
it('has a symbol', async function () {
|
||||
expect(await this.token.symbol()).to.equal(symbol);
|
||||
});
|
||||
|
||||
it('has decimals', async function () {
|
||||
expect(await this.token.decimals()).to.be.bignumber.equal(decimals);
|
||||
});
|
||||
});
|
||||
|
||||
context('when called directly', function () {
|
||||
it('mock function can be called', async function () {
|
||||
const { logs } = await this.recipient.mockFunction();
|
||||
expectEvent.inLogs(logs, 'MockFunctionCalled');
|
||||
});
|
||||
});
|
||||
|
||||
context('when relay-called', function () {
|
||||
beforeEach(async function () {
|
||||
await gsn.fundRecipient(web3, { recipient: this.recipient.address });
|
||||
this.relayHub = await IRelayHub.at('0xD216153c06E857cD7f72665E0aF1d7D82172F494');
|
||||
});
|
||||
|
||||
it('charges the sender for GSN fees in tokens', async function () {
|
||||
// The recipient will be charged from its RelayHub balance, and in turn charge the sender from its sender balance.
|
||||
// Both amounts should be roughly equal.
|
||||
|
||||
// The sender has a balance in tokens, not ether, but since the exchange rate is 1:1, this works fine.
|
||||
const senderPreBalance = ether('2');
|
||||
await this.recipient.mint(sender, senderPreBalance);
|
||||
|
||||
const recipientPreBalance = await this.relayHub.balanceOf(this.recipient.address);
|
||||
|
||||
const { tx } = await this.recipient.mockFunction({ from: sender, useGSN: true });
|
||||
await expectEvent.inTransaction(tx, IRelayHub, 'TransactionRelayed', { status: '0' });
|
||||
|
||||
const senderPostBalance = await this.token.balanceOf(sender);
|
||||
const recipientPostBalance = await this.relayHub.balanceOf(this.recipient.address);
|
||||
|
||||
const senderCharge = senderPreBalance.sub(senderPostBalance);
|
||||
const recipientCharge = recipientPreBalance.sub(recipientPostBalance);
|
||||
|
||||
expect(senderCharge).to.be.bignumber.closeTo(recipientCharge, recipientCharge.divn(10));
|
||||
});
|
||||
});
|
||||
});
|
||||
73
test/GSN/GSNBouncerSignature.test.js
Normal file
73
test/GSN/GSNBouncerSignature.test.js
Normal file
@ -0,0 +1,73 @@
|
||||
const { expectEvent } = require('openzeppelin-test-helpers');
|
||||
const gsn = require('@openzeppelin/gsn-helpers');
|
||||
const { fixSignature } = require('../helpers/sign');
|
||||
const { utils: { toBN } } = require('web3');
|
||||
|
||||
const GSNBouncerSignatureMock = artifacts.require('GSNBouncerSignatureMock');
|
||||
|
||||
contract('GSNBouncerSignature', function ([_, signer, other]) {
|
||||
beforeEach(async function () {
|
||||
this.recipient = await GSNBouncerSignatureMock.new(signer);
|
||||
});
|
||||
|
||||
context('when called directly', function () {
|
||||
it('mock function can be called', async function () {
|
||||
const { logs } = await this.recipient.mockFunction();
|
||||
expectEvent.inLogs(logs, 'MockFunctionCalled');
|
||||
});
|
||||
});
|
||||
|
||||
context('when relay-called', function () {
|
||||
beforeEach(async function () {
|
||||
await gsn.fundRecipient(web3, { recipient: this.recipient.address });
|
||||
});
|
||||
|
||||
it('rejects unsigned relay requests', async function () {
|
||||
await gsn.expectError(this.recipient.mockFunction({ value: 0, useGSN: true }));
|
||||
});
|
||||
|
||||
it('rejects relay requests where some parameters are signed', async function () {
|
||||
const approveFunction = async (data) =>
|
||||
fixSignature(
|
||||
await web3.eth.sign(
|
||||
web3.utils.soliditySha3(
|
||||
// the nonce is not signed
|
||||
data.relayerAddress, data.from, data.encodedFunctionCall, data.txFee, data.gasPrice, data.gas
|
||||
), signer
|
||||
)
|
||||
);
|
||||
|
||||
await gsn.expectError(this.recipient.mockFunction({ value: 0, useGSN: true, approveFunction }));
|
||||
});
|
||||
|
||||
it('accepts relay requests where all parameters are signed', async function () {
|
||||
const approveFunction = async (data) =>
|
||||
fixSignature(
|
||||
await web3.eth.sign(
|
||||
web3.utils.soliditySha3(
|
||||
// eslint-disable-next-line max-len
|
||||
data.relayerAddress, data.from, data.encodedFunctionCall, toBN(data.txFee), toBN(data.gasPrice), toBN(data.gas), toBN(data.nonce), data.relayHubAddress, data.to
|
||||
), signer
|
||||
)
|
||||
);
|
||||
|
||||
const { tx } = await this.recipient.mockFunction({ value: 0, useGSN: true, approveFunction });
|
||||
|
||||
await expectEvent.inTransaction(tx, GSNBouncerSignatureMock, 'MockFunctionCalled');
|
||||
});
|
||||
|
||||
it('rejects relay requests where all parameters are signed by an invalid signer', async function () {
|
||||
const approveFunction = async (data) =>
|
||||
fixSignature(
|
||||
await web3.eth.sign(
|
||||
web3.utils.soliditySha3(
|
||||
// eslint-disable-next-line max-len
|
||||
data.relay_address, data.from, data.encodedFunctionCall, data.txfee, data.gasPrice, data.gas, data.nonce, data.relayHubAddress, data.to
|
||||
), other
|
||||
)
|
||||
);
|
||||
|
||||
await gsn.expectError(this.recipient.mockFunction({ value: 0, useGSN: true, approveFunction }));
|
||||
});
|
||||
});
|
||||
});
|
||||
78
test/GSN/GSNContext.test.js
Normal file
78
test/GSN/GSNContext.test.js
Normal file
@ -0,0 +1,78 @@
|
||||
const { BN, constants, expectEvent, expectRevert } = require('openzeppelin-test-helpers');
|
||||
const { ZERO_ADDRESS } = constants;
|
||||
const gsn = require('@openzeppelin/gsn-helpers');
|
||||
|
||||
const GSNContextMock = artifacts.require('GSNContextMock');
|
||||
const ContextMockCaller = artifacts.require('ContextMockCaller');
|
||||
|
||||
const { shouldBehaveLikeRegularContext } = require('./Context.behavior');
|
||||
|
||||
contract('GSNContext', function ([_, deployer, sender, newRelayHub]) {
|
||||
beforeEach(async function () {
|
||||
this.context = await GSNContextMock.new();
|
||||
this.caller = await ContextMockCaller.new();
|
||||
});
|
||||
|
||||
describe('get/set RelayHub', function () {
|
||||
const singletonRelayHub = '0xD216153c06E857cD7f72665E0aF1d7D82172F494';
|
||||
|
||||
it('initially returns the singleton instance address', async function () {
|
||||
expect(await this.context.getRelayHub()).to.equal(singletonRelayHub);
|
||||
});
|
||||
|
||||
it('can be upgraded to a new RelayHub', async function () {
|
||||
const { logs } = await this.context.upgradeRelayHub(newRelayHub);
|
||||
expectEvent.inLogs(logs, 'RelayHubChanged', { oldRelayHub: singletonRelayHub, newRelayHub });
|
||||
});
|
||||
|
||||
it('cannot upgrade to the same RelayHub', async function () {
|
||||
await expectRevert(
|
||||
this.context.upgradeRelayHub(singletonRelayHub),
|
||||
'GSNContext: new RelayHub is the current one'
|
||||
);
|
||||
});
|
||||
|
||||
it('cannot upgrade to the zero address', async function () {
|
||||
await expectRevert(this.context.upgradeRelayHub(ZERO_ADDRESS), 'GSNContext: new RelayHub is the zero address');
|
||||
});
|
||||
|
||||
context('with new RelayHub', function () {
|
||||
beforeEach(async function () {
|
||||
await this.context.upgradeRelayHub(newRelayHub);
|
||||
});
|
||||
|
||||
it('returns the new instance address', async function () {
|
||||
expect(await this.context.getRelayHub()).to.equal(newRelayHub);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('when called directly', function () {
|
||||
shouldBehaveLikeRegularContext(sender);
|
||||
});
|
||||
|
||||
context('when receiving a relayed call', function () {
|
||||
beforeEach(async function () {
|
||||
await gsn.fundRecipient(web3, { recipient: this.context.address });
|
||||
});
|
||||
|
||||
describe('msgSender', function () {
|
||||
it('returns the relayed transaction original sender', async function () {
|
||||
const { tx } = await this.context.msgSender({ from: sender, useGSN: true });
|
||||
await expectEvent.inTransaction(tx, GSNContextMock, 'Sender', { sender });
|
||||
});
|
||||
});
|
||||
|
||||
describe('msgData', function () {
|
||||
it('returns the relayed transaction original data', async function () {
|
||||
const integerValue = new BN('42');
|
||||
const stringValue = 'OpenZeppelin';
|
||||
const callData = this.context.contract.methods.msgData(integerValue.toString(), stringValue).encodeABI();
|
||||
|
||||
// The provider doesn't properly estimate gas for a relayed call, so we need to manually set a higher value
|
||||
const { tx } = await this.context.msgData(integerValue, stringValue, { gas: 1000000, useGSN: true });
|
||||
await expectEvent.inTransaction(tx, GSNContextMock, 'Data', { data: callData, integerValue, stringValue });
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
44
test/GSN/GSNRecipient.test.js
Normal file
44
test/GSN/GSNRecipient.test.js
Normal file
@ -0,0 +1,44 @@
|
||||
const { balance, ether, expectRevert } = require('openzeppelin-test-helpers');
|
||||
const gsn = require('@openzeppelin/gsn-helpers');
|
||||
|
||||
const { expect } = require('chai');
|
||||
|
||||
const GSNRecipientMock = artifacts.require('GSNRecipientMock');
|
||||
|
||||
contract('GSNRecipient', function ([_, payee]) {
|
||||
beforeEach(async function () {
|
||||
this.recipient = await GSNRecipientMock.new();
|
||||
});
|
||||
|
||||
it('returns the RelayHub address address', async function () {
|
||||
expect(await this.recipient.getHubAddr()).to.equal('0xD216153c06E857cD7f72665E0aF1d7D82172F494');
|
||||
});
|
||||
|
||||
it('returns the compatible RelayHub version', async function () {
|
||||
expect(await this.recipient.relayHubVersion()).to.equal('1.0.0');
|
||||
});
|
||||
|
||||
context('with deposited funds', async function () {
|
||||
const amount = ether('1');
|
||||
|
||||
beforeEach(async function () {
|
||||
await gsn.fundRecipient(web3, { recipient: this.recipient.address, amount });
|
||||
});
|
||||
|
||||
it('funds can be withdrawn', async function () {
|
||||
const balanceTracker = await balance.tracker(payee);
|
||||
await this.recipient.withdrawDeposits(amount, payee);
|
||||
expect(await balanceTracker.delta()).to.be.bignumber.equal(amount);
|
||||
});
|
||||
|
||||
it('partial funds can be withdrawn', async function () {
|
||||
const balanceTracker = await balance.tracker(payee);
|
||||
await this.recipient.withdrawDeposits(amount.divn(2), payee);
|
||||
expect(await balanceTracker.delta()).to.be.bignumber.equal(amount.divn(2));
|
||||
});
|
||||
|
||||
it('reverts on overwithdrawals', async function () {
|
||||
await expectRevert(this.recipient.withdrawDeposits(amount.addn(1), payee), 'insufficient funds');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user