mirror of https://github.com/ethereum/go-ethereum
commit
81800ca39e
@ -0,0 +1,68 @@ |
||||
package core |
||||
|
||||
import ( |
||||
"sync" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
) |
||||
|
||||
// BlockCache implements a caching mechanism specifically for blocks and uses FILO to pop
|
||||
type BlockCache struct { |
||||
size int |
||||
|
||||
hashes []common.Hash |
||||
blocks map[common.Hash]*types.Block |
||||
|
||||
mu sync.RWMutex |
||||
} |
||||
|
||||
// Creates and returns a `BlockCache` with `size`. If `size` is smaller than 1 it will panic
|
||||
func NewBlockCache(size int) *BlockCache { |
||||
if size < 1 { |
||||
panic("block cache size not allowed to be smaller than 1") |
||||
} |
||||
|
||||
bc := &BlockCache{size: size} |
||||
bc.Clear() |
||||
return bc |
||||
} |
||||
|
||||
func (bc *BlockCache) Clear() { |
||||
bc.blocks = make(map[common.Hash]*types.Block) |
||||
bc.hashes = nil |
||||
|
||||
} |
||||
|
||||
func (bc *BlockCache) Push(block *types.Block) { |
||||
bc.mu.Lock() |
||||
defer bc.mu.Unlock() |
||||
|
||||
if len(bc.hashes) == bc.size { |
||||
delete(bc.blocks, bc.hashes[0]) |
||||
|
||||
// XXX There are a few other options on solving this
|
||||
// 1) use a poller / GC like mechanism to clean up untracked objects
|
||||
// 2) copy as below
|
||||
// re-use the slice and remove the reference to bc.hashes[0]
|
||||
// this will allow the element to be garbage collected.
|
||||
copy(bc.hashes, bc.hashes[1:]) |
||||
} else { |
||||
bc.hashes = append(bc.hashes, common.Hash{}) |
||||
} |
||||
|
||||
hash := block.Hash() |
||||
bc.blocks[hash] = block |
||||
bc.hashes[len(bc.hashes)-1] = hash |
||||
} |
||||
|
||||
func (bc *BlockCache) Get(hash common.Hash) *types.Block { |
||||
bc.mu.RLock() |
||||
defer bc.mu.RUnlock() |
||||
|
||||
if block, haz := bc.blocks[hash]; haz { |
||||
return block |
||||
} |
||||
|
||||
return nil |
||||
} |
@ -0,0 +1,48 @@ |
||||
package core |
||||
|
||||
import ( |
||||
"math/big" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
) |
||||
|
||||
func newChain(size int) (chain []*types.Block) { |
||||
var parentHash common.Hash |
||||
for i := 0; i < size; i++ { |
||||
block := types.NewBlock(parentHash, common.Address{}, common.Hash{}, new(big.Int), 0, "") |
||||
block.Header().Number = big.NewInt(int64(i)) |
||||
chain = append(chain, block) |
||||
parentHash = block.Hash() |
||||
} |
||||
return |
||||
} |
||||
|
||||
func insertChainCache(cache *BlockCache, chain []*types.Block) { |
||||
for _, block := range chain { |
||||
cache.Push(block) |
||||
} |
||||
} |
||||
|
||||
func TestNewBlockCache(t *testing.T) { |
||||
chain := newChain(3) |
||||
cache := NewBlockCache(2) |
||||
insertChainCache(cache, chain) |
||||
|
||||
if cache.hashes[0] != chain[1].Hash() { |
||||
t.Error("oldest block incorrect") |
||||
} |
||||
} |
||||
|
||||
func TestInclusion(t *testing.T) { |
||||
chain := newChain(3) |
||||
cache := NewBlockCache(3) |
||||
insertChainCache(cache, chain) |
||||
|
||||
for _, block := range chain { |
||||
if b := cache.Get(block.Hash()); b == nil { |
||||
t.Errorf("getting %x failed", block.Hash()) |
||||
} |
||||
} |
||||
} |
@ -1,80 +0,0 @@ |
||||
package eth |
||||
|
||||
/* |
||||
import ( |
||||
"crypto/ecdsa" |
||||
"errors" |
||||
"math/big" |
||||
|
||||
"github.com/ethereum/go-ethereum/core" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
) |
||||
|
||||
type Account struct { |
||||
w *Wallet |
||||
} |
||||
|
||||
func (self *Account) Transact(to *Account, value, gas, price *big.Int, data []byte) error { |
||||
return self.w.transact(self, to, value, gas, price, data) |
||||
} |
||||
|
||||
func (self *Account) Address() []byte { |
||||
return nil |
||||
} |
||||
|
||||
func (self *Account) PrivateKey() *ecdsa.PrivateKey { |
||||
return nil |
||||
} |
||||
|
||||
type Wallet struct{} |
||||
|
||||
func NewWallet() *Wallet { |
||||
return &Wallet{} |
||||
} |
||||
|
||||
func (self *Wallet) GetAccount(i int) *Account { |
||||
} |
||||
|
||||
func (self *Wallet) transact(from, to *Account, value, gas, price *big.Int, data []byte) error { |
||||
if from.PrivateKey() == nil { |
||||
return errors.New("accounts is not owned (no private key available)") |
||||
} |
||||
|
||||
var createsContract bool |
||||
if to == nil { |
||||
createsContract = true |
||||
} |
||||
|
||||
var msg *types.Transaction |
||||
if contractCreation { |
||||
msg = types.NewContractCreationTx(value, gas, price, data) |
||||
} else { |
||||
msg = types.NewTransactionMessage(to.Address(), value, gas, price, data) |
||||
} |
||||
|
||||
state := self.chainManager.TransState() |
||||
nonce := state.GetNonce(key.Address()) |
||||
|
||||
msg.SetNonce(nonce) |
||||
msg.SignECDSA(from.PriateKey()) |
||||
|
||||
// Do some pre processing for our "pre" events and hooks
|
||||
block := self.chainManager.NewBlock(from.Address()) |
||||
coinbase := state.GetOrNewStateObject(from.Address()) |
||||
coinbase.SetGasPool(block.GasLimit()) |
||||
self.blockManager.ApplyTransactions(coinbase, state, block, types.Transactions{tx}, true) |
||||
|
||||
err := self.obj.TxPool().Add(tx) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
state.SetNonce(key.Address(), nonce+1) |
||||
|
||||
if contractCreation { |
||||
addr := core.AddressFromMessage(tx) |
||||
pipelogger.Infof("Contract addr %x\n", addr) |
||||
} |
||||
|
||||
return tx, nil |
||||
} |
||||
*/ |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,41 @@ |
||||
package rpc |
||||
|
||||
import ( |
||||
"testing" |
||||
) |
||||
|
||||
func TestInsufficientParamsError(t *testing.T) { |
||||
err := NewInsufficientParamsError(0, 1) |
||||
expected := "insufficient params, want 1 have 0" |
||||
|
||||
if err.Error() != expected { |
||||
t.Error(err.Error()) |
||||
} |
||||
} |
||||
|
||||
func TestNotImplementedError(t *testing.T) { |
||||
err := NewNotImplementedError("foo") |
||||
expected := "foo method not implemented" |
||||
|
||||
if err.Error() != expected { |
||||
t.Error(err.Error()) |
||||
} |
||||
} |
||||
|
||||
func TestDecodeParamError(t *testing.T) { |
||||
err := NewDecodeParamError("foo") |
||||
expected := "could not decode, foo" |
||||
|
||||
if err.Error() != expected { |
||||
t.Error(err.Error()) |
||||
} |
||||
} |
||||
|
||||
func TestValidationError(t *testing.T) { |
||||
err := NewValidationError("foo", "should be `bar`") |
||||
expected := "foo not valid, should be `bar`" |
||||
|
||||
if err.Error() != expected { |
||||
t.Error(err.Error()) |
||||
} |
||||
} |
@ -1,86 +0,0 @@ |
||||
/* |
||||
This file is part of go-ethereum |
||||
|
||||
go-ethereum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
go-ethereum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package rpc |
||||
|
||||
import ( |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/logger" |
||||
"github.com/ethereum/go-ethereum/state" |
||||
"github.com/ethereum/go-ethereum/xeth" |
||||
) |
||||
|
||||
var rpclogger = logger.NewLogger("RPC") |
||||
|
||||
type Log struct { |
||||
Address string `json:"address"` |
||||
Topic []string `json:"topic"` |
||||
Data string `json:"data"` |
||||
Number uint64 `json:"number"` |
||||
} |
||||
|
||||
func toLogs(logs state.Logs) (ls []Log) { |
||||
ls = make([]Log, len(logs)) |
||||
|
||||
for i, log := range logs { |
||||
var l Log |
||||
l.Topic = make([]string, len(log.Topics())) |
||||
l.Address = log.Address().Hex() |
||||
l.Data = common.ToHex(log.Data()) |
||||
l.Number = log.Number() |
||||
for j, topic := range log.Topics() { |
||||
l.Topic[j] = topic.Hex() |
||||
} |
||||
ls[i] = l |
||||
} |
||||
|
||||
return |
||||
} |
||||
|
||||
type whisperFilter struct { |
||||
messages []xeth.WhisperMessage |
||||
timeout time.Time |
||||
id int |
||||
} |
||||
|
||||
func (w *whisperFilter) add(msgs ...xeth.WhisperMessage) { |
||||
w.messages = append(w.messages, msgs...) |
||||
} |
||||
func (w *whisperFilter) get() []xeth.WhisperMessage { |
||||
w.timeout = time.Now() |
||||
tmp := w.messages |
||||
w.messages = nil |
||||
return tmp |
||||
} |
||||
|
||||
type logFilter struct { |
||||
logs state.Logs |
||||
timeout time.Time |
||||
id int |
||||
} |
||||
|
||||
func (l *logFilter) add(logs ...state.Log) { |
||||
l.logs = append(l.logs, logs...) |
||||
} |
||||
|
||||
func (l *logFilter) get() state.Logs { |
||||
l.timeout = time.Now() |
||||
tmp := l.logs |
||||
l.logs = nil |
||||
return tmp |
||||
} |
Loading…
Reference in new issue