Defautl block

This commit is contained in:
obscuren 2015-02-26 12:06:10 +01:00
parent 49ded3aa77
commit a1c830cd3c
3 changed files with 529 additions and 357 deletions

View File

@ -62,6 +62,8 @@
web3.setProvider(new web3.providers.HttpSyncProvider('http://localhost:8545')); web3.setProvider(new web3.providers.HttpSyncProvider('http://localhost:8545'));
eth.defaultBlock = -2
document.querySelector("#number").innerHTML = eth.number; document.querySelector("#number").innerHTML = eth.number;
document.querySelector("#coinbase").innerHTML = eth.coinbase document.querySelector("#coinbase").innerHTML = eth.coinbase
document.querySelector("#peer_count").innerHTML = eth.peerCount; document.querySelector("#peer_count").innerHTML = eth.peerCount;
@ -72,8 +74,9 @@
document.querySelector("#mining").innerHTML = eth.mining; document.querySelector("#mining").innerHTML = eth.mining;
document.querySelector("#listening").innerHTML = eth.listening; document.querySelector("#listening").innerHTML = eth.listening;
eth.watch('chain').changed(function() { eth.watch('chain').changed(function() {
document.querySelector("#number").innerHTML = eth.number; document.querySelector("#number").innerHTML = eth.number;
}); });
</script> </script>

File diff suppressed because it is too large Load Diff

View File

@ -52,19 +52,20 @@ type EthereumApi struct {
db ethutil.Database db ethutil.Database
defaultBlockAge int defaultBlockAge int64
} }
func NewEthereumApi(eth *xeth.XEth) *EthereumApi { func NewEthereumApi(eth *xeth.XEth) *EthereumApi {
db, _ := ethdb.NewLDBDatabase("dapps") db, _ := ethdb.NewLDBDatabase("dapps")
api := &EthereumApi{ api := &EthereumApi{
eth: eth, eth: eth,
mux: eth.Backend().EventMux(), mux: eth.Backend().EventMux(),
quit: make(chan struct{}), quit: make(chan struct{}),
filterManager: filter.NewFilterManager(eth.Backend().EventMux()), filterManager: filter.NewFilterManager(eth.Backend().EventMux()),
logs: make(map[int]*logFilter), logs: make(map[int]*logFilter),
messages: make(map[int]*whisperFilter), messages: make(map[int]*whisperFilter),
db: db, db: db,
defaultBlockAge: -1,
} }
go api.filterManager.Start() go api.filterManager.Start()
go api.start() go api.start()
@ -72,6 +73,22 @@ func NewEthereumApi(eth *xeth.XEth) *EthereumApi {
return api return api
} }
func (self *EthereumApi) setStateByBlockNumber(num int64) {
chain := self.xeth().Backend().ChainManager()
var block *types.Block
if self.defaultBlockAge < 0 {
num = chain.CurrentBlock().Number().Int64() + num + 1
}
block = chain.GetBlockByNumber(uint64(num))
if block != nil {
self.useState(state.New(block.Root(), self.xeth().Backend().Db()))
} else {
self.useState(chain.State())
}
}
func (self *EthereumApi) start() { func (self *EthereumApi) start() {
timer := time.NewTicker(filterTickerTime) timer := time.NewTicker(filterTickerTime)
events := self.mux.Subscribe(core.ChainEvent{}) events := self.mux.Subscribe(core.ChainEvent{})
@ -83,12 +100,7 @@ done:
switch ev.(type) { switch ev.(type) {
case core.ChainEvent: case core.ChainEvent:
if self.defaultBlockAge < 0 { if self.defaultBlockAge < 0 {
chain := self.xeth().Backend().ChainManager() self.setStateByBlockNumber(self.defaultBlockAge)
block := chain.GetBlockByNumber(chain.CurrentBlock().Number().Uint64() - uint64(self.defaultBlockAge))
if block != nil {
statedb := state.New(block.Root(), self.db)
self.useState(statedb)
}
} }
} }
case <-timer.C: case <-timer.C:
@ -239,21 +251,6 @@ func (p *EthereumApi) GetBlock(args *GetBlockArgs, reply *interface{}) error {
return nil return nil
} }
/*
func unlockAccount(server, account *Account) bool {
pwd, status := server.PasswordDialog()
switch status {
case Ok:
if !account.Unlock([]byte(pwd)) {
return unlockAccount(account)
}
return true
default:
return false
}
}
*/
func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) error { func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) error {
if len(args.Gas) == 0 { if len(args.Gas) == 0 {
args.Gas = defaultGas.String() args.Gas = defaultGas.String()
@ -378,8 +375,10 @@ func (p *EthereumApi) GetDefaultBlockAge(reply *interface{}) error {
return nil return nil
} }
func (p *EthereumApi) SetDefaultBlockAge(defaultBlockAge int, reply *interface{}) error { func (p *EthereumApi) SetDefaultBlockAge(defaultBlockAge int64, reply *interface{}) error {
p.defaultBlockAge = defaultBlockAge p.defaultBlockAge = defaultBlockAge
p.setStateByBlockNumber(p.defaultBlockAge)
*reply = true *reply = true
return nil return nil
} }
@ -531,7 +530,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
if err != nil { if err != nil {
return err return err
} }
return p.SetDefaultBlockAge(args, reply) return p.SetDefaultBlockAge(int64(args), reply)
case "eth_peerCount": case "eth_peerCount":
return p.GetPeerCount(reply) return p.GetPeerCount(reply)
case "eth_number": case "eth_number":
@ -720,7 +719,7 @@ func (self *EthereumApi) useState(statedb *state.StateDB) {
self.xethMu.Lock() self.xethMu.Lock()
defer self.xethMu.Unlock() defer self.xethMu.Unlock()
self.eth = self.xeth().UseState(statedb) self.eth = self.eth.UseState(statedb)
} }
func t(f ui.Frontend) { func t(f ui.Frontend) {