diff --git a/src/main/java/com/github/ontio/OntSdk.java b/src/main/java/com/github/ontio/OntSdk.java index eb06065c..7a6b02a0 100644 --- a/src/main/java/com/github/ontio/OntSdk.java +++ b/src/main/java/com/github/ontio/OntSdk.java @@ -71,6 +71,7 @@ public class OntSdk { public SignatureScheme defaultSignScheme = SignatureScheme.SHA256WITHECDSA; public long DEFAULT_GAS_LIMIT = 20000; public long DEFAULT_DEPLOY_GAS_LIMIT = 30000000; + public long sideChainId; public static synchronized OntSdk getInstance(){ if(instance == null){ instance = new OntSdk(); @@ -157,6 +158,9 @@ public ConnectMgr getSideChainConnectMgr() { public void setSideChainRpc(String url) { this.sideChainConnectMgr = new ConnectMgr(url, "rpc"); } + public void setSideChainId(long sideChainId) { + this.sideChainId = sideChainId; + } public void setSideChainRest(String url) { this.sideChainConnectMgr = new ConnectMgr(url, "restful"); } diff --git a/src/main/java/com/github/ontio/core/sidechaingovernance/InflationParam.java b/src/main/java/com/github/ontio/core/sidechaingovernance/InflationParam.java index 04c36767..c0220325 100644 --- a/src/main/java/com/github/ontio/core/sidechaingovernance/InflationParam.java +++ b/src/main/java/com/github/ontio/core/sidechaingovernance/InflationParam.java @@ -4,11 +4,11 @@ import com.github.ontio.common.Address; public class InflationParam { - public String sideChainId; + public long sideChainId; public Address address; public long depositAdd; public long ongPoolAdd; - public InflationParam(String sideChainId, Address address, long depositAdd, long ongPoolAdd){ + public InflationParam(long sideChainId, Address address, long depositAdd, long ongPoolAdd){ this.sideChainId = sideChainId; this.address = address; this.depositAdd = depositAdd; diff --git a/src/main/java/com/github/ontio/core/sidechaingovernance/NodeToSideChainParams.java b/src/main/java/com/github/ontio/core/sidechaingovernance/NodeToSideChainParams.java index 9082ea2b..9416e665 100644 --- a/src/main/java/com/github/ontio/core/sidechaingovernance/NodeToSideChainParams.java +++ b/src/main/java/com/github/ontio/core/sidechaingovernance/NodeToSideChainParams.java @@ -11,9 +11,9 @@ public class NodeToSideChainParams implements Serializable { public String peerPubkey; public Address address; - public String sideChainId; + public long sideChainId; public NodeToSideChainParams(){} - public NodeToSideChainParams(String peerPubkey, Address address, String sideChainId){ + public NodeToSideChainParams(String peerPubkey, Address address, long sideChainId){ this.peerPubkey = peerPubkey; this.address = address; this.sideChainId = sideChainId; @@ -23,7 +23,7 @@ public NodeToSideChainParams(String peerPubkey, Address address, String sideChai public void deserialize(BinaryReader reader) throws IOException { this.peerPubkey = reader.readVarString(); this.address = utils.readAddress(reader); - this.sideChainId = reader.readVarString(); + this.sideChainId = reader.readLong(); } @Override diff --git a/src/main/java/com/github/ontio/core/sidechaingovernance/QuitSideChainParam.java b/src/main/java/com/github/ontio/core/sidechaingovernance/QuitSideChainParam.java index 7b03fcec..09f4e2d1 100644 --- a/src/main/java/com/github/ontio/core/sidechaingovernance/QuitSideChainParam.java +++ b/src/main/java/com/github/ontio/core/sidechaingovernance/QuitSideChainParam.java @@ -4,9 +4,9 @@ import com.github.ontio.common.Address; public class QuitSideChainParam { - public String sideChainID; + public long sideChainID; public Address address; - public QuitSideChainParam(String sideChainID, Address address){ + public QuitSideChainParam(long sideChainID, Address address){ this.sideChainID = sideChainID; this.address = address; } diff --git a/src/main/java/com/github/ontio/core/sidechaingovernance/RegisterSideChainParam.java b/src/main/java/com/github/ontio/core/sidechaingovernance/RegisterSideChainParam.java index c9d1dd11..2a02e1ac 100644 --- a/src/main/java/com/github/ontio/core/sidechaingovernance/RegisterSideChainParam.java +++ b/src/main/java/com/github/ontio/core/sidechaingovernance/RegisterSideChainParam.java @@ -4,19 +4,21 @@ import com.github.ontio.common.Address; public class RegisterSideChainParam { - public String sideChainID; + public long sideChainID; public Address address; public int ratio; public long deposit; public long ongPool; + public byte[] genesisBlock; public byte[] caller; public int keyNo; - public RegisterSideChainParam(String sideChainID, Address address, int ratio, long deposit, long ongPool, byte[] caller, int keyNo){ + public RegisterSideChainParam(long sideChainID, Address address, int ratio, long deposit, long ongPool,byte[] genesisBlock, byte[] caller, int keyNo){ this.sideChainID = sideChainID; this.address = address; this.ratio = ratio; this.deposit = deposit; this.ongPool = ongPool; + this.genesisBlock = genesisBlock; this.caller = caller; this.keyNo = keyNo; } diff --git a/src/main/java/com/github/ontio/core/sidechaingovernance/SideChain.java b/src/main/java/com/github/ontio/core/sidechaingovernance/SideChain.java index e707f93f..ba169db2 100644 --- a/src/main/java/com/github/ontio/core/sidechaingovernance/SideChain.java +++ b/src/main/java/com/github/ontio/core/sidechaingovernance/SideChain.java @@ -2,6 +2,7 @@ import com.alibaba.fastjson.JSON; import com.github.ontio.common.Address; +import com.github.ontio.common.Helper; import com.github.ontio.io.BinaryReader; import com.github.ontio.io.BinaryWriter; import com.github.ontio.io.Serializable; @@ -11,18 +12,19 @@ import java.util.Map; public class SideChain implements Serializable { - public String sideChainId; + public long sideChainId; public Address address; public long ratio; public long deposit; public long ongNum; public long ongPool; public byte status; + public byte[] genesisBlock; public SideChain(){} @Override public void deserialize(BinaryReader reader) throws IOException { - this.sideChainId = reader.readVarString(); + this.sideChainId = reader.readLong(); try { this.address = reader.readSerializable(Address.class); } catch (InstantiationException e) { @@ -35,6 +37,7 @@ public void deserialize(BinaryReader reader) throws IOException { this.ongNum = reader.readLong(); this.ongPool = reader.readLong(); this.status = reader.readByte(); + this.genesisBlock = reader.readVarBytes(); } @Override @@ -50,6 +53,7 @@ public String toJson(){ map.put("ongNum", this.ongNum); map.put("ongPool", this.ongPool); map.put("status", this.status); + map.put("genesisBlock", Helper.toHexString(genesisBlock)); return JSON.toJSONString(map); } } diff --git a/src/main/java/com/github/ontio/core/sidechaingovernance/SwapParam.java b/src/main/java/com/github/ontio/core/sidechaingovernance/SwapParam.java index 0ec63557..a4e045a4 100644 --- a/src/main/java/com/github/ontio/core/sidechaingovernance/SwapParam.java +++ b/src/main/java/com/github/ontio/core/sidechaingovernance/SwapParam.java @@ -8,10 +8,10 @@ import java.util.Map; public class SwapParam { - public String sideChainId; + public long sideChainId; public Address address; public long ongXAccount; - public SwapParam(String sideChainId, Address address, long ongXAccount){ + public SwapParam(long sideChainId, Address address, long ongXAccount){ this.sideChainId = sideChainId; this.address = address; this.ongXAccount = ongXAccount; diff --git a/src/main/java/com/github/ontio/network/connect/IConnector.java b/src/main/java/com/github/ontio/network/connect/IConnector.java index d1e6d2f3..6dd13918 100644 --- a/src/main/java/com/github/ontio/network/connect/IConnector.java +++ b/src/main/java/com/github/ontio/network/connect/IConnector.java @@ -35,6 +35,8 @@ public interface IConnector { int getBlockHeight() throws ConnectorException, IOException; Block getBlock(int height) throws ConnectorException, IOException; Block getBlock(String hash) throws ConnectorException, IOException ; + String getBlockBytes(int height) throws ConnectorException, IOException ; + String getBlockBytes(String hash) throws ConnectorException, IOException ; Object getBlockJson(int height) throws ConnectorException, IOException; Object getBlockJson(String hash) throws ConnectorException, IOException; @@ -54,5 +56,5 @@ public interface IConnector { String getVersion() throws ConnectorException, IOException; String getGrantOng(String address) throws ConnectorException, IOException; int getNetworkId() throws ConnectorException, IOException; - String getSideChainData(String sideChainID) throws ConnectorException, IOException; + String getSideChainData(int sideChainID) throws ConnectorException, IOException; } \ No newline at end of file diff --git a/src/main/java/com/github/ontio/network/rest/RestClient.java b/src/main/java/com/github/ontio/network/rest/RestClient.java index 117c54ea..6afc311f 100644 --- a/src/main/java/com/github/ontio/network/rest/RestClient.java +++ b/src/main/java/com/github/ontio/network/rest/RestClient.java @@ -129,6 +129,25 @@ public Block getBlock(String hash) throws RestfulException { throw new RestfulException(ErrorCode.BlockDeserializeError, e); } } + @Override + public String getBlockBytes(int height) throws ConnectorException, IOException { + String rs = api.getBlock(height, "1"); + Result rr = JSON.parseObject(rs, Result.class); + if (rr.Error != 0) { + throw new RestfulException(to(rr)); + } + return (String) rr.Result; + } + + @Override + public String getBlockBytes(String hash) throws ConnectorException, IOException { + String rs = api.getBlock(hash, "1"); + Result rr = JSON.parseObject(rs, Result.class); + if (rr.Error != 0) { + throw new RestfulException(to(rr)); + } + return (String) rr.Result; + } @Override public Object getBalance(String address) throws RestfulException { @@ -299,7 +318,7 @@ public int getNetworkId() throws ConnectorException, IOException { } @Override - public String getSideChainData(String sideChainID) throws ConnectorException, IOException { + public String getSideChainData(int sideChainID) throws ConnectorException, IOException { return null; } diff --git a/src/main/java/com/github/ontio/network/rpc/RpcClient.java b/src/main/java/com/github/ontio/network/rpc/RpcClient.java index d13368a4..eaa07726 100644 --- a/src/main/java/com/github/ontio/network/rpc/RpcClient.java +++ b/src/main/java/com/github/ontio/network/rpc/RpcClient.java @@ -167,6 +167,18 @@ public Block getBlock(String hash) throws ConnectorException, IOException { } } + @Override + public String getBlockBytes(int height) throws ConnectorException, IOException { + Object result = rpc.call("getblock",height); + return (String) result; + } + + @Override + public String getBlockBytes(String hash) throws ConnectorException, IOException { + Object result = rpc.call("getblock",hash); + return (String) result; + } + @Override public Object getSmartCodeEvent(int height) throws ConnectorException, IOException { Object result = rpc.call("getsmartcodeevent", height); @@ -273,7 +285,7 @@ public int getNetworkId() throws ConnectorException, IOException { } @Override - public String getSideChainData(String sideChainID) throws ConnectorException, IOException { + public String getSideChainData(int sideChainID) throws ConnectorException, IOException { Object result = rpc.call("getsidechaindata", sideChainID); try { return (String) result; diff --git a/src/main/java/com/github/ontio/network/websocket/WebsocketClient.java b/src/main/java/com/github/ontio/network/websocket/WebsocketClient.java index e8f9ca84..c848650f 100644 --- a/src/main/java/com/github/ontio/network/websocket/WebsocketClient.java +++ b/src/main/java/com/github/ontio/network/websocket/WebsocketClient.java @@ -180,6 +180,31 @@ public Block getBlock(String hash) throws ConnectorException, IOException{ mWebSocket.send(JSON.toJSONString(map)); return null; } + + @Override + public String getBlockBytes(int height) throws ConnectorException, IOException { + Map map = new HashMap<>(); + map.put("Action", "getblockbyheight"); + map.put("Version", "1.0.0"); + map.put("Height",height); + map.put("Raw","1"); + map.put("Id", generateReqId()); + mWebSocket.send(JSON.toJSONString(map)); + return null; + } + + @Override + public String getBlockBytes(String hash) throws ConnectorException, IOException { + Map map = new HashMap<>(); + map.put("Action", "getblockbyhash"); + map.put("Version", "1.0.0"); + map.put("Hash",hash); + map.put("Raw","1"); + map.put("Id", generateReqId()); + mWebSocket.send(JSON.toJSONString(map)); + return null; + } + @Override public Block getBlockJson(int height) throws ConnectorException, IOException{ Map map = new HashMap<>(); @@ -342,7 +367,7 @@ public int getNetworkId() throws ConnectorException, IOException { } @Override - public String getSideChainData(String sideChainID) throws ConnectorException, IOException { + public String getSideChainData(int sideChainID) throws ConnectorException, IOException { return null; } diff --git a/src/main/java/com/github/ontio/sdk/manager/ConnectMgr.java b/src/main/java/com/github/ontio/sdk/manager/ConnectMgr.java index f90338e7..945fc3fa 100644 --- a/src/main/java/com/github/ontio/sdk/manager/ConnectMgr.java +++ b/src/main/java/com/github/ontio/sdk/manager/ConnectMgr.java @@ -213,6 +213,17 @@ public Block getBlock(String hash) throws ConnectorException, IOException { } + public String getBlockBytes(int height) throws ConnectorException, IOException, SDKException { + if (height < 0) { + throw new SDKException(ErrorCode.ParamError); + } + return connector.getBlockBytes(height); + } + + public String getBlockBytes(String blockHash) throws ConnectorException, IOException { + return connector.getBlockBytes(blockHash); + } + public Object getBalance(String address) throws ConnectorException, IOException { return connector.getBalance(address); } @@ -280,7 +291,7 @@ public String getGrantOng(String address) throws ConnectorException, IOException public int getNetworkId() throws ConnectorException, IOException { return connector.getNetworkId(); } - public String getSideChainData(String sideChianId) throws ConnectorException, IOException { + public String getSideChainData(int sideChianId) throws ConnectorException, IOException { return connector.getSideChainData(sideChianId); } diff --git a/src/main/java/com/github/ontio/sidechain/SidechainVm.java b/src/main/java/com/github/ontio/sidechain/SidechainVm.java index 105a426e..3288735e 100644 --- a/src/main/java/com/github/ontio/sidechain/SidechainVm.java +++ b/src/main/java/com/github/ontio/sidechain/SidechainVm.java @@ -1,8 +1,25 @@ package com.github.ontio.sidechain; import com.github.ontio.OntSdk; +import com.github.ontio.account.Account; +import com.github.ontio.common.Address; +import com.github.ontio.common.Common; +import com.github.ontio.common.ErrorCode; +import com.github.ontio.core.asset.Sig; +import com.github.ontio.core.program.Program; +import com.github.ontio.core.scripts.ScriptBuilder; +import com.github.ontio.core.scripts.ScriptOp; +import com.github.ontio.core.transaction.Attribute; +import com.github.ontio.sdk.exception.SDKException; +import com.github.ontio.sidechain.core.transaction.InvokeCode; +import com.github.ontio.sidechain.core.transaction.Transaction; import com.github.ontio.sidechain.smartcontract.governance.Governance; import com.github.ontio.sidechain.smartcontract.ongx.OngX; +import com.github.ontio.smartcontract.Vm; + +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Random; public class SidechainVm { private Governance governance; @@ -25,4 +42,121 @@ public OngX ongX() { } return ongX; } + + public Transaction signTx(Transaction tx, Account[][] accounts) throws Exception{ + if (accounts.length > Common.TX_MAX_SIG_SIZE) { + throw new SDKException(ErrorCode.ParamErr("the number of transaction signatures should not be over 16")); + } + Sig[] sigs = new Sig[accounts.length]; + for (int i = 0; i < accounts.length; i++) { + sigs[i] = new Sig(); + sigs[i].pubKeys = new byte[accounts[i].length][]; + sigs[i].sigData = new byte[accounts[i].length][]; + for (int j = 0; j < accounts[i].length; j++) { + sigs[i].M++; + byte[] signature = tx.sign(accounts[i][j], accounts[i][j].getSignatureScheme()); + sigs[i].pubKeys[j] = accounts[i][j].serializePublicKey(); + sigs[i].sigData[j] = signature; + } + } + tx.sigs = sigs; + return tx; + } + + public Transaction addSign(Transaction tx, String addr, String password, byte[] salt) throws Exception { + return addSign(tx,sdk.getWalletMgr().getAccount(addr,password,salt)); + } + public Transaction addSign(Transaction tx, Account acct) throws Exception { + if(tx.sigs == null){ + tx.sigs = new Sig[0]; + } else { + if (tx.sigs.length >= Common.TX_MAX_SIG_SIZE) { + throw new SDKException(ErrorCode.ParamErr("the number of transaction signatures should not be over 16")); + } + } + Sig[] sigs = new Sig[tx.sigs.length + 1]; + for(int i= 0; i< tx.sigs.length; i++){ + sigs[i] = tx.sigs[i]; + } + sigs[tx.sigs.length] = new Sig(); + sigs[tx.sigs.length].M = 1; + sigs[tx.sigs.length].pubKeys = new byte[1][]; + sigs[tx.sigs.length].sigData = new byte[1][]; + sigs[tx.sigs.length].pubKeys[0] = acct.serializePublicKey(); + sigs[tx.sigs.length].sigData[0] = tx.sign(acct,acct.getSignatureScheme()); + tx.sigs = sigs; + return tx; + } + + public Transaction addMultiSign(Transaction tx,int M,byte[][] pubKeys, Account acct) throws Exception { + addMultiSign(tx,M,pubKeys,tx.sign(acct, acct.getSignatureScheme())); + return tx; + } + public Transaction addMultiSign(Transaction tx,int M,byte[][] pubKeys, byte[] signatureData) throws Exception { + pubKeys = Program.sortPublicKeys(pubKeys); + if (tx.sigs == null) { + tx.sigs = new Sig[0]; + } else { + if (tx.sigs.length > Common.TX_MAX_SIG_SIZE || M > pubKeys.length || M <= 0 || signatureData == null || pubKeys == null) { + throw new SDKException(ErrorCode.ParamError); + } + for (int i = 0; i < tx.sigs.length; i++) { + if(Arrays.deepEquals(tx.sigs[i].pubKeys,pubKeys)){ + if (tx.sigs[i].sigData.length + 1 > pubKeys.length) { + throw new SDKException(ErrorCode.ParamErr("too more sigData")); + } + if(tx.sigs[i].M != M){ + throw new SDKException(ErrorCode.ParamErr("M error")); + } + int len = tx.sigs[i].sigData.length; + byte[][] sigData = new byte[len+1][]; + for (int j = 0; j < tx.sigs[i].sigData.length; j++) { + sigData[j] = tx.sigs[i].sigData[j]; + } + sigData[len] = signatureData; + tx.sigs[i].sigData = sigData; + return tx; + } + } + } + Sig[] sigs = new Sig[tx.sigs.length + 1]; + for (int i = 0; i < tx.sigs.length; i++) { + sigs[i] = tx.sigs[i]; + } + sigs[tx.sigs.length] = new Sig(); + sigs[tx.sigs.length].M = M; + sigs[tx.sigs.length].pubKeys = pubKeys; + sigs[tx.sigs.length].sigData = new byte[1][]; + sigs[tx.sigs.length].sigData[0] = signatureData; + + tx.sigs = sigs; + return tx; + } + public InvokeCode makeInvokeCodeTransaction(byte[] params, String payer, long gaslimit, long gasprice) throws SDKException { + + InvokeCode tx = new InvokeCode(); + tx.sideChainId = sdk.sideChainId; + tx.attributes = new Attribute[0]; + tx.nonce = new Random().nextInt(); + tx.code = params; + tx.gasLimit = gaslimit; + tx.gasPrice = gasprice; + if(payer != null){ + tx.payer = Address.decodeBase58(payer.replace(Common.didont,"")); + } + return tx; + } + public Transaction buildNativeParams(Address codeAddr, String initMethod, byte[] args, String payer, long gaslimit, long gasprice) throws SDKException { + ScriptBuilder sb = new ScriptBuilder(); + if(args.length >0) { + sb.add(args); + } + sb.emitPushByteArray(initMethod.getBytes()); + sb.emitPushByteArray(codeAddr.toArray()); + sb.emitPushInteger(BigInteger.valueOf(0)); + sb.emit(ScriptOp.OP_SYSCALL); + sb.emitPushByteArray(Vm.NATIVE_INVOKE_NAME.getBytes()); + Transaction tx = makeInvokeCodeTransaction(sb.toArray(),payer,gaslimit,gasprice); + return tx; + } } diff --git a/src/main/java/com/github/ontio/sidechain/core/block/Block.java b/src/main/java/com/github/ontio/sidechain/core/block/Block.java new file mode 100644 index 00000000..1627cc94 --- /dev/null +++ b/src/main/java/com/github/ontio/sidechain/core/block/Block.java @@ -0,0 +1,238 @@ +package com.github.ontio.sidechain.core.block; + +import com.alibaba.fastjson.JSON; +import com.github.ontio.common.Address; +import com.github.ontio.common.Helper; +import com.github.ontio.common.UInt256; +import com.github.ontio.core.Inventory; +import com.github.ontio.core.InventoryType; +import com.github.ontio.core.payload.Bookkeeping; +import com.github.ontio.core.payload.DeployCode; +import com.github.ontio.core.payload.InvokeCode; +import com.github.ontio.core.transaction.Transaction; +import com.github.ontio.io.BinaryReader; +import com.github.ontio.io.BinaryWriter; +import com.github.ontio.io.Serializable; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +public class Block extends Inventory { + + public int version; + public long sideChainID; + public UInt256 prevBlockHash; + public UInt256 transactionsRoot; + public UInt256 blockRoot; + public int timestamp; + public int height; + public long consensusData; + public byte[] consensusPayload; + public Address nextBookkeeper; + public String[] sigData; + public byte[][] bookkeepers; + public Transaction[] transactions; + public UInt256 hash; + private Block _header = null; + + public Block header() { + if (isHeader()) { + return this; + } + if (_header == null) { + _header = new Block(); + _header.sideChainID = sideChainID; + _header.prevBlockHash = prevBlockHash; + _header.transactionsRoot = this.transactionsRoot; + _header.blockRoot = this.blockRoot; + _header.timestamp = this.timestamp; + _header.height = this.height; + _header.consensusData = this.consensusData; + _header.nextBookkeeper = this.nextBookkeeper; + _header.sigData = this.sigData; + _header.bookkeepers = this.bookkeepers; + _header.transactions = new Transaction[0]; + } + return _header; + } + + @Override + public InventoryType inventoryType() { + return InventoryType.Block; + } + + public boolean isHeader() { + return transactions.length == 0; + } + + @Override + public void deserialize(BinaryReader reader) throws IOException { + deserializeUnsigned(reader); + int len = (int) reader.readVarInt(); + sigData = new String[len]; + for (int i = 0; i < len; i++) { + this.sigData[i] = Helper.toHexString(reader.readVarBytes()); + } + + len = reader.readInt(); + transactions = new Transaction[len]; + for (int i = 0; i < transactions.length; i++) { + transactions[i] = Transaction.deserializeFrom(reader); + } + } + + @Override + public void deserializeUnsigned(BinaryReader reader) throws IOException { + try { + version = reader.readInt(); + sideChainID = reader.readLong(); + prevBlockHash = reader.readSerializable(UInt256.class); + transactionsRoot = reader.readSerializable(UInt256.class); + blockRoot = reader.readSerializable(UInt256.class); + timestamp = reader.readInt(); + height = reader.readInt(); + consensusData = Long.valueOf(reader.readLong()); + consensusPayload = reader.readVarBytes(); + nextBookkeeper = reader.readSerializable(Address.class); + int len = (int) reader.readVarInt(); + bookkeepers = new byte[len][]; + for (int i = 0; i < len; i++) { + this.bookkeepers[i] = reader.readVarBytes(); + } + transactions = new Transaction[0]; + } catch (InstantiationException | IllegalAccessException ex) { + throw new IOException(ex); + } + } + + @Override + public void serialize(BinaryWriter writer) throws IOException { + serializeUnsigned(writer); + writer.writeVarInt(bookkeepers.length); + for(int i=0;iHelper.toHexString(p)).toArray(Object[]::new)); + + json.put("Header", head); + json.put("Transactions", Arrays.stream(transactions).map(p -> { + if (p instanceof InvokeCode) { + return ((InvokeCode) p).json(); + } else if (p instanceof DeployCode) { + return ((DeployCode) p).json(); + } else if (p instanceof Bookkeeping) { + return ((Bookkeeping) p).json(); + } else { + return p.json(); + } + }).toArray(Object[]::new)); + return JSON.toJSONString(json); + } + + public byte[] trim() { + try (ByteArrayOutputStream ms = new ByteArrayOutputStream()) { + try (BinaryWriter writer = new BinaryWriter(ms)) { + serializeUnsigned(writer); + writer.writeByte((byte) 1); + writer.writeSerializableArray(Arrays.stream(transactions).map(p -> p.hash()).toArray(Serializable[]::new)); + writer.flush(); + return ms.toByteArray(); + } + } catch (IOException ex) { + throw new UnsupportedOperationException(ex); + } + } + + public static com.github.ontio.core.block.Block fromTrimmedData(byte[] data, int index, Function txSelector) throws IOException { + com.github.ontio.core.block.Block block = new com.github.ontio.core.block.Block(); + try (ByteArrayInputStream ms = new ByteArrayInputStream(data, index, data.length - index)) { + try (BinaryReader reader = new BinaryReader(ms)) { + block.deserializeUnsigned(reader); + reader.readByte(); + if (txSelector == null) { + block.transactions = new Transaction[0]; + } else { + block.transactions = new Transaction[(int) reader.readVarInt(0x10000000)]; + for (int i = 0; i < block.transactions.length; i++) { + block.transactions[i] = txSelector.apply(reader.readSerializable(UInt256.class)); + } + } + } catch (InstantiationException | IllegalAccessException ex) { + throw new IOException(ex); + } + } + return block; + } + @Override + public boolean verify() { + return true; + } +} diff --git a/src/main/java/com/github/ontio/sidechain/core/transaction/InvokeCode.java b/src/main/java/com/github/ontio/sidechain/core/transaction/InvokeCode.java new file mode 100644 index 00000000..3b151fc6 --- /dev/null +++ b/src/main/java/com/github/ontio/sidechain/core/transaction/InvokeCode.java @@ -0,0 +1,47 @@ +package com.github.ontio.sidechain.core.transaction; + +import com.github.ontio.common.Address; +import com.github.ontio.common.Helper; +import com.github.ontio.core.transaction.TransactionType; +import com.github.ontio.io.BinaryReader; +import com.github.ontio.io.BinaryWriter; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class InvokeCode extends Transaction { + public byte[] code; + + public InvokeCode() { + super(TransactionType.InvokeCode); + } + + @Override + protected void deserializeExclusiveData(BinaryReader reader) throws IOException { + try { + code = reader.readVarBytes(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + protected void serializeExclusiveData(BinaryWriter writer) throws IOException { + writer.writeVarBytes(code); + } + + @Override + public Address[] getAddressU160ForVerifying() { + return null; + } + + @Override + public Object json() { + Map obj = (Map) super.json(); + Map payload = new HashMap(); + payload.put("Code", Helper.toHexString(code)); + obj.put("Payload", payload); + return obj; + } +} diff --git a/src/main/java/com/github/ontio/sidechain/core/transaction/Transaction.java b/src/main/java/com/github/ontio/sidechain/core/transaction/Transaction.java new file mode 100644 index 00000000..fc991ab2 --- /dev/null +++ b/src/main/java/com/github/ontio/sidechain/core/transaction/Transaction.java @@ -0,0 +1,177 @@ +package com.github.ontio.sidechain.core.transaction; + +import com.github.ontio.common.Address; +import com.github.ontio.core.Inventory; +import com.github.ontio.core.InventoryType; +import com.github.ontio.core.asset.Sig; +import com.github.ontio.core.transaction.Attribute; +import com.github.ontio.core.transaction.TransactionType; +import com.github.ontio.io.BinaryReader; +import com.github.ontio.io.BinaryWriter; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; + +public class Transaction extends Inventory { + public byte version = 1; + public long sideChainId; + public TransactionType txType; + public int nonce = new Random().nextInt(); + public long gasPrice = 0; + public long gasLimit = 0; + public Address payer = new Address(); + public Attribute[] attributes; + public Sig[] sigs = new Sig[0]; + protected Transaction(TransactionType type) { + this.txType = type; + } + + public static Transaction deserializeFrom(byte[] value) throws IOException { + return deserializeFrom(value, 0); + } + + public static Transaction deserializeFrom(byte[] value, int offset) throws IOException { + try (ByteArrayInputStream ms = new ByteArrayInputStream(value, offset, value.length - offset)) { + try (BinaryReader reader = new BinaryReader(ms)) { + return deserializeFrom(reader); + } + } + } + + public static Transaction deserializeFrom(BinaryReader reader) throws IOException { + try { + byte ver = reader.readByte(); + TransactionType type = TransactionType.valueOf(reader.readByte()); + String typeName = "com.github.ontio.core.payload." + type.toString(); + Transaction transaction = (Transaction) Class.forName(typeName).newInstance(); + transaction.nonce = reader.readInt(); + transaction.version = ver; + transaction.sideChainId = reader.readLong(); + transaction.gasPrice = reader.readLong(); + transaction.gasLimit = reader.readLong(); + transaction.payer = reader.readSerializable(Address.class); + transaction.deserializeUnsignedWithoutType(reader); + transaction.sigs = new Sig[(int) reader.readVarInt()]; + for (int i = 0; i < transaction.sigs.length; i++) { + transaction.sigs[i] = reader.readSerializable(Sig.class); + } + return transaction; + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) { + throw new IOException(ex); + } + } + @Override + public void deserialize(BinaryReader reader) throws IOException { + deserializeUnsigned(reader); + try { + sigs = reader.readSerializableArray(Sig.class); + } catch (InstantiationException | IllegalAccessException ex) { + throw new RuntimeException(ex); + } + } + + @Override + public void deserializeUnsigned(BinaryReader reader) throws IOException { + txType = TransactionType.valueOf(reader.readByte()); + nonce = reader.readInt(); + version = reader.readByte(); + gasPrice = reader.readLong(); + gasLimit = reader.readLong(); + try { + payer = reader.readSerializable(Address.class); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + deserializeUnsignedWithoutType(reader); + } + + private void deserializeUnsignedWithoutType(BinaryReader reader) throws IOException { + try { + deserializeExclusiveData(reader); + attributes = reader.readSerializableArray(Attribute.class); + } catch (InstantiationException | IllegalAccessException ex) { + throw new IOException(ex); + } + } + + protected void deserializeExclusiveData(BinaryReader reader) throws IOException { + } + + @Override + public void serialize(BinaryWriter writer) throws IOException { + serializeUnsigned(writer); + writer.writeSerializableArray(sigs); + } + + @Override + public void serializeUnsigned(BinaryWriter writer) throws IOException { + writer.writeByte(version); + writer.writeInt((int) sideChainId); + writer.writeByte(txType.value()); + writer.writeInt(nonce); + writer.writeLong(gasPrice); + writer.writeLong(gasLimit); + writer.writeSerializable(payer); + serializeExclusiveData(writer); + writer.writeSerializableArray(attributes); + } + + protected void serializeExclusiveData(BinaryWriter writer) throws IOException { + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof com.github.ontio.core.transaction.Transaction)) { + return false; + } + com.github.ontio.core.transaction.Transaction tx = (com.github.ontio.core.transaction.Transaction) obj; + return hash().equals(tx.hash()); + } + + @Override + public int hashCode() { + return hash().hashCode(); + } + + + @Override + public Address[] getAddressU160ForVerifying() { + return null; + } + + @Override + public final InventoryType inventoryType() { + return InventoryType.TX; + } + + public Object json() { + Map json = new HashMap(); + json.put("Hash", hash().toString()); + json.put("Version", (int) version); + json.put("Nonce", nonce& 0xFFFFFFFF); + json.put("TxType", txType.value() & 0xFF); + json.put("GasPrice",gasPrice); + json.put("GasLimit",gasLimit); + json.put("Payer",payer.toBase58()); + json.put("Attributes", Arrays.stream(attributes).map(p -> p.json()).toArray(Object[]::new)); + json.put("Sigs", Arrays.stream(sigs).map(p -> p.json()).toArray(Object[]::new)); + return json; + } + + @Override + public boolean verify() { + return true; + } +} diff --git a/src/main/java/com/github/ontio/sidechain/smartcontract/governance/Governance.java b/src/main/java/com/github/ontio/sidechain/smartcontract/governance/Governance.java index 13864406..7a8e4567 100644 --- a/src/main/java/com/github/ontio/sidechain/smartcontract/governance/Governance.java +++ b/src/main/java/com/github/ontio/sidechain/smartcontract/governance/Governance.java @@ -5,11 +5,11 @@ import com.github.ontio.common.*; import com.github.ontio.core.governance.*; import com.github.ontio.core.sidechaingovernance.NodeToSideChainParams; -import com.github.ontio.core.transaction.Transaction; import com.github.ontio.io.BinaryReader; import com.github.ontio.io.utils; import com.github.ontio.network.exception.ConnectorException; import com.github.ontio.sdk.exception.SDKException; +import com.github.ontio.sidechain.core.transaction.Transaction; import com.github.ontio.smartcontract.nativevm.abi.NativeBuildParams; import com.github.ontio.smartcontract.nativevm.abi.Struct; @@ -32,16 +32,6 @@ public Governance(OntSdk sdk){ this.sdk = sdk; } - public String getSideChainId() throws ConnectorException, IOException, SDKException { - - String res = sdk.getSideChainConnectMgr().getStorage(Helper.reverse(contractAddress), Helper.toHexString(SIDE_CHAIN_ID.getBytes())); - if(res == null || res.equals("")){ - return null; - } - ByteArrayInputStream bais = new ByteArrayInputStream(Helper.hexToBytes(res)); - BinaryReader reader = new BinaryReader(bais); - return reader.readVarString(); - } public GovernanceView getGovernanceView() throws SDKException, ConnectorException, IOException, SDKException { if(sdk.getSideChainConnectMgr() == null){ @@ -131,11 +121,11 @@ public String commitDpos(Account account, String sideChainData, Account payer, l struct.add(Helper.hexToBytes(sideChainData)); list.add(struct); byte[] args = NativeBuildParams.createCodeParamsScript(list); - Transaction tx = sdk.vm().buildNativeParams(new Address(Helper.hexToBytes(contractAddress)),"commitDpos", + Transaction tx = sdk.sidechainVm().buildNativeParams(new Address(Helper.hexToBytes(contractAddress)),"commitDpos", args,payer.getAddressU160().toBase58(),gaslimit, gasprice); - sdk.signTx(tx,new Account[][]{{account}}); + sdk.sidechainVm().signTx(tx,new Account[][]{{account}}); if(!account.equals(payer)){ - sdk.addSign(tx,payer); + sdk.sidechainVm().addSign(tx,payer); } sdk.getSideChainConnectMgr().sendRawTransaction(tx.toHexString()); return tx.hash().toString(); @@ -167,10 +157,10 @@ public String inputPeerPoolMap(Account account, InputPeerPoolMapParam peerPoolMa } list.add(struct); byte[] args = NativeBuildParams.createCodeParamsScript(list); - Transaction tx = sdk.vm().buildNativeParams(new Address(Helper.hexToBytes(contractAddress)),"inputPeerPoolMap",args,payer.getAddressU160().toBase58(),gaslimit, gasprice); - sdk.signTx(tx,new Account[][]{{account}}); + Transaction tx = sdk.sidechainVm().buildNativeParams(new Address(Helper.hexToBytes(contractAddress)),"inputPeerPoolMap",args,payer.getAddressU160().toBase58(),gaslimit, gasprice); + sdk.sidechainVm().signTx(tx,new Account[][]{{account}}); if(!account.equals(payer)){ - sdk.addSign(tx,payer); + sdk.sidechainVm().addSign(tx,payer); } sdk.getSideChainConnectMgr().sendRawTransaction(tx.toHexString()); return tx.hash().toString(); @@ -187,10 +177,10 @@ public String inputConfig(Account account, Configuration configuration, Account struct.add(configuration.N,configuration.C,configuration.K,configuration.L,configuration.BlockMsgDelay,configuration.HashMsgDelay,configuration.PeerHandshakeTimeout,configuration.MaxBlockChangeView); list.add(struct); byte[] args = NativeBuildParams.createCodeParamsScript(list); - Transaction tx = sdk.vm().buildNativeParams(new Address(Helper.hexToBytes(contractAddress)),"inputConfig",args,payer.getAddressU160().toBase58(),gaslimit, gasprice); - sdk.signTx(tx,new Account[][]{{account}}); + Transaction tx = sdk.sidechainVm().buildNativeParams(new Address(Helper.hexToBytes(contractAddress)),"inputConfig",args,payer.getAddressU160().toBase58(),gaslimit, gasprice); + sdk.sidechainVm().signTx(tx,new Account[][]{{account}}); if(!account.equals(payer)){ - sdk.addSign(tx,payer); + sdk.sidechainVm().addSign(tx,payer); } sdk.getSideChainConnectMgr().sendRawTransaction(tx.toHexString()); return tx.hash().toString(); @@ -205,10 +195,10 @@ public String inputGlobalParam(Account account, GlobalParam param, Account payer struct.add(param.candidateFeeSplitNum, param.A, param.B,param.yita); list.add(struct); byte[] args = NativeBuildParams.createCodeParamsScript(list); - Transaction tx = sdk.vm().buildNativeParams(new Address(Helper.hexToBytes(contractAddress)),"inputGlobalParam",args,payer.getAddressU160().toBase58(),gaslimit, gasprice); - sdk.signTx(tx,new Account[][]{{account}}); + Transaction tx = sdk.sidechainVm().buildNativeParams(new Address(Helper.hexToBytes(contractAddress)),"inputGlobalParam",args,payer.getAddressU160().toBase58(),gaslimit, gasprice); + sdk.sidechainVm().signTx(tx,new Account[][]{{account}}); if(!account.equals(payer)){ - sdk.addSign(tx,payer); + sdk.sidechainVm().addSign(tx,payer); } sdk.getSideChainConnectMgr().sendRawTransaction(tx.toHexString()); return tx.hash().toString(); @@ -226,10 +216,10 @@ public String inputSplitCurve(Account account, SplitCurve splitCurve, Account pa } list.add(struct); byte[] args = NativeBuildParams.createCodeParamsScript(list); - Transaction tx = sdk.vm().buildNativeParams(new Address(Helper.hexToBytes(contractAddress)),"inputSplitCurve",args,payer.getAddressU160().toBase58(),gaslimit, gasprice); - sdk.signTx(tx,new Account[][]{{account}}); + Transaction tx = sdk.sidechainVm().buildNativeParams(new Address(Helper.hexToBytes(contractAddress)),"inputSplitCurve",args,payer.getAddressU160().toBase58(),gaslimit, gasprice); + sdk.sidechainVm().signTx(tx,new Account[][]{{account}}); if(!account.equals(payer)){ - sdk.addSign(tx,payer); + sdk.sidechainVm().addSign(tx,payer); } sdk.getSideChainConnectMgr().sendRawTransaction(tx.toHexString()); return tx.hash().toString(); @@ -243,10 +233,10 @@ public String inputGovernanceView(Account account, GovernanceView view, Account struct.add(view.view, view.height, view.txhash); list.add(struct); byte[] args = NativeBuildParams.createCodeParamsScript(list); - Transaction tx = sdk.vm().buildNativeParams(new Address(Helper.hexToBytes(contractAddress)),"inputGovernanceView",args,payer.getAddressU160().toBase58(),gaslimit, gasprice); - sdk.signTx(tx,new Account[][]{{account}}); + Transaction tx = sdk.sidechainVm().buildNativeParams(new Address(Helper.hexToBytes(contractAddress)),"inputGovernanceView",args,payer.getAddressU160().toBase58(),gaslimit, gasprice); + sdk.sidechainVm().signTx(tx,new Account[][]{{account}}); if(!account.equals(payer)){ - sdk.addSign(tx,payer); + sdk.sidechainVm().addSign(tx,payer); } sdk.getSideChainConnectMgr().sendRawTransaction(tx.toHexString()); return tx.hash().toString(); diff --git a/src/main/java/com/github/ontio/sidechain/smartcontract/ongx/OngX.java b/src/main/java/com/github/ontio/sidechain/smartcontract/ongx/OngX.java index e76c55ed..c9aef9fe 100644 --- a/src/main/java/com/github/ontio/sidechain/smartcontract/ongx/OngX.java +++ b/src/main/java/com/github/ontio/sidechain/smartcontract/ongx/OngX.java @@ -7,11 +7,11 @@ import com.github.ontio.common.ErrorCode; import com.github.ontio.common.Helper; import com.github.ontio.core.asset.State; -import com.github.ontio.core.transaction.Transaction; import com.github.ontio.io.BinaryReader; import com.github.ontio.io.utils; import com.github.ontio.network.exception.ConnectorException; import com.github.ontio.sdk.exception.SDKException; +import com.github.ontio.sidechain.core.transaction.Transaction; import com.github.ontio.smartcontract.nativevm.abi.NativeBuildParams; import com.github.ontio.smartcontract.nativevm.abi.Struct; @@ -34,54 +34,69 @@ public String getContractAddress() { public String sendTransfer(Account sendAcct, String recvAddr, long amount, Account payerAcct, long gaslimit, long gasprice) throws Exception { - Transaction tx = sdk.nativevm().ong().makeTransfer(sendAcct.getAddressU160().toBase58(),recvAddr,amount, + Transaction tx = makeTransfer(sendAcct.getAddressU160().toBase58(),recvAddr,amount, payerAcct.getAddressU160().toBase58(), gaslimit,gasprice); - sdk.signTx(tx, new Account[][]{{sendAcct}}); + sdk.sidechainVm().signTx(tx, new Account[][]{{sendAcct}}); if(!sendAcct.equals(payerAcct)){ - sdk.addSign(tx, payerAcct); + sdk.sidechainVm().addSign(tx, payerAcct); } sdk.getSideChainConnectMgr().sendRawTransaction(tx.toHexString()); return tx.toHexString(); } - public Transaction makeTransfer(String sendAddr, String recvAddr, long amount, String payer, long gaslimit, long gasprice) throws Exception { - return sdk.nativevm().ong().makeTransfer(sendAddr,recvAddr,amount,payer,gaslimit,gasprice); + if(sendAddr==null || sendAddr.equals("")|| recvAddr==null||recvAddr.equals("") || + payer==null||payer.equals("")){ + throw new SDKException(ErrorCode.ParamErr("parameters should not be null")); + } + if (amount <= 0 || gasprice < 0 || gaslimit < 0) { + throw new SDKException(ErrorCode.ParamErr("amount or gasprice or gaslimit should not be less than 0")); + } + + + List list = new ArrayList(); + List listStruct = new ArrayList(); + listStruct.add(new Struct().add(Address.decodeBase58(sendAddr),Address.decodeBase58(recvAddr),amount)); + list.add(listStruct); + byte[] args = NativeBuildParams.createCodeParamsScript(list); + Transaction tx = sdk.sidechainVm().buildNativeParams(new Address(Helper.hexToBytes(ongXContract)),"transfer",args,payer,gaslimit, gasprice); + return tx; } + public Transaction makeTransfer(State[] states, String payer, long gaslimit, long gasprice) throws Exception { - return sdk.nativevm().ong().makeTransfer(states,payer,gaslimit,gasprice); + return makeTransfer(states,payer,gaslimit,gasprice); } public String sendApprove(Account sendAcct, String recvAddr, long amount, Account payerAcct, long gaslimit, long gasprice) throws Exception { - Transaction tx = sdk.nativevm().ong().makeApprove(sendAcct.getAddressU160().toBase58(),recvAddr,amount, + Transaction tx = makeApprove(sendAcct.getAddressU160().toBase58(),recvAddr,amount, payerAcct.getAddressU160().toBase58(),gaslimit,gasprice); - sdk.signTx(tx, new Account[][]{{sendAcct}}); + sdk.sidechainVm().signTx(tx, new Account[][]{{sendAcct}}); if(!sendAcct.equals(payerAcct)){ - sdk.addSign(tx, payerAcct); + sdk.sidechainVm().addSign(tx, payerAcct); } sdk.getSideChainConnectMgr().sendRawTransaction(tx.toHexString()); return tx.hash().toHexString(); } public Transaction makeApprove(String sendAddr,String recvAddr,long amount,String payer,long gaslimit,long gasprice) throws Exception { - return sdk.nativevm().ong().makeApprove(sendAddr,recvAddr,amount,payer,gaslimit,gasprice); + return makeApprove(sendAddr,recvAddr,amount,payer,gaslimit,gasprice); } public String sendTransferFrom(Account sendAcct, String fromAddr, String toAddr, long amount, Account payerAcct, long gaslimit, long gasprice) throws Exception { - Transaction tx = sdk.nativevm().ong().makeTransferFrom(sendAcct.getAddressU160().toBase58(),fromAddr,toAddr,amount, + Transaction tx = makeTransferFrom(sendAcct.getAddressU160().toBase58(),fromAddr,toAddr,amount, payerAcct.getAddressU160().toBase58(),gaslimit,gasprice); - sdk.signTx(tx, new Account[][]{{sendAcct}}); + sdk.sidechainVm().signTx(tx, new Account[][]{{sendAcct}}); if(!sendAcct.equals(payerAcct)){ - sdk.addSign(tx, payerAcct); + sdk.sidechainVm().addSign(tx, payerAcct); } sdk.getSideChainConnectMgr().sendRawTransaction(tx.toHexString()); return tx.hash().toHexString(); } public Transaction makeTransferFrom(String sendAddr, String fromAddr, String toAddr,long amount,String payer,long gaslimit,long gasprice) throws Exception { - return sdk.nativevm().ong().makeTransferFrom(sendAddr,fromAddr,toAddr,amount,payer,gaslimit,gasprice); + return makeTransferFrom(sendAddr,fromAddr,toAddr,amount,payer,gaslimit,gasprice); } public String queryName() throws Exception { - Transaction tx = this.sdk.vm().buildNativeParams(new Address(Helper.hexToBytes(ongXContract)), "name", new byte[]{0}, (String)null, 0L, 0L); + Transaction tx = this.sdk.sidechainVm().buildNativeParams(new Address(Helper.hexToBytes(ongXContract)), "name", new byte[]{0}, (String)null, 0L, 0L); Object obj = sdk.getSideChainConnectMgr().sendRawTransactionPreExec(tx.toHexString()); String res = ((JSONObject)obj).getString("Result"); return new String(Helper.hexToBytes(res)); @@ -92,7 +107,7 @@ public String queryName() throws Exception { * @throws Exception */ public String querySymbol() throws Exception { - Transaction tx = this.sdk.vm().buildNativeParams(new Address(Helper.hexToBytes(ongXContract)), "symbol", new byte[]{0}, (String)null, 0L, 0L); + Transaction tx = this.sdk.sidechainVm().buildNativeParams(new Address(Helper.hexToBytes(ongXContract)), "symbol", new byte[]{0}, (String)null, 0L, 0L); Object obj = sdk.getSideChainConnectMgr().sendRawTransactionPreExec(tx.toHexString()); String res = ((JSONObject)obj).getString("Result"); return new String(Helper.hexToBytes(res)); @@ -103,14 +118,14 @@ public String querySymbol() throws Exception { * @throws Exception */ public long queryDecimals() throws Exception { - Transaction tx = this.sdk.vm().buildNativeParams(new Address(Helper.hexToBytes(ongXContract)), "decimals", new byte[]{0}, (String)null, 0L, 0L); + Transaction tx = this.sdk.sidechainVm().buildNativeParams(new Address(Helper.hexToBytes(ongXContract)), "decimals", new byte[]{0}, (String)null, 0L, 0L); Object obj = sdk.getSideChainConnectMgr().sendRawTransactionPreExec(tx.toHexString()); String res = ((JSONObject)obj).getString("Result"); return "".equals(res) ? 0L : Long.valueOf(Helper.reverse(res), 16); } public long queryTotalSupply() throws Exception { - Transaction tx = this.sdk.vm().buildNativeParams(new Address(Helper.hexToBytes(ongXContract)), "totalSupply", new byte[]{0}, (String)null, 0L, 0L); + Transaction tx = this.sdk.sidechainVm().buildNativeParams(new Address(Helper.hexToBytes(ongXContract)), "totalSupply", new byte[]{0}, (String)null, 0L, 0L); Object obj = sdk.getSideChainConnectMgr().sendRawTransactionPreExec(tx.toHexString()); String res = ((JSONObject)obj).getString("Result"); return res != null && !res.equals("") ? Long.valueOf(Helper.reverse(res), 16) : 0L; @@ -130,7 +145,7 @@ public long queryBalanceOf(String address) throws Exception { List list = new ArrayList(); list.add(Address.decodeBase58(address)); byte[] arg = NativeBuildParams.createCodeParamsScript(list); - Transaction tx = this.sdk.vm().buildNativeParams(new Address(Helper.hexToBytes(ongXContract)), "balanceOf", arg, (String)null, 0L, 0L); + Transaction tx = this.sdk.sidechainVm().buildNativeParams(new Address(Helper.hexToBytes(ongXContract)), "balanceOf", arg, (String)null, 0L, 0L); Object obj = sdk.getSideChainConnectMgr().sendRawTransactionPreExec(tx.toHexString()); String res = ((JSONObject)obj).getString("Result"); return res != null && !res.equals("") ? Long.valueOf(Helper.reverse(res), 16) : 0L; @@ -144,7 +159,7 @@ public long queryAllowance(String fromAddr, String toAddr) throws Exception { List list = new ArrayList(); list.add((new Struct()).add(new Object[]{Address.decodeBase58(fromAddr), Address.decodeBase58(toAddr)})); byte[] arg = NativeBuildParams.createCodeParamsScript(list); - Transaction tx = this.sdk.vm().buildNativeParams(new Address(Helper.hexToBytes(ongXContract)), "allowance", arg, (String)null, 0L, 0L); + Transaction tx = this.sdk.sidechainVm().buildNativeParams(new Address(Helper.hexToBytes(ongXContract)), "allowance", arg, (String)null, 0L, 0L); Object obj = sdk.getSideChainConnectMgr().sendRawTransactionPreExec(tx.toHexString()); String res = ((JSONObject)obj).getString("Result"); return res != null && !res.equals("") ? Long.valueOf(Helper.reverse(res), 16) : 0L; @@ -163,10 +178,10 @@ public String ongxSetSyncAddr(Account[] accounts,byte[][] allPubkeys,int M,Strin struct.add(Address.decodeBase58(address)); list.add(struct); byte[] args = NativeBuildParams.createCodeParamsScript(list); - Transaction tx = sdk.vm().buildNativeParams(new Address(Helper.hexToBytes(ongXContract)),"setSyncAddr",args,payer.getAddressU160().toBase58(),gaslimit, gasprice); - sdk.signTx(tx, new Account[][]{{payer}}); + Transaction tx = sdk.sidechainVm().buildNativeParams(new Address(Helper.hexToBytes(ongXContract)),"setSyncAddr",args,payer.getAddressU160().toBase58(),gaslimit, gasprice); + sdk.sidechainVm().signTx(tx, new Account[][]{{payer}}); for(int i=0;i