Skip to content

Commit

Permalink
Decouple the AccountManager.DeleteOldKeys disk persistence execution (a…
Browse files Browse the repository at this point in the history
…lgorand#355)

* Decouple the AccountManager.DeleteOldKeys and AccountManager.Key execution lock.

* reduce keydilution

* rollback change to scripts/travis/external_build.sh; will be handled in a separate PR.
  • Loading branch information
tsachiherman authored Sep 27, 2019
1 parent 67ef0da commit 7b052d5
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
24 changes: 15 additions & 9 deletions data/account/participation.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,28 @@ func (part Participation) OverlapsInterval(first, last basics.Round) bool {
}

// DeleteOldKeys securely deletes ephemeral keys for rounds strictly older than the given round.
func (part Participation) DeleteOldKeys(current basics.Round, proto config.ConsensusParams) error {
func (part Participation) DeleteOldKeys(current basics.Round, proto config.ConsensusParams) <-chan error {
keyDilution := part.KeyDilution
if keyDilution == 0 {
keyDilution = proto.DefaultKeyDilution
}

part.Voting.DeleteBeforeFineGrained(basics.OneTimeIDForRound(current, keyDilution), keyDilution)
raw := protocol.Encode(part.Voting.Snapshot())

return part.Store.Atomic(func(tx *sql.Tx) error {
_, err := tx.Exec("UPDATE ParticipationAccount SET voting=?", raw)
if err != nil {
return fmt.Errorf("Participation.DeleteOldKeys: failed to update account: %v", err)
}
return nil
})
errorCh := make(chan error, 1)
deleteOldKeys := func(encodedVotingSecrets []byte) {
errorCh <- part.Store.Atomic(func(tx *sql.Tx) error {
_, err := tx.Exec("UPDATE ParticipationAccount SET voting=?", encodedVotingSecrets)
if err != nil {
return fmt.Errorf("Participation.DeleteOldKeys: failed to update account: %v", err)
}
return nil
})
close(errorCh)
}
encodedVotingSecrets := protocol.Encode(part.Voting.Snapshot())
go deleteOldKeys(encodedVotingSecrets)
return errorCh
}

// PersistNewParent writes a new parent address to the partkey database.
Expand Down
25 changes: 19 additions & 6 deletions data/accountManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package data

import (
"fmt"

"github.com/algorand/go-deadlock"

"github.com/algorand/go-algorand/config"
Expand Down Expand Up @@ -121,14 +123,25 @@ func (manager *AccountManager) AddParticipation(participation account.Participat
// current round.
func (manager *AccountManager) DeleteOldKeys(current basics.Round, proto config.ConsensusParams) {
manager.mu.Lock()
defer manager.mu.Unlock()
pendingItems := make(map[string]<-chan error, len(manager.partIntervals))
func() {
defer manager.mu.Unlock()
for _, part := range manager.partIntervals {
// we pre-create the reported error string here, so that we won't need to have the participation key object if error is detected.
first, last := part.ValidInterval()
errString := fmt.Sprintf("AccountManager.DeleteOldKeys(%d): key for %s (%d-%d)",
current, part.Address().String(), first, last)
errCh := part.DeleteOldKeys(current, proto)

for _, part := range manager.partIntervals {
err := part.DeleteOldKeys(current, proto)
pendingItems[errString] = errCh
}
}()

// wait all all disk flushes, and report errors as they appear.
for errString, errCh := range pendingItems {
err := <-errCh
if err != nil {
first, last := part.ValidInterval()
logging.Base().Warnf("AccountManager.DeleteOldKeys(%d): key for %s (%d-%d): %v",
current, part.Address().String(), first, last, err)
logging.Base().Warnf("%s: %v", errString, err)
}
}
}
9 changes: 6 additions & 3 deletions gen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ func GenerateGenesisFiles(genesisData GenesisData, outDir string, verbose bool)
genesisData.RewardsPool = defaultPoolAddr
}

return generateGenesisFiles(outDir, proto, genesisData.NetworkName, genesisData.VersionModifier, allocation, genesisData.FirstPartKeyRound, genesisData.LastPartKeyRound, genesisData.FeeSink, genesisData.RewardsPool, genesisData.Comment, verbose)
return generateGenesisFiles(outDir, proto, genesisData.NetworkName, genesisData.VersionModifier, allocation, genesisData.FirstPartKeyRound, genesisData.LastPartKeyRound, genesisData.PartKeyDilution, genesisData.FeeSink, genesisData.RewardsPool, genesisData.Comment, verbose)
}

func generateGenesisFiles(outDir string, proto protocol.ConsensusVersion, netName string, schemaVersionModifier string,
allocation []genesisAllocation, firstWalletValid uint64, lastWalletValid uint64, feeSink, rewardsPool basics.Address, comment string, verbose bool) (err error) {
allocation []genesisAllocation, firstWalletValid uint64, lastWalletValid uint64, partKeyDilution uint64, feeSink, rewardsPool basics.Address, comment string, verbose bool) (err error) {

genesisAddrs := make(map[string]basics.Address)
records := make(map[string]basics.AccountData)
Expand All @@ -106,6 +106,9 @@ func generateGenesisFiles(outDir string, proto protocol.ConsensusVersion, netNam
if !ok {
return fmt.Errorf("protocol %s not supported", proto)
}
if partKeyDilution == 0 {
partKeyDilution = params.DefaultKeyDilution
}

// Sort account names alphabetically
sort.SliceStable(allocation, func(i, j int) bool {
Expand Down Expand Up @@ -162,7 +165,7 @@ func generateGenesisFiles(outDir string, proto protocol.ConsensusVersion, netNam
return
}

part, err = account.FillDBWithParticipationKeys(partDB, root.Address(), basics.Round(firstWalletValid), basics.Round(lastWalletValid), params.DefaultKeyDilution)
part, err = account.FillDBWithParticipationKeys(partDB, root.Address(), basics.Round(firstWalletValid), basics.Round(lastWalletValid), partKeyDilution)
if err != nil {
err = fmt.Errorf("could not generate new participation file %s: %v", pfilename, err)
os.Remove(pfilename)
Expand Down
1 change: 1 addition & 0 deletions gen/walletData.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type GenesisData struct {
ConsensusProtocol protocol.ConsensusVersion
FirstPartKeyRound uint64
LastPartKeyRound uint64
PartKeyDilution uint64
Wallets []WalletData
FeeSink basics.Address
RewardsPool basics.Address
Expand Down
6 changes: 5 additions & 1 deletion libgoal/participation.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ func (c *Client) InstallParticipationKeys(inputfile string) (part account.Partic
// disk blocks, but regardless of whether that works, we
// delete the input file. The consensus protocol version
// is irrelevant for the maxuint64 round number we pass in.
partkey.DeleteOldKeys(basics.Round(math.MaxUint64), config.Consensus[protocol.ConsensusCurrentVersion])
errCh := partkey.DeleteOldKeys(basics.Round(math.MaxUint64), config.Consensus[protocol.ConsensusCurrentVersion])
err = <-errCh
if err != nil {
return
}
os.Remove(inputfile)

return newpartkey, newdbpath, nil
Expand Down
6 changes: 6 additions & 0 deletions netdeploy/networkTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/algorand/go-algorand/config"
Expand Down Expand Up @@ -161,6 +162,11 @@ func loadTemplate(templateFile string) (NetworkTemplate, error) {
}
defer f.Close()

if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
// for arm machines, use smaller key dilution
template.Genesis.PartKeyDilution = 100
}

err = loadTemplateFromReader(f, &template)
return template, err
}
Expand Down

0 comments on commit 7b052d5

Please sign in to comment.