Skip to content

Commit

Permalink
chore(adapter.test.js): add adapter::applynewtee test
Browse files Browse the repository at this point in the history
  • Loading branch information
gitmp01 committed Nov 10, 2024
1 parent c5cb265 commit 5112785
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
55 changes: 44 additions & 11 deletions cpp/test/adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const {
Protocols,
ProofcastEventAttestator,
} = require('@pnetwork/event-attestator')
const { TimePointSec } = require('@wharfkit/antelope')

const TEE_ADDRESS_CHANGE_GRACE_PERIOD_MS = 172800 * 1000
const TABLE_STORAGE = 'storage'
const TABLE_TEE = 'tee'

Expand Down Expand Up @@ -84,6 +86,15 @@ describe('Adapter tests', () => {
chainId: Chains(Protocols.Evm).Mainnet,
})

const NULL_KEY = 'PUB_K1_11111111111111111111111111111111149Mr2R'
const anotherAttestation = 'deadc0de'
const anotherEventAttestator = new ProofcastEventAttestator()
const anotherPublicKey = fromEthersPublicKey(
anotherEventAttestator.signingKey.compressedPublicKey,
)
const teePubKey = fromEthersPublicKey(evmEA.signingKey.compressedPublicKey)
const attestation = ''

before(() => {
blockchain.createAccounts(user, evil, issuer, bridge, recipient, feemanager)

Expand Down Expand Up @@ -357,8 +368,6 @@ describe('Adapter tests', () => {
})

describe('adapter::settee', () => {
const teePubKey = fromEthersPublicKey(evmEA.signingKey.compressedPublicKey)
const attestation = ''
it('Should throw if called by not authorized account', async () => {
const action = adapter.contract.actions
.settee([teePubKey, attestation])
Expand All @@ -372,27 +381,21 @@ describe('Adapter tests', () => {
.settee([teePubKey, attestation])
.send(active(adapter.account))

const tee = getSingletonInstance(adapter.contract, 'tee')
const tee = getSingletonInstance(adapter.contract, TABLE_TEE)

const nullKey = 'PUB_K1_11111111111111111111111111111111149Mr2R'
expect(tee.key).to.be.equal(teePubKey.toString())
expect(tee.updating_key).to.be.equal(nullKey)
expect(tee.updating_key).to.be.equal(NULL_KEY)
expect(tee.attestation).to.be.equal(attestation)
expect(tee.updating_attestation).to.be.equal('')
expect(tee.change_grace_threshold).to.be.equal(0)
})

it('Should wait and set a grace time period when a new tee is set', async () => {
const anotherAttestation = 'deadc0de'
const anotherEventAttestator = new ProofcastEventAttestator()
const anotherPublicKey = fromEthersPublicKey(
anotherEventAttestator.signingKey.compressedPublicKey,
)
await adapter.contract.actions
.settee([anotherPublicKey, anotherAttestation])
.send(active(adapter.account))

const tee = getSingletonInstance(adapter.contract, 'tee')
const tee = getSingletonInstance(adapter.contract, TABLE_TEE)

expect(tee.key).to.be.equal(teePubKey.toString())
expect(tee.updating_key).to.be.equal(anotherPublicKey.toString())
Expand All @@ -402,6 +405,36 @@ describe('Adapter tests', () => {
})
})

describe('adapter::applytee', () => {
it('Should reject when calling the action before the grace period elapsed', async () => {
const action = adapter.contract.actions
.applynewtee([])
.send(active(adapter.account))

await expectToThrow(action, errors.GRACE_PERIOD_NOT_ELAPSED)
})

it('Should be able to apply the new tee key after the grace period', async () => {
const timestamp = TimePointSec.fromMilliseconds(
Date.now() + TEE_ADDRESS_CHANGE_GRACE_PERIOD_MS,
)

blockchain.setTime(timestamp)

await adapter.contract.actions
.applynewtee([])
.send(active(adapter.account))

const tee = getSingletonInstance(adapter.contract, TABLE_TEE)

expect(tee.key).to.be.equal(anotherPublicKey.toString())
expect(tee.updating_key).to.be.equal(NULL_KEY)
expect(tee.attestation).to.be.equal(anotherAttestation)
expect(tee.updating_attestation).to.be.equal('')
expect(tee.change_grace_threshold).to.be.equal(0)
})
})

describe('adapter::setorigin', () => {
const originChainId = no0x(bytes32(Chains(Protocols.Evm).Mainnet))

Expand Down
3 changes: 3 additions & 0 deletions cpp/test/utils/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const CONTRACT_ALREADY_INITIALIZED = eosio_assert(
'contract already initialized',
)

const GRACE_PERIOD_NOT_ELAPSED = eosio_assert('grace period not elapsed')

module.exports = {
AUTH_MISSING,
SYMBOL_NOT_FOUND,
Expand Down Expand Up @@ -108,4 +110,5 @@ module.exports = {
USER_DATA_RECORD_NOT_FOUND,
INVALID_PAYLOAD,
CONTRACT_ALREADY_INITIALIZED,
GRACE_PERIOD_NOT_ELAPSED,
}

0 comments on commit 5112785

Please sign in to comment.