Skip to content

Commit

Permalink
UT fix (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal authored Nov 28, 2023
1 parent 013b5f2 commit daa678a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 38 deletions.
1 change: 0 additions & 1 deletion consensus/polybft/consensus_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ func (c *consensusRuntime) initStakeManager(logger hcf.Logger, dbTx *bolt.Tx) er
logger.Named("stake-manager"),
c.state,
wallet.NewEcdsaSigner(c.config.Key),
contracts.EpochManagerContract,
contracts.StakeManagerContract,
c.config.blockchain,
c.config.polybftBackend,
Expand Down
3 changes: 1 addition & 2 deletions consensus/polybft/stake_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func newStakeManager(
logger hclog.Logger,
state *State,
key ethgo.Key,
validatorSetAddr, stakeManagerAddr types.Address,
stakeManagerAddr types.Address,
blockchain blockchainBackend,
polybftBackend polybftBackend,
maxValidatorSetSize int,
Expand All @@ -91,7 +91,6 @@ func newStakeManager(
state: state,
key: key,
stakeManagerContractAddr: stakeManagerAddr,
validatorSetContract: validatorSetAddr,
maxValidatorSetSize: maxValidatorSetSize,
polybftBackend: polybftBackend,
blockchain: blockchain,
Expand Down
6 changes: 2 additions & 4 deletions consensus/polybft/stake_manager_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ func FuzzTestStakeManagerPostBlock(f *testing.F) {
hclog.NewNullLogger(),
state,
wallet.NewEcdsaSigner(validators.GetValidator("A").Key()),
validatorSetAddr,
types.StringToAddress("0x0002"),
bcMock,
nil,
Expand All @@ -122,11 +121,10 @@ func FuzzTestStakeManagerPostBlock(f *testing.F) {
require.NoError(t, err)

header := &types.Header{Number: data.BlockID}
require.NoError(t, stakeManager.ProcessLog(header, convertLog(createTestLogForTransferEvent(
require.NoError(t, stakeManager.ProcessLog(header, convertLog(createTestLogForStakeAddedEvent(
t,
validatorSetAddr,
validators.GetValidator(initialSetAliases[data.ValidatorID]).Address(),
types.ZeroAddress,
data.StakeValue,
)), nil))

Expand Down Expand Up @@ -157,7 +155,7 @@ func FuzzTestStakeManagerUpdateValidatorSet(f *testing.F) {
hclog.NewNullLogger(),
state,
wallet.NewEcdsaSigner(validators.GetValidator("A").Key()),
types.StringToAddress("0x0001"), types.StringToAddress("0x0002"),
types.StringToAddress("0x0001"),
bcMock,
nil,
10,
Expand Down
66 changes: 39 additions & 27 deletions consensus/polybft/stake_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestStakeManager_PostBlock(t *testing.T) {
newStake = uint64(100)
firstValidator = uint64(0)
secondValidator = uint64(1)
validatorSetAddr = types.StringToAddress("0x0001")
stakeManagerAddr = types.StringToAddress("0x0001")
)

t.Run("PostBlock - unstake to zero", func(t *testing.T) {
Expand All @@ -53,7 +53,7 @@ func TestStakeManager_PostBlock(t *testing.T) {
hclog.NewNullLogger(),
state,
wallet.NewEcdsaSigner(validators.GetValidator("A").Key()),
validatorSetAddr, types.StringToAddress("0x0002"),
stakeManagerAddr,
bcMock,
nil,
5,
Expand All @@ -63,11 +63,10 @@ func TestStakeManager_PostBlock(t *testing.T) {

header := &types.Header{Number: block}

require.NoError(t, stakeManager.ProcessLog(header, convertLog(createTestLogForTransferEvent(
require.NoError(t, stakeManager.ProcessLog(header, convertLog(createTestLogForStakeRemovedEvent(
t,
validatorSetAddr,
stakeManagerAddr,
validators.GetValidator(initialSetAliases[firstValidator]).Address(),
types.ZeroAddress,
1, // initial validator stake was 1
)), nil))

Expand Down Expand Up @@ -111,7 +110,7 @@ func TestStakeManager_PostBlock(t *testing.T) {
hclog.NewNullLogger(),
state,
wallet.NewEcdsaSigner(validators.GetValidator("A").Key()),
types.StringToAddress("0x0001"), types.StringToAddress("0x0002"),
types.StringToAddress("0x0001"),
bcMock,
nil,
5,
Expand All @@ -120,10 +119,9 @@ func TestStakeManager_PostBlock(t *testing.T) {
require.NoError(t, err)

header := &types.Header{Number: block}
require.NoError(t, stakeManager.ProcessLog(header, convertLog(createTestLogForTransferEvent(
require.NoError(t, stakeManager.ProcessLog(header, convertLog(createTestLogForStakeAddedEvent(
t,
validatorSetAddr,
types.ZeroAddress,
stakeManagerAddr,
validators.GetValidator(initialSetAliases[secondValidator]).Address(),
250,
)), nil))
Expand Down Expand Up @@ -178,7 +176,7 @@ func TestStakeManager_PostBlock(t *testing.T) {
hclog.NewNullLogger(),
state,
wallet.NewEcdsaSigner(validators.GetValidator("A").Key()),
types.StringToAddress("0x0001"), types.StringToAddress("0x0002"),
types.StringToAddress("0x0001"),
bcMock,
nil,
5,
Expand All @@ -189,10 +187,9 @@ func TestStakeManager_PostBlock(t *testing.T) {
header := &types.Header{Number: block}

for i := 0; i < len(allAliases); i++ {
require.NoError(t, stakeManager.ProcessLog(header, convertLog(createTestLogForTransferEvent(
require.NoError(t, stakeManager.ProcessLog(header, convertLog(createTestLogForStakeAddedEvent(
t,
validatorSetAddr,
types.ZeroAddress,
stakeManagerAddr,
validators.GetValidator(allAliases[i]).Address(),
newStake,
)), nil))
Expand Down Expand Up @@ -237,7 +234,7 @@ func TestStakeManager_UpdateValidatorSet(t *testing.T) {
hclog.NewNullLogger(),
state,
wallet.NewEcdsaSigner(validators.GetValidator("A").Key()),
types.StringToAddress("0x0001"), types.StringToAddress("0x0002"),
types.StringToAddress("0x0001"),
bcMock,
nil,
10,
Expand Down Expand Up @@ -409,7 +406,7 @@ func TestStakeManager_UpdateOnInit(t *testing.T) {

var (
allAliases = []string{"A", "B", "C", "D", "E", "F"}
validatorSetAddr = types.StringToAddress("0xf001")
stakeManagerAddr = types.StringToAddress("0xf001")
epochID = uint64(120)
)

Expand Down Expand Up @@ -444,10 +441,9 @@ func TestStakeManager_UpdateOnInit(t *testing.T) {
{
Status: &success,
Logs: []*types.Log{
createTestLogForTransferEvent(
createTestLogForStakeAddedEvent(
t,
validatorSetAddr,
types.ZeroAddress,
stakeManagerAddr,
addresses[len(addresses)-2],
4,
),
Expand All @@ -458,10 +454,9 @@ func TestStakeManager_UpdateOnInit(t *testing.T) {
{
Status: &success,
Logs: []*types.Log{
createTestLogForTransferEvent(
createTestLogForStakeAddedEvent(
t,
validatorSetAddr,
types.ZeroAddress,
stakeManagerAddr,
addresses[len(addresses)-1],
6,
),
Expand All @@ -474,7 +469,7 @@ func TestStakeManager_UpdateOnInit(t *testing.T) {
hclog.NewNullLogger(),
state,
wallet.NewEcdsaSigner(validators.GetValidator("A").Key()),
validatorSetAddr, types.StringToAddress("0x0002"),
stakeManagerAddr,
bcMock,
polyBackendMock,
5,
Expand Down Expand Up @@ -503,15 +498,14 @@ func TestStakeManager_UpdateOnInit(t *testing.T) {
}
}

func createTestLogForTransferEvent(t *testing.T, validatorSet, from, to types.Address, stake uint64) *types.Log {
func createTestLogForStakeAddedEvent(t *testing.T, validatorSet, to types.Address, stake uint64) *types.Log {
t.Helper()

var transferEvent contractsapi.TransferEvent
var transferEvent contractsapi.StakeAddedEvent

topics := make([]types.Hash, 3)
topics := make([]types.Hash, 2)
topics[0] = types.Hash(transferEvent.Sig())
topics[1] = types.BytesToHash(from.Bytes())
topics[2] = types.BytesToHash(to.Bytes())
topics[1] = types.BytesToHash(to.Bytes())
encodedData, err := abi.MustNewType("uint256").Encode(new(big.Int).SetUint64(stake))
require.NoError(t, err)

Expand All @@ -522,6 +516,24 @@ func createTestLogForTransferEvent(t *testing.T, validatorSet, from, to types.Ad
}
}

func createTestLogForStakeRemovedEvent(t *testing.T, validatorSet, to types.Address, unstake uint64) *types.Log {
t.Helper()

var transferEvent contractsapi.StakeRemovedEvent

topics := make([]types.Hash, 2)
topics[0] = types.Hash(transferEvent.Sig())
topics[1] = types.BytesToHash(to.Bytes())
encodedData, err := abi.MustNewType("uint256").Encode(new(big.Int).SetUint64(unstake))
require.NoError(t, err)

return &types.Log{
Address: validatorSet,
Topics: topics,
Data: encodedData,
}
}

var _ txrelayer.TxRelayer = (*dummyStakeTxRelayer)(nil)

type dummyStakeTxRelayer struct {
Expand Down
8 changes: 4 additions & 4 deletions consensus/polybft/state_event_getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestEventDBInsertRetry_GetEvents(t *testing.T) {
receipt := &types.Receipt{
Logs: []*types.Log{
createTestLogForTransferEvent(t, contracts.EpochManagerContract, types.ZeroAddress, types.ZeroAddress, 10),
createTestLogForStakeAddedEvent(t, contracts.EpochManagerContract, types.ZeroAddress, 10),
},
}
receipt.SetStatus(types.ReceiptSuccess)
Expand All @@ -25,15 +25,15 @@ func TestEventDBInsertRetry_GetEvents(t *testing.T) {
}, true)
backend.On("GetReceiptsByHash", mock.Anything).Return([]*types.Receipt{receipt}, nil)

retryManager := &eventsGetter[*contractsapi.TransferEvent]{
retryManager := &eventsGetter[*contractsapi.StakeAddedEvent]{
receiptsGetter: receiptsGetter{
blockchain: backend,
},
isValidLogFn: func(l *types.Log) bool {
return l.Address == contracts.EpochManagerContract
},
parseEventFn: func(h *types.Header, l *ethgo.Log) (*contractsapi.TransferEvent, bool, error) {
var e contractsapi.TransferEvent
parseEventFn: func(h *types.Header, l *ethgo.Log) (*contractsapi.StakeAddedEvent, bool, error) {
var e contractsapi.StakeAddedEvent
doesMatch, err := e.ParseLog(l)

return &e, doesMatch, err
Expand Down

0 comments on commit daa678a

Please sign in to comment.