Skip to content

Commit

Permalink
feat: extend PostageBatch type with new properties (#350)
Browse files Browse the repository at this point in the history
* feat: extend PostageBatch type with new properties

* test: add postage batch shape test

* build: update bee versions to 1.0.0-f5a460b-dirty

* style: add trailing comma

* build: update blockchain_version to 1.2.0

* build: change bee-factory branch

* fix(test): change stamp amount type to string

* style: add trailing comma

* test: do not skip reupload tests

* build: bump image version

* build: bump image version

* build: bump image version

* test: skip reupload tests

* test: add --runInBand experimentally and temporarily

* test: add --maxWorkers=1 and --no-cache test flags

* test: remove temporary flags

* test: add sleep to the beginning of tests

* Revert "test: add sleep to the beginning of tests"

This reverts commit b8d5f3c.

* feat: add immutable header to postage batch creation

* test: add test for immutable flag

* fix: check for !== undefined for immutableFlag

* test(fix): create stamps sequentially

* test(fix): increase postage batch timeout

* ci: change to latest bee
  • Loading branch information
Cafe137 committed Jun 17, 2021
1 parent a083a1b commit 7695e27
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 28 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ on:
- '**'

env:
BEE_VERSION: '0.6.3-388256b-dirty'
BLOCKCHAIN_VERSION: '1.1.1'
BEE_VERSION: '1.0.0-3a92ef5-dirty'
BLOCKCHAIN_VERSION: '1.2.0'
BEE_ENV_PREFIX: 'swarm-test'
BEE_IMAGE_PREFIX: 'docker.pkg.github.com/ethersphere/bee-factory'
COMMIT_VERSION_TAG: 'false'
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
# Setup Bee environment
- name: Start Bee Factory environment
run: |
git clone --depth=1 https://github.com/ethersphere/bee-factory.git
git clone -b bee-1.0.0-rc2 --depth=1 https://github.com/ethersphere/bee-factory.git
chmod +x -R ./bee-factory/scripts
./bee-factory/scripts/environment.sh start --detach --workers=$WORKERS
Expand Down Expand Up @@ -97,7 +97,7 @@ jobs:
# Setup Bee environment
- name: Start Bee Factory environment
run: |
git clone --depth=1 https://github.com/ethersphere/bee-factory.git
git clone -b bee-1.0.0-rc2 --depth=1 https://github.com/ethersphere/bee-factory.git
chmod +x -R ./bee-factory/scripts
./bee-factory/scripts/environment.sh start --detach --workers=$WORKERS
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@
"engines": {
"node": ">=12.0.0",
"npm": ">=6.0.0",
"bee": "0.6.3-388256b-dirty"
"bee": "1.0.0-3a92ef5-dirty"
}
}
5 changes: 5 additions & 0 deletions src/bee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { wrapBytesWithHelpers } from './utils/bytes'
import {
assertAddressPrefix,
assertBatchId,
assertBoolean,
assertCollectionUploadOptions,
assertData,
assertFileData,
Expand Down Expand Up @@ -676,6 +677,10 @@ export class Bee {
assertNonNegativeInteger(options.gasPrice)
}

if (options?.immutableFlag !== undefined) {
assertBoolean(options.immutableFlag)
}

return stamps.createPostageBatch(this.url, amount, depth, options)
}

Expand Down
4 changes: 4 additions & 0 deletions src/modules/stamps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export async function createPostageBatch(
headers['gas-price'] = options.gasPrice.toString()
}

if (options?.immutableFlag !== undefined) {
headers.immutable = String(options.immutableFlag)
}

const response = await safeAxios<CreateStampResponse>({
method: 'post',
url: `${url}${STAMPS_ENDPOINT}/${amount}/${depth}`,
Expand Down
18 changes: 13 additions & 5 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { BeeError } from '../utils/error'
import type { AxiosRequestConfig } from 'axios'
import { HexString } from '../utils/hex'
import { Bytes } from '../utils/bytes'
import { EthAddress, HexEthAddress } from '../utils/eth'
import { Identifier, SingleOwnerChunk } from '../chunk/soc'
import { ChunkReference, FeedUploadOptions } from '../feed'
import { FeedType } from '../feed/type'
import { FeedUpdateOptions, FetchFeedUpdateResponse } from '../modules/feed'
import { ChunkReference, FeedUploadOptions } from '../feed'
import { Bytes } from '../utils/bytes'
import { BeeError } from '../utils/error'
import { EthAddress, HexEthAddress } from '../utils/eth'
import { HexString } from '../utils/hex'
export * from './debug'

export interface Dictionary<T> {
Expand Down Expand Up @@ -245,6 +245,13 @@ export interface SOCWriter extends SOCReader {
export interface PostageBatch {
batchID: BatchId
utilization: number
usable: boolean
label: '' | string
depth: number
amount: string
bucketDepth: number
blockNumber: number
immutableFlag: boolean
}

/**
Expand All @@ -253,6 +260,7 @@ export interface PostageBatch {
export interface PostageBatchOptions {
label?: string
gasPrice?: NumberString
immutableFlag?: boolean
}

/**
Expand Down
14 changes: 9 additions & 5 deletions src/utils/type.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { Readable } from 'stream'
import {
Address,
ADDRESS_HEX_LENGTH,
AddressPrefix,
BATCH_ID_HEX_LENGTH,
ADDRESS_HEX_LENGTH,
BatchId,
BATCH_ID_HEX_LENGTH,
CollectionUploadOptions,
ENCRYPTED_REFERENCE_HEX_LENGTH,
FileUploadOptions,
NumberString,
PssMessageHandler,
PUBKEY_HEX_LENGTH,
PublicKey,
Reference,
REFERENCE_HEX_LENGTH,
Tag,
UploadOptions,
NumberString,
} from '../types'
import { assertHexString } from './hex'
import { BeeArgumentError } from './error'
import { Readable } from 'stream'
import { isFile } from './file'
import { assertHexString } from './hex'

export function isReadable(entry: unknown): entry is Readable {
return (
Expand All @@ -45,6 +45,10 @@ export function isInteger(value: unknown): value is number | NumberString {
)
}

export function assertBoolean(value: unknown): asserts value is boolean {
if (value !== true && value !== false) throw new TypeError('value is not boolean')
}

export function assertInteger(value: unknown): asserts value is number | NumberString {
if (!isInteger(value)) throw new TypeError('value is not integer')
}
Expand Down
44 changes: 39 additions & 5 deletions test/integration/bee-class.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Bee, BeeArgumentError, BeeDebug, Collection } from '../../src'

import { makeSigner } from '../../src/chunk/signer'
import { makeSOCAddress, uploadSingleOwnerChunkData } from '../../src/chunk/soc'
import { ChunkReference } from '../../src/feed'
import * as bzz from '../../src/modules/bzz'
import { REFERENCE_HEX_LENGTH } from '../../src/types'
import { makeBytes } from '../../src/utils/bytes'
import { makeEthAddress } from '../../src/utils/eth'
import { bytesToHex, HexString } from '../../src/utils/hex'
import {
beeDebugUrl,
Expand All @@ -21,10 +24,6 @@ import {
testJsonPayload,
tryDeleteChunkFromLocalStorage,
} from '../utils'
import { makeSigner } from '../../src/chunk/signer'
import { makeSOCAddress, uploadSingleOwnerChunkData } from '../../src/chunk/soc'
import { makeEthAddress } from '../../src/utils/eth'
import * as bzz from '../../src/modules/bzz'

commonMatchers()

Expand Down Expand Up @@ -465,6 +464,41 @@ describe('Bee class', () => {
POSTAGE_BATCH_TIMEOUT,
)

it(
'should have both immutable true and false',
async () => {
await bee.createPostageBatch('1', 17, { immutableFlag: true })
await bee.createPostageBatch('1', 17, { immutableFlag: false })
const allBatches = await bee.getAllPostageBatch()

expect(allBatches.find(batch => batch.immutableFlag === true)).toBeTruthy()
expect(allBatches.find(batch => batch.immutableFlag === false)).toBeTruthy()
},
POSTAGE_BATCH_TIMEOUT * 2,
)

it('should have all properties', async () => {
const allBatches = await bee.getAllPostageBatch()

expect(allBatches.length).toBeGreaterThan(0)

expect(allBatches).toEqual(
expect.arrayContaining([
expect.objectContaining({
batchID: expect.any(String),
utilization: expect.any(Number),
usable: expect.any(Boolean),
label: expect.any(String),
depth: expect.any(Number),
amount: expect.any(String),
bucketDepth: expect.any(Number),
blockNumber: expect.any(Number),
immutableFlag: expect.any(Boolean),
}),
]),
)
})

it('should error with negative amount', async () => {
await expect(bee.createPostageBatch('-1', 17)).rejects.toThrowError(BeeArgumentError)
})
Expand Down
4 changes: 2 additions & 2 deletions test/integration/modules/bzz.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as bzz from '../../../src/modules/bzz'
import * as tag from '../../../src/modules/tag'
import { Collection, ENCRYPTED_REFERENCE_HEX_LENGTH } from '../../../src/types'
import { makeCollectionFromFS } from '../../../src/utils/collection'
import {
beeUrl,
BIG_FILE_TIMEOUT,
Expand All @@ -8,8 +10,6 @@ import {
invalidReference,
randomByteArray,
} from '../../utils'
import { makeCollectionFromFS } from '../../../src/utils/collection'
import * as tag from '../../../src/modules/tag'

const BEE_URL = beeUrl()

Expand Down
21 changes: 21 additions & 0 deletions test/unit/bee-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,14 @@ describe('Bee class', () => {
assertAllIsDone()
})

it('should pass headers if immutable flag is specified', async () => {
createPostageBatchMock('10', '17', undefined, undefined, 'true').reply(201, BATCH_RESPONSE)

const bee = new Bee(MOCK_SERVER_URL)
await expect(bee.createPostageBatch('10', 17, { immutableFlag: true })).resolves.toEqual(BATCH_ID)
assertAllIsDone()
})

it('should throw error if passed wrong gas price input', async () => {
const bee = new Bee(MOCK_SERVER_URL)

Expand All @@ -559,6 +567,19 @@ describe('Bee class', () => {
await expect(bee.createPostageBatch('10', 17, { gasPrice: '-1' })).rejects.toThrow(BeeArgumentError)
})

it('should throw error if passed wrong immutable input', async () => {
const bee = new Bee(MOCK_SERVER_URL)

// @ts-ignore: Input testing
await expect(bee.createPostageBatch('10', 17, { immutableFlag: 'asd' })).rejects.toThrow(TypeError)

// @ts-ignore: Input testing
await expect(bee.createPostageBatch('10', 17, { immutableFlag: -1 })).rejects.toThrow(TypeError)

// @ts-ignore: Input testing
await expect(bee.createPostageBatch('10', 17, { immutableFlag: 'true' })).rejects.toThrow(TypeError)
})

it('should throw error if too small depth', async () => {
const bee = new Bee(MOCK_SERVER_URL)

Expand Down
21 changes: 15 additions & 6 deletions test/unit/nock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import nock from 'nock'
import { HexEthAddress } from '../../src/utils/eth'
import { Reference } from '../../src/types'
import nock, { RequestHeaderMatcher } from 'nock'
import { DEFAULT_FEED_TYPE, FeedType } from '../../src/feed/type'
import { Reference } from '../../src/types'
import { HexEthAddress } from '../../src/utils/eth'

export const MOCK_SERVER_URL = 'http://localhost:12345/'

Expand Down Expand Up @@ -34,14 +34,23 @@ export function createPostageBatchMock(
depth: string,
gasPrice?: string,
label?: string,
immutableFlag?: string,
): nock.Interceptor {
let nockScope: nock.Scope

const reqheaders: Record<string, RequestHeaderMatcher> = {}

if (immutableFlag) {
reqheaders.immutable = immutableFlag
}

if (gasPrice) {
reqheaders['gas-price'] = gasPrice
}

if (immutableFlag || gasPrice) {
nockScope = nock(MOCK_SERVER_URL, {
reqheaders: {
'gas-price': gasPrice,
},
reqheaders,
})
} else {
nockScope = nock(MOCK_SERVER_URL)
Expand Down

0 comments on commit 7695e27

Please sign in to comment.