bitxhub/internal/ledger/state_changer.go

228 lines
4.4 KiB
Go
Raw Normal View History

2021-04-21 00:13:16 +08:00
package ledger
import (
2021-04-24 14:47:00 +08:00
"fmt"
2021-04-21 00:13:16 +08:00
"math/big"
2021-04-27 15:57:21 +08:00
"sync"
2021-04-21 00:13:16 +08:00
"github.com/meshplus/bitxhub-kit/types"
)
type stateChange interface {
// revert undoes the state changes by this entry
2021-04-24 14:47:00 +08:00
revert(*ChainLedger)
2021-04-21 00:13:16 +08:00
// dirted returns the address modified by this state entry
dirtied() *types.Address
}
type stateChanger struct {
changes []stateChange
dirties map[types.Address]int // dirty address and the number of changes
2021-04-27 15:57:21 +08:00
lock sync.RWMutex
2021-04-21 00:13:16 +08:00
}
func newChanger() *stateChanger {
return &stateChanger{
dirties: make(map[types.Address]int),
}
}
func (s *stateChanger) append(change stateChange) {
2021-04-27 15:57:21 +08:00
s.lock.Lock()
defer s.lock.Unlock()
2021-04-21 00:13:16 +08:00
s.changes = append(s.changes, change)
if addr := change.dirtied(); addr != nil {
s.dirties[*addr]++
}
}
2021-04-24 14:47:00 +08:00
func (s *stateChanger) revert(ledger *ChainLedger, snapshot int) {
2021-04-27 15:57:21 +08:00
s.lock.Lock()
defer s.lock.Unlock()
2021-04-21 00:13:16 +08:00
for i := len(s.changes) - 1; i >= snapshot; i-- {
2021-04-24 14:47:00 +08:00
s.changes[i].revert(ledger)
2021-04-21 00:13:16 +08:00
if addr := s.changes[i].dirtied(); addr != nil {
if s.dirties[*addr]--; s.dirties[*addr] == 0 {
delete(s.dirties, *addr)
}
}
}
s.changes = s.changes[:snapshot]
}
func (s *stateChanger) dirty(addr types.Address) {
s.dirties[addr]++
}
func (s *stateChanger) length() int {
return len(s.changes)
}
type (
createObjectChange struct {
account *types.Address
}
resetObjectChange struct {
prev *Account
}
suicideChange struct {
account *types.Address
prev bool
prevbalance *big.Int
}
balanceChange struct {
account *types.Address
prev *big.Int
}
nonceChange struct {
account *types.Address
prev uint64
}
storageChange struct {
account *types.Address
key, prevalue *types.Hash
}
codeChange struct {
2021-04-24 14:47:00 +08:00
account *types.Address
prevcode []byte
2021-04-21 00:13:16 +08:00
}
refundChange struct {
prev uint64
}
addLogChange struct {
2021-04-24 14:47:00 +08:00
txHash *types.Hash
2021-04-21 00:13:16 +08:00
}
addPreimageChange struct {
hash types.Hash
}
touchChange struct {
account *types.Address
}
2021-04-24 14:47:00 +08:00
accessListAddAccountChange struct {
address *types.Address
}
accessListAddSlotChange struct {
address *types.Address
slot *types.Hash
}
2021-04-21 00:13:16 +08:00
)
2021-04-24 14:47:00 +08:00
func (ch createObjectChange) revert(l *ChainLedger) {
2021-04-21 00:13:16 +08:00
delete(l.accounts, ch.account.String())
l.accountCache.rmAccount(ch.account)
}
2021-04-24 14:47:00 +08:00
func (ch createObjectChange) dirtied() *types.Address {
2021-04-21 00:13:16 +08:00
return ch.account
}
2021-04-24 14:47:00 +08:00
func (ch resetObjectChange) revert(l *ChainLedger) {
2021-04-21 00:13:16 +08:00
l.setAccount(ch.prev)
}
2021-04-24 14:47:00 +08:00
func (ch resetObjectChange) dirtied() *types.Address {
2021-04-21 00:13:16 +08:00
return nil
}
2021-04-24 14:47:00 +08:00
func (ch suicideChange) revert(l *ChainLedger) {
account := l.GetAccount(ch.account)
account.suicided = ch.prev
account.SetBalance(ch.prevbalance)
}
func (ch suicideChange) dirtied() *types.Address {
return ch.account
}
func (ch touchChange) revert(l *ChainLedger) {
}
func (ch touchChange) dirtied() *types.Address {
return ch.account
}
func (ch balanceChange) revert(l *ChainLedger) {
fmt.Println(ch.prev.Int64())
l.GetAccount(ch.account).SetBalance(ch.prev)
}
func (ch balanceChange) dirtied() *types.Address {
return ch.account
}
func (ch nonceChange) revert(l *ChainLedger) {
l.GetAccount(ch.account).SetNonce(ch.prev)
}
func (ch nonceChange) dirtied() *types.Address {
return ch.account
}
func (ch codeChange) revert(l *ChainLedger) {
l.GetAccount(ch.account).SetCodeAndHash(ch.prevcode)
}
2021-04-21 00:13:16 +08:00
2021-04-24 14:47:00 +08:00
func (ch codeChange) dirtied() *types.Address {
return ch.account
}
func (ch storageChange) revert(l *ChainLedger) {
l.GetAccount(ch.account).SetState(ch.key.Bytes(), ch.prevalue.Bytes())
}
func (ch storageChange) dirtied() *types.Address {
return ch.account
}
func (ch refundChange) revert(l *ChainLedger) {
l.refund = ch.prev
}
func (ch refundChange) dirtied() *types.Address {
return nil
}
func (ch addPreimageChange) revert(l *ChainLedger) {
delete(l.preimages, ch.hash)
}
func (ch addPreimageChange) dirtied() *types.Address {
return nil
}
func (ch accessListAddAccountChange) revert(l *ChainLedger) {
l.accessList.DeleteAddress(*ch.address)
}
func (ch accessListAddAccountChange) dirtied() *types.Address {
return nil
}
func (ch accessListAddSlotChange) revert(l *ChainLedger) {
l.accessList.DeleteSlot(*ch.address, *ch.slot)
}
func (ch accessListAddSlotChange) dirtied() *types.Address {
return nil
}
func (ch addLogChange) revert(l *ChainLedger) {
logs := l.logs.logs[*ch.txHash]
if len(logs) == 1 {
delete(l.logs.logs, *ch.txHash)
} else {
l.logs.logs[*ch.txHash] = logs[:len(logs)-1]
}
l.logs.logSize--
}
func (ch addLogChange) dirtied() *types.Address {
return nil
2021-04-21 00:13:16 +08:00
}