Skip to content

Commit

Permalink
Separate chain interface for the coordinator package (#3650)
Browse files Browse the repository at this point in the history
In this PR we separate `coordinator.Chain` interface definition from the
`tbtc.Chain` interface. This change makes the interfaces cleaner as each
package requires only the functions that it is actually using.
  • Loading branch information
pdyraga authored Jun 27, 2023
2 parents 60994bf + da06660 commit 99f994a
Show file tree
Hide file tree
Showing 11 changed files with 363 additions and 821 deletions.
74 changes: 8 additions & 66 deletions pkg/chain/ethereum/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"sort"
"time"

"github.com/keep-network/keep-common/pkg/chain/ethereum/ethutil"
"github.com/keep-network/keep-core/pkg/bitcoin"
"github.com/keep-network/keep-core/pkg/coordinator"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/keep-network/keep-common/pkg/chain/ethereum/ethutil"
"github.com/keep-network/keep-core/pkg/bitcoin"

"github.com/keep-network/keep-common/pkg/chain/ethereum"
"github.com/keep-network/keep-core/pkg/chain"
Expand Down Expand Up @@ -1125,8 +1127,8 @@ func (tc *TbtcChain) GetDepositRequest(
}

func (tc *TbtcChain) PastNewWalletRegisteredEvents(
filter *tbtc.NewWalletRegisteredEventFilter,
) ([]*tbtc.NewWalletRegisteredEvent, error) {
filter *coordinator.NewWalletRegisteredEventFilter,
) ([]*coordinator.NewWalletRegisteredEvent, error) {
var startBlock uint64
var endBlock *uint64
var ecdsaWalletID [][32]byte
Expand All @@ -1149,9 +1151,9 @@ func (tc *TbtcChain) PastNewWalletRegisteredEvents(
return nil, err
}

convertedEvents := make([]*tbtc.NewWalletRegisteredEvent, 0)
convertedEvents := make([]*coordinator.NewWalletRegisteredEvent, 0)
for _, event := range events {
convertedEvent := &tbtc.NewWalletRegisteredEvent{
convertedEvent := &coordinator.NewWalletRegisteredEvent{
EcdsaWalletID: event.EcdsaWalletID,
WalletPublicKeyHash: event.WalletPubKeyHash,
BlockNumber: event.Raw.BlockNumber,
Expand Down Expand Up @@ -1453,66 +1455,6 @@ func (tc *TbtcChain) OnDepositSweepProposalSubmitted(
OnEvent(onEvent)
}

func (tc *TbtcChain) PastDepositSweepProposalSubmittedEvents(
filter *tbtc.DepositSweepProposalSubmittedEventFilter,
) ([]*tbtc.DepositSweepProposalSubmittedEvent, error) {
var startBlock uint64
var endBlock *uint64
var coordinator []common.Address
var walletPublicKeyHash [20]byte

if filter != nil {
startBlock = filter.StartBlock
endBlock = filter.EndBlock

for _, ps := range filter.Coordinator {
coordinator = append(
coordinator,
common.HexToAddress(ps.String()),
)
}

walletPublicKeyHash = filter.WalletPublicKeyHash
}

events, err := tc.walletCoordinator.PastDepositSweepProposalSubmittedEvents(
startBlock,
endBlock,
coordinator,
)
if err != nil {
return nil, err
}

convertedEvents := make([]*tbtc.DepositSweepProposalSubmittedEvent, 0)
for _, event := range events {
// If the wallet PKH filter is set, omit all events that target
// different wallets.
if walletPublicKeyHash != [20]byte{} {
if event.Proposal.WalletPubKeyHash != walletPublicKeyHash {
continue
}
}

convertedEvent := &tbtc.DepositSweepProposalSubmittedEvent{
Proposal: convertDepositSweepProposalFromAbiType(event.Proposal),
Coordinator: chain.Address(event.Coordinator.Hex()),
BlockNumber: event.Raw.BlockNumber,
}

convertedEvents = append(convertedEvents, convertedEvent)
}

sort.SliceStable(
convertedEvents,
func(i, j int) bool {
return convertedEvents[i].BlockNumber < convertedEvents[j].BlockNumber
},
)

return convertedEvents, err
}

func convertDepositSweepProposalFromAbiType(
proposal tbtcabi.WalletCoordinatorDepositSweepProposal,
) *tbtc.DepositSweepProposal {
Expand Down
93 changes: 89 additions & 4 deletions pkg/coordinator/chain.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,69 @@
package coordinator

import (
"math/big"

"github.com/keep-network/keep-core/pkg/bitcoin"
"github.com/keep-network/keep-core/pkg/tbtc"
"math/big"
)

// NewWalletRegisteredEvent represents a new wallet registered event.
type NewWalletRegisteredEvent struct {
EcdsaWalletID [32]byte
WalletPublicKeyHash [20]byte
BlockNumber uint64
}

// NewWalletRegisteredEventFilter is a component allowing to filter NewWalletRegisteredEvent.
type NewWalletRegisteredEventFilter struct {
StartBlock uint64
EndBlock *uint64
EcdsaWalletID [][32]byte
WalletPublicKeyHash [][20]byte
}

// Chain represents the interface that the coordinator module expects to interact
// with the anchoring blockchain on.
type Chain interface {
// TODO: Change to something more specific once https://github.com/keep-network/keep-core/issues/3632
// is handled.
tbtc.Chain
// GetDepositRequest gets the on-chain deposit request for the given
// funding transaction hash and output index. Returns an error if the
// deposit was not found.
GetDepositRequest(
fundingTxHash bitcoin.Hash,
fundingOutputIndex uint32,
) (*tbtc.DepositChainRequest, error)

// PastNewWalletRegisteredEvents fetches past new wallet registered events
// according to the provided filter or unfiltered if the filter is nil. Returned
// events are sorted by the block number in the ascending order, i.e. the
// latest event is at the end of the slice.
PastNewWalletRegisteredEvents(
filter *NewWalletRegisteredEventFilter,
) ([]*NewWalletRegisteredEvent, error)

// BuildDepositKey calculates a deposit key for the given funding transaction
// which is a unique identifier for a deposit on-chain.
BuildDepositKey(fundingTxHash bitcoin.Hash, fundingOutputIndex uint32) *big.Int

// GetDepositParameters gets the current value of parameters relevant
// for the depositing process.
GetDepositParameters() (
dustThreshold uint64,
treasuryFeeDivisor uint64,
txMaxFee uint64,
revealAheadPeriod uint32,
err error,
)

// SubmitDepositSweepProposalWithReimbursement submits a deposit sweep
// proposal to the chain. It reimburses the gas cost to the caller.
SubmitDepositSweepProposalWithReimbursement(
proposal *tbtc.DepositSweepProposal,
) error

// GetDepositSweepMaxSize gets the maximum number of deposits that can
// be part of a deposit sweep proposal.
GetDepositSweepMaxSize() (uint16, error)

// PastRedemptionRequestedEvents fetches past redemption requested events according
// to the provided filter or unfiltered if the filter is nil. Returned
Expand Down Expand Up @@ -56,4 +108,37 @@ type Chain interface {
// the redemption request creation before a request becomes eligible for
// a processing.
GetRedemptionRequestMinAge() (uint32, error)

// PastDepositRevealedEvents fetches past deposit reveal events according
// to the provided filter or unfiltered if the filter is nil. Returned
// events are sorted by the block number in the ascending order, i.e. the
// latest event is at the end of the slice.
PastDepositRevealedEvents(
filter *tbtc.DepositRevealedEventFilter,
) ([]*tbtc.DepositRevealedEvent, error)

// GetPendingRedemptionRequest gets the on-chain pending redemption request
// for the given wallet public key hash and redeemer output script.
// Returns an error if the request was not found.
GetPendingRedemptionRequest(
walletPublicKeyHash [20]byte,
redeemerOutputScript bitcoin.Script,
) (*tbtc.RedemptionRequest, error)

// ValidateDepositSweepProposal validates the given deposit sweep proposal
// against the chain. It requires some additional data about the deposits
// that must be fetched externally. Returns an error if the proposal is
// not valid or nil otherwise.
ValidateDepositSweepProposal(
proposal *tbtc.DepositSweepProposal,
depositsExtraInfo []struct {
*tbtc.Deposit
FundingTx *bitcoin.Transaction
},
) error

// ValidateRedemptionProposal validates the given redemption proposal
// against the chain. Returns an error if the proposal is not valid or
// nil otherwise.
ValidateRedemptionProposal(proposal *tbtc.RedemptionProposal) error
}
Loading

0 comments on commit 99f994a

Please sign in to comment.