Skip to content
Open
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
37 changes: 7 additions & 30 deletions chains/bsc/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ import (
"fmt"
"math/big"
"strconv"
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/mapprotocol/compass/internal/constant"
"github.com/mapprotocol/compass/internal/mapprotocol"
"github.com/mapprotocol/compass/pkg/abi"
"github.com/mapprotocol/compass/pkg/contract"
"github.com/mapprotocol/compass/pkg/msg"
"github.com/pkg/errors"

Expand All @@ -26,10 +22,13 @@ import (
)

type Chain struct {
proofConn *chain.ProofConnector
}

func New() *Chain {
return &Chain{}
return &Chain{
proofConn: chain.NewProofConnector(mapprotocol.Default(), chain.DefaultProofDialer, nil),
}
}

func (c *Chain) New(chainCfg *core.ChainConfig, logger log15.Logger, sysErr chan<- error, role mapprotocol.Role) (core.Chain, error) {
Expand All @@ -47,7 +46,7 @@ func (c *Chain) syncHeaderToMap(m *chain.Maintainer, latestBlock *big.Int) error
return nil
}
// synced height check
syncedHeight, err := mapprotocol.Get2MapHeight(m.Cfg.Id)
syncedHeight, err := mapprotocol.Default().Chain2MapHeight(m.Cfg.Id)
if err != nil {
m.Log.Error("Get current synced Height failed", "err", err)
return err
Expand Down Expand Up @@ -112,29 +111,7 @@ func (c *Chain) assembleProof(m *chain.Messenger, log *types.Log, proofType int6
}

func (c *Chain) Connect(id, endpoint, mcs, lightNode, oracleNode string) (*ethclient.Client, error) {
conn := connection.NewConnection(endpoint, true, nil, nil, big.NewInt(chain.DefaultGasLimit),
big.NewInt(chain.DefaultGasPrice), chain.DefaultGasMultiplier)
err := conn.Connect()
if err != nil {
return nil, err
}

fn := sync.OnceFunc(func() {
idInt, _ := strconv.ParseUint(id, 10, 64)
oracleAbi, _ := abi.New(mapprotocol.OracleAbiJson)
call := contract.New(conn, []common.Address{common.HexToAddress(mcs)}, oracleAbi)
mapprotocol.ContractMapping[msg.ChainId(idInt)] = call

oAbi, _ := abi.New(mapprotocol.SignerJson)
oracleCall := contract.New(conn, []common.Address{common.HexToAddress(oracleNode)}, oAbi)
mapprotocol.SingMapping[msg.ChainId(idInt)] = oracleCall

fn := mapprotocol.Map2EthHeight(constant.ZeroAddress.Hex(), common.HexToAddress(lightNode), conn.Client())
mapprotocol.Map2OtherHeight[msg.ChainId(idInt)] = fn
})
fn()

return conn.Client(), nil
return c.proofConn.Connect(id, endpoint, mcs, lightNode, oracleNode)
}

func (c *Chain) Proof(client *ethclient.Client, log *types.Log, endpoint string, proofType int64, selfId,
Expand Down Expand Up @@ -187,7 +164,7 @@ func (c *Chain) Proof(client *ethclient.Client, log *types.Log, endpoint string,
}

func (c *Chain) Maintainer(client *ethclient.Client, selfId, toChainId uint64, srcEndpoint string) ([]byte, error) {
syncedHeight, err := mapprotocol.Get2MapHeight(msg.ChainId(selfId))
syncedHeight, err := mapprotocol.Default().Chain2MapHeight(msg.ChainId(selfId))
if err != nil {
return nil, errors.Wrap(err, "unable to get synced height")
}
Expand Down
47 changes: 15 additions & 32 deletions chains/eth2/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package eth2
import (
"fmt"
"math/big"
"strconv"
"sync"

"github.com/ChainSafe/log15"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -13,7 +11,6 @@ import (
"github.com/mapprotocol/compass/connections/eth2"
"github.com/mapprotocol/compass/core"
"github.com/mapprotocol/compass/internal/chain"
"github.com/mapprotocol/compass/internal/constant"
ieth "github.com/mapprotocol/compass/internal/eth2"
"github.com/mapprotocol/compass/internal/mapprotocol"
"github.com/mapprotocol/compass/internal/tx"
Expand All @@ -33,10 +30,17 @@ type Chain struct {
writer *chain.Writer // The writer of the chain
listen core.Listener // The listener of this chain
stop chan<- int

proofConn *chain.ProofConnector
}

func New() *Chain {
return &Chain{}
return &Chain{
proofConn: chain.NewProofConnector(mapprotocol.Default(), func(endpoint string) core.Connection {
return eth2.NewConnection(endpoint, "", true, nil, nil, big.NewInt(chain.DefaultGasLimit),
big.NewInt(chain.DefaultGasPrice), chain.DefaultGasMultiplier)
}, nil),
}
}

func (c *Chain) New(chainCfg *core.ChainConfig, logger log15.Logger, sysErr chan<- error, role mapprotocol.Role) (core.Chain, error) {
Expand Down Expand Up @@ -71,6 +75,7 @@ func (c *Chain) New(chainCfg *core.ChainConfig, logger log15.Logger, sysErr chan

// simplified a little bit
var listen core.Listener
registry := mapprotocol.Default()
cs := chain.NewCommonSync(conn, cfg, logger, stop, sysErr, bs,
chain.OptOfOracleHandler(chain.DefaultOracleHandler))
cs.RegisterState(cfg.Name, string(role))
Expand All @@ -82,22 +87,22 @@ func (c *Chain) New(chainCfg *core.ChainConfig, logger log15.Logger, sysErr chan
return nil, errors.Wrap(err, "eth2 get init headerHeight failed")
}
logger.Info("map2eth2 Current situation", "height", height, "lightNode", cfg.LightNode)
mapprotocol.SyncOtherMap[cfg.Id] = height
mapprotocol.Map2OtherHeight[cfg.Id] = fn
registry.SetMap2OtherInitHeight(cfg.Id, height)
registry.SetMap2OtherHeight(cfg.Id, fn)
listen = NewMaintainer(cs, conn.Eth2Client())
case mapprotocol.RoleOfMessenger:
oracleAbi, _ := abi.New(mapprotocol.OracleAbiJson)
call := contract.New(conn, cfg.McsContract, oracleAbi)
mapprotocol.ContractMapping[cfg.Id] = call
registry.SetMosCall(cfg.Id, call)
listen = NewMessenger(cs)
case mapprotocol.RoleOfOracle:
oAbi, _ := abi.New(mapprotocol.SignerJson)
oracleCall := contract.New(conn, []common.Address{cfg.OracleNode}, oAbi)
mapprotocol.SingMapping[cfg.Id] = oracleCall
registry.SetSignerCall(cfg.Id, oracleCall)

otherAbi, _ := abi.New(mapprotocol.OtherAbi)
call := contract.New(conn, []common.Address{cfg.LightNode}, otherAbi)
mapprotocol.LightNodeMapping[cfg.Id] = call
registry.SetLightNodeCall(cfg.Id, call)
listen = chain.NewOracle(cs)
}
wri := chain.NewWriter(conn, cfg, logger, stop, sysErr)
Expand Down Expand Up @@ -153,29 +158,7 @@ func (c *Chain) Conn() core.Connection {
}

func (c *Chain) Connect(id, endpoint, mcs, lightNode, oracleNode string) (*ethclient.Client, error) {
conn := eth2.NewConnection(endpoint, "", true, nil, nil, big.NewInt(chain.DefaultGasLimit),
big.NewInt(chain.DefaultGasPrice), chain.DefaultGasMultiplier)
err := conn.Connect()
if err != nil {
return nil, err
}

fn := sync.OnceFunc(func() {
idInt, _ := strconv.ParseUint(id, 10, 64)
oracleAbi, _ := abi.New(mapprotocol.OracleAbiJson)
call := contract.New(conn, []common.Address{common.HexToAddress(mcs)}, oracleAbi)
mapprotocol.ContractMapping[msg.ChainId(idInt)] = call

oAbi, _ := abi.New(mapprotocol.SignerJson)
oracleCall := contract.New(conn, []common.Address{common.HexToAddress(oracleNode)}, oAbi)
mapprotocol.SingMapping[msg.ChainId(idInt)] = oracleCall

fn := mapprotocol.Map2EthHeight(constant.ZeroAddress.Hex(), common.HexToAddress(lightNode), conn.Client())
mapprotocol.Map2OtherHeight[msg.ChainId(idInt)] = fn
})
fn()

return conn.Client(), nil
return c.proofConn.Connect(id, endpoint, mcs, lightNode, oracleNode)
}

func (c *Chain) Proof(client *ethclient.Client, log *types.Log, endpoint string, proofType int64, selfId,
Expand Down
4 changes: 2 additions & 2 deletions chains/eth2/maintainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (m *Maintainer) sync() error {
continue
}

startNumber, endNumber, err := mapprotocol.GetEth22MapNumber(m.Cfg.Id)
startNumber, endNumber, err := mapprotocol.Default().Eth2MapNumber(m.Cfg.Id)
if err != nil {
m.Log.Error("Get startNumber failed", "err", err)
time.Sleep(constant.BlockRetryInterval)
Expand Down Expand Up @@ -153,7 +153,7 @@ func (m *Maintainer) sync() error {
}

func (m *Maintainer) updateSyncHeight() error {
syncedHeight, err := mapprotocol.Get2MapHeight(m.Cfg.Id)
syncedHeight, err := mapprotocol.Default().Chain2MapHeight(m.Cfg.Id)
if err != nil {
m.Log.Error("Get synced Height failed", "err", err)
return err
Expand Down
63 changes: 25 additions & 38 deletions chains/ethereum/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"math/big"
"strconv"
"strings"
"sync"

"github.com/ChainSafe/log15"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -20,25 +19,40 @@ import (
"github.com/mapprotocol/compass/internal/mapo"
"github.com/mapprotocol/compass/internal/mapprotocol"
"github.com/mapprotocol/compass/internal/tx"
"github.com/mapprotocol/compass/pkg/abi"
"github.com/mapprotocol/compass/pkg/contract"
"github.com/mapprotocol/compass/pkg/ethclient"
"github.com/mapprotocol/compass/pkg/msg"
)

type Chain struct {
proofConn *chain.ProofConnector
}

func New() *Chain {
return &Chain{}
c := &Chain{}
c.proofConn = chain.NewProofConnector(mapprotocol.Default(), chain.DefaultProofDialer, c.registerHeight)
return c
}

// registerHeight publishes the height providers a proof request needs. The MAP
// chain is the one that answers "how far have you followed chain X", every other
// chain answers "how far have I followed MAP".
func (c *Chain) registerHeight(registry *mapprotocol.Registry, id msg.ChainId, conn core.Connection, lightNode string) error {
if uint64(id) == constant.MapChainId {
registry.SetMapConn(conn.Client())
registry.SetLightManager(mapprotocol.NewLightManager(conn.Client(), common.HexToAddress(lightNode)))
return nil
}
registry.SetMap2OtherHeight(id, mapprotocol.Map2EthHeight(constant.ZeroAddress.Hex(),
common.HexToAddress(lightNode), conn.Client()))
return nil
}

func (c *Chain) New(chainCfg *core.ChainConfig, logger log15.Logger, sysErr chan<- error,
role mapprotocol.Role) (core.Chain, error) {
opts := make([]chain.SyncOpt, 0)

opts = append(opts, chain.OptOfInitHeight(mapprotocol.HeaderOneCount))
if strconv.FormatUint(uint64(chainCfg.Id), 10) == mapprotocol.MapId {
if strconv.FormatUint(uint64(chainCfg.Id), 10) == mapprotocol.Default().MapID() {
opts = append(opts, chain.OptOfSync2Map(c.mapToOther))
opts = append(opts, chain.OptOfInitHeight(mapprotocol.EpochOfMap))
} else {
Expand Down Expand Up @@ -77,14 +91,14 @@ func (c *Chain) mapToOther(m *chain.Maintainer, latestBlock *big.Int) error {
msgpayload := []interface{}{input}
waitCount := len(m.Cfg.SyncChainIDList)
for _, cid := range m.Cfg.SyncChainIDList {
if v, ok := mapprotocol.SyncOtherMap[cid]; ok && latestBlock.Cmp(v) <= 0 {
if v, ok := mapprotocol.Default().Map2OtherInitHeight(cid); ok && latestBlock.Cmp(v) <= 0 {
waitCount--
m.Log.Info("map to other current less than synchronized headerHeight", "toChainId", cid, "synced height", v,
"current height", latestBlock)
continue
}
// Query the latest height for comparison
if fn, ok := mapprotocol.Map2OtherHeight[cid]; ok {
if fn, ok := mapprotocol.Default().Map2OtherHeight(cid); ok {
height, err := fn()
if err != nil {
return fmt.Errorf("get headerHeight failed, cid(%d),err is %v", cid, err)
Expand All @@ -96,7 +110,7 @@ func (c *Chain) mapToOther(m *chain.Maintainer, latestBlock *big.Int) error {
continue
}
}
if name, ok := mapprotocol.OnlineChaId[cid]; ok && strings.ToLower(name) == "near" {
if name, ok := mapprotocol.Default().ChainName(cid); ok && strings.ToLower(name) == "near" {
param := map[string]interface{}{
"header": mapprotocol.ConvertNearNeedHeader(header),
"agg_pk": map[string]interface{}{
Expand Down Expand Up @@ -127,7 +141,7 @@ func (c *Chain) mapToOther(m *chain.Maintainer, latestBlock *big.Int) error {
}

func (c *Chain) headerToMap(m *chain.Maintainer, latestBlock *big.Int) error {
syncedHeight, err := mapprotocol.Get2MapHeight(m.Cfg.Id)
syncedHeight, err := mapprotocol.Default().Chain2MapHeight(m.Cfg.Id)
if err != nil {
m.Log.Error("Get synced Height failed", "err", err)
return err
Expand Down Expand Up @@ -219,34 +233,7 @@ func (c *Chain) rlpEthereumHeaders(source, destination msg.ChainId, headers []ty
}

func (c *Chain) Connect(id, endpoint, mcs, lightNode, oracleNode string) (*ethclient.Client, error) {
conn := connection.NewConnection(endpoint, true, nil, nil, big.NewInt(chain.DefaultGasLimit),
big.NewInt(chain.DefaultGasPrice), chain.DefaultGasMultiplier)
err := conn.Connect()
if err != nil {
return nil, err
}

fn := sync.OnceFunc(func() {
idInt, _ := strconv.ParseUint(id, 10, 64)
oracleAbi, _ := abi.New(mapprotocol.OracleAbiJson)
call := contract.New(conn, []common.Address{common.HexToAddress(mcs)}, oracleAbi)
mapprotocol.ContractMapping[msg.ChainId(idInt)] = call

oAbi, _ := abi.New(mapprotocol.SignerJson)
oracleCall := contract.New(conn, []common.Address{common.HexToAddress(oracleNode)}, oAbi)
mapprotocol.SingMapping[msg.ChainId(idInt)] = oracleCall

if idInt == constant.MapChainId {
mapprotocol.GlobalMapConn = conn.Client()
mapprotocol.InitOtherChain2MapHeight(common.HexToAddress(lightNode))
} else {
fn := mapprotocol.Map2EthHeight(constant.ZeroAddress.Hex(), common.HexToAddress(lightNode), conn.Client())
mapprotocol.Map2OtherHeight[msg.ChainId(idInt)] = fn
}
})
fn()

return conn.Client(), nil
return c.proofConn.Connect(id, endpoint, mcs, lightNode, oracleNode)
}

func (c *Chain) Proof(client *ethclient.Client, log *types.Log, endpoint string, proofType int64, selfId,
Expand Down Expand Up @@ -296,7 +283,7 @@ func (c *Chain) Proof(client *ethclient.Client, log *types.Log, endpoint string,
func (c *Chain) Maintainer(client *ethclient.Client, selfId, toChainId uint64, srcEndpoint string) ([]byte, error) {
ret := make([]byte, 0)
if selfId == constant.MapChainId {
syncedHeight, err := mapprotocol.Map2OtherHeight[msg.ChainId(toChainId)]()
syncedHeight, err := mapprotocol.Default().Map2OtherSyncedHeight(msg.ChainId(toChainId))
if err != nil {
return nil, err
}
Expand Down
Loading