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

当合约方法中出现多个solidity的string类型时,建表将超出mysql单行大小限制报错 #67

Open
maoqingheng opened this issue Apr 25, 2024 · 4 comments

Comments

@maoqingheng
Copy link

2024-04-24 17:16:31.091 [pool-2-thread-1] ERROR c.w.b.d.export.tools.DataSourceUtils - export data table create failed, reason is :
java.sql.SQLSyntaxErrorException: Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

@maoqingheng
Copy link
Author

可以将代码里的数据库类型映射JavaTypeEnum中的varchar(4096)改成text

@wangyue168git
Copy link
Contributor

很好的案例,方便的话 ,可以提个PR么

@wangyue168git
Copy link
Contributor

还有分支和环境 方便的话也提供下 谢谢

@maoqingheng
Copy link
Author

master分支

下面是合约源码
pragma solidity >=0.6.10 <0.8.20;
pragma experimental ABIEncoderV2;

/*

  •   Copyright© (2018) WeBank Co., Ltd.
    
  •   Licensed under the Apache License, Version 2.0 (the "License");
    
  •   you may not use this file except in compliance with the License.
    
  •   You may obtain a copy of the License at
    
  •      http://www.apache.org/licenses/LICENSE-2.0
    
  •   Unless required by applicable law or agreed to in writing, software
    
  •   distributed under the License is distributed on an "AS IS" BASIS,
    
  •   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    
  •   See the License for the specific language governing permissions and
    
  •   limitations under the License.
    

*/
//SPDX-License-Identifier: Apache-2.0
contract EvidenceContract {

//Evidence struct
struct Evidence {
    address[] signer;
    string[] sig;
    string[] log;
    uint256[] updated;
    bool[] revoked;
    mapping(string => string) extra;
}
// Evidence hash => Evidence
mapping(bytes32 => Evidence) evidences;
// extra id => Evidence hash
mapping(string => bytes32) extraKeyMapping;


// Evidence attribute change event including signature and logs
event EvidenceAttributeChanged(
    bytes32 hash,
    address signer,
    string sig,
    string logs,
    uint256 updated
);

event CreateEvidence(
    bytes32 hash,
    address signer,
    string sig,
    string logs,
    uint256 updated
);

// Additional Evidence attribute change event
event EvidenceExtraAttributeChanged(
    bytes32 hash,
    address signer,
    string key,
    string value,
    uint256 updated
);

event Revoke(
    bytes32 hash,
    address signer,
    bool revoke
);

/**
 * Create evidence. Here, hash value is the key; signature and log are values. 
 * This will only create a new evidence if its hash does not exist.
 */
function createEvidence(
    bytes32[] memory hash,
    address[] memory signer,
    string[] memory sigs,
    string[] memory logs,
    uint256[] memory updated
)
    public
{
    uint256 sigSize = hash.length;
    for (uint256 i = 0; i < sigSize; i++) {
        Evidence storage evidence = evidences[hash[i]];
        if (!isHashExist(hash[i])) {
            evidence.sig.push(sigs[i]);
            evidence.log.push(logs[i]);
            evidence.signer.push(signer[i]);
            evidence.updated.push(updated[i]);
            evidence.revoked.push(false);
            emit CreateEvidence(hash[i], signer[i], sigs[i], logs[i], updated[i]);
        }
    }
}

/**
 * Add signature and logs to an existing evidence. Here, hash value is the key; signature and log are values. 
 */
function addSignatureAndLogs(
    bytes32[] memory hash,
    address[] memory signer,
    string[] memory sigs,
    string[] memory logs,
    uint256[] memory updated
)
    public
{
    uint256 sigSize = hash.length;
    for (uint256 i = 0; i < sigSize; i++) {
        Evidence storage evidence = evidences[hash[i]];
        if (isHashExist(hash[i])) {
            evidence.sig.push(sigs[i]);
            evidence.log.push(logs[i]);
            evidence.signer.push(signer[i]);
            evidence.updated.push(updated[i]);
            evidence.revoked.push(false);
            emit EvidenceAttributeChanged(hash[i], signer[i], sigs[i], logs[i], updated[i]);
        }
    }
}

/**
 * Create evidence by extra key. As in the normal createEvidence case, this further allocates
 * each evidence with an extra key in String format which caller can fetch as key,
 * to obtain the detailed info from within.
 * This will only create a new evidence if its hash does not exist.
 */
function createEvidenceWithExtraKey(
    bytes32[] memory hash,
    address[] memory signer,
    string[] memory sigs,
    string[] memory logs,
    uint256[] memory updated,
    string[] memory extraKey
)
    public
{
    uint256 sigSize = hash.length;
    for (uint256 i = 0; i < sigSize; i++) {
        Evidence storage evidence = evidences[hash[i]];
        if (!isHashExist(hash[i])) {
            evidence.sig.push(sigs[i]);
            evidence.log.push(logs[i]);
            evidence.signer.push(signer[i]);
            evidence.updated.push(updated[i]);
            evidence.revoked.push(false);
            if (!isEqualString(extraKey[i], "")) {
                extraKeyMapping[extraKey[i]] = hash[i];
            }
            emit CreateEvidence(hash[i], signer[i], sigs[i], logs[i], updated[i]);
        }
    }
}

/**
 * Create evidence by extra key. As in the normal createEvidence case, this further allocates
 * each evidence with an extra key in String format which caller can fetch as key,
 * to obtain the detailed info from within. This will only emit creation events when an evidence exists.
 */
function addSignatureAndLogsWithExtraKey(
    bytes32[] memory hash,
    address[] memory signer,
    string[] memory sigs,
    string[] memory logs,
    uint256[] memory updated,
    string[] memory extraKey
)
    public
{
    uint256 sigSize = hash.length;
    for (uint256 i = 0; i < sigSize; i++) {
        Evidence storage evidence = evidences[hash[i]];
        if (isHashExist(hash[i])) {
            evidence.sig.push(sigs[i]);
            evidence.log.push(logs[i]);
            evidence.signer.push(signer[i]);
            evidence.updated.push(updated[i]);
            evidence.revoked.push(false);
            if (!isEqualString(extraKey[i], "")) {
                extraKeyMapping[extraKey[i]] = hash[i];
            }
            emit EvidenceAttributeChanged(hash[i], signer[i], sigs[i], logs[i], updated[i]);
        }
    }
}

/**
 * Set arbitrary extra attributes to any EXISTING evidence.
 */
function setAttribute(
    bytes32[] memory hash,
    address[] memory signer,
    string[] memory key,
    string[] memory value,
    uint256[] memory updated
)
    public
{
    uint256 sigSize = hash.length;
    for (uint256 i = 0; i < sigSize; i++) {
        if (isHashExist(hash[i])) {
            Evidence storage evidence = evidences[hash[i]];
            evidence.extra[key[i]] = value[i];
            emit EvidenceExtraAttributeChanged(hash[i], signer[i], key[i], value[i], updated[i]);
        }
    }
}

function revoke(
    bytes32 _hash,
    address _signer,
    bool _revoke
)
    public
{
    require(isHashExist(_hash), "require evidence not exist");
    Evidence storage evidence = evidences[_hash];
    bool tag = false;
    for (uint256 i = 0; i < evidence.signer.length; i++) {
        if (_signer == evidence.signer[i]) {
            tag = true;
            evidence.revoked[i] = _revoke;
            emit Revoke(_hash, _signer, _revoke);
            break;
        }
    }
    require(tag, "signer of this evidence not exist");
}

function getAttribute(
    bytes32 hash,
    string memory key
)
    public
    view
    returns (string memory)
{
    return evidences[hash].extra[key];
}

function getEvidence(
    bytes32 hash
)
    public
    view
    returns (address[] memory signer, string[] memory sig, string[] memory log, uint256[] memory updated, bool[] memory revoked)
{
    require(isHashExist(hash), "require evidence not exist");
    return (evidences[hash].signer, evidences[hash].sig, evidences[hash].log, evidences[hash].updated, evidences[hash].revoked);
}

function isHashExist(bytes32 hash) public view returns (bool) {
    if (evidences[hash].signer.length != 0) {
        return true;
    }
    return false;
}

function getHashByExtraKey(
    string memory extraKey
)
    public
    view
    returns (bytes32)
{
    return extraKeyMapping[extraKey];
}

function isEqualString(string memory a, string memory b) private pure returns (bool) {	
    if (bytes(a).length != bytes(b).length) {	
        return false;	
    } else {	
        return keccak256(abi.encode(a)) == keccak256(abi.encode(b));	
    }	
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants