区块链技术,也被称之为分布式账本技术,是一种互联网数据库技术,其特点是去中心化、公开透明,让每个人均可参与数据库记录。区块链技术不是一个单项的技术,而是一个集成了多方面研究成果基础之上的综合性技术系统。There are three indispensable core technologies:consensus mechanism,Cryptography principle and distributed data storage.
DApp是指以区块链为底层技术平台的分布式应用程序,它使得开发者可以构建去中心化和自主运行的应用程序,并通过链上的合约机制实现代码不可更改性和事务透明性
//Write back to storage before making the transfer.
accounts[accountKey].pendingWithdrawE8=0;
uint truncatedWei=amountE8(ETH_SCALE_FACTOR/10*8);
address withdrawAddr=traders[traderAddr].withdrawAddr;
if(withdrawAddr==0)withdrawAddr=traderAddr;
withdrawAddr.transfer(truncatedWei);
emit WithdrawEvent(traderAddr,0,"ETH",uint64(amountE8),exeStatus.lastOperationIndex);
}
//Withdraw token(other than ETH)from the contract.
function withdrawToken(address traderAddr,uint16 tokenCode)external{
if(traderAddr==0)revert();
if(tokenCode==0)revert();//this function does not handle ETH
if(msg.data.length!=4+32+32)revert();//length condition of param count
TokenInfo memory tokenInfo=tokens[tokenCode];
if(tokenInfo.scaleFactor==0)revert();//unsupported token
uint176 accountKey=uint176(tokenCode)<<160|uint176(traderAddr);
uint amountE8=accounts[accountKey].pendingWithdrawE8;
if(amountE8==0)return;
//Write back to storage before making the transfer.
accounts[accountKey].pendingWithdrawE8=0;
uint truncatedAmount=amountE8uint(tokenInfo.scaleFactor)/10*8;
address withdrawAddr=traders[traderAddr].withdrawAddr;
if(withdrawAddr==0)withdrawAddr=traderAddr;
if(!Token(tokenInfo.tokenAddr).transfer(withdrawAddr,truncatedAmount))revert();
emit WithdrawEvent(traderAddr,tokenCode,tokens[tokenCode].symbol,uint64(amountE8),
exeStatus.lastOperationIndex);
}
//Transfer the collected fee out of the contract.
function transferFee(uint16 tokenCode,uint64 amountE8,address toAddr)external{
if(msg.sender!=admin)revert();
if(toAddr==0)revert();
if(msg.data.length!=4+32+32+32)revert();
TokenAccount memory feeAccount=accounts[uint176(tokenCode)<<160];
uint64 withdrawE8=feeAccount.pendingWithdrawE8;
if(amountE8<withdrawE8){
withdrawE8=amountE8;
}
feeAccount.pendingWithdrawE8-=withdrawE8;
accounts[uint176(tokenCode)<<160]=feeAccount;
TokenInfo memory tokenInfo=tokens[tokenCode];
uint originalAmount=uint(withdrawE8)uint(tokenInfo.scaleFactor)/10*8;
if(tokenCode==0){//ETH
toAddr.transfer(originalAmount);
}else{
if(!Token(tokenInfo.tokenAddr).transfer(toAddr,originalAmount))revert();
}
emit TransferFeeEvent(tokenCode,withdrawE8,toAddr);
}
//Replay the trading sequence from the off-chain ledger exactly onto the on-chain ledger.
function exeSequence(uint header,uint[]body)external{
if(msg.sender!=admin)revert();
uint64 nextOperationIndex=uint64(header);
if(nextOperationIndex!=exeStatus.lastOperationIndex+1)revert();//check sequence index
uint64 newLogicTimeSec=uint64(header>>64);
if(newLogicTimeSec<exeStatus.logicTimeSec)revert();
for(uint i=0;i<body.length;nextOperationIndex++){
uint bits=body;