fix: add changer lock
This commit is contained in:
parent
63c3811b7f
commit
2eb643289d
|
@ -3,6 +3,7 @@ package ledger
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/meshplus/bitxhub-kit/types"
|
"github.com/meshplus/bitxhub-kit/types"
|
||||||
)
|
)
|
||||||
|
@ -18,6 +19,8 @@ type stateChange interface {
|
||||||
type stateChanger struct {
|
type stateChanger struct {
|
||||||
changes []stateChange
|
changes []stateChange
|
||||||
dirties map[types.Address]int // dirty address and the number of changes
|
dirties map[types.Address]int // dirty address and the number of changes
|
||||||
|
|
||||||
|
lock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func newChanger() *stateChanger {
|
func newChanger() *stateChanger {
|
||||||
|
@ -27,6 +30,9 @@ func newChanger() *stateChanger {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stateChanger) append(change stateChange) {
|
func (s *stateChanger) append(change stateChange) {
|
||||||
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
|
||||||
s.changes = append(s.changes, change)
|
s.changes = append(s.changes, change)
|
||||||
if addr := change.dirtied(); addr != nil {
|
if addr := change.dirtied(); addr != nil {
|
||||||
s.dirties[*addr]++
|
s.dirties[*addr]++
|
||||||
|
@ -34,6 +40,9 @@ func (s *stateChanger) append(change stateChange) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stateChanger) revert(ledger *ChainLedger, snapshot int) {
|
func (s *stateChanger) revert(ledger *ChainLedger, snapshot int) {
|
||||||
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
|
||||||
for i := len(s.changes) - 1; i >= snapshot; i-- {
|
for i := len(s.changes) - 1; i >= snapshot; i-- {
|
||||||
s.changes[i].revert(ledger)
|
s.changes[i].revert(ledger)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue