Skip to content

Commit

Permalink
refactor: sdk rhp
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Feb 29, 2024
1 parent 329d4a2 commit ea37241
Show file tree
Hide file tree
Showing 39 changed files with 962 additions and 529 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-cougars-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@siafoundation/sdk': minor
---

Updated SDK to latest core changes, updated structure.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
GITHUB_TOKEN=secret_token
NOTION_TOKEN=secret_token
ASSETS=/User/bob/web/assets

# Make Go use UTC for time formatting
TZ=UTC
7 changes: 6 additions & 1 deletion .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ runs:
# If source files changed but packages didn't, rebuild from a prior cache.
restore-keys: |
${{ runner.os }}-${{ inputs.node_version }}-${{ hashFiles('**/package-lock.json') }}-
- name: Install
- name: Install JavaScript dependencies
# could do this since its a ci, but it force rebuilds node_modules
# run: npm ci
run: npm install
shell: bash
- name: Install Go dependencies
run: |
go mod tidy
go mod download
shell: bash
5 changes: 4 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ jobs:
- name: Commit lint
shell: bash
run: npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }}
- name: Lint
- name: Lint TypeScript
shell: bash
run: npx nx affected --target=lint --parallel=5
- name: Lint Go
uses: golangci/golangci-lint-action@v3
with:
skip-cache: true
- name: Compile
shell: bash
run: npx nx affected --target=compile --parallel=5
- name: Test
shell: bash
run: npx nx affected --target=test --parallel=5
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/release-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
- name: Lint
shell: bash
run: npx nx run-many --target=lint --all --parallel=5
- name: Compile
shell: bash
run: npx nx run-many --target=compile --all --parallel=5
- name: Test
shell: bash
run: npx nx run-many --target=test --all --parallel=5
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"cache": true,
"options": {
"commands": [
"tinygo build -o libs/sdk/src/wasm/resources/sdk.wasm -target wasm ./sdk"
"tinygo build -o libs/sdk/src/resources/sdk.wasm -target wasm ./sdk"
]
}
},
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion libs/sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './init'
export * from './types'
export * from './wasm'
7 changes: 7 additions & 0 deletions libs/sdk/src/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getSDK } from './sdk'
import { initWASM } from './wasm'

export async function initSDK() {
await initWASM()
return getSDK()
}
7 changes: 7 additions & 0 deletions libs/sdk/src/initTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getSDK } from './sdk'
import { initWASMTest } from './wasmTest'

export async function initSDKTest() {
await initWASMTest()
return getSDK()
}
14 changes: 9 additions & 5 deletions libs/sdk/src/js/encoder.ts → libs/sdk/src/legacy/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,24 @@ export function decodeString(d: Decoder): string {
return s
}

export function encodeCurrency(e: Encoder, c: bigint) {
export function encodeCurrency(e: Encoder, c: string) {
// currency is 128 bits, little endian
e.dataView.setBigUint64(e.offset, c & BigInt('0xFFFFFFFFFFFFFFFF'), true)
e.dataView.setBigUint64(
e.offset,
BigInt(c) & BigInt('0xFFFFFFFFFFFFFFFF'),
true
)
e.offset += 8
e.dataView.setBigUint64(e.offset, c >> BigInt(64), true)
e.dataView.setBigUint64(e.offset, BigInt(c) >> BigInt(64), true)
e.offset += 8
}

export function decodeCurrency(d: Decoder): bigint {
export function decodeCurrency(d: Decoder): string {
const lo = d.dataView.getBigUint64(d.offset, true)
d.offset += 8
const hi = d.dataView.getBigUint64(d.offset, true)
d.offset += 8
return (hi << BigInt(64)) | lo
return String((hi << BigInt(64)) | lo)
}

export function encodeAddress(e: Encoder, a: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import {
decodeHostSettings,
} from './encoding'
import { newEncoder, newDecoder } from './encoder'
import { HostPrices, HostSettings } from '../types'
import { HostPrices, HostSettings } from './types'

describe('encoding', () => {
it('encodeHostPrices', () => {
const hostPrices: HostPrices = {
contractPrice: BigInt(1000000000),
collateral: BigInt(2000000000),
storagePrice: BigInt(3000000000),
ingressPrice: BigInt(4000000000),
egressPrice: BigInt(5000000000),
contractPrice: '1000000000',
collateral: '2000000000',
storagePrice: '3000000000',
ingressPrice: '4000000000',
egressPrice: '5000000000',
tipHeight: 450_000,
validUntil: '2022-12-31T00:00:00.000Z',
signature:
Expand All @@ -30,26 +30,26 @@ describe('encoding', () => {

it('encodeHostSettings', () => {
const prices: HostPrices = {
contractPrice: BigInt(1000000000),
collateral: BigInt(2000000000),
storagePrice: BigInt(3000000000),
ingressPrice: BigInt(4000000000),
egressPrice: BigInt(5000000000),
contractPrice: '1000000000',
collateral: '2000000000',
storagePrice: '3000000000',
ingressPrice: '4000000000',
egressPrice: '5000000000',
tipHeight: 450_000,
validUntil: '2022-12-31T00:00:00.000Z',
signature:
'abcd567890123456789012345678901234567890123456789012345678901234',
}
const hostSettings: HostSettings = {
version: '123',
version: new Uint8Array([1, 2, 3]),
netAddresses: [
{ protocol: 'protocol1', address: 'address1longer' },
{ protocol: 'protocol2longer', address: 'address2' },
],
// 32 bytes
walletAddress: '12345678901234567890123456789012',
acceptingContracts: true,
maxCollateral: BigInt(1000000000),
maxCollateral: '1000000000',
maxDuration: 100,
remainingStorage: 100,
totalStorage: 100,
Expand Down
10 changes: 5 additions & 5 deletions libs/sdk/src/js/encoding.ts → libs/sdk/src/legacy/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import {
decodeCurrency,
encodeAddress,
encodeBoolean,
encodeBytes,
encodeString,
encodeUint64,
encodeLengthPrefix,
decodeAddress,
decodeBoolean,
decodeBytes,
decodeString,
decodeUint64,
decodeLengthPrefix,
Expand All @@ -19,8 +17,10 @@ import {
decodeTime,
Encoder,
Decoder,
decodeUint8Array,
encodeUint8Array,
} from './encoder'
import { HostPrices, HostSettings, NetAddress } from '../types'
import { HostPrices, HostSettings, NetAddress } from './types'

export function encodeHostPrices(e: Encoder, hostPrices: HostPrices) {
encodeCurrency(e, hostPrices.contractPrice)
Expand Down Expand Up @@ -69,7 +69,7 @@ export function decodeNetAddress(d: Decoder): NetAddress {
}

export function encodeHostSettings(e: Encoder, hostSettings: HostSettings) {
encodeBytes(e, hostSettings.version)
encodeUint8Array(e, hostSettings.version)
encodeLengthPrefix(e, hostSettings.netAddresses.length)
for (let i = 0; i < hostSettings.netAddresses.length; i++) {
encodeNetAddress(e, hostSettings.netAddresses[i])
Expand All @@ -84,7 +84,7 @@ export function encodeHostSettings(e: Encoder, hostSettings: HostSettings) {
}

export function decodeHostSettings(d: Decoder): HostSettings {
const version = decodeBytes(d, 3)
const version = decodeUint8Array(d, 3)
const netAddresses = []
const length = decodeLengthPrefix(d)
for (let i = 0; i < length; i++) {
Expand Down
File renamed without changes.
16 changes: 8 additions & 8 deletions libs/sdk/src/js/rpc.spec.ts → libs/sdk/src/legacy/rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
RPCSettingsResponse,
RPCWriteSectorRequest,
RPCWriteSectorResponse,
} from '../types'
} from './types'
import {
decodeRpcRequestReadSector,
decodeRpcRequestSettings,
Expand All @@ -26,26 +26,26 @@ import {
} from './rpc'

const prices: HostPrices = {
contractPrice: BigInt(1000000000),
collateral: BigInt(2000000000),
storagePrice: BigInt(3000000000),
ingressPrice: BigInt(4000000000),
egressPrice: BigInt(5000000000),
contractPrice: '1000000000',
collateral: '2000000000',
storagePrice: '3000000000',
ingressPrice: '4000000000',
egressPrice: '5000000000',
tipHeight: 450_000,
validUntil: '2022-12-31T00:00:00.000Z',
signature: 'abcd567890123456789012345678901234567890123456789012345678901234',
}

const hostSettings: HostSettings = {
version: '123',
version: new Uint8Array([1, 2, 3]),
netAddresses: [
{ protocol: 'protocol1', address: 'address1longer' },
{ protocol: 'protocol2longer', address: 'address2' },
],
// 32 bytes
walletAddress: '12345678901234567890123456789012',
acceptingContracts: true,
maxCollateral: BigInt(1000000000),
maxCollateral: '1000000000',
maxDuration: 100,
remainingStorage: 100,
totalStorage: 100,
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk/src/js/rpc.ts → libs/sdk/src/legacy/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
RPCSettingsResponse,
RPCWriteSectorRequest,
RPCWriteSectorResponse,
} from '../types'
} from './types'

// NOTE: This JavaScript RPC and encoding implementations is not currently used
// and may be incomplete or incorrect. It was written as a comparison to the WASM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
RPCReadSector,
RPCWriteSector,
RPCSettings,
} from '../types'
} from './types'

export class WebTransportClient {
private transport!: WebTransport
Expand Down
79 changes: 79 additions & 0 deletions libs/sdk/src/legacy/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
type Currency = string
type Signature = string
type Address = string
type Hash256 = string // 32 bytes
type AccountID = string // 16 bytes

export type HostPrices = {
contractPrice: Currency
collateral: Currency
storagePrice: Currency
ingressPrice: Currency
egressPrice: Currency
tipHeight: number
validUntil: string
signature: Signature
}

export type NetAddress = {
protocol: string
address: string
}

export type HostSettings = {
version: Uint8Array // 3 bytes
netAddresses: NetAddress[]
walletAddress: Address // 32 bytes
acceptingContracts: boolean
maxCollateral: Currency
maxDuration: number
remainingStorage: number
totalStorage: number
prices: HostPrices
}

export type RPCSettingsRequest = void

export type RPCSettingsResponse = {
settings: HostSettings
}

export type RPCSettings = {
request: RPCSettingsRequest
response: RPCSettingsResponse
}

export type RPCReadSectorRequest = {
prices: HostPrices
accountId: AccountID // 16 bytes
root: Hash256 // 32 bytes - types.Hash256
offset: number // uint64
length: number // uint64
}

export type RPCReadSectorResponse = {
proof: Hash256[] // 32 bytes each - types.Hash256
sector: Uint8Array // []byte
}

export type RPCReadSector = {
request: RPCReadSectorRequest
response: RPCReadSectorResponse
}

export type RPCWriteSectorRequest = {
prices: HostPrices
accountId: AccountID // 16 bytes
sector: Uint8Array // []byte - extended to SectorSize by host
}

export type RPCWriteSectorResponse = {
root: Hash256 // 32 bytes - types.Hash256
}

export type RPCWriteSector = {
request: RPCWriteSectorRequest
response: RPCWriteSectorResponse
}

export type RPC = RPCSettings | RPCReadSector | RPCWriteSector
File renamed without changes.
11 changes: 11 additions & 0 deletions libs/sdk/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { WebTransportClient } from './transport'
import { WASM } from './types'

export function getSDK() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const wasm = (global as any).sia as WASM
return {
wasm,
WebTransportClient,
}
}
Loading

0 comments on commit ea37241

Please sign in to comment.