Skip to content

Commit

Permalink
Improve the frequency of how often we ping the evm node (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vizualni authored Jul 22, 2022
1 parent 7d5a7cc commit d50b197
Show file tree
Hide file tree
Showing 30 changed files with 505 additions and 115 deletions.
2 changes: 2 additions & 0 deletions chain/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const (

ErrNotFound = whoops.String("not found")

ErrNotConnectedToRightChain = whoops.String("not connected to the right chain")

EnrichedChainReferenceID whoops.Field[string] = "chainReferenceID"
EnrichedID whoops.Field[uint64] = "id"
EnrichedItemType whoops.Field[string] = "type"
Expand Down
4 changes: 4 additions & 0 deletions chain/evm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ func (c Client) TransactionByHash(ctx context.Context, txHash common.Hash) (*eth
return c.conn.TransactionByHash(ctx, txHash)
}

func (c Client) BlockByHash(ctx context.Context, blockHash common.Hash) (*ethtypes.Block, error) {
return c.conn.BlockByHash(ctx, blockHash)
}

//go:generate mockery --name=ethClientToFilterLogs --inpackage --testonly
type ethClientToFilterLogs interface {
FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]etherumtypes.Log, error)
Expand Down
6 changes: 4 additions & 2 deletions chain/evm/compass.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type compass struct {
paloma PalomaClienter
evm evmClienter

startingBlockHeight int64

chainID *big.Int
}

Expand Down Expand Up @@ -201,7 +203,7 @@ func (t compass) findLastValsetMessageID(ctx context.Context) (uint64, error) {
latestMessageID := big.NewInt(0)

var retErr error
_, err := t.evm.FilterLogs(ctx, filter, nil, func(logs []etherumtypes.Log) bool {
_, err := t.evm.FilterLogs(ctx, filter, big.NewInt(t.startingBlockHeight), func(logs []etherumtypes.Log) bool {
for _, log := range logs {
if log.BlockNumber > highestBlock {
highestBlock = log.BlockNumber
Expand Down Expand Up @@ -251,7 +253,7 @@ func (t compass) isArbitraryCallAlreadyExecuted(ctx context.Context, messageID u
}

var found bool
_, err := t.evm.FilterLogs(ctx, filter, nil, func(logs []etherumtypes.Log) bool {
_, err := t.evm.FilterLogs(ctx, filter, big.NewInt(t.startingBlockHeight), func(logs []etherumtypes.Log) bool {
found = len(logs) > 0
return !found
})
Expand Down
19 changes: 12 additions & 7 deletions chain/evm/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func (f *Factory) Build(
smartContractABIJson,
smartContractAddress string,
chainID *big.Int,
blockHeight int64,
blockHeightHash common.Hash,
) (chain.Processor, error) {

var smartContractABI *abi.ABI
Expand All @@ -54,16 +56,19 @@ func (f *Factory) Build(

return Processor{
compass: compass{
CompassID: smartContractID,
ChainReferenceID: chainReferenceID,
smartContractAddr: common.HexToAddress(smartContractAddress),
chainID: chainID,
compassAbi: smartContractABI,
paloma: f.palomaClienter,
evm: client,
CompassID: smartContractID,
ChainReferenceID: chainReferenceID,
smartContractAddr: common.HexToAddress(smartContractAddress),
chainID: chainID,
compassAbi: smartContractABI,
paloma: f.palomaClienter,
evm: client,
startingBlockHeight: blockHeight,
},
evmClient: client,
chainType: "EVM",
chainReferenceID: chainReferenceID,
blockHeight: blockHeight,
blockHeightHash: blockHeightHash,
}, nil
}
14 changes: 9 additions & 5 deletions chain/evm/mock_ethClientToFilterLogs_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions chain/evm/mock_ethClienter_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions chain/evm/mock_evmClienter_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions chain/evm/mocks/PalomaClienter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions chain/evm/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/palomachain/pigeon/chain"
"github.com/palomachain/pigeon/errors"
"github.com/palomachain/pigeon/util/slice"
log "github.com/sirupsen/logrus"
)
Expand All @@ -23,6 +24,9 @@ type Processor struct {
chainReferenceID string

turnstoneEVMContract common.Address

blockHeight int64
blockHeightHash common.Hash
}

var _ chain.Processor = Processor{}
Expand Down Expand Up @@ -87,3 +91,26 @@ func (p Processor) ExternalAccount() chain.ExternalAccount {
PubKey: p.evmClient.addr.Bytes(),
}
}

func (p Processor) IsRightChain(ctx context.Context) error {
block, err := p.evmClient.BlockByHash(ctx, p.blockHeightHash)
if err != nil {
return err
}

return p.isRightChain(block.Hash())
}

func (p Processor) isRightChain(blockHash common.Hash) error {
if blockHash != p.blockHeightHash {
return errors.Unrecoverable(chain.ErrNotConnectedToRightChain.WrapS(
"chain %s hash at block height %d should be %s, while it is %s. Check the rpc-url of the chain in the config.",
p.chainReferenceID,
p.blockHeight,
p.blockHeightHash,
blockHash,
))
}

return nil
}
47 changes: 47 additions & 0 deletions chain/evm/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/palomachain/pigeon/chain"
"github.com/palomachain/pigeon/config"
"github.com/palomachain/pigeon/errors"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -85,3 +87,48 @@ func TestProcessingMessages(t *testing.T) {
})
}
}

func TestIsRightChainLogic(t *testing.T) {
for _, tt := range []struct {
name string
blockHeight int64
blockHash common.Hash
gotBlockHash common.Hash

expErr error
}{
{
name: "with correct block hash it doesn return an error",
blockHeight: 5,
blockHash: common.HexToHash("0xabc"),
gotBlockHash: common.HexToHash("0xabc"),
expErr: nil,
},
{
name: "with incorrect block hash it returns an error (Unrecoverable)",
blockHeight: 5,
blockHash: common.HexToHash("0xabc"),
gotBlockHash: common.HexToHash("0x123"),
expErr: errors.ErrUnrecoverable,
},
{
name: "with incorrect block hash it returns an error (ErrNotConnectedToRightChain)",
blockHeight: 5,
blockHash: common.HexToHash("0xabc"),
gotBlockHash: common.HexToHash("0x123"),
expErr: chain.ErrNotConnectedToRightChain,
},
} {
t.Run(tt.name, func(t *testing.T) {
processor := Processor{
chainReferenceID: "test",
blockHeight: tt.blockHeight,
blockHeightHash: tt.blockHash,
}

err := processor.isRightChain(tt.gotBlockHash)

require.ErrorIs(t, err, tt.expErr)
})
}
}
28 changes: 23 additions & 5 deletions chain/mocks/Processor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d50b197

Please sign in to comment.