Skip to content

Commit

Permalink
test contract resolution and fix bug when updating resolved/valid sta…
Browse files Browse the repository at this point in the history
…tus of file contract
  • Loading branch information
chris124567 committed Mar 27, 2024
1 parent d425c3f commit 5d0bf01
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
14 changes: 5 additions & 9 deletions persist/sqlite/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,15 +544,16 @@ func (s *Store) addFileContractElements(dbTxn txn, bid types.BlockID, update con
stmt, err := dbTxn.Prepare(`INSERT INTO file_contract_elements(block_id, contract_id, leaf_index, merkle_proof, resolved, valid, filesize, file_merkle_root, window_start, window_end, payout, unlock_hash, revision_number)
VALUES (?, ?, ?, ?, FALSE, TRUE, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (contract_id, revision_number)
DO UPDATE SET resolved = ? AND valid = ?`)
DO UPDATE SET resolved = ?, valid = ?
RETURNING id;`)
if err != nil {
return nil, fmt.Errorf("addFileContractElements: failed to prepare file_contract_elements statement: %w", err)
}
defer stmt.Close()

revisionStmt, err := dbTxn.Prepare(`INSERT INTO last_contract_revision(contract_id, contract_element_id)
VALUES (?, ?)
ON CONFLICT
ON CONFLICT (contract_id)
DO UPDATE SET contract_element_id = ?`)
if err != nil {
return nil, fmt.Errorf("addFileContractElements: failed to prepare last_contract_revision statement: %w", err)
Expand All @@ -570,18 +571,13 @@ func (s *Store) addFileContractElements(dbTxn txn, bid types.BlockID, update con
fc = &rev.FileContract
}

result, err := stmt.Exec(dbEncode(bid), dbEncode(fce.StateElement.ID), dbEncode(fce.StateElement.LeafIndex), dbEncode(fce.StateElement.MerkleProof), fc.Filesize, dbEncode(fc.FileMerkleRoot), fc.WindowStart, fc.WindowEnd, dbEncode(fc.Payout), dbEncode(fc.UnlockHash), fc.RevisionNumber, resolved, valid)
var dbID int64
err := stmt.QueryRow(dbEncode(bid), dbEncode(fce.StateElement.ID), dbEncode(fce.StateElement.LeafIndex), dbEncode(fce.StateElement.MerkleProof), fc.Filesize, dbEncode(fc.FileMerkleRoot), fc.WindowStart, fc.WindowEnd, dbEncode(fc.Payout), dbEncode(fc.UnlockHash), fc.RevisionNumber, resolved, valid).Scan(&dbID)
if err != nil {
updateErr = fmt.Errorf("addFileContractElements: failed to execute file_contract_elements statement: %w", err)
return
}

dbID, err := result.LastInsertId()
if err != nil {
updateErr = fmt.Errorf("addFileContractElements: failed to get last insert ID: %w", err)
return
}

if _, err := revisionStmt.Exec(dbEncode(fce.StateElement.ID), dbID, dbID); err != nil {
updateErr = fmt.Errorf("addFileContractElements: failed to update last revision number: %w", err)
return
Expand Down
46 changes: 32 additions & 14 deletions persist/sqlite/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TestBalance(t *testing.T) {
check(t, "source", explorer.SourceMinerPayout, utxos[0].Source)

// Mine until the payout matures
for i := cm.TipState().Index.Height; i < maturityHeight; i++ {
for i := cm.Tip().Height; i < maturityHeight; i++ {
checkBalance(addr1, types.ZeroCurrency, expectedPayout, 0)
if err := cm.AddBlocks([]types.Block{mineBlock(cm.TipState(), nil, types.VoidAddress)}); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -328,7 +328,7 @@ func TestSendTransactions(t *testing.T) {
}

// Mine until the payout matures
for i := cm.TipState().Index.Height; i < maturityHeight; i++ {
for i := cm.Tip().Height; i < maturityHeight; i++ {
if err := cm.AddBlocks([]types.Block{mineBlock(cm.TipState(), nil, types.VoidAddress)}); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -496,7 +496,7 @@ func TestTip(t *testing.T) {
}

const n = 100
for i := cm.TipState().Index.Height; i < n; i++ {
for i := cm.Tip().Height; i < n; i++ {
if err := cm.AddBlocks([]types.Block{mineBlock(cm.TipState(), nil, types.VoidAddress)}); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -633,9 +633,9 @@ func TestFileContract(t *testing.T) {
}
}

checkFC := func(expected types.FileContract, got explorer.FileContract) {
check(t, "resolved state", false, got.Resolved)
check(t, "valid state", true, got.Valid)
checkFC := func(resolved, valid bool, expected types.FileContract, got explorer.FileContract) {
check(t, "resolved state", resolved, got.Resolved)
check(t, "valid state", valid, got.Valid)
check(t, "filesize", expected.Filesize, got.Filesize)
check(t, "file merkle root", expected.FileMerkleRoot, got.FileMerkleRoot)
check(t, "window start", expected.WindowStart, got.WindowStart)
Expand All @@ -655,7 +655,9 @@ func TestFileContract(t *testing.T) {
}
}

fc := prepareContractFormation(renterPublicKey, hostPublicKey, types.Siacoins(1), types.Siacoins(1), cm.Tip().Height+10, 100, types.VoidAddress)
windowStart := cm.Tip().Height + 10
windowEnd := windowStart + 10
fc := prepareContractFormation(renterPublicKey, hostPublicKey, types.Siacoins(1), types.Siacoins(1), windowStart, windowEnd, types.VoidAddress)
txn := types.Transaction{
SiacoinInputs: []types.SiacoinInput{{
ParentID: scOutputID,
Expand All @@ -667,19 +669,20 @@ func TestFileContract(t *testing.T) {
}},
FileContracts: []types.FileContract{fc},
}
fcID := txn.FileContractID(0)
signTxn(&txn)

if err := cm.AddBlocks([]types.Block{mineBlock(cm.TipState(), []types.Transaction{txn}, types.VoidAddress)}); err != nil {
t.Fatal(err)
}

{
dbFCs, err := db.Contracts([]types.FileContractID{txn.FileContractID(0)})
dbFCs, err := db.Contracts([]types.FileContractID{fcID})
if err != nil {
t.Fatal(err)
}
check(t, "fcs", 1, len(dbFCs))
checkFC(fc, dbFCs[0])
checkFC(false, true, fc, dbFCs[0])
}

{
Expand All @@ -689,7 +692,7 @@ func TestFileContract(t *testing.T) {
}
check(t, "transactions", 1, len(txns))
check(t, "file contracts", 1, len(txns[0].FileContracts))
checkFC(fc, txns[0].FileContracts[0])
checkFC(false, true, fc, txns[0].FileContracts[0])
}

uc := types.UnlockConditions{
Expand All @@ -702,7 +705,7 @@ func TestFileContract(t *testing.T) {
fc.RevisionNumber++
reviseTxn := types.Transaction{
FileContractRevisions: []types.FileContractRevision{{
ParentID: txn.FileContractID(0),
ParentID: fcID,
UnlockConditions: uc,
FileContract: fc,
}},
Expand All @@ -715,12 +718,12 @@ func TestFileContract(t *testing.T) {

// Explorer.Contracts should return latest revision
{
dbFCs, err := db.Contracts([]types.FileContractID{txn.FileContractID(0)})
dbFCs, err := db.Contracts([]types.FileContractID{fcID})
if err != nil {
t.Fatal(err)
}
check(t, "fcs", 1, len(dbFCs))
checkFC(fc, dbFCs[0])
checkFC(false, true, fc, dbFCs[0])
}

{
Expand All @@ -735,6 +738,21 @@ func TestFileContract(t *testing.T) {
check(t, "parent id", txn.FileContractID(0), fcr.ParentID)
check(t, "unlock conditions", uc, fcr.UnlockConditions)

checkFC(fc, fcr.FileContract)
checkFC(false, true, fc, fcr.FileContract)
}

for i := cm.Tip().Height; i < windowEnd+10; i++ {
if err := cm.AddBlocks([]types.Block{mineBlock(cm.TipState(), nil, types.VoidAddress)}); err != nil {
t.Fatal(err)
}
}

{
dbFCs, err := db.Contracts([]types.FileContractID{fcID})
if err != nil {
t.Fatal(err)
}
check(t, "fcs", 1, len(dbFCs))
checkFC(true, false, fc, dbFCs[0])
}
}

0 comments on commit 5d0bf01

Please sign in to comment.