Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating data to datastore #548

Merged
merged 27 commits into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ All notable changes to this project will be documented in this file.
* Fixed `addModule` function to be backwards compatible and call the new `addModuleWithLabel` function with an empty label.
* Fixed event `ModuleAdded` to also emit `_label`.
* Fixed function `getModule` to also return the respective module label.
* Added datastore that is used to store data like investor list that is shared among modules.
* `getInvestorCount()` now returns length of investor array that is everyone who ever held some st or has kyc data attached.
* `holderCount()` returns the number of current st holders.
* Added flags for Investors. Accredited and canbuyfromsto are now flags

## STR
* Introduce new contract `STRGetter.sol`. It only contains the getter functions of the STR.
Expand All @@ -19,7 +23,9 @@ All notable changes to this project will be documented in this file.
* Removed `_polyToken` parameter from `initialize` function in `SecurityTokenRegistry`.

## GeneralTransferManager
* Add `_isAccredited` variable in the `modifyWhitelist()` function of the GeneralTransferManager.
* `modifyWhitelist()` function renamed to `modifyKYCData()`.
* Added functions to modify and get flags
* `canBuyFromSto` is now `canNotBuyFromSto` and it is the flag `1`

## Generalize
* Removed `_polyAddress` parameter from constructors of all modules and module factories.
Expand Down
149 changes: 84 additions & 65 deletions contracts/datastore/DataStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,29 @@ contract DataStore is DataStoreStorage, IDataStore {
//NB To modify a specific element of an array, First push a new element to the array and then delete the old element.
//Whenver an element is deleted from an Array, last element of that array is moved to the index of deleted element.
//Delegate with MANAGEDATA permission can modify data.

event SecurityTokenChanged(address indexed _oldSecurityToken, address indexed _newSecurityToken);

modifier onlyAuthorized() {
bool isOwner = msg.sender == IOwnable(address(securityToken)).owner();
require(isOwner ||
securityToken.isModule(msg.sender, DATA_KEY) ||
securityToken.checkPermission(msg.sender, address(this), MANAGEDATA),
_isAuthorized();
_;
}

function _isAuthorized() internal view {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok lov u

require(msg.sender == address(securityToken) ||
msg.sender == IOwnable(address(securityToken)).owner() ||
securityToken.checkPermission(msg.sender, address(this), MANAGEDATA) ||
securityToken.isModule(msg.sender, DATA_KEY),
"Unauthorized"
);
_;
}

modifier validKey(bytes32 _key) {
require(_key != bytes32(0), "Missing key");
require(_key != bytes32(0), "bad key");
_;
}

modifier validArrayLength(uint256 _keyLength, uint256 _dataLength) {
require(_keyLength == _dataLength, "Array length mismatch");
require(_keyLength == _dataLength, "bad length");
_;
}

Expand All @@ -56,26 +59,26 @@ contract DataStore is DataStoreStorage, IDataStore {
* @param _data Data to be stored against the key
*/
function setUint256(bytes32 _key, uint256 _data) external onlyAuthorized {
_setData(_key, _data);
_setData(_key, _data, false);
}

function setBytes32(bytes32 _key, bytes32 _data) external onlyAuthorized {
_setData(_key, _data);
_setData(_key, _data, false);
}

function setAddress(bytes32 _key, address _data) external onlyAuthorized {
_setData(_key, _data);
_setData(_key, _data, false);
}

function setString(bytes32 _key, string calldata _data) external onlyAuthorized {
_setData(_key, _data);
function setBool(bytes32 _key, bool _data) external onlyAuthorized {
_setData(_key, _data, false);
}

function setBytes(bytes32 _key, bytes calldata _data) external onlyAuthorized {
function setString(bytes32 _key, string calldata _data) external onlyAuthorized {
_setData(_key, _data);
}

function setBool(bytes32 _key, bool _data) external onlyAuthorized {
function setBytes(bytes32 _key, bytes calldata _data) external onlyAuthorized {
_setData(_key, _data);
}

Expand Down Expand Up @@ -106,19 +109,19 @@ contract DataStore is DataStoreStorage, IDataStore {
* @param _data Element to push into the array
*/
function insertUint256(bytes32 _key, uint256 _data) external onlyAuthorized {
_insertData(_key, _data);
_setData(_key, _data, true);
}

function insertBytes32(bytes32 _key, bytes32 _data) external onlyAuthorized {
_insertData(_key, _data);
_setData(_key, _data, true);
}

function insertAddress(bytes32 _key, address _data) external onlyAuthorized {
_insertData(_key, _data);
_setData(_key, _data, true);
}

function insertBool(bytes32 _key, bool _data) external onlyAuthorized {
_insertData(_key, _data);
_setData(_key, _data, true);
}

/**
Expand Down Expand Up @@ -150,25 +153,25 @@ contract DataStore is DataStoreStorage, IDataStore {
*/
function setUint256Multi(bytes32[] calldata _keys, uint256[] calldata _data) external onlyAuthorized validArrayLength(_keys.length, _data.length) {
for (uint256 i = 0; i < _keys.length; i++) {
_setData(_keys[i], _data[i]);
_setData(_keys[i], _data[i], false);
}
}

function setBytes32Multi(bytes32[] calldata _keys, bytes32[] calldata _data) external onlyAuthorized validArrayLength(_keys.length, _data.length) {
for (uint256 i = 0; i < _keys.length; i++) {
_setData(_keys[i], _data[i]);
_setData(_keys[i], _data[i], false);
}
}

function setAddressMulti(bytes32[] calldata _keys, address[] calldata _data) external onlyAuthorized validArrayLength(_keys.length, _data.length) {
for (uint256 i = 0; i < _keys.length; i++) {
_setData(_keys[i], _data[i]);
_setData(_keys[i], _data[i], false);
}
}

function setBoolMulti(bytes32[] calldata _keys, bool[] calldata _data) external onlyAuthorized validArrayLength(_keys.length, _data.length) {
for (uint256 i = 0; i < _keys.length; i++) {
_setData(_keys[i], _data[i]);
_setData(_keys[i], _data[i], false);
}
}

Expand All @@ -179,25 +182,25 @@ contract DataStore is DataStoreStorage, IDataStore {
*/
function insertUint256Multi(bytes32[] calldata _keys, uint256[] calldata _data) external onlyAuthorized validArrayLength(_keys.length, _data.length) {
for (uint256 i = 0; i < _keys.length; i++) {
_insertData(_keys[i], _data[i]);
_setData(_keys[i], _data[i], true);
}
}

function insertBytes32Multi(bytes32[] calldata _keys, bytes32[] calldata _data) external onlyAuthorized validArrayLength(_keys.length, _data.length) {
for (uint256 i = 0; i < _keys.length; i++) {
_insertData(_keys[i], _data[i]);
_setData(_keys[i], _data[i], true);
}
}

function insertAddressMulti(bytes32[] calldata _keys, address[] calldata _data) external onlyAuthorized validArrayLength(_keys.length, _data.length) {
for (uint256 i = 0; i < _keys.length; i++) {
_insertData(_keys[i], _data[i]);
_setData(_keys[i], _data[i], true);
}
}

function insertBoolMulti(bytes32[] calldata _keys, bool[] calldata _data) external onlyAuthorized validArrayLength(_keys.length, _data.length) {
for (uint256 i = 0; i < _keys.length; i++) {
_insertData(_keys[i], _data[i]);
_setData(_keys[i], _data[i], true);
}
}

Expand Down Expand Up @@ -273,16 +276,60 @@ contract DataStore is DataStoreStorage, IDataStore {
return boolArrayData[_key][_index];
}

function _setData(bytes32 _key, uint256 _data) internal validKey(_key) {
uintData[_key] = _data;
function getUint256ArrayElements(bytes32 _key, uint256 _startIndex, uint256 _endIndex) external view returns(uint256[] memory array) {
uint256 size = _endIndex - _startIndex + 1;
array = new uint256[](size);
for(uint256 i; i < size; i++)
array[i] = uintArrayData[_key][i + _startIndex];
}

function _setData(bytes32 _key, bytes32 _data) internal validKey(_key) {
bytes32Data[_key] = _data;
function getBytes32ArrayElements(bytes32 _key, uint256 _startIndex, uint256 _endIndex) external view returns(bytes32[] memory array) {
uint256 size = _endIndex - _startIndex + 1;
array = new bytes32[](size);
for(uint256 i; i < size; i++)
array[i] = bytes32ArrayData[_key][i + _startIndex];
}

function _setData(bytes32 _key, address _data) internal validKey(_key) {
addressData[_key] = _data;
function getAddressArrayElements(bytes32 _key, uint256 _startIndex, uint256 _endIndex) external view returns(address[] memory array) {
uint256 size = _endIndex - _startIndex + 1;
array = new address[](size);
for(uint256 i; i < size; i++)
array[i] = addressArrayData[_key][i + _startIndex];
}

function getBoolArrayElements(bytes32 _key, uint256 _startIndex, uint256 _endIndex) external view returns(bool[] memory array) {
uint256 size = _endIndex - _startIndex + 1;
array = new bool[](size);
for(uint256 i; i < size; i++)
array[i] = boolArrayData[_key][i + _startIndex];
}

function _setData(bytes32 _key, uint256 _data, bool _insert) internal validKey(_key) {
if (_insert)
uintArrayData[_key].push(_data);
else
uintData[_key] = _data;
}

function _setData(bytes32 _key, bytes32 _data, bool _insert) internal validKey(_key) {
if (_insert)
bytes32ArrayData[_key].push(_data);
else
bytes32Data[_key] = _data;
}

function _setData(bytes32 _key, address _data, bool _insert) internal validKey(_key) {
if (_insert)
addressArrayData[_key].push(_data);
else
addressData[_key] = _data;
}

function _setData(bytes32 _key, bool _data, bool _insert) internal validKey(_key) {
if (_insert)
boolArrayData[_key].push(_data);
else
boolData[_key] = _data;
}

function _setData(bytes32 _key, string memory _data) internal validKey(_key) {
Expand All @@ -293,10 +340,6 @@ contract DataStore is DataStoreStorage, IDataStore {
bytesData[_key] = _data;
}

function _setData(bytes32 _key, bool _data) internal validKey(_key) {
boolData[_key] = _data;
}

function _setData(bytes32 _key, uint256[] memory _data) internal validKey(_key) {
uintArrayData[_key] = _data;
}
Expand All @@ -313,51 +356,27 @@ contract DataStore is DataStoreStorage, IDataStore {
boolArrayData[_key] = _data;
}

function _insertData(bytes32 _key, uint256 _data) internal validKey(_key) {
uintArrayData[_key].push(_data);
}

function _insertData(bytes32 _key, bytes32 _data) internal validKey(_key) {
bytes32ArrayData[_key].push(_data);
}

function _insertData(bytes32 _key, address _data) internal validKey(_key) {
addressArrayData[_key].push(_data);
}

function _insertData(bytes32 _key, bool _data) internal validKey(_key) {
boolArrayData[_key].push(_data);
}

function _deleteUint(bytes32 _key, uint256 _index) internal validKey(_key) {
require(uintArrayData[_key].length > _index, "Invalid Index"); //Also prevents undeflow
if(uintArrayData[_key].length - 1 != _index) {
uintArrayData[_key][_index] = uintArrayData[_key][uintArrayData[_key].length - 1];
}
uintArrayData[_key][_index] = uintArrayData[_key][uintArrayData[_key].length - 1];
uintArrayData[_key].length--;
}

function _deleteBytes32(bytes32 _key, uint256 _index) internal validKey(_key) {
require(bytes32ArrayData[_key].length > _index, "Invalid Index"); //Also prevents undeflow
if(bytes32ArrayData[_key].length - 1 != _index) {
bytes32ArrayData[_key][_index] = bytes32ArrayData[_key][bytes32ArrayData[_key].length - 1];
}
bytes32ArrayData[_key][_index] = bytes32ArrayData[_key][bytes32ArrayData[_key].length - 1];
bytes32ArrayData[_key].length--;
}

function _deleteAddress(bytes32 _key, uint256 _index) internal validKey(_key) {
require(addressArrayData[_key].length > _index, "Invalid Index"); //Also prevents undeflow
if(addressArrayData[_key].length - 1 != _index) {
addressArrayData[_key][_index] = addressArrayData[_key][addressArrayData[_key].length - 1];
}
addressArrayData[_key][_index] = addressArrayData[_key][addressArrayData[_key].length - 1];
addressArrayData[_key].length--;
}

function _deleteBool(bytes32 _key, uint256 _index) internal validKey(_key) {
require(boolArrayData[_key].length > _index, "Invalid Index"); //Also prevents undeflow
if(boolArrayData[_key].length - 1 != _index) {
boolArrayData[_key][_index] = boolArrayData[_key][boolArrayData[_key].length - 1];
}
boolArrayData[_key][_index] = boolArrayData[_key][boolArrayData[_key].length - 1];
boolArrayData[_key].length--;
}
}
4 changes: 2 additions & 2 deletions contracts/datastore/DataStoreStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ contract DataStoreStorage {
mapping (bytes32 => address[]) internal addressArrayData;
mapping (bytes32 => bool[]) internal boolArrayData;

uint8 constant DATA_KEY = 6;
bytes32 public constant MANAGEDATA = "MANAGEDATA";
uint8 internal constant DATA_KEY = 6;
bytes32 internal constant MANAGEDATA = "MANAGEDATA";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then this two variables won't be accessible

}
8 changes: 8 additions & 0 deletions contracts/interfaces/IDataStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,12 @@ interface IDataStore {
function getAddressArrayElement(bytes32 _key, uint256 _index) external view returns(address);

function getBoolArrayElement(bytes32 _key, uint256 _index) external view returns(bool);

function getUint256ArrayElements(bytes32 _key, uint256 _startIndex, uint256 _endIndex) external view returns(uint256[] memory);

function getBytes32ArrayElements(bytes32 _key, uint256 _startIndex, uint256 _endIndex) external view returns(bytes32[] memory);

function getAddressArrayElements(bytes32 _key, uint256 _startIndex, uint256 _endIndex) external view returns(address[] memory);

function getBoolArrayElements(bytes32 _key, uint256 _startIndex, uint256 _endIndex) external view returns(bool[] memory);
}
5 changes: 5 additions & 0 deletions contracts/interfaces/ISecurityToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ interface ISecurityToken {
*/
function getInvestorCount() external view returns(uint256);

/**
* @notice Gets the holder count (investors with non zero balance)
*/
function holderCount() external view returns(uint256);

/**
* @notice Overloaded version of the transfer function
* @param _to receiver of transfer
Expand Down
Loading