new type + additional methods

pull/532/head
obscuren 10 years ago
parent b523441361
commit f486c0ae56
  1. 2
      common/bytes.go
  2. 36
      common/types.go
  3. 34
      core/types/block.go
  4. 25
      state/managed_state.go
  5. 14
      state/state_object.go
  6. 49
      state/statedb.go
  7. 9
      trie/trie.go

@ -211,7 +211,7 @@ func RightPadString(str string, l int) string {
}
func Address(slice []byte) (addr []byte) {
func ToAddress(slice []byte) (addr []byte) {
if len(slice) < 20 {
addr = LeftPadBytes(slice, 20)
} else if len(slice) > 20 {

@ -4,3 +4,39 @@ type (
Hash [32]byte
Address [20]byte
)
// Don't use the default 'String' method in case we want to overwrite
// Get the string representation of the underlying hash
func (h Hash) Str() string {
return string(h[:])
}
// Sets the hash to the value of b. If b is larger than len(h) it will panic
func (h Hash) SetBytes(b []byte) {
if len(b) > len(h) {
panic("unable to set bytes. too big")
}
// reverse loop
for i := len(b); i >= 0; i-- {
h[i] = b[i]
}
}
// Get the string representation of the underlying address
func (a Address) Str() string {
return string(a[:])
}
// Sets the address to the value of b. If b is larger than len(a) it will panic
func (h Address) SetBytes(b []byte) {
if len(b) > len(h) {
panic("unable to set bytes. too big")
}
// reverse loop
for i := len(b); i >= 0; i-- {
h[i] = b[i]
}
}

@ -8,26 +8,26 @@ import (
"sort"
"time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
)
type Header struct {
// Hash to the previous block
ParentHash []byte
ParentHash common.Hash
// Uncles of this block
UncleHash []byte
UncleHash common.Hash
// The coin base address
Coinbase []byte
Coinbase common.Address
// Block Trie state
Root []byte
Root common.Hash
// Tx sha
TxHash []byte
TxHash common.Hash
// Receipt sha
ReceiptHash []byte
ReceiptHash common.Hash
// Bloom
Bloom []byte
Bloom [256]byte
// Difficulty for the current block
Difficulty *big.Int
// The block number
@ -41,9 +41,9 @@ type Header struct {
// Extra data
Extra string
// Mix digest for quick checking to prevent DOS
MixDigest []byte
MixDigest common.Hash
// Nonce
Nonce []byte
Nonce [8]byte
}
func (self *Header) rlpData(withNonce bool) []interface{} {
@ -206,13 +206,13 @@ func (self *Block) SetNonce(nonce uint64) {
self.header.setNonce(nonce)
}
func (self *Block) Bloom() []byte { return self.header.Bloom }
func (self *Block) Coinbase() []byte { return self.header.Coinbase }
func (self *Block) Time() int64 { return int64(self.header.Time) }
func (self *Block) GasLimit() *big.Int { return self.header.GasLimit }
func (self *Block) GasUsed() *big.Int { return self.header.GasUsed }
func (self *Block) Root() []byte { return self.header.Root }
func (self *Block) SetRoot(root []byte) { self.header.Root = root }
func (self *Block) Bloom() []byte { return self.header.Bloom }
func (self *Block) Coinbase() []byte { return self.header.Coinbase }
func (self *Block) Time() int64 { return int64(self.header.Time) }
func (self *Block) GasLimit() *big.Int { return self.header.GasLimit }
func (self *Block) GasUsed() *big.Int { return self.header.GasUsed }
func (self *Block) Root() []byte { return self.header.Root }
func (self *Block) SetRoot(root []byte) { self.header.Root = root }
func (self *Block) Size() common.StorageSize { return common.StorageSize(len(common.Encode(self))) }
func (self *Block) GetTransaction(i int) *Transaction {
if len(self.transactions) > i {

@ -1,6 +1,10 @@
package state
import "sync"
import (
"sync"
"github.com/ethereum/go-ethereum/common"
)
type account struct {
stateObject *StateObject
@ -29,7 +33,7 @@ func (ms *ManagedState) SetState(statedb *StateDB) {
ms.StateDB = statedb
}
func (ms *ManagedState) RemoveNonce(addr []byte, n uint64) {
func (ms *ManagedState) RemoveNonce(addr common.Address, n uint64) {
if ms.hasAccount(addr) {
ms.mu.Lock()
defer ms.mu.Unlock()
@ -43,7 +47,7 @@ func (ms *ManagedState) RemoveNonce(addr []byte, n uint64) {
}
}
func (ms *ManagedState) NewNonce(addr []byte) uint64 {
func (ms *ManagedState) NewNonce(addr common.Address) uint64 {
ms.mu.RLock()
defer ms.mu.RUnlock()
@ -57,26 +61,27 @@ func (ms *ManagedState) NewNonce(addr []byte) uint64 {
return uint64(len(account.nonces)) + account.nstart
}
func (ms *ManagedState) hasAccount(addr []byte) bool {
_, ok := ms.accounts[string(addr)]
func (ms *ManagedState) hasAccount(addr common.Address) bool {
_, ok := ms.accounts[addr.Str()]
return ok
}
func (ms *ManagedState) getAccount(addr []byte) *account {
if account, ok := ms.accounts[string(addr)]; !ok {
func (ms *ManagedState) getAccount(addr common.Address) *account {
straddr := addr.Str()
if account, ok := ms.accounts[straddr]; !ok {
so := ms.GetOrNewStateObject(addr)
ms.accounts[string(addr)] = newAccount(so)
ms.accounts[straddr] = newAccount(so)
} else {
// Always make sure the state account nonce isn't actually higher
// than the tracked one.
so := ms.StateDB.GetStateObject(addr)
if so != nil && uint64(len(account.nonces))+account.nstart < so.nonce {
ms.accounts[string(addr)] = newAccount(so)
ms.accounts[straddr] = newAccount(so)
}
}
return ms.accounts[string(addr)]
return ms.accounts[straddr]
}
func newAccount(so *StateObject) *account {

@ -5,8 +5,8 @@ import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
)
@ -44,7 +44,7 @@ type StateObject struct {
State *StateDB
// Address belonging to this account
address []byte
address common.Address
// The balance of the account
balance *big.Int
// The nonce of the account
@ -77,9 +77,9 @@ func (self *StateObject) Reset() {
self.State.Reset()
}
func NewStateObject(addr []byte, db common.Database) *StateObject {
func NewStateObject(address common.Address, db common.Database) *StateObject {
// This to ensure that it has 20 bytes (and not 0 bytes), thus left or right pad doesn't matter.
address := common.Address(addr)
//address := common.ToAddress(addr)
object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int), dirty: true}
object.State = New(nil, db) //New(trie.New(common.Config.Db, ""))
@ -90,12 +90,12 @@ func NewStateObject(addr []byte, db common.Database) *StateObject {
return object
}
func NewStateObjectFromBytes(address, data []byte, db common.Database) *StateObject {
func NewStateObjectFromBytes(address common.Address, data []byte, db common.Database) *StateObject {
// TODO clean me up
var extobject struct {
Nonce uint64
Balance *big.Int
Root []byte
Root common.Hash
CodeHash []byte
}
err := rlp.Decode(bytes.NewReader(data), &extobject)
@ -284,7 +284,7 @@ func (c *StateObject) N() *big.Int {
}
// Returns the address of the contract/account
func (c *StateObject) Address() []byte {
func (c *StateObject) Address() common.Address {
return c.address
}

@ -45,15 +45,16 @@ func (self *StateDB) Logs() Logs {
return self.logs
}
func (self *StateDB) Refund(addr []byte, gas *big.Int) {
if self.refund[string(addr)] == nil {
self.refund[string(addr)] = new(big.Int)
func (self *StateDB) Refund(address common.Address, gas *big.Int) {
addr := address.Str()
if self.refund[addr] == nil {
self.refund[addr] = new(big.Int)
}
self.refund[string(addr)].Add(self.refund[string(addr)], gas)
self.refund[addr].Add(self.refund[addr], gas)
}
// Retrieve the balance from the given address or 0 if object not found
func (self *StateDB) GetBalance(addr []byte) *big.Int {
func (self *StateDB) GetBalance(addr common.Address) *big.Int {
stateObject := self.GetStateObject(addr)
if stateObject != nil {
return stateObject.balance
@ -62,14 +63,14 @@ func (self *StateDB) GetBalance(addr []byte) *big.Int {
return common.Big0
}
func (self *StateDB) AddBalance(addr []byte, amount *big.Int) {
func (self *StateDB) AddBalance(addr common.Address, amount *big.Int) {
stateObject := self.GetStateObject(addr)
if stateObject != nil {
stateObject.AddBalance(amount)
}
}
func (self *StateDB) GetNonce(addr []byte) uint64 {
func (self *StateDB) GetNonce(addr common.Address) uint64 {
stateObject := self.GetStateObject(addr)
if stateObject != nil {
return stateObject.nonce
@ -78,7 +79,7 @@ func (self *StateDB) GetNonce(addr []byte) uint64 {
return 0
}
func (self *StateDB) GetCode(addr []byte) []byte {
func (self *StateDB) GetCode(addr common.Address) []byte {
stateObject := self.GetStateObject(addr)
if stateObject != nil {
return stateObject.code
@ -87,7 +88,7 @@ func (self *StateDB) GetCode(addr []byte) []byte {
return nil
}
func (self *StateDB) GetState(a, b []byte) []byte {
func (self *StateDB) GetState(a common.Adress, b common.Hash) []byte {
stateObject := self.GetStateObject(a)
if stateObject != nil {
return stateObject.GetState(b).Bytes()
@ -96,28 +97,28 @@ func (self *StateDB) GetState(a, b []byte) []byte {
return nil
}
func (self *StateDB) SetNonce(addr []byte, nonce uint64) {
func (self *StateDB) SetNonce(addr common.Address, nonce uint64) {
stateObject := self.GetStateObject(addr)
if stateObject != nil {
stateObject.SetNonce(nonce)
}
}
func (self *StateDB) SetCode(addr, code []byte) {
func (self *StateDB) SetCode(addr common.Address, code []byte) {
stateObject := self.GetStateObject(addr)
if stateObject != nil {
stateObject.SetCode(code)
}
}
func (self *StateDB) SetState(addr, key []byte, value interface{}) {
func (self *StateDB) SetState(addr common.Address, key common.Hash, value interface{}) {
stateObject := self.GetStateObject(addr)
if stateObject != nil {
stateObject.SetState(key, common.NewValue(value))
}
}
func (self *StateDB) Delete(addr []byte) bool {
func (self *StateDB) Delete(addr common.Address) bool {
stateObject := self.GetStateObject(addr)
if stateObject != nil {
stateObject.MarkForDeletion()
@ -129,7 +130,7 @@ func (self *StateDB) Delete(addr []byte) bool {
return false
}
func (self *StateDB) IsDeleted(addr []byte) bool {
func (self *StateDB) IsDeleted(addr common.Address) bool {
stateObject := self.GetStateObject(addr)
if stateObject != nil {
return stateObject.remove
@ -143,27 +144,27 @@ func (self *StateDB) IsDeleted(addr []byte) bool {
// Update the given state object and apply it to state trie
func (self *StateDB) UpdateStateObject(stateObject *StateObject) {
addr := stateObject.Address()
//addr := stateObject.Address()
if len(stateObject.CodeHash()) > 0 {
self.db.Put(stateObject.CodeHash(), stateObject.code)
}
self.trie.Update(addr, stateObject.RlpEncode())
self.trie.Update(stateObject.Address(), stateObject.RlpEncode())
}
// Delete the given state object and delete it from the state trie
func (self *StateDB) DeleteStateObject(stateObject *StateObject) {
self.trie.Delete(stateObject.Address())
delete(self.stateObjects, string(stateObject.Address()))
delete(self.stateObjects, stateObject.Address().Str())
}
// Retrieve a state object given my the address. Nil if not found
func (self *StateDB) GetStateObject(addr []byte) *StateObject {
addr = common.Address(addr)
func (self *StateDB) GetStateObject(addr common.Address) *StateObject {
//addr = common.Address(addr)
stateObject := self.stateObjects[string(addr)]
stateObject := self.stateObjects[addr.Str()]
if stateObject != nil {
return stateObject
}
@ -180,11 +181,11 @@ func (self *StateDB) GetStateObject(addr []byte) *StateObject {
}
func (self *StateDB) SetStateObject(object *StateObject) {
self.stateObjects[string(object.address)] = object
self.stateObjects[object.Address().Str()] = object
}
// Retrieve a state object or create a new state object if nil
func (self *StateDB) GetOrNewStateObject(addr []byte) *StateObject {
func (self *StateDB) GetOrNewStateObject(addr common.Address) *StateObject {
stateObject := self.GetStateObject(addr)
if stateObject == nil {
stateObject = self.NewStateObject(addr)
@ -194,8 +195,8 @@ func (self *StateDB) GetOrNewStateObject(addr []byte) *StateObject {
}
// Create a state object whether it exist in the trie or not
func (self *StateDB) NewStateObject(addr []byte) *StateObject {
addr = common.Address(addr)
func (self *StateDB) NewStateObject(addr common.Address) *StateObject {
//addr = common.Address(addr)
statelogger.Debugf("(+) %x\n", addr)

@ -6,8 +6,8 @@ import (
"fmt"
"sync"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
func ParanoiaCheck(t1 *Trie, backend Backend) (bool, *Trie) {
@ -24,13 +24,13 @@ func ParanoiaCheck(t1 *Trie, backend Backend) (bool, *Trie) {
type Trie struct {
mu sync.Mutex
root Node
roothash []byte
roothash common.Hash
cache *Cache
revisions *list.List
}
func New(root []byte, backend Backend) *Trie {
func New(root common.Hash, backend Backend) *Trie {
trie := &Trie{}
trie.revisions = list.New()
trie.roothash = root
@ -51,6 +51,9 @@ func (self *Trie) Iterator() *Iterator {
}
func (self *Trie) Copy() *Trie {
// cheap copying method
var cpy common.Hash
cpy.Set(self.roothash[:])
cpy := make([]byte, 32)
copy(cpy, self.roothash)
trie := New(nil, nil)

Loading…
Cancel
Save