-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: tcar <[email protected]> Co-authored-by: tcar <[email protected]>
- Loading branch information
1 parent
b60bbcd
commit ff09579
Showing
120 changed files
with
4,853 additions
and
423 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// The Licensed Work is (c) 2022 Sygma | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package btc | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/ChainSafe/sygma-relayer/chains/btc/executor" | ||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
"github.com/sygmaprotocol/sygma-core/relayer/message" | ||
"github.com/sygmaprotocol/sygma-core/relayer/proposal" | ||
) | ||
|
||
type BatchProposalExecutor interface { | ||
Execute(msgs []*message.Message) error | ||
} | ||
type EventListener interface { | ||
ListenToEvents(ctx context.Context, startBlock *big.Int) | ||
} | ||
type BtcChain struct { | ||
id uint8 | ||
|
||
listener EventListener | ||
executor *executor.Executor | ||
mh *executor.BtcMessageHandler | ||
|
||
startBlock *big.Int | ||
logger zerolog.Logger | ||
} | ||
|
||
func NewBtcChain( | ||
listener EventListener, | ||
executor *executor.Executor, | ||
mh *executor.BtcMessageHandler, | ||
id uint8, | ||
) *BtcChain { | ||
return &BtcChain{ | ||
listener: listener, | ||
executor: executor, | ||
mh: mh, | ||
id: id, | ||
|
||
logger: log.With().Uint8("domainID", id).Logger()} | ||
} | ||
|
||
func (c *BtcChain) Write(props []*proposal.Proposal) error { | ||
err := c.executor.Execute(props) | ||
if err != nil { | ||
c.logger.Err(err).Str("messageID", props[0].MessageID).Msgf("error writing proposals %+v on network %d", props, c.DomainID()) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *BtcChain) ReceiveMessage(m *message.Message) (*proposal.Proposal, error) { | ||
return c.mh.HandleMessage(m) | ||
} | ||
|
||
// PollEvents is the goroutine that polls blocks and searches Deposit events in them. | ||
// Events are then sent to eventsChan. | ||
func (c *BtcChain) PollEvents(ctx context.Context) { | ||
c.logger.Info().Str("startBlock", c.startBlock.String()).Msg("Polling Blocks...") | ||
go c.listener.ListenToEvents(ctx, c.startBlock) | ||
} | ||
|
||
func (c *BtcChain) DomainID() uint8 { | ||
return c.id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// The Licensed Work is (c) 2022 Sygma | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package config | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"math/big" | ||
"time" | ||
|
||
"github.com/ChainSafe/sygma-relayer/config/chain" | ||
"github.com/btcsuite/btcd/btcutil" | ||
"github.com/btcsuite/btcd/chaincfg" | ||
"github.com/creasty/defaults" | ||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
type RawResource struct { | ||
Address string | ||
ResourceID string | ||
Tweak string | ||
Script string | ||
} | ||
|
||
type Resource struct { | ||
Address btcutil.Address | ||
ResourceID [32]byte | ||
Tweak string | ||
Script []byte | ||
} | ||
|
||
type RawBtcConfig struct { | ||
chain.GeneralChainConfig `mapstructure:",squash"` | ||
Resources []RawResource `mapstrcture:"resources"` | ||
StartBlock int64 `mapstructure:"startBlock"` | ||
Username string `mapstructure:"username"` | ||
Password string `mapstructure:"password"` | ||
BlockInterval int64 `mapstructure:"blockInterval" default:"5"` | ||
BlockRetryInterval uint64 `mapstructure:"blockRetryInterval" default:"5"` | ||
BlockConfirmations int64 `mapstructure:"blockConfirmations" default:"10"` | ||
Network string `mapstructure:"network" default:"mainnet"` | ||
MempoolUrl string `mapstructure:"mempoolUrl"` | ||
} | ||
|
||
func (c *RawBtcConfig) Validate() error { | ||
if err := c.GeneralChainConfig.Validate(); err != nil { | ||
return err | ||
} | ||
|
||
if c.BlockConfirmations != 0 && c.BlockConfirmations < 1 { | ||
return fmt.Errorf("blockConfirmations has to be >=1") | ||
} | ||
|
||
if c.Username == "" { | ||
return fmt.Errorf("required field chain.Username empty for chain %v", *c.Id) | ||
} | ||
|
||
if c.Password == "" { | ||
return fmt.Errorf("required field chain.Password empty for chain %v", *c.Id) | ||
} | ||
return nil | ||
} | ||
|
||
type BtcConfig struct { | ||
GeneralChainConfig chain.GeneralChainConfig | ||
Resources []Resource | ||
Username string | ||
Password string | ||
StartBlock *big.Int | ||
BlockInterval *big.Int | ||
BlockRetryInterval time.Duration | ||
BlockConfirmations *big.Int | ||
Tweak string | ||
Script []byte | ||
MempoolUrl string | ||
Network chaincfg.Params | ||
} | ||
|
||
// NewBtcConfig decodes and validates an instance of an BtcConfig from | ||
// raw chain config | ||
func NewBtcConfig(chainConfig map[string]interface{}) (*BtcConfig, error) { | ||
var c RawBtcConfig | ||
err := mapstructure.Decode(chainConfig, &c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = defaults.Set(&c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = c.Validate() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
networkParams, err := networkParams(c.Network) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resources := make([]Resource, len(c.Resources)) | ||
for i, r := range c.Resources { | ||
scriptBytes, err := hex.DecodeString(r.Script) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
address, err := btcutil.DecodeAddress(r.Address, &networkParams) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resourceBytes, err := hex.DecodeString(r.ResourceID[2:]) | ||
if err != nil { | ||
panic(err) | ||
} | ||
var resource32Bytes [32]byte | ||
copy(resource32Bytes[:], resourceBytes) | ||
resources[i] = Resource{ | ||
Address: address, | ||
ResourceID: resource32Bytes, | ||
Script: scriptBytes, | ||
Tweak: r.Tweak, | ||
} | ||
} | ||
|
||
c.GeneralChainConfig.ParseFlags() | ||
config := &BtcConfig{ | ||
GeneralChainConfig: c.GeneralChainConfig, | ||
StartBlock: big.NewInt(c.StartBlock), | ||
BlockConfirmations: big.NewInt(c.BlockConfirmations), | ||
BlockInterval: big.NewInt(c.BlockInterval), | ||
BlockRetryInterval: time.Duration(c.BlockRetryInterval) * time.Second, | ||
Username: c.Username, | ||
Password: c.Password, | ||
Network: networkParams, | ||
MempoolUrl: c.MempoolUrl, | ||
Resources: resources, | ||
} | ||
return config, nil | ||
} | ||
|
||
func networkParams(network string) (chaincfg.Params, error) { | ||
switch network { | ||
case "mainnet": | ||
return chaincfg.MainNetParams, nil | ||
case "testnet": | ||
return chaincfg.TestNet3Params, nil | ||
case "regtest": | ||
return chaincfg.RegressionNetParams, nil | ||
case "signet": | ||
return chaincfg.SigNetParams, nil | ||
default: | ||
return chaincfg.Params{}, fmt.Errorf("unknown network %s", network) | ||
} | ||
} |
Oops, something went wrong.