Skip to content

Commit

Permalink
Merge branch 'master' into fix-typos
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Mar 22, 2024
2 parents 7053253 + 23cff4d commit dde87ea
Show file tree
Hide file tree
Showing 2 changed files with 317 additions and 1 deletion.
100 changes: 99 additions & 1 deletion api/coreservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,29 @@ import (
"testing"
"time"

. "github.com/agiledragon/gomonkey/v2"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/eth/tracers/logger"
"github.com/golang/mock/gomock"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/actpool"
"github.com/iotexproject/iotex-core/api/logfilter"
"github.com/iotexproject/iotex-core/blockchain"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/blockchain/blockdao"
"github.com/iotexproject/iotex-core/test/identityset"
"github.com/iotexproject/iotex-core/test/mock/mock_blockdao"
"github.com/iotexproject/iotex-core/test/mock/mock_blockindex"
"github.com/iotexproject/iotex-core/test/mock/mock_envelope"
"github.com/iotexproject/iotex-core/testutil"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
)

func TestLogsInRange(t *testing.T) {
Expand Down Expand Up @@ -387,3 +394,94 @@ func TestProofAndCompareReverseActions(t *testing.T) {
})
}
}

func TestReverseActionsInBlock(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

var (
blkDAO = mock_blockdao.NewMockBlockDAO(ctrl)
envelope = mock_envelope.NewMockEnvelope(ctrl)
core = &coreService{dao: blkDAO}
blk = &block.Block{
Header: block.Header{},
Body: block.Body{
Actions: []*action.SealedEnvelope{&action.SealedEnvelope{Envelope: envelope}},
},
Footer: block.Footer{},
Receipts: nil,
}
receiptes = []*action.Receipt{
&action.Receipt{
ActionHash: hash.ZeroHash256,
},
}
)

t.Run("CheckParams", func(t *testing.T) {
t.Run("ReverseStartGreaterThanSize", func(t *testing.T) {
actions := core.reverseActionsInBlock(blk, 2, 1)
require.Empty(actions)
})

t.Run("CountIsZero", func(t *testing.T) {
actions := core.reverseActionsInBlock(blk, 1, 0)
require.Empty(actions)
})
})

t.Run("FailedToGetReceiptFromDAO", func(t *testing.T) {
blkDAO.EXPECT().GetReceipts(gomock.Any()).Return(nil, errors.New(t.Name())).Times(1)
actions := core.reverseActionsInBlock(blk, 0, 1)
require.Empty(actions)
})

t.Run("ForeachActions", func(t *testing.T) {
t.Run("FailedToHash", func(t *testing.T) {
p := NewPatches()
defer p.Reset()

p = p.ApplyMethodReturn(&action.SealedEnvelope{}, "Hash", nil, errors.New(t.Name()))

blkDAO.EXPECT().GetReceipts(gomock.Any()).Return(receiptes, nil).Times(1)

actions := core.reverseActionsInBlock(blk, 0, 1)
require.Empty(actions)
})

t.Run("FailedToGetReceiptFromMap", func(t *testing.T) {
p := NewPatches()
defer p.Reset()

p = p.ApplyMethodReturn(&action.SealedEnvelope{}, "Hash", hash.BytesToHash256([]byte("test")), nil)

blkDAO.EXPECT().GetReceipts(gomock.Any()).Return(receiptes, nil).Times(1)

actions := core.reverseActionsInBlock(blk, 0, 1)
require.Empty(actions)
})

t.Run("ReverseActionsInBlockSuccess", func(t *testing.T) {
p := NewPatches()
defer p.Reset()

p = p.ApplyMethodReturn(&action.SealedEnvelope{}, "Hash", hash.ZeroHash256, nil)
p = p.ApplyMethodReturn(&action.SealedEnvelope{}, "SenderAddress", identityset.Address(1))
p = p.ApplyMethodReturn(&action.SealedEnvelope{}, "Proto", &iotextypes.Action{})
p = p.ApplyMethodReturn(&block.Header{}, "BlockHeaderCoreProto", &iotextypes.BlockHeaderCore{Timestamp: timestamppb.Now()})

blkDAO.EXPECT().GetReceipts(gomock.Any()).Return(receiptes, nil).Times(1)
envelope.EXPECT().GasPrice().Return(big.NewInt(1)).Times(1)
actions := core.reverseActionsInBlock(blk, 0, 1)
require.Equal(1, len(actions))
})
})

t.Run("StartIsNotZero", func(t *testing.T) {
blkDAO.EXPECT().GetReceipts(gomock.Any()).Return(nil, errors.New(t.Name())).Times(1)
blk.Actions = append(blk.Actions, &action.SealedEnvelope{})
actions := core.reverseActionsInBlock(blk, 0, 1)
require.Empty(actions)
})
}
218 changes: 218 additions & 0 deletions test/mock/mock_envelope/mock_envelope.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dde87ea

Please sign in to comment.