Skip to content

Commit

Permalink
Merge pull request #3228 from 0chain/sprint-1.14
Browse files Browse the repository at this point in the history
Sprint 1.14
  • Loading branch information
dabasov committed May 1, 2024
2 parents 943ac1e + 74a2fd0 commit 0481652
Show file tree
Hide file tree
Showing 127 changed files with 5,778 additions and 1,937 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

- uses: actions/setup-go@v2
with:
go-version: '1.20'
go-version: '1.21'

- name: Check msgp changes
run: |
Expand Down Expand Up @@ -42,7 +42,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '1.20'
go-version: '1.21'

- name: Check msgp changes
run: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"0chain.net/core/datastore"
"0chain.net/core/memorystore"
"0chain.net/core/mocks"
"github.com/0chain/common/core/statecache"
"github.com/0chain/common/core/util"
"github.com/stretchr/testify/require"
"github.com/vmihailenco/msgpack/v5"
Expand Down Expand Up @@ -47,7 +48,7 @@ func TestStateChangeComputeRoot(t *testing.T) {
{"1235", "1235A"},
}

clientState := util.NewMerklePatriciaTrie(util.NewMemoryNodeDB(), 1, nil)
clientState := util.NewMerklePatriciaTrie(util.NewMemoryNodeDB(), 1, nil, statecache.NewEmpty())
for _, pv := range initPathValues {
_, err := clientState.Insert(util.Path(pv[0]), &util.SecureSerializableValue{Buffer: []byte(pv[1])})
require.NoError(t, err)
Expand All @@ -57,7 +58,7 @@ func TestStateChangeComputeRoot(t *testing.T) {
require.Equal(t, bsc.GetRoot().GetHash(), util.ToHex(clientState.GetRoot()))

// apply new updates
newClientState := util.NewMerklePatriciaTrie(clientState.GetNodeDB(), 2, clientState.GetRoot())
newClientState := util.NewMerklePatriciaTrie(clientState.GetNodeDB(), 2, clientState.GetRoot(), statecache.NewEmpty())
for _, pv := range newPathValues {
_, err := newClientState.Insert(util.Path(pv[0]), &util.SecureSerializableValue{Buffer: []byte(pv[1])})
require.NoError(t, err)
Expand All @@ -71,7 +72,7 @@ func TestStateChangeComputeRoot(t *testing.T) {
func TestNewBlockStateChange(t *testing.T) {
b := NewBlock("", 1)
b.HashBlock()
b.ClientState = util.NewMerklePatriciaTrie(util.NewMemoryNodeDB(), 1, nil)
b.ClientState = util.NewMerklePatriciaTrie(util.NewMemoryNodeDB(), 1, nil, statecache.NewEmpty())
_, err := b.ClientState.Insert(util.Path("path"), &util.SecureSerializableValue{Buffer: []byte("value")})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -235,7 +236,7 @@ func TestStateChange_Delete(t *testing.T) {

func TestStateChange_MarshalJSON(t *testing.T) {
b := NewBlock("", 1)
b.ClientState = util.NewMerklePatriciaTrie(util.NewMemoryNodeDB(), 1, nil)
b.ClientState = util.NewMerklePatriciaTrie(util.NewMemoryNodeDB(), 1, nil, statecache.NewEmpty())
_, err := b.ClientState.Insert(util.Path("path"), &util.SecureSerializableValue{Buffer: []byte("value")})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -290,7 +291,7 @@ func TestStateChange_MarshalJSON(t *testing.T) {

func TestStateChange_UnmarshalJSON(t *testing.T) {
b := NewBlock("", 1)
b.ClientState = util.NewMerklePatriciaTrie(util.NewMemoryNodeDB(), 1, nil)
b.ClientState = util.NewMerklePatriciaTrie(util.NewMemoryNodeDB(), 1, nil, statecache.NewEmpty())
_, err := b.ClientState.Insert(util.Path("path"), &util.SecureSerializableValue{Buffer: []byte("value")})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -382,7 +383,7 @@ func TestStateChange_UnmarshalJSON(t *testing.T) {

func TestStateChange_UnmarshalMsgpack(t *testing.T) {
b := NewBlock("", 1)
b.ClientState = util.NewMerklePatriciaTrie(util.NewMemoryNodeDB(), 1, nil)
b.ClientState = util.NewMerklePatriciaTrie(util.NewMemoryNodeDB(), 1, nil, statecache.NewEmpty())
_, err := b.ClientState.Insert(util.Path("path"), &util.SecureSerializableValue{Buffer: []byte("value")})
if err != nil {
t.Fatal(err)
Expand Down
48 changes: 21 additions & 27 deletions code/go/0chain.net/chaincore/block/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"0chain.net/core/encryption"
"0chain.net/smartcontract/dbs/event"
"github.com/0chain/common/core/logging"
"github.com/0chain/common/core/statecache"
"github.com/0chain/common/core/util"
)

Expand Down Expand Up @@ -343,29 +344,6 @@ func (b *Block) SetPreviousBlock(prevBlock *Block) {
}
}

/*SetStateDB - set the state from the previous block */
func (b *Block) SetStateDB(prevBlock *Block, stateDB util.NodeDB) {
var pndb util.NodeDB
var rootHash util.Key
if prevBlock.ClientState == nil {
logging.Logger.Error("set state db - prior state not available",
zap.Int64("round", b.Round),
zap.String("block", b.Hash),
zap.Int64("previous round", prevBlock.Round),
zap.String("previous block", prevBlock.Hash))
pndb = stateDB
} else {
pndb = prevBlock.ClientState.GetNodeDB()
}
rootHash = prevBlock.ClientStateHash
logging.Logger.Warn("set state db",
zap.Int64("round", b.Round),
zap.String("block", b.Hash),
zap.String("prev_block", prevBlock.Hash),
zap.String("root", util.ToHex(rootHash)))
b.CreateState(pndb, rootHash)
}

// InitStateDB - initialize the block's state from the db
// (assuming it's already computed).
func (b *Block) InitStateDB(ndb util.NodeDB) error {
Expand All @@ -383,7 +361,7 @@ func (b *Block) InitStateDB(ndb util.NodeDB) error {
func (b *Block) CreateState(pndb util.NodeDB, root util.Key) {
mndb := util.NewMemoryNodeDB()
ndb := util.NewLevelNodeDB(mndb, pndb, false)
b.ClientState = util.NewMerklePatriciaTrie(ndb, util.Sequence(b.Round), root)
b.ClientState = util.NewMerklePatriciaTrie(ndb, util.Sequence(b.Round), root, statecache.NewEmpty())
}

// setClientState sets the block client state
Expand Down Expand Up @@ -811,8 +789,13 @@ type Chainer interface {
GetBlockStateChange(b *Block) error
ComputeState(ctx context.Context, pb *Block, waitC ...chan struct{}) error
GetStateDB() util.NodeDB
UpdateState(ctx context.Context, b *Block, bState util.MerklePatriciaTrieI, txn *transaction.Transaction, waitC ...chan struct{}) ([]event.Event, error)
UpdateState(ctx context.Context,
b *Block, bState util.MerklePatriciaTrieI,
txn *transaction.Transaction,
blockStateCache *statecache.BlockCache,
waitC ...chan struct{}) ([]event.Event, error)
GetEventDb() *event.EventDb
GetStateCache() *statecache.StateCache
}

// CreateStateWithPreviousBlock creates block client state with previous block
Expand Down Expand Up @@ -844,7 +827,7 @@ func CreateStateWithPreviousBlock(prevBlock *Block, stateDB util.NodeDB, round i
func CreateState(stateDB util.NodeDB, round int64, root util.Key) util.MerklePatriciaTrieI {
mndb := util.NewMemoryNodeDB()
ndb := util.NewLevelNodeDB(mndb, stateDB, false)
return util.NewMerklePatriciaTrie(ndb, util.Sequence(round), root)
return util.NewMerklePatriciaTrie(ndb, util.Sequence(round), root, statecache.NewEmpty())
}

// ComputeState computes block client state
Expand Down Expand Up @@ -923,9 +906,15 @@ func (b *Block) ComputeState(ctx context.Context, c Chainer, waitC ...chan struc
//b.SetStateDB(pb, c.GetStateDB())

bState := CreateStateWithPreviousBlock(pb, c.GetStateDB(), b.Round)
blockStateCache := statecache.NewBlockCache(c.GetStateCache(), statecache.Block{
Round: b.Round,
Hash: b.Hash,
PrevHash: b.PrevHash,
})

beginStateRoot := bState.GetRoot()
b.Events = []event.Event{}
ts := time.Now()
for _, txn := range b.Txns {
if datastore.IsEmpty(txn.ClientID) {
if err := txn.ComputeClientID(); err != nil {
Expand All @@ -952,7 +941,7 @@ func (b *Block) ComputeState(ctx context.Context, c Chainer, waitC ...chan struc
},
})

events, err := c.UpdateState(ctx, b, bState, txn, waitC...)
events, err := c.UpdateState(ctx, b, bState, txn, blockStateCache, waitC...)
switch err {
case context.Canceled:
b.SetStateStatus(StateCancelled)
Expand Down Expand Up @@ -1030,17 +1019,22 @@ func (b *Block) ComputeState(ctx context.Context, c Chainer, waitC ...chan struc
StateSanityCheck(ctx, b)
b.SetStateStatus(StateSuccessful)

// commit the block state cache to the global state cache
blockStateCache.Commit()

logging.Logger.Info("compute state successful",
zap.Int64("round", b.Round),
zap.String("block", b.Hash),
zap.String("block ptr", fmt.Sprintf("%p", b)),
zap.Int("block_size", len(b.Txns)),
zap.Any("duration", time.Since(ts)),
zap.Int("changes", b.ClientState.GetChangeCount()),
zap.String("begin_client_state", util.ToHex(beginStateRoot)),
zap.String("computed_state_hash", util.ToHex(b.ClientState.GetRoot())),
zap.String("block_state_hash", util.ToHex(b.ClientStateHash)),
zap.String("prev_block", b.PrevHash),
zap.String("prev_block_client_state", util.ToHex(pb.ClientStateHash)))

return nil
}

Expand Down

0 comments on commit 0481652

Please sign in to comment.