Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maindb v4 #635

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
9d3f299
cmd/thor: update instance dir to v4
qianbin Dec 14, 2023
4cc913e
trie: implement varint-prefix coder
qianbin Jan 9, 2024
8fc2d56
deps: add github.com/qianbin/drlp
qianbin Jan 9, 2024
af05e11
trie: implement appendHexToCompact & compactLen
qianbin Jan 9, 2024
2461c69
trie: temporarily remove merkle proof stuff
qianbin Jan 9, 2024
73cfd2f
trie: many changes
qianbin Jan 11, 2024
3a23814
trie: optimize full-node encoding/decoding
qianbin Jan 18, 2024
11a235a
trie: tweak shortnode encoding
qianbin Jan 19, 2024
cdd32e4
muxdb: move engine pkg
qianbin Jan 19, 2024
93877a5
trie: add Version() method for node interface
qianbin Jan 26, 2024
dd8ed4c
muxdb: refactor due to trie updates and:
qianbin Jan 26, 2024
7822724
chain: a lot of changes
qianbin Jan 28, 2024
27ba2e0
state: changes due to update of trie
qianbin Feb 1, 2024
20452bd
lowrlp: remove this pkg
qianbin Feb 1, 2024
4ef96d4
txpool: changes due to underlying pkg update
qianbin Feb 2, 2024
c14adb2
genesis: changes due to underlying pkg update
qianbin Feb 2, 2024
2aedac0
consensus: changes due to underlying pkg update
qianbin Feb 2, 2024
36efe1d
builtin: changes due to underlying pkg update
qianbin Feb 2, 2024
c181231
runtime: changes due to underlying pkg update
qianbin Feb 2, 2024
c823001
api: changes due to underlying pkg update
qianbin Feb 2, 2024
539a628
cmd/thor/pruner: rename pkg optimizer to pruner
qianbin Feb 2, 2024
01aba0e
cmd/thor: changes due to underlying pkg update
qianbin Feb 2, 2024
cd8124f
muxdb: abandon leaf filter
qianbin Feb 12, 2024
7a49eeb
cmd/thor/pruner: use smaller period when nearly synced
qianbin Feb 12, 2024
e76952e
muxdb: improve trie node path encoding
qianbin Feb 12, 2024
316a8c4
trie: treat short nodes as standalone nodes when skipping hash
qianbin Feb 12, 2024
1862da6
cmd/thor: fix disablePrunerFlag not work
qianbin Feb 13, 2024
9ebaa98
trie: improve refNode encoding/decoding
qianbin Feb 19, 2024
439b51c
muxdb: improve history node key encoding
qianbin Feb 19, 2024
cce009e
cmd/thor: adjust pruner parameters
qianbin Feb 20, 2024
56386a2
build: fix test cases
qianbin Feb 21, 2024
c2eb3a6
lint: fix lint error
qianbin Feb 21, 2024
9f72ae1
muxdb: fix ver encoding in node blob cache
qianbin Feb 28, 2024
631fe55
muxdb: add test cases for cache
qianbin Feb 28, 2024
0ebf3e7
runtime: fix test compile error
qianbin Mar 25, 2024
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
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/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func packTx(repo *chain.Repository, stater *state.Stater, transaction *tx.Transa
if _, err := stage.Commit(); err != nil {
t.Fatal(err)
}
if err := repo.AddBlock(b, receipts, 0); err != nil {
if err := repo.AddBlock(b, receipts, 0, false); err != nil {
t.Fatal(err)
}
if err := repo.SetBestBlockID(b.Header().ID()); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func initBlockServer(t *testing.T) {
if _, err := stage.Commit(); err != nil {
t.Fatal(err)
}
if err := repo.AddBlock(block, receipts, 0); err != nil {
if err := repo.AddBlock(block, receipts, 0, false); err != nil {
t.Fatal(err)
}
if err := repo.SetBestBlockID(block.Header().ID()); err != nil {
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 api/transactions/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func initTransactionServer(t *testing.T) {
if _, err := stage.Commit(); err != nil {
t.Fatal(err)
}
if err := repo.AddBlock(b, receipts, 0); err != nil {
if err := repo.AddBlock(b, receipts, 0, false); err != nil {
t.Fatal(err)
}
if err := repo.SetBestBlockID(b.Header().ID()); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion bft/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,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 bft/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (test *TestBFT) newBlock(parentSummary *chain.BlockSummary, master genesis.
return nil, err
}

if err = test.repo.AddBlock(b, nil, conflicts); err != nil {
if err = test.repo.AddBlock(b, nil, conflicts, false); err != nil {
return nil, err
}

Expand Down
3 changes: 2 additions & 1 deletion builtin/authority/authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/vechain/thor/v2/muxdb"
"github.com/vechain/thor/v2/state"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/trie"
)

func M(a ...interface{}) []interface{} {
Expand All @@ -21,7 +22,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, trie.Root{})

p1 := thor.BytesToAddress([]byte("p1"))
p2 := thor.BytesToAddress([]byte("p2"))
Expand Down
15 changes: 8 additions & 7 deletions builtin/energy/energy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/vechain/thor/v2/muxdb"
"github.com/vechain/thor/v2/state"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/trie"
)

func M(a ...interface{}) []interface{} {
Expand All @@ -21,7 +22,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, trie.Root{})

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

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

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

eng := New(thor.BytesToAddress([]byte("eng")), st, 0)

Expand All @@ -64,7 +65,7 @@ func TestInitialSupply(t *testing.T) {

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

eng := New(thor.BytesToAddress([]byte("a1")), st, 0)

Expand All @@ -78,7 +79,7 @@ func TestInitialSupplyError(t *testing.T) {

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

eng := New(thor.BytesToAddress([]byte("eng")), st, 0)

Expand All @@ -92,7 +93,7 @@ func TestTotalSupply(t *testing.T) {

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

eng := New(thor.BytesToAddress([]byte("eng")), st, 0)

Expand All @@ -106,7 +107,7 @@ func TestTokenTotalSupply(t *testing.T) {

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

eng := New(thor.BytesToAddress([]byte("eng")), st, 0)

Expand All @@ -120,7 +121,7 @@ func TestTotalBurned(t *testing.T) {

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

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

Expand Down
3 changes: 2 additions & 1 deletion builtin/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/vechain/thor/v2/runtime"
"github.com/vechain/thor/v2/state"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/trie"
"github.com/vechain/thor/v2/tx"
"github.com/vechain/thor/v2/xenv"
)
Expand Down Expand Up @@ -73,7 +74,7 @@ func initExectorTest() *ctest {
})

repo, _ := chain.NewRepository(db, b0)
st := state.New(db, b0.Header().StateRoot(), 0, 0, 0)
st := state.New(db, trie.Root{Hash: b0.Header().StateRoot()})
chain := repo.NewChain(b0.Header().ID())

rt := runtime.New(chain, st, &xenv.BlockContext{Time: uint64(time.Now().Unix())}, thor.NoFork)
Expand Down
35 changes: 18 additions & 17 deletions builtin/native_calls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/vechain/thor/v2/runtime"
"github.com/vechain/thor/v2/state"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/trie"
"github.com/vechain/thor/v2/tx"
"github.com/vechain/thor/v2/vm"
"github.com/vechain/thor/v2/xenv"
Expand Down Expand Up @@ -121,7 +122,7 @@ func (c *ccase) Assert(t *testing.T) *ccase {
assert.True(t, ok, "should have method")

constant := method.Const()
stage, err := c.rt.State().Stage(0, 0)
stage, err := c.rt.State().Stage(trie.Version{})
assert.Nil(t, err, "should stage state")
stateRoot := stage.Hash()

Expand All @@ -140,7 +141,7 @@ func (c *ccase) Assert(t *testing.T) *ccase {
vmout, _, err := exec()
assert.Nil(t, err)
if constant || vmout.VMErr != nil {
stage, err := c.rt.State().Stage(0, 0)
stage, err := c.rt.State().Stage(trie.Version{})
assert.Nil(t, err, "should stage state")
newStateRoot := stage.Hash()
assert.Equal(t, stateRoot, newStateRoot)
Expand Down Expand Up @@ -195,7 +196,7 @@ func TestParamsNative(t *testing.T) {
return nil
})
repo, _ := chain.NewRepository(db, b0)
st := state.New(db, b0.Header().StateRoot(), 0, 0, 0)
st := state.New(db, trie.Root{Hash: b0.Header().StateRoot()})
chain := repo.NewChain(b0.Header().ID())

rt := runtime.New(chain, st, &xenv.BlockContext{}, thor.NoFork)
Expand Down Expand Up @@ -263,7 +264,7 @@ func TestAuthorityNative(t *testing.T) {
return nil
})
repo, _ := chain.NewRepository(db, b0)
st := state.New(db, b0.Header().StateRoot(), 0, 0, 0)
st := state.New(db, trie.Root{Hash: b0.Header().StateRoot()})
chain := repo.NewChain(b0.Header().ID())

rt := runtime.New(chain, st, &xenv.BlockContext{}, thor.NoFork)
Expand Down Expand Up @@ -369,7 +370,7 @@ func TestEnergyNative(t *testing.T) {
})

repo, _ := chain.NewRepository(db, b0)
st := state.New(db, b0.Header().StateRoot(), 0, 0, 0)
st := state.New(db, trie.Root{Hash: b0.Header().StateRoot()})
chain := repo.NewChain(b0.Header().ID())

st.SetEnergy(addr, eng, b0.Header().Timestamp())
Expand Down Expand Up @@ -495,7 +496,7 @@ func TestPrototypeNative(t *testing.T) {
gene := genesis.NewDevnet()
genesisBlock, _, _, _ := gene.Build(state.NewStater(db))
repo, _ := chain.NewRepository(db, genesisBlock)
st := state.New(db, genesisBlock.Header().StateRoot(), 0, 0, 0)
st := state.New(db, trie.Root{Hash: genesisBlock.Header().StateRoot()})
chain := repo.NewChain(genesisBlock.Header().ID())

st.SetStorage(thor.Address(acc1), key, value)
Expand Down Expand Up @@ -768,14 +769,14 @@ func TestPrototypeNativeWithLongerBlockNumber(t *testing.T) {
db := muxdb.NewMem()
gene := genesis.NewDevnet()
genesisBlock, _, _, _ := gene.Build(state.NewStater(db))
st := state.New(db, genesisBlock.Header().StateRoot(), 0, 0, 0)
st := state.New(db, trie.Root{Hash: genesisBlock.Header().StateRoot()})
repo, _ := chain.NewRepository(db, genesisBlock)
launchTime := genesisBlock.Header().Timestamp()

for i := 1; i < 100; i++ {
st.SetBalance(acc1, big.NewInt(int64(i)))
st.SetEnergy(acc1, big.NewInt(int64(i)), launchTime+uint64(i)*10)
stage, _ := st.Stage(uint32(i), 0)
stage, _ := st.Stage(trie.Version{Major: uint32(i)})
stateRoot, _ := stage.Commit()
b := new(block.Builder).
ParentID(repo.BestBlockSummary().Header.ID()).
Expand All @@ -784,11 +785,11 @@ func TestPrototypeNativeWithLongerBlockNumber(t *testing.T) {
StateRoot(stateRoot).
Build().
WithSignature(sig[:])
repo.AddBlock(b, tx.Receipts{}, 0)
repo.AddBlock(b, tx.Receipts{}, 0, false)
repo.SetBestBlockID(b.Header().ID())
}

st = state.New(db, repo.BestBlockSummary().Header.StateRoot(), repo.BestBlockSummary().Header.Number(), 0, 0)
st = state.New(db, repo.BestBlockSummary().Root())
chain := repo.NewBestChain()

rt := runtime.New(chain, st, &xenv.BlockContext{
Expand Down Expand Up @@ -838,14 +839,14 @@ func TestPrototypeNativeWithBlockNumber(t *testing.T) {
db := muxdb.NewMem()
gene := genesis.NewDevnet()
genesisBlock, _, _, _ := gene.Build(state.NewStater(db))
st := state.New(db, genesisBlock.Header().StateRoot(), 0, 0, 0)
st := state.New(db, trie.Root{Hash: genesisBlock.Header().StateRoot()})
repo, _ := chain.NewRepository(db, genesisBlock)
launchTime := genesisBlock.Header().Timestamp()

for i := 1; i < 100; i++ {
st.SetBalance(acc1, big.NewInt(int64(i)))
st.SetEnergy(acc1, big.NewInt(int64(i)), launchTime+uint64(i)*10)
stage, _ := st.Stage(uint32(i), 0)
stage, _ := st.Stage(trie.Version{Major: uint32(i)})
stateRoot, _ := stage.Commit()
b := new(block.Builder).
ParentID(repo.BestBlockSummary().Header.ID()).
Expand All @@ -854,11 +855,11 @@ func TestPrototypeNativeWithBlockNumber(t *testing.T) {
StateRoot(stateRoot).
Build().
WithSignature(sig[:])
repo.AddBlock(b, tx.Receipts{}, 0)
repo.AddBlock(b, tx.Receipts{}, 0, false)
repo.SetBestBlockID(b.Header().ID())
}

st = state.New(db, repo.BestBlockSummary().Header.StateRoot(), repo.BestBlockSummary().Header.Number(), 0, repo.BestBlockSummary().SteadyNum)
st = state.New(db, repo.BestBlockSummary().Root())
chain := repo.NewBestChain()

rt := runtime.New(chain, st, &xenv.BlockContext{
Expand Down Expand Up @@ -898,7 +899,7 @@ func newBlock(parent *block.Block, score uint64, timestamp uint64, privateKey *e

func TestExtensionNative(t *testing.T) {
db := muxdb.NewMem()
st := state.New(db, thor.Bytes32{}, 0, 0, 0)
st := state.New(db, trie.Root{})
gene := genesis.NewDevnet()
genesisBlock, _, _, _ := gene.Build(state.NewStater(db))
repo, _ := chain.NewRepository(db, genesisBlock)
Expand All @@ -920,9 +921,9 @@ func TestExtensionNative(t *testing.T) {

gasPayer := thor.BytesToAddress([]byte("gasPayer"))

err := repo.AddBlock(b1, nil, 0)
err := repo.AddBlock(b1, nil, 0, false)
assert.Equal(t, err, nil)
err = repo.AddBlock(b2, nil, 0)
err = repo.AddBlock(b2, nil, 0, false)
assert.Equal(t, err, nil)

assert.Equal(t, builtin.Extension.Address, builtin.Extension.Address)
Expand Down
Loading
Loading