From 7216df5829406a1b06e27c709dcc945796339ac2 Mon Sep 17 00:00:00 2001 From: Richard Holland Date: Mon, 23 May 2022 10:19:57 +0000 Subject: [PATCH] start updating example hooks --- hook-api-examples/carbon/install.js | 7 +- hook-api-examples/carbon/makefile | 2 + hook-api-examples/doubler/makefile | 1 + hook-api-examples/hookapi.h | 2 +- hook-api-examples/utils-tests.js | 323 ++++++++++++++++++---------- src/ripple/app/hook/makefile | 2 + 6 files changed, 219 insertions(+), 118 deletions(-) diff --git a/hook-api-examples/carbon/install.js b/hook-api-examples/carbon/install.js index ce73396db..d3468bf9f 100644 --- a/hook-api-examples/carbon/install.js +++ b/hook-api-examples/carbon/install.js @@ -17,7 +17,7 @@ require('../utils-tests.js').TestRig('ws://localhost:6005').then(t=> } const secret = process.argv[2]; const account = t.xrpljs.Wallet.fromSeed(secret) - t.api.submit( + t.feeSubmit(process.argv[2], { Account: account.classicAddress, TransactionType: "SetHook", @@ -31,9 +31,8 @@ require('../utils-tests.js').TestRig('ws://localhost:6005').then(t=> Flags: t.hsfOVERRIDE } } - ], - Fee: "100000" - }, {wallet: account}).then(x=> + ] + }).then(x=> { t.assertTxnSuccess(x) console.log(x); diff --git a/hook-api-examples/carbon/makefile b/hook-api-examples/carbon/makefile index 84812e086..e0eedde08 100644 --- a/hook-api-examples/carbon/makefile +++ b/hook-api-examples/carbon/makefile @@ -1,3 +1,5 @@ all: wasmcc carbon.c -o carbon.wasm -O0 -Wl,--allow-undefined -I../ + hook-cleaner carbon.wasm + diff --git a/hook-api-examples/doubler/makefile b/hook-api-examples/doubler/makefile index bad42c5f9..687699694 100644 --- a/hook-api-examples/doubler/makefile +++ b/hook-api-examples/doubler/makefile @@ -1,3 +1,4 @@ all: wasmcc doubler.c -o doubler.wasm -O0 -Wl,--allow-undefined -I../ + hook-cleaner doubler.wasm diff --git a/hook-api-examples/hookapi.h b/hook-api-examples/hookapi.h index 79eefee55..656273aa2 100644 --- a/hook-api-examples/hookapi.h +++ b/hook-api-examples/hookapi.h @@ -209,7 +209,7 @@ extern int64_t ledger_last_hash (uint32_t write_ptr, uint32_t write_len); * @param write_len The length of that buffer * @return The number of bytes written into the buffer of a negative integer if an error occured. */ -extern int64_t nonce (uint32_t write_ptr, uint32_t write_len); +extern int64_t etxn_nonce (uint32_t write_ptr, uint32_t write_len); /** diff --git a/hook-api-examples/utils-tests.js b/hook-api-examples/utils-tests.js index 0092a7790..96abdaacd 100644 --- a/hook-api-examples/utils-tests.js +++ b/hook-api-examples/utils-tests.js @@ -1,137 +1,234 @@ const fs = require('fs') const xrpljs = require('xrpl-hooks'); const kp = require('ripple-keypairs'); -const { exec } = require('child_process'); +const crypto = require('crypto') +const rbc = require('xrpl-binary-codec') + +const err = (x) => +{ + console.log(x); process.exit(1); +} // Fails via process.exit module.exports = { TestRig: (endpoint)=> { return new Promise((resolve, reject)=> { - const api = new xrpljs.Client(endpoint); + const api = new xrpljs.Client(endpoint); - const execShell = cmd => - { - return new Promise((resolve, reject) => + const fee = (tx_blob) => { - exec(cmd, (error, stdout, stderr) => + return new Promise((resolve, reject) => { - if (error) + let req = {command: 'fee'}; + if (tx_blob) + req['tx_blob'] = tx_blob; + + api.request(req).then(resp => { - console.log(error); - process.exit(2); - } - console.log("Ran cmd: `" + cmd + "`"); - if (stdout) - console.log("stdout:", stdout); - if (stderr) - console.log("stderr:", stderr); - resolve([stdout, stderr]); + resolve(resp.result.drops); + }).catch(e => + { + reject(e); + }); }); - }); - }; + }; - const assertTxnSuccess = x => - { - if (!x || !x.result || x.result.engine_result_code != 0) + + const assertTxnSuccess = x => { - console.log("Transaction failed:", x) - process.exit(1); - } - }; - - const assertTxnFailure = x => - { - if (!x || !x.result || x.result.engine_result_code == 0) - { - console.log("Transaction failed:", x) - process.exit(1); - } - }; - - const err = (x) => - { - console.log(x); process.exit(1); - } - - const wasm = (x) => - { - return fs.readFileSync(x).toString('hex').toUpperCase(); - } - - const genesis = xrpljs.Wallet.fromSeed('snoPBrXtMeMyMHUVTgbuqAfg1SUTb'); - - const randomAccount = ()=> - { - return xrpljs.Wallet.fromSeed(kp.generateSeed()); - }; - - const findWasm = ()=> - { - return fs.readdirSync('.').filter(fn => fn.endsWith('.wasm')); - } - - const fundFromGenesis = (acc) => - { - return new Promise((resolve, reject) => - { - if (typeof(acc) != 'string') - acc = acc.classicAddress; - - api.submit({ - Account: genesis.classicAddress, // fund account from genesis - TransactionType: "Payment", - Amount: "1000000000", - Destination: acc - }, {wallet: genesis}).then(x=> + if (!x || !x.result || x.result.engine_result_code != 0) { - assertTxnSuccess(x); - resolve(x); - }).catch(err); - }); - }; - - const pay = (seed, amt, dest) => - { - return new Promise((resolve, reject) => + console.log("Transaction failed:", x) + process.exit(1); + } + }; + + const assertTxnFailure = x => { - let wal = xrpljs.Wallet.fromSeed(seed); - - api.submit({ - Account: wal.classicAddress, // fund account from genesis - TransactionType: "Payment", - Amount: ''+amt, - Destination: dest, - Fee: "10000" - }, {wallet: wal}).then(x=> + if (!x || !x.result || x.result.engine_result_code == 0) { - assertTxnSuccess(x); - resolve(x); - }).catch(err); - }); - }; + console.log("Transaction failed:", x) + process.exit(1); + } + }; - api.connect().then(()=> - { - resolve({ - api: api, - xrpljs: xrpljs, - assertTxnSuccess: assertTxnSuccess, - assertTxnFailure: assertTxnFailure, - wasm: wasm, - kp: kp, - genesis: genesis, - randomAccount: randomAccount, - fundFromGenesis: fundFromGenesis, - err: err, - hsfOVERRIDE: 1, - hsfNSDELETE: 2, - pay: pay, - findWasm: findWasm, - execShell: execShell - }); - }).catch(err); + + const wasm = (x) => + { + if (x.slice(0,1) != '/') + x = 'wasm/' + x; + return fs.readFileSync( x).toString('hex').toUpperCase(); + }; + + + const feeCompute = (account_seed, txn_org) => + { + return new Promise((resolve, reject) => + { + txn_to_send = { ... txn_org }; + txn_to_send['SigningPubKey'] = ''; + + let wal = xrpljs.Wallet.fromSeed(account_seed); + api.prepareTransaction(txn_to_send, {wallet: wal}).then(txn => + { + let ser = rbc.encode(txn); + fee(ser).then(fees => + { + let base_drops = fees.base_fee + + delete txn_to_send['SigningPubKey'] + txn_to_send['Fee'] = base_drops + ''; + + + api.prepareTransaction(txn_to_send, {wallet: wal}).then(txn => + { + resolve(txn); + }).catch(e=>{reject(e);}); + }).catch(e=>{reject(e);}); + }).catch(e=>{reject(e);}); + }); + } + + const feeSubmit = (seed, txn) => + { + return new Promise((resolve, reject) => + { + feeCompute(seed, txn).then(txn=> + { + api.submit(txn, + {wallet: xrpljs.Wallet.fromSeed(seed)}).then(s=> + { + resolve(s); + }).catch(e=>{reject(e);}); + }).catch(e=>{reject(e);}); + }); + } + + const genesisseed = 'snoPBrXtMeMyMHUVTgbuqAfg1SUTb'; + const genesisaddr = 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh'; + + + const genesis = xrpljs.Wallet.fromSeed(genesisseed); + + const randomAccount = ()=> + { + const acc = xrpljs.Wallet.fromSeed(kp.generateSeed()); + console.log(acc) + return acc + }; + + const pay_mock = (seed, amt, dest) => + { + if (dest.classicAddress != undefined) + dest = dest.classicAddress; + + return new Promise((resolve, reject) => + { + + let wal = xrpljs.Wallet.fromSeed(seed); + api.prepareTransaction({ + Account: wal.classicAddress, + TransactionType: "Payment", + Amount: ''+amt, + Destination: dest, + SigningPubKey: '' + }, {wallet: wal}).then(txn => + { + resolve(rbc.encode(txn)); + }).catch(e=> + { + reject(e); + }); + }); + + } + + const pay = (seed, amt, dest) => + { + if (dest.classicAddress != undefined) + dest = dest.classicAddress; + + return new Promise((resolve, reject) => + { + let wal = xrpljs.Wallet.fromSeed(seed); + + api.submit({ + Account: wal.classicAddress, + TransactionType: "Payment", + Amount: ''+amt, + Destination: dest, + Fee: "10000" + }, {wallet: wal}).then(x=> + { + assertTxnSuccess(x); + resolve(x); + }).catch(err); + }); + }; + + const hookHash = fn => + { + let b = fs.readFileSync('wasm/' + fn); + return crypto.createHash('SHA512').update(b).digest().slice(0,32).toString('hex').toUpperCase() + } + + const fundFromGenesis = (acc) => + { + return new Promise((resolve, reject) => + { + if (typeof(acc) != 'string') + acc = acc.classicAddress; + + api.submit({ + Account: genesis.classicAddress, // fund account from genesis + TransactionType: "Payment", + Amount: "1000000000", + Destination: acc, + Fee: "10000" + }, {wallet: genesis}).then(x=> + { + assertTxnSuccess(x); + resolve(); + }).catch(err); + }); + }; + + const findWasm = ()=> + { + return fs.readdirSync('.').filter(fn => fn.endsWith('.wasm')); + }; + + api.connect().then(()=> + { + resolve({ + rbc: rbc, + api: api, + xrpljs: xrpljs, + assertTxnSuccess: assertTxnSuccess, + assertTxnFailure: assertTxnFailure, + wasm: wasm, + kp: kp, + genesis: genesis, + randomAccount: randomAccount, + fundFromGenesis: fundFromGenesis, + err: err, + hsfOVERRIDE: 1, + hsfNSDELETE: 2, + hfsOVERRIDE: 1, + hfsNSDELETE: 2, + hookHash: hookHash, + pay: pay, + pay_mock: pay_mock, + fee: fee, + genesisseed: genesisseed, + genesisaddr: genesisaddr, + feeCompute: feeCompute, + feeSubmit: feeSubmit, + findWasm: findWasm + }); + }).catch(err); }); } }; diff --git a/src/ripple/app/hook/makefile b/src/ripple/app/hook/makefile index 77acf169a..7e56edfbe 100644 --- a/src/ripple/app/hook/makefile +++ b/src/ripple/app/hook/makefile @@ -1,2 +1,4 @@ guard_checker: guard_checker.cpp Guard.h Enum.h g++ -o guard_checker guard_checker.cpp --std=c++17 -g +install: guard_checker + cp guard_checker /usr/bin/