diff --git a/contracts/Git3.sol b/contracts/Git3.sol index 64502d2..2a09d8c 100644 --- a/contracts/Git3.sol +++ b/contracts/Git3.sol @@ -111,12 +111,10 @@ contract Git3 { srs = nameToRefInfo[name]; if (srs.hash1 == bytes32(0) && srs.hash2 == bytes8(0)) { - // first store refHash + // store refHash for the first time require(refs.length <= uint256(uint192(int192(-1))),"refs exceed valid length"); - _setRefInfo(nameToRefInfo[name],refHash,uint192(refs.length)); - console.log("refs_length:",refs.length); refs.push(name); }else{ // only update refHash @@ -126,14 +124,19 @@ contract Git3 { // delRef(path: string): Promise function delRef(string memory name) public { + // only execute `sload` once to reduce gas consumption refInfo memory srs; srs = nameToRefInfo[name]; + uint256 refsLen = refs.length; require(srs.hash1 != bytes32(0) || srs.hash2 != bytes8(0),"Reference of this name does not exist"); - refs[srs.index] = refs[refs.length - 1]; - nameToRefInfo[refs[refs.length - 1]].index = srs.index; - delete refs[refs.length - 1]; + require(srs.index < refsLen,"System Error: Invalid index"); + if (srs.index < refsLen-1){ + refs[srs.index] = refs[refsLen - 1]; + nameToRefInfo[refs[refsLen - 1]].index = srs.index; + } + refs.pop(); delete nameToRefInfo[name]; } } \ No newline at end of file diff --git a/test/git3-test.js b/test/git3-test.js index f987723..6b4628f 100644 --- a/test/git3-test.js +++ b/test/git3-test.js @@ -88,12 +88,19 @@ describe("Git3 Test", function () { expect(refs[0]).to.eql([data0,key0]); expect(refs[1]).to.eql([data1,key1]); expect(refs[2]).to.eql([data2,key2]); + expect(refs.length).to.eql(3); // check delRef await git3.delRef(key0); refs = await git3.listRefs(); expect(refs[0]).to.eql([data2,key2]); expect(refs[1]).to.eql([data1,key1]); + expect(refs.length).to.eql(2); + + await git3.delRef(key1); + refs = await git3.listRefs(); + expect(refs[0]).to.eql([data2,key2]); + expect(refs.length).to.eql(1); })