|
| 1 | +package org.tron.common.runtime; |
| 2 | + |
| 3 | +import org.spongycastle.util.encoders.Hex; |
| 4 | +import org.tron.common.runtime.vm.program.ProgramResult; |
| 5 | +import org.tron.common.runtime.vm.program.invoke.ProgramInvokeFactory; |
| 6 | +import org.tron.common.runtime.vm.program.invoke.ProgramInvokeFactoryImpl; |
| 7 | +import org.tron.common.storage.Deposit; |
| 8 | +import org.tron.common.storage.DepositImpl; |
| 9 | +import org.tron.common.storage.DepositQueue; |
| 10 | +import org.tron.common.storage.Key; |
| 11 | +import org.tron.common.storage.Type; |
| 12 | +import org.tron.common.storage.Value; |
| 13 | +import org.tron.core.capsule.BlockCapsule; |
| 14 | +import org.tron.core.capsule.TransactionCapsule; |
| 15 | +import org.tron.core.db.Manager; |
| 16 | + |
| 17 | +import static org.tron.common.runtime.utils.MUtil.transfer; |
| 18 | + |
| 19 | +/** |
| 20 | + * Deposit controller : process pre transaction , block, query contract and etc.. |
| 21 | + * |
| 22 | + * @author Guo Yonggang |
| 23 | + * @since 27.04.2018 |
| 24 | + */ |
| 25 | +public class DepositController { |
| 26 | + |
| 27 | + private Manager dbManager; |
| 28 | + private ProgramInvokeFactory programInvokeFactory = new ProgramInvokeFactoryImpl();; |
| 29 | + private static int QUEUE_SIZE = 4; |
| 30 | + private DepositQueue<Deposit> depositQueue = new DepositQueue<>(QUEUE_SIZE); |
| 31 | + |
| 32 | + private DepositController(Manager dbManager) { |
| 33 | + this.dbManager = dbManager; |
| 34 | + } |
| 35 | + |
| 36 | + private Deposit getLastDeposit() { |
| 37 | + if (depositQueue.isEmpty()) return null; |
| 38 | + return depositQueue.last(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @API roll back one block's data |
| 43 | + */ |
| 44 | + public Deposit rollback() { |
| 45 | + Deposit deposit = depositQueue.removeLast(); |
| 46 | + if (getLastDeposit() != null) getLastDeposit().setNextDeposit(null); |
| 47 | + return deposit; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * The trx may be invalid due to check with not newest data. |
| 52 | + * |
| 53 | + * @param trxCap |
| 54 | + * @return |
| 55 | + */ |
| 56 | + public int preProcessTransaction(TransactionCapsule trxCap) { |
| 57 | + DepositImpl deposit = DepositImpl.createRoot(dbManager); |
| 58 | + Runtime runtime = new Runtime(trxCap.getInstance(), deposit, programInvokeFactory); |
| 59 | + runtime.execute(); |
| 60 | + runtime.go(); |
| 61 | + ProgramResult programResult = runtime.getResult(); |
| 62 | + if (programResult.getException() != null) { |
| 63 | + return -1; |
| 64 | + } |
| 65 | + |
| 66 | + return 0; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param block |
| 71 | + * @return |
| 72 | + */ |
| 73 | + public int processBlock(BlockCapsule block) { |
| 74 | + Deposit lastDeposit = getLastDeposit(); |
| 75 | + Deposit currentDeposit; |
| 76 | + if (lastDeposit == null) { |
| 77 | + currentDeposit = DepositImpl.createRoot(dbManager); |
| 78 | + } else { |
| 79 | + currentDeposit = lastDeposit.newDepositNext(); |
| 80 | + } |
| 81 | + |
| 82 | + depositQueue.put(currentDeposit); |
| 83 | + for (TransactionCapsule trxCap : block.getTransactions()) { |
| 84 | + Deposit trxDeposit = currentDeposit.newDepositChild(); |
| 85 | + Runtime runtime = new Runtime(trxCap.getInstance(), block.getInstance(), trxDeposit, |
| 86 | + programInvokeFactory); |
| 87 | + runtime.execute(); |
| 88 | + runtime.go(); |
| 89 | + |
| 90 | + ProgramResult programResult = runtime.getResult(); |
| 91 | + if (programResult.getException() != null) { |
| 92 | + rollback(); |
| 93 | + return -1; |
| 94 | + } |
| 95 | + |
| 96 | + Key key = Key.create(trxCap.getTransactionId().getBytes()); |
| 97 | + Value value = Value.create(trxCap.getData(), Type.VALUE_TYPE_CREATE); |
| 98 | + currentDeposit.putTransaction(key, value); |
| 99 | + } |
| 100 | + |
| 101 | + Key bKey = Key.create(block.getBlockId().getBytes()); |
| 102 | + Value bValue = Value.create(block.getData(), Type.VALUE_TYPE_CREATE); |
| 103 | + currentDeposit.putBlock(bKey, bValue); |
| 104 | + |
| 105 | + // reward witness node |
| 106 | + byte[] foundAddress = Hex.decode("FF00"); |
| 107 | + byte[] coinBase = block.getWitnessAddress().toByteArray(); |
| 108 | + transfer(currentDeposit, foundAddress, coinBase, 36 * 1000000); |
| 109 | + |
| 110 | + |
| 111 | + if (depositQueue.size() > QUEUE_SIZE) { |
| 112 | + Deposit deposit = depositQueue.get(); |
| 113 | + deposit.commit(); |
| 114 | + depositQueue.peek().setPrevDeposit(null); |
| 115 | + } |
| 116 | + |
| 117 | + return 0; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * |
| 122 | + * @param trxCap |
| 123 | + * @return |
| 124 | + */ |
| 125 | + public ProgramResult processConstantTransaction(TransactionCapsule trxCap) { |
| 126 | + DepositImpl deposit = DepositImpl.createRoot(dbManager); |
| 127 | + Runtime runtime = new Runtime(trxCap.getInstance(), programInvokeFactory, deposit); |
| 128 | + runtime.execute(); |
| 129 | + runtime.go(); |
| 130 | + ProgramResult programResult = runtime.getResult(); |
| 131 | + return programResult; |
| 132 | + } |
| 133 | + |
| 134 | + public ProgramInvokeFactory getProgramInvokeFactory() { |
| 135 | + return programInvokeFactory; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Single instance |
| 140 | + */ |
| 141 | + private static DepositController instance = null; |
| 142 | + public static DepositController create(Manager dbManager) { |
| 143 | + if (instance != null) return instance; |
| 144 | + instance = new DepositController(dbManager); |
| 145 | + return instance; |
| 146 | + } |
| 147 | + |
| 148 | + public static DepositController getInstance() { |
| 149 | + return instance; |
| 150 | + } |
| 151 | +} |
0 commit comments