Skip to content
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

Algod: support state proofs recoverability #4803

Merged
merged 125 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 77 commits
Commits
Show all changes
125 commits
Select commit Hold shift + click to select a range
598705d
Persist StateProof builders on disk (#4553)
Aharonee Oct 23, 2022
fae555c
State Proof Recoverability: state proof verification tracking (#4570)
almog-t Oct 23, 2022
9ebdbce
fix tests: use better function for waiting
Oct 23, 2022
7f11ab3
Algod: state-proof key deletion safety (#4601)
algonathan Oct 26, 2022
3bac343
merged with avm box changes to master
Nov 2, 2022
e734ed7
fixed tracker db version
Nov 2, 2022
dc631d2
fixed lint error
Nov 2, 2022
194b7ac
updated msgp_gen
Nov 2, 2022
7d55071
built with latest dev tools
Nov 2, 2022
13c0fb4
fixed comment typo
Nov 2, 2022
311bc30
added missing dot
Nov 2, 2022
e569879
Merge pull request #4735 from almog-t/merge-recoverability-master
id-ms Nov 2, 2022
0bd34b8
StateProofs: Use stateproof tracker (#4733)
id-ms Nov 6, 2022
2005611
CR: shorter names for verification data in dcc
Nov 7, 2022
8845eb4
CR: renamed non db insert to append
Nov 7, 2022
a4dae19
renamed mutex
Nov 7, 2022
ec8dda1
CR: now using db queries pointer
Nov 7, 2022
788b50f
CR: using nested struct in tracker.go
Nov 7, 2022
02edfdf
renamed TargetRound to LastAttestedRound
Nov 7, 2022
f9cc17f
fixed potential segfault
Nov 7, 2022
4e01f81
renamed stateProofVerificationData to SPVerificationContext
Nov 8, 2022
2a6bc73
renamed internal stateproofverification struct to spVerification
Nov 8, 2022
30dd781
another rename for stateproofverificationcontext
Nov 8, 2022
e70d7d8
refactored deletion data usage
Nov 8, 2022
f000773
full renamed of stateproofverificationdata to stateproofverificationc…
Nov 8, 2022
eefc0f9
ran msgp again
Nov 8, 2022
f4456ef
stateproofround -> lastattestedround
Nov 9, 2022
c9dd1af
more stateproofround -> lastattestedroun
Nov 9, 2022
478d94c
CR: various renamings
Nov 9, 2022
687258f
StateProofs: Sp verification-tracker last lookup cache (#4780)
algonathan Nov 10, 2022
af27896
merged and renamed relevant variables in feature branch
Nov 10, 2022
f6959af
CR: shortened dcc variables
Nov 13, 2022
e0432f4
Merge pull request #4769 from almog-t/spv-tracking-cr-fixes
almog-t Nov 13, 2022
30de42d
Merge branch 'master' into merge-sp-recovery-with-master
Nov 15, 2022
056719b
Merge pull request #4793 from algoidan/merge-sp-recovery-with-master
id-ms Nov 15, 2022
8180823
StateProofs: Limit builders memory usage and reduce StateProofs signa…
Aharonee Nov 16, 2022
7409b73
Merge branch 'master' into merge-sp-recovery-with-master
Nov 16, 2022
7a2c94d
Merge pull request #4801 from algoidan/merge-sp-recovery-with-master
id-ms Nov 16, 2022
e6ed9eb
make state proof unit-tests synced
Nov 20, 2022
6dd8f40
remove printf
Nov 20, 2022
a85fcda
fix state proof message tests
Nov 20, 2022
75f6ac5
test cache limit on sig handler.
Nov 21, 2022
3a6982e
remove panic from test
Nov 21, 2022
1722455
fix race problem
Nov 21, 2022
d65a96b
refactor AllSigs test
Nov 21, 2022
50f009f
reduce log spamming
Nov 21, 2022
c9bd815
add signature broadcast test
Nov 21, 2022
9c7bf73
Revert "reduce log spamming"
Nov 21, 2022
1b35e37
handling mem issue
Nov 21, 2022
ae2b236
fix Broadcast test
Nov 21, 2022
4726d1d
reduce memory consumption in tests
Nov 22, 2022
d337e8a
CR fixed
Nov 23, 2022
7b9b68e
test builder cache context
Dec 4, 2022
7c8bce1
Merge pull request #4817 from algoidan/sp-recovery-test-refactor
id-ms Dec 4, 2022
d661705
Stateproof Recoverability: verification tracking catchpoint support (…
almog-t Dec 5, 2022
88d857e
fix merge issues
Dec 5, 2022
eab1fff
moving stateproofverification tracker's sql function into a file.
Dec 5, 2022
1e4544d
some refactoring
Dec 6, 2022
2ba5c97
refactoring
Dec 6, 2022
43a72f0
Merge pull request #4863 from algoidan/merge-sp-recoverability-from-m…
id-ms Dec 6, 2022
e5d48e4
merge with master
Dec 11, 2022
ebc6ef2
Merge pull request #4888 from algoidan/merge-sp-recoverability-from-m…
id-ms Dec 11, 2022
3c004a5
Fix catchup stateproof race (#4887)
almog-t Dec 27, 2022
0f58463
merge changes from master
Dec 27, 2022
b1905b4
Merge pull request #4950 from algoidan/merge-sp-recvoery-with-master
id-ms Dec 27, 2022
fe354d8
Apply StateProof transactions on catchup (#4949)
Aharonee Dec 29, 2022
8278b53
StateProofs: Fix CR comments (#4958)
id-ms Jan 1, 2023
42caa30
merge with master
Jan 2, 2023
b3fc3f5
Merge pull request #4959 from algoidan/merge-sp-recoverability-with-m…
id-ms Jan 2, 2023
c1ad50c
Fix flaky unit test
Aharonee Jan 2, 2023
40f1168
Merge branch 'master' into merge-sp-recoverbility-with-master
Jan 15, 2023
9f18d39
Merge pull request #5020 from algoidan/merge-sp-recoverbility-with-ma…
id-ms Jan 15, 2023
7e6d874
Add recoverability catchpoint e2e (#4961)
almog-t Jan 16, 2023
98d5a4f
Refactor insertSigToBuilder and handleSig, avoid unused allocations (…
Aharonee Jan 18, 2023
cd7c667
StateProof Recoverability: Fix CR comments (#5030)
id-ms Jan 19, 2023
816449f
change sp interval in short tests (#5081)
id-ms Jan 31, 2023
ede87b2
fix broken nightly tests
Jan 31, 2023
96d9c3b
merge with master
Jan 31, 2023
57ba79b
refactor sp tracker to support new store
Feb 1, 2023
2272129
use prepare statement
Feb 1, 2023
5b899b6
Merge pull request #5086 from algoidan/merge-sp-recvoerability-with-m…
id-ms Feb 1, 2023
4be1ecb
changed RLock to Lock to properly protect lastFlushTime writes (#5092)
almog-t Feb 1, 2023
1c25270
ledger: eternalData trackers concept
algorandskiy Feb 1, 2023
0ad0943
stateproof: e2e flakey-test AttestorsChange (#5101)
algonathan Feb 2, 2023
827365e
StateProof recoverability CR fixes (#5085)
id-ms Feb 2, 2023
4a2f579
CR fixes
algorandskiy Feb 2, 2023
295177c
Merge remote-tracking branch 'upstream/feature/stateproofs-recoverabi…
algorandskiy Feb 2, 2023
493a5f8
Merge pull request #5099 from algorandskiy/pavel/sp-recov
almog-t Feb 2, 2023
62b0c02
Revert "ledger: eternalData trackers concept" (#5104)
algorandskiy Feb 2, 2023
618736a
Fix metrics test
algorandskiy Feb 2, 2023
c90be81
Merge pull request #5106 from algorandskiy/pavel/sp-recov-test
almog-t Feb 2, 2023
8f8f6f1
Remove unused function to fetch message from db and small refactor
Aharonee Feb 5, 2023
d8ccde8
Fix errors tests log (#5117)
almog-t Feb 6, 2023
e331404
Inline sigExists and builderExists methods
Aharonee Jan 18, 2023
760c231
Stop stateproof worker on catchpoint cathcup and restart when done. (…
Aharonee Feb 16, 2023
f97766a
Merge branch 'master' into spr-merge-with-master
Feb 16, 2023
28c1313
Merge pull request #5144 from algoidan/spr-merge-with-master
id-ms Feb 16, 2023
4b8854b
StateProofRecoverability: fix comments (#5143)
id-ms Feb 16, 2023
f5e16e8
Merge remote-tracking branch 'upstream/master' into feature/stateproo…
algorandskiy Feb 17, 2023
6e2f233
post merge fixes:
algorandskiy Feb 17, 2023
9efe32c
fix lint
algorandskiy Feb 17, 2023
22b3125
Fix exceed partitiontest call
algorandskiy Feb 18, 2023
ecbcc8e
fix flaky test
Feb 19, 2023
ab7a68b
code review: fix interface names as suggested
algorandskiy Feb 21, 2023
8201750
Merge pull master (#5148)
algorandskiy Feb 21, 2023
da1d70f
fix linter in ledger_test.go
algorandskiy Feb 21, 2023
f19a285
StateProof Recvoerability: CR fixes (#5153)
id-ms Feb 24, 2023
52c8bcb
Merge remote-tracking branch 'upstream/master' into feature/stateproo…
algorandskiy Feb 27, 2023
d229775
merge master into feature/stateproofs-recoverability
algorandskiy Feb 27, 2023
0d0697a
Do not stop stateproof service in fast catchup mode (#5170)
algorandskiy Mar 1, 2023
0bc8014
Merge branch 'master' into merge-spr-with-master
Mar 7, 2023
7a2714f
Merge pull request #5179 from algoidan/merge-spr-with-master
id-ms Mar 7, 2023
3d303b0
fix flaky test
Mar 8, 2023
3c831df
CR fixes (#5176)
id-ms Mar 13, 2023
8d31459
Merge remote-tracking branch 'upstream/master' into pavel/stateproofs…
algorandskiy Mar 13, 2023
56cd05b
Merge remote-tracking branch 'upstream/master' into pavel/stateproofs…
algorandskiy Mar 13, 2023
389b8ac
Linter fixes
algorandskiy Mar 13, 2023
4f12cb6
Merge master (#5199)
algorandskiy Mar 13, 2023
c659a1d
Update log message
Aharonee Mar 16, 2023
6e4b414
Add a second mutex to stateproof.Worker to allow more granular locking.
Aharonee Mar 20, 2023
150efa2
convert to rw lock and fix linter error.
Mar 20, 2023
30da8b6
update comments.
Mar 23, 2023
8bf459b
using verification ctx from tracker when creating stats
Mar 29, 2023
fdbe4e5
Merge branch 'master' into merge-sp-recoverability-from-master
Mar 30, 2023
cd7dbcc
Merge pull request #5241 from algoidan/merge-sp-recoverability-from-m…
id-ms Mar 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions catchup/catchpointService.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"sync"
"time"

"github.com/algorand/go-algorand/stateproof"

"github.com/algorand/go-deadlock"

"github.com/algorand/go-algorand/config"
Expand All @@ -33,6 +31,7 @@ import (
"github.com/algorand/go-algorand/ledger/ledgercore"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/network"
"github.com/algorand/go-algorand/stateproof"
)

const (
Expand Down Expand Up @@ -259,6 +258,7 @@ func (cs *CatchpointCatchupService) processStageInactive() (err error) {
if err != nil {
return cs.abort(fmt.Errorf("processStageInactive failed to set a catchpoint label : %v", err))
}

err = cs.updateStage(ledger.CatchpointCatchupStateLedgerDownload)
if err != nil {
return cs.abort(fmt.Errorf("processStageInactive failed to update stage : %v", err))
Expand Down
4 changes: 4 additions & 0 deletions cmd/catchpointdump/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ var databaseCmd = &cobra.Command{
if err != nil {
reportErrorf("Unable to print key value store : %v", err)
}
err = printStateProofVerificationContext("./ledger.tracker.sqlite", outFile)
if err != nil {
reportWarnf("Unable to print state proof verification database : %v", err)
id-ms marked this conversation as resolved.
Show resolved Hide resolved
}
},
}

Expand Down
39 changes: 37 additions & 2 deletions cmd/catchpointdump/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import (

var catchpointFile string
var outFileName string
var excludedFields *cmdutil.CobraStringSliceValue = cmdutil.MakeCobraStringSliceValue(nil, []string{"version", "catchpoint"})
var excludedFields = cmdutil.MakeCobraStringSliceValue(nil, []string{"version", "catchpoint"})

func init() {
fileCmd.Flags().StringVarP(&catchpointFile, "tar", "t", "", "Specify the catchpoint file (either .tar or .tar.gz) to process")
Expand Down Expand Up @@ -127,7 +127,6 @@ var fileCmd = &cobra.Command{
}
defer outFile.Close()
}

err = printAccountsDatabase("./ledger.tracker.sqlite", true, fileHeader, outFile, excludedFields.GetSlice())
if err != nil {
reportErrorf("Unable to print account database : %v", err)
Expand All @@ -136,6 +135,10 @@ var fileCmd = &cobra.Command{
if err != nil {
reportErrorf("Unable to print key value store : %v", err)
}
err = printStateProofVerificationContext("./ledger.tracker.sqlite", outFile)
if err != nil {
reportErrorf("Unable to print state proof verification database : %v", err)
}
}
},
}
Expand Down Expand Up @@ -434,6 +437,38 @@ func printAccountsDatabase(databaseName string, stagingTables bool, fileHeader l
})
}

func printStateProofVerificationContext(databaseName string, outFile *os.File) error {
fileWriter := bufio.NewWriterSize(outFile, 1024*1024)
defer fileWriter.Flush()

dbAccessor, err := db.MakeAccessor(databaseName, true, false)
if err != nil || dbAccessor.Handle == nil {
return err
}
defer dbAccessor.Close()

var stateProofVerificationContext []ledgercore.StateProofVerificationContext
err = dbAccessor.Atomic(func(ctx context.Context, tx *sql.Tx) (err error) {
stateProofVerificationContext, err = store.GetAllCatchpointStateProofVerification(ctx, tx)
return err
})

if err != nil {
return err
}

var printedLines []string
for _, ctx := range stateProofVerificationContext {
jsonData, err := json.Marshal(ctx)
if err != nil {
return err
}
printedLines = append(printedLines, fmt.Sprintf("%d : %s", ctx.LastAttestedRound, string(jsonData)))
}
_, err = fmt.Fprintf(fileWriter, "State Proof Verification Data:\n"+strings.Join(printedLines, "\n")+"\n")
id-ms marked this conversation as resolved.
Show resolved Hide resolved
return err
}

func printKeyValue(writer *bufio.Writer, key, value []byte) {
var pretty string
ai, rest, err := logic.SplitBoxKey(string(key))
Expand Down
4 changes: 4 additions & 0 deletions cmd/catchpointdump/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ func loadAndDump(addr string, tarFile string, genesisInitState ledgercore.InitSt
if err != nil {
return err
}
err = printStateProofVerificationContext("./ledger.tracker.sqlite", outFile)
if err != nil {
return err
}

}
return nil
Expand Down
4 changes: 4 additions & 0 deletions cmd/tealdbg/localLedger.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ func (l *localLedger) BlockHdrCached(basics.Round) (bookkeeping.BlockHeader, err
return bookkeeping.BlockHeader{}, nil
}

func (l *localLedger) GetStateProofVerificationContext(_ basics.Round) (*ledgercore.StateProofVerificationContext, error) {
return nil, fmt.Errorf("localLedger: GetStateProofVerificationContext, needed for state proof verification, is not implemented in debugger")
}

func (l *localLedger) CheckDup(config.ConsensusParams, basics.Round, basics.Round, basics.Round, transactions.Txid, ledgercore.Txlease) error {
return nil
}
Expand Down
6 changes: 6 additions & 0 deletions config/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,10 @@ type ConsensusParams struct {
// the rewardsLevel, but the rewardsLevel has no meaning because the account
// has fewer than RewardUnit algos.
UnfundedSenders bool

// StateProofUseTrackerVerification specifies whether the node will use data from state proof verification tracker
// in order to verify state proofs.
StateProofUseTrackerVerification bool
}

// PaysetCommitType enumerates possible ways for the block header to commit to
Expand Down Expand Up @@ -1250,6 +1254,8 @@ func initConsensusProtocols() {

vFuture.LogicSigVersion = 9 // When moving this to a release, put a new higher LogicSigVersion here

vFuture.StateProofUseTrackerVerification = true

Consensus[protocol.ConsensusFuture] = vFuture

// vAlphaX versions are an separate series of consensus parameters and versions for alphanet
Expand Down
87 changes: 52 additions & 35 deletions crypto/stateproof/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,28 @@ var (
ErrCoinIndexError = errors.New("could not find corresponding index for a given coin")
)

// VotersAllocBound should be equal to config.Consensus[protocol.ConsensusCurrentVersion].VotersAllocBound
id-ms marked this conversation as resolved.
Show resolved Hide resolved
const VotersAllocBound = 1024
id-ms marked this conversation as resolved.
Show resolved Hide resolved

// BuilderPersistingFields is the set of fields of a Builder that are persisted to disk.
type BuilderPersistingFields struct {
id-ms marked this conversation as resolved.
Show resolved Hide resolved
_struct struct{} `codec:",omitempty,omitemptyarray"`
Data MessageHash `codec:"data"`
Round uint64 `codec:"rnd"`
Participants []basics.Participant `codec:"parts,allocbound=VotersAllocBound"`
Parttree *merklearray.Tree `codec:"parttree"`
LnProvenWeight uint64 `codec:"lnprv"`
ProvenWeight uint64 `codec:"prv"`
StrengthTarget uint64 `codec:"str"`
}

// Builder keeps track of signatures on a message and eventually produces
// a state proof for that message.
type Builder struct {
data MessageHash
round uint64
sigs []sigslot // Indexed by pos in participants
signedWeight uint64 // Total weight of signatures so far
participants []basics.Participant
parttree *merklearray.Tree
lnProvenWeight uint64
provenWeight uint64
strengthTarget uint64
cachedProof *StateProof
BuilderPersistingFields
sigs []sigslot // Indexed by pos in Participants
signedWeight uint64 // Total weight of signatures so far
cachedProof *StateProof
}

// MakeBuilder constructs an empty builder. After adding enough signatures and signed weight, this builder is used to create a stateproof.
Expand All @@ -58,16 +67,19 @@ func MakeBuilder(data MessageHash, round uint64, provenWeight uint64, part []bas
}

b := &Builder{
data: data,
round: round,
sigs: make([]sigslot, npart),
signedWeight: 0,
participants: part,
parttree: parttree,
lnProvenWeight: lnProvenWt,
provenWeight: provenWeight,
strengthTarget: strengthTarget,
cachedProof: nil,
BuilderPersistingFields: BuilderPersistingFields{
Data: data,
Round: round,
Participants: part,
Parttree: parttree,
LnProvenWeight: lnProvenWt,
ProvenWeight: provenWeight,
StrengthTarget: strengthTarget,
},

sigs: make([]sigslot, npart),
signedWeight: 0,
cachedProof: nil,
}

return b, nil
Expand All @@ -86,11 +98,11 @@ func (b *Builder) Present(pos uint64) (bool, error) {
// IsValid verifies that the participant along with the signature can be inserted to the builder.
// verifySig can be set to false when the signature is already verified (e.g. loaded from the DB)
func (b *Builder) IsValid(pos uint64, sig *merklesignature.Signature, verifySig bool) error {
if pos >= uint64(len(b.participants)) {
return fmt.Errorf("%w pos %d >= len(participants) %d", ErrPositionOutOfBound, pos, len(b.participants))
if pos >= uint64(len(b.Participants)) {
return fmt.Errorf("%w pos %d >= len(participants) %d", ErrPositionOutOfBound, pos, len(b.Participants))
}

p := b.participants[pos]
p := b.Participants[pos]

if p.Weight == 0 {
return fmt.Errorf("builder.IsValid: %w: position = %d", ErrPositionWithZeroWeight, pos)
Expand All @@ -101,7 +113,7 @@ func (b *Builder) IsValid(pos uint64, sig *merklesignature.Signature, verifySig
if err := sig.ValidateSaltVersion(merklesignature.SchemeSaltVersion); err != nil {
return err
}
if err := p.PK.VerifyBytes(b.round, b.data[:], sig); err != nil {
if err := p.PK.VerifyBytes(b.Round, b.Data[:], sig); err != nil {
return err
}
}
Expand All @@ -118,7 +130,7 @@ func (b *Builder) Add(pos uint64, sig merklesignature.Signature) error {
return ErrPositionAlreadyPresent
}

p := b.participants[pos]
p := b.Participants[pos]

// Remember the signature
b.sigs[pos].Weight = p.Weight
Expand All @@ -130,7 +142,7 @@ func (b *Builder) Add(pos uint64, sig merklesignature.Signature) error {

// Ready returns whether the state proof is ready to be built.
func (b *Builder) Ready() bool {
return b.cachedProof != nil || b.signedWeight > b.provenWeight
return b.cachedProof != nil || b.signedWeight > b.ProvenWeight
}

// SignedWeight returns the total weight of signatures added so far.
Expand Down Expand Up @@ -175,7 +187,7 @@ func (b *Builder) Build() (*StateProof, error) {
}

if !b.Ready() {
return nil, fmt.Errorf("%w: %d <= %d", ErrSignedWeightLessThanProvenWeight, b.signedWeight, b.provenWeight)
return nil, fmt.Errorf("%w: %d <= %d", ErrSignedWeightLessThanProvenWeight, b.signedWeight, b.ProvenWeight)
}

// Commit to the sigs array
Expand All @@ -197,17 +209,17 @@ func (b *Builder) Build() (*StateProof, error) {
MerkleSignatureSaltVersion: merklesignature.SchemeSaltVersion,
}

nr, err := numReveals(b.signedWeight, b.lnProvenWeight, b.strengthTarget)
nr, err := numReveals(b.signedWeight, b.LnProvenWeight, b.StrengthTarget)
if err != nil {
return nil, err
}

choice := coinChoiceSeed{
partCommitment: b.parttree.Root(),
lnProvenWeight: b.lnProvenWeight,
partCommitment: b.Parttree.Root(),
lnProvenWeight: b.LnProvenWeight,
sigCommitment: s.SigCommit,
signedWeight: s.SignedWeight,
data: b.data,
data: b.Data,
}

coinHash := makeCoinGenerator(&choice)
Expand All @@ -221,8 +233,8 @@ func (b *Builder) Build() (*StateProof, error) {
return nil, err
}

if pos >= uint64(len(b.participants)) {
return nil, fmt.Errorf("%w pos %d >= len(participants) %d", ErrPositionOutOfBound, pos, len(b.participants))
if pos >= uint64(len(b.Participants)) {
return nil, fmt.Errorf("%w pos %d >= len(participants) %d", ErrPositionOutOfBound, pos, len(b.Participants))
}

revealsSequence[j] = pos
Expand All @@ -236,7 +248,7 @@ func (b *Builder) Build() (*StateProof, error) {
// Generate the reveal for pos
s.Reveals[pos] = Reveal{
SigSlot: b.sigs[pos].sigslotCommit,
Part: b.participants[pos],
Part: b.Participants[pos],
}

proofPositions = append(proofPositions, pos)
Expand All @@ -247,7 +259,7 @@ func (b *Builder) Build() (*StateProof, error) {
return nil, err
}

partProofs, err := b.parttree.Prove(proofPositions)
partProofs, err := b.Parttree.Prove(proofPositions)
if err != nil {
return nil, err
}
Expand All @@ -258,3 +270,8 @@ func (b *Builder) Build() (*StateProof, error) {
b.cachedProof = s
return s, nil
}

// AllocSigs should only be used after decoding msgpacked Builder, as the sigs field is not exported and encoded
func (b *Builder) AllocSigs() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is very tricky. It is safer to replace this this with Start/Stop functions to make sure the Builder state is terminated before starting a new one.

Copy link
Contributor

Choose a reason for hiding this comment

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

This comment does not make much sense at this state of the code, but I will return to this after the Stateproof Start/Stop functionality is implemented.

Copy link
Contributor

Choose a reason for hiding this comment

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

not sure if start/stop service functionality is relevant here, builders are for stateproof rounds, not one per service. for the context, this is an answer in a thread

b.sigs = make([]sigslot, len(b.Participants))
algonautshant marked this conversation as resolved.
Show resolved Hide resolved
}
18 changes: 13 additions & 5 deletions crypto/stateproof/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/algorand/falcon"

"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/crypto/merklearray"
"github.com/algorand/go-algorand/crypto/merklesignature"
Expand Down Expand Up @@ -496,12 +497,12 @@ func TestBuildAndReady(t *testing.T) {
_, err = builder.Build()
a.ErrorIs(err, ErrSignedWeightLessThanProvenWeight)

builder.signedWeight = builder.provenWeight
builder.signedWeight = builder.ProvenWeight
a.False(builder.Ready())
_, err = builder.Build()
a.ErrorIs(err, ErrSignedWeightLessThanProvenWeight)

builder.signedWeight = builder.provenWeight + 1
builder.signedWeight = builder.ProvenWeight + 1
a.True(builder.Ready())
_, err = builder.Build()
a.NotErrorIs(err, ErrSignedWeightLessThanProvenWeight)
Expand All @@ -516,19 +517,19 @@ func TestErrorCases(t *testing.T) {
_, err := builder.Present(1)
a.ErrorIs(err, ErrPositionOutOfBound)

builder.participants = make([]basics.Participant, 1, 1)
builder.Participants = make([]basics.Participant, 1, 1)
builder.sigs = make([]sigslot, 1, 1)
err = builder.IsValid(1, &merklesignature.Signature{}, false)
a.ErrorIs(err, ErrPositionOutOfBound)

err = builder.IsValid(0, &merklesignature.Signature{}, false)
require.ErrorIs(t, err, ErrPositionWithZeroWeight)

builder.participants[0].Weight = 1
builder.Participants[0].Weight = 1
err = builder.IsValid(0, &merklesignature.Signature{}, true)
a.ErrorIs(err, merklesignature.ErrKeyLifetimeIsZero)

builder.participants[0].PK.KeyLifetime = 20
builder.Participants[0].PK.KeyLifetime = 20
err = builder.IsValid(0, &merklesignature.Signature{}, true)
a.ErrorIs(err, merklesignature.ErrSignatureSchemeVerificationFailed)

Expand Down Expand Up @@ -631,6 +632,13 @@ func TestBuilder_BuildStateProofCache(t *testing.T) {
return
}

// Verifies that the VotersAllocBound constant is equal to the current consensus parameters.
// It is used for msgpack allocbound (needs to be static)
func TestBuilder_StateProofTopVoters(t *testing.T) {
partitiontest.PartitionTest(t)
require.Equal(t, config.Consensus[protocol.ConsensusCurrentVersion].StateProofTopVoters, uint64(VotersAllocBound))
}

func BenchmarkBuildVerify(b *testing.B) {
totalWeight := 1000000
npart := 1000
Expand Down