Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Make PrevStateRoot of models.Batch non-optional #605

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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 api/admin/get_pending_batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (a *API) GetPendingBatches(ctx context.Context) ([]dto.PendingBatch, error)
ID: batches[i].ID,
Type: batches[i].Type,
TransactionHash: batches[i].TransactionHash,
PrevStateRoot: *batches[i].PrevStateRoot,
PrevStateRoot: batches[i].PrevStateRoot,
Commitments: commitments,
})
}
Expand Down
12 changes: 6 additions & 6 deletions api/admin/get_pending_batches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ func (s *GetPendingBatchesTestSuite) SetupTest() {
ID: models.MakeUint256(1),
Type: batchtype.Create2Transfer,
TransactionHash: utils.RandomHash(),
PrevStateRoot: utils.NewRandomHash(),
PrevStateRoot: utils.RandomHash(),
}

s.batches = []models.Batch{
{
ID: models.MakeUint256(1),
Type: batchtype.Transfer,
TransactionHash: utils.RandomHash(),
PrevStateRoot: utils.NewRandomHash(),
PrevStateRoot: utils.RandomHash(),
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(42000),
MinedTime: models.NewTimestamp(time.Unix(140, 0).UTC()),
Expand All @@ -66,13 +66,13 @@ func (s *GetPendingBatchesTestSuite) SetupTest() {
ID: models.MakeUint256(2),
Type: batchtype.Create2Transfer,
TransactionHash: utils.RandomHash(),
PrevStateRoot: utils.NewRandomHash(),
PrevStateRoot: utils.RandomHash(),
},
{
ID: models.MakeUint256(3),
Type: batchtype.MassMigration,
TransactionHash: utils.RandomHash(),
PrevStateRoot: utils.NewRandomHash(),
PrevStateRoot: utils.RandomHash(),
},
}
}
Expand All @@ -94,7 +94,7 @@ func (s *GetPendingBatchesTestSuite) TestGetPendingBatches_DifferentBatchTypes()
s.Equal(expected[i].ID, batches[i].ID)
s.Equal(expected[i].Type, batches[i].Type)
s.Equal(expected[i].TransactionHash, batches[i].TransactionHash)
s.Equal(*expected[i].PrevStateRoot, batches[i].PrevStateRoot)
s.Equal(expected[i].PrevStateRoot, batches[i].PrevStateRoot)
}
}

Expand Down Expand Up @@ -282,7 +282,7 @@ func newPendingBatch(batch *models.Batch, commitment models.Commitment, txs mode
ID: batch.ID,
Type: batch.Type,
TransactionHash: batch.TransactionHash,
PrevStateRoot: *batch.PrevStateRoot,
PrevStateRoot: batch.PrevStateRoot,
Commitments: []dto.PendingCommitment{
{
Commitment: commitment,
Expand Down
11 changes: 9 additions & 2 deletions api/calculate_transfer_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func (s *CalculateTransactionStatusTestSuite) TestCalculateTransactionStatus_TxI

func (s *CalculateTransactionStatusTestSuite) TestCalculateTransactionStatus_TxInPendingBatch() {
batch := models.Batch{
ID: models.MakeUint256(1),
ID: models.MakeUint256(1),
PrevStateRoot: utils.RandomHash(),
}
err := s.storage.AddBatch(&batch)
s.NoError(err)
Expand All @@ -101,11 +102,14 @@ func (s *CalculateTransactionStatusTestSuite) TestCalculateTransactionStatus_TxI
}

func (s *CalculateTransactionStatusTestSuite) TestCalculateTransactionStatus_InBatch() {
root, err := s.storage.Storage.StateTree.Root()
s.NoError(err)
batch := models.Batch{
ID: models.MakeUint256(1),
FinalisationBlock: ref.Uint32(math.MaxUint32),
PrevStateRoot: *root,
}
err := s.storage.AddBatch(&batch)
err = s.storage.AddBatch(&batch)
s.NoError(err)

s.transfer.CommitmentID = &models.CommitmentID{
Expand All @@ -122,9 +126,12 @@ func (s *CalculateTransactionStatusTestSuite) TestCalculateTransactionStatus_InB
func (s *CalculateTransactionStatusTestSuite) TestCalculateTransactionStatus_Finalised() {
currentBlockNumber, err := s.sim.GetLatestBlockNumber()
s.NoError(err)
prevStateRoot, err := s.storage.StateTree.Root()
s.NoError(err)
batch := models.Batch{
ID: models.MakeUint256(1),
FinalisationBlock: ref.Uint32(uint32(*currentBlockNumber) + 1),
PrevStateRoot: *prevStateRoot,
}
err = s.storage.AddBatch(&batch)
s.NoError(err)
Expand Down
5 changes: 5 additions & 0 deletions api/get_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (s *GetBatchTestSuite) SetupTest() {
s.testClient, err = eth.NewTestClient()
s.NoError(err)
s.api = &API{storage: s.storage.Storage, client: s.testClient.Client}
prevStateRoot, err := s.storage.StateTree.Root()
s.NoError(err)

s.batch = &models.Batch{
ID: models.MakeUint256(1),
Expand All @@ -49,6 +51,7 @@ func (s *GetBatchTestSuite) SetupTest() {
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(42000),
AccountTreeRoot: utils.NewRandomHash(),
PrevStateRoot: *prevStateRoot,
MinedTime: models.NewTimestamp(time.Unix(140, 0).UTC()),
}

Expand Down Expand Up @@ -178,6 +181,7 @@ func (s *GetBatchTestSuite) TestGetBatchByHash_GenesisBatch() {
TransactionHash: utils.RandomHash(),
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(10),
PrevStateRoot: utils.RandomHash(),
}
err := s.storage.AddBatch(&genesisBatch)
s.NoError(err)
Expand Down Expand Up @@ -252,6 +256,7 @@ func (s *GetBatchTestSuite) TestGetBatchByID_GenesisBatch() {
TransactionHash: utils.RandomHash(),
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(10),
PrevStateRoot: utils.RandomHash(),
}
err := s.storage.AddBatch(&genesisBatch)
s.NoError(err)
Expand Down
5 changes: 5 additions & 0 deletions api/get_batches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (s *GetBatchesTestSuite) SetupTest() {
TransactionHash: utils.RandomHash(),
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(42000),
PrevStateRoot: utils.RandomHash(),
},
{
ID: models.MakeUint256(1),
Expand All @@ -53,6 +54,7 @@ func (s *GetBatchesTestSuite) SetupTest() {
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(42000),
MinedTime: models.NewTimestamp(time.Unix(140, 0).UTC()),
PrevStateRoot: utils.RandomHash(),
},
{
ID: models.MakeUint256(2),
Expand All @@ -61,6 +63,7 @@ func (s *GetBatchesTestSuite) SetupTest() {
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(43000),
MinedTime: models.NewTimestamp(time.Unix(150, 0).UTC()),
PrevStateRoot: utils.RandomHash(),
},
{
ID: models.MakeUint256(3),
Expand All @@ -69,6 +72,7 @@ func (s *GetBatchesTestSuite) SetupTest() {
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(44000),
MinedTime: models.NewTimestamp(time.Unix(160, 0).UTC()),
PrevStateRoot: utils.RandomHash(),
},
{
ID: models.MakeUint256(4),
Expand All @@ -77,6 +81,7 @@ func (s *GetBatchesTestSuite) SetupTest() {
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(44000),
MinedTime: models.NewTimestamp(time.Unix(160, 0).UTC()),
PrevStateRoot: utils.RandomHash(),
},
}
}
Expand Down
1 change: 1 addition & 0 deletions api/get_commitment_proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (s *GetCommitmentProofTestSuite) SetupTest() {
FinalisationBlock: ref.Uint32(113),
MinedTime: models.NewTimestamp(time.Unix(140, 0).UTC()),
AccountTreeRoot: utils.NewRandomHash(),
PrevStateRoot: utils.RandomHash(),
}

s.txCommitment = commitment
Expand Down
1 change: 1 addition & 0 deletions api/get_commitment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (s *GetCommitmentTestSuite) SetupTest() {
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(113),
MinedTime: models.NewTimestamp(time.Unix(140, 0).UTC()),
PrevStateRoot: utils.RandomHash(),
}

s.txCommitment = &models.TxCommitment{
Expand Down
6 changes: 4 additions & 2 deletions api/get_mass_migration_commitment_proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (s *GetMassMigrationCommitmentProofTestSuite) SetupTest() {
Hash: ref.Hash(utils.RandomHash()),
FinalisationBlock: ref.Uint32(10),
AccountTreeRoot: &accountTreeRoot,
PrevStateRoot: *stateRoot1,
}

err = s.storage.AddBatch(s.batch)
Expand Down Expand Up @@ -138,8 +139,9 @@ func (s *GetMassMigrationCommitmentProofTestSuite) TestGetMassMigrationCommitmen

func (s *GetMassMigrationCommitmentProofTestSuite) TestGetMassMigrationCommitmentProof_NotMassMigrationCommitment() {
err := s.storage.AddBatch(&models.Batch{
ID: models.MakeUint256(2),
Type: batchtype.Transfer,
ID: models.MakeUint256(2),
Type: batchtype.Transfer,
PrevStateRoot: utils.RandomHash(),
})
s.NoError(err)

Expand Down
4 changes: 4 additions & 0 deletions api/get_network_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ func (s *NetworkInfoTestSuite) TestGetNetworkInfo_NoFinalisedBatches() {
TransactionHash: utils.RandomHash(),
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(1234),
PrevStateRoot: utils.RandomHash(),
},
{
ID: models.MakeUint256(2000),
Type: batchtype.Create2Transfer,
TransactionHash: utils.RandomHash(),
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(2000),
PrevStateRoot: utils.RandomHash(),
},
}
err := s.api.storage.AddBatch(&batches[0])
Expand All @@ -91,13 +93,15 @@ func (s *NetworkInfoTestSuite) TestGetNetworkInfo() {
TransactionHash: utils.RandomHash(),
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(1),
PrevStateRoot: utils.RandomHash(),
},
{
ID: models.MakeUint256(2000),
Type: batchtype.Create2Transfer,
TransactionHash: utils.RandomHash(),
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(2000),
PrevStateRoot: utils.RandomHash(),
},
}
err := s.api.storage.AddBatch(&batches[0])
Expand Down
10 changes: 6 additions & 4 deletions api/get_withdraw_proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ func (s *GetWithdrawProofTestSuite) SetupTest() {
s.NoError(err)

err = s.storage.AddBatch(&models.Batch{
ID: models.MakeUint256(1),
Type: batchtype.MassMigration,
ID: models.MakeUint256(1),
Type: batchtype.MassMigration,
PrevStateRoot: *stateRoot,
})
s.NoError(err)
}
Expand Down Expand Up @@ -136,8 +137,9 @@ func (s *GetWithdrawProofTestSuite) TestGetWithdrawProof_InvalidBatchType() {
}

err := s.storage.AddBatch(&models.Batch{
ID: commitmentID.BatchID,
Type: batchtype.Transfer,
ID: commitmentID.BatchID,
Type: batchtype.Transfer,
PrevStateRoot: utils.RandomHash(),
})
s.NoError(err)

Expand Down
2 changes: 1 addition & 1 deletion commander/disputer/context_test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (s *testSuiteWithContexts) addGenesisBatch(root *common.Hash) {
s.NoError(err)

batch := contractBatch.ToModelBatch()
batch.PrevStateRoot = root
batch.PrevStateRoot = *root
err = s.storage.AddBatch(batch)
s.NoError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion commander/disputer/dispute_transition_test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *disputeTransitionTestSuite) submitInvalidBatch(txs models.GenericTransa

pendingBatch, err := s.txsCtx.NewPendingBatch(s.txsCtx.BatchType)
s.NoError(err)
fmt.Println(*pendingBatch.PrevStateRoot)
fmt.Println(pendingBatch.PrevStateRoot)

commitments, err := s.txsCtx.CreateCommitments()
s.NoError(err)
Expand Down
2 changes: 1 addition & 1 deletion commander/executor/create_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ func (c *ExecutionContext) NewPendingBatch(batchType batchtype.BatchType) (*mode
return &models.Batch{
ID: *batchID,
Type: batchType,
PrevStateRoot: prevStateRoot,
PrevStateRoot: *prevStateRoot,
}, nil
}
4 changes: 2 additions & 2 deletions commander/executor/execute_pending_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (c *TxsContext) ExecutePendingBatch(batch *models.PendingBatch) error {
ID: batch.ID,
Type: batch.Type,
TransactionHash: batch.TransactionHash,
PrevStateRoot: &batch.PrevStateRoot,
PrevStateRoot: batch.PrevStateRoot,
})
if err != nil {
return err
Expand Down Expand Up @@ -73,7 +73,7 @@ func (c *DepositsContext) ExecutePendingBatch(batch *models.PendingBatch) error
ID: batch.ID,
Type: batch.Type,
TransactionHash: batch.TransactionHash,
PrevStateRoot: &batch.PrevStateRoot,
PrevStateRoot: batch.PrevStateRoot,
})
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion commander/executor/execute_pending_deposit_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (s *ExecutePendingDepositBatchTestSuite) TestExecutePendingBatch_AddsBatch(
ID: s.pendingBatch.ID,
Type: s.pendingBatch.Type,
TransactionHash: s.pendingBatch.TransactionHash,
PrevStateRoot: &s.pendingBatch.PrevStateRoot,
PrevStateRoot: s.pendingBatch.PrevStateRoot,
}

batch, err := s.storage.GetBatch(s.pendingBatch.ID)
Expand Down
2 changes: 1 addition & 1 deletion commander/executor/execute_pending_tx_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (s *ExecutePendingTxBatchTestSuite) TestExecutePendingBatch_AddsPendingBatc
ID: s.pendingBatch.ID,
Type: s.pendingBatch.Type,
TransactionHash: s.pendingBatch.TransactionHash,
PrevStateRoot: &s.pendingBatch.PrevStateRoot,
PrevStateRoot: s.pendingBatch.PrevStateRoot,
}

batch, err := s.storage.GetBatch(s.pendingBatch.ID)
Expand Down
2 changes: 1 addition & 1 deletion commander/executor/revert_batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func (c *ExecutionContext) RevertBatches(startBatch *models.Batch) error {
err := c.storage.StateTree.RevertTo(*startBatch.PrevStateRoot)
err := c.storage.StateTree.RevertTo(startBatch.PrevStateRoot)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion commander/executor/submit_deposit_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (s *SubmitDepositBatchTestSuite) addGenesisBatch() {
s.NoError(err)

batch := contractBatch.ToModelBatch()
batch.PrevStateRoot = root
batch.PrevStateRoot = *root
err = s.storage.AddBatch(batch)
s.NoError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion commander/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (c *Commander) addGenesisBatch() error {
return err
}
batch = contractBatch.ToModelBatch()
batch.PrevStateRoot = root
batch.PrevStateRoot = *root

return c.storage.AddBatch(batch)
}
4 changes: 2 additions & 2 deletions commander/prover/commitment_proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ func (c *Context) genesisBatchCommitmentInclusionProof() (*models.CommitmentIncl
}

return createCommitmentInclusionProof(
[]common.Hash{*previousBatch.PrevStateRoot},
[]common.Hash{previousBatch.PrevStateRoot},
0,
*previousBatch.PrevStateRoot,
previousBatch.PrevStateRoot,
merkletree.GetZeroHash(0),
)
}
Expand Down
6 changes: 4 additions & 2 deletions commander/prover/commitment_proofs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (s *CommitmentProofsTestSuite) TestPreviousCommitmentInclusionProof_Previou
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(10),
AccountTreeRoot: utils.NewRandomHash(),
PrevStateRoot: utils.RandomHash(),
}
err := s.storage.AddBatch(&batch)
s.NoError(err)
Expand Down Expand Up @@ -157,6 +158,7 @@ func (s *CommitmentProofsTestSuite) TestPreviousCommitmentInclusionProof_Previou
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(10),
AccountTreeRoot: utils.NewRandomHash(),
PrevStateRoot: utils.RandomHash(),
}
err := s.storage.AddBatch(&batch)
s.NoError(err)
Expand Down Expand Up @@ -209,7 +211,7 @@ func (s *CommitmentProofsTestSuite) TestGenesisBatchCommitmentInclusionProof() {

expected := models.CommitmentInclusionProof{
CommitmentInclusionProofBase: models.CommitmentInclusionProofBase{
StateRoot: *genesisBatch.PrevStateRoot,
StateRoot: genesisBatch.PrevStateRoot,
Path: &models.MerklePath{
Path: 0,
Depth: 2,
Expand Down Expand Up @@ -302,7 +304,7 @@ func (s *CommitmentProofsTestSuite) addGenesisBatch() *models.Batch {
Type: batchtype.Genesis,
TransactionHash: common.Hash{},
Hash: utils.NewRandomHash(),
PrevStateRoot: root,
PrevStateRoot: *root,
}

err = s.storage.AddBatch(batch)
Expand Down
Loading