start updating example hooks

This commit is contained in:
Richard Holland
2022-05-23 10:19:57 +00:00
parent f74cc56acd
commit 7216df5829
6 changed files with 219 additions and 118 deletions

View File

@@ -17,7 +17,7 @@ require('../utils-tests.js').TestRig('ws://localhost:6005').then(t=>
} }
const secret = process.argv[2]; const secret = process.argv[2];
const account = t.xrpljs.Wallet.fromSeed(secret) const account = t.xrpljs.Wallet.fromSeed(secret)
t.api.submit( t.feeSubmit(process.argv[2],
{ {
Account: account.classicAddress, Account: account.classicAddress,
TransactionType: "SetHook", TransactionType: "SetHook",
@@ -31,9 +31,8 @@ require('../utils-tests.js').TestRig('ws://localhost:6005').then(t=>
Flags: t.hsfOVERRIDE Flags: t.hsfOVERRIDE
} }
} }
], ]
Fee: "100000" }).then(x=>
}, {wallet: account}).then(x=>
{ {
t.assertTxnSuccess(x) t.assertTxnSuccess(x)
console.log(x); console.log(x);

View File

@@ -1,3 +1,5 @@
all: all:
wasmcc carbon.c -o carbon.wasm -O0 -Wl,--allow-undefined -I../ wasmcc carbon.c -o carbon.wasm -O0 -Wl,--allow-undefined -I../
hook-cleaner carbon.wasm

View File

@@ -1,3 +1,4 @@
all: all:
wasmcc doubler.c -o doubler.wasm -O0 -Wl,--allow-undefined -I../ wasmcc doubler.c -o doubler.wasm -O0 -Wl,--allow-undefined -I../
hook-cleaner doubler.wasm

View File

@@ -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 * @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. * @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);
/** /**

View File

@@ -1,137 +1,234 @@
const fs = require('fs') const fs = require('fs')
const xrpljs = require('xrpl-hooks'); const xrpljs = require('xrpl-hooks');
const kp = require('ripple-keypairs'); 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 // Fails via process.exit
module.exports = { module.exports = {
TestRig: (endpoint)=> TestRig: (endpoint)=>
{ {
return new Promise((resolve, reject)=> return new Promise((resolve, reject)=>
{ {
const api = new xrpljs.Client(endpoint); const api = new xrpljs.Client(endpoint);
const execShell = cmd => const fee = (tx_blob) =>
{
return new Promise((resolve, reject) =>
{ {
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); resolve(resp.result.drops);
process.exit(2); }).catch(e =>
} {
console.log("Ran cmd: `" + cmd + "`"); reject(e);
if (stdout) });
console.log("stdout:", stdout);
if (stderr)
console.log("stderr:", stderr);
resolve([stdout, stderr]);
}); });
}); };
};
const assertTxnSuccess = x =>
{ const assertTxnSuccess = x =>
if (!x || !x.result || x.result.engine_result_code != 0)
{ {
console.log("Transaction failed:", x) if (!x || !x.result || x.result.engine_result_code != 0)
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=>
{ {
assertTxnSuccess(x); console.log("Transaction failed:", x)
resolve(x); process.exit(1);
}).catch(err); }
}); };
};
const assertTxnFailure = x =>
const pay = (seed, amt, dest) =>
{
return new Promise((resolve, reject) =>
{ {
let wal = xrpljs.Wallet.fromSeed(seed); if (!x || !x.result || x.result.engine_result_code == 0)
api.submit({
Account: wal.classicAddress, // fund account from genesis
TransactionType: "Payment",
Amount: ''+amt,
Destination: dest,
Fee: "10000"
}, {wallet: wal}).then(x=>
{ {
assertTxnSuccess(x); console.log("Transaction failed:", x)
resolve(x); process.exit(1);
}).catch(err); }
}); };
};
api.connect().then(()=>
{ const wasm = (x) =>
resolve({ {
api: api, if (x.slice(0,1) != '/')
xrpljs: xrpljs, x = 'wasm/' + x;
assertTxnSuccess: assertTxnSuccess, return fs.readFileSync( x).toString('hex').toUpperCase();
assertTxnFailure: assertTxnFailure, };
wasm: wasm,
kp: kp,
genesis: genesis, const feeCompute = (account_seed, txn_org) =>
randomAccount: randomAccount, {
fundFromGenesis: fundFromGenesis, return new Promise((resolve, reject) =>
err: err, {
hsfOVERRIDE: 1, txn_to_send = { ... txn_org };
hsfNSDELETE: 2, txn_to_send['SigningPubKey'] = '';
pay: pay,
findWasm: findWasm, let wal = xrpljs.Wallet.fromSeed(account_seed);
execShell: execShell api.prepareTransaction(txn_to_send, {wallet: wal}).then(txn =>
}); {
}).catch(err); 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);
}); });
} }
}; };

View File

@@ -1,2 +1,4 @@
guard_checker: guard_checker.cpp Guard.h Enum.h guard_checker: guard_checker.cpp Guard.h Enum.h
g++ -o guard_checker guard_checker.cpp --std=c++17 -g g++ -o guard_checker guard_checker.cpp --std=c++17 -g
install: guard_checker
cp guard_checker /usr/bin/