mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
Merge 000e0d4a32 into c4a4613d95
This commit is contained in:
commit
410dc37ff2
22 changed files with 312 additions and 311 deletions
|
|
@ -14,7 +14,7 @@
|
|||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// Provides support for dealing with EVM assembly instructions (e.g., disassembling them).
|
||||
// Package asm provides support for dealing with EVM assembly instructions (e.g., disassembling them).
|
||||
package asm
|
||||
|
||||
import (
|
||||
|
|
@ -34,14 +34,14 @@ type instructionIterator struct {
|
|||
started bool
|
||||
}
|
||||
|
||||
// Create a new instruction iterator.
|
||||
// NewInstructionIterator creates a new instruction iterator.
|
||||
func NewInstructionIterator(code []byte) *instructionIterator {
|
||||
it := new(instructionIterator)
|
||||
it.code = code
|
||||
return it
|
||||
}
|
||||
|
||||
// Returns true if there is a next instruction and moves on.
|
||||
// Next returns true if there is a next instruction and moves on.
|
||||
func (it *instructionIterator) Next() bool {
|
||||
if it.error != nil || uint64(len(it.code)) <= it.pc {
|
||||
// We previously reached an error or the end.
|
||||
|
|
@ -99,7 +99,7 @@ func (it *instructionIterator) Arg() []byte {
|
|||
return it.arg
|
||||
}
|
||||
|
||||
// Pretty-print all disassembled EVM instructions to stdout.
|
||||
// PrintDisassembled pretty-prints all disassembled EVM instructions to stdout.
|
||||
func PrintDisassembled(code string) error {
|
||||
script, err := hex.DecodeString(code)
|
||||
if err != nil {
|
||||
|
|
@ -117,7 +117,7 @@ func PrintDisassembled(code string) error {
|
|||
return it.Error()
|
||||
}
|
||||
|
||||
// Return all disassembled EVM instructions in human-readable format.
|
||||
// Disassemble returns all disassembled EVM instructions in human-readable format.
|
||||
func Disassemble(script []byte) ([]string, error) {
|
||||
instrs := make([]string, 0)
|
||||
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ type Compiler struct {
|
|||
debug bool
|
||||
}
|
||||
|
||||
// newCompiler returns a new allocated compiler.
|
||||
// NewCompiler returns a new allocated compiler.
|
||||
func NewCompiler(debug bool) *Compiler {
|
||||
return &Compiler{
|
||||
labels: make(map[string]int),
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ type lexer struct {
|
|||
debug bool // flag for triggering debug output
|
||||
}
|
||||
|
||||
// lex lexes the program by name with the given source. It returns a
|
||||
// Lex lexes the program by name with the given source. It returns a
|
||||
// channel on which the tokens are delivered.
|
||||
func Lex(name string, source []byte, debug bool) <-chan token {
|
||||
ch := make(chan token)
|
||||
|
|
|
|||
|
|
@ -48,7 +48,6 @@ import (
|
|||
|
||||
var (
|
||||
blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil)
|
||||
|
||||
ErrNoGenesis = errors.New("Genesis not found in chain")
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import (
|
|||
|
||||
var _ = (*genesisSpecMarshaling)(nil)
|
||||
|
||||
//MarshalJSON assigns receiver values to the Genesis struct and returns its JSON encoding.
|
||||
func (g Genesis) MarshalJSON() ([]byte, error) {
|
||||
type Genesis struct {
|
||||
Config *params.ChainConfig `json:"config"`
|
||||
|
|
@ -51,6 +52,7 @@ func (g Genesis) MarshalJSON() ([]byte, error) {
|
|||
return json.Marshal(&enc)
|
||||
}
|
||||
|
||||
//UnmarshalJSON parses JSON-encoded input and assigns resulting values to the receiver.
|
||||
func (g *Genesis) UnmarshalJSON(input []byte) error {
|
||||
type Genesis struct {
|
||||
Config *params.ChainConfig `json:"config"`
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import (
|
|||
|
||||
var _ = (*genesisAccountMarshaling)(nil)
|
||||
|
||||
//MarshalJSON returns the marshalled receiver.
|
||||
func (g GenesisAccount) MarshalJSON() ([]byte, error) {
|
||||
type GenesisAccount struct {
|
||||
Code hexutil.Bytes `json:"code,omitempty"`
|
||||
|
|
@ -36,6 +37,7 @@ func (g GenesisAccount) MarshalJSON() ([]byte, error) {
|
|||
return json.Marshal(&enc)
|
||||
}
|
||||
|
||||
//UnmarshalJSON parses JSON-encoded input and assigns resulting values to the receiver.
|
||||
func (g *GenesisAccount) UnmarshalJSON(input []byte) error {
|
||||
type GenesisAccount struct {
|
||||
Code *hexutil.Bytes `json:"code,omitempty"`
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ import (
|
|||
lru "github.com/hashicorp/golang-lru"
|
||||
)
|
||||
|
||||
// Trie cache generation limit after which to evict trie nodes from memory.
|
||||
// MaxTrieCacheGen sets trie cache generation limit after which to evict trie nodes from memory.
|
||||
var MaxTrieCacheGen = uint16(120)
|
||||
|
||||
const (
|
||||
|
|
|
|||
|
|
@ -39,15 +39,15 @@ type Dump struct {
|
|||
Accounts map[string]DumpAccount `json:"accounts"`
|
||||
}
|
||||
|
||||
func (self *StateDB) RawDump() Dump {
|
||||
func (s *StateDB) RawDump() Dump {
|
||||
dump := Dump{
|
||||
Root: fmt.Sprintf("%x", self.trie.Hash()),
|
||||
Root: fmt.Sprintf("%x", s.trie.Hash()),
|
||||
Accounts: make(map[string]DumpAccount),
|
||||
}
|
||||
|
||||
it := trie.NewIterator(self.trie.NodeIterator(nil))
|
||||
it := trie.NewIterator(s.trie.NodeIterator(nil))
|
||||
for it.Next() {
|
||||
addr := self.trie.GetKey(it.Key)
|
||||
addr := s.trie.GetKey(it.Key)
|
||||
var data Account
|
||||
if err := rlp.DecodeBytes(it.Value, &data); err != nil {
|
||||
panic(err)
|
||||
|
|
@ -59,20 +59,20 @@ func (self *StateDB) RawDump() Dump {
|
|||
Nonce: data.Nonce,
|
||||
Root: common.Bytes2Hex(data.Root[:]),
|
||||
CodeHash: common.Bytes2Hex(data.CodeHash),
|
||||
Code: common.Bytes2Hex(obj.Code(self.db)),
|
||||
Code: common.Bytes2Hex(obj.Code(s.db)),
|
||||
Storage: make(map[string]string),
|
||||
}
|
||||
storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil))
|
||||
storageIt := trie.NewIterator(obj.getTrie(s.db).NodeIterator(nil))
|
||||
for storageIt.Next() {
|
||||
account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
|
||||
account.Storage[common.Bytes2Hex(s.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
|
||||
}
|
||||
dump.Accounts[common.Bytes2Hex(addr)] = account
|
||||
}
|
||||
return dump
|
||||
}
|
||||
|
||||
func (self *StateDB) Dump() []byte {
|
||||
json, err := json.MarshalIndent(self.RawDump(), "", " ")
|
||||
func (s *StateDB) Dump() []byte {
|
||||
json, err := json.MarshalIndent(s.RawDump(), "", " ")
|
||||
if err != nil {
|
||||
fmt.Println("dump err", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ type ManagedState struct {
|
|||
accounts map[common.Address]*account
|
||||
}
|
||||
|
||||
// ManagedState returns a new managed state with the statedb as it's backing layer
|
||||
// ManageState returns a new managed state with the statedb as it's backing layer
|
||||
func ManageState(statedb *StateDB) *ManagedState {
|
||||
return &ManagedState{
|
||||
StateDB: statedb.Copy(),
|
||||
|
|
@ -92,9 +92,8 @@ func (ms *ManagedState) GetNonce(addr common.Address) uint64 {
|
|||
if ms.hasAccount(addr) {
|
||||
account := ms.getAccount(addr)
|
||||
return uint64(len(account.nonces)) + account.nstart
|
||||
} else {
|
||||
return ms.StateDB.GetNonce(addr)
|
||||
}
|
||||
return ms.StateDB.GetNonce(addr)
|
||||
}
|
||||
|
||||
// SetNonce sets the new canonical nonce for the managed state
|
||||
|
|
|
|||
|
|
@ -31,23 +31,23 @@ var emptyCodeHash = crypto.Keccak256(nil)
|
|||
|
||||
type Code []byte
|
||||
|
||||
func (self Code) String() string {
|
||||
return string(self) //strings.Join(Disassemble(self), " ")
|
||||
func (c Code) String() string {
|
||||
return string(c) //strings.Join(Disassemble(self), " ")
|
||||
}
|
||||
|
||||
type Storage map[common.Hash]common.Hash
|
||||
|
||||
func (self Storage) String() (str string) {
|
||||
for key, value := range self {
|
||||
func (st Storage) String() (str string) {
|
||||
for key, value := range st {
|
||||
str += fmt.Sprintf("%X : %X\n", key, value)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (self Storage) Copy() Storage {
|
||||
func (st Storage) Copy() Storage {
|
||||
cpy := make(Storage)
|
||||
for key, value := range self {
|
||||
for key, value := range st {
|
||||
cpy[key] = value
|
||||
}
|
||||
|
||||
|
|
@ -89,8 +89,8 @@ type stateObject struct {
|
|||
}
|
||||
|
||||
// empty returns whether the account is considered empty.
|
||||
func (s *stateObject) empty() bool {
|
||||
return s.data.Nonce == 0 && s.data.Balance.Sign() == 0 && bytes.Equal(s.data.CodeHash, emptyCodeHash)
|
||||
func (so *stateObject) empty() bool {
|
||||
return so.data.Nonce == 0 && so.data.Balance.Sign() == 0 && bytes.Equal(so.data.CodeHash, emptyCodeHash)
|
||||
}
|
||||
|
||||
// Account is the Ethereum consensus representation of accounts.
|
||||
|
|
@ -121,168 +121,168 @@ func newObject(db *StateDB, address common.Address, data Account) *stateObject {
|
|||
}
|
||||
|
||||
// EncodeRLP implements rlp.Encoder.
|
||||
func (c *stateObject) EncodeRLP(w io.Writer) error {
|
||||
return rlp.Encode(w, c.data)
|
||||
func (so *stateObject) EncodeRLP(w io.Writer) error {
|
||||
return rlp.Encode(w, so.data)
|
||||
}
|
||||
|
||||
// setError remembers the first non-nil error it is called with.
|
||||
func (self *stateObject) setError(err error) {
|
||||
if self.dbErr == nil {
|
||||
self.dbErr = err
|
||||
func (so *stateObject) setError(err error) {
|
||||
if so.dbErr == nil {
|
||||
so.dbErr = err
|
||||
}
|
||||
}
|
||||
|
||||
func (self *stateObject) markSuicided() {
|
||||
self.suicided = true
|
||||
func (so *stateObject) markSuicided() {
|
||||
so.suicided = true
|
||||
}
|
||||
|
||||
func (c *stateObject) touch() {
|
||||
c.db.journal.append(touchChange{
|
||||
account: &c.address,
|
||||
func (so *stateObject) touch() {
|
||||
so.db.journal.append(touchChange{
|
||||
account: &so.address,
|
||||
})
|
||||
if c.address == ripemd {
|
||||
if so.address == ripemd {
|
||||
// Explicitly put it in the dirty-cache, which is otherwise generated from
|
||||
// flattened journals.
|
||||
c.db.journal.dirty(c.address)
|
||||
so.db.journal.dirty(so.address)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *stateObject) getTrie(db Database) Trie {
|
||||
if c.trie == nil {
|
||||
func (so *stateObject) getTrie(db Database) Trie {
|
||||
if so.trie == nil {
|
||||
var err error
|
||||
c.trie, err = db.OpenStorageTrie(c.addrHash, c.data.Root)
|
||||
so.trie, err = db.OpenStorageTrie(so.addrHash, so.data.Root)
|
||||
if err != nil {
|
||||
c.trie, _ = db.OpenStorageTrie(c.addrHash, common.Hash{})
|
||||
c.setError(fmt.Errorf("can't create storage trie: %v", err))
|
||||
so.trie, _ = db.OpenStorageTrie(so.addrHash, common.Hash{})
|
||||
so.setError(fmt.Errorf("can't create storage trie: %v", err))
|
||||
}
|
||||
}
|
||||
return c.trie
|
||||
return so.trie
|
||||
}
|
||||
|
||||
// GetState returns a value in account storage.
|
||||
func (self *stateObject) GetState(db Database, key common.Hash) common.Hash {
|
||||
value, exists := self.cachedStorage[key]
|
||||
func (so *stateObject) GetState(db Database, key common.Hash) common.Hash {
|
||||
value, exists := so.cachedStorage[key]
|
||||
if exists {
|
||||
return value
|
||||
}
|
||||
// Load from DB in case it is missing.
|
||||
enc, err := self.getTrie(db).TryGet(key[:])
|
||||
enc, err := so.getTrie(db).TryGet(key[:])
|
||||
if err != nil {
|
||||
self.setError(err)
|
||||
so.setError(err)
|
||||
return common.Hash{}
|
||||
}
|
||||
if len(enc) > 0 {
|
||||
_, content, _, err := rlp.Split(enc)
|
||||
if err != nil {
|
||||
self.setError(err)
|
||||
so.setError(err)
|
||||
}
|
||||
value.SetBytes(content)
|
||||
}
|
||||
self.cachedStorage[key] = value
|
||||
so.cachedStorage[key] = value
|
||||
return value
|
||||
}
|
||||
|
||||
// SetState updates a value in account storage.
|
||||
func (self *stateObject) SetState(db Database, key, value common.Hash) {
|
||||
self.db.journal.append(storageChange{
|
||||
account: &self.address,
|
||||
func (so *stateObject) SetState(db Database, key, value common.Hash) {
|
||||
so.db.journal.append(storageChange{
|
||||
account: &so.address,
|
||||
key: key,
|
||||
prevalue: self.GetState(db, key),
|
||||
prevalue: so.GetState(db, key),
|
||||
})
|
||||
self.setState(key, value)
|
||||
so.setState(key, value)
|
||||
}
|
||||
|
||||
func (self *stateObject) setState(key, value common.Hash) {
|
||||
self.cachedStorage[key] = value
|
||||
self.dirtyStorage[key] = value
|
||||
func (so *stateObject) setState(key, value common.Hash) {
|
||||
so.cachedStorage[key] = value
|
||||
so.dirtyStorage[key] = value
|
||||
}
|
||||
|
||||
// updateTrie writes cached storage modifications into the object's storage trie.
|
||||
func (self *stateObject) updateTrie(db Database) Trie {
|
||||
tr := self.getTrie(db)
|
||||
for key, value := range self.dirtyStorage {
|
||||
delete(self.dirtyStorage, key)
|
||||
func (so *stateObject) updateTrie(db Database) Trie {
|
||||
tr := so.getTrie(db)
|
||||
for key, value := range so.dirtyStorage {
|
||||
delete(so.dirtyStorage, key)
|
||||
if (value == common.Hash{}) {
|
||||
self.setError(tr.TryDelete(key[:]))
|
||||
so.setError(tr.TryDelete(key[:]))
|
||||
continue
|
||||
}
|
||||
// Encoding []byte cannot fail, ok to ignore the error.
|
||||
v, _ := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00"))
|
||||
self.setError(tr.TryUpdate(key[:], v))
|
||||
so.setError(tr.TryUpdate(key[:], v))
|
||||
}
|
||||
return tr
|
||||
}
|
||||
|
||||
// UpdateRoot sets the trie root to the current root hash of
|
||||
func (self *stateObject) updateRoot(db Database) {
|
||||
self.updateTrie(db)
|
||||
self.data.Root = self.trie.Hash()
|
||||
func (so *stateObject) updateRoot(db Database) {
|
||||
so.updateTrie(db)
|
||||
so.data.Root = so.trie.Hash()
|
||||
}
|
||||
|
||||
// CommitTrie the storage trie of the object to dwb.
|
||||
// This updates the trie root.
|
||||
func (self *stateObject) CommitTrie(db Database) error {
|
||||
self.updateTrie(db)
|
||||
if self.dbErr != nil {
|
||||
return self.dbErr
|
||||
func (so *stateObject) CommitTrie(db Database) error {
|
||||
so.updateTrie(db)
|
||||
if so.dbErr != nil {
|
||||
return so.dbErr
|
||||
}
|
||||
root, err := self.trie.Commit(nil)
|
||||
root, err := so.trie.Commit(nil)
|
||||
if err == nil {
|
||||
self.data.Root = root
|
||||
so.data.Root = root
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// AddBalance removes amount from c's balance.
|
||||
// It is used to add funds to the destination account of a transfer.
|
||||
func (c *stateObject) AddBalance(amount *big.Int) {
|
||||
func (so *stateObject) AddBalance(amount *big.Int) {
|
||||
// EIP158: We must check emptiness for the objects such that the account
|
||||
// clearing (0,0,0 objects) can take effect.
|
||||
if amount.Sign() == 0 {
|
||||
if c.empty() {
|
||||
c.touch()
|
||||
if so.empty() {
|
||||
so.touch()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
c.SetBalance(new(big.Int).Add(c.Balance(), amount))
|
||||
so.SetBalance(new(big.Int).Add(so.Balance(), amount))
|
||||
}
|
||||
|
||||
// SubBalance removes amount from c's balance.
|
||||
// It is used to remove funds from the origin account of a transfer.
|
||||
func (c *stateObject) SubBalance(amount *big.Int) {
|
||||
func (so *stateObject) SubBalance(amount *big.Int) {
|
||||
if amount.Sign() == 0 {
|
||||
return
|
||||
}
|
||||
c.SetBalance(new(big.Int).Sub(c.Balance(), amount))
|
||||
so.SetBalance(new(big.Int).Sub(so.Balance(), amount))
|
||||
}
|
||||
|
||||
func (self *stateObject) SetBalance(amount *big.Int) {
|
||||
self.db.journal.append(balanceChange{
|
||||
account: &self.address,
|
||||
prev: new(big.Int).Set(self.data.Balance),
|
||||
func (so *stateObject) SetBalance(amount *big.Int) {
|
||||
so.db.journal.append(balanceChange{
|
||||
account: &so.address,
|
||||
prev: new(big.Int).Set(so.data.Balance),
|
||||
})
|
||||
self.setBalance(amount)
|
||||
so.setBalance(amount)
|
||||
}
|
||||
|
||||
func (self *stateObject) setBalance(amount *big.Int) {
|
||||
self.data.Balance = amount
|
||||
func (so *stateObject) setBalance(amount *big.Int) {
|
||||
so.data.Balance = amount
|
||||
}
|
||||
|
||||
// Return the gas back to the origin. Used by the Virtual machine or Closures
|
||||
func (c *stateObject) ReturnGas(gas *big.Int) {}
|
||||
func (so *stateObject) ReturnGas(gas *big.Int) {}
|
||||
|
||||
func (self *stateObject) deepCopy(db *StateDB) *stateObject {
|
||||
stateObject := newObject(db, self.address, self.data)
|
||||
if self.trie != nil {
|
||||
stateObject.trie = db.db.CopyTrie(self.trie)
|
||||
func (so *stateObject) deepCopy(db *StateDB) *stateObject {
|
||||
stateObject := newObject(db, so.address, so.data)
|
||||
if so.trie != nil {
|
||||
stateObject.trie = db.db.CopyTrie(so.trie)
|
||||
}
|
||||
stateObject.code = self.code
|
||||
stateObject.dirtyStorage = self.dirtyStorage.Copy()
|
||||
stateObject.cachedStorage = self.dirtyStorage.Copy()
|
||||
stateObject.suicided = self.suicided
|
||||
stateObject.dirtyCode = self.dirtyCode
|
||||
stateObject.deleted = self.deleted
|
||||
stateObject.code = so.code
|
||||
stateObject.dirtyStorage = so.dirtyStorage.Copy()
|
||||
stateObject.cachedStorage = so.dirtyStorage.Copy()
|
||||
stateObject.suicided = so.suicided
|
||||
stateObject.dirtyCode = so.dirtyCode
|
||||
stateObject.deleted = so.deleted
|
||||
return stateObject
|
||||
}
|
||||
|
||||
|
|
@ -290,70 +290,70 @@ func (self *stateObject) deepCopy(db *StateDB) *stateObject {
|
|||
// Attribute accessors
|
||||
//
|
||||
|
||||
// Returns the address of the contract/account
|
||||
func (c *stateObject) Address() common.Address {
|
||||
return c.address
|
||||
// Address returns the address of the contract/account
|
||||
func (so *stateObject) Address() common.Address {
|
||||
return so.address
|
||||
}
|
||||
|
||||
// Code returns the contract code associated with this object, if any.
|
||||
func (self *stateObject) Code(db Database) []byte {
|
||||
if self.code != nil {
|
||||
return self.code
|
||||
func (so *stateObject) Code(db Database) []byte {
|
||||
if so.code != nil {
|
||||
return so.code
|
||||
}
|
||||
if bytes.Equal(self.CodeHash(), emptyCodeHash) {
|
||||
if bytes.Equal(so.CodeHash(), emptyCodeHash) {
|
||||
return nil
|
||||
}
|
||||
code, err := db.ContractCode(self.addrHash, common.BytesToHash(self.CodeHash()))
|
||||
code, err := db.ContractCode(so.addrHash, common.BytesToHash(so.CodeHash()))
|
||||
if err != nil {
|
||||
self.setError(fmt.Errorf("can't load code hash %x: %v", self.CodeHash(), err))
|
||||
so.setError(fmt.Errorf("can't load code hash %x: %v", so.CodeHash(), err))
|
||||
}
|
||||
self.code = code
|
||||
so.code = code
|
||||
return code
|
||||
}
|
||||
|
||||
func (self *stateObject) SetCode(codeHash common.Hash, code []byte) {
|
||||
prevcode := self.Code(self.db.db)
|
||||
self.db.journal.append(codeChange{
|
||||
account: &self.address,
|
||||
prevhash: self.CodeHash(),
|
||||
func (so *stateObject) SetCode(codeHash common.Hash, code []byte) {
|
||||
prevcode := so.Code(so.db.db)
|
||||
so.db.journal.append(codeChange{
|
||||
account: &so.address,
|
||||
prevhash: so.CodeHash(),
|
||||
prevcode: prevcode,
|
||||
})
|
||||
self.setCode(codeHash, code)
|
||||
so.setCode(codeHash, code)
|
||||
}
|
||||
|
||||
func (self *stateObject) setCode(codeHash common.Hash, code []byte) {
|
||||
self.code = code
|
||||
self.data.CodeHash = codeHash[:]
|
||||
self.dirtyCode = true
|
||||
func (so *stateObject) setCode(codeHash common.Hash, code []byte) {
|
||||
so.code = code
|
||||
so.data.CodeHash = codeHash[:]
|
||||
so.dirtyCode = true
|
||||
}
|
||||
|
||||
func (self *stateObject) SetNonce(nonce uint64) {
|
||||
self.db.journal.append(nonceChange{
|
||||
account: &self.address,
|
||||
prev: self.data.Nonce,
|
||||
func (so *stateObject) SetNonce(nonce uint64) {
|
||||
so.db.journal.append(nonceChange{
|
||||
account: &so.address,
|
||||
prev: so.data.Nonce,
|
||||
})
|
||||
self.setNonce(nonce)
|
||||
so.setNonce(nonce)
|
||||
}
|
||||
|
||||
func (self *stateObject) setNonce(nonce uint64) {
|
||||
self.data.Nonce = nonce
|
||||
func (so *stateObject) setNonce(nonce uint64) {
|
||||
so.data.Nonce = nonce
|
||||
}
|
||||
|
||||
func (self *stateObject) CodeHash() []byte {
|
||||
return self.data.CodeHash
|
||||
func (so *stateObject) CodeHash() []byte {
|
||||
return so.data.CodeHash
|
||||
}
|
||||
|
||||
func (self *stateObject) Balance() *big.Int {
|
||||
return self.data.Balance
|
||||
func (so *stateObject) Balance() *big.Int {
|
||||
return so.data.Balance
|
||||
}
|
||||
|
||||
func (self *stateObject) Nonce() uint64 {
|
||||
return self.data.Nonce
|
||||
func (so *stateObject) Nonce() uint64 {
|
||||
return so.data.Nonce
|
||||
}
|
||||
|
||||
// Never called, but must be present to allow stateObject to be used
|
||||
// as a vm.Account interface that also satisfies the vm.ContractRef
|
||||
// interface. Interfaces are awesome.
|
||||
func (self *stateObject) Value() *big.Int {
|
||||
func (so *stateObject) Value() *big.Int {
|
||||
panic("Value on stateObject should never be called")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ type StateDB struct {
|
|||
lock sync.Mutex
|
||||
}
|
||||
|
||||
// Create a new state from a given trie.
|
||||
// New creates a new state from a given trie.
|
||||
func New(root common.Hash, db Database) (*StateDB, error) {
|
||||
tr, err := db.OpenTrie(root)
|
||||
if err != nil {
|
||||
|
|
@ -101,103 +101,103 @@ func New(root common.Hash, db Database) (*StateDB, error) {
|
|||
}
|
||||
|
||||
// setError remembers the first non-nil error it is called with.
|
||||
func (self *StateDB) setError(err error) {
|
||||
if self.dbErr == nil {
|
||||
self.dbErr = err
|
||||
func (s *StateDB) setError(err error) {
|
||||
if s.dbErr == nil {
|
||||
s.dbErr = err
|
||||
}
|
||||
}
|
||||
|
||||
func (self *StateDB) Error() error {
|
||||
return self.dbErr
|
||||
func (s *StateDB) Error() error {
|
||||
return s.dbErr
|
||||
}
|
||||
|
||||
// Reset clears out all ephemeral state objects from the state db, but keeps
|
||||
// the underlying state trie to avoid reloading data for the next operations.
|
||||
func (self *StateDB) Reset(root common.Hash) error {
|
||||
tr, err := self.db.OpenTrie(root)
|
||||
func (s *StateDB) Reset(root common.Hash) error {
|
||||
tr, err := s.db.OpenTrie(root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
self.trie = tr
|
||||
self.stateObjects = make(map[common.Address]*stateObject)
|
||||
self.stateObjectsDirty = make(map[common.Address]struct{})
|
||||
self.thash = common.Hash{}
|
||||
self.bhash = common.Hash{}
|
||||
self.txIndex = 0
|
||||
self.logs = make(map[common.Hash][]*types.Log)
|
||||
self.logSize = 0
|
||||
self.preimages = make(map[common.Hash][]byte)
|
||||
self.clearJournalAndRefund()
|
||||
s.trie = tr
|
||||
s.stateObjects = make(map[common.Address]*stateObject)
|
||||
s.stateObjectsDirty = make(map[common.Address]struct{})
|
||||
s.thash = common.Hash{}
|
||||
s.bhash = common.Hash{}
|
||||
s.txIndex = 0
|
||||
s.logs = make(map[common.Hash][]*types.Log)
|
||||
s.logSize = 0
|
||||
s.preimages = make(map[common.Hash][]byte)
|
||||
s.clearJournalAndRefund()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *StateDB) AddLog(log *types.Log) {
|
||||
self.journal.append(addLogChange{txhash: self.thash})
|
||||
func (s *StateDB) AddLog(log *types.Log) {
|
||||
s.journal.append(addLogChange{txhash: s.thash})
|
||||
|
||||
log.TxHash = self.thash
|
||||
log.BlockHash = self.bhash
|
||||
log.TxIndex = uint(self.txIndex)
|
||||
log.Index = self.logSize
|
||||
self.logs[self.thash] = append(self.logs[self.thash], log)
|
||||
self.logSize++
|
||||
log.TxHash = s.thash
|
||||
log.BlockHash = s.bhash
|
||||
log.TxIndex = uint(s.txIndex)
|
||||
log.Index = s.logSize
|
||||
s.logs[s.thash] = append(s.logs[s.thash], log)
|
||||
s.logSize++
|
||||
}
|
||||
|
||||
func (self *StateDB) GetLogs(hash common.Hash) []*types.Log {
|
||||
return self.logs[hash]
|
||||
func (s *StateDB) GetLogs(hash common.Hash) []*types.Log {
|
||||
return s.logs[hash]
|
||||
}
|
||||
|
||||
func (self *StateDB) Logs() []*types.Log {
|
||||
func (s *StateDB) Logs() []*types.Log {
|
||||
var logs []*types.Log
|
||||
for _, lgs := range self.logs {
|
||||
for _, lgs := range s.logs {
|
||||
logs = append(logs, lgs...)
|
||||
}
|
||||
return logs
|
||||
}
|
||||
|
||||
// AddPreimage records a SHA3 preimage seen by the VM.
|
||||
func (self *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
|
||||
if _, ok := self.preimages[hash]; !ok {
|
||||
self.journal.append(addPreimageChange{hash: hash})
|
||||
func (s *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
|
||||
if _, ok := s.preimages[hash]; !ok {
|
||||
s.journal.append(addPreimageChange{hash: hash})
|
||||
pi := make([]byte, len(preimage))
|
||||
copy(pi, preimage)
|
||||
self.preimages[hash] = pi
|
||||
s.preimages[hash] = pi
|
||||
}
|
||||
}
|
||||
|
||||
// Preimages returns a list of SHA3 preimages that have been submitted.
|
||||
func (self *StateDB) Preimages() map[common.Hash][]byte {
|
||||
return self.preimages
|
||||
func (s *StateDB) Preimages() map[common.Hash][]byte {
|
||||
return s.preimages
|
||||
}
|
||||
|
||||
func (self *StateDB) AddRefund(gas uint64) {
|
||||
self.journal.append(refundChange{prev: self.refund})
|
||||
self.refund += gas
|
||||
func (s *StateDB) AddRefund(gas uint64) {
|
||||
s.journal.append(refundChange{prev: s.refund})
|
||||
s.refund += gas
|
||||
}
|
||||
|
||||
// Exist reports whether the given account address exists in the state.
|
||||
// Notably this also returns true for suicided accounts.
|
||||
func (self *StateDB) Exist(addr common.Address) bool {
|
||||
return self.getStateObject(addr) != nil
|
||||
func (s *StateDB) Exist(addr common.Address) bool {
|
||||
return s.getStateObject(addr) != nil
|
||||
}
|
||||
|
||||
// Empty returns whether the state object is either non-existent
|
||||
// or empty according to the EIP161 specification (balance = nonce = code = 0)
|
||||
func (self *StateDB) Empty(addr common.Address) bool {
|
||||
so := self.getStateObject(addr)
|
||||
func (s *StateDB) Empty(addr common.Address) bool {
|
||||
so := s.getStateObject(addr)
|
||||
return so == nil || so.empty()
|
||||
}
|
||||
|
||||
// Retrieve the balance from the given address or 0 if object not found
|
||||
func (self *StateDB) GetBalance(addr common.Address) *big.Int {
|
||||
stateObject := self.getStateObject(addr)
|
||||
// GetBalance retrieves the balance from the given address or 0 if object not found
|
||||
func (s *StateDB) GetBalance(addr common.Address) *big.Int {
|
||||
stateObject := s.getStateObject(addr)
|
||||
if stateObject != nil {
|
||||
return stateObject.Balance()
|
||||
}
|
||||
return common.Big0
|
||||
}
|
||||
|
||||
func (self *StateDB) GetNonce(addr common.Address) uint64 {
|
||||
stateObject := self.getStateObject(addr)
|
||||
func (s *StateDB) GetNonce(addr common.Address) uint64 {
|
||||
stateObject := s.getStateObject(addr)
|
||||
if stateObject != nil {
|
||||
return stateObject.Nonce()
|
||||
}
|
||||
|
|
@ -205,63 +205,63 @@ func (self *StateDB) GetNonce(addr common.Address) uint64 {
|
|||
return 0
|
||||
}
|
||||
|
||||
func (self *StateDB) GetCode(addr common.Address) []byte {
|
||||
stateObject := self.getStateObject(addr)
|
||||
func (s *StateDB) GetCode(addr common.Address) []byte {
|
||||
stateObject := s.getStateObject(addr)
|
||||
if stateObject != nil {
|
||||
return stateObject.Code(self.db)
|
||||
return stateObject.Code(s.db)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *StateDB) GetCodeSize(addr common.Address) int {
|
||||
stateObject := self.getStateObject(addr)
|
||||
func (s *StateDB) GetCodeSize(addr common.Address) int {
|
||||
stateObject := s.getStateObject(addr)
|
||||
if stateObject == nil {
|
||||
return 0
|
||||
}
|
||||
if stateObject.code != nil {
|
||||
return len(stateObject.code)
|
||||
}
|
||||
size, err := self.db.ContractCodeSize(stateObject.addrHash, common.BytesToHash(stateObject.CodeHash()))
|
||||
size, err := s.db.ContractCodeSize(stateObject.addrHash, common.BytesToHash(stateObject.CodeHash()))
|
||||
if err != nil {
|
||||
self.setError(err)
|
||||
s.setError(err)
|
||||
}
|
||||
return size
|
||||
}
|
||||
|
||||
func (self *StateDB) GetCodeHash(addr common.Address) common.Hash {
|
||||
stateObject := self.getStateObject(addr)
|
||||
func (s *StateDB) GetCodeHash(addr common.Address) common.Hash {
|
||||
stateObject := s.getStateObject(addr)
|
||||
if stateObject == nil {
|
||||
return common.Hash{}
|
||||
}
|
||||
return common.BytesToHash(stateObject.CodeHash())
|
||||
}
|
||||
|
||||
func (self *StateDB) GetState(addr common.Address, bhash common.Hash) common.Hash {
|
||||
stateObject := self.getStateObject(addr)
|
||||
func (s *StateDB) GetState(addr common.Address, bhash common.Hash) common.Hash {
|
||||
stateObject := s.getStateObject(addr)
|
||||
if stateObject != nil {
|
||||
return stateObject.GetState(self.db, bhash)
|
||||
return stateObject.GetState(s.db, bhash)
|
||||
}
|
||||
return common.Hash{}
|
||||
}
|
||||
|
||||
// Database retrieves the low level database supporting the lower level trie ops.
|
||||
func (self *StateDB) Database() Database {
|
||||
return self.db
|
||||
func (s *StateDB) Database() Database {
|
||||
return s.db
|
||||
}
|
||||
|
||||
// StorageTrie returns the storage trie of an account.
|
||||
// The return value is a copy and is nil for non-existent accounts.
|
||||
func (self *StateDB) StorageTrie(addr common.Address) Trie {
|
||||
stateObject := self.getStateObject(addr)
|
||||
func (s *StateDB) StorageTrie(addr common.Address) Trie {
|
||||
stateObject := s.getStateObject(addr)
|
||||
if stateObject == nil {
|
||||
return nil
|
||||
}
|
||||
cpy := stateObject.deepCopy(self)
|
||||
return cpy.updateTrie(self.db)
|
||||
cpy := stateObject.deepCopy(s)
|
||||
return cpy.updateTrie(s.db)
|
||||
}
|
||||
|
||||
func (self *StateDB) HasSuicided(addr common.Address) bool {
|
||||
stateObject := self.getStateObject(addr)
|
||||
func (s *StateDB) HasSuicided(addr common.Address) bool {
|
||||
stateObject := s.getStateObject(addr)
|
||||
if stateObject != nil {
|
||||
return stateObject.suicided
|
||||
}
|
||||
|
|
@ -273,46 +273,46 @@ func (self *StateDB) HasSuicided(addr common.Address) bool {
|
|||
*/
|
||||
|
||||
// AddBalance adds amount to the account associated with addr.
|
||||
func (self *StateDB) AddBalance(addr common.Address, amount *big.Int) {
|
||||
stateObject := self.GetOrNewStateObject(addr)
|
||||
func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) {
|
||||
stateObject := s.GetOrNewStateObject(addr)
|
||||
if stateObject != nil {
|
||||
stateObject.AddBalance(amount)
|
||||
}
|
||||
}
|
||||
|
||||
// SubBalance subtracts amount from the account associated with addr.
|
||||
func (self *StateDB) SubBalance(addr common.Address, amount *big.Int) {
|
||||
stateObject := self.GetOrNewStateObject(addr)
|
||||
func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) {
|
||||
stateObject := s.GetOrNewStateObject(addr)
|
||||
if stateObject != nil {
|
||||
stateObject.SubBalance(amount)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *StateDB) SetBalance(addr common.Address, amount *big.Int) {
|
||||
stateObject := self.GetOrNewStateObject(addr)
|
||||
func (s *StateDB) SetBalance(addr common.Address, amount *big.Int) {
|
||||
stateObject := s.GetOrNewStateObject(addr)
|
||||
if stateObject != nil {
|
||||
stateObject.SetBalance(amount)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *StateDB) SetNonce(addr common.Address, nonce uint64) {
|
||||
stateObject := self.GetOrNewStateObject(addr)
|
||||
func (s *StateDB) SetNonce(addr common.Address, nonce uint64) {
|
||||
stateObject := s.GetOrNewStateObject(addr)
|
||||
if stateObject != nil {
|
||||
stateObject.SetNonce(nonce)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *StateDB) SetCode(addr common.Address, code []byte) {
|
||||
stateObject := self.GetOrNewStateObject(addr)
|
||||
func (s *StateDB) SetCode(addr common.Address, code []byte) {
|
||||
stateObject := s.GetOrNewStateObject(addr)
|
||||
if stateObject != nil {
|
||||
stateObject.SetCode(crypto.Keccak256Hash(code), code)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *StateDB) SetState(addr common.Address, key, value common.Hash) {
|
||||
stateObject := self.GetOrNewStateObject(addr)
|
||||
func (s *StateDB) SetState(addr common.Address, key, value common.Hash) {
|
||||
stateObject := s.GetOrNewStateObject(addr)
|
||||
if stateObject != nil {
|
||||
stateObject.SetState(self.db, key, value)
|
||||
stateObject.SetState(s.db, key, value)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -321,12 +321,12 @@ func (self *StateDB) SetState(addr common.Address, key, value common.Hash) {
|
|||
//
|
||||
// The account's state object is still available until the state is committed,
|
||||
// getStateObject will return a non-nil account after Suicide.
|
||||
func (self *StateDB) Suicide(addr common.Address) bool {
|
||||
stateObject := self.getStateObject(addr)
|
||||
func (s *StateDB) Suicide(addr common.Address) bool {
|
||||
stateObject := s.getStateObject(addr)
|
||||
if stateObject == nil {
|
||||
return false
|
||||
}
|
||||
self.journal.append(suicideChange{
|
||||
s.journal.append(suicideChange{
|
||||
account: &addr,
|
||||
prev: stateObject.suicided,
|
||||
prevbalance: new(big.Int).Set(stateObject.Balance()),
|
||||
|
|
@ -342,26 +342,26 @@ func (self *StateDB) Suicide(addr common.Address) bool {
|
|||
//
|
||||
|
||||
// updateStateObject writes the given object to the trie.
|
||||
func (self *StateDB) updateStateObject(stateObject *stateObject) {
|
||||
func (s *StateDB) updateStateObject(stateObject *stateObject) {
|
||||
addr := stateObject.Address()
|
||||
data, err := rlp.EncodeToBytes(stateObject)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err))
|
||||
}
|
||||
self.setError(self.trie.TryUpdate(addr[:], data))
|
||||
s.setError(s.trie.TryUpdate(addr[:], data))
|
||||
}
|
||||
|
||||
// deleteStateObject removes the given object from the state trie.
|
||||
func (self *StateDB) deleteStateObject(stateObject *stateObject) {
|
||||
func (s *StateDB) deleteStateObject(stateObject *stateObject) {
|
||||
stateObject.deleted = true
|
||||
addr := stateObject.Address()
|
||||
self.setError(self.trie.TryDelete(addr[:]))
|
||||
s.setError(s.trie.TryDelete(addr[:]))
|
||||
}
|
||||
|
||||
// Retrieve a state object given my the address. Returns nil if not found.
|
||||
func (self *StateDB) getStateObject(addr common.Address) (stateObject *stateObject) {
|
||||
func (s *StateDB) getStateObject(addr common.Address) (stateObject *stateObject) {
|
||||
// Prefer 'live' objects.
|
||||
if obj := self.stateObjects[addr]; obj != nil {
|
||||
if obj := s.stateObjects[addr]; obj != nil {
|
||||
if obj.deleted {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -369,9 +369,9 @@ func (self *StateDB) getStateObject(addr common.Address) (stateObject *stateObje
|
|||
}
|
||||
|
||||
// Load the object from the database.
|
||||
enc, err := self.trie.TryGet(addr[:])
|
||||
enc, err := s.trie.TryGet(addr[:])
|
||||
if len(enc) == 0 {
|
||||
self.setError(err)
|
||||
s.setError(err)
|
||||
return nil
|
||||
}
|
||||
var data Account
|
||||
|
|
@ -380,36 +380,36 @@ func (self *StateDB) getStateObject(addr common.Address) (stateObject *stateObje
|
|||
return nil
|
||||
}
|
||||
// Insert into the live set.
|
||||
obj := newObject(self, addr, data)
|
||||
self.setStateObject(obj)
|
||||
obj := newObject(s, addr, data)
|
||||
s.setStateObject(obj)
|
||||
return obj
|
||||
}
|
||||
|
||||
func (self *StateDB) setStateObject(object *stateObject) {
|
||||
self.stateObjects[object.Address()] = object
|
||||
func (s *StateDB) setStateObject(object *stateObject) {
|
||||
s.stateObjects[object.Address()] = object
|
||||
}
|
||||
|
||||
// Retrieve a state object or create a new state object if nil.
|
||||
func (self *StateDB) GetOrNewStateObject(addr common.Address) *stateObject {
|
||||
stateObject := self.getStateObject(addr)
|
||||
// GetOrNewStateObject retrieves a state object or creates a new state object if nil.
|
||||
func (s *StateDB) GetOrNewStateObject(addr common.Address) *stateObject {
|
||||
stateObject := s.getStateObject(addr)
|
||||
if stateObject == nil || stateObject.deleted {
|
||||
stateObject, _ = self.createObject(addr)
|
||||
stateObject, _ = s.createObject(addr)
|
||||
}
|
||||
return stateObject
|
||||
}
|
||||
|
||||
// createObject creates a new state object. If there is an existing account with
|
||||
// the given address, it is overwritten and returned as the second return value.
|
||||
func (self *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) {
|
||||
prev = self.getStateObject(addr)
|
||||
newobj = newObject(self, addr, Account{})
|
||||
func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) {
|
||||
prev = s.getStateObject(addr)
|
||||
newobj = newObject(s, addr, Account{})
|
||||
newobj.setNonce(0) // sets the object to dirty
|
||||
if prev == nil {
|
||||
self.journal.append(createObjectChange{account: &addr})
|
||||
s.journal.append(createObjectChange{account: &addr})
|
||||
} else {
|
||||
self.journal.append(resetObjectChange{prev: prev})
|
||||
s.journal.append(resetObjectChange{prev: prev})
|
||||
}
|
||||
self.setStateObject(newobj)
|
||||
s.setStateObject(newobj)
|
||||
return newobj, prev
|
||||
}
|
||||
|
||||
|
|
@ -423,15 +423,15 @@ func (self *StateDB) createObject(addr common.Address) (newobj, prev *stateObjec
|
|||
// 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)
|
||||
//
|
||||
// Carrying over the balance ensures that Ether doesn't disappear.
|
||||
func (self *StateDB) CreateAccount(addr common.Address) {
|
||||
new, prev := self.createObject(addr)
|
||||
func (s *StateDB) CreateAccount(addr common.Address) {
|
||||
new, prev := s.createObject(addr)
|
||||
if prev != nil {
|
||||
new.setBalance(prev.data.Balance)
|
||||
}
|
||||
}
|
||||
|
||||
func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) {
|
||||
so := db.getStateObject(addr)
|
||||
func (s *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) {
|
||||
so := s.getStateObject(addr)
|
||||
if so == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -441,10 +441,10 @@ func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common
|
|||
cb(h, value)
|
||||
}
|
||||
|
||||
it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil))
|
||||
it := trie.NewIterator(so.getTrie(s.db).NodeIterator(nil))
|
||||
for it.Next() {
|
||||
// ignore cached values
|
||||
key := common.BytesToHash(db.trie.GetKey(it.Key))
|
||||
key := common.BytesToHash(s.trie.GetKey(it.Key))
|
||||
if _, ok := so.cachedStorage[key]; !ok {
|
||||
cb(key, common.BytesToHash(it.Value))
|
||||
}
|
||||
|
|
@ -453,29 +453,29 @@ func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common
|
|||
|
||||
// Copy creates a deep, independent copy of the state.
|
||||
// Snapshots of the copied state cannot be applied to the copy.
|
||||
func (self *StateDB) Copy() *StateDB {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
func (s *StateDB) Copy() *StateDB {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
// Copy all the basic fields, initialize the memory ones
|
||||
state := &StateDB{
|
||||
db: self.db,
|
||||
trie: self.db.CopyTrie(self.trie),
|
||||
stateObjects: make(map[common.Address]*stateObject, len(self.journal.dirties)),
|
||||
stateObjectsDirty: make(map[common.Address]struct{}, len(self.journal.dirties)),
|
||||
refund: self.refund,
|
||||
logs: make(map[common.Hash][]*types.Log, len(self.logs)),
|
||||
logSize: self.logSize,
|
||||
db: s.db,
|
||||
trie: s.db.CopyTrie(s.trie),
|
||||
stateObjects: make(map[common.Address]*stateObject, len(s.journal.dirties)),
|
||||
stateObjectsDirty: make(map[common.Address]struct{}, len(s.journal.dirties)),
|
||||
refund: s.refund,
|
||||
logs: make(map[common.Hash][]*types.Log, len(s.logs)),
|
||||
logSize: s.logSize,
|
||||
preimages: make(map[common.Hash][]byte),
|
||||
journal: newJournal(),
|
||||
}
|
||||
// Copy the dirty states, logs, and preimages
|
||||
for addr := range self.journal.dirties {
|
||||
for addr := range s.journal.dirties {
|
||||
// As documented [here](https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527),
|
||||
// and in the Finalise-method, there is a case where an object is in the journal but not
|
||||
// in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for
|
||||
// nil
|
||||
if object, exist := self.stateObjects[addr]; exist {
|
||||
if object, exist := s.stateObjects[addr]; exist {
|
||||
state.stateObjects[addr] = object.deepCopy(state)
|
||||
state.stateObjectsDirty[addr] = struct{}{}
|
||||
}
|
||||
|
|
@ -483,50 +483,50 @@ func (self *StateDB) Copy() *StateDB {
|
|||
// Above, we don't copy the actual journal. This means that if the copy is copied, the
|
||||
// loop above will be a no-op, since the copy's journal is empty.
|
||||
// Thus, here we iterate over stateObjects, to enable copies of copies
|
||||
for addr := range self.stateObjectsDirty {
|
||||
for addr := range s.stateObjectsDirty {
|
||||
if _, exist := state.stateObjects[addr]; !exist {
|
||||
state.stateObjects[addr] = self.stateObjects[addr].deepCopy(state)
|
||||
state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state)
|
||||
state.stateObjectsDirty[addr] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
for hash, logs := range self.logs {
|
||||
for hash, logs := range s.logs {
|
||||
state.logs[hash] = make([]*types.Log, len(logs))
|
||||
copy(state.logs[hash], logs)
|
||||
}
|
||||
for hash, preimage := range self.preimages {
|
||||
for hash, preimage := range s.preimages {
|
||||
state.preimages[hash] = preimage
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
||||
// Snapshot returns an identifier for the current revision of the state.
|
||||
func (self *StateDB) Snapshot() int {
|
||||
id := self.nextRevisionId
|
||||
self.nextRevisionId++
|
||||
self.validRevisions = append(self.validRevisions, revision{id, self.journal.length()})
|
||||
func (s *StateDB) Snapshot() int {
|
||||
id := s.nextRevisionId
|
||||
s.nextRevisionId++
|
||||
s.validRevisions = append(s.validRevisions, revision{id, s.journal.length()})
|
||||
return id
|
||||
}
|
||||
|
||||
// RevertToSnapshot reverts all state changes made since the given revision.
|
||||
func (self *StateDB) RevertToSnapshot(revid int) {
|
||||
func (s *StateDB) RevertToSnapshot(revid int) {
|
||||
// Find the snapshot in the stack of valid snapshots.
|
||||
idx := sort.Search(len(self.validRevisions), func(i int) bool {
|
||||
return self.validRevisions[i].id >= revid
|
||||
idx := sort.Search(len(s.validRevisions), func(i int) bool {
|
||||
return s.validRevisions[i].id >= revid
|
||||
})
|
||||
if idx == len(self.validRevisions) || self.validRevisions[idx].id != revid {
|
||||
if idx == len(s.validRevisions) || s.validRevisions[idx].id != revid {
|
||||
panic(fmt.Errorf("revision id %v cannot be reverted", revid))
|
||||
}
|
||||
snapshot := self.validRevisions[idx].journalIndex
|
||||
snapshot := s.validRevisions[idx].journalIndex
|
||||
|
||||
// Replay the journal to undo changes and remove invalidated snapshots
|
||||
self.journal.revert(self, snapshot)
|
||||
self.validRevisions = self.validRevisions[:idx]
|
||||
s.journal.revert(s, snapshot)
|
||||
s.validRevisions = s.validRevisions[:idx]
|
||||
}
|
||||
|
||||
// GetRefund returns the current value of the refund counter.
|
||||
func (self *StateDB) GetRefund() uint64 {
|
||||
return self.refund
|
||||
func (s *StateDB) GetRefund() uint64 {
|
||||
return s.refund
|
||||
}
|
||||
|
||||
// Finalise finalises the state by removing the self destructed objects
|
||||
|
|
@ -566,10 +566,10 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
|
|||
|
||||
// Prepare sets the current transaction hash and index and block hash which is
|
||||
// used when the EVM emits new state logs.
|
||||
func (self *StateDB) Prepare(thash, bhash common.Hash, ti int) {
|
||||
self.thash = thash
|
||||
self.bhash = bhash
|
||||
self.txIndex = ti
|
||||
func (s *StateDB) Prepare(thash, bhash common.Hash, ti int) {
|
||||
s.thash = thash
|
||||
s.bhash = bhash
|
||||
s.txIndex = ti
|
||||
}
|
||||
|
||||
func (s *StateDB) clearJournalAndRefund() {
|
||||
|
|
|
|||
|
|
@ -1282,13 +1282,13 @@ func TestTransactionPoolRepricingKeepsLocals(t *testing.T) {
|
|||
// Create transaction (both pending and queued) with a linearly growing gasprice
|
||||
for i := uint64(0); i < 500; i++ {
|
||||
// Add pending
|
||||
p_tx := pricedTransaction(i, 100000, big.NewInt(int64(i)), keys[2])
|
||||
if err := pool.AddLocal(p_tx); err != nil {
|
||||
pTx := pricedTransaction(i, 100000, big.NewInt(int64(i)), keys[2])
|
||||
if err := pool.AddLocal(pTx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Add queued
|
||||
q_tx := pricedTransaction(i+501, 100000, big.NewInt(int64(i)), keys[2])
|
||||
if err := pool.AddLocal(q_tx); err != nil {
|
||||
qTx := pricedTransaction(i+501, 100000, big.NewInt(int64(i)), keys[2])
|
||||
if err := pool.AddLocal(qTx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -277,7 +277,7 @@ func (b *Block) EncodeRLP(w io.Writer) error {
|
|||
})
|
||||
}
|
||||
|
||||
// [deprecated by eth/63]
|
||||
// DecodeRLP was [deprecated by eth/63]
|
||||
func (b *StorageBlock) DecodeRLP(s *rlp.Stream) error {
|
||||
var sb storageblock
|
||||
if err := s.Decode(&sb); err != nil {
|
||||
|
|
@ -392,10 +392,10 @@ type Blocks []*Block
|
|||
|
||||
type BlockBy func(b1, b2 *Block) bool
|
||||
|
||||
func (self BlockBy) Sort(blocks Blocks) {
|
||||
func (b BlockBy) Sort(blocks Blocks) {
|
||||
bs := blockSorter{
|
||||
blocks: blocks,
|
||||
by: self,
|
||||
by: b,
|
||||
}
|
||||
sort.Sort(bs)
|
||||
}
|
||||
|
|
@ -405,10 +405,10 @@ type blockSorter struct {
|
|||
by func(b1, b2 *Block) bool
|
||||
}
|
||||
|
||||
func (self blockSorter) Len() int { return len(self.blocks) }
|
||||
func (self blockSorter) Swap(i, j int) {
|
||||
self.blocks[i], self.blocks[j] = self.blocks[j], self.blocks[i]
|
||||
func (s blockSorter) Len() int { return len(s.blocks) }
|
||||
func (s blockSorter) Swap(i, j int) {
|
||||
s.blocks[i], s.blocks[j] = s.blocks[j], s.blocks[i]
|
||||
}
|
||||
func (self blockSorter) Less(i, j int) bool { return self.by(self.blocks[i], self.blocks[j]) }
|
||||
func (s blockSorter) Less(i, j int) bool { return s.by(s.blocks[i], s.blocks[j]) }
|
||||
|
||||
func Number(b1, b2 *Block) bool { return b1.header.Number.Cmp(b2.header.Number) < 0 }
|
||||
|
|
|
|||
|
|
@ -39,9 +39,8 @@ var (
|
|||
func deriveSigner(V *big.Int) Signer {
|
||||
if V.Sign() != 0 && isProtectedV(V) {
|
||||
return NewEIP155Signer(deriveChainId(V))
|
||||
} else {
|
||||
return HomesteadSigner{}
|
||||
}
|
||||
return HomesteadSigner{}
|
||||
}
|
||||
|
||||
type Transaction struct {
|
||||
|
|
|
|||
|
|
@ -168,7 +168,7 @@ func (s EIP155Signer) Hash(tx *Transaction) common.Hash {
|
|||
// homestead rules.
|
||||
type HomesteadSigner struct{ FrontierSigner }
|
||||
|
||||
func (s HomesteadSigner) Equal(s2 Signer) bool {
|
||||
func (hs HomesteadSigner) Equal(s2 Signer) bool {
|
||||
_, ok := s2.(HomesteadSigner)
|
||||
return ok
|
||||
}
|
||||
|
|
@ -185,7 +185,7 @@ func (hs HomesteadSigner) Sender(tx *Transaction) (common.Address, error) {
|
|||
|
||||
type FrontierSigner struct{}
|
||||
|
||||
func (s FrontierSigner) Equal(s2 Signer) bool {
|
||||
func (fs FrontierSigner) Equal(s2 Signer) bool {
|
||||
_, ok := s2.(FrontierSigner)
|
||||
return ok
|
||||
}
|
||||
|
|
|
|||
|
|
@ -861,7 +861,7 @@ func makeDup(size int64) executionFunc {
|
|||
// make swap instruction function
|
||||
func makeSwap(size int64) executionFunc {
|
||||
// switch n + 1 otherwise n would be swapped with n
|
||||
size += 1
|
||||
size++
|
||||
return func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
||||
stack.swap(int(size))
|
||||
return nil, nil
|
||||
|
|
|
|||
|
|
@ -21,8 +21,8 @@ import (
|
|||
"math/big"
|
||||
)
|
||||
|
||||
// stack is an object for basic stack operations. Items popped to the stack are
|
||||
// expected to be changed and modified. stack does not take care of adding newly
|
||||
// Stack is an object for basic stack operations. Items popped to the stack are
|
||||
// expected to be changed and modified. Stack does not take care of adding newly
|
||||
// initialised objects.
|
||||
type Stack struct {
|
||||
data []*big.Int
|
||||
|
|
|
|||
|
|
@ -35,8 +35,8 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
secp256k1_N, _ = new(big.Int).SetString("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16)
|
||||
secp256k1_halfN = new(big.Int).Div(secp256k1_N, big.NewInt(2))
|
||||
secp256k1N, _ = new(big.Int).SetString("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16)
|
||||
secp256k1halfN = new(big.Int).Div(secp256k1N, big.NewInt(2))
|
||||
)
|
||||
|
||||
// Keccak256 calculates and returns the Keccak256 hash of the input data.
|
||||
|
|
@ -68,7 +68,7 @@ func Keccak512(data ...[]byte) []byte {
|
|||
return d.Sum(nil)
|
||||
}
|
||||
|
||||
// Creates an ethereum address given the bytes and the nonce
|
||||
// CreateAddress creates an ethereum address given the bytes and the nonce
|
||||
func CreateAddress(b common.Address, nonce uint64) common.Address {
|
||||
data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
|
||||
return common.BytesToAddress(Keccak256(data)[12:])
|
||||
|
|
@ -99,7 +99,7 @@ func toECDSA(d []byte, strict bool) (*ecdsa.PrivateKey, error) {
|
|||
priv.D = new(big.Int).SetBytes(d)
|
||||
|
||||
// The priv.D must < N
|
||||
if priv.D.Cmp(secp256k1_N) >= 0 {
|
||||
if priv.D.Cmp(secp256k1N) >= 0 {
|
||||
return nil, fmt.Errorf("invalid private key, >=N")
|
||||
}
|
||||
// The priv.D must not be zero or negative.
|
||||
|
|
@ -184,11 +184,11 @@ func ValidateSignatureValues(v byte, r, s *big.Int, homestead bool) bool {
|
|||
}
|
||||
// reject upper range of s values (ECDSA malleability)
|
||||
// see discussion in secp256k1/libsecp256k1/include/secp256k1.h
|
||||
if homestead && s.Cmp(secp256k1_halfN) > 0 {
|
||||
if homestead && s.Cmp(secp256k1halfN) > 0 {
|
||||
return false
|
||||
}
|
||||
// Frontier: allow s to be in full N range
|
||||
return r.Cmp(secp256k1_N) < 0 && s.Cmp(secp256k1_N) < 0 && (v == 0 || v == 1)
|
||||
return r.Cmp(secp256k1N) < 0 && s.Cmp(secp256k1N) < 0 && (v == 0 || v == 1)
|
||||
}
|
||||
|
||||
func PubkeyToAddress(p ecdsa.PublicKey) common.Address {
|
||||
|
|
|
|||
|
|
@ -154,7 +154,7 @@ func TestValidateSignatureValues(t *testing.T) {
|
|||
minusOne := big.NewInt(-1)
|
||||
one := common.Big1
|
||||
zero := common.Big0
|
||||
secp256k1nMinus1 := new(big.Int).Sub(secp256k1_N, common.Big1)
|
||||
secp256k1nMinus1 := new(big.Int).Sub(secp256k1N, common.Big1)
|
||||
|
||||
// correct v,r,s
|
||||
check(true, 0, one, one)
|
||||
|
|
@ -181,9 +181,9 @@ func TestValidateSignatureValues(t *testing.T) {
|
|||
// correct sig with max r,s
|
||||
check(true, 0, secp256k1nMinus1, secp256k1nMinus1)
|
||||
// correct v, combinations of incorrect r,s at upper limit
|
||||
check(false, 0, secp256k1_N, secp256k1nMinus1)
|
||||
check(false, 0, secp256k1nMinus1, secp256k1_N)
|
||||
check(false, 0, secp256k1_N, secp256k1_N)
|
||||
check(false, 0, secp256k1N, secp256k1nMinus1)
|
||||
check(false, 0, secp256k1nMinus1, secp256k1N)
|
||||
check(false, 0, secp256k1N, secp256k1N)
|
||||
|
||||
// current callers ensures r,s cannot be negative, but let's test for that too
|
||||
// as crypto package could be used stand-alone
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ func (BitCurve *BitCurve) Params() *elliptic.CurveParams {
|
|||
}
|
||||
}
|
||||
|
||||
// IsOnBitCurve returns true if the given (x,y) lies on the BitCurve.
|
||||
// IsOnCurve returns true if the given (x,y) lies on the BitCurve.
|
||||
func (BitCurve *BitCurve) IsOnCurve(x, y *big.Int) bool {
|
||||
// y² = x³ + b
|
||||
y2 := new(big.Int).Mul(y, y) //y²
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ func randSig() []byte {
|
|||
// tests for malleability
|
||||
// highest bit of signature ECDSA s value must be 0, in the 33th byte
|
||||
func compactSigCheck(t *testing.T, sig []byte) {
|
||||
var b int = int(sig[32])
|
||||
var b = int(sig[32])
|
||||
if b < 0 {
|
||||
t.Errorf("highest bit is negative: %d", b)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ func VerifySignature(pubkey, hash, signature []byte) bool {
|
|||
return false
|
||||
}
|
||||
// Reject malleable signatures. libsecp256k1 does this check but btcec doesn't.
|
||||
if sig.S.Cmp(secp256k1_halfN) > 0 {
|
||||
if sig.S.Cmp(secp256k1halfN) > 0 {
|
||||
return false
|
||||
}
|
||||
return sig.Verify(hash, key)
|
||||
|
|
|
|||
Loading…
Reference in a new issue