From 7fc051cf9b059edac8636c0a132126b4e90bf22e Mon Sep 17 00:00:00 2001 From: p4u Date: Fri, 21 Jun 2024 13:06:32 +0200 Subject: [PATCH] vochain: allow define root (if it is the same) on set_process_census tx if dynamicCensus=false Signed-off-by: p4u --- vochain/process_test.go | 52 +++++++++++++++++++++++++++++++++++++++- vochain/state/process.go | 6 +++-- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/vochain/process_test.go b/vochain/process_test.go index 0edc64659..9370a01b6 100644 --- a/vochain/process_test.go +++ b/vochain/process_test.go @@ -579,13 +579,32 @@ func TestSetProcessCensusSize(t *testing.T) { qt.Assert(t, proc.MaxCensusSize, qt.Equals, uint64(10)) qt.Assert(t, proc.CensusRoot, qt.IsNotNil) + // Set census size (with same root and no URI) (should work) + qt.Assert(t, testSetProcessCensus(t, pid, accounts[0], app, proc.CensusRoot, nil, 12), qt.IsNil) + app.AdvanceTestBlock() + + proc, err = app.State.Process(pid, true) + qt.Assert(t, err, qt.IsNil) + qt.Assert(t, proc.MaxCensusSize, qt.Equals, uint64(12)) + qt.Assert(t, proc.CensusRoot, qt.IsNotNil) + + // Set census size (with same root and different URI) (should fail) + uri := "ipfs://987654321" + qt.Assert(t, testSetProcessCensus(t, pid, accounts[0], app, proc.CensusRoot, &uri, 13), qt.IsNotNil) + app.AdvanceTestBlock() + + proc, err = app.State.Process(pid, true) + qt.Assert(t, err, qt.IsNil) + qt.Assert(t, proc.MaxCensusSize, qt.Equals, uint64(12)) + qt.Assert(t, proc.CensusRoot, qt.IsNotNil) + // Set smaller census size (should fail) qt.Assert(t, testSetProcessCensus(t, pid, accounts[0], app, nil, nil, 5), qt.IsNotNil) app.AdvanceTestBlock() proc, err = app.State.Process(pid, true) qt.Assert(t, err, qt.IsNil) - qt.Assert(t, proc.MaxCensusSize, qt.Equals, uint64(10)) + qt.Assert(t, proc.MaxCensusSize, qt.Equals, uint64(12)) // Check cost is increased with larger census size (should work) account, err := app.State.GetAccount(accounts[0].Address(), true) @@ -600,6 +619,37 @@ func TestSetProcessCensusSize(t *testing.T) { // check that newBalance is at least 100 tokens less than oldBalance qt.Assert(t, oldBalance-newBalance >= 100, qt.IsTrue) + + // define a new process, this time with dynamicCensus=true + process = &models.Process{ + StartBlock: 0, + EnvelopeType: &models.EnvelopeType{EncryptedVotes: false}, + Mode: &models.ProcessMode{Interruptible: true, DynamicCensus: true}, + VoteOptions: &models.ProcessVoteOptions{MaxCount: 16, MaxValue: 16}, + Status: models.ProcessStatus_READY, + EntityId: accounts[0].Address().Bytes(), + CensusRoot: util.RandomBytes(32), + CensusURI: &censusURI, + CensusOrigin: models.CensusOrigin_OFF_CHAIN_TREE, + Duration: 60 * 60, + MaxCensusSize: 2, + } + + // create the process + pid = testCreateProcess(t, accounts[0], app, process) + app.AdvanceTestBlock() + + proc, err = app.State.Process(pid, true) + qt.Assert(t, err, qt.IsNil) + qt.Assert(t, proc.MaxCensusSize, qt.Equals, uint64(2)) + + // Set census size with root (should work since dynamicCensus=true) + qt.Assert(t, testSetProcessCensus(t, pid, accounts[0], app, util.RandomBytes(32), nil, 5), qt.IsNil) + app.AdvanceTestBlock() + + // Set census size with root (should work since dynamicCensus=true) + qt.Assert(t, testSetProcessCensus(t, pid, accounts[0], app, util.RandomBytes(32), &uri, 5), qt.IsNil) + app.AdvanceTestBlock() } func TestSetProcessDuration(t *testing.T) { diff --git a/vochain/state/process.go b/vochain/state/process.go index 0aba35455..a3b8383e4 100644 --- a/vochain/state/process.go +++ b/vochain/state/process.go @@ -367,8 +367,8 @@ func (v *State) SetProcessCensus(pid, censusRoot []byte, censusURI string, censu if err != nil { return err } - // check dynamic census only if root is being updated - if censusRoot != nil { + // check dynamic census only if root or uri are being updated + if (censusRoot != nil && !bytes.Equal(process.CensusRoot, censusRoot)) || (censusURI != "" && censusURI != *process.CensusURI) { if !process.Mode.DynamicCensus { return fmt.Errorf( "cannot update census, only processes with dynamic census can update their root") @@ -402,6 +402,8 @@ func (v *State) SetProcessCensus(pid, censusRoot []byte, censusURI string, censu if commit { if censusRoot != nil { process.CensusRoot = censusRoot + } + if censusURI != "" { process.CensusURI = &censusURI } if censusSize > 0 {