From 2eb50b5f6f84d5febaf5e3ef67d525ba8793d6b5 Mon Sep 17 00:00:00 2001 From: Arijit Das Date: Tue, 21 Sep 2021 23:12:13 +0530 Subject: [PATCH] Add test to destroy contract --- test/contract/contracts/GLDToken.sol | 3 +++ test/contract/src/index.js | 15 +++++++++++++++ test/helper.go | 19 +++++++++++++++++++ test/integration_test.go | 28 ++++++++++++++++++++++++++-- 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/test/contract/contracts/GLDToken.sol b/test/contract/contracts/GLDToken.sol index 4cd3205f8..596baad3b 100644 --- a/test/contract/contracts/GLDToken.sol +++ b/test/contract/contracts/GLDToken.sol @@ -4,4 +4,7 @@ contract GLDToken is ERC20 { constructor() ERC20("Gold", "GLD") { _mint(msg.sender, 1000000000000000000000); } + function destroy() public { + selfdestruct(payable(msg.sender)); + } } diff --git a/test/contract/src/index.js b/test/contract/src/index.js index 37639542e..163cce686 100644 --- a/test/contract/src/index.js +++ b/test/contract/src/index.js @@ -22,6 +22,21 @@ fastify.get('/v1/deployContract', async (req, reply) => { } }); +fastify.get('/v1/destroyContract', async (req, reply) => { + const addr = req.query.addr; + + const Token = await hre.ethers.getContractFactory("GLDToken"); + const token = await Token.attach(addr); + + await token.destroy(); + + return { + txHash: token.deployTransaction.hash, + blockNumber: token.deployTransaction.blockNumber, + blockHash: token.deployTransaction.blockHash, + } +}); + fastify.get('/v1/sendEth', async (req, reply) => { const to = req.query.to; const value = req.query.value; diff --git a/test/helper.go b/test/helper.go index e458bfde7..2f20454c1 100644 --- a/test/helper.go +++ b/test/helper.go @@ -60,3 +60,22 @@ func SendEth(to string, value string) (*Tx, error) { return &tx, nil } + +type ContractDestroyed struct { + TransactionHash string `json:"txHash"` + BlockNumber int64 `json:"blockNumber"` + BlockHash string `json:"blockHash"` +} + +func DestoyContract(addr string) (*ContractDestroyed, error) { + res, err := http.Get(fmt.Sprintf("%s/v1/destroyContract?addr=%s", srvUrl, addr)) + if err != nil { + return nil, err + } + defer res.Body.Close() + + var data ContractDestroyed + decoder := json.NewDecoder(res.Body) + + return &data, decoder.Decode(&data) +} diff --git a/test/integration_test.go b/test/integration_test.go index 43cb86261..8da5b143d 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -9,12 +9,12 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/rlp" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - integration "github.com/vulcanize/ipld-eth-server/test" - "github.com/ethereum/go-ethereum/ethclient" + integration "github.com/vulcanize/ipld-eth-server/test" ) const nonExistingBlockHash = "0x111111111111111111111111111111111111111111111111111111111111111" @@ -391,6 +391,30 @@ var _ = Describe("Integration test", func() { Expect(err).To(MatchError("header not found")) Expect(gethStorage).To(Equal(ipldStorage)) }) + + It("get storage after selfdestruct", func() { + totalSupplyIndex := "0x2" + zeroHash := make([]byte, 32) + + tx, err := integration.DestoyContract(contract.Address) + Expect(err).ToNot(HaveOccurred()) + + gethStorage1, err := gethClient.StorageAt(ctx, common.HexToAddress(contract.Address), common.HexToHash(totalSupplyIndex), big.NewInt(tx.BlockNumber-1)) + Expect(err).ToNot(HaveOccurred()) + gethStorage2, err := gethClient.StorageAt(ctx, common.HexToAddress(contract.Address), common.HexToHash(totalSupplyIndex), big.NewInt(tx.BlockNumber)) + Expect(err).ToNot(HaveOccurred()) + + Expect(gethStorage1).NotTo(Equal(gethStorage2)) + Expect(gethStorage2).To(Equal(zeroHash)) + + ipldStorage1, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract.Address), common.HexToHash(totalSupplyIndex), big.NewInt(tx.BlockNumber-1)) + Expect(err).ToNot(HaveOccurred()) + ipldStorage2, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract.Address), common.HexToHash(totalSupplyIndex), big.NewInt(tx.BlockNumber)) + Expect(err).ToNot(HaveOccurred()) + + Expect(ipldStorage1).To(Equal(gethStorage1)) + Expect(ipldStorage2).To(Equal(gethStorage2)) + }) }) Describe("eth call", func() {