Skip to content

Commit d50b197

Browse files
authoredJul 22, 2022
Improve the frequency of how often we ping the evm node (#39)
1 parent 7d5a7cc commit d50b197

30 files changed

+505
-115
lines changed
 

‎chain/errors.go

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const (
1212

1313
ErrNotFound = whoops.String("not found")
1414

15+
ErrNotConnectedToRightChain = whoops.String("not connected to the right chain")
16+
1517
EnrichedChainReferenceID whoops.Field[string] = "chainReferenceID"
1618
EnrichedID whoops.Field[uint64] = "id"
1719
EnrichedItemType whoops.Field[string] = "type"

‎chain/evm/client.go

+4
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ func (c Client) TransactionByHash(ctx context.Context, txHash common.Hash) (*eth
252252
return c.conn.TransactionByHash(ctx, txHash)
253253
}
254254

255+
func (c Client) BlockByHash(ctx context.Context, blockHash common.Hash) (*ethtypes.Block, error) {
256+
return c.conn.BlockByHash(ctx, blockHash)
257+
}
258+
255259
//go:generate mockery --name=ethClientToFilterLogs --inpackage --testonly
256260
type ethClientToFilterLogs interface {
257261
FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]etherumtypes.Log, error)

‎chain/evm/compass.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type compass struct {
4545
paloma PalomaClienter
4646
evm evmClienter
4747

48+
startingBlockHeight int64
49+
4850
chainID *big.Int
4951
}
5052

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

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

253255
var found bool
254-
_, err := t.evm.FilterLogs(ctx, filter, nil, func(logs []etherumtypes.Log) bool {
256+
_, err := t.evm.FilterLogs(ctx, filter, big.NewInt(t.startingBlockHeight), func(logs []etherumtypes.Log) bool {
255257
found = len(logs) > 0
256258
return !found
257259
})

‎chain/evm/factory.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ func (f *Factory) Build(
2828
smartContractABIJson,
2929
smartContractAddress string,
3030
chainID *big.Int,
31+
blockHeight int64,
32+
blockHeightHash common.Hash,
3133
) (chain.Processor, error) {
3234

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

5557
return Processor{
5658
compass: compass{
57-
CompassID: smartContractID,
58-
ChainReferenceID: chainReferenceID,
59-
smartContractAddr: common.HexToAddress(smartContractAddress),
60-
chainID: chainID,
61-
compassAbi: smartContractABI,
62-
paloma: f.palomaClienter,
63-
evm: client,
59+
CompassID: smartContractID,
60+
ChainReferenceID: chainReferenceID,
61+
smartContractAddr: common.HexToAddress(smartContractAddress),
62+
chainID: chainID,
63+
compassAbi: smartContractABI,
64+
paloma: f.palomaClienter,
65+
evm: client,
66+
startingBlockHeight: blockHeight,
6467
},
6568
evmClient: client,
6669
chainType: "EVM",
6770
chainReferenceID: chainReferenceID,
71+
blockHeight: blockHeight,
72+
blockHeightHash: blockHeightHash,
6873
}, nil
6974
}

‎chain/evm/mock_ethClientToFilterLogs_test.go

+9-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎chain/evm/mock_ethClienter_test.go

+9-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎chain/evm/mock_evmClienter_test.go

+9-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎chain/evm/mocks/PalomaClienter.go

+9-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎chain/evm/processor.go

+27
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/ethereum/go-ethereum/common"
99
"github.com/ethereum/go-ethereum/crypto"
1010
"github.com/palomachain/pigeon/chain"
11+
"github.com/palomachain/pigeon/errors"
1112
"github.com/palomachain/pigeon/util/slice"
1213
log "github.com/sirupsen/logrus"
1314
)
@@ -23,6 +24,9 @@ type Processor struct {
2324
chainReferenceID string
2425

2526
turnstoneEVMContract common.Address
27+
28+
blockHeight int64
29+
blockHeightHash common.Hash
2630
}
2731

2832
var _ chain.Processor = Processor{}
@@ -87,3 +91,26 @@ func (p Processor) ExternalAccount() chain.ExternalAccount {
8791
PubKey: p.evmClient.addr.Bytes(),
8892
}
8993
}
94+
95+
func (p Processor) IsRightChain(ctx context.Context) error {
96+
block, err := p.evmClient.BlockByHash(ctx, p.blockHeightHash)
97+
if err != nil {
98+
return err
99+
}
100+
101+
return p.isRightChain(block.Hash())
102+
}
103+
104+
func (p Processor) isRightChain(blockHash common.Hash) error {
105+
if blockHash != p.blockHeightHash {
106+
return errors.Unrecoverable(chain.ErrNotConnectedToRightChain.WrapS(
107+
"chain %s hash at block height %d should be %s, while it is %s. Check the rpc-url of the chain in the config.",
108+
p.chainReferenceID,
109+
p.blockHeight,
110+
p.blockHeightHash,
111+
blockHash,
112+
))
113+
}
114+
115+
return nil
116+
}

‎chain/evm/processor_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"context"
55
"testing"
66

7+
"github.com/ethereum/go-ethereum/common"
78
"github.com/ethereum/go-ethereum/crypto"
89
"github.com/palomachain/pigeon/chain"
910
"github.com/palomachain/pigeon/config"
11+
"github.com/palomachain/pigeon/errors"
1012
"github.com/stretchr/testify/require"
1113
)
1214

@@ -85,3 +87,48 @@ func TestProcessingMessages(t *testing.T) {
8587
})
8688
}
8789
}
90+
91+
func TestIsRightChainLogic(t *testing.T) {
92+
for _, tt := range []struct {
93+
name string
94+
blockHeight int64
95+
blockHash common.Hash
96+
gotBlockHash common.Hash
97+
98+
expErr error
99+
}{
100+
{
101+
name: "with correct block hash it doesn return an error",
102+
blockHeight: 5,
103+
blockHash: common.HexToHash("0xabc"),
104+
gotBlockHash: common.HexToHash("0xabc"),
105+
expErr: nil,
106+
},
107+
{
108+
name: "with incorrect block hash it returns an error (Unrecoverable)",
109+
blockHeight: 5,
110+
blockHash: common.HexToHash("0xabc"),
111+
gotBlockHash: common.HexToHash("0x123"),
112+
expErr: errors.ErrUnrecoverable,
113+
},
114+
{
115+
name: "with incorrect block hash it returns an error (ErrNotConnectedToRightChain)",
116+
blockHeight: 5,
117+
blockHash: common.HexToHash("0xabc"),
118+
gotBlockHash: common.HexToHash("0x123"),
119+
expErr: chain.ErrNotConnectedToRightChain,
120+
},
121+
} {
122+
t.Run(tt.name, func(t *testing.T) {
123+
processor := Processor{
124+
chainReferenceID: "test",
125+
blockHeight: tt.blockHeight,
126+
blockHeightHash: tt.blockHash,
127+
}
128+
129+
err := processor.isRightChain(tt.gotBlockHash)
130+
131+
require.ErrorIs(t, err, tt.expErr)
132+
})
133+
}
134+
}

‎chain/mocks/Processor.go

+23-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)