Skip to content

Commit 8632a5f

Browse files
authored
Merge pull request #246 from SiaFoundation/christopher/fk-indices
Speed up reverts subtantially
2 parents cf9bbf6 + 45208e4 commit 8632a5f

File tree

8 files changed

+462
-126
lines changed

8 files changed

+462
-126
lines changed

internal/testutil/chain.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,24 @@ func MineBlock(state consensus.State, txns []types.Transaction, minerAddr types.
9999

100100
// MineV2Block mines sets the metadata fields of the block along with the
101101
// transactions and then generates a valid nonce for the block.
102-
func MineV2Block(state consensus.State, txns []types.V2Transaction, minerAddr types.Address) types.Block {
102+
func MineV2Block(state consensus.State, v1Txns []types.Transaction, v2Txns []types.V2Transaction, minerAddr types.Address) types.Block {
103103
reward := state.BlockReward()
104-
for _, txn := range txns {
104+
for _, txn := range v1Txns {
105+
for _, fee := range txn.MinerFees {
106+
reward = reward.Add(fee)
107+
}
108+
}
109+
for _, txn := range v2Txns {
105110
reward = reward.Add(txn.MinerFee)
106111
}
107112

108113
b := types.Block{
109114
ParentID: state.Index.ID,
110115
Timestamp: types.CurrentTimestamp(),
111116
MinerPayouts: []types.SiacoinOutput{{Address: minerAddr, Value: reward}},
112-
117+
Transactions: v1Txns,
113118
V2: &types.V2BlockData{
114-
Transactions: txns,
119+
Transactions: v2Txns,
115120
Height: state.Index.Height + 1,
116121
},
117122
}

persist/sqlite/consensus.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -740,11 +740,6 @@ func addEvents(tx *txn, bid types.BlockID, scDBIds map[types.SiacoinOutputID]int
740740
return nil
741741
}
742742

743-
func deleteBlock(tx *txn, bid types.BlockID) error {
744-
_, err := tx.Exec("DELETE FROM blocks WHERE id = ?", encode(bid))
745-
return err
746-
}
747-
748743
func updateFileContractElements(tx *txn, revert bool, index types.ChainIndex, b types.Block, fces []explorer.FileContractUpdate) (map[explorer.DBFileContract]int64, error) {
749744
stmt, err := tx.Prepare(`INSERT INTO file_contract_elements(contract_id, block_id, transaction_id, leaf_index, filesize, file_merkle_root, window_start, window_end, payout, unlock_hash, revision_number)
750745
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

persist/sqlite/consensus_refactored_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"math"
77
"path/filepath"
8+
"strings"
89
"testing"
910
"time"
1011

@@ -3046,3 +3047,110 @@ func BenchmarkAddressEvents(b *testing.B) {
30463047
runBenchmarkEvents(fmt.Sprintf("%d addresses and %d transactions per address", bm.addresses, bm.eventsPerAddress), bm.addresses, bm.eventsPerAddress)
30473048
}
30483049
}
3050+
3051+
func BenchmarkRevert(b *testing.B) {
3052+
pk1 := types.GeneratePrivateKey()
3053+
uc1 := types.StandardUnlockConditions(pk1.PublicKey())
3054+
addr1 := uc1.UnlockHash()
3055+
addr1Policy := types.SpendPolicy{Type: types.PolicyTypeUnlockConditions(uc1)}
3056+
3057+
n := newTestChain(b, false, func(network *consensus.Network, genesisBlock types.Block) {
3058+
network.HardforkV2.AllowHeight = 1
3059+
network.HardforkV2.RequireHeight = 1_000_000
3060+
genesisBlock.Transactions[0].SiacoinOutputs[0].Address = addr1
3061+
})
3062+
genesisTxn := n.genesis().Transactions[0]
3063+
3064+
val := genesisTxn.SiacoinOutputs[0].Value
3065+
scID := genesisTxn.SiacoinOutputID(0)
3066+
3067+
for i := range 20000 {
3068+
if i%100 == 0 {
3069+
b.Logf("Mined %d blocks", i)
3070+
}
3071+
3072+
// transaction with random arbitrary data
3073+
data := make([]byte, 16)
3074+
frand.Read(data)
3075+
txn1 := types.Transaction{
3076+
ArbitraryData: [][]byte{data},
3077+
}
3078+
3079+
fc := prepareContract(addr1, n.tipState().Index.Height+1)
3080+
// create file contract
3081+
txn2 := types.Transaction{
3082+
SiacoinInputs: []types.SiacoinInput{{
3083+
ParentID: scID,
3084+
UnlockConditions: uc1,
3085+
}},
3086+
SiacoinOutputs: []types.SiacoinOutput{{
3087+
Address: addr1,
3088+
Value: val.Sub(fc.Payout),
3089+
}},
3090+
FileContracts: []types.FileContract{fc},
3091+
}
3092+
testutil.SignTransaction(n.tipState(), pk1, &txn2)
3093+
3094+
scID = txn2.SiacoinOutputID(0)
3095+
val = txn2.SiacoinOutputs[0].Value
3096+
3097+
// txn3
3098+
txn3 := types.V2Transaction{
3099+
SiacoinInputs: []types.V2SiacoinInput{{
3100+
// Parent: getSCE(b, n.db, scID),
3101+
Parent: types.SiacoinElement{
3102+
ID: txn2.SiacoinOutputID(0),
3103+
StateElement: types.StateElement{
3104+
LeafIndex: types.UnassignedLeafIndex,
3105+
},
3106+
SiacoinOutput: txn2.SiacoinOutputs[0],
3107+
},
3108+
SatisfiedPolicy: types.SatisfiedPolicy{Policy: addr1Policy},
3109+
}},
3110+
SiacoinOutputs: []types.SiacoinOutput{{
3111+
Address: addr1,
3112+
Value: val,
3113+
}},
3114+
}
3115+
testutil.SignV2Transaction(n.tipState(), pk1, &txn3)
3116+
3117+
scID = txn3.SiacoinOutputID(txn3.ID(), 0)
3118+
val = txn3.SiacoinOutputs[0].Value
3119+
3120+
block := testutil.MineV2Block(n.tipState(), []types.Transaction{txn1, txn2}, []types.V2Transaction{txn3}, types.VoidAddress)
3121+
n.applyBlock(b, block)
3122+
}
3123+
3124+
block := n.blocks[len(n.blocks)-1]
3125+
bs := n.supplements[len(n.supplements)-1]
3126+
prevState := n.states[len(n.states)-2]
3127+
3128+
ru := consensus.RevertBlock(prevState, block, bs)
3129+
crus := []chain.RevertUpdate{{
3130+
RevertUpdate: ru,
3131+
Block: block,
3132+
State: prevState,
3133+
}}
3134+
3135+
b.ResetTimer()
3136+
for range b.N {
3137+
err := n.db.transaction(func(tx *txn) error {
3138+
defer tx.Rollback()
3139+
utx := &updateTx{
3140+
tx: tx,
3141+
}
3142+
3143+
b.StartTimer()
3144+
err := explorer.UpdateChainState(utx, crus, nil)
3145+
b.StopTimer()
3146+
3147+
if err != nil {
3148+
return fmt.Errorf("failed to update chain state: %w", err)
3149+
}
3150+
return nil
3151+
})
3152+
if err != nil && !strings.Contains(err.Error(), "rolled back") {
3153+
b.Fatal(err)
3154+
}
3155+
}
3156+
}

0 commit comments

Comments
 (0)