|
| 1 | +package org.fisco.bcos.sdk.v3.contract.precompiled.sharding; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.List; |
| 7 | +import org.fisco.bcos.sdk.v3.client.Client; |
| 8 | +import org.fisco.bcos.sdk.v3.codec.datatypes.Function; |
| 9 | +import org.fisco.bcos.sdk.v3.codec.datatypes.Type; |
| 10 | +import org.fisco.bcos.sdk.v3.codec.datatypes.TypeReference; |
| 11 | +import org.fisco.bcos.sdk.v3.codec.datatypes.Utf8String; |
| 12 | +import org.fisco.bcos.sdk.v3.codec.datatypes.generated.Int256; |
| 13 | +import org.fisco.bcos.sdk.v3.codec.datatypes.generated.tuples.generated.Tuple1; |
| 14 | +import org.fisco.bcos.sdk.v3.codec.datatypes.generated.tuples.generated.Tuple2; |
| 15 | +import org.fisco.bcos.sdk.v3.contract.Contract; |
| 16 | +import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; |
| 17 | +import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair; |
| 18 | +import org.fisco.bcos.sdk.v3.model.CryptoType; |
| 19 | +import org.fisco.bcos.sdk.v3.model.TransactionReceipt; |
| 20 | +import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback; |
| 21 | +import org.fisco.bcos.sdk.v3.transaction.model.exception.ContractException; |
| 22 | + |
| 23 | +@SuppressWarnings("unchecked") |
| 24 | +public class ShardingPrecompiled extends Contract { |
| 25 | + public static final String[] BINARY_ARRAY = { |
| 26 | + "608060405234801561001057600080fd5b5061027f806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80631d82d998146100465780639c1284bc1461006d578063b7ede6cb14610092575b600080fd5b61005a61005436600461014b565b50600090565b6040519081526020015b60405180910390f35b61008461007b36600461014b565b60006060915091565b604051610064929190610188565b61005a6100a03660046101e5565b600092915050565b634e487b7160e01b600052604160045260246000fd5b600082601f8301126100cf57600080fd5b813567ffffffffffffffff808211156100ea576100ea6100a8565b604051601f8301601f19908116603f01168101908282118183101715610112576101126100a8565b8160405283815286602085880101111561012b57600080fd5b836020870160208301376000602085830101528094505050505092915050565b60006020828403121561015d57600080fd5b813567ffffffffffffffff81111561017457600080fd5b610180848285016100be565b949350505050565b82815260006020604081840152835180604085015260005b818110156101bc578581018301518582016060015282016101a0565b818111156101ce576000606083870101525b50601f01601f191692909201606001949350505050565b600080604083850312156101f857600080fd5b823567ffffffffffffffff8082111561021057600080fd5b61021c868387016100be565b9350602085013591508082111561023257600080fd5b5061023f858286016100be565b915050925092905056fea26469706673582212201d63f9baf4cb69a7122bb65d9774a882029582eb9471f8bcc9e1bccd9fb7636164736f6c634300080b0033" |
| 27 | + }; |
| 28 | + |
| 29 | + public static final String BINARY = |
| 30 | + org.fisco.bcos.sdk.v3.utils.StringUtils.joinAll("", BINARY_ARRAY); |
| 31 | + |
| 32 | + public static final String[] SM_BINARY_ARRAY = { |
| 33 | + "608060405234801561001057600080fd5b50610283806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c806346be3ae114610046578063d31591d214610074578063f76f20ca14610098575b600080fd5b61005d61005436600461014f565b60006060915091565b60405161006b92919061018c565b60405180910390f35b61008a6100823660046101e9565b600092915050565b60405190815260200161006b565b61008a6100a636600461014f565b50600090565b63b95aa35560e01b600052604160045260246000fd5b600082601f8301126100d357600080fd5b813567ffffffffffffffff808211156100ee576100ee6100ac565b604051601f8301601f19908116603f01168101908282118183101715610116576101166100ac565b8160405283815286602085880101111561012f57600080fd5b836020870160208301376000602085830101528094505050505092915050565b60006020828403121561016157600080fd5b813567ffffffffffffffff81111561017857600080fd5b610184848285016100c2565b949350505050565b82815260006020604081840152835180604085015260005b818110156101c0578581018301518582016060015282016101a4565b818111156101d2576000606083870101525b50601f01601f191692909201606001949350505050565b600080604083850312156101fc57600080fd5b823567ffffffffffffffff8082111561021457600080fd5b610220868387016100c2565b9350602085013591508082111561023657600080fd5b50610243858286016100c2565b915050925092905056fea2646970667358221220afd1216fc346bdc04dbbace27779ff191659b571289147caa7781b74ab2a3b3064736f6c634300080b0033" |
| 34 | + }; |
| 35 | + |
| 36 | + public static final String SM_BINARY = |
| 37 | + org.fisco.bcos.sdk.v3.utils.StringUtils.joinAll("", SM_BINARY_ARRAY); |
| 38 | + |
| 39 | + public static final String[] ABI_ARRAY = { |
| 40 | + "[{\"inputs\":[{\"internalType\":\"string\",\"name\":\"absolutePath\",\"type\":\"string\"}],\"name\":\"getContractShard\",\"outputs\":[{\"internalType\":\"int256\",\"name\":\"\",\"type\":\"int256\"},{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"string\",\"name\":\"shardName\",\"type\":\"string\"},{\"internalType\":\"string\",\"name\":\"_address\",\"type\":\"string\"}],\"name\":\"linkShard\",\"outputs\":[{\"internalType\":\"int256\",\"name\":\"\",\"type\":\"int256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"string\",\"name\":\"shardName\",\"type\":\"string\"}],\"name\":\"makeShard\",\"outputs\":[{\"internalType\":\"int256\",\"name\":\"\",\"type\":\"int256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]" |
| 41 | + }; |
| 42 | + |
| 43 | + public static final String ABI = org.fisco.bcos.sdk.v3.utils.StringUtils.joinAll("", ABI_ARRAY); |
| 44 | + |
| 45 | + public static final String FUNC_GETCONTRACTSHARD = "getContractShard"; |
| 46 | + |
| 47 | + public static final String FUNC_LINKSHARD = "linkShard"; |
| 48 | + |
| 49 | + public static final String FUNC_MAKESHARD = "makeShard"; |
| 50 | + |
| 51 | + protected ShardingPrecompiled(String contractAddress, Client client, CryptoKeyPair credential) { |
| 52 | + super(getBinary(client.getCryptoSuite()), contractAddress, client, credential); |
| 53 | + } |
| 54 | + |
| 55 | + public static String getBinary(CryptoSuite cryptoSuite) { |
| 56 | + return (cryptoSuite.getCryptoTypeConfig() == CryptoType.ECDSA_TYPE ? BINARY : SM_BINARY); |
| 57 | + } |
| 58 | + |
| 59 | + public static String getABI() { |
| 60 | + return ABI; |
| 61 | + } |
| 62 | + |
| 63 | + public Tuple2<BigInteger, String> getContractShard(String absolutePath) |
| 64 | + throws ContractException { |
| 65 | + final Function function = |
| 66 | + new Function( |
| 67 | + FUNC_GETCONTRACTSHARD, |
| 68 | + Arrays.<Type>asList(new Utf8String(absolutePath)), |
| 69 | + Arrays.<TypeReference<?>>asList( |
| 70 | + new TypeReference<Int256>() {}, |
| 71 | + new TypeReference<Utf8String>() {})); |
| 72 | + List<Type> results = executeCallWithMultipleValueReturn(function); |
| 73 | + return new Tuple2<BigInteger, String>( |
| 74 | + (BigInteger) results.get(0).getValue(), (String) results.get(1).getValue()); |
| 75 | + } |
| 76 | + |
| 77 | + public TransactionReceipt linkShard(String shardName, String _address) { |
| 78 | + final Function function = |
| 79 | + new Function( |
| 80 | + FUNC_LINKSHARD, |
| 81 | + Arrays.<Type>asList(new Utf8String(shardName), new Utf8String(_address)), |
| 82 | + Collections.<TypeReference<?>>emptyList(), |
| 83 | + 0); |
| 84 | + return executeTransaction(function); |
| 85 | + } |
| 86 | + |
| 87 | + public String linkShard(String shardName, String _address, TransactionCallback callback) { |
| 88 | + final Function function = |
| 89 | + new Function( |
| 90 | + FUNC_LINKSHARD, |
| 91 | + Arrays.<Type>asList(new Utf8String(shardName), new Utf8String(_address)), |
| 92 | + Collections.<TypeReference<?>>emptyList(), |
| 93 | + 0); |
| 94 | + return asyncExecuteTransaction(function, callback); |
| 95 | + } |
| 96 | + |
| 97 | + public String getSignedTransactionForLinkShard(String shardName, String _address) { |
| 98 | + final Function function = |
| 99 | + new Function( |
| 100 | + FUNC_LINKSHARD, |
| 101 | + Arrays.<Type>asList(new Utf8String(shardName), new Utf8String(_address)), |
| 102 | + Collections.<TypeReference<?>>emptyList(), |
| 103 | + 0); |
| 104 | + return createSignedTransaction(function); |
| 105 | + } |
| 106 | + |
| 107 | + public Tuple2<String, String> getLinkShardInput(TransactionReceipt transactionReceipt) { |
| 108 | + String data = transactionReceipt.getInput().substring(10); |
| 109 | + final Function function = |
| 110 | + new Function( |
| 111 | + FUNC_LINKSHARD, |
| 112 | + Arrays.<Type>asList(), |
| 113 | + Arrays.<TypeReference<?>>asList( |
| 114 | + new TypeReference<Utf8String>() {}, |
| 115 | + new TypeReference<Utf8String>() {})); |
| 116 | + List<Type> results = |
| 117 | + this.functionReturnDecoder.decode(data, function.getOutputParameters()); |
| 118 | + return new Tuple2<String, String>( |
| 119 | + (String) results.get(0).getValue(), (String) results.get(1).getValue()); |
| 120 | + } |
| 121 | + |
| 122 | + public Tuple1<BigInteger> getLinkShardOutput(TransactionReceipt transactionReceipt) { |
| 123 | + String data = transactionReceipt.getOutput(); |
| 124 | + final Function function = |
| 125 | + new Function( |
| 126 | + FUNC_LINKSHARD, |
| 127 | + Arrays.<Type>asList(), |
| 128 | + Arrays.<TypeReference<?>>asList(new TypeReference<Int256>() {})); |
| 129 | + List<Type> results = |
| 130 | + this.functionReturnDecoder.decode(data, function.getOutputParameters()); |
| 131 | + return new Tuple1<BigInteger>((BigInteger) results.get(0).getValue()); |
| 132 | + } |
| 133 | + |
| 134 | + public TransactionReceipt makeShard(String shardName) { |
| 135 | + final Function function = |
| 136 | + new Function( |
| 137 | + FUNC_MAKESHARD, |
| 138 | + Arrays.<Type>asList(new Utf8String(shardName)), |
| 139 | + Collections.<TypeReference<?>>emptyList(), |
| 140 | + 0); |
| 141 | + return executeTransaction(function); |
| 142 | + } |
| 143 | + |
| 144 | + public String makeShard(String shardName, TransactionCallback callback) { |
| 145 | + final Function function = |
| 146 | + new Function( |
| 147 | + FUNC_MAKESHARD, |
| 148 | + Arrays.<Type>asList(new Utf8String(shardName)), |
| 149 | + Collections.<TypeReference<?>>emptyList(), |
| 150 | + 0); |
| 151 | + return asyncExecuteTransaction(function, callback); |
| 152 | + } |
| 153 | + |
| 154 | + public String getSignedTransactionForMakeShard(String shardName) { |
| 155 | + final Function function = |
| 156 | + new Function( |
| 157 | + FUNC_MAKESHARD, |
| 158 | + Arrays.<Type>asList(new Utf8String(shardName)), |
| 159 | + Collections.<TypeReference<?>>emptyList(), |
| 160 | + 0); |
| 161 | + return createSignedTransaction(function); |
| 162 | + } |
| 163 | + |
| 164 | + public Tuple1<String> getMakeShardInput(TransactionReceipt transactionReceipt) { |
| 165 | + String data = transactionReceipt.getInput().substring(10); |
| 166 | + final Function function = |
| 167 | + new Function( |
| 168 | + FUNC_MAKESHARD, |
| 169 | + Arrays.<Type>asList(), |
| 170 | + Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {})); |
| 171 | + List<Type> results = |
| 172 | + this.functionReturnDecoder.decode(data, function.getOutputParameters()); |
| 173 | + return new Tuple1<String>((String) results.get(0).getValue()); |
| 174 | + } |
| 175 | + |
| 176 | + public Tuple1<BigInteger> getMakeShardOutput(TransactionReceipt transactionReceipt) { |
| 177 | + String data = transactionReceipt.getOutput(); |
| 178 | + final Function function = |
| 179 | + new Function( |
| 180 | + FUNC_MAKESHARD, |
| 181 | + Arrays.<Type>asList(), |
| 182 | + Arrays.<TypeReference<?>>asList(new TypeReference<Int256>() {})); |
| 183 | + List<Type> results = |
| 184 | + this.functionReturnDecoder.decode(data, function.getOutputParameters()); |
| 185 | + return new Tuple1<BigInteger>((BigInteger) results.get(0).getValue()); |
| 186 | + } |
| 187 | + |
| 188 | + public static ShardingPrecompiled load( |
| 189 | + String contractAddress, Client client, CryptoKeyPair credential) { |
| 190 | + return new ShardingPrecompiled(contractAddress, client, credential); |
| 191 | + } |
| 192 | +} |
0 commit comments