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

[NayNay] New SDK RC integration #262

Merged
merged 21 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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: 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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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-1",
"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
4 changes: 2 additions & 2 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 { endpointOption, loadEntropy } from './common/utils-cli'
import * as config from './config'

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

.addOption(endpointOption())
rh0delta marked this conversation as resolved.
Show resolved Hide resolved
.addCommand(entropyBalanceCommand())
.addCommand(entropyAccountCommand())
.addCommand(entropyTransferCommand())
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
1 change: 1 addition & 0 deletions src/common/utils-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,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
7 changes: 7 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,10 @@ export function findAccountByAddressOrName (accounts: EntropyAccountConfig[], al
accounts.find(account => account.name === aliasOrAddress)
)
}

export async function jumpStartNetwork (entropy) {
await entropy.substrate.tx.registry.jumpStartNetwork()
.signAndSend(entropy.keyring.accounts.registration.pair)
rh0delta marked this conversation as resolved.
Show resolved Hide resolved

return
}
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)
}
}

19 changes: 17 additions & 2 deletions src/tui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Entropy from '@entropyxyz/sdk'
import * as config from './config'
import { EntropyTuiOptions } from './types'
import { logo } from './common/ascii'
import { print } from './common/utils'
import { jumpStartNetwork, print } from './common/utils'
import { loadEntropy } from './common/utils-cli'
import { EntropyLogger } from './common/logger'

Expand Down Expand Up @@ -44,6 +44,15 @@ export default function tui (entropy: Entropy, options: EntropyTuiOptions) {
'User Programs',
]

const devChoices = [
'Jump Start Network',
// 'Create and Fund Faucet(s)'
]

if (options.dev) {
choices = [...choices, ...devChoices]
}

// assign exit so its last
choices = [...choices, 'Exit']

Expand Down Expand Up @@ -129,8 +138,14 @@ async function main (entropy: Entropy, choices, options, logger: EntropyLogger)
.catch(err => console.error('There was an error with program dev', err))
break
}
case 'Jump Start Network': {
await jumpStartNetwork(entropy)
.catch(err => console.error('There was an issue jumpstarting the network', err))
rh0delta marked this conversation as resolved.
Show resolved Hide resolved
break
}
default: {
throw Error(`unsupported choice: ${answers.choice}`)
console.error('Unsupported Action:' + answers.choice)
break
}
}
}
Expand Down
37 changes: 28 additions & 9 deletions tests/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import test from 'tape'
import { wasmGlobalsReady } from '@entropyxyz/sdk'
// @ts-ignore
import { isValidSubstrateAddress } from '@entropyxyz/sdk/utils'
import { jumpStartNetwork } from '@entropyxyz/sdk/testing'
// @ts-ignore
import Keyring from '@entropyxyz/sdk/keys'
import { randomAsHex } from '@polkadot/util-crypto'
import { EntropyAccount } from '../src/account/main'
import { EntropyTransfer } from '../src/transfer/main'
import { EntropyAccountConfig, EntropyConfig } from '../src/config/types'
import * as config from '../src/config'
import { promiseRunner, setupTest } from './testing-utils'
import { charlieStashAddress, charlieStashSeed } from './testing-utils/constants'
import { charlieStashAddress, charlieStashSeed, eveSeed } from './testing-utils/constants'
import { readFileSync } from 'fs'

test('Account - list', async t => {
Expand Down Expand Up @@ -78,33 +80,50 @@ test('Account - import', async t => {
t.end()
})

const networkType = 'two-nodes'
const networkType = 'four-nodes'
const endpoint = 'ws://127.0.0.1:9944'

test('Account - Register: Default Program', async (t) => {
const { run, entropy } = await setupTest(t, { networkType, seed: charlieStashSeed })
const accountService = new EntropyAccount(entropy, endpoint)
// Naynay entropy instance required here as @JesseAbram brought to attn that running jumpstart using the charlie seed will result in errors.
// the error found here was: RpcError: 1014: Priority is too low: (4471 vs 4471): The transaction has too low priority to replace another transaction already in the pool.
// which indicated a potential nonce issue, where the solution would be to manually wait for ~50 blocks to make any subsequent calls to the network. However,
// using naynay instance (fresh seed) jump start and the subsequent calls to the network (i.e register), the flow worked without issues.
const { entropy: naynay } = await setupTest(t, { networkType })

const accountService = new EntropyAccount(naynay, endpoint)
const transferService = new EntropyTransfer(entropy, endpoint)

// use of charlie seed is solely to transfer funds to the naynay (fresh seed) instance. these funds will be used for the jumpstart and register call
await run('transferring funds', transferService.transfer(naynay.keyring.accounts.registration.address, "1000"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the setup should be extracted (set up of a funded account).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here #263

await run('jump-start network', jumpStartNetwork(naynay))

const verifyingKey = await run('register account', accountService.register())

const fullAccount = entropy.keyring.getAccount()
const fullAccount = naynay.keyring.getAccount()

t.equal(verifyingKey, fullAccount?.registration?.verifyingKeys?.[0], 'verifying key matches key added to registration account')

t.end()
})

test('Account - Register: Barebones Program', async t => {
const { run, entropy } = await setupTest(t, { networkType, seed: charlieStashSeed })
const { run, entropy } = await setupTest(t, { networkType, seed: eveSeed })
const { entropy: naynay } = await setupTest(t, { networkType })
// await run('jump-start network', jumpStartNetwork(entropy))
const dummyProgram: any = readFileSync(
new URL('./programs/template_barebones.wasm', import.meta.url)
)

const accountService = new EntropyAccount(naynay, endpoint)
const transferService = new EntropyTransfer(entropy, endpoint)

await run('transferring funds', transferService.transfer(naynay.keyring.accounts.registration.address, "1000"))
await run('jump-start network', jumpStartNetwork(naynay))
const pointer = await run(
'deploy program',
entropy.programs.dev.deploy(dummyProgram)
)

const accountService = new EntropyAccount(entropy, endpoint)
const verifyingKey = await run(
'register - using custom params',
accountService.register({
Expand All @@ -113,9 +132,9 @@ test('Account - Register: Barebones Program', async t => {
})
)

const fullAccount = entropy.keyring.getAccount()
const fullAccount = naynay.keyring.getAccount()

t.equal(verifyingKey, fullAccount?.registration?.verifyingKeys?.[1], 'verifying key matches key added to registration account')
t.equal(verifyingKey, fullAccount?.registration?.verifyingKeys?.[0], 'verifying key matches key added to registration account')

t.end()
})
Expand Down
2 changes: 1 addition & 1 deletion tests/balance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import test from 'tape'
import { setupTest, charlieStashAddress as richAddress } from './testing-utils'
import { EntropyBalance } from '../src/balance/main'

const networkType = 'two-nodes'
const networkType = 'four-nodes'

test('getBalance + getBalances', async (t) => {
const { run, entropy, endpoint } = await setupTest(t, { networkType })
Expand Down
14 changes: 8 additions & 6 deletions tests/faucet.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from 'tape'
import { charlieStashSeed, setupTest } from './testing-utils'
import { eveSeed, setupTest } from './testing-utils'
import { jumpStartNetwork } from '@entropyxyz/sdk/testing'
import { stripHexPrefix } from '../src/common/utils'
import { readFileSync } from 'fs'
import { EntropyBalance } from '../src/balance/main'
Expand All @@ -8,11 +9,12 @@ import { EntropyFaucet } from '../src/faucet/main'
import { LOCAL_PROGRAM_HASH } from '../src/faucet/utils'
import { EntropyAccount } from '../src/account/main'

async function setupAndFundFaucet (t, naynayEntropy) {
const { run, entropy, endpoint } = await setupTest(t, { seed: charlieStashSeed })
async function setupAndFundFaucet (t) {
const { run, entropy, endpoint } = await setupTest(t, { seed: eveSeed })
mixmix marked this conversation as resolved.
Show resolved Hide resolved
await run('jump-start network', jumpStartNetwork(entropy))
const accountService = new EntropyAccount(entropy, endpoint)
const transferService = new EntropyTransfer(entropy, endpoint)
const faucetService = new EntropyFaucet(naynayEntropy, endpoint)
const faucetService = new EntropyFaucet(entropy, endpoint)

const faucetProgram = readFileSync('tests/programs/faucet_program.wasm')

Expand Down Expand Up @@ -40,7 +42,7 @@ async function setupAndFundFaucet (t, naynayEntropy) {
t.equal(faucetProgramPointer, LOCAL_PROGRAM_HASH, 'Program pointer matches')

// register with faucet program
await run('Register Faucet Program for charlie stash', accountService.register(
await run('Register Faucet Program for eve', accountService.register(
{
programModAddress: entropy.keyring.accounts.registration.address,
programData: [{ program_pointer: faucetProgramPointer, program_config: userConfig }]
Expand All @@ -61,7 +63,7 @@ test('Faucet Tests: Successfully send funds and register', async t => {
const faucetService = new EntropyFaucet(naynayEntropy, endpoint)
const balanceService = new EntropyBalance(naynayEntropy, endpoint)

const { faucetAddress, chosenVerifyingKey, faucetProgramPointer } = await setupAndFundFaucet(t, naynayEntropy)
const { faucetAddress, chosenVerifyingKey, faucetProgramPointer } = await setupAndFundFaucet(t)

let naynayBalance = await balanceService.getBalance(naynayEntropy.keyring.accounts.registration.address)
t.equal(naynayBalance, 0, 'Naynay is broke af')
Expand Down
Loading
Loading