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.8KB

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