Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
qianbin committed Jan 20, 2024
1 parent d095c80 commit ff122e4
Show file tree
Hide file tree
Showing 41 changed files with 824 additions and 1,755 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.Header.StateRoot(), summary.Version()).
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.Header.StateRoot(), summary.Version())
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.Header.StateRoot(), summary.Version()).
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.Header.StateRoot(), summary.Version())

signer, _ := header.Signer()
rt := runtime.New(a.repo.NewChain(header.ParentID()), state,
Expand Down
4 changes: 2 additions & 2 deletions 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.Header.StateRoot(), summary.Version())
signer, _ := header.Signer()
rt := runtime.New(
d.repo.NewChain(header.ID()),
Expand Down Expand Up @@ -272,7 +272,7 @@ func (d *Debug) debugStorage(ctx context.Context, contractAddress thor.Address,
}

func storageRangeAt(t *muxdb.Trie, start []byte, maxResult int) (*StorageRangeResult, error) {
it := trie.NewIterator(t.NodeIterator(start, 0))
it := trie.NewIterator(t.NodeIterator(start, trie.Version{}))
result := StorageRangeResult{Storage: StorageMap{}}
for i := 0; i < maxResult && it.Next(); i++ {
_, content, _, err := rlp.Split(it.Value)
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.Header.StateRoot(), sum.Version())
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

cannot use 0 (untyped int constant) as trie.Version value in argument 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)

cannot use 0 (untyped int constant) as trie.Version value in argument 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)

cannot use 0 (untyped int constant) as trie.Version value in argument 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)

cannot use 0 (untyped int constant) as trie.Version value in argument 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)

cannot use 0 (untyped int constant) as trie.Version value in argument 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)

cannot use 0 (untyped int constant) as trie.Version value in argument 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)

cannot use 0 (untyped int constant) as trie.Version value in argument 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)

cannot use 0 (untyped int constant) as trie.Version value in argument 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)

cannot use 0 (untyped int constant) as trie.Version value in argument 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)

cannot use 0 (untyped int constant) as trie.Version value in argument 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

cannot use 0 (untyped int constant) as "github.com/vechain/thor/v2/trie".Version value in argument 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)

cannot use 0 (untyped int constant) as "github.com/vechain/thor/v2/trie".Version value in argument 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)

cannot use 0 (untyped int constant) as "github.com/vechain/thor/v2/trie".Version value in argument 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)

cannot use 0 (untyped int constant) as trie.Version value in argument 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)

cannot use 0 (untyped int constant) as trie.Version value in argument 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.Header.StateRoot(), summary.Version())

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.Header.StateRoot(), summary.Version())

env.UseGas(thor.GetBalanceGas)
val, err := state.GetEnergy(thor.Address(args.Self), summary.Header.Timestamp())
Expand Down
42 changes: 20 additions & 22 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package chain

import (
"encoding/binary"
"fmt"
"math"
"sort"

Expand Down Expand Up @@ -64,9 +65,9 @@ func newChain(repo *Repository, headID thor.Bytes32) *Chain {
func() (*muxdb.Trie, error) {
if indexTrie == nil && initErr == nil {
if summary, err := repo.GetBlockSummary(headID); err == nil {
indexTrie = repo.db.NewNonCryptoTrie(IndexTrieName, trie.NonCryptoNodeHash, summary.Header.Number(), summary.Conflicts)
indexTrie = repo.db.NewTrie(IndexTrieName, thor.BytesToBytes32([]byte{1}), summary.Version())
} else {
initErr = errors.Wrap(err, "lazy init chain")
initErr = errors.Wrap(err, fmt.Sprintf("lazy init chain, head=%v", headID))
}
}
return indexTrie, initErr
Expand Down Expand Up @@ -116,23 +117,27 @@ func (c *Chain) GetTransactionMeta(id thor.Bytes32) (*TxMeta, error) {
iter := c.repo.txIndexer.Iterate(kv.Range(*util.BytesPrefix(id[:])))
defer iter.Release()
for iter.Next() {
if len(iter.Key()) != 64 { // skip the pure txid key
if len(iter.Key()) != 32+8 { // skip the pure txid key
continue
}

blockID := thor.BytesToBytes32(iter.Key()[32:])
major := binary.BigEndian.Uint32(iter.Key()[32:])
minor := binary.BigEndian.Uint32(iter.Key()[32+4:])

has, err := c.HasBlock(blockID)
bs, err := c.GetBlockSummary(major)
if err != nil {
return nil, err
if !c.IsNotFound(err) {
return nil, err
}
continue
}
if has {
if bs.Conflicts == minor {
var sMeta storageTxMeta
if err := rlp.DecodeBytes(iter.Value(), &sMeta); err != nil {
return nil, err
}
return &TxMeta{
BlockID: blockID,
BlockID: bs.Header.ID(),
Index: sMeta.Index,
Reverted: sMeta.Reverted,
}, nil
Expand All @@ -154,13 +159,7 @@ func (c *Chain) HasTransaction(txid thor.Bytes32, txBlockRef uint32) (bool, erro
}
// tx block ref too old, fallback to retrieve tx meta.
if headNum-txBlockRef > 100 {
if _, err := c.GetTransactionMeta(txid); err != nil {
if c.IsNotFound(err) {
return false, nil
}
return false, err
}
return true, nil
return c.repo.txIndexer.Has(txid[:])
}

// iterate block summaries from head block to ref block,
Expand Down Expand Up @@ -357,17 +356,16 @@ func (r *Repository) indexBlock(parentConflicts uint32, newBlockID thor.Bytes32,
newNum = block.Number(newBlockID)
root thor.Bytes32
)

if newNum != 0 { // not a genesis block
root = trie.NonCryptoNodeHash
root = thor.BytesToBytes32([]byte{1})
}

trie := r.db.NewNonCryptoTrie(IndexTrieName, root, newNum-1, parentConflicts)
rev := trie.Version{Major: newNum - 1, Minor: parentConflicts}

t := r.db.NewTrie(IndexTrieName, root, rev)
// map block number to block ID
if err := trie.Update(newBlockID[:4], newBlockID[:], nil); err != nil {
if err := t.Update(newBlockID[:4], newBlockID[:], nil); err != nil {
return err
}

_, commit := trie.Stage(newNum, newConflicts)
return commit()
return t.Commit(trie.Version{Major: newNum, Minor: newConflicts}, true)
}
33 changes: 16 additions & 17 deletions chain/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/kv"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/trie"
"github.com/vechain/thor/v2/tx"
)

Expand All @@ -26,7 +27,10 @@ type BlockSummary struct {
Txs []thor.Bytes32
Size uint64
Conflicts uint32
SteadyNum uint32
}

func (s *BlockSummary) Version() trie.Version {
return trie.Version{Major: s.Header.Number(), Minor: s.Conflicts}
}

// the key for tx/receipt.
Expand All @@ -43,12 +47,12 @@ func (k *txKey) SetIndex(i uint64) {
binary.BigEndian.PutUint64(k[33:], i)
}

func saveRLP(w kv.Putter, key []byte, val interface{}) error {
data, err := rlp.EncodeToBytes(val)
if err != nil {
func saveRLP(w kv.Putter, key []byte, val interface{}, sw *sliceWriter) error {
*sw = (*sw)[:0]
if err := rlp.Encode(sw, val); err != nil {
return err
}
return w.Put(key, data)
return w.Put(key, *sw)
}

func loadRLP(r kv.Getter, key []byte, val interface{}) error {
Expand All @@ -59,10 +63,6 @@ func loadRLP(r kv.Getter, key []byte, val interface{}) error {
return rlp.DecodeBytes(data, val)
}

func saveBlockSummary(w kv.Putter, summary *BlockSummary) error {
return saveRLP(w, summary.Header.ID().Bytes(), summary)
}

func indexChainHead(w kv.Putter, header *block.Header) error {
if err := w.Delete(header.ParentID().Bytes()); err != nil {
return err
Expand All @@ -79,10 +79,6 @@ func loadBlockSummary(r kv.Getter, id thor.Bytes32) (*BlockSummary, error) {
return &summary, nil
}

func saveTransaction(w kv.Putter, key txKey, tx *tx.Transaction) error {
return saveRLP(w, key[:], tx)
}

func loadTransaction(r kv.Getter, key txKey) (*tx.Transaction, error) {
var tx tx.Transaction
if err := loadRLP(r, key[:], &tx); err != nil {
Expand All @@ -91,14 +87,17 @@ func loadTransaction(r kv.Getter, key txKey) (*tx.Transaction, error) {
return &tx, nil
}

func saveReceipt(w kv.Putter, key txKey, receipt *tx.Receipt) error {
return saveRLP(w, key[:], receipt)
}

func loadReceipt(r kv.Getter, key txKey) (*tx.Receipt, error) {
var receipt tx.Receipt
if err := loadRLP(r, key[:], &receipt); err != nil {
return nil, err
}
return &receipt, nil
}

type sliceWriter []byte

func (s *sliceWriter) Write(data []byte) (n int, err error) {
*s = append(*s, data...)
return len(data), nil
}
Loading

0 comments on commit ff122e4

Please sign in to comment.