Coder Social home page Coder Social logo

dcrm's People

Contributors

weiqiushi avatar waterflier avatar lurenpluto avatar fulldecent avatar streetycat avatar photosssa avatar

Stargazers

 avatar  avatar xinbenlv avatar

Watchers

 avatar xinbenlv avatar  avatar

dcrm's Issues

Create test case CI

Please use GitHub Actions to create test case with performance recording / gas for function usage

sortedlist 试两次

{ "hash": "0x39c767e230f1cc4d8fa7baa4ef8c39bc2e4add8680d09bfe086e1efdaa0d6437", "score": 290 }, // 10
{ "hash": "0x39c767e230f1cc4d8fa7baa4ef8c39bc2e4add8680d09bfe086e1efdaa0d6438", "score": 290 }, // 10

看那个先进

Optimize sorted list

This new API is more efficient assuming update is run more often than ranking.

  1. Update: 1 SSTORE, N SLOAD
  2. Ranking: 2N SLOAD

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// todo: getDataSize 必须不要回答0(现在可以回答0)

library TopN {
    // stores the top N scores, order is undefined, except that any zero scores are at the end
    struct List {
        uint256 maxLength;
        mapping(uint256 => bytes32) public keys;
        mapping(uint256 => uint256) public values;
    }

    error InvalidKey(bytes32 key);

    error ValueTooSmall(uint256 value);

    error MaxLengthCannotShrink();

    /// @notice 获取一个值
    /// @param  self      TopN数据结构
    /// @param  maxLength 最大长度, 必须不小于当前最大长度
    function setMaxLength(List storage self, uint256 maxLength) public {
        if (maxLength < self.maxLength) {
            revert MaxLengthCannotShrink();
        }
        self.maxLength = maxLength;
    }

    /// @notice 更新一个值
    /// @param  self  TopN数据结构
    /// @param  key   键,必须部位0
    /// @param  value 值,必须大于当前这个key的值
    function updateValue(List storage self, bytes32 key, uint256 value) public {
        if (key == bytes32(0)) {
            revert InvalidKey(key);
        }
        if (value == 0) {
            revert ValueTooSmall(value);
        }

        // 找最小的值,如果比最小的值还小,就不用更新了
        uint256 replaceAt = 0;
        uint256 replaceValue = 0;
        bool found = false;
        for (uint256 i = 0; i < self.maxLength; i++) {
            uint256 v = self.values[i];
            if (v == 0) {
                // insert
                self.keys[i] = key;
                self.values[i] = value;
                return;
            }
            if (self.keys[i] == key) {
                if (value <= v) {
                    revert InvalidValue(value);
                }
                self.values[i] = value;
                return;
            }
            if (value > v && (v < replaceValue || !found)) {
                replaceValue = v;
                replaceAt = i;
                found = true;
            }
        }
        if (found) {
            self.keys[replaceAt] = key;
            self.values[replaceAt] = value;
        }
    }

    // todo: 如果不同的key有相同的value,他们每一个ranking一样。这个好吗?

    /// @notice 考虑一个key的排名
    /// @param  self  TopN数据结构
    /// @param  key   键,必须部位0
    /// @return 最大为1,第二大为2。。。。。。,如果不存在,返回0
    function getRanking(List storage self, bytes32 key) public view returns (uint256) {
        // 找key和value
        uint256 keyValue = 0;
        for (uint256 i = 0; i < self.maxLength; i++) {
            if (self.keys[i] == key) {
                keyValue = self.values[i];
                break;
            }
            if (self.keys[i] == bytes32(0)) {
                return 0;
            }
        }
        if (keyValue == 0) {
            return 0;
        }

        // 找比这个值大的值
        uint256 ranking = 1;
        for (uint256 i = 0; i < self.maxLength; i++) {
            if (self.values[i] > keyValue) {
                ranking += 1;
            }
            if (self.values[i] == 0) {
                break;
            }
        }
        return ranking;
    }
}

Public data gameplay (NFT inscription might be a good name?) rules darft

I am preparing for the "public data gameplay" ,Can have some interaction with communities based on ERC721 NFTs.

a. Qualified storage suppliers: Only suppliers meeting certain criteria (time, a certain amount of data delivery size) are eligible to participate in the public data storage reward program.

b. Qualified suppliers actively store public data in a certain NFT contract.

c. In specific blocks under certain conditions, SHOW the designated Merkle path of public data is considered a successful storage, and successful storage SHOW will be rewarded. The reward comes from the system, and the owner of the public data can also contribute addition rewards.

d. In a settlement period (usually including multiple eligible reward blocks), the most showed public data (Top n? and possibly exceeding a certain base amount) will earn additional rewards for the author/owner and the suppliers of the public data.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.