|
| 1 | +package net.consensys.gpact.functioncall.common; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.util.ArrayList; |
| 5 | +import net.consensys.gpact.common.Tuple; |
| 6 | +import net.consensys.gpact.functioncall.CallExecutionTree; |
| 7 | +import net.consensys.gpact.functioncall.CallExecutionTreeException; |
| 8 | +import org.apache.tuweni.bytes.Bytes; |
| 9 | +import org.junit.jupiter.api.Assertions; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +public abstract class CallExecutionTreeV1TestCommon extends CallExecutionTreeTestCommon { |
| 13 | + abstract Tuple<BigInteger, String, String> extractFunction(byte[] encodedCallTree, int[] callPath) |
| 14 | + throws CallExecutionTreeException; |
| 15 | + |
| 16 | + public void checkJavaEncodeDecode(final CallExecutionTree seg) throws CallExecutionTreeException { |
| 17 | + byte[] encoded = seg.encode(CallExecutionTree.ENCODING_V1); |
| 18 | + CallExecutionTree.dump(encoded); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + public void rootOneSeg() throws Exception { |
| 23 | + CallExecutionTree seg = new CallExecutionTree(blockchainId2, contract2, function2); |
| 24 | + ArrayList<CallExecutionTree> rootCalls1 = new ArrayList<>(); |
| 25 | + rootCalls1.add(seg); |
| 26 | + CallExecutionTree root = new CallExecutionTree(blockchainId1, contract1, function1, rootCalls1); |
| 27 | + |
| 28 | + byte[] encoded = root.encode(); |
| 29 | + Tuple<BigInteger, String, String> func = extractFunction(encoded, new int[] {0}); |
| 30 | + Assertions.assertEquals(func.getFirst().toString(16), blockchainId1.toPlainString()); |
| 31 | + Assertions.assertEquals(func.getSecond(), contract1); |
| 32 | + Assertions.assertEquals(func.getThird(), function1); |
| 33 | + |
| 34 | + func = extractFunction(encoded, new int[] {1}); |
| 35 | + Assertions.assertEquals(func.getFirst().toString(16), blockchainId2.toPlainString()); |
| 36 | + Assertions.assertEquals(func.getSecond(), contract2); |
| 37 | + Assertions.assertEquals(func.getThird(), function2); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void rootTwoSeg() throws Exception { |
| 42 | + CallExecutionTree seg1 = new CallExecutionTree(blockchainId2, contract2, function2); |
| 43 | + CallExecutionTree seg2 = new CallExecutionTree(blockchainId3, contract3, function3); |
| 44 | + ArrayList<CallExecutionTree> rootCalls1 = new ArrayList<>(); |
| 45 | + rootCalls1.add(seg1); |
| 46 | + rootCalls1.add(seg2); |
| 47 | + CallExecutionTree root = new CallExecutionTree(blockchainId1, contract1, function1, rootCalls1); |
| 48 | + root.toString(); |
| 49 | + |
| 50 | + byte[] encoded = root.encode(); |
| 51 | + Tuple<BigInteger, String, String> func = extractFunction(encoded, new int[] {0}); |
| 52 | + Assertions.assertEquals(func.getFirst().toString(16), blockchainId1.toPlainString()); |
| 53 | + Assertions.assertEquals(func.getSecond(), contract1); |
| 54 | + Assertions.assertEquals(func.getThird(), function1); |
| 55 | + |
| 56 | + func = extractFunction(encoded, new int[] {1}); |
| 57 | + Assertions.assertEquals(func.getFirst().toString(16), blockchainId2.toPlainString()); |
| 58 | + Assertions.assertEquals(func.getSecond(), contract2); |
| 59 | + Assertions.assertEquals(func.getThird(), function2); |
| 60 | + |
| 61 | + func = extractFunction(encoded, new int[] {2}); |
| 62 | + Assertions.assertEquals(func.getFirst().toString(16), blockchainId3.toPlainString()); |
| 63 | + Assertions.assertEquals(func.getSecond(), contract3); |
| 64 | + Assertions.assertEquals(func.getThird(), function3); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void complex() throws Exception { |
| 69 | + CallExecutionTree seg1 = new CallExecutionTree(blockchainId2, contract2, function2); |
| 70 | + CallExecutionTree seg2 = new CallExecutionTree(blockchainId3, contract3, function3); |
| 71 | + |
| 72 | + CallExecutionTree seg3 = new CallExecutionTree(blockchainId5, contract5, function5); |
| 73 | + ArrayList<CallExecutionTree> seg4Calls = new ArrayList<>(); |
| 74 | + seg4Calls.add(seg1); |
| 75 | + seg4Calls.add(seg2); |
| 76 | + CallExecutionTree seg4 = new CallExecutionTree(blockchainId4, contract4, function4, seg4Calls); |
| 77 | + ArrayList<CallExecutionTree> seg5Calls = new ArrayList<>(); |
| 78 | + seg5Calls.add(seg4); |
| 79 | + CallExecutionTree seg5 = new CallExecutionTree(blockchainId6, contract6, function6, seg5Calls); |
| 80 | + CallExecutionTree seg6 = new CallExecutionTree(blockchainId7, contract7, function7); |
| 81 | + CallExecutionTree seg7 = new CallExecutionTree(blockchainId8, contract8, function8); |
| 82 | + |
| 83 | + ArrayList<CallExecutionTree> rootCalls1 = new ArrayList<>(); |
| 84 | + rootCalls1.add(seg3); |
| 85 | + rootCalls1.add(seg5); |
| 86 | + rootCalls1.add(seg6); |
| 87 | + rootCalls1.add(seg7); |
| 88 | + CallExecutionTree root = new CallExecutionTree(blockchainId1, contract1, function1, rootCalls1); |
| 89 | + root.toString(); |
| 90 | + |
| 91 | + byte[] encoded = root.encode(); |
| 92 | + Tuple<BigInteger, String, String> func = extractFunction(encoded, new int[] {0}); |
| 93 | + Assertions.assertEquals(func.getFirst().toString(16), blockchainId1.toPlainString()); |
| 94 | + Assertions.assertEquals(func.getSecond(), contract1); |
| 95 | + Assertions.assertEquals(func.getThird(), function1); |
| 96 | + |
| 97 | + func = extractFunction(encoded, new int[] {1}); |
| 98 | + Assertions.assertEquals(func.getFirst().toString(16), blockchainId5.toPlainString()); |
| 99 | + Assertions.assertEquals(func.getSecond(), contract5); |
| 100 | + Assertions.assertEquals(func.getThird(), function5); |
| 101 | + |
| 102 | + func = extractFunction(encoded, new int[] {2, 0}); |
| 103 | + Assertions.assertEquals(func.getFirst().toString(16), blockchainId6.toPlainString()); |
| 104 | + Assertions.assertEquals(func.getSecond(), contract6); |
| 105 | + Assertions.assertEquals(func.getThird(), function6); |
| 106 | + |
| 107 | + func = extractFunction(encoded, new int[] {2, 1, 0}); |
| 108 | + Assertions.assertEquals(func.getFirst().toString(16), blockchainId4.toPlainString()); |
| 109 | + Assertions.assertEquals(func.getSecond(), contract4); |
| 110 | + Assertions.assertEquals(func.getThird(), function4); |
| 111 | + |
| 112 | + func = extractFunction(encoded, new int[] {2, 1, 1}); |
| 113 | + Assertions.assertEquals(func.getFirst().toString(16), blockchainId2.toPlainString()); |
| 114 | + Assertions.assertEquals(func.getSecond(), contract2); |
| 115 | + Assertions.assertEquals(func.getThird(), function2); |
| 116 | + |
| 117 | + func = extractFunction(encoded, new int[] {2, 1, 2}); |
| 118 | + Assertions.assertEquals(func.getFirst().toString(16), blockchainId3.toPlainString()); |
| 119 | + Assertions.assertEquals(func.getSecond(), contract3); |
| 120 | + Assertions.assertEquals(func.getThird(), function3); |
| 121 | + |
| 122 | + func = extractFunction(encoded, new int[] {3}); |
| 123 | + Assertions.assertEquals(func.getFirst().toString(16), blockchainId7.toPlainString()); |
| 124 | + Assertions.assertEquals(func.getSecond(), contract7); |
| 125 | + Assertions.assertEquals(func.getThird(), function7); |
| 126 | + |
| 127 | + func = extractFunction(encoded, new int[] {4}); |
| 128 | + Assertions.assertEquals(func.getFirst().toString(16), blockchainId8.toPlainString()); |
| 129 | + Assertions.assertEquals(func.getSecond(), contract8); |
| 130 | + Assertions.assertEquals(func.getThird(), function8); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void checkCallTreeFromJs() throws Exception { |
| 135 | + String callTree = |
| 136 | + "0x000100000009000000e3000000000000000000000000000000000000000000000000000000000000001fff7f799bd183b3efd57b5a964cb7cbfec4fbd84400a469f36dcf0000000000000000000000009f2758f2e0e9a5c4ba389035c4c2895232d84b460000000000000000000000000e565f77c56e35d812a1f02356d263320012b3d700000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000944bde1f618e8d7caec2ec10861f6a2b1ef092db000000000000000000000000000000000000000000000000000000000000000020705188c0cf57f6890e2fcd5fb6d21e529071fa5a0084123756cd000000000000000000000000944bde1f618e8d7caec2ec10861f6a2b1ef092db00000000000000000000000000000000000000000000000000000000000000640000000000000000000000009f2758f2e0e9a5c4ba389035c4c2895232d84b46000000000000000000000000bb7c00f1dd65bce0e10a6b6228dff430c9c1c871"; |
| 137 | + Bytes callTreeBytes = Bytes.fromHexString(callTree); |
| 138 | + byte[] callTreeB = callTreeBytes.toArray(); |
| 139 | + System.out.println(CallExecutionTreeEncoderV1.dump(callTreeB)); |
| 140 | + |
| 141 | + Tuple<BigInteger, String, String> root = extractFunction(callTreeB, new int[] {0}); |
| 142 | + Assertions.assertEquals("1f", root.getFirst().toString(16)); |
| 143 | + Assertions.assertEquals("0xff7f799bd183b3efd57b5a964cb7cbfec4fbd844", root.getSecond()); |
| 144 | + Assertions.assertEquals( |
| 145 | + "0x69f36dcf0000000000000000000000009f2758f2e0e9a5c4ba389035c4c2895232d84b460000000000000000000000000e565f77c56e35d812a1f02356d263320012b3d700000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000944bde1f618e8d7caec2ec10861f6a2b1ef092db", |
| 146 | + root.getThird()); |
| 147 | + |
| 148 | + Tuple<BigInteger, String, String> seg = extractFunction(callTreeB, new int[] {1}); |
| 149 | + Assertions.assertEquals("20", seg.getFirst().toString(16)); |
| 150 | + Assertions.assertEquals("0x705188c0cf57f6890e2fcd5fb6d21e529071fa5a", seg.getSecond()); |
| 151 | + Assertions.assertEquals( |
| 152 | + "0x123756cd000000000000000000000000944bde1f618e8d7caec2ec10861f6a2b1ef092db00000000000000000000000000000000000000000000000000000000000000640000000000000000000000009f2758f2e0e9a5c4ba389035c4c2895232d84b46000000000000000000000000bb7c00f1dd65bce0e10a6b6228dff430c9c1c871", |
| 153 | + seg.getThird()); |
| 154 | + } |
| 155 | +} |
0 commit comments