github.com/annchain/OG@v0.0.9/vm/ovm/layer_db.go (about)

     1  package ovm
     2  
     3  import (
     4  	math2 "github.com/annchain/OG/arefactor/common/math"
     5  	"github.com/annchain/OG/arefactor/og/types"
     6  	"github.com/annchain/OG/common/math"
     7  	ogcrypto2 "github.com/annchain/OG/deprecated/ogcrypto"
     8  	vmtypes "github.com/annchain/OG/vm/types"
     9  
    10  	"fmt"
    11  
    12  	"errors"
    13  	"github.com/annchain/OG/common"
    14  	"math/big"
    15  	"strings"
    16  )
    17  
    18  var MAX_LAYER = 1024
    19  var FAST_FAIL = false
    20  var empty = types.Hash{}
    21  
    22  // LayerStateDB is the cascading storage for contracts.
    23  // It consists of multiple layers, each of which represents the result of a contract.
    24  // e.g.
    25  // -.-#-+-#-----	<- Latest changes after contract
    26  // --+-------+--	<- ...
    27  // ----.----.---	<- first contract
    28  // -------------	<- StateDB (on disk)
    29  // When accessing, watch from the top to the bottom.
    30  // Same mechanism as Docker image layers.
    31  // Add a layer each time a new contract is run
    32  type LayerStateDB struct {
    33  	Layers      []vmtypes.StateDBDebug
    34  	activeLayer vmtypes.StateDBDebug
    35  }
    36  
    37  func (l *LayerStateDB) GetStateObject(addr common.Address) *vmtypes.StateObject {
    38  	for i := len(l.Layers) - 1; i >= 0; i-- {
    39  		layer := l.Layers[i]
    40  		if so := layer.GetStateObject(addr); so != nil {
    41  			// return a copy in case you need to modify it.
    42  			if i == len(l.Layers)-1 {
    43  				return so
    44  			} else {
    45  				return so.Copy()
    46  			}
    47  
    48  		}
    49  	}
    50  	return nil
    51  }
    52  
    53  func (l *LayerStateDB) SetStateObject(addr common.Address, stateObject *vmtypes.StateObject) {
    54  	l.activeLayer.SetStateObject(addr, stateObject)
    55  	stateObject.DirtySO = true
    56  }
    57  
    58  func NewLayerDB(baseLayer vmtypes.StateDBDebug) *LayerStateDB {
    59  	return &LayerStateDB{
    60  		Layers:      []vmtypes.StateDBDebug{baseLayer},
    61  		activeLayer: baseLayer,
    62  	}
    63  }
    64  
    65  func (l *LayerStateDB) NewLayer() (index int, err error) {
    66  	// add a new memory layer onto the current layer stack
    67  	index = len(l.Layers)
    68  	if index > MAX_LAYER {
    69  		err = errors.New("max layer count reached")
    70  		return
    71  	}
    72  	l.activeLayer = NewMemoryStateDB()
    73  	l.Layers = append(l.Layers, l.activeLayer)
    74  	return
    75  }
    76  
    77  func (l *LayerStateDB) PopLayer(index int) (err error) {
    78  	if len(l.Layers) != index+1 {
    79  		return errors.New("only top layer can be removed")
    80  	}
    81  	if len(l.Layers) == 1 {
    82  		return errors.New("no more layers can be removed")
    83  	}
    84  	l.Layers = l.Layers[:len(l.Layers)-1]
    85  	l.activeLayer = l.Layers[len(l.Layers)-1]
    86  	return
    87  }
    88  
    89  func (l *LayerStateDB) String() string {
    90  	buffer := strings.Builder{}
    91  	for i, layer := range l.Layers {
    92  		if i == 0 {
    93  			buffer.WriteString(fmt.Sprintf("Layer BOTTOM\r\n"))
    94  		} else {
    95  			buffer.WriteString(fmt.Sprintf("Layer %d\r\n", i))
    96  		}
    97  
    98  		buffer.WriteString(layer.String())
    99  		buffer.WriteString("\r\n")
   100  	}
   101  	return buffer.String()
   102  }
   103  
   104  func (l *LayerStateDB) CreateAccount(addr common.Address) {
   105  	if so := l.GetStateObject(addr); so == nil {
   106  		l.activeLayer.CreateAccount(addr)
   107  	} else {
   108  		if FAST_FAIL {
   109  			panic("address already exists")
   110  		}
   111  	}
   112  }
   113  
   114  func (l *LayerStateDB) SubBalance(addr common.Address, value *math.BigInt) {
   115  	if so := l.GetStateObject(addr); so != nil {
   116  		so.Balance = new(big.Int).Sub(so.Balance, value.Value)
   117  		// store to this layer
   118  		l.SetStateObject(addr, so)
   119  	} else {
   120  		if FAST_FAIL {
   121  			panic("address not exists")
   122  		}
   123  	}
   124  }
   125  
   126  func (l *LayerStateDB) AddBalance(addr common.Address, value *math.BigInt) {
   127  	if so := l.GetStateObject(addr); so != nil {
   128  		so.Balance = new(big.Int).Add(so.Balance, value.Value)
   129  		// store to this layer
   130  		l.SetStateObject(addr, so)
   131  	} else {
   132  		if FAST_FAIL {
   133  			panic("address not exists")
   134  		}
   135  	}
   136  }
   137  
   138  func (l *LayerStateDB) GetBalance(addr common.Address) *math.BigInt {
   139  	if so := l.GetStateObject(addr); so != nil {
   140  		return math.NewBigIntFromBigInt(so.Balance)
   141  	} else {
   142  		if FAST_FAIL {
   143  			panic("address not exists")
   144  		}
   145  	}
   146  	return math.NewBigIntFromBigInt(math2.Big0)
   147  }
   148  
   149  func (l *LayerStateDB) GetNonce(addr common.Address) uint64 {
   150  	if so := l.GetStateObject(addr); so != nil {
   151  		return so.Nonce
   152  	} else {
   153  		if FAST_FAIL {
   154  			panic("address not exists")
   155  		}
   156  	}
   157  	return 0
   158  }
   159  
   160  func (l *LayerStateDB) SetNonce(addr common.Address, nonce uint64) {
   161  	if so := l.GetStateObject(addr); so != nil {
   162  		so.Nonce = nonce
   163  		l.SetStateObject(addr, so)
   164  	} else {
   165  		if FAST_FAIL {
   166  			panic("address not exists")
   167  		}
   168  	}
   169  }
   170  
   171  func (l *LayerStateDB) GetCodeHash(addr common.Address) types.Hash {
   172  	if so := l.GetStateObject(addr); so != nil {
   173  		return so.CodeHash
   174  	} else {
   175  		if FAST_FAIL {
   176  			panic("address not exists")
   177  		}
   178  	}
   179  	return types.Hash{}
   180  }
   181  
   182  func (l *LayerStateDB) GetCode(addr common.Address) []byte {
   183  	if so := l.GetStateObject(addr); so != nil {
   184  		return so.Code
   185  	} else {
   186  		if FAST_FAIL {
   187  			panic("address not exists")
   188  		}
   189  	}
   190  	return nil
   191  }
   192  
   193  func (l *LayerStateDB) SetCode(addr common.Address, code []byte) {
   194  	if so := l.GetStateObject(addr); so != nil {
   195  		so.Code = code
   196  		so.CodeHash = ogcrypto2.Keccak256Hash(code)
   197  		so.DirtyCode = true
   198  		l.SetStateObject(addr, so)
   199  	} else {
   200  		if FAST_FAIL {
   201  			panic("address not exists")
   202  		}
   203  	}
   204  }
   205  
   206  func (l *LayerStateDB) GetCodeSize(addr common.Address) int {
   207  	so := l.GetStateObject(addr)
   208  	if so == nil {
   209  		return 0
   210  	}
   211  	if so.Code != nil {
   212  		return len(so.Code)
   213  	}
   214  	return 0
   215  }
   216  
   217  func (l *LayerStateDB) AddRefund(value uint64) {
   218  	l.activeLayer.AddRefund(value)
   219  }
   220  
   221  func (l *LayerStateDB) SubRefund(value uint64) {
   222  	l.activeLayer.SubRefund(value)
   223  }
   224  
   225  func (l *LayerStateDB) GetRefund() uint64 {
   226  	sum := uint64(0)
   227  	for _, layer := range l.Layers {
   228  		sum += layer.GetRefund()
   229  	}
   230  	return sum
   231  }
   232  
   233  func (l *LayerStateDB) GetCommittedState(addr common.Address, hash types.Hash) types.Hash {
   234  	// TODO: committed state
   235  	panic("implement me")
   236  }
   237  
   238  func (l *LayerStateDB) GetState(addr common.Address, key types.Hash) types.Hash {
   239  	for i := len(l.Layers) - 1; i >= 0; i-- {
   240  		layer := l.Layers[i]
   241  		if so := layer.GetState(addr, key); so != empty {
   242  			return so
   243  		}
   244  	}
   245  	return types.Hash{}
   246  }
   247  
   248  func (l *LayerStateDB) SetState(addr common.Address, key types.Hash, value types.Hash) {
   249  	l.activeLayer.SetState(addr, key, value)
   250  }
   251  
   252  func (l *LayerStateDB) Suicide(addr common.Address) bool {
   253  	so := l.GetStateObject(addr)
   254  	if so == nil {
   255  		return false
   256  	}
   257  	so.Suicided = true
   258  	so.Balance = new(big.Int)
   259  	l.SetStateObject(addr, so)
   260  	return true
   261  }
   262  
   263  func (l *LayerStateDB) HasSuicided(addr common.Address) bool {
   264  	so := l.GetStateObject(addr)
   265  	if so == nil {
   266  		return false
   267  	}
   268  	return so.Suicided
   269  }
   270  
   271  func (l *LayerStateDB) Exist(addr common.Address) bool {
   272  	so := l.GetStateObject(addr)
   273  	return so != nil
   274  }
   275  
   276  func (l *LayerStateDB) Empty(addr common.Address) bool {
   277  	so := l.GetStateObject(addr)
   278  	return so == nil || so.Empty()
   279  }
   280  
   281  func (l *LayerStateDB) RevertToSnapshot(i int) {
   282  	l.Layers = l.Layers[0:i]
   283  }
   284  
   285  func (l *LayerStateDB) Snapshot() int {
   286  	l.NewLayer()
   287  	return len(l.Layers) - 1
   288  }
   289  
   290  func (l *LayerStateDB) AddLog(log *vmtypes.Log) {
   291  	l.activeLayer.AddLog(log)
   292  }
   293  
   294  func (l *LayerStateDB) AddPreimage(hash types.Hash, code []byte) {
   295  	l.activeLayer.AddPreimage(hash, code)
   296  }
   297  
   298  func (l *LayerStateDB) ForEachStorage(addr common.Address, f func(types.Hash, types.Hash) bool) {
   299  	// TODO: foreach storage
   300  	panic("implement me")
   301  }
   302  
   303  // MergeChanges merges all layers that are above the bottom layer to one layer
   304  func (l *LayerStateDB) MergeChanges() {
   305  	// if there is only bottom layer, ignore
   306  	if len(l.Layers) <= 2 {
   307  		return
   308  	}
   309  	// put all changes to layer #1
   310  
   311  	for i := 2; i < len(l.Layers); i++ {
   312  		l.mergeLayer(1, i)
   313  	}
   314  	// delete all layers after #1
   315  	l.Layers = l.Layers[0:2]
   316  }
   317  func (l *LayerStateDB) mergeLayer(toLayerIndex int, fromLayerIndex int) {
   318  	toLayer, toOk := l.Layers[toLayerIndex].(*MemoryStateDB)
   319  	fromLayer, fromOk := l.Layers[fromLayerIndex].(*MemoryStateDB)
   320  
   321  	if !toOk {
   322  		panic("to layer does not support merging")
   323  	}
   324  	if !fromOk {
   325  		panic("from layer does not support merging")
   326  	}
   327  
   328  	for k, v := range fromLayer.soLedger {
   329  		toLayer.soLedger[k] = v
   330  	}
   331  	for k, v := range fromLayer.kvLedger {
   332  		toLayer.kvLedger[k] = v
   333  	}
   334  
   335  }
   336  
   337  func (l *LayerStateDB) CurrentLayer() int {
   338  	return len(l.Layers) - 1
   339  }