Skip to content

Commit

Permalink
fix new delegates
Browse files Browse the repository at this point in the history
  • Loading branch information
cryi committed Jun 26, 2024
1 parent 1eb0e35 commit ab9e3a4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 28 deletions.
14 changes: 9 additions & 5 deletions constants/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ var (

ErrDelegateHasNoMinimumDelegatedBalance = errors.New("delegate has no minimum delegated balance")

ErrFailedToFetchContract = errors.New("failed to fetch contract")
ErrBalanceNotFoundInDelegationState = errors.New("balance not found in delegation state")
ErrDelegatorNotFoundInDelegationState = errors.New("delegator not found in delegation state")
ErrMinimumDelegatedBalanceNotFound = errors.New("minimum delegated balance not found")
ErrFailedToFetchContractBalances = errors.New("failed to fetch contract balances")
ErrFailedToFetchContract = errors.New("failed to fetch contract")
ErrFailedToFetchContractBalance = errors.Join(ErrFailedToFetchContract, errors.New("failed to fetch contract balance"))
ErrFailedToFetchContractUnstakeRequests = errors.Join(ErrFailedToFetchContract, errors.New("failed to fetch contract unstake requests"))
ErrFailedToFetchContractDelegated = errors.Join(ErrFailedToFetchContract, errors.New("failed to fetch contract delegated"))
ErrBalanceNotFoundInDelegationState = errors.New("balance not found in delegation state")
ErrDelegatorNotFoundInDelegationState = errors.New("delegator not found in delegation state")
ErrMinimumDelegatedBalanceNotFound = errors.New("minimum delegated balance not found")
ErrFailedToFetchContractBalances = errors.New("failed to fetch contract balances")
ErrDelegateNotRegistered = errors.New("delegate not registered")

// notifications

Expand Down
2 changes: 1 addition & 1 deletion constants/version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package constants

const (
VERSION = "0.0.13"
VERSION = "0.0.14"
CODENAME = "ogun"
)
51 changes: 36 additions & 15 deletions core/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,19 @@ func (engine *rpcCollector) getDelegateDelegatedContracts(ctx context.Context, a
return attemptWithClients(engine.rpcs, func(client *rpc.Client) ([]tezos.Address, error) {
var delegatedContracts []tezos.Address
err := client.Get(ctx, u, &delegatedContracts)
if err != nil && strings.Contains(err.Error(), "delegate.not_registered") { // return empty list if delegate is not registered yet
return []tezos.Address{}, nil
if err != nil {
var rpcErrors []rpc.GenericError
err2 := client.Get(ctx, u, &rpcErrors)
if err2 != nil {
return nil, err
}
for _, rpcError := range rpcErrors {
if strings.Contains(rpcError.ID, "delegate.not_registered") {
return []tezos.Address{}, constants.ErrDelegateNotRegistered
}
}
}

return delegatedContracts, err
})
}
Expand Down Expand Up @@ -228,7 +238,7 @@ func (engine *rpcCollector) fetchContractInitialBalanceInfo(ctx context.Context,
return client.GetContractBalance(ctx, address, blockBeforeMinimumId)
})
if err != nil {
return nil, errors.Join(constants.ErrFailedToFetchContract, err)
return nil, errors.Join(constants.ErrFailedToFetchContractBalance, err)
}

delegate, err := engine.getContractDelegate(ctx, address, blockBeforeMinimumId)
Expand All @@ -242,7 +252,7 @@ func (engine *rpcCollector) fetchContractInitialBalanceInfo(ctx context.Context,

unstakeRequests, err := engine.getContractUnstakeRequests(ctx, address, blockBeforeMinimumId)
if err != nil {
return nil, errors.Join(constants.ErrFailedToFetchContract, err)
return nil, errors.Join(constants.ErrFailedToFetchContractUnstakeRequests, err)
}

stakedBalance, err := engine.getContractStakedBalance(ctx, address, lastBlockInCycle)
Expand Down Expand Up @@ -283,8 +293,13 @@ func (engine *rpcCollector) fetchInitialDelegationState(ctx context.Context, del
state.Parameters = params

// but we fill the rest from delegate state at the beginning of the block
newlyRegistered := false
delegateDelegatedContracts, err := engine.getDelegateDelegatedContracts(ctx, delegate.Delegate, blockBeforeMinimumId)
if err != nil {
switch err {
case constants.ErrDelegateNotRegistered:
newlyRegistered = true
case nil:
default:
return nil, err
}

Expand Down Expand Up @@ -351,18 +366,24 @@ func (engine *rpcCollector) fetchInitialDelegationState(ctx context.Context, del
return nil, constants.ErrFailedToFetchContractBalances
}

// fetch expected delegated balance and determine extra
// there is no raw context for newly registered delegates so skip this part
if !newlyRegistered {
// fetch expected delegated balance and determine extra
// fetch expected delegated balance and determine extra

expectedDelegated, err := engine.getDelegatedBalanceFromRawContext(ctx, delegate.Delegate, blockBeforeMinimumId)
if err != nil {
return nil, errors.Join(constants.ErrFailedToFetchContract, err)
}
// fetch expected delegated balance and determine extra

if expectedDelegated.Int64() > state.GetDelegatedBalance() {
state.AddBalance(tezos.BurnAddress, common.DelegationStateBalanceInfo{
Balance: expectedDelegated.Int64() - state.GetDelegatedBalance(),
Baker: delegate.Delegate,
})
expectedDelegated, err := engine.getDelegatedBalanceFromRawContext(ctx, delegate.Delegate, blockBeforeMinimumId)
if err != nil {
return nil, errors.Join(constants.ErrFailedToFetchContractDelegated, err)
}

if expectedDelegated.Int64() > state.GetDelegatedBalance() {
state.AddBalance(tezos.BurnAddress, common.DelegationStateBalanceInfo{
Balance: expectedDelegated.Int64() - state.GetDelegatedBalance(),
Baker: delegate.Delegate,
})
}
}

return state, nil
Expand Down
10 changes: 3 additions & 7 deletions core/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,7 @@ func TestGetDelegationState(t *testing.T) {
func TestCycle749RaceConditions(t *testing.T) {
assert := assert.New(t)
debug.SetMaxThreads(1000000)
/*
- tz1S5WxdZR5f9NzsPXhr7L9L1vrEb5spZFur
- tz1eu3mkvEjzPgGoRMuKY7EHHtSwz88VxS31
- tz3LV9aGKHDnAZHCtC9SjNtTrKRu678FqSki
- tz1aKxnrzx5PXZJe7unufEswVRCMU9yafmfb
- tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv
*/

cycle := int64(749)
collector, err := newRpcCollector(defaultCtx, []string{"https://eu.rpc.tez.capital/", "https://rpc.tzkt.io/mainnet/"}, getTransport(fmt.Sprintf("../test/data/%d", cycle)))
assert.Nil(err)
Expand All @@ -141,6 +135,8 @@ func TestCycle749RaceConditions(t *testing.T) {
tezos.MustParseAddress("tz3LV9aGKHDnAZHCtC9SjNtTrKRu678FqSki"),
tezos.MustParseAddress("tz1aKxnrzx5PXZJe7unufEswVRCMU9yafmfb"),
tezos.MustParseAddress("tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv"),
tezos.MustParseAddress("tz1NuAqi3T35CPZV7tQu94wa3urCCzJrV7zc"),
tezos.MustParseAddress("tz3Uzceas5ZauAh47FkKEVLupFoXstWq7MbX"),
}

err = runInParallel(defaultCtx, delegates, constants.OGUN_DELEGATE_FETCH_BATCH_SIZE, func(ctx context.Context, addr tezos.Address, mtx *sync.RWMutex) bool {
Expand Down
Binary file modified test/data/749.gob.lz4
Binary file not shown.

0 comments on commit ab9e3a4

Please sign in to comment.