|
| 1 | +/* |
| 2 | + * Copyright 2020 ICONLOOP Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.iconloop.testsvc; |
| 18 | + |
| 19 | +import score.Address; |
| 20 | + |
| 21 | +import java.lang.reflect.Constructor; |
| 22 | +import java.lang.reflect.InvocationTargetException; |
| 23 | +import java.math.BigInteger; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Random; |
| 27 | +import java.util.Stack; |
| 28 | + |
| 29 | +public class ServiceManager { |
| 30 | + private static final BigInteger ICX = BigInteger.TEN.pow(18); |
| 31 | + |
| 32 | + private final Stack<Frame> contexts = new Stack<>(); |
| 33 | + private final Map<Class<?>, Score> classScoreMap = new HashMap<>(); |
| 34 | + private final Map<Address, Score> addressScoreMap = new HashMap<>(); |
| 35 | + private int nextCount = 1; |
| 36 | + |
| 37 | + public Score deploy(Account owner, Class<?> mainClass, Object... params) throws Exception { |
| 38 | + getBlock().increase(); |
| 39 | + var score = new Score(Account.newScoreAccount(nextCount++), owner); |
| 40 | + classScoreMap.put(mainClass, score); |
| 41 | + addressScoreMap.put(score.getAddress(), score); |
| 42 | + pushFrame(owner, score.getAccount(), false, "<init>", BigInteger.ZERO); |
| 43 | + try { |
| 44 | + Class<?>[] paramClasses = new Class<?>[params.length]; |
| 45 | + for (int i = 0; i < params.length; i++) { |
| 46 | + paramClasses[i] = params[i].getClass(); |
| 47 | + } |
| 48 | + Constructor<?> ctor = mainClass.getConstructor(paramClasses); |
| 49 | + score.setInstance(ctor.newInstance(params)); |
| 50 | + } catch (InstantiationException | InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { |
| 51 | + e.printStackTrace(); |
| 52 | + throw e; |
| 53 | + } finally { |
| 54 | + popFrame(); |
| 55 | + } |
| 56 | + return score; |
| 57 | + } |
| 58 | + |
| 59 | + public Account createAccount() { |
| 60 | + return createAccount(0); |
| 61 | + } |
| 62 | + |
| 63 | + public Account createAccount(int initialIcx) { |
| 64 | + var acct = Account.newExternalAccount(nextCount++); |
| 65 | + acct.addBalance("ICX", ICX.multiply(BigInteger.valueOf(initialIcx))); |
| 66 | + return acct; |
| 67 | + } |
| 68 | + |
| 69 | + public Address getOwner() { |
| 70 | + var address = getCurrentFrame().to.getAddress(); |
| 71 | + return getScoreFromAddress(address).getOwner().getAddress(); |
| 72 | + } |
| 73 | + |
| 74 | + public Address getOrigin() { |
| 75 | + return getFirstFrame().from.getAddress(); |
| 76 | + } |
| 77 | + |
| 78 | + public Address getCaller() { |
| 79 | + return getCurrentFrame().from.getAddress(); |
| 80 | + } |
| 81 | + |
| 82 | + public Address getAddress() { |
| 83 | + return getCurrentFrame().to.getAddress(); |
| 84 | + } |
| 85 | + |
| 86 | + private Score getScoreFromClass(Class<?> caller) { |
| 87 | + var score = classScoreMap.get(caller); |
| 88 | + if (score == null) { |
| 89 | + throw new IllegalStateException(caller.getName() + " not found"); |
| 90 | + } |
| 91 | + return score; |
| 92 | + } |
| 93 | + |
| 94 | + private Score getScoreFromAddress(Address target) { |
| 95 | + var score = addressScoreMap.get(target); |
| 96 | + if (score == null) { |
| 97 | + throw new IllegalStateException("ScoreNotFound"); |
| 98 | + } |
| 99 | + return score; |
| 100 | + } |
| 101 | + |
| 102 | + public Object call(Account from, BigInteger value, Address targetAddress, String method, Object... params) { |
| 103 | + Score score = getScoreFromAddress(targetAddress); |
| 104 | + return score.call(from, false, value, method, params); |
| 105 | + } |
| 106 | + |
| 107 | + public Object call(Class<?> caller, BigInteger value, Address targetAddress, String method, Object... params) { |
| 108 | + Score from = getScoreFromClass(caller); |
| 109 | + if ("fallback".equals(method) || "".equals(method)) { |
| 110 | + transfer(from.getAccount(), targetAddress, value); |
| 111 | + return null; |
| 112 | + } else { |
| 113 | + return call(from.getAccount(), value, targetAddress, method, params); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public void transfer(Account from, Address targetAddress, BigInteger value) { |
| 118 | + getBlock().increase(); |
| 119 | + var fromBalance = from.getBalance(); |
| 120 | + if (fromBalance.compareTo(value) < 0) { |
| 121 | + throw new IllegalStateException("OutOfBalance"); |
| 122 | + } |
| 123 | + var to = Account.getAccount(targetAddress); |
| 124 | + if (to == null) { |
| 125 | + throw new IllegalStateException("NoAccount"); |
| 126 | + } |
| 127 | + from.subtractBalance("ICX", value); |
| 128 | + to.addBalance("ICX", value); |
| 129 | + if (targetAddress.isContract()) { |
| 130 | + call(from, value, targetAddress, "fallback"); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public static class Block { |
| 135 | + private static Block sInstance; |
| 136 | + |
| 137 | + private long height; |
| 138 | + private long timestamp; |
| 139 | + |
| 140 | + public Block(long height, long timestamp) { |
| 141 | + this.height = height; |
| 142 | + this.timestamp = timestamp; |
| 143 | + } |
| 144 | + |
| 145 | + public static Block getInstance() { |
| 146 | + if (sInstance == null) { |
| 147 | + Random rand = new Random(); |
| 148 | + sInstance = new Block(rand.nextInt(1000), System.nanoTime() * 1000); |
| 149 | + } |
| 150 | + return sInstance; |
| 151 | + } |
| 152 | + |
| 153 | + public long getHeight() { |
| 154 | + return height; |
| 155 | + } |
| 156 | + |
| 157 | + public long getTimestamp() { |
| 158 | + return timestamp; |
| 159 | + } |
| 160 | + |
| 161 | + public void increase() { |
| 162 | + increase(1); |
| 163 | + } |
| 164 | + |
| 165 | + public void increase(long delta) { |
| 166 | + height += delta; |
| 167 | + timestamp = System.nanoTime() * 1000; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + public Block getBlock() { |
| 172 | + return Block.getInstance(); |
| 173 | + } |
| 174 | + |
| 175 | + public static class Frame { |
| 176 | + Account from; |
| 177 | + Account to; |
| 178 | + String method; |
| 179 | + boolean readonly; |
| 180 | + BigInteger value; |
| 181 | + |
| 182 | + public Frame(Account from, Account to, boolean readonly, String method, BigInteger value) { |
| 183 | + this.from = from; |
| 184 | + this.to = to; |
| 185 | + this.readonly = readonly; |
| 186 | + this.method = method; |
| 187 | + this.value = value; |
| 188 | + } |
| 189 | + |
| 190 | + public boolean isReadonly() { |
| 191 | + return readonly; |
| 192 | + } |
| 193 | + |
| 194 | + public BigInteger getValue() { |
| 195 | + return value; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + protected void pushFrame(Account from, Account to, boolean readonly, String method, BigInteger value) { |
| 200 | + contexts.push(new Frame(from, to, readonly, method, value)); |
| 201 | + } |
| 202 | + |
| 203 | + protected void popFrame() { |
| 204 | + contexts.pop(); |
| 205 | + } |
| 206 | + |
| 207 | + public Frame getCurrentFrame() { |
| 208 | + return contexts.peek(); |
| 209 | + } |
| 210 | + |
| 211 | + public Frame getFirstFrame() { |
| 212 | + return contexts.firstElement(); |
| 213 | + } |
| 214 | +} |
0 commit comments