-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
import graalvm and support javascript contract language
- Loading branch information
Showing
29 changed files
with
757 additions
and
692 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 61 additions & 98 deletions
159
contract/contract-jvm/src/main/java/com/jd/blockchain/contract/jvm/AbstractContractCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,74 @@ | ||
package com.jd.blockchain.contract.jvm; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import com.jd.blockchain.contract.ContractEventContext; | ||
import com.jd.blockchain.contract.engine.ContractCode; | ||
import com.jd.blockchain.ledger.BytesValue; | ||
import com.jd.blockchain.ledger.ContractExecuteException; | ||
import com.jd.blockchain.ledger.LedgerException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
import com.jd.blockchain.contract.ContractEventContext; | ||
import com.jd.blockchain.contract.ContractException; | ||
import com.jd.blockchain.contract.EventProcessingAware; | ||
import com.jd.blockchain.contract.engine.ContractCode; | ||
import com.jd.blockchain.ledger.BytesValue; | ||
import com.jd.blockchain.ledger.BytesValueEncoding; | ||
import com.jd.blockchain.ledger.BytesValueList; | ||
|
||
import utils.Bytes; | ||
|
||
/** | ||
* @author huanghaiquan | ||
* | ||
*/ | ||
public abstract class AbstractContractCode implements ContractCode { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractContractCode.class); | ||
private Bytes address; | ||
private long version; | ||
|
||
private ContractDefinition contractDefinition; | ||
|
||
public AbstractContractCode(Bytes address, long version, ContractDefinition contractDefinition) { | ||
this.address = address; | ||
this.version = version; | ||
this.contractDefinition = contractDefinition; | ||
} | ||
|
||
public ContractDefinition getContractDefinition() { | ||
return contractDefinition; | ||
} | ||
|
||
@Override | ||
public Bytes getAddress() { | ||
return address; | ||
} | ||
|
||
@Override | ||
public long getVersion() { | ||
return version; | ||
} | ||
|
||
@Override | ||
public BytesValue processEvent(ContractEventContext eventContext) { | ||
EventProcessingAware evtProcAwire = null; | ||
Object retn = null; | ||
Method handleMethod = null; | ||
LedgerException error = null; | ||
try { | ||
// 执行预处理; | ||
Object contractInstance = getContractInstance(); | ||
if (contractInstance instanceof EventProcessingAware) { | ||
evtProcAwire = (EventProcessingAware) contractInstance; | ||
} | ||
|
||
if (evtProcAwire != null) { | ||
evtProcAwire.beforeEvent(eventContext); | ||
} | ||
|
||
// 反序列化参数; | ||
handleMethod = contractDefinition.getType().getHandleMethod(eventContext.getEvent()); | ||
|
||
if (handleMethod == null) { | ||
throw new ContractException( | ||
String.format("Contract[%s:%s] has no handle method to handle event[%s]!", address.toString(), | ||
contractDefinition.getType().getName(), eventContext.getEvent())); | ||
} | ||
|
||
BytesValueList bytesValues = eventContext.getArgs(); | ||
Object[] args = BytesValueEncoding.decode(bytesValues, handleMethod.getParameterTypes()); | ||
|
||
retn = ReflectionUtils.invokeMethod(handleMethod, contractInstance, args); | ||
|
||
} catch (LedgerException e) { | ||
error = e; | ||
} catch (Throwable e) { | ||
String errorMessage = String.format("Error occurred while processing event[%s] of contract[%s]! --%s", | ||
eventContext.getEvent(), address.toString(), e.getMessage()); | ||
error = new ContractExecuteException(errorMessage, e); | ||
} | ||
|
||
if (evtProcAwire != null) { | ||
try { | ||
evtProcAwire.postEvent(eventContext, error); | ||
} catch (Throwable e) { | ||
String errorMessage = "Error occurred while posting contract event! --" + e.getMessage(); | ||
LOGGER.error(errorMessage, e); | ||
throw new ContractExecuteException(errorMessage); | ||
} | ||
} | ||
if (error != null) { | ||
// Rethrow error; | ||
throw error; | ||
} | ||
|
||
BytesValue retnBytes = BytesValueEncoding.encodeSingle(retn, handleMethod.getReturnType()); | ||
return retnBytes; | ||
} | ||
|
||
protected abstract Object getContractInstance(); | ||
|
||
protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractContractCode.class); | ||
private Bytes address; | ||
private long version; | ||
|
||
public AbstractContractCode(Bytes address, long version) { | ||
this.address = address; | ||
this.version = version; | ||
} | ||
|
||
@Override | ||
public Bytes getAddress() { | ||
return address; | ||
} | ||
|
||
@Override | ||
public long getVersion() { | ||
return version; | ||
} | ||
|
||
@Override | ||
public BytesValue processEvent(ContractEventContext eventContext) { | ||
Object retn = null; | ||
LedgerException error = null; | ||
Object contractInstance = null; | ||
try { | ||
contractInstance = getContractInstance(); | ||
// 执行预处理; | ||
beforeEvent(contractInstance, eventContext); | ||
|
||
// 合约方法执行 | ||
retn = doProcessEvent(contractInstance, eventContext); | ||
|
||
} catch (LedgerException e) { | ||
error = e; | ||
} catch (Throwable e) { | ||
String errorMessage = String.format("Error occurred while processing event[%s] of contract[%s]! --%s", | ||
eventContext.getEvent(), address.toString(), e.getMessage()); | ||
error = new ContractExecuteException(errorMessage, e); | ||
} | ||
|
||
try { | ||
postEvent(contractInstance, eventContext, error); | ||
} catch (Throwable e) { | ||
throw new ContractExecuteException("Error occurred while posting contract event!", e); | ||
} | ||
if (error != null) { | ||
throw error; | ||
} | ||
return (BytesValue) retn; | ||
} | ||
|
||
protected abstract Object getContractInstance(); | ||
|
||
protected abstract void beforeEvent(Object contractInstance, ContractEventContext eventContext); | ||
|
||
protected abstract BytesValue doProcessEvent(Object contractInstance, ContractEventContext eventContext); | ||
|
||
protected abstract void postEvent(Object contractInstance, ContractEventContext eventContext, LedgerException error); | ||
} |
13 changes: 13 additions & 0 deletions
13
contract/contract-jvm/src/main/java/com/jd/blockchain/contract/jvm/ContractCodeLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.jd.blockchain.contract.jvm; | ||
|
||
import com.jd.blockchain.contract.engine.ContractCode; | ||
import utils.Bytes; | ||
|
||
/** | ||
* 合约加载器 | ||
*/ | ||
public interface ContractCodeLoader { | ||
|
||
ContractCode loadContract(Bytes address, long version, byte[] codeBytes); | ||
|
||
} |
26 changes: 0 additions & 26 deletions
26
...t/contract-jvm/src/main/java/com/jd/blockchain/contract/jvm/InstantiatedContractCode.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.