Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Test.ts 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import { z } from "zod";
  2. import { RJSVM_Implementations, RJSVM_Interface, RJSVM, RJSVM_Config } from "../src/RJSVM/framework/types"
  3. import { DataParser, RJSVM_Builder } from "../src/main";
  4. import { xrp_transaction_hash_schema } from "../src/RJSVM/framework/schemas";
  5. //Test requirements
  6. import { assert, expect } from 'chai';
  7. import { describe, it } from "mocha";
  8. import { makeTestnetWallet } from "./tools";
  9. import { Wallet } from "xrpio";
  10. import { Datawriter } from "../src/RJSVM/datawriter/datawriter";
  11. import { InsufficientFeeError, RestrictedAccessError } from "../src/util/errors";
  12. var should = require('chai').should();
  13. var chai = require('chai');
  14. var chaiAsPromised = require('chai-as-promised');
  15. chai.use(chaiAsPromised);
  16. const xrpNode = "wss://s.altnet.rippletest.net:51233"
  17. let ownerWallet: Wallet //the RJSVM owner
  18. let userWallet: Wallet //a RJSVM user
  19. let listeningWallet: Wallet //wallet the RJSVM listens to
  20. let drainWallet: Wallet //random wallet to send stuff to, could be any user-controlled secondary wallet
  21. let rjsvm: RJSVM
  22. let user_datawriter: Datawriter
  23. let owner_datawriter: Datawriter
  24. const setup = async () => {
  25. [ownerWallet, listeningWallet, userWallet, drainWallet] = await Promise.all([makeTestnetWallet(), makeTestnetWallet(), makeTestnetWallet(), makeTestnetWallet()])
  26. owner_datawriter = new Datawriter({
  27. receiveAddress: drainWallet.address,
  28. sendWallet: ownerWallet,
  29. xrpNode: xrpNode,
  30. contractAddress: listeningWallet.address
  31. })
  32. user_datawriter = new Datawriter({
  33. receiveAddress: drainWallet.address,
  34. sendWallet: userWallet,
  35. xrpNode: xrpNode,
  36. contractAddress: listeningWallet.address
  37. })
  38. //await owner_dw.callEndpoint('submit', { title: "1", body: "2", from: "3" }, 100)
  39. //await owner_dw.callEndpoint('submit', { title: "1", body: "2", from: "3" }, 100)
  40. //await user_dw.callEndpoint('restricted', { title: "user", body: "user", from: "user" })
  41. //await owner_dw.callEndpoint('restricted', { title: "owner", body: "owner", from: "owner" })
  42. // #########################
  43. // Define parameter types
  44. // #########################
  45. const shoutSchema = z.object({
  46. title: z.string(),
  47. body: z.string(),
  48. from: z.string(),
  49. id: z.optional(z.string())
  50. })
  51. type Shout = z.infer<typeof shoutSchema>
  52. type State = {
  53. shouts: Shout[]
  54. }
  55. // #########################
  56. // Define endpoints
  57. // #########################
  58. type RJSVM_Endpoints = {
  59. submit: (data: Shout) => void
  60. restricted: (data: Shout) => void
  61. }
  62. // #########################
  63. // Define init state
  64. // #########################
  65. abstract class RJSVM_Base
  66. extends RJSVM<State, RJSVM_Endpoints>
  67. implements RJSVM_Interface<RJSVM_Base> {
  68. owner = ownerWallet.address
  69. state: State = {
  70. shouts: []
  71. }
  72. }
  73. // #########################
  74. // Implement logic
  75. // #########################
  76. const RJSVM_Contract: RJSVM_Implementations<RJSVM_Base> = {
  77. submit: {
  78. implementation: function (env, shout) {
  79. this.state.shouts.unshift(shout)
  80. },
  81. visibility: 'public',
  82. fee: 10,
  83. parameterSchema: shoutSchema
  84. },
  85. restricted: {
  86. implementation: function (env, shout) {
  87. this.state.shouts.unshift(shout)
  88. },
  89. visibility: 'owner',
  90. parameterSchema: shoutSchema
  91. },
  92. }
  93. // #########################
  94. // Build and connect
  95. // #########################
  96. const Rjsvm = RJSVM_Builder.from(RJSVM_Base, RJSVM_Contract);
  97. const conf: RJSVM_Config = {
  98. listeningAddress: listeningWallet.address,
  99. rippleNode: xrpNode
  100. }
  101. rjsvm = new Rjsvm(conf)
  102. await rjsvm.connect()
  103. }
  104. describe('RJSVM basic functions', () => {
  105. before(async function () {
  106. this.timeout(10000)
  107. await setup()
  108. })
  109. after(async () => {
  110. await rjsvm.disconnect()
  111. })
  112. it('Env contains the payload carrying transaction', function (done) {
  113. this.timeout(30000)
  114. makeTestnetWallet().then(async testWallet => {
  115. //create a mock RJSVM
  116. abstract class RJSVM_Base
  117. extends RJSVM<undefined, { testEndpoint: () => void }>
  118. implements RJSVM_Interface<RJSVM_Base> {
  119. owner = ownerWallet.address
  120. state = undefined
  121. }
  122. //Implementation of the 'test' endpoint
  123. const RJSVM_Contract: RJSVM_Implementations<RJSVM_Base> = {
  124. testEndpoint: {
  125. implementation: function (env) {
  126. expect(env.Account).to.be.equal(userWallet.address)
  127. expect(env.Amount).to.be.equal('1')
  128. expect(env.Destination).to.be.equal(testWallet.address)
  129. expect(Number(env.Fee)).to.be.greaterThanOrEqual(10)
  130. expect(env.LastLedgerSequence).to.be.a('number')
  131. expect(env.Memos).to.be.an('Array')
  132. const memo = JSON.parse(DataParser.hex_to_ascii(env.Memos[0].Memo.MemoData))
  133. expect(memo.endpoint).to.be.equal('testEndpoint')
  134. expect(env.Sequence).to.be.a('number')
  135. expect(env.SigningPubKey).to.be.a('string')
  136. expect(env.TransactionType).to.be.equal('Payment')
  137. expect(env.TxnSignature).to.be.a('string')
  138. expect(env.date).to.be.a('number')
  139. expect(env.hash).to.be.a('string')
  140. expect(env.inLedger).to.be.a('number')
  141. expect(env.ledger_index).to.be.a('number')
  142. runnable.disconnect()
  143. done()
  144. },
  145. visibility: 'public',
  146. parameterSchema: z.any()
  147. },
  148. }
  149. const Rjsvm = RJSVM_Builder.from(RJSVM_Base, RJSVM_Contract)
  150. const runnable = new Rjsvm({listeningAddress: testWallet.address,rippleNode: xrpNode})
  151. await runnable.connect()
  152. const dw = new Datawriter({
  153. contractAddress: testWallet.address,
  154. sendWallet: userWallet,
  155. receiveAddress: drainWallet.address,
  156. xrpNode: xrpNode
  157. })
  158. dw.callEndpoint('testEndpoint', "")
  159. })
  160. })
  161. it('Called endpoint triggers in RJSVM', function (done) {
  162. this.timeout(30000)
  163. const data = { title: "1", body: "2", from: "3" };
  164. rjsvm.once('submit', (payload) => {
  165. expect(payload).to.be.an('object')
  166. expect(payload).to.deep.equal(data)
  167. done()
  168. })
  169. owner_datawriter.callEndpoint('submit', data, 10)
  170. })
  171. it('Calling an endpoint with insufficient fee fails', function (done) {
  172. this.timeout(30000)
  173. const data = { title: "f", body: "f", from: "f" };
  174. rjsvm.once('error', (err) => {
  175. expect(err).to.be.instanceOf(InsufficientFeeError)
  176. done()
  177. })
  178. owner_datawriter.callEndpoint('submit', data, 1)
  179. })
  180. it('Restricted endpoint can be called by owner', function (done) {
  181. this.timeout(30000)
  182. const data = { title: "11", body: "22", from: "33" };
  183. rjsvm.once('restricted', (payload) => {
  184. expect(payload).to.be.an('object')
  185. expect(payload).to.deep.equal(data)
  186. done()
  187. })
  188. owner_datawriter.callEndpoint('restricted', data)
  189. })
  190. it('Restricted endpoint cannot be called by non-owner', function (done) {
  191. this.timeout(30000)
  192. const data = { title: "e", body: "e", from: "e" };
  193. rjsvm.once('error', (err) => {
  194. expect(err).to.be.instanceOf(RestrictedAccessError)
  195. done()
  196. })
  197. user_datawriter.callEndpoint('restricted', data)
  198. })
  199. })