Contract-Development

合約如何知道要查詢哪個庫或知道庫地址?

  • September 1, 2018

合約如何知道要查詢哪個庫?例如下面程式碼中的範例(程式碼來自solidity docs:https ://solidity.readthedocs.io/en/v0.4.24/contracts.html?highlight=library#libraries

pragma solidity ^0.4.22;

library Set {
 // We define a new struct datatype that will be used to
 // hold its data in the calling contract.
 struct Data { mapping(uint => bool) flags; }

 // Note that the first parameter is of type "storage
 // reference" and thus only its storage address and not
 // its contents is passed as part of the call.  This is a
 // special feature of library functions.  It is idiomatic
 // to call the first parameter `self`, if the function can
 // be seen as a method of that object.
 function insert(Data storage self, uint value)
     public
     returns (bool)
 {
     if (self.flags[value])
         return false; // already there
     self.flags[value] = true;
     return true;
 }

 function remove(Data storage self, uint value)
     public
     returns (bool)
 {
     if (!self.flags[value])
         return false; // not there
     self.flags[value] = false;
     return true;
 }

 function contains(Data storage self, uint value)
     public
     view
     returns (bool)
 {
     return self.flags[value];
 }
}

contract C {
   Set.Data knownValues;

   function register(uint value) public {
       // The library functions can be called without a
       // specific instance of the library, since the
       // "instance" will be the current contract.
       require(Set.insert(knownValues, value));
   }
   // In this contract, we can also directly access knownValues.flags, if we want.
}

合約 C 是如何知道 Set 庫的位置以及如果有其他稱為 Set 的庫怎麼辦?

此程式碼中是否缺少某些內容?

在同一份文件中,它說:

由於編譯器不知道庫將部署在哪裡,這些地址必須由連結器填充到最終的字節碼中

並繼續解釋如何做到這一點。

引用自:https://ethereum.stackexchange.com/questions/57877