Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper function for checking whether an AFS has been committed to #168

Merged
merged 6 commits into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ The contracts in this repository are currently deployed on [Ara Privatenet](http
* [async registry.deployNewStandard(opts)](#newstandard)

### Library

* [async library.getLibrary(requesterDid)](#getlibrary)
* [async library.getLibrarySize(requesterDid)](#librarysize)
* [async library.getLibraryItem(opts)](#libraryitem)
* [async library.hasPurchased(opts)](#haspurchased)

### Rewards

* [async rewards.submit(opts)](#submit)
* [async rewards.allocate(opts)](#allocate)
* [async rewards.redeem(opts)](#redeem)
Expand Down
34 changes: 33 additions & 1 deletion storage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
const { web3: { tx, call, isAddress } } = require('ara-util')
const { abi } = require('./build/contracts/AFS.json')
const isBuffer = require('is-buffer')

const {
web3: {
isAddress,
call,
tx
}
} = require('ara-util')

async function read(opts) {
_validateOpts(opts)
const { address, fileIndex, offset } = opts
Expand Down Expand Up @@ -91,6 +98,30 @@ async function hasBuffer(opts) {
})
}

async function isEmpty(address = '') {
if (!address || 'string' !== typeof address) {
throw new TypeError('Expecting address to be non-empty string')
}

if (!isAddress(address)) {
throw new Error(`${address} is not a valid Ethereum address`)
}

let empty = true
try {
// only need to check first header
const buf = await read({
fileIndex: 0,
offset: 0,
address
})
empty = null === buf
} catch (err) {
throw err
}
return empty
}

function _validateOpts(opts) {
if (!opts || 'object' !== typeof opts) {
throw new TypeError('Expecting opts to be of type object')
Expand All @@ -107,6 +138,7 @@ function _validateOpts(opts) {

module.exports = {
hasBuffer,
isEmpty,
write,
read
}
16 changes: 16 additions & 0 deletions test/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ test.serial("hasBuffer(opts) buffer does not exist", async (t) => {
t.true(false === Boolean(exists))
})

test.serial("isEmpty(did) empty", async (t) => {
const { address } = t.context
t.is(true, await storage.isEmpty(address))
})

test.serial("hasBuffer(opts) buffer exists", async (t) => {
const { address } = t.context

Expand Down Expand Up @@ -196,6 +201,11 @@ test.serial("hasBuffer(opts) buffer exists", async (t) => {
t.true(true === Boolean(exists))
})

test.serial("isEmpty(did) not empty", async (t) => {
const { address } = t.context
t.is(false, await storage.isEmpty(address))
})

test.serial("write(opts) read(opts) append", async (t) => {
const { address } = t.context

Expand Down Expand Up @@ -235,3 +245,9 @@ test.serial("write(opts) read(opts) append", async (t) => {
t.is(result, msBuffer)
})

test.serial("isEmpty(did) invalid did", async (t) => {
await t.throwsAsync(storage.isEmpty())
await t.throwsAsync(storage.isEmpty({ }))
await t.throwsAsync(storage.isEmpty(null))
await t.throwsAsync(storage.isEmpty('0x1234'))
})