continue on light client

This commit is contained in:
Marko Baricevic
2021-02-03 12:47:50 +01:00
parent 7e7e962d93
commit e77796550a
21 changed files with 190 additions and 190 deletions
+2 -2
View File
@@ -385,7 +385,7 @@ func (c *Client) compareWithLatestHeight(height uint64) (uint64, error) {
if err != nil {
return 0, fmt.Errorf("can't get last trusted height: %w", err)
}
if latestHeight == -1 {
if latestHeight == -1 { // todo standardize errors
return 0, errors.New("no headers exist")
}
@@ -442,7 +442,7 @@ func (c *Client) Update(ctx context.Context, now time.Time) (*types.LightBlock,
// primary.
//
// It will replace the primary provider if an error from a request to the provider occurs
func (c *Client) VerifyLightBlockAtHeight(ctx context.Context, height int64, now time.Time) (*types.LightBlock, error) {
func (c *Client) VerifyLightBlockAtHeight(ctx context.Context, height uint64, now time.Time) (*types.LightBlock, error) {
if height <= 0 {
return nil, errors.New("negative or zero height")
}
+3 -3
View File
@@ -141,7 +141,7 @@ func genHeader(chainID string, height uint64, bTime time.Time, txs types.Txs,
}
// GenSignedHeader calls genHeader and signHeader and combines them into a SignedHeader.
func (pkz privKeys) GenSignedHeader(chainID string, height int64, bTime time.Time, txs types.Txs,
func (pkz privKeys) GenSignedHeader(chainID string, height uint64, bTime time.Time, txs types.Txs,
valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int) *types.SignedHeader {
header := genHeader(chainID, height, bTime, txs, valset, nextValset, appHash, consHash, resHash)
@@ -152,7 +152,7 @@ func (pkz privKeys) GenSignedHeader(chainID string, height int64, bTime time.Tim
}
// GenSignedHeaderLastBlockID calls genHeader and signHeader and combines them into a SignedHeader.
func (pkz privKeys) GenSignedHeaderLastBlockID(chainID string, height int64, bTime time.Time, txs types.Txs,
func (pkz privKeys) GenSignedHeaderLastBlockID(chainID string, height uint64, bTime time.Time, txs types.Txs,
valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int,
lastBlockID types.BlockID) *types.SignedHeader {
@@ -212,7 +212,7 @@ func genMockNodeWithKeys(
valVariationInt = int(totalVariation)
totalVariation = -float32(valVariationInt)
newKeys = keys.ChangeKeys(valVariationInt)
currentHeader = keys.GenSignedHeaderLastBlockID(chainID, height, bTime.Add(time.Duration(height)*time.Minute),
currentHeader = keys.GenSignedHeaderLastBlockID(chainID, uint64(height), bTime.Add(time.Duration(height)*time.Minute),
nil,
keys.ToValidators(2, 0), newKeys.ToValidators(2, 0), hash("app_hash"), hash("cons_hash"),
hash("results_hash"), 0, len(keys), types.BlockID{Hash: lastHeader.Hash()})
+4 -4
View File
@@ -59,7 +59,7 @@ func (p *http) String() string {
// LightBlock fetches a LightBlock at the given height and checks the
// chainID matches.
func (p *http) LightBlock(ctx context.Context, height int64) (*types.LightBlock, error) {
func (p *http) LightBlock(ctx context.Context, height uint64) (*types.LightBlock, error) {
h, err := validateHeight(height)
if err != nil {
return nil, provider.ErrBadLightBlock{Reason: err}
@@ -94,7 +94,7 @@ func (p *http) ReportEvidence(ctx context.Context, ev types.Evidence) error {
return err
}
func (p *http) validatorSet(ctx context.Context, height *int64) (*types.ValidatorSet, error) {
func (p *http) validatorSet(ctx context.Context, height *uint64) (*types.ValidatorSet, error) {
// Since the malicious node could report a massive number of pages, making us
// spend a considerable time iterating, we restrict the number of pages here.
// => 10000 validators max
@@ -152,7 +152,7 @@ func (p *http) validatorSet(ctx context.Context, height *int64) (*types.Validato
return valSet, nil
}
func (p *http) signedHeader(ctx context.Context, height *int64) (*types.SignedHeader, error) {
func (p *http) signedHeader(ctx context.Context, height *uint64) (*types.SignedHeader, error) {
for attempt := 1; attempt <= maxRetryAttempts; attempt++ {
commit, err := p.client.Commit(ctx, height)
if err != nil {
@@ -169,7 +169,7 @@ func (p *http) signedHeader(ctx context.Context, height *int64) (*types.SignedHe
return nil, provider.ErrNoResponse
}
func validateHeight(height int64) (*int64, error) {
func validateHeight(height uint64) (*uint64, error) {
if height < 0 {
return nil, fmt.Errorf("expected height >= 0, got height %d", height)
}
+1 -1
View File
@@ -24,7 +24,7 @@ func (p *deadMock) String() string {
return fmt.Sprintf("DeadMock-%s", p.id)
}
func (p *deadMock) LightBlock(_ context.Context, height int64) (*types.LightBlock, error) {
func (p *deadMock) LightBlock(_ context.Context, height uint64) (*types.LightBlock, error) {
return nil, errNoResp
}
+4 -4
View File
@@ -44,7 +44,7 @@ func (p *Mock) String() string {
return fmt.Sprintf("Mock{id: %s, headers: %s, vals: %v}", p.id, headers.String(), vals.String())
}
func (p *Mock) LightBlock(_ context.Context, height int64) (*types.LightBlock, error) {
func (p *Mock) LightBlock(_ context.Context, height uint64) (*types.LightBlock, error) {
var lb *types.LightBlock
if height == 0 && len(p.headers) > 0 {
sh := p.headers[int64(len(p.headers))]
@@ -55,9 +55,9 @@ func (p *Mock) LightBlock(_ context.Context, height int64) (*types.LightBlock, e
}
}
if _, ok := p.headers[height]; ok {
sh := p.headers[height]
vals := p.vals[height]
if _, ok := p.headers[int64(height)]; ok {
sh := p.headers[int64(height)]
vals := p.vals[int64(height)]
lb = &types.LightBlock{
SignedHeader: sh,
ValidatorSet: vals,
+4 -4
View File
@@ -166,10 +166,10 @@ func makeConsensusStateFunc(c *lrpc.Client) rpcConsensusStateFunc {
}
}
type rpcConsensusParamsFunc func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultConsensusParams, error)
type rpcConsensusParamsFunc func(ctx *rpctypes.Context, height *uint64) (*ctypes.ResultConsensusParams, error)
func makeConsensusParamsFunc(c *lrpc.Client) rpcConsensusParamsFunc {
return func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultConsensusParams, error) {
return func(ctx *rpctypes.Context, height *uint64) (*ctypes.ResultConsensusParams, error) {
return c.ConsensusParams(ctx.Context(), height)
}
}
@@ -215,11 +215,11 @@ func makeBroadcastTxAsyncFunc(c *lrpc.Client) rpcBroadcastTxAsyncFunc {
}
type rpcABCIQueryFunc func(ctx *rpctypes.Context, path string,
data bytes.HexBytes, height int64, prove bool) (*ctypes.ResultABCIQuery, error)
data bytes.HexBytes, height uint64, prove bool) (*ctypes.ResultABCIQuery, error)
func makeABCIQueryFunc(c *lrpc.Client) rpcABCIQueryFunc {
return func(ctx *rpctypes.Context, path string, data bytes.HexBytes,
height int64, prove bool) (*ctypes.ResultABCIQuery, error) {
height uint64, prove bool) (*ctypes.ResultABCIQuery, error) {
return c.ABCIQueryWithOptions(ctx.Context(), path, data, rpcclient.ABCIQueryOptions{
Height: height,
+9 -9
View File
@@ -28,8 +28,8 @@ type KeyPathFunc func(path string, key []byte) (merkle.KeyPath, error)
// LightClient is an interface that contains functionality needed by Client from the light client.
type LightClient interface {
ChainID() string
VerifyLightBlockAtHeight(ctx context.Context, height int64, now time.Time) (*types.LightBlock, error)
TrustedLightBlock(height int64) (*types.LightBlock, error)
VerifyLightBlockAtHeight(ctx context.Context, height uint64, now time.Time) (*types.LightBlock, error)
TrustedLightBlock(height uint64) (*types.LightBlock, error)
}
// Client is an RPC client, which uses light#Client to verify data (if it can
@@ -233,7 +233,7 @@ func (c *Client) Health(ctx context.Context) (*ctypes.ResultHealth, error) {
// BlockchainInfo calls rpcclient#BlockchainInfo and then verifies every header
// returned.
func (c *Client) BlockchainInfo(ctx context.Context, minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
func (c *Client) BlockchainInfo(ctx context.Context, minHeight, maxHeight uint64) (*ctypes.ResultBlockchainInfo, error) {
res, err := c.next.BlockchainInfo(ctx, minHeight, maxHeight)
if err != nil {
return nil, err
@@ -277,7 +277,7 @@ func (c *Client) Genesis(ctx context.Context) (*ctypes.ResultGenesis, error) {
}
// Block calls rpcclient#Block and then verifies the result.
func (c *Client) Block(ctx context.Context, height *int64) (*ctypes.ResultBlock, error) {
func (c *Client) Block(ctx context.Context, height *uint64) (*ctypes.ResultBlock, error) {
res, err := c.next.Block(ctx, height)
if err != nil {
return nil, err
@@ -346,8 +346,8 @@ func (c *Client) BlockByHash(ctx context.Context, hash []byte) (*ctypes.ResultBl
// BlockResults returns the block results for the given height. If no height is
// provided, the results of the block preceding the latest are returned.
func (c *Client) BlockResults(ctx context.Context, height *int64) (*ctypes.ResultBlockResults, error) {
var h int64
func (c *Client) BlockResults(ctx context.Context, height *uint64) (*ctypes.ResultBlockResults, error) {
var h uint64
if height == nil {
res, err := c.next.Status(ctx)
if err != nil {
@@ -407,7 +407,7 @@ func (c *Client) BlockResults(ctx context.Context, height *int64) (*ctypes.Resul
return res, nil
}
func (c *Client) Commit(ctx context.Context, height *int64) (*ctypes.ResultCommit, error) {
func (c *Client) Commit(ctx context.Context, height *uint64) (*ctypes.ResultCommit, error) {
// Update the light client if we're behind and retrieve the light block at the requested height
l, err := c.updateLightClientIfNeededTo(ctx, *height)
if err != nil {
@@ -449,7 +449,7 @@ func (c *Client) TxSearch(ctx context.Context, query string, prove bool, page, p
}
// Validators fetches and verifies validators.
func (c *Client) Validators(ctx context.Context, height *int64, pagePtr, perPagePtr *int) (*ctypes.ResultValidators,
func (c *Client) Validators(ctx context.Context, height *uint64, pagePtr, perPagePtr *int) (*ctypes.ResultValidators,
error) {
// Update the light client if we're behind and retrieve the light block at the requested height.
l, err := c.updateLightClientIfNeededTo(ctx, *height)
@@ -492,7 +492,7 @@ func (c *Client) UnsubscribeAll(ctx context.Context, subscriber string) error {
return c.next.UnsubscribeAll(ctx, subscriber)
}
func (c *Client) updateLightClientIfNeededTo(ctx context.Context, height int64) (*types.LightBlock, error) {
func (c *Client) updateLightClientIfNeededTo(ctx context.Context, height uint64) (*types.LightBlock, error) {
l, err := c.lc.VerifyLightBlockAtHeight(ctx, height, time.Now())
if err != nil {
return nil, fmt.Errorf("failed to update light client to %d: %w", height, err)
+1 -1
View File
@@ -32,7 +32,7 @@ func (_m *LightClient) ChainID() string {
}
// TrustedLightBlock provides a mock function with given fields: height
func (_m *LightClient) TrustedLightBlock(height int64) (*types.LightBlock, error) {
func (_m *LightClient) TrustedLightBlock(height uint64) (*types.LightBlock, error) {
ret := _m.Called(height)
var r0 *types.LightBlock
+9 -9
View File
@@ -83,7 +83,7 @@ func (s *dbs) SaveLightBlock(lb *types.LightBlock) error {
// the db.
//
// Safe for concurrent use by multiple goroutines.
func (s *dbs) DeleteLightBlock(height int64) error {
func (s *dbs) DeleteLightBlock(height uint64) error {
if height <= 0 {
panic("negative or zero height")
}
@@ -110,7 +110,7 @@ func (s *dbs) DeleteLightBlock(height int64) error {
// LightBlock retrieves the LightBlock at the given height.
//
// Safe for concurrent use by multiple goroutines.
func (s *dbs) LightBlock(height int64) (*types.LightBlock, error) {
func (s *dbs) LightBlock(height uint64) (*types.LightBlock, error) {
if height <= 0 {
panic("negative or zero height")
}
@@ -140,7 +140,7 @@ func (s *dbs) LightBlock(height int64) (*types.LightBlock, error) {
// LastLightBlockHeight returns the last LightBlock height stored.
//
// Safe for concurrent use by multiple goroutines.
func (s *dbs) LastLightBlockHeight() (int64, error) {
func (s *dbs) LastLightBlockHeight() (uint64, error) {
itr, err := s.db.ReverseIterator(
s.lbKey(1),
append(s.lbKey(1<<63-1), byte(0x00)),
@@ -154,13 +154,13 @@ func (s *dbs) LastLightBlockHeight() (int64, error) {
return s.decodeLbKey(itr.Key())
}
return -1, itr.Error()
return 0, itr.Error()
}
// FirstLightBlockHeight returns the first LightBlock height stored.
//
// Safe for concurrent use by multiple goroutines.
func (s *dbs) FirstLightBlockHeight() (int64, error) {
func (s *dbs) FirstLightBlockHeight() (uint64, error) {
itr, err := s.db.Iterator(
s.lbKey(1),
append(s.lbKey(1<<63-1), byte(0x00)),
@@ -174,14 +174,14 @@ func (s *dbs) FirstLightBlockHeight() (int64, error) {
return s.decodeLbKey(itr.Key())
}
return -1, itr.Error()
return 0, itr.Error()
}
// LightBlockBefore iterates over light blocks until it finds a block before
// the given height. It returns ErrLightBlockNotFound if no such block exists.
//
// Safe for concurrent use by multiple goroutines.
func (s *dbs) LightBlockBefore(height int64) (*types.LightBlock, error) {
func (s *dbs) LightBlockBefore(height uint64) (*types.LightBlock, error) {
if height <= 0 {
panic("negative or zero height")
}
@@ -286,7 +286,7 @@ func (s *dbs) sizeKey() []byte {
return key
}
func (s *dbs) lbKey(height int64) []byte {
func (s *dbs) lbKey(height uint64) []byte {
key, err := orderedcode.Append(nil, prefixLightBlock, height)
if err != nil {
panic(err)
@@ -294,7 +294,7 @@ func (s *dbs) lbKey(height int64) []byte {
return key
}
func (s *dbs) decodeLbKey(key []byte) (height int64, err error) {
func (s *dbs) decodeLbKey(key []byte) (height uint64, err error) {
var lightBlockPrefix int64
remaining, err := orderedcode.Parse(string(key), &lightBlockPrefix, &height)
if err != nil {
+7 -7
View File
@@ -30,7 +30,7 @@ func TestLast_FirstLightBlockHeight(t *testing.T) {
assert.EqualValues(t, -1, height)
// 1 key
err = dbStore.SaveLightBlock(randLightBlock(int64(1)))
err = dbStore.SaveLightBlock(randLightBlock(uint64(1)))
require.NoError(t, err)
height, err = dbStore.LastLightBlockHeight()
@@ -80,7 +80,7 @@ func Test_LightBlockBefore(t *testing.T) {
_, _ = dbStore.LightBlockBefore(100)
})
err := dbStore.SaveLightBlock(randLightBlock(int64(2)))
err := dbStore.SaveLightBlock(randLightBlock(uint64(2)))
require.NoError(t, err)
h, err := dbStore.LightBlockBefore(3)
@@ -117,7 +117,7 @@ func Test_Prune(t *testing.T) {
// Multiple headers
for i := 1; i <= 10; i++ {
err = dbStore.SaveLightBlock(randLightBlock(int64(i)))
err = dbStore.SaveLightBlock(randLightBlock(uint64(i)))
require.NoError(t, err)
}
@@ -134,9 +134,9 @@ func Test_Concurrency(t *testing.T) {
dbStore := New(dbm.NewMemDB())
var wg sync.WaitGroup
for i := 1; i <= 100; i++ {
for i := uint64(1); i <= 100; i++ {
wg.Add(1)
go func(i int64) {
go func(i uint64) {
defer wg.Done()
err := dbStore.SaveLightBlock(randLightBlock(i))
@@ -176,13 +176,13 @@ func Test_Concurrency(t *testing.T) {
}
}
}(int64(i))
}(i)
}
wg.Wait()
}
func randLightBlock(height int64) *types.LightBlock {
func randLightBlock(height uint64) *types.LightBlock {
vals, _ := types.RandValidatorSet(2, 1)
return &types.LightBlock{
SignedHeader: &types.SignedHeader{