Skip to content

Commit

Permalink
vochain: allow define root (if it is the same) on set_process_census …
Browse files Browse the repository at this point in the history
…tx if dynamicCensus=false

Signed-off-by: p4u <[email protected]>
  • Loading branch information
p4u committed Jun 21, 2024
1 parent 59aa664 commit 7fc051c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
52 changes: 51 additions & 1 deletion vochain/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions vochain/state/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 7fc051c

Please sign in to comment.