Skip to content

Commit

Permalink
add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Nov 19, 2024
1 parent 5154fe7 commit d17c737
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion x/evm/contracts/counter/Counter.go

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions x/evm/contracts/counter/Counter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ contract Counter is IIBCAsyncCallback {

constructor() payable {}

function increase_for_fuzz(uint64 num) external {
for (uint64 i = 0; i < num; i++) {
increase();
function increase_for_fuzz(uint64 num) public {
if (num == 0) {
return;
}

increase();
increase_for_fuzz(num - 1);
}

function increase() public payable {
count++;

emit increased(count - 1, count);
}

Expand Down
26 changes: 16 additions & 10 deletions x/evm/keeper/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package keeper_test

import (
"bytes"
"sync"
"sync/atomic"
"testing"

storetypes "cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
coretypes "github.com/ethereum/go-ethereum/core/types"

"github.com/holiman/uint256"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -48,9 +45,6 @@ func Fuzz_Concurrent_Counter(f *testing.F) {
inputBz, err := parsed.Pack("increase_for_fuzz", uint64(numCount))
require.NoError(t, err)

atomicBloomBytes := atomic.Pointer[[]byte]{}
atomicBloomBytes.Store(nil)

var wg sync.WaitGroup
cacheCtxes := make([]sdk.Context, numThread)
for i := uint8(0); i < numThread; i++ {
Expand All @@ -65,21 +59,33 @@ func Fuzz_Concurrent_Counter(f *testing.F) {
res, logs, err := input.EVMKeeper.EVMCall(ctx, caller, contractAddr, inputBz, nil, nil)
require.NoError(t, err)
require.Empty(t, res)
bloomBytes := coretypes.LogsBloom(logs.ToEthLogs())
prev := atomicBloomBytes.Swap(&bloomBytes)
require.True(t, prev == nil || bytes.Equal(*prev, bloomBytes))
assertLogs(t, contractAddr, logs)
}(cacheCtx)
}
wg.Wait()

for i := uint8(0); i < numThread; i++ {
count := getCount(t, cacheCtxes[i], input, contractAddr)
require.Equal(t, uint256.NewInt(uint64(numCount)), count)
require.NotEmpty(t, atomicBloomBytes.Load())
}
})
}

func assertLogs(t *testing.T, contractAddr common.Address, logs []types.Log) {
for i := range logs {
require.Equal(t, contractAddr.Hex(), logs[i].Address)
dataBz, err := hexutil.Decode(logs[i].Data)
require.NoError(t, err)
require.Len(t, dataBz, 64)
before := new(uint256.Int).SetBytes(dataBz[:32])
after := new(uint256.Int).SetBytes(dataBz[32:])

require.NoError(t, err)
require.Equal(t, uint256.NewInt(uint64(i)), before)
require.Equal(t, uint256.NewInt(uint64(i+1)), after)
}
}

func getCount(t *testing.T, ctx sdk.Context, input TestKeepers, contractAddr common.Address) *uint256.Int {
parsed, err := counter.CounterMetaData.GetAbi()
require.NoError(t, err)
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions x/evm/state/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ func Test_SnapshotRevert(t *testing.T) {
// take more snapshots
stateDB.Snapshot()
stateDB.SubRefund(10)
require.Equal(t, uint64(90), stateDB.GetBalance(common.BytesToAddress(addr1)).Uint64())
require.Equal(t, uint64(80), stateDB.GetRefund())
require.Equal(t, evmtypes.NewLogs([]*coretypes.Log{log1, log2}), stateDB.Logs()[:2])
require.True(t, stateDB.AddressInAccessList(common.BytesToAddress(addr1)))
require.True(t, stateDB.AddressInAccessList(common.BytesToAddress(addr2)))
require.Equal(t, common.BytesToHash([]byte("value2")), stateDB.GetState(common.BytesToAddress(addr1), common.BytesToHash([]byte("key"))))
require.Equal(t, common.BytesToHash([]byte("value2")), stateDB.GetState(common.BytesToAddress(addr2), common.BytesToHash([]byte("key"))))

stateDB.Snapshot()
stateDB.SubBalance(common.BytesToAddress(addr1), uint256.NewInt(10), tracing.BalanceDecreaseSelfdestructBurn)

Expand Down

0 comments on commit d17c737

Please sign in to comment.