Skip to content

Commit

Permalink
core/account: add expiration block processor
Browse files Browse the repository at this point in the history
This runs the expiration of account control programs as a separate
process. This fixes the expiration to wait until the transaction
processor has completed before deleting any data that may be needed.

1.1-stable version of #612

Closes #679
  • Loading branch information
erykwalder authored and iampogo committed Mar 2, 2017
1 parent 07519fd commit ba870fb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
5 changes: 5 additions & 0 deletions cmd/cored/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ func launchConfiguredCore(ctx context.Context, db *sql.DB, conf *config.Config,
}
// Start listeners
go pinStore.Listen(ctx, account.PinName, *dbURL)
go pinStore.Listen(ctx, account.ExpirePinName, *dbURL)
go pinStore.Listen(ctx, asset.PinName, *dbURL)

// Setup the transaction query indexer to index every transaction.
Expand Down Expand Up @@ -392,6 +393,10 @@ func launchConfiguredCore(ctx context.Context, db *sql.DB, conf *config.Config,
if err != nil {
chainlog.Fatal(ctx, chainlog.KeyError, err)
}
err = pinStore.CreatePin(ctx, account.ExpirePinName, height)
if err != nil {
chainlog.Fatal(ctx, chainlog.KeyError, err)
}
err = pinStore.CreatePin(ctx, asset.PinName, height)
if err != nil {
chainlog.Fatal(ctx, chainlog.KeyError, err)
Expand Down
24 changes: 14 additions & 10 deletions core/account/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ import (
"chain/protocol/bc"
)

// PinName is used to identify the pin associated with
// the account block processor.
const PinName = "account"
const (
// PinName is used to identify the pin associated with
// the account indexer block processor.
PinName = "account"
// ExpirePinName is used to identify the pin associated
// with the account control program expiration processor.
ExpirePinName = "expire-control-programs"
)

var emptyJSONObject = json.RawMessage(`{}`)

Expand Down Expand Up @@ -92,18 +97,17 @@ func (m *Manager) ProcessBlocks(ctx context.Context) {
if m.pinStore == nil {
return
}
m.pinStore.ProcessBlocks(ctx, m.chain, PinName, m.processBlock)
go m.pinStore.ProcessBlocks(ctx, m.chain, ExpirePinName, m.expireControlPrograms)
m.pinStore.ProcessBlocks(ctx, m.chain, PinName, m.indexAccountUTXOs)
}

func (m *Manager) processBlock(ctx context.Context, b *bc.Block) error {
err := m.indexAccountUTXOs(ctx, b)
if err != nil {
return err
}
func (m *Manager) expireControlPrograms(ctx context.Context, b *bc.Block) error {
<-m.pinStore.PinWaiter(PinName, b.Height)
<-m.pinStore.PinWaiter(query.TxPinName, b.Height)

// Delete expired account control programs.
const deleteQ = `DELETE FROM account_control_programs WHERE expires_at IS NOT NULL AND expires_at < $1`
_, err = m.db.Exec(ctx, deleteQ, b.Time())
_, err := m.db.Exec(ctx, deleteQ, b.Time())
return err
}

Expand Down

0 comments on commit ba870fb

Please sign in to comment.