Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/goal/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ var listParticipationKeysCmd = &cobra.Command{
*/

// it's okay to proceed without algod info
lastUsed := maxRound(0, part.LastVote)
lastUsed := maxRound(0, part.LastStateProof)
lastUsed = maxRound(lastUsed, part.LastBlockProposal)
lastUsed = maxRound(lastUsed, part.LastStateProof)
lastUsedString := "N/A"
Expand Down
13 changes: 10 additions & 3 deletions daemon/algod/api/client/restClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ const (

// rawRequestPaths is a set of paths where the body should not be urlencoded
var rawRequestPaths = map[string]bool{
"/v1/transactions": true,
"/v2/teal/dryrun": true,
"/v2/teal/compile": true,
"/v1/transactions": true,
"/v2/teal/dryrun": true,
"/v2/teal/compile": true,
"/v2/participation": true,
}

// unauthorizedRequestError is generated when we receive 401 error from the server. This error includes the inner error
Expand Down Expand Up @@ -605,6 +606,12 @@ func (client RestClient) Proof(txid string, round uint64) (response generatedV2.
return
}

// PostParticipationKey sends a key file to the node.
func (client RestClient) PostParticipationKey(file []byte) (response generatedV2.PostParticipationResponse, err error) {
err = client.post(&response, "/v2/participation", file)
return
}

// GetParticipationKeys gets all of the participation keys
func (client RestClient) GetParticipationKeys() (response generatedV2.ParticipationKeysResponse, err error) {
err = client.get(&response, "/v2/participation", nil)
Expand Down
36 changes: 36 additions & 0 deletions libgoal/libgoal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package libgoal
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -30,6 +31,7 @@ import (

"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/daemon/algod/api/server/v2/generated"
"github.com/algorand/go-algorand/daemon/algod/api/spec/common"
v1 "github.com/algorand/go-algorand/daemon/algod/api/spec/v1"
"github.com/algorand/go-algorand/daemon/kmd/lib/kmdapi"
Expand Down Expand Up @@ -891,6 +893,40 @@ func (c *Client) GetPendingTransactionsByAddress(addr string, maxTxns uint64) (r
return
}

// AddParticipationKey takes a participation key file and sends it to the node.
// The key will be loaded into the system when the function returns successfully.
func (c *Client) AddParticipationKey(keyfile string) (resp generated.PostParticipationResponse, err error) {
data, err := ioutil.ReadFile(keyfile)
if err != nil {
return
}

algod, err := c.ensureAlgodClient()
if err != nil {
return
}

return algod.PostParticipationKey(data)
}

// GetParticipationKeys gets the currently installed participation keys.
func (c *Client) GetParticipationKeys() (resp generated.ParticipationKeysResponse, err error) {
algod, err := c.ensureAlgodClient()
if err == nil {
return algod.GetParticipationKeys()
}
return
}

// GetParticipationKeyByID looks up a specific participation key by its participationID.
func (c *Client) GetParticipationKeyByID(id string) (resp generated.ParticipationKeyResponse, err error) {
algod, err := c.ensureAlgodClient()
if err == nil {
return algod.GetParticipationKeyByID(id)
}
return
}

// ExportKey exports the private key of the passed account, assuming it's available
func (c *Client) ExportKey(walletHandle []byte, password, account string) (resp kmdapi.APIV1POSTKeyExportResponse, err error) {
kmd, err := c.ensureKmdClient()
Expand Down
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ func (node *AlgorandFullNode) VotingKeys(votingRound, keysRound basics.Round) []
matchingAccountsKeys[part.Address()] = true

// Make sure the key is registered.
err := node.accountManager.Registry().Register(part.ID(), keysRound)
err := node.accountManager.Registry().Register(part.ID(), votingRound)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also found a bug, this was causing the wrong "EffectiveRound" variables to be set in the registry.

if err != nil {
node.log.Warnf("Failed to register participation key (%s) with participation registry: %v\n", part.ID(), err)
}
Expand Down
66 changes: 66 additions & 0 deletions test/e2e-go/features/devmode/devmode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.

// Check that devmode is functioning as designed.
package devmode

import (
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/test/framework/fixtures"
"github.com/algorand/go-algorand/test/partitiontest"
)

func TestDevMode(t *testing.T) {
partitiontest.PartitionTest(t)

if testing.Short() {
t.Skip()
}

t.Parallel()

// Start devmode network, and make sure everything is primed by sending a transaction.
var fixture fixtures.RestClientFixture
fixture.SetupNoStart(t, filepath.Join("nettemplates", "DevModeNetwork.json"))
fixture.Start()
sender, err := fixture.GetRichestAccount()
require.NoError(t, err)
key := crypto.GenerateSignatureSecrets(crypto.Seed{})
receiver := basics.Address(key.SignatureVerifier)
txn := fixture.SendMoneyAndWait(0, 100000, 1000, sender.Address, receiver.String(), "")
firstRound := txn.ConfirmedRound + 1
start := time.Now()

// 2 transactions should be sent within one normal confirmation time.
for i := uint64(0); i < 2; i++ {
txn = fixture.SendMoneyAndWait(firstRound+i, 100000, 1000, sender.Address, receiver.String(), "")
require.Equal(t, firstRound+i, txn.FirstRound)
}
require.True(t, time.Since(start) < 2*time.Second, "Transactions should be quickly confirmed.")

// Without transactions there should be no rounds even after a normal confirmation time.
time.Sleep(10 * time.Second)
status, err := fixture.LibGoalClient.Status()
require.NoError(t, err)
require.Equal(t, txn.ConfirmedRound, status.LastRound, "There should be no rounds without a transaction.")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.

package participation

// Tests in this file are focused on testing how a specific account uses and
// manages its participation keys. DevMode is used to make things more
// deterministic.

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

"github.com/algorand/go-algorand/daemon/algod/api/server/v2/generated"
"github.com/algorand/go-algorand/data/account"
"github.com/algorand/go-algorand/libgoal"
"github.com/algorand/go-algorand/test/framework/fixtures"
"github.com/algorand/go-algorand/test/partitiontest"
)

// installParticipationKey generates a new key for a given account and installs it with the client.
func installParticipationKey(t *testing.T, client libgoal.Client, addr string, firstValid, lastValid uint64) (resp generated.PostParticipationResponse, part account.Participation, err error) {
dir, err := ioutil.TempDir("", "temporary_partkey_dir")
require.NoError(t, err)
defer os.RemoveAll(dir)

// Install overlapping participation keys...
part, filePath, err := client.GenParticipationKeysTo(addr, firstValid, lastValid, 100, dir)
require.NoError(t, err)
require.NotNil(t, filePath)
require.Equal(t, addr, part.Parent.String())

resp, err = client.AddParticipationKey(filePath)
return
}

func registerParticipationAndWait(t *testing.T, client libgoal.Client, part account.Participation) generated.NodeStatusResponse {
txParams, err := client.SuggestedParams()
require.NoError(t, err)
sAccount := part.Address().String()
sWH, err := client.GetUnencryptedWalletHandle()
require.NoError(t, err)
goOnlineTx, err := client.MakeUnsignedGoOnlineTx(sAccount, &part, txParams.LastRound+1, txParams.LastRound+1, txParams.Fee, [32]byte{})
require.NoError(t, err)
require.Equal(t, sAccount, goOnlineTx.Src().String())
onlineTxID, err := client.SignAndBroadcastTransaction(sWH, nil, goOnlineTx)
require.NoError(t, err)
require.NotEmpty(t, onlineTxID)
status, err := client.WaitForRound(txParams.LastRound)
require.NoError(t, err)
return status
}

func TestKeyRegistration(t *testing.T) {
partitiontest.PartitionTest(t)

if testing.Short() {
t.Skip()
}

t.Parallel()

// Start devmode network and initialize things for the test.
var fixture fixtures.RestClientFixture
fixture.SetupNoStart(t, filepath.Join("nettemplates", "DevModeOneWallet.json"))
fixture.Start()
sClient := fixture.GetLibGoalClientForNamedNode("Node")
minTxnFee, _, err := fixture.MinFeeAndBalance(0)
require.NoError(t, err)
accountResponse, err := fixture.GetRichestAccount()
require.NoError(t, err)
sAccount := accountResponse.Address

// Add an overlapping participation keys for the account on round 1 and 2
last := uint64(6_000_000)
numNew := 2
for i := 0; i < numNew; i++ {
response, part, err := installParticipationKey(t, sClient, sAccount, 0, last)
require.NoError(t, err)
require.NotNil(t, response)
registerParticipationAndWait(t, sClient, part)
}

// Make sure the new keys are installed.
keys, err := fixture.LibGoalClient.GetParticipationKeys()
require.NoError(t, err)
require.Len(t, keys, numNew+1)

// Zip ahead MaxBalLookback.
params, err := fixture.CurrentConsensusParams()
require.NoError(t, err)
lookback := params.MaxBalLookback
for i := uint64(1); i < lookback; i++ {
fixture.SendMoneyAndWait(2+i, 0, minTxnFee, sAccount, sAccount, "")
}

keys, err = fixture.LibGoalClient.GetParticipationKeys()
require.Equal(t, *(keys[0].EffectiveFirstValid), uint64(1))
require.Equal(t, *(keys[0].EffectiveLastValid), lookback)
require.Equal(t, *(keys[0].LastBlockProposal), lookback)

require.Equal(t, *(keys[1].EffectiveFirstValid), lookback+1)
require.Equal(t, *(keys[1].EffectiveLastValid), lookback+1)
require.Equal(t, *(keys[1].LastBlockProposal), lookback+1)

require.Equal(t, *(keys[2].EffectiveFirstValid), lookback+2)
require.Equal(t, *(keys[2].EffectiveLastValid), last)
require.Equal(t, *(keys[2].LastBlockProposal), lookback+2)
}
22 changes: 22 additions & 0 deletions test/testdata/nettemplates/DevModeOneWallet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"Genesis": {
"NetworkName": "devmodenet",
"Wallets": [
{
"Name": "Wallet1",
"Stake": 100,
"Online": true
}
],
"DevMode": true
},
"Nodes": [
{
"Name": "Node",
"IsRelay": false,
"Wallets": [
{ "Name": "Wallet1", "ParticipationOnly": false }
]
}
]
}