-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdb.go
57 lines (49 loc) · 1.53 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Package levm is a higher level wrapper for the EVM and the related
// StateDB.
// levm.go contains methods for creating the LEVM and deploying/calling
// contracts.
// db.go contains methods for interacting with the stateDB i.e. getting
// accounts and balances
package levm
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
)
// Account is a simple representation of an
// account in the stateDB, for further
// controls call DB() to retrieve the db
// reference itself.
// Almost as a snapshot
type Account struct {
Balance *big.Int
Nonce uint64
Code []byte
}
// DB returns a pointer to the vm's state DB.
func (lvm *LEVM) DB() *state.StateDB {
return lvm.stateDB
}
// GetAccount returns a copy of stateDB
func (lvm *LEVM) GetAccount(addr common.Address) Account {
acc := Account{}
lvm.stateDB.GetOrNewStateObject(addr)
acc.Balance = lvm.stateDB.GetBalance(addr)
acc.Nonce = lvm.stateDB.GetNonce(addr)
acc.Code = lvm.stateDB.GetCode(addr)
return acc
}
// SetAccount will update the statedb with
// related values contained in the Account
// snapshot.
func (lvm *LEVM) SetAccount(addr common.Address, acc Account) {
lvm.stateDB.GetOrNewStateObject(addr)
lvm.stateDB.SetBalance(addr, acc.Balance)
lvm.stateDB.SetNonce(addr, acc.Nonce)
lvm.stateDB.SetCode(addr, acc.Code)
}
// NewAccount Create a new Account and Set its balance
func (lvm *LEVM) NewAccount(addr common.Address, balance *big.Int) {
lvm.stateDB.GetOrNewStateObject(addr)
lvm.stateDB.SetBalance(addr, balance)
}