Skip to content

Commit

Permalink
Complete initial test coverage of extended jurisdiction
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Dec 2, 2018
1 parent 6df321c commit d43639e
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 24 deletions.
41 changes: 20 additions & 21 deletions contracts/ExtendedJurisdiction.sol
Original file line number Diff line number Diff line change
Expand Up @@ -690,30 +690,29 @@ contract ExtendedJurisdiction is Ownable, Pausable, AttributeRegistryInterface,
_recoverableFunds = _recoverableFunds.add(stake.sub(transactionCost));
}

// emit an event for the payment of the transaction rebate
emit TransactionRebatePaid(
tx.origin,
refundAddress,
attributeTypeID,
transactionCost
);

// refund the cost of the transaction to the trasaction submitter
if (tx.origin.send(transactionCost)) {
emit TransactionRebatePaid(
tx.origin,
refundAddress,
attributeTypeID,
transactionCost
);
} else {
_recoverableFunds = _recoverableFunds.add(transactionCost);
}
tx.origin.transfer(transactionCost);

// otherwise, allocate entire stake to partially refunding the transaction
} else if (stake > 0 && address(this).balance >= stake) {
if (tx.origin.send(stake)) {
emit TransactionRebatePaid(
tx.origin,
refundAddress,
attributeTypeID,
stake
);
} else {
_recoverableFunds = _recoverableFunds.add(stake);
}
} else {
// emit an event for the payment of the partial transaction rebate
emit TransactionRebatePaid(
tx.origin,
refundAddress,
attributeTypeID,
stake
);

// refund the partial cost of the transaction to trasaction submitter
tx.origin.transfer(stake);
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions contracts/mock/test/PaymentRejector.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,30 @@ contract PaymentRejector {
function setValidatorSigningKey(address newKey) public {
_jurisdiction.setValidatorSigningKey(newKey);
}

function addAttributeFor(
address account,
uint256 attributeTypeID,
uint256 value,
uint256 validatorFee,
bytes signature
) public payable {
_jurisdiction.addAttributeFor.value(msg.value)(
account,
attributeTypeID,
value,
validatorFee,
signature
);
}

function removeAttributeFor(
address account,
uint256 attributeTypeID
) public {
_jurisdiction.removeAttributeFor(
account,
attributeTypeID
);
}
}
44 changes: 43 additions & 1 deletion scripts/test/testExtended.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,19 @@ module.exports = {test: async function (provider, testingContext) {
)
passed++
})


await Jurisdiction.methods.recoverableTokens(TPLToken.options.address).call({
from: address,
gas: 5000000,
gasPrice: 10 ** 1
}).then(tokens => {
assert.strictEqual(tokens, '0')
console.log(
' ✓ - recoverable token balance of registry is initially zero'
)
passed++
})

await TPLToken.methods.balanceOf(address).call({
from: address,
gas: 5000000,
Expand All @@ -271,6 +283,21 @@ module.exports = {test: async function (provider, testingContext) {
passed++
})

await Jurisdiction.methods.recoverTokens(
TPLToken.options.address,
inattributedAddress,
0
).call({
from: address,
gas: 5000000,
gasPrice: 10 ** 1
}).catch(error => {
console.log(
' ✓ - tokens cannot be recovered before attribute is assigned'
)
passed++
})

// create stub objects that will be used for setting and comparing values
const validator = {
address: validatorAddress,
Expand Down Expand Up @@ -891,6 +918,21 @@ module.exports = {test: async function (provider, testingContext) {
failed++
})

await Jurisdiction.methods.recoverTokens(
TPLToken.options.address,
attributedAddress,
0
).call({
from: address,
gas: 5000000,
gasPrice: 10 ** 1
}).then(receipt => {
console.log(
' ✓ - tokens can be "recovered" once attribute is assigned'
)
passed++
})

await Jurisdiction.methods.issueAttribute(
attributedAddress,
attribute.attributeId,
Expand Down
192 changes: 190 additions & 2 deletions scripts/test/testExtendedPayments.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = {test: async function (provider, testingContext) {
from: from,
value: value,
gas: gas,
gasPrice:gasPrice
gasPrice: gasPrice
}).catch(error => {
succeeded = false
})
Expand Down Expand Up @@ -818,7 +818,7 @@ module.exports = {test: async function (provider, testingContext) {
attributeValue
)
await runTest(
'operators can issue signed attribute that requires a stake',
'operators can issue signed attribute that requires a stake (1)',
Jurisdiction,
'addAttributeFor',
'send',
Expand Down Expand Up @@ -856,6 +856,194 @@ module.exports = {test: async function (provider, testingContext) {
minimumRequiredStake
)

await runTest(
'jurisdiction owner can then revoke the attribute (2)',
Jurisdiction,
'revokeAttribute',
'send',
[attributedAddress, attributeId]
)

signature = await signValidation(
validatorAddress,
Jurisdiction.options.address,
attributedAddress,
attributedAddress, // operator
minimumRequiredStake + 1, // stake + jurisdiction fee + validator fee
0,
attributeId,
attributeValue
)
await runTest(
'operators can issue signed attribute that requires a stake (2)',
Jurisdiction,
'addAttributeFor',
'send',
[
attributedAddress,
attributeId,
attributeValue,
0,
signature
],
true,
value => {
assert.strictEqual(
value.events.StakeAllocated.returnValues.staker,
attributedAddress
)
assert.strictEqual(
value.events.AttributeAdded.returnValues.validator,
validatorAddress
)
assert.strictEqual(
value.events.AttributeAdded.returnValues.attributee,
attributedAddress
)
assert.strictEqual(
value.events.AttributeAdded.returnValues.attributeTypeID,
attributeId.toString()
)
assert.strictEqual(
value.events.AttributeAdded.returnValues.attributeValue,
attributeValue.toString()
)
},
attributedAddress,
minimumRequiredStake + 1
)

await runTest(
'attribute operator can revoke the attribute (1)',
Jurisdiction,
'removeAttributeFor',
'send',
[attributedAddress, attributeId],
true,
value => {
assert.strictEqual(
value.events.AttributeRemoved.returnValues.attributeTypeID,
attributeId.toString()
)
},
attributedAddress
)

signature = await signValidation(
validatorAddress,
Jurisdiction.options.address,
attributedAddress,
PaymentRejector.options.address, // operator
minimumRequiredStake, // stake + jurisdiction fee + validator fee
0,
attributeId,
attributeValue
)
await runTest(
'operators can issue signed attribute that requires a stake (3)',
PaymentRejector,
'addAttributeFor',
'send',
[
attributedAddress,
attributeId,
attributeValue,
0,
signature
],
true,
value => {},
attributedAddress,
minimumRequiredStake
)

await runTest(
'attribute operator can revoke the attribute (2)',
PaymentRejector,
'removeAttributeFor',
'send',
[attributedAddress, attributeId],
true,
value => {},
attributedAddress
)

signature = await signValidation(
validatorAddress,
Jurisdiction.options.address,
attributedAddress,
PaymentRejector.options.address, // operator
minimumRequiredStake + 1, // stake + jurisdiction fee + validator fee
0,
attributeId,
attributeValue
)
await runTest(
'operators can issue signed attribute that requires a stake (4)',
PaymentRejector,
'addAttributeFor',
'send',
[
attributedAddress,
attributeId,
attributeValue,
0,
signature
],
true,
value => {},
attributedAddress,
minimumRequiredStake + 1
)

await runTest(
'attribute holder can revoke the attribute (5)',
Jurisdiction,
'removeAttribute',
'send',
[attributeId],
true,
value => {},
attributedAddress
)

// stake > tx rebate
signature = await signValidation(
validatorAddress,
Jurisdiction.options.address,
attributedAddress,
PaymentRejector.options.address, // operator
377001,
0,
attributeId,
attributeValue
)
await runTest(
'operators can issue signed attribute that requires a stake (5)',
PaymentRejector,
'addAttributeFor',
'send',
[
attributedAddress,
attributeId,
attributeValue,
0,
signature
],
true,
value => {},
attributedAddress,
377001
)

await runTest(
'jurisdiction owner can then revoke the attribute (3)',
Jurisdiction,
'revokeAttribute',
'send',
[attributedAddress, attributeId]
)

console.log(
`completed ${passed + failed} tests with ${failed} ` +
`failure${failed === 1 ? '' : 's'}.`
Expand Down

0 comments on commit d43639e

Please sign in to comment.