diff options
Diffstat (limited to 'rpc')
-rw-r--r-- | rpc/api/admin.go | 48 | ||||
-rw-r--r-- | rpc/api/admin_args.go | 31 |
2 files changed, 79 insertions, 0 deletions
diff --git a/rpc/api/admin.go b/rpc/api/admin.go index b27482cfe..78c75a60b 100644 --- a/rpc/api/admin.go +++ b/rpc/api/admin.go @@ -3,7 +3,9 @@ package api import ( "fmt" "io" + "math/big" "os" + "time" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" @@ -244,3 +246,49 @@ func (self *adminApi) StopRPC(req *shared.Request) (interface{}, error) { comms.StopHttp() return true, nil } + +func (self *adminApi) SleepBlocks(req *shared.Request) (interface{}, error) { + args := new(SleepBlocksArgs) + if err := self.coder.Decode(req.Params, &args); err != nil { + return nil, shared.NewDecodeParamError(err.Error()) + } + var timer <-chan time.Time + var height *big.Int + var err error + if args.Timeout > 0 { + timer = time.NewTimer(time.Duration(args.Timeout) * time.Second).C + } + + height = new(big.Int).Add(self.xeth.CurrentBlock().Number(), big.NewInt(args.N)) + height, err = sleepBlocks(self.xeth.UpdateState(), height, timer) + if err != nil { + return nil, err + } + return height.Uint64(), nil +} + +func sleepBlocks(wait chan *big.Int, height *big.Int, timer <-chan time.Time) (newHeight *big.Int, err error) { + wait <- height + select { + case <-timer: + // if times out make sure the xeth loop does not block + go func() { + select { + case wait <- nil: + case <-wait: + } + }() + return nil, fmt.Errorf("timeout") + case newHeight = <-wait: + } + return +} + +// sec, err := call.Argument(0).ToInteger() +// if err != nil { +// fmt.Println(err) +// return otto.FalseValue() +// } +// time.Sleep(time.Duration(sec) * time.Second) +// return otto.UndefinedValue() +// } diff --git a/rpc/api/admin_args.go b/rpc/api/admin_args.go index 5437971ca..7aee5d678 100644 --- a/rpc/api/admin_args.go +++ b/rpc/api/admin_args.go @@ -147,3 +147,34 @@ func (args *StartRPCArgs) UnmarshalJSON(b []byte) (err error) { return nil } + +type SleepBlocksArgs struct { + N int64 + Timeout int64 +} + +func (args *SleepBlocksArgs) UnmarshalJSON(b []byte) (err error) { + var obj []interface{} + if err := json.Unmarshal(b, &obj); err != nil { + return shared.NewDecodeParamError(err.Error()) + } + + args.N = 1 + args.Timeout = 0 + if len(obj) >= 1 { + if n, ok := obj[0].(int64); ok { + args.N = n + } else { + return shared.NewInvalidTypeError("N", "not an integer") + } + } + + if len(obj) >= 2 { + if n, ok := obj[1].(int64); ok { + args.N = n + } else { + return shared.NewInvalidTypeError("Timeout", "not an integer") + } + } + return nil +} |