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

refactor: sdk rhp #519

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
9 changes: 7 additions & 2 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ runs:
registry-url: https://registry.npmjs.org
- uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.21.7'
- uses: acifani/setup-tinygo@v2
with:
tinygo-version: '0.30.0'
Expand All @@ -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
4 changes: 2 additions & 2 deletions go.work
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
go 1.20
go 1.21.7

use (
./
./hostd
./renterd
./walletd
./sdk
./walletd
)
6 changes: 6 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
go.etcd.io/bbolt v1.3.8/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
go.etcd.io/gofail v0.1.0/go.mod h1:VZBCXYGZhHAinaBiiqYvuDynvahNsAyLFwB3kEHKz1M=
go.sia.tech/mux v1.2.0/go.mod h1:Yyo6wZelOYTyvrHmJZ6aQfRoer3o4xyKQ4NmQLJrBSo=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
2 changes: 1 addition & 1 deletion libs/sdk/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": ["plugin:@nx/react", "../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"ignorePatterns": ["!**/*", "src/utils/wasm_exec.js"],
"rules": {
"@nx/dependency-checks": [
"error",
Expand Down
1 change: 0 additions & 1 deletion libs/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"description": "SDK for interacting directly with the Sia network from browsers and web clients.",
"version": "0.0.2",
"license": "MIT",
"dependencies": {},
"devDependencies": {
"undici": "5.28.3"
},
Expand Down
4 changes: 3 additions & 1 deletion libs/sdk/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"cache": true,
"options": {
"commands": [
"tinygo build -o libs/sdk/src/wasm/resources/sdk.wasm -target wasm ./sdk"
"cp $(go env GOROOT)/misc/wasm/wasm_exec.js libs/sdk/src/utils/wasm_exec.js",
"GOOS=js GOARCH=wasm go build -o libs/sdk/src/resources/sdk.wasm ./sdk"
]
}
},
Expand Down Expand Up @@ -50,6 +51,7 @@
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/libs/sdk"],
"dependsOn": ["compile"],
"options": {
"jestConfig": "libs/sdk/jest.config.ts"
}
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
Loading
Loading