Skip to content

Commit

Permalink
[NayNay] Updating CLI with new sdk rc
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
rh0delta committed Oct 21, 2024
1 parent 1934adc commit 229040f
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 66 deletions.
37 changes: 0 additions & 37 deletions src/account/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,42 +66,5 @@ 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)
}
})
})
}
}
3 changes: 2 additions & 1 deletion 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,6 +28,7 @@ program
.env('DEV_MODE')
.hideHelp()
)
.addOption(endpointOption())
.addCommand(entropyBalanceCommand())
.addCommand(entropyAccountCommand())
.addCommand(entropyTransferCommand())
Expand Down
4 changes: 0 additions & 4 deletions src/common/initializeEntropy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ export const initializeEntropy = async ({ keyMaterial, endpoint, configPath }: I
if (!entropy?.keyring?.accounts?.registration?.seed) {
throw new Error("Keys are undefined")
}

await entropy.substrate.tx.registry.jumpStartNetwork()
.signAndSend(entropy.keyring.accounts.registration.pair)


return entropy
} catch (error) {
Expand Down
3 changes: 3 additions & 0 deletions src/common/utils-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ 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 })
await entropy.substrate.tx.registry.jumpStartNetwork()
.signAndSend(entropy.keyring.accounts.registration.pair)

if (!entropy?.keyring?.accounts?.registration?.pair) {
throw new Error("Signer keypair is undefined or not properly initialized.")
}
Expand Down
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'
35 changes: 27 additions & 8 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 @@ -83,28 +85,45 @@ 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"))
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
8 changes: 5 additions & 3 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 @@ -9,7 +10,8 @@ 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 })
const { run, entropy, endpoint } = await setupTest(t, { seed: eveSeed })
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)
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 Down
18 changes: 10 additions & 8 deletions tests/programs.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import test from 'tape'
import { jumpStartNetwork } from '@entropyxyz/sdk/testing'

import { promiseRunner, charlieStashSeed, setupTest } from './testing-utils'
import { promiseRunner, eveSeed, setupTest } from './testing-utils'
import { EntropyProgram } from '../src/program/main'

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

test('program', async t => {
const { run, entropy } = await setupTest(t, { seed: charlieStashSeed, networkType })
const { run, entropy } = await setupTest(t, { seed: eveSeed, networkType })
await run('jump-start network', jumpStartNetwork(entropy))
await run('register', entropy.register()) // TODO: consider removing this in favour of just testing add

const program = new EntropyProgram(entropy, endpoint)
Expand All @@ -34,7 +36,7 @@ test('program', async t => {
const run = promiseRunner(t)

const programsBeforeAdd = await run('get programs initial', getPrograms())
t.equal(programsBeforeAdd.length, 1, 'charlie has 1 program')
t.equal(programsBeforeAdd.length, 1, 'eve has 1 program')

await run(
'adding program',
Expand All @@ -44,7 +46,7 @@ test('program', async t => {
})
)
const programsAfterAdd = await run('get programs after add', getPrograms())
t.equal(programsAfterAdd.length, 2, 'charlie has 2 programs')
t.equal(programsAfterAdd.length, 2, 'eve has 2 programs')

t.end()
})
Expand All @@ -53,7 +55,7 @@ test('program', async t => {
const run = promiseRunner(t)

const programsBeforeRemove = await run('get programs initial', getPrograms())
t.equal(programsBeforeRemove.length, 2, 'charlie has 2 programs')
t.equal(programsBeforeRemove.length, 2, 'eve has 2 programs')

await run(
'removing noop program',
Expand All @@ -63,7 +65,7 @@ test('program', async t => {
})
)
const programsAfterRemove = await run('get programs initial', getPrograms())
t.equal(programsAfterRemove.length, 1, 'charlie has 1 less program')
t.equal(programsAfterRemove.length, 1, 'eve has 1 less program')

t.end()
})
Expand All @@ -72,11 +74,11 @@ test('program', async t => {
const run = promiseRunner(t)

const programs = await run(
'get charlie programs',
'get eve programs',
program.list({ verifyingKey })
)

t.equal(programs.length, 1, 'charlie has 1 program')
t.equal(programs.length, 1, 'eve has 1 program')

t.end()
})
Expand Down
7 changes: 5 additions & 2 deletions tests/sign.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import test from 'tape'
import { jumpStartNetwork } from '@entropyxyz/sdk/testing'

import { EntropySign } from '../src/sign/main'
import { setupTest, eveSeed } from './testing-utils'

import { setupTest, charlieStashSeed } from './testing-utils'
const endpoint = 'ws://127.0.0.1:9944'

test('Sign - signMessageWithAdapters', async (t) => {
const { run, entropy } = await setupTest(t, { seed: charlieStashSeed })
const { run, entropy } = await setupTest(t, { seed: eveSeed })
await run('jump-start network', jumpStartNetwork(entropy))
const signService = new EntropySign(entropy, endpoint)

await run('register', entropy.register())
Expand Down
6 changes: 5 additions & 1 deletion tests/testing-utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ export const charlieStashSeed =

export const charlieSeed =
'0xbc1ede780f784bb6991a585e4f6e61522c14e1cae6ad0895fb57b9a205a8f938'
export const charlieAddress = '5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y'
export const charlieAddress = '5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y'

export const eveSeed =
'0x786ad0e2df456fe43dd1f91ebca22e235bc162e0bb8d53c633e8c85b2af68b7a'
export const eveAddress = '5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw'
1 change: 0 additions & 1 deletion tests/testing-utils/setup-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export async function setupTest (t: Test, opts?: SetupTestOpts): Promise<{ entro
})

await run('entropy ready', entropy.ready)
await run('jump-start network', jumpStartNetwork(entropy))

return { entropy, run, endpoint }
}

0 comments on commit 229040f

Please sign in to comment.