Merge branch 'master' into marko/int64-

This commit is contained in:
Marko Baricevic
2021-02-04 14:29:06 +01:00
49 changed files with 5358 additions and 2067 deletions
+1 -1
View File
@@ -70,7 +70,7 @@ func (p *http) LightBlock(ctx context.Context, height uint64) (*types.LightBlock
return nil, err
}
vs, err := p.validatorSet(ctx, h)
vs, err := p.validatorSet(ctx, &sh.Height)
if err != nil {
return nil, err
}
+26 -13
View File
@@ -29,6 +29,7 @@ type KeyPathFunc func(path string, key []byte) (merkle.KeyPath, error)
//go:generate mockery --case underscore --name LightClient
type LightClient interface {
ChainID() string
Update(ctx context.Context, now time.Time) (*types.LightBlock, error)
VerifyLightBlockAtHeight(ctx context.Context, height uint64, now time.Time) (*types.LightBlock, error)
TrustedLightBlock(height uint64) (*types.LightBlock, error)
}
@@ -131,7 +132,8 @@ func (c *Client) ABCIQueryWithOptions(ctx context.Context, path string, data tmb
// Update the light client if we're behind.
// NOTE: AppHash for height H is in header H+1.
l, err := c.updateLightClientIfNeededTo(ctx, resp.Height+1)
nextHeight := resp.Height + 1
l, err := c.updateLightClientIfNeededTo(ctx, &nextHeight)
if err != nil {
return nil, err
}
@@ -214,7 +216,7 @@ func (c *Client) ConsensusParams(ctx context.Context, height *uint64) (*ctypes.R
}
// Update the light client if we're behind.
l, err := c.updateLightClientIfNeededTo(ctx, res.BlockHeight)
l, err := c.updateLightClientIfNeededTo(ctx, &res.BlockHeight)
if err != nil {
return nil, err
}
@@ -253,7 +255,7 @@ func (c *Client) BlockchainInfo(ctx context.Context, minHeight, maxHeight uint64
// Update the light client if we're behind.
if len(res.BlockMetas) > 0 {
lastHeight := res.BlockMetas[len(res.BlockMetas)-1].Header.Height
if _, err := c.updateLightClientIfNeededTo(ctx, lastHeight); err != nil {
if _, err := c.updateLightClientIfNeededTo(ctx, &lastHeight); err != nil {
return nil, err
}
}
@@ -297,7 +299,7 @@ func (c *Client) Block(ctx context.Context, height *uint64) (*ctypes.ResultBlock
}
// Update the light client if we're behind.
l, err := c.updateLightClientIfNeededTo(ctx, res.Block.Height)
l, err := c.updateLightClientIfNeededTo(ctx, &res.Block.Height)
if err != nil {
return nil, err
}
@@ -331,7 +333,7 @@ func (c *Client) BlockByHash(ctx context.Context, hash []byte) (*ctypes.ResultBl
}
// Update the light client if we're behind.
l, err := c.updateLightClientIfNeededTo(ctx, res.Block.Height)
l, err := c.updateLightClientIfNeededTo(ctx, &res.Block.Height)
if err != nil {
return nil, err
}
@@ -372,7 +374,8 @@ func (c *Client) BlockResults(ctx context.Context, height *uint64) (*ctypes.Resu
}
// Update the light client if we're behind.
trustedBlock, err := c.updateLightClientIfNeededTo(ctx, h+1)
nextHeight := h + 1
trustedBlock, err := c.updateLightClientIfNeededTo(ctx, &nextHeight)
if err != nil {
return nil, err
}
@@ -410,7 +413,8 @@ func (c *Client) BlockResults(ctx context.Context, height *uint64) (*ctypes.Resu
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)
// or at the latest height if no height is provided.
l, err := c.updateLightClientIfNeededTo(ctx, height)
if err != nil {
return nil, err
}
@@ -435,7 +439,7 @@ func (c *Client) Tx(ctx context.Context, hash []byte, prove bool) (*ctypes.Resul
}
// Update the light client if we're behind.
l, err := c.updateLightClientIfNeededTo(ctx, res.Height)
l, err := c.updateLightClientIfNeededTo(ctx, &res.Height)
if err != nil {
return nil, err
}
@@ -452,8 +456,9 @@ 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 *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)
// Update the light client if we're behind and retrieve the light block at the requested height
// or at the latest height if no height is provided.
l, err := c.updateLightClientIfNeededTo(ctx, height)
if err != nil {
return nil, err
}
@@ -470,7 +475,7 @@ func (c *Client) Validators(ctx context.Context, height *uint64, pagePtr, perPag
v := l.ValidatorSet.Validators[skipCount : skipCount+tmmath.MinInt(perPage, totalCount-skipCount)]
return &ctypes.ResultValidators{
BlockHeight: *height,
BlockHeight: l.Height,
Validators: v,
Count: len(v),
Total: totalCount}, nil
@@ -493,8 +498,16 @@ func (c *Client) UnsubscribeAll(ctx context.Context, subscriber string) error {
return c.next.UnsubscribeAll(ctx, subscriber)
}
func (c *Client) updateLightClientIfNeededTo(ctx context.Context, height uint64) (*types.LightBlock, error) {
l, err := c.lc.VerifyLightBlockAtHeight(ctx, height, time.Now())
func (c *Client) updateLightClientIfNeededTo(ctx context.Context, height *uint64) (*types.LightBlock, error) {
var (
l *types.LightBlock
err error
)
if height == nil {
l, err = c.lc.Update(ctx, time.Now())
} else {
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)
}
+24 -1
View File
@@ -1,4 +1,4 @@
// Code generated by mockery v2.5.1. DO NOT EDIT.
// Code generated by mockery v0.0.0-dev. DO NOT EDIT.
package mocks
@@ -54,6 +54,29 @@ func (_m *LightClient) TrustedLightBlock(height uint64) (*types.LightBlock, erro
return r0, r1
}
// Update provides a mock function with given fields: ctx, now
func (_m *LightClient) Update(ctx context.Context, now time.Time) (*types.LightBlock, error) {
ret := _m.Called(ctx, now)
var r0 *types.LightBlock
if rf, ok := ret.Get(0).(func(context.Context, time.Time) *types.LightBlock); ok {
r0 = rf(ctx, now)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*types.LightBlock)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, time.Time) error); ok {
r1 = rf(ctx, now)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// VerifyLightBlockAtHeight provides a mock function with given fields: ctx, height, now
func (_m *LightClient) VerifyLightBlockAtHeight(ctx context.Context, height uint64, now time.Time) (*types.LightBlock, error) {
ret := _m.Called(ctx, height, now)
+26 -26
View File
@@ -14,8 +14,8 @@ import (
)
const (
prefixLightBlock = int64(0x0a)
prefixSize = int64(0x0b)
prefixLightBlock = int64(11)
prefixSize = int64(12)
)
type dbs struct {
@@ -230,27 +230,11 @@ func (s *dbs) Prune(size uint16) error {
}
numToPrune := sSize - size
// 2) Iterate over headers and perform a batch operation.
itr, err := s.db.Iterator(
s.lbKey(1),
append(s.lbKey(1<<63-1), byte(0x00)),
)
if err != nil {
panic(err)
}
defer itr.Close()
b := s.db.NewBatch()
defer b.Close()
for itr.Valid() && numToPrune > 0 {
if err = b.Delete(itr.Key()); err != nil {
return err
}
itr.Next()
numToPrune--
}
if err = itr.Error(); err != nil {
// 2) use an iterator to batch together all the blocks that need to be deleted
if err := s.batchDelete(b, numToPrune); err != nil {
return err
}
@@ -261,12 +245,7 @@ func (s *dbs) Prune(size uint16) error {
}
// 4) write batch deletion to disk
err = b.WriteSync()
if err != nil {
return err
}
return nil
return b.WriteSync()
}
// Size returns the number of header & validator set pairs.
@@ -278,6 +257,27 @@ func (s *dbs) Size() uint16 {
return s.size
}
func (s *dbs) batchDelete(batch dbm.Batch, numToPrune uint16) error {
itr, err := s.db.Iterator(
s.lbKey(1),
append(s.lbKey(1<<63-1), byte(0x00)),
)
if err != nil {
return err
}
defer itr.Close()
for itr.Valid() && numToPrune > 0 {
if err = batch.Delete(itr.Key()); err != nil {
return err
}
itr.Next()
numToPrune--
}
return itr.Error()
}
func (s *dbs) sizeKey() []byte {
key, err := orderedcode.Append(nil, prefixSize)
if err != nil {