-
Notifications
You must be signed in to change notification settings - Fork 492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented New REST interfaces #3099
Merged
winder
merged 4 commits into
algorand:feature/partkey
from
AlgoStephenAkiki:feature/2656-implement-new-participation-key-endpoints
Oct 21, 2021
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import ( | |
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
|
@@ -56,6 +57,7 @@ import ( | |
"github.com/algorand/go-algorand/util/metrics" | ||
"github.com/algorand/go-algorand/util/timers" | ||
"github.com/algorand/go-deadlock" | ||
uuid "github.com/satori/go.uuid" | ||
) | ||
|
||
// StatusReport represents the current basic status of the node | ||
|
@@ -798,6 +800,143 @@ func (node *AlgorandFullNode) checkForParticipationKeys() { | |
} | ||
} | ||
|
||
// ListParticipationKeys returns all participation keys currently installed on the node | ||
func (node *AlgorandFullNode) ListParticipationKeys() (partKeys []account.ParticipationRecord, err error) { | ||
return node.accountManager.Registry().GetAll(), nil | ||
AlgoStephenAkiki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// GetParticipationKey retries the information of a participation id from the node | ||
func (node *AlgorandFullNode) GetParticipationKey(partKey account.ParticipationID) (account.ParticipationRecord, error) { | ||
rval := node.accountManager.Registry().Get(partKey) | ||
|
||
if rval.IsZero() { | ||
return account.ParticipationRecord{}, account.ErrParticipationIDNotFound | ||
} | ||
|
||
return node.accountManager.Registry().Get(partKey), nil | ||
} | ||
|
||
// RemoveParticipationKey given a participation id, remove the records from the node | ||
func (node *AlgorandFullNode) RemoveParticipationKey(partKey account.ParticipationID) error { | ||
|
||
// Need to remove the file and then remove the entry in the registry | ||
// Let's first get the recorded information from the registry so we can lookup the file | ||
|
||
partRecord := node.accountManager.Registry().Get(partKey) | ||
|
||
if partRecord.IsZero() { | ||
return nil | ||
} | ||
|
||
genID := node.GenesisID() | ||
|
||
outDir := filepath.Join(node.rootDir, genID) | ||
|
||
filename := config.PartKeyFilename(partRecord.ParticipationID.String(), uint64(partRecord.FirstValid), uint64(partRecord.LastValid)) | ||
fullyQualifiedFilename := filepath.Join(outDir, filepath.Base(filename)) | ||
|
||
err := node.accountManager.Registry().Delete(partKey) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = node.accountManager.Registry().Flush() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Only after deleting and flushing do we want to remove the file | ||
_ = os.Remove(fullyQualifiedFilename) | ||
|
||
return nil | ||
} | ||
|
||
func createTemporaryParticipationKey(outDir string, partKeyBinary *[]byte) (string, error) { | ||
var sb strings.Builder | ||
|
||
// Create a temporary filename with a UUID so that we can call this function twice | ||
// in a row without worrying about collisions | ||
sb.WriteString("tempPartKeyBinary.") | ||
sb.WriteString(uuid.NewV4().String()) | ||
sb.WriteString(".bin") | ||
|
||
tempFile := filepath.Join(outDir, filepath.Base(sb.String())) | ||
|
||
file, err := os.Create(tempFile) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
_, err = file.Write(*partKeyBinary) | ||
|
||
file.Close() | ||
|
||
if err != nil { | ||
os.Remove(tempFile) | ||
return "", err | ||
} | ||
|
||
return tempFile, nil | ||
} | ||
|
||
// InstallParticipationKey Given a participation key binary stream install the participation key | ||
func (node *AlgorandFullNode) InstallParticipationKey(partKeyBinary *[]byte) (account.ParticipationID, error) { | ||
genID := node.GenesisID() | ||
|
||
outDir := filepath.Join(node.rootDir, genID) | ||
|
||
fullyQualifiedTempFile, err := createTemporaryParticipationKey(outDir, partKeyBinary) | ||
// We need to make sure no tempfile is created/remains if there is an error | ||
// However, we will eventually rename this file but if we fail in-between | ||
// this point and the rename we want to ensure that we remove the temporary file | ||
// After we rename, this will fail anyway since the file will not exist | ||
|
||
// Explicitly ignore the error with a closure | ||
defer func(name string) { | ||
_ = os.Remove(name) | ||
}(fullyQualifiedTempFile) | ||
|
||
if err != nil { | ||
return account.ParticipationID{}, err | ||
} | ||
|
||
inputdb, err := db.MakeErasableAccessor(fullyQualifiedTempFile) | ||
if err != nil { | ||
return account.ParticipationID{}, err | ||
} | ||
defer inputdb.Close() | ||
|
||
partkey, err := account.RestoreParticipation(inputdb) | ||
if err != nil { | ||
return account.ParticipationID{}, err | ||
} | ||
defer partkey.Close() | ||
|
||
if partkey.Parent == (basics.Address{}) { | ||
return account.ParticipationID{}, fmt.Errorf("cannot install partkey with missing (zero) parent address") | ||
} | ||
|
||
// Tell the AccountManager about the Participation (dupes don't matter) so we ignore the return value | ||
_ = node.accountManager.AddParticipation(partkey) | ||
AlgoStephenAkiki marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On closer inspection,
The code would currently overwrite the duplicate key, I think that's probably fine. In the future when we stop writing the file to begin with, it would just be a no-op. |
||
|
||
err = node.accountManager.Registry().Flush() | ||
if err != nil { | ||
return account.ParticipationID{}, err | ||
} | ||
|
||
newFilename := config.PartKeyFilename(partkey.ID().String(), uint64(partkey.FirstValid), uint64(partkey.LastValid)) | ||
newFullyQualifiedFilename := filepath.Join(outDir, filepath.Base(newFilename)) | ||
|
||
err = os.Rename(fullyQualifiedTempFile, newFullyQualifiedFilename) | ||
|
||
if err != nil { | ||
return account.ParticipationID{}, nil | ||
} | ||
|
||
return partkey.ID(), nil | ||
} | ||
|
||
func (node *AlgorandFullNode) loadParticipationKeys() error { | ||
// Generate a list of all potential participation key files | ||
genesisDir := filepath.Join(node.rootDir, node.genesisID) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coming soon™