Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(contracts): majority reached #315

Merged
merged 2 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
599 changes: 598 additions & 1 deletion app/clients/evm/contracts/router/diamond-router.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions app/domain/service/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type Contracts interface {
GetClient() client.Core
// IsMember returns true/false depending on whether the provided address is a Bridge member or not
IsMember(address string) bool
// HasValidSignaturesLength returns whether the signatures are enough for submission
HasValidSignaturesLength(*big.Int) (bool, error)
// ParseBurnLog parses a general typed log to a RouterBurn event
ParseBurnLog(log types.Log) (*abi.RouterBurn, error)
// ParseLockLog parses a general typed log to a RouterLock event
Expand Down
9 changes: 5 additions & 4 deletions app/process/handler/message/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/limechain/hedera-eth-bridge-validator/app/model/message"
"github.com/limechain/hedera-eth-bridge-validator/config"
log "github.com/sirupsen/logrus"
"math/big"
)

type Handler struct {
Expand Down Expand Up @@ -85,7 +86,7 @@ func (cmh Handler) handleSignatureMessage(tsm message.Message) {

majorityReached, err := cmh.checkMajority(tsm.TransferID, int64(tsm.TargetChainId))
if err != nil {
cmh.logger.Errorf("[%s] - Could not determine whether majority was reached", tsm.TransferID)
cmh.logger.Errorf("[%s] - Could not determine whether majority was reached. Error: [%s]", tsm.TransferID, err)
return
}

Expand All @@ -105,9 +106,9 @@ func (cmh *Handler) checkMajority(transferID string, targetChainId int64) (major
}

membersCount := len(cmh.contracts[targetChainId].GetMembers())
requiredSigCount := membersCount/2 + 1
bnSignaturesLength := big.NewInt(int64(len(signatureMessages)))
cmh.logger.Infof("[%s] - Collected [%d/%d] Signatures", transferID, len(signatureMessages), membersCount)

return len(signatureMessages) >= requiredSigCount,
nil
return cmh.contracts[targetChainId].
HasValidSignaturesLength(bnSignaturesLength)
}
10 changes: 10 additions & 0 deletions app/process/handler/message/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/limechain/hedera-eth-bridge-validator/test/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"math/big"
"testing"
)

Expand Down Expand Up @@ -97,8 +98,11 @@ func Test_HandleSignatureMessage_MajorityReached(t *testing.T) {
mocks.MMessageService.On("ProcessSignature", tsm).Return(nil)
mocks.MMessageRepository.On("Get", tsm.TransferID).Return([]entity.Message{{}, {}, {}}, nil)
mocks.MBridgeContractService.On("GetMembers").Return([]string{"", "", ""})
mocks.MBridgeContractService.On("HasValidSignaturesLength", big.NewInt(3)).Return(true, nil)
mocks.MTransferRepository.On("UpdateStatusCompleted", tsm.TransferID).Return(nil)
h.handleSignatureMessage(tsm)
mocks.MBridgeContractService.AssertCalled(t, "HasValidSignaturesLength", big.NewInt(3))
mocks.MTransferRepository.AssertCalled(t, "UpdateStatusCompleted", tsm.TransferID)
}

func Test_Handle(t *testing.T) {
Expand All @@ -107,8 +111,11 @@ func Test_Handle(t *testing.T) {
mocks.MMessageService.On("ProcessSignature", tsm).Return(nil)
mocks.MMessageRepository.On("Get", tsm.TransferID).Return([]entity.Message{{}, {}, {}}, nil)
mocks.MBridgeContractService.On("GetMembers").Return([]string{"", "", ""})
mocks.MBridgeContractService.On("HasValidSignaturesLength", big.NewInt(3)).Return(true, nil)
mocks.MTransferRepository.On("UpdateStatusCompleted", tsm.TransferID).Return(nil)
h.Handle(&tsm)
mocks.MBridgeContractService.AssertCalled(t, "HasValidSignaturesLength", big.NewInt(3))
mocks.MTransferRepository.AssertCalled(t, "UpdateStatusCompleted", tsm.TransferID)
}

func Test_HandleSignatureMessage_UpdateStatusCompleted_Fails(t *testing.T) {
Expand All @@ -117,8 +124,11 @@ func Test_HandleSignatureMessage_UpdateStatusCompleted_Fails(t *testing.T) {
mocks.MMessageService.On("ProcessSignature", tsm).Return(nil)
mocks.MMessageRepository.On("Get", tsm.TransferID).Return([]entity.Message{{}, {}, {}}, nil)
mocks.MBridgeContractService.On("GetMembers").Return([]string{"", "", ""})
mocks.MBridgeContractService.On("HasValidSignaturesLength", big.NewInt(3)).Return(true, nil)
mocks.MTransferRepository.On("UpdateStatusCompleted", tsm.TransferID).Return(errors.New("some-error"))
h.handleSignatureMessage(tsm)
mocks.MBridgeContractService.AssertCalled(t, "HasValidSignaturesLength", big.NewInt(3))
mocks.MTransferRepository.AssertNotCalled(t, "UpdateStatusCompleted")
}

func Test_HandleSignatureMessage_CheckMajority_Fails(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions app/services/contracts/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func (bsc *Service) IsMember(address string) bool {
return false
}

// HasValidSignaturesLength returns whether the signatures are enough for submission
func (bsc *Service) HasValidSignaturesLength(signaturesLength *big.Int) (bool, error) {
return bsc.contract.HasValidSignaturesLength(nil, signaturesLength)
}

// ParseBurnLog parses a general typed log to a RouterBurn event
func (bsc *Service) ParseBurnLog(log types.Log) (*router.RouterBurn, error) {
return bsc.contract.ParseBurn(log)
Expand Down
10 changes: 8 additions & 2 deletions app/services/transfers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,14 @@ func (ts *Service) TransferData(txId string) (service.TransferData, error) {
signatures = append(signatures, m.Signature)
}

requiredSigCount := len(ts.contractServices[t.TargetChainID].GetMembers())/2 + 1
reachedMajority := len(t.Messages) >= requiredSigCount
bnSignaturesLength := big.NewInt(int64(len(t.Messages)))
reachedMajority, err := ts.contractServices[t.TargetChainID].
HasValidSignaturesLength(bnSignaturesLength)

if err != nil {
ts.logger.Errorf("[%s] - Failed to check has valid signatures length. Error [%s]", t.TransactionID, err)
return service.TransferData{}, err
}

return service.TransferData{
Recipient: t.Receiver,
Expand Down
8 changes: 8 additions & 0 deletions test/mocks/bridge-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ func (m *MockBridgeContract) IsMember(address string) bool {
panic("implement me")
}

func (m *MockBridgeContract) HasValidSignaturesLength(signaturesLength *big.Int) (bool, error) {
args := m.Called(signaturesLength)
if args[0] == nil {
return false, args.Get(1).(error)
}
return args.Get(0).(bool), nil
}

func (m *MockBridgeContract) WatchBurnEventLogs(opts *bind.WatchOpts, sink chan<- *router.RouterBurn) (event.Subscription, error) {
args := m.Called(opts, sink)
if args[0] == nil && args[1] == nil {
Expand Down