Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
qianbin committed Jan 28, 2024
1 parent 16fe3fc commit cf8f5aa
Show file tree
Hide file tree
Showing 27 changed files with 250 additions and 496 deletions.
8 changes: 4 additions & 4 deletions api/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func New(

func (a *Accounts) getCode(addr thor.Address, summary *chain.BlockSummary) ([]byte, error) {
code, err := a.stater.
NewState(summary.Header.StateRoot(), summary.Header.Number(), summary.Conflicts, summary.SteadyNum).
NewState(summary.Root()).
GetCode(addr)
if err != nil {
return nil, err
Expand All @@ -74,7 +74,7 @@ func (a *Accounts) handleGetCode(w http.ResponseWriter, req *http.Request) error
}

func (a *Accounts) getAccount(addr thor.Address, summary *chain.BlockSummary) (*Account, error) {
state := a.stater.NewState(summary.Header.StateRoot(), summary.Header.Number(), summary.Conflicts, summary.SteadyNum)
state := a.stater.NewState(summary.Root())
b, err := state.GetBalance(addr)
if err != nil {
return nil, err
Expand All @@ -97,7 +97,7 @@ func (a *Accounts) getAccount(addr thor.Address, summary *chain.BlockSummary) (*

func (a *Accounts) getStorage(addr thor.Address, key thor.Bytes32, summary *chain.BlockSummary) (thor.Bytes32, error) {
storage, err := a.stater.
NewState(summary.Header.StateRoot(), summary.Header.Number(), summary.Conflicts, summary.SteadyNum).
NewState(summary.Root()).
GetStorage(addr, key)

if err != nil {
Expand Down Expand Up @@ -200,7 +200,7 @@ func (a *Accounts) batchCall(ctx context.Context, batchCallData *BatchCallData,
return nil, err
}
header := summary.Header
state := a.stater.NewState(header.StateRoot(), header.Number(), summary.Conflicts, summary.SteadyNum)
state := a.stater.NewState(summary.Root())

signer, _ := header.Signer()
rt := runtime.New(a.repo.NewChain(header.ParentID()), state,
Expand Down
2 changes: 1 addition & 1 deletion api/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (d *Debug) handleTraceCall(w http.ResponseWriter, req *http.Request) error

func (d *Debug) traceCall(ctx context.Context, tracer tracers.Tracer, summary *chain.BlockSummary, txCtx *xenv.TransactionContext, gas uint64, clause *tx.Clause) (interface{}, error) {
header := summary.Header
state := d.stater.NewState(header.StateRoot(), header.Number(), summary.Conflicts, summary.SteadyNum)
state := d.stater.NewState(summary.Root())
signer, _ := header.Signer()
rt := runtime.New(
d.repo.NewChain(header.ID()),
Expand Down
16 changes: 8 additions & 8 deletions api/transactions/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (t *Transactions) getRawTransaction(txID thor.Bytes32, head thor.Bytes32, a
return nil, err
}

summary, err := t.repo.GetBlockSummary(meta.BlockID)
header, err := chain.GetBlockHeader(meta.BlockNum)
if err != nil {
return nil, err
}
Expand All @@ -62,9 +62,9 @@ func (t *Transactions) getRawTransaction(txID thor.Bytes32, head thor.Bytes32, a
return &rawTransaction{
RawTx: RawTx{hexutil.Encode(raw)},
Meta: &TxMeta{
BlockID: summary.Header.ID(),
BlockNumber: summary.Header.Number(),
BlockTimestamp: summary.Header.Timestamp(),
BlockID: header.ID(),
BlockNumber: header.Number(),
BlockTimestamp: header.Timestamp(),
},
}, nil
}
Expand All @@ -84,11 +84,11 @@ func (t *Transactions) getTransactionByID(txID thor.Bytes32, head thor.Bytes32,
return nil, err
}

summary, err := t.repo.GetBlockSummary(meta.BlockID)
header, err := chain.GetBlockHeader(meta.BlockNum)
if err != nil {
return nil, err
}
return convertTransaction(tx, summary.Header), nil
return convertTransaction(tx, header), nil
}

// GetTransactionReceiptByID get tx's receipt
Expand All @@ -107,12 +107,12 @@ func (t *Transactions) getTransactionReceiptByID(txID thor.Bytes32, head thor.By
return nil, err
}

summary, err := t.repo.GetBlockSummary(meta.BlockID)
header, err := chain.GetBlockHeader(meta.BlockNum)
if err != nil {
return nil, err
}

return convertReceipt(receipt, summary.Header, tx)
return convertReceipt(receipt, header, tx)
}
func (t *Transactions) handleSendTransaction(w http.ResponseWriter, req *http.Request) error {
var rawTx *RawTx
Expand Down
2 changes: 1 addition & 1 deletion bft/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (engine *BFTEngine) findCheckpointByQuality(target uint32, finalized, paren
}

func (engine *BFTEngine) getMaxBlockProposers(sum *chain.BlockSummary) (uint64, error) {
state := engine.stater.NewState(sum.Header.StateRoot(), sum.Header.Number(), sum.Conflicts, sum.SteadyNum)
state := engine.stater.NewState(sum.Root())
params, err := builtin.Params.Native(state).Get(thor.KeyMaxBlockProposers)
if err != nil {
return 0, err
Expand Down
2 changes: 1 addition & 1 deletion builtin/authority/authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func M(a ...interface{}) []interface{} {

func TestAuthority(t *testing.T) {
db := muxdb.NewMem()
st := state.New(db, thor.Bytes32{}, 0, 0, 0)
st := state.New(db, thor.Bytes32{}, 0)

Check failure on line 24 in builtin/authority/authority_test.go

View workflow job for this annotation

GitHub Actions / test_coverage

too many arguments in call to state.New

Check failure on line 24 in builtin/authority/authority_test.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.21.x, ubuntu-latest)

too many arguments in call to state.New

Check failure on line 24 in builtin/authority/authority_test.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.19.x, ubuntu-latest)

too many arguments in call to state.New

Check failure on line 24 in builtin/authority/authority_test.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.20.x, ubuntu-latest)

too many arguments in call to state.New

p1 := thor.BytesToAddress([]byte("p1"))
p2 := thor.BytesToAddress([]byte("p2"))
Expand Down
4 changes: 2 additions & 2 deletions builtin/energy/energy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func M(a ...interface{}) []interface{} {

func TestEnergy(t *testing.T) {
db := muxdb.NewMem()
st := state.New(db, thor.Bytes32{}, 0, 0, 0)
st := state.New(db, thor.Bytes32{}, 0)

Check failure on line 24 in builtin/energy/energy_test.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.21.x, ubuntu-latest)

too many arguments in call to state.New

Check failure on line 24 in builtin/energy/energy_test.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.19.x, ubuntu-latest)

too many arguments in call to state.New

Check failure on line 24 in builtin/energy/energy_test.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.20.x, ubuntu-latest)

too many arguments in call to state.New

acc := thor.BytesToAddress([]byte("a1"))

Expand All @@ -44,7 +44,7 @@ func TestEnergy(t *testing.T) {

func TestEnergyGrowth(t *testing.T) {
db := muxdb.NewMem()
st := state.New(db, thor.Bytes32{}, 0, 0, 0)
st := state.New(db, thor.Bytes32{}, 0)

Check failure on line 47 in builtin/energy/energy_test.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.21.x, ubuntu-latest)

too many arguments in call to state.New

Check failure on line 47 in builtin/energy/energy_test.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.19.x, ubuntu-latest)

too many arguments in call to state.New

Check failure on line 47 in builtin/energy/energy_test.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.20.x, ubuntu-latest)

too many arguments in call to state.New

acc := thor.BytesToAddress([]byte("a1"))

Expand Down
2 changes: 1 addition & 1 deletion builtin/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func initExectorTest() *ctest {
})

repo, _ := chain.NewRepository(db, b0)
st := state.New(db, b0.Header().StateRoot(), 0, 0, 0)
st := state.New(db, b0.Header().StateRoot(), 0)

Check failure on line 76 in builtin/executor_test.go

View workflow job for this annotation

GitHub Actions / test_coverage

too many arguments in call to state.New

Check failure on line 76 in builtin/executor_test.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.21.x, ubuntu-latest)

too many arguments in call to state.New

Check failure on line 76 in builtin/executor_test.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.19.x, ubuntu-latest)

too many arguments in call to state.New

Check failure on line 76 in builtin/executor_test.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.20.x, ubuntu-latest)

too many arguments in call to state.New
chain := repo.NewChain(b0.Header().ID())

rt := runtime.New(chain, st, &xenv.BlockContext{Time: uint64(time.Now().Unix())}, thor.NoFork)
Expand Down
2 changes: 1 addition & 1 deletion builtin/params/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

func TestParamsGetSet(t *testing.T) {
db := muxdb.NewMem()
st := state.New(db, thor.Bytes32{}, 0, 0, 0)
st := state.New(db, thor.Bytes32{}, 0)

Check failure on line 20 in builtin/params/params_test.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.19.x, ubuntu-latest)

too many arguments in call to state.New
setv := big.NewInt(10)
key := thor.BytesToBytes32([]byte("key"))
p := New(thor.BytesToAddress([]byte("par")), st)
Expand Down
2 changes: 1 addition & 1 deletion builtin/prototype/prototype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func M(a ...interface{}) []interface{} {

func TestPrototype(t *testing.T) {
db := muxdb.NewMem()
st := state.New(db, thor.Bytes32{}, 0, 0, 0)
st := state.New(db, thor.Bytes32{}, 0)

Check failure on line 25 in builtin/prototype/prototype_test.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.19.x, ubuntu-latest)

too many arguments in call to state.New

proto := prototype.New(thor.BytesToAddress([]byte("proto")), st)
binding := proto.Bind(thor.BytesToAddress([]byte("binding")))
Expand Down
4 changes: 2 additions & 2 deletions builtin/prototype_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func init() {
}

env.UseGas(thor.SloadGas)
state := env.State().Checkout(summary.Header.StateRoot(), summary.Header.Number(), summary.Conflicts, summary.SteadyNum)
state := env.State().Checkout(summary.Root())

env.UseGas(thor.GetBalanceGas)
val, err := state.GetBalance(thor.Address(args.Self))
Expand Down Expand Up @@ -136,7 +136,7 @@ func init() {
}

env.UseGas(thor.SloadGas)
state := env.State().Checkout(summary.Header.StateRoot(), summary.Header.Number(), summary.Conflicts, summary.SteadyNum)
state := env.State().Checkout(summary.Root())

env.UseGas(thor.GetBalanceGas)
val, err := state.GetEnergy(thor.Address(args.Self), summary.Header.Timestamp())
Expand Down
8 changes: 4 additions & 4 deletions cmd/thor/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (n *Node) processBlock(newBlock *block.Block, stats *blockStats) (bool, err
}

// add the new block into repository
if err := n.repo.AddBlock(newBlock, receipts, conflicts); err != nil {
if err := n.repo.AddBlock(newBlock, receipts, conflicts, becomeNewBest); err != nil {
return errors.Wrap(err, "add block")
}

Expand All @@ -384,9 +384,9 @@ func (n *Node) processBlock(newBlock *block.Block, stats *blockStats) (bool, err
}

if becomeNewBest {
if err := n.repo.SetBestBlockID(newBlock.Header().ID()); err != nil {
return err
}
// if err := n.repo.SetBestBlockID(newBlock.Header().ID()); err != nil {
// return err
// }
n.processFork(newBlock, oldBest.Header.ID())
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/thor/node/packer_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (n *Node) pack(flow *packer.Flow) error {
}

// add the new block into repository
if err := n.repo.AddBlock(newBlock, receipts, conflicts); err != nil {
if err := n.repo.AddBlock(newBlock, receipts, conflicts, true); err != nil {
return errors.Wrap(err, "add block")
}

Expand All @@ -184,9 +184,9 @@ func (n *Node) pack(flow *packer.Flow) error {
}
}

if err := n.repo.SetBestBlockID(newBlock.Header().ID()); err != nil {
return err
}
// if err := n.repo.SetBestBlockID(newBlock.Header().ID()); err != nil {
// return err
// }

n.processFork(newBlock, oldBest.Header.ID())
commitElapsed := mclock.Now() - startTime - execElapsed
Expand Down
93 changes: 23 additions & 70 deletions cmd/thor/optimizer/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
"math"
"time"

"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/common"
"github.com/inconshreveable/log15"
"github.com/pkg/errors"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/co"
"github.com/vechain/thor/v2/muxdb"
Expand Down Expand Up @@ -69,8 +68,8 @@ func (p *Optimizer) loop(prune bool) error {
log.Info("optimizer started")

const (
period = 2000 // the period to update leafbank.
prunePeriod = 10000 // the period to prune tries.
period = 10000 // the period to update leafbank.
prunePeriod = 50000 // the period to prune tries.
pruneReserved = 70000 // must be > thor.MaxStateHistory
)

Expand All @@ -94,9 +93,9 @@ func (p *Optimizer) loop(prune bool) error {
startTime := time.Now().UnixNano()

// dump account/storage trie leaves into leafbank
if err := p.dumpStateLeaves(targetChain, status.Base, target); err != nil {
return errors.Wrap(err, "dump state trie leaves")
}
// if err := p.dumpStateLeaves(targetChain, status.Base, target); err != nil {
// return errors.Wrap(err, "dump state trie leaves")
// }

// prune index/account/storage tries
if prune && target > pruneReserved {
Expand All @@ -112,7 +111,7 @@ func (p *Optimizer) loop(prune bool) error {
lastLogTime = now
log.Info("optimized tries",
"range", fmt.Sprintf("#%v+%v", status.Base, target-status.Base),
"et", time.Duration(now-startTime),
"et", common.PrettyDuration(now-startTime),
)
}
status.Base = target
Expand All @@ -128,54 +127,14 @@ func (p *Optimizer) newStorageTrieIfUpdated(accLeaf *trie.Leaf, base uint32) *mu
return nil
}

var (
acc state.Account
meta state.AccountMetadata
)
if err := rlp.DecodeBytes(accLeaf.Value, &acc); err != nil {
acc, err := state.DecodeAccount(accLeaf.Value, accLeaf.Meta)
if err != nil {
panic(errors.Wrap(err, "decode account"))
}

if err := rlp.DecodeBytes(accLeaf.Meta, &meta); err != nil {
panic(errors.Wrap(err, "decode account metadata"))
}

if meta.StorageCommitNum >= base {
return p.db.NewTrie(
state.StorageTrieName(meta.StorageID),
thor.BytesToBytes32(acc.StorageRoot),
meta.StorageCommitNum,
meta.StorageDistinctNum,
)
}
return nil
}

// dumpStateLeaves dumps account/storage trie leaves updated within [base, target) into leafbank.
func (p *Optimizer) dumpStateLeaves(targetChain *chain.Chain, base, target uint32) error {
h, err := targetChain.GetBlockSummary(target - 1)
if err != nil {
return err
}
accTrie := p.db.NewTrie(state.AccountTrieName, h.Header.StateRoot(), h.Header.Number(), h.Conflicts)
accTrie.SetNoFillCache(true)

var sTries []*muxdb.Trie
if err := accTrie.DumpLeaves(p.ctx, base, h.Header.Number(), func(leaf *trie.Leaf) *trie.Leaf {
if sTrie := p.newStorageTrieIfUpdated(leaf, base); sTrie != nil {
sTries = append(sTries, sTrie)
}
return leaf
}); err != nil {
return err
}
for _, sTrie := range sTries {
sTrie.SetNoFillCache(true)
if err := sTrie.DumpLeaves(p.ctx, base, h.Header.Number(), func(leaf *trie.Leaf) *trie.Leaf {
return &trie.Leaf{Value: leaf.Value} // skip metadata to save space
}); err != nil {
return err
}
root := acc.StorageRoot()
if root.Ver.Major >= base {
return p.db.NewTrie(acc.StorageTrieName(), root)
}
return nil
}
Expand All @@ -188,19 +147,19 @@ func (p *Optimizer) dumpTrieNodes(targetChain *chain.Chain, base, target uint32)
}

// dump index trie
indexTrie := p.db.NewNonCryptoTrie(chain.IndexTrieName, trie.NonCryptoNodeHash, summary.Header.Number(), summary.Conflicts)
indexTrie := p.db.NewTrie(chain.IndexTrieName, summary.IndexRoot())
indexTrie.SetNoFillCache(true)

if err := indexTrie.DumpNodes(p.ctx, base, nil); err != nil {
if err := indexTrie.Checkpoint(p.ctx, base, nil); err != nil {
return err
}

// dump account trie
accTrie := p.db.NewTrie(state.AccountTrieName, summary.Header.StateRoot(), summary.Header.Number(), summary.Conflicts)
accTrie := p.db.NewTrie(state.AccountTrieName, summary.Root())
accTrie.SetNoFillCache(true)

var sTries []*muxdb.Trie
if err := accTrie.DumpNodes(p.ctx, base, func(leaf *trie.Leaf) {
if err := accTrie.Checkpoint(p.ctx, base, func(leaf *trie.Leaf) {
if sTrie := p.newStorageTrieIfUpdated(leaf, base); sTrie != nil {
sTries = append(sTries, sTrie)
}
Expand All @@ -211,7 +170,7 @@ func (p *Optimizer) dumpTrieNodes(targetChain *chain.Chain, base, target uint32)
// dump storage tries
for _, sTrie := range sTries {
sTrie.SetNoFillCache(true)
if err := sTrie.DumpNodes(p.ctx, base, nil); err != nil {
if err := sTrie.Checkpoint(p.ctx, base, nil); err != nil {
return err
}
}
Expand All @@ -225,11 +184,11 @@ func (p *Optimizer) pruneTries(targetChain *chain.Chain, base, target uint32) er
}

cleanBase := base
if base == 0 {
// keeps genesis state history like the previous version.
cleanBase = 1
}
if err := p.db.CleanTrieHistory(p.ctx, cleanBase, target); err != nil {
// if base == 0 {
// // keeps genesis state history like the previous version.
// cleanBase = 1
// }
if err := p.db.DeleteTrieHistoryNodes(p.ctx, cleanBase, target); err != nil {
return errors.Wrap(err, "clean trie history")
}
return nil
Expand All @@ -238,10 +197,6 @@ func (p *Optimizer) pruneTries(targetChain *chain.Chain, base, target uint32) er
// awaitUntilSteady waits until the target block number becomes almost final(steady),
// and returns the steady chain.
func (p *Optimizer) awaitUntilSteady(target uint32) (*chain.Chain, error) {
// the knowned steady id is newer than target
if steadyID := p.repo.SteadyBlockID(); block.Number(steadyID) >= target {
return p.repo.NewChain(steadyID), nil
}

const windowSize = 100000

Expand Down Expand Up @@ -269,9 +224,7 @@ func (p *Optimizer) awaitUntilSteady(target uint32) (*chain.Chain, error) {
if len(set) >= int(math.Round((meanScore+1)/2)) {
// got enough unique signers
steadyID := prev.ID()
if err := p.repo.SetSteadyBlockID(steadyID); err != nil {
return nil, err
}

return p.repo.NewChain(steadyID), nil
}
parent, err := p.repo.GetBlockSummary(prev.ParentID())
Expand Down
Loading

0 comments on commit cf8f5aa

Please sign in to comment.