Blockchain 2.0 has become a term for decentralized blockchain databases.Utilize blockchain data structures to validate and store data,and use distributed node consensus algorithms to generate and update data,利用密码学的方式保证数据传输和访问的安全、利用由自动化脚本代码组成的智能合约,编程和操作数据的全新的分布式基础架构与计算范式。就是一个又一个区块组成的链条,每一个区块中保存了一定的信息,它们按照各自产生的时间顺序连接成链条
在区块链中,每个块包含了一定数量的交易信息和该块的唯一标识符,同时还包含了前一个块的哈希值。这样的设计保证了区块之间的顺序和完整性,一旦一个块被添加到区块链中,它就不可更改。这使得区块链成为一个安全可信的分布式账本,可用于记录和验证各种类型的交易。
function setDeposits(uint64 depositIndex,address traderAddr,uint16 tokenCode,uint64 amountE8)private{
deposits[depositIndex].traderAddr=traderAddr;
deposits[depositIndex].tokenCode=tokenCode;
deposits[depositIndex].pendingAmountE8=amountE8;
}
function setExeStatus(uint64 logicTimeSec,uint64 lastOperationIndex)private{
exeStatus.logicTimeSec=logicTimeSec;
exeStatus.lastOperationIndex=lastOperationIndex;
}
function confirmDeposit(uint64 depositIndex)private{
Deposit memory deposit=deposits[depositIndex];
uint176 accountKey=(uint176(deposit.tokenCode)<<160)|uint176(deposit.traderAddr);
TokenAccount memory account=accounts[accountKey];
//Check that pending amount is non-zero and no overflow would happen.
if(account.balanceE8+deposit.pendingAmountE8<=account.balanceE8)revert();
account.balanceE8+=deposit.pendingAmountE8;
deposits[depositIndex].pendingAmountE8=0;
accounts[accountKey].balanceE8+=deposit.pendingAmountE8;
emit ConfirmDepositEvent(deposit.traderAddr,deposit.tokenCode,account.balanceE8);
}
function initiateWithdraw(uint176 tokenAccountKey,uint64 amountE8)private{
uint64 balanceE8=accounts[tokenAccountKey].balanceE8;
uint64 pendingWithdrawE8=accounts[tokenAccountKey].pendingWithdrawE8;
if(balanceE8<amountE8||amountE8==0)revert();
balanceE8-=amountE8;
uint64 feeE8=calcFeeE8(amountE8,withdrawFeeRateE4,address(tokenAccountKey));
amountE8-=feeE8;
if(pendingWithdrawE8+amountE8<amountE8)revert();//check overflow
pendingWithdrawE8+=amountE8;
accounts[tokenAccountKey].balanceE8=balanceE8;
accounts[tokenAccountKey].pendingWithdrawE8=pendingWithdrawE8;
//Note that the fee account has a dummy trader address of 0.
if(accounts[tokenAccountKey&(0xffff<<160)].pendingWithdrawE8+feeE8>=feeE8){//no overflow
accounts[tokenAccountKey&(0xffff<<160)].pendingWithdrawE8+=feeE8;
}
emit InitiateWithdrawEvent(address(tokenAccountKey),uint16(tokenAccountKey>>160)/tokenCode/,
amountE8,pendingWithdrawE8);
}
function getDealInfo(uint32 pairId,uint64 priceE8,uint64 amount1E8,uint64 amount2E8)
private pure returns(DealInfo deal){
deal.stockCode=uint16(pairId);
deal.cashCode=uint16(pairId>>16);
if(deal.stockCode==deal.cashCode)revert();//we disallow homogeneous trading
deal.stockDealAmountE8=amount1E8<amount2E8?amount1E8:amount2E8;
uint cashDealAmountE8=uint(priceE8)uint(deal.stockDealAmountE8)/10*8;
if(cashDealAmountE8>=2**64)revert();
deal.cashDealAmountE8=uint64(cashDealAmountE8);
}