Skip to content

Commit

Permalink
[NayNay] New SDK RC integration (#262)
Browse files Browse the repository at this point in the history
* wip: integrating new sdk with new core changes

* wip: updating tests with four-nodes

* [NayNay] Updating CLI with new sdk rc

- updated CLI with new sdk rc, and made changes to reflect the changes from core/sdk
- updated network type defualt to four-nodes
- added jump-start network to the initialization of entropy for both cli and tui
- updated all tests to reflect changes

* updated hosts file for CI

* updated faucet test to refine setup faucet mwethod

* updated rc

* trying something here...

* removing logs

* fixed tests due to faulty config, added new dev choice to manually jump start network

* added loader to jumpstart

* created new option for endpoint in tui

* Mixmix/new sdk rc (#263)

* exract "fundAccount" from tests

* extraaact

* tidy

* Update tests/faucet.test.ts

Co-authored-by: Nayyir Jutha <nayyir.jutha@gmail.com>

* Update tests/faucet.test.ts

Co-authored-by: Nayyir Jutha <nayyir.jutha@gmail.com>

* fixed test

---------

Co-authored-by: Nayyir Jutha <nayyir.jutha@gmail.com>

* updated default testnet endpoint

* updated for release

* Update src/common/utils-cli.ts

* revert setupTest to have unique temp configPaths

---------

Co-authored-by: mixmix <mix@protozoa.nz>
  • Loading branch information
rh0delta and mixmix authored Oct 24, 2024
1 parent b0db1b0 commit d8d0b33
Show file tree
Hide file tree
Showing 25 changed files with 626 additions and 467 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ jobs:
run: yarn run build
- name: Add TSS server host mappings
run: |
echo "127.0.0.1 alice-tss-server bob-tss-server" | sudo tee -a /etc/hosts
echo "127.0.0.1 alice-tss-server bob-tss-server charlie-tss-server dave-tss-server" | sudo tee -a /etc/hosts
- name: Test
run: yarn run test
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The format extends [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

Version header format: `[version] Name - year-month-day (entropy-core compatibility: version [range])`

## [UNRELEASED]
## [0.0.4] Carnage - 2024-10-23 (entropy-core compatibility: 0.3.0)

### Added
- new: 'src/flows/register/register.ts' - service file for register pure function
Expand Down Expand Up @@ -43,6 +43,7 @@ Version header format: `[version] Name - year-month-day (entropy-core compatibil
- new: './src/faucet/interaction.ts' - main entrypoint for TUI flows
- new: './src/faucet/command.ts' - main entrypoint for CLI flows
- new: package added - yocto-spinner for adding loading spinners to the cli
- new: added new menu item for TUI to trigger a jumpstart to the network (needs to only be run once)

### Changed

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Arguments:
Options:
-e, --endpoint <endpoint> Runs entropy with the given endpoint and ignores network endpoints in
config. Can also be given a stored endpoint name from config eg: `entropy
--endpoint test-net`. (default: "ws://testnet.entropy.xyz:9944/", env:
--endpoint test-net`. (default: "wss://testnet.entropy.xyz/", env:
ENDPOINT)
-h, --help display help for command
```
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@entropyxyz/cli",
"version": "0.0.3",
"version": "0.0.4",
"description": "cli and tui for interacting with the entropy protocol",
"type": "module",
"scripts": {
Expand All @@ -20,7 +20,7 @@
"link:sdk": "yarn link @entropyxyz/sdk",
"unlink:sdk": "yarn unlink @entropyxyz/sdk",
"re-link:sdk": "yarn unlink:sdk && yarn link:sdk",
"removedb": "rm -rf .entropy && docker compose --file node_modules/@entropyxyz/sdk/dev/docker-scripts/two-nodes.yaml down 2> /dev/null"
"removedb": "rm -rf .entropy && docker compose --file node_modules/@entropyxyz/sdk/dev/docker-scripts/four-nodes.yaml down 2> /dev/null"
},
"files": [
"dist"
Expand All @@ -44,7 +44,7 @@
},
"homepage": "https://github.com/entropyxyz/cli#readme",
"dependencies": {
"@entropyxyz/sdk": "^0.2.3",
"@entropyxyz/sdk": "0.3.0",
"ansi-colors": "^4.1.3",
"cli-progress": "^3.12.0",
"commander": "^12.1.0",
Expand Down
37 changes: 0 additions & 37 deletions src/account/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,43 +68,6 @@ export class EntropyAccount extends EntropyBase {

this.logger.debug(`registering with params: ${registerParams}`, 'REGISTER')
return this.entropy.register(registerParams)
// NOTE: if "register" fails for any reason, core currently leaves the chain in a "polluted"
// state. To fix this we manually "prune" the dirty registration transaction.
.catch(async error => {
await this.pruneRegistration()
throw error
})
}

/* PRIVATE */

private async pruneRegistration () {
return new Promise((resolve, reject) => {
this.entropy.substrate.tx.registry.pruneRegistration()
.signAndSend(this.entropy.keyring.accounts.registration.pair, ({ status, dispatchError }) => {
if (dispatchError) {
let msg: string
if (dispatchError.isModule) {
// for module errors, we have the section indexed, lookup
const decoded = this.entropy.substrate.registry.findMetaError(
dispatchError.asModule
)
const { docs, name, section } = decoded

msg = `${section}.${name}: ${docs.join(' ')}`
} else {
// Other, CannotLookup, BadOrigin, no extra info
msg = dispatchError.toString()
}
const error = Error(msg)
this.logger.error('There was an issue pruning registration', error)
return reject(error)
}
if (status.isFinalized) {
resolve(status)
}
})
})
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/balance/interaction.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { print } from "src/common/utils"
import { findAccountByAddressOrName, print } from "src/common/utils"
import { EntropyBalance } from "./main"

export async function entropyBalance (entropy, endpoint, storedConfig) {
try {
const balanceService = new EntropyBalance(entropy, endpoint)
const balance = await balanceService.getBalance(storedConfig.selectedAccount)
print(`Address ${storedConfig.selectedAccount} has a balance of: ${balance.toLocaleString('en-US')} BITS`)
const address = findAccountByAddressOrName(storedConfig.accounts, storedConfig.selectedAccount)?.address
const balance = await balanceService.getBalance(address)
print(`Entropy Account [${storedConfig.selectedAccount}] (${address}) has a balance of: ${balance.toLocaleString('en-US')} BITS`)
} catch (error) {
console.error('There was an error retrieving balance', error)
}
Expand Down
8 changes: 4 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { Command, Option } from 'commander'

import { EntropyTuiOptions } from './types'
import { loadEntropy } from './common/utils-cli'
import { loadEntropy, tuiEndpointOption } from './common/utils-cli'
import * as config from './config'

import launchTui from './tui'
Expand All @@ -28,17 +28,17 @@ program
.env('DEV_MODE')
.hideHelp()
)

.addOption(tuiEndpointOption())
.addCommand(entropyBalanceCommand())
.addCommand(entropyAccountCommand())
.addCommand(entropyTransferCommand())
.addCommand(entropySignCommand())
.addCommand(entropyProgramCommand())

.action(async (opts: EntropyTuiOptions) => {
const { account, endpoint } = opts
const { account, tuiEndpoint } = opts
const entropy = account
? await loadEntropy(account, endpoint)
? await loadEntropy(account, tuiEndpoint)
: undefined
// NOTE: on initial startup you have no account
launchTui(entropy, opts)
Expand Down
3 changes: 1 addition & 2 deletions src/common/initializeEntropy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,10 @@ export const initializeEntropy = async ({ keyMaterial, endpoint, configPath }: I

const entropy = new Entropy({ keyring: selectedAccount, endpoint })
await entropy.ready

if (!entropy?.keyring?.accounts?.registration?.seed) {
throw new Error("Keys are undefined")
}


return entropy
} catch (error) {
Expand Down
28 changes: 27 additions & 1 deletion src/common/utils-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,32 @@ export function endpointOption () {

return endpoint
})
.default('ws://testnet.entropy.xyz:9944/')
.default('wss://testnet.entropy.xyz/')
// NOTE: default cannot be "test-net" as argParser only runs if the -e/--endpoint flag
// or ENTROPY_ENDPOINT env set
}

export function tuiEndpointOption () {
return new Option(
'-et, --tui-endpoint <url>',
[
'Runs entropy with the given endpoint and ignores network endpoints in config.',
'Can also be given a stored endpoint name from config eg: `entropy --endpoint test-net`.'
].join(' ')
)
.env('ENTROPY_TUI_ENDPOINT')
.argParser(aliasOrEndpoint => {
/* see if it's a raw endpoint */
if (aliasOrEndpoint.match(/^wss?:\/\//)) return aliasOrEndpoint

/* look up endpoint-alias */
const storedConfig = getConfigOrNull()
const endpoint = storedConfig?.endpoints?.[aliasOrEndpoint]
if (!endpoint) throw Error('unknown endpoint alias: ' + aliasOrEndpoint)

return endpoint
})
.default('wss://testnet.entropy.xyz/')
// NOTE: default cannot be "test-net" as argParser only runs if the -e/--endpoint flag
// or ENTROPY_ENDPOINT env set
}
Expand Down Expand Up @@ -82,6 +107,7 @@ export async function loadEntropy (addressOrName: string, endpoint: string): Pro
if (!selectedAccount) throw new Error(`No account with name or address: "${addressOrName}"`)

const entropy = await initializeEntropy({ keyMaterial: selectedAccount.data, endpoint })

if (!entropy?.keyring?.accounts?.registration?.pair) {
throw new Error("Signer keypair is undefined or not properly initialized.")
}
Expand Down
30 changes: 30 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Buffer } from 'buffer'
import { EntropyAccountConfig } from "../config/types"
import { EntropyLogger } from './logger'

export function stripHexPrefix (str: string): string {
if (str.startsWith('0x')) return str.slice(2)
Expand Down Expand Up @@ -73,3 +74,32 @@ export function findAccountByAddressOrName (accounts: EntropyAccountConfig[], al
accounts.find(account => account.name === aliasOrAddress)
)
}

export async function jumpStartNetwork (entropy, endpoint): Promise<any> {
const logger = new EntropyLogger('JUMPSTART_NETWORK', endpoint)
return new Promise((resolve, reject) => {
entropy.substrate.tx.registry.jumpStartNetwork()
.signAndSend(entropy.keyring.accounts.registration.pair, ({ status, dispatchError }) => {
if (dispatchError) {
let msg: string
if (dispatchError.isModule) {
// for module errors, we have the section indexed, lookup
const decoded = entropy.substrate.registry.findMetaError(
dispatchError.asModule
)
const { docs, name, section } = decoded

msg = `${section}.${name}: ${docs.join(' ')}`
} else {
// Other, CannotLookup, BadOrigin, no extra info
msg = dispatchError.toString()
}
const error = Error(msg)
logger.error('There was an issue jump starting the network', error)
return reject(error)
}

if (status.isFinalized) resolve(status)
})
})
}
18 changes: 18 additions & 0 deletions src/config/migrations/03.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const version = 3

export function migrate (data = {}) {
try {
const migratedData = {
...data,
endpoints: {
// @ts-ignore
...data.endpoints,
'stg': 'wss://api.staging.testnet.testnet-2024.infrastructure.entropy.xyz'
}
}
return migratedData
} catch (e) {
console.error(`error in migration ${version}: e.message`)
}
return data
}
2 changes: 2 additions & 0 deletions src/config/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as migration00 from './00'
import * as migration01 from './01'
import * as migration02 from './02'
import * as migration03 from './03'

const migrations = [
migration00,
migration01,
migration02,
migration03
]

export default migrations
4 changes: 3 additions & 1 deletion src/faucet/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ export const FAUCET_PROGRAM_MOD_KEY = '5GWamxgW4XWcwGsrUynqnFq2oNZPqNXQhMDfgNH9x
// TO-DO: update this when faucet is deployed properly
export const TESTNET_PROGRAM_HASH = '0x12af0bd1f2d91f12e34aeb07ea622c315dbc3c2bdc1e25ff98c23f1e61106c77'
// Hash with max send of 1e10
export const LOCAL_PROGRAM_HASH = '0x5fa0536818acaa380b0c349c8e887bf269d593a47e30c8e31de53a75d327f7b1'
// export const LOCAL_PROGRAM_HASH = '0x5fa0536818acaa380b0c349c8e887bf269d593a47e30c8e31de53a75d327f7b1'
// Hash with max send of 2e10
export const LOCAL_PROGRAM_HASH = '0x2eaf750c4fa0fe125ca8a9d4037c0c0608b57ae70d6586dc6acdfcb4e9872deb'
4 changes: 2 additions & 2 deletions src/program/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ export class EntropyProgram extends EntropyBase {

async get (programPointer: string): Promise<any> {
this.logger.debug(`program pointer: ${programPointer}`, `${FLOW_CONTEXT}::PROGRAM_PRESENCE_CHECK`);
return this.entropy.programs.dev.getProgramInfo(programPointer)
return this.entropy.programs.dev.get(programPointer)
}

async listDeployed () {
const address = this.entropy.keyring.accounts.registration.address
// QUESTION: will we always be wanting this address?
return this.entropy.programs.dev.get(address)
return this.entropy.programs.dev.getByDeployer(address)
}
}

Loading

0 comments on commit d8d0b33

Please sign in to comment.