try to use same statedb

This commit is contained in:
atvanguard 2020-03-11 21:25:25 +05:30
parent 9dd4e4b0f9
commit 2d0b9ea63d
4 changed files with 31 additions and 17 deletions

View file

@ -665,7 +665,7 @@ func (c *Bor) Finalize(chain consensus.ChainReader, header *types.Header, state
// commit span
headerNumber := header.Number.Uint64()
fmt.Println(header.Number, headerNumber % c.config.Sprint)
if headerNumber > 0 &&
if /* headerNumber > 0 && */
headerNumber % c.config.Sprint == 0 {
cx := chainContext{Chain: chain, Bor: c}
fmt.Println("here 0.1")
@ -1002,6 +1002,7 @@ func (c *Bor) checkAndCommitSpan(
go func() {
var err error
pending, err = c.isSpanPending(headerNumber - 1)
// pending, err = c.isSpanPending(headerNumber)
errors <- err
// wg.Done()
}()
@ -1074,7 +1075,7 @@ func (c *Bor) fetchAndCommitSpan(
if err := json.Unmarshal(response.Result, &heimdallSpan); err != nil {
return err
}
fmt.Println("here 4", heimdallSpan)
// fmt.Println("here 4", heimdallSpan)
// check if chain id matches with heimdall span
if heimdallSpan.ChainID != c.chainConfig.ChainID.String() {
return fmt.Errorf(

View file

@ -17,7 +17,7 @@ import (
"github.com/maticnetwork/bor/consensus/bor"
"github.com/maticnetwork/bor/core"
"github.com/maticnetwork/bor/core/rawdb"
"github.com/maticnetwork/bor/core/state"
// "github.com/maticnetwork/bor/core/state"
"github.com/maticnetwork/bor/core/types"
// "github.com/maticnetwork/bor/core/vm"
"github.com/maticnetwork/bor/crypto"
@ -35,11 +35,11 @@ type initializeData struct {
func TestCommitSpan(t *testing.T) {
var (
db = rawdb.NewMemoryDatabase()
// db = rawdb.NewMemoryDatabase()
// key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
// addr = crypto.PubkeyToAddress(key.PublicKey)
)
init := buildEthereumInstance(t, db)
init := buildEthereumInstance(t, rawdb.NewMemoryDatabase())
chain := init.ethereum.BlockChain()
engine := init.ethereum.Engine()
_bor := engine.(*bor.Bor)
@ -56,8 +56,10 @@ func TestCommitSpan(t *testing.T) {
h := &mocks.IHeimdallClient{}
h.On("FetchWithRetry", "bor", "span", "1").Return(res, nil)
_bor.SetHeimdallClient(h)
db := init.ethereum.ChainDb()
block := init.genesis.ToBlock(db)
statedb, _ := state.New(block.Root(), state.NewDatabase(db))
statedb, err := chain.StateAt(block.Root())
// statedb, _ := state.New(block.Root(), state.NewDatabase(db))
// _, _, _, err = chain.Processor().Process(block, statedb, vm.Config{})
// fmt.Println(err)
// _bor.Finalize(chain, block.Header(), statedb, nil, nil)
@ -69,9 +71,17 @@ func TestCommitSpan(t *testing.T) {
header := block.Header()
header.Number = big.NewInt(1)
header.ParentHash = block.Hash()
fmt.Println(block.Hash())
// header.Hash =
block = types.NewBlockWithHeader(header)
fmt.Println("statedb.IntermediateRoot", statedb.IntermediateRoot(true))
_bor.Finalize(chain, block.Header(), statedb, nil, nil)
// _, _, _, err = chain.Processor().Process(block, statedb, vm.Config{})
fmt.Println("statedb.IntermediateRoot 2", statedb.IntermediateRoot(true))
statedb, err = chain.StateAt(block.Root())
fmt.Println("statedb.IntermediateRoot 3", statedb.IntermediateRoot(true))
if err != nil {
t.Fatalf("%s", err)
}
@ -81,24 +91,24 @@ func TestCommitSpan(t *testing.T) {
// status, err := chain.writeBlockWithState(block, receipts, statedb)
// chain.chainmu.Lock()
k := big.NewInt(0)
k.Add(block.Difficulty(), chain.GetTdByHash(block.ParentHash()))
fmt.Println("k", k)
td := big.NewInt(0)
td.Add(block.Difficulty(), chain.GetTdByHash(block.ParentHash()))
fmt.Println("td", td)
rawdb.WriteTd(db,
block.Hash(),
block.NumberU64(),
k,
td,
)
rawdb.WriteBlock(db, block)
root, err := statedb.Commit(true /* false ??*/)
root, err := statedb.Commit(false)
fmt.Println("root", root)
if err != nil {
t.Fatalf("%s", err)
}
if err := statedb.Reset(root); err != nil {
t.Fatalf("state reset after block %d failed: %v", block.NumberU64(), err)
}
// if err := statedb.Reset(root); err != nil {
// t.Fatalf("state reset after block %d failed: %v", block.NumberU64(), err)
// }
// blockchain.chainmu.Unlock()
assert.True(t, h.AssertNumberOfCalls(t, "FetchWithRetry", 1))
@ -111,8 +121,8 @@ func TestCommitSpan(t *testing.T) {
validators, _ = _bor.GetCurrentValidators(0, 256)
fmt.Println(4, validators)
// validators, _ = _bor.GetCurrentValidators(1, 256)
// fmt.Println(4, validators)
validators, _ = _bor.GetCurrentValidators(1, 256)
fmt.Println(4, validators)
// engine
}
@ -193,7 +203,7 @@ func buildEthereumInstance(t *testing.T, db ethdb.Database) (*initializeData) {
var ethereum *eth.Ethereum
stack.Service(&ethereum)
ethConf.Genesis.MustCommit(ethereum.ChainDb())
return &initializeData{
genesis: gen,
ethereum: ethereum,

View file

@ -19,6 +19,7 @@ package eth
import (
"context"
"errors"
"fmt"
"math/big"
"github.com/maticnetwork/bor/accounts"
@ -101,6 +102,7 @@ func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.
return nil, nil, err
}
if header == nil {
fmt.Println("yoyo")
return nil, nil, errors.New("header not found")
}
stateDb, err := b.eth.BlockChain().StateAt(header.Root)

View file

@ -730,6 +730,7 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumb
defer func(start time.Time) { log.Debug("Executing EVM call finished", "runtime", time.Since(start)) }(time.Now())
state, header, err := b.StateAndHeaderByNumber(ctx, blockNr)
fmt.Println("statedb.IntermediateRoot in DoCall", state.IntermediateRoot(true))
if state == nil || err != nil {
return nil, 0, false, err
}