智能合約源碼解析之Bridge Contract

源代碼

演算法描述參見我的博客文章- 有效的在基於以太坊EVM的鏈之間跨鏈,下面主要分析智能合約代碼:

Bridge.sol

數據結構

Validator的獎勵:

reward = base + a*n (n是Header中提議的塊數 = end-start)

struct Reward { uint256 base; uint256 a;}Reward reward;uint256 public maxReward;

Epoch隨機數種子

bytes32 public epochSeed = keccak256(block.difficulty + block.number + now);

籌碼/保證金(Stake)

struct Stake { // Validator預付的保證金金額及validator的地址 uint256 amount; address staker;}mapping(address => uint256) stakers; // Validator MapStake[] stakes; // 保證金數組uint256 public stakeSum; // 保證金總額address public stakeToken; // 保證金的幣種

全局變數

// 連續塊頭的默克爾樹的根mapping(address => bytes32[]) roots;// 每一個橋接鏈的最後一塊的塊號mapping(address => uint256) lastBlock;// 只有Admin才能創建和建立側鏈和主鏈的映射// fromChainId => (oldTokenAddr => newTokenAddr)mapping(address => mapping(address => address)) tokens;Block// 對一個給定的鏈,一個特定的EPOCH,記錄開始塊和終塊並提供Headerrootevent RootStorage(address indexed chain, uint256 indexed start, uint256 indexed end, bytes32 headerRoot, uint256 i, address proposer);event Deposit(address indexed user, address indexed toChain, address indexed depositToken, address fromChain, uint256 amount);event Withdraw(address indexed user, address indexed fromChain, address indexed withdrawToken, uint256 amount);event TokenAdded(address indexed fromChain, address indexed origToken, address indexed newToken);event TokenAssociated(address indexed toChain, address indexed fromToken, address indexed toToken);

// Pending withdrawals. The user prepares a withdrawal with tx data and then // releases it with a withdraw. It can be overwritten by the user and gets wiped // upon withdrawal. struct Withdrawal { address withdrawToken; // Token to withdraw (i.e. the one mapped to deposit) address fromChain; uint256 amount; // Number of atomic units to withdraw bytes32 txRoot; // Transactions root for the block housing this tx bytes32 txHash; // Hash of this tx bytes32 receiptsRoot; // Receipts root for the block housing this tx } mapping(address => Withdrawal) pendingWithdrawals;

主要函數

保證金/Stake

stake(uint256 amount)

以特定的幣種充值保證金。設置充值幣種,充值數額/總額,存入Validator數組

destake(uint256 amount)

撤銷保證金。調整充值數額/總額

function proposeRoot(bytes32 headerRoot, address chainId, uint256 end, bytes sigs)

將Headerroot存入roots數組

計算獎勵金額並發送

生成EpochSeed=keccak256(block.difficulty + block.number + now);

發送RootStorage Event

記錄本Epoch的Last Block

Helper函數

function toBytes(address a) constant returns (bytes b) function toBytes(uint256 x) returns (bytes b) function encodeAddress(address a) returns(bytes) function getStake(address a) public constant returns (uint256) function getStakeIndex(address a) public constant returns (uint256) function getLastBlock(address fromChain) public constant returns (uint256) // 挑選提議者。被挑中的概率取決於保證金額大小function getProposer() public constant returns (address) function getTokenMapping(address chain, address token) public constant returns (address) // 取32 bytes強制類型轉換成byes32function getBytes32(uint64 start, bytes data) pure returns (bytes32) // 取32 bytes強制類型轉換成uint256function getUint256(uint64 start, bytes data) pure returns (uint256)// 取8 bytes強制類型轉換成uint64function getUint64(uint64 start, bytes data) pure returns (uint64) // proof is a concatenated set of [right][hash], i.e. 33 byte chunksfunction merkleProof(bytes32 leaf, bytes32 targetHash, bytes proof) private constant returns (bool) // Change the number of validators required to allow a passed header rootfunction updateValidatorThreshold(uint256 newThreshold) public onlyAdmin()// The admin can update the reward at any time.function updateReward(uint256 base, uint256 a, uint256 max) public onlyAdmin() // 保證金幣種只能在實例初始化的時候設定function Bridge(address token) modifier onlyAdmin()

推薦閱讀:

如何通過以太坊智能合約來進行眾籌(ICO)
美鏈BEC合約漏洞技術分析
逆向分析以太坊智能合約
教你執行以太坊智能合約或轉賬
智能合約是怎樣運作的?三分鐘讀懂智能合約

TAG:智能合約 | 以太坊 | 跨鏈 |