Skip to content

Commit

Permalink
Merge pull request #178 from OffchainLabs/milliblock-ticks
Browse files Browse the repository at this point in the history
Milliblock ticks
  • Loading branch information
hkalodner committed Feb 5, 2020
2 parents 825f88f + 3f17cbb commit 88dde9f
Show file tree
Hide file tree
Showing 20 changed files with 70 additions and 46 deletions.
2 changes: 1 addition & 1 deletion packages/arb-bridge-eth/contracts/libraries/RollupTime.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pragma solidity ^0.5.3;


library RollupTime {
uint256 constant TICKS_PER_BLOCK = 13000; // 1 tick ~= 1 millisecond
uint256 constant TICKS_PER_BLOCK = 1000; // 1 tick == 1 milliblock

function ticksToBlocks(uint256 ticks) internal pure returns (uint128) {
return uint128(ticks / TICKS_PER_BLOCK);
Expand Down

Large diffs are not rendered by default.

20 changes: 19 additions & 1 deletion packages/arb-util/common/timeblocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ import (

type TimeBlocks big.Int

var _durationPerBlock time.Duration

func init() {
_durationPerBlock = time.Duration(2) * time.Second
}

func SetDurationPerBlock(d time.Duration) {
_durationPerBlock = d
}

func NewTimeBlocks(val *big.Int) *TimeBlocks {
return (*TimeBlocks)(val)
}
Expand All @@ -39,8 +49,12 @@ func (tb *TimeBlocks) AsInt() *big.Int {
return (*big.Int)(tb)
}

func BlocksFromSeconds(seconds int64) *TimeBlocks {
return (*TimeBlocks)(big.NewInt(int64(time.Duration(seconds) * time.Second / _durationPerBlock)))
}

func (tb *TimeBlocks) Duration() time.Duration {
return TimeFromBlockNum(tb).Duration()
return TicksFromBlockNum(tb).Duration()
}

func (tb *TimeBlocks) Cmp(tb2 *TimeBlocks) int {
Expand All @@ -54,3 +68,7 @@ func (tb *TimeBlocks) Marshal() *TimeBlocksBuf {
func (tb *TimeBlocksBuf) Unmarshal() *TimeBlocks {
return (*TimeBlocks)(tb.Val.Unmarshal())
}

func (tb *TimeBlocks) String() string {
return tb.AsInt().String()
}
26 changes: 10 additions & 16 deletions packages/arb-util/common/timeticks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,18 @@ import (
"time"
)

const (
AverageSecondsPerBlock = 2
)
const TicksPerBlock = int64(1000)

type TimeTicks struct {
Val *big.Int
}

var _timeConversionFactor *big.Int
var _timeTicksPerSecond *big.Int

func init() {
_timeTicksPerSecond = big.NewInt(1000)
_timeConversionFactor = new(big.Int).Mul(big.NewInt(13), _timeTicksPerSecond)
}

func TimeFromBlockNum(blockNum *TimeBlocks) TimeTicks {
return TimeTicks{new(big.Int).Mul(_timeConversionFactor, blockNum.AsInt())}
func TicksFromBlockNum(blockNum *TimeBlocks) TimeTicks {
return TimeTicks{new(big.Int).Mul(big.NewInt(TicksPerBlock), blockNum.AsInt())}
}

func TimeFromSeconds(seconds int64) TimeTicks {
return TimeTicks{new(big.Int).Mul(_timeTicksPerSecond, big.NewInt(seconds))}
func TicksFromSeconds(seconds int64) TimeTicks {
return TimeTicks{big.NewInt(int64(time.Duration(seconds*TicksPerBlock) * time.Second / _durationPerBlock))}
}

func (rt TimeTicks) Add(rt2 TimeTicks) TimeTicks {
Expand All @@ -54,7 +44,11 @@ func (rt TimeTicks) Cmp(rt2 TimeTicks) int {
}

func (rt TimeTicks) Duration() time.Duration {
return time.Millisecond * time.Duration(AverageSecondsPerBlock*rt.Val.Int64()/13)
return time.Duration(rt.Val.Int64()) * _durationPerBlock / time.Duration(TicksPerBlock)
}

func (rt TimeTicks) Clone() TimeTicks {
return TimeTicks{new(big.Int).Set(rt.Val)}
}

func (rt TimeTicks) MarshalToBuf() *TimeTicksBuf {
Expand Down
2 changes: 1 addition & 1 deletion packages/arb-validator/challenges/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func getNextEventWithTimeout(
if err != nil {
return nil, 0, err
}
if common.TimeFromBlockNum(blockId.Height).Cmp(deadline) >= 0 {
if common.TicksFromBlockNum(blockId.Height).Cmp(deadline) >= 0 {
err := contract.TimeoutChallenge(ctx)
if err != nil {
return nil, 0, err
Expand Down
2 changes: 1 addition & 1 deletion packages/arb-validator/challenges/testHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func testChallenge(
context.Background(),
client1.Address(),
client2.Address(),
common.TimeFromBlockNum(common.NewTimeBlocksInt(5)),
common.TicksFromBlockNum(common.NewTimeBlocksInt(5)),
challengeHash,
new(big.Int).SetUint64(uint64(challengeType)),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"math/big"
"os"
"strings"
"time"

"github.com/offchainlabs/arbitrum/packages/arb-validator/rollupmanager"

Expand Down Expand Up @@ -131,15 +132,18 @@ func validateRollupChain() error {

validateCmd := flag.NewFlagSet("validate", flag.ExitOnError)
rpcEnable := validateCmd.Bool("rpc", false, "rpc")
blocktime := validateCmd.Int64("blocktime", 2, "blocktime=N")
err := validateCmd.Parse(os.Args[2:])
if err != nil {
return err
}

if validateCmd.NArg() != 5 {
return errors.New("usage: evilRollupServer validate [--rpc] <contract.ao> <private_key.txt> <ethURL> <rollup_address> <db_path>")
return errors.New("usage: rollupServer validate [--rpc] [--blocktime=N] <contract.ao> <private_key.txt> <ethURL> <rollup_address> <db_path>")
}

common.SetDurationPerBlock(time.Duration(*blocktime) * time.Second)

// 2) Private key
keyFile, err := os.Open(validateCmd.Arg(1))
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion packages/arb-validator/cmd/rollupServer/rollupServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/offchainlabs/arbitrum/packages/arb-validator/rollupmanager"

Expand Down Expand Up @@ -121,15 +122,18 @@ func validateRollupChain() error {

validateCmd := flag.NewFlagSet("validate", flag.ExitOnError)
rpcEnable := validateCmd.Bool("rpc", false, "rpc")
blocktime := validateCmd.Int64("blocktime", 2, "blocktime=N")
err := validateCmd.Parse(os.Args[2:])
if err != nil {
return err
}

if validateCmd.NArg() != 3 {
return errors.New("usage: rollupServer validate [--rpc] <validator_folder> <ethURL> <rollup_address>")
return errors.New("usage: rollupServer validate [--rpc] [--blocktime=NumSeconds] <validator_folder> <ethURL> <rollup_address>")
}

common.SetDurationPerBlock(time.Duration(*blocktime) * time.Second)

validatorFolder := validateCmd.Arg(0)
ethURL := validateCmd.Arg(1)
addressString := validateCmd.Arg(2)
Expand Down
Loading

0 comments on commit 88dde9f

Please sign in to comment.