Implement register and watchTx

This commit is contained in:
obscuren 2015-02-17 16:54:52 +01:00
parent 5238b9439e
commit 13c00afc68
3 changed files with 88 additions and 2 deletions

View File

@ -1417,6 +1417,8 @@ var ethMethods = function () {
var methods = [
{ name: 'balanceAt', call: 'eth_balanceAt' },
{ name: 'register', call: 'eth_register' },
{ name: 'unregister', call: 'eth_unregister' },
{ name: 'stateAt', call: 'eth_stateAt' },
{ name: 'storageAt', call: 'eth_storageAt' },
{ name: 'countAt', call: 'eth_countAt'},

View File

@ -342,3 +342,31 @@ func (req *RpcRequest) ToWhisperHasIdentityArgs() (string, error) {
rpclogger.DebugDetailf("%T %v", args, args)
return args, nil
}
func (req *RpcRequest) ToRegisterArgs() (string, error) {
if len(req.Params) < 1 {
return "", NewErrorResponse(ErrorArguments)
}
var args string
err := json.Unmarshal(req.Params[0], &args)
if err != nil {
return "", err
}
rpclogger.DebugDetailf("%T %v", args, args)
return args, nil
}
func (req *RpcRequest) ToWatchTxArgs() (string, error) {
if len(req.Params) < 1 {
return "", NewErrorResponse(ErrorArguments)
}
var args string
err := json.Unmarshal(req.Params[0], &args)
if err != nil {
return "", err
}
rpclogger.DebugDetailf("%T %v", args, args)
return args, nil
}

View File

@ -55,6 +55,9 @@ type EthereumApi struct {
messagesMut sync.RWMutex
messages map[int][]xeth.WhisperMessage
// Register keeps a list of accounts and transaction data
regmut sync.Mutex
register map[string][]*NewTxArgs
db ethutil.Database
}
@ -73,6 +76,36 @@ func NewEthereumApi(eth *xeth.XEth) *EthereumApi {
return api
}
func (self *EthereumApi) Register(args string, reply *interface{}) error {
self.regmut.Lock()
defer self.regmut.Unlock()
if _, ok := self.register[args]; ok {
self.register[args] = nil // register with empty
}
return nil
}
func (self *EthereumApi) Unregister(args string, reply *interface{}) error {
self.regmut.Lock()
defer self.regmut.Unlock()
delete(self.register, args)
return nil
}
func (self *EthereumApi) WatchTx(args string, reply *interface{}) error {
self.regmut.Lock()
defer self.regmut.Unlock()
txs := self.register[args]
self.register[args] = nil
*reply = txs
return nil
}
func (self *EthereumApi) NewFilter(args *FilterOptions, reply *interface{}) error {
var id int
filter := core.NewFilter(self.xeth.Backend())
@ -149,8 +182,13 @@ func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) error {
args.GasPrice = defaultGasPrice
}
// TODO if no_private_key then
if _, exists := p.register[args.From]; exists {
p.register[args.From] = append(p.register[args.From], args)
} else {
result, _ := p.xeth.Transact( /* TODO specify account */ args.To, args.Value, args.Gas, args.GasPrice, args.Data)
*reply = result
}
return nil
}
@ -424,6 +462,24 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
case "eth_gasPrice":
*reply = defaultGasPrice
return nil
case "eth_register":
args, err := req.ToRegisterArgs()
if err != nil {
return err
}
return p.Register(args, reply)
case "eth_unregister":
args, err := req.ToRegisterArgs()
if err != nil {
return err
}
return p.Unregister(args, reply)
case "eth_watchTx":
args, err := req.ToWatchTxArgs()
if err != nil {
return err
}
return p.WatchTx(args, reply)
case "web3_sha3":
args, err := req.ToSha3Args()
if err != nil {