Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change block height from uint64 to int64 & lint #21

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions challenger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ When the `initiate_token_deposit` event is detected in l1, saves it as a `Deposi
type Deposit struct {
EventType string `json:"event_type"`
Sequence uint64 `json:"sequence"`
L1BlockHeight uint64 `json:"l1_block_height"`
L1BlockHeight int64 `json:"l1_block_height"`
From string `json:"from"`
To string `json:"to"`
L1Denom string `json:"l1_denom"`
Expand All @@ -101,7 +101,7 @@ When the `propose_output` event is detected in l1, saves it as a `Output` challe
// Output is the challenge event for the output
type Output struct {
EventType string `json:"event_type"`
L2BlockNumber uint64 `json:"l2_block_number"`
L2BlockNumber int64 `json:"l2_block_number"`
OutputIndex uint64 `json:"output_index"`
OutputRoot []byte `json:"output_root"`
Time time.Time `json:"time"`
Expand Down
6 changes: 3 additions & 3 deletions challenger/challenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (c *Challenger) RegisterQuerier() {
})
}

func (c *Challenger) getStartHeights(ctx context.Context, bridgeId uint64) (l1StartHeight uint64, l2StartHeight uint64, startOutputIndex uint64, err error) {
func (c *Challenger) getStartHeights(ctx context.Context, bridgeId uint64) (l1StartHeight int64, l2StartHeight int64, startOutputIndex uint64, err error) {
// get the bridge start height from the host
l1StartHeight, err = c.host.QueryCreateBridgeHeight(ctx, bridgeId)
if err != nil {
Expand All @@ -205,8 +205,8 @@ func (c *Challenger) getStartHeights(ctx context.Context, bridgeId uint64) (l1St
if err != nil {
return 0, 0, 0, err
} else if output != nil {
l1StartHeight = output.OutputProposal.L1BlockNumber
l2StartHeight = output.OutputProposal.L2BlockNumber
l1StartHeight = types.MustUint64ToInt64(output.OutputProposal.L1BlockNumber)
l2StartHeight = types.MustUint64ToInt64(output.OutputProposal.L2BlockNumber)
startOutputIndex = output.OutputIndex + 1
}
}
Expand Down
8 changes: 4 additions & 4 deletions challenger/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ type Child struct {

eventQueue []challengertypes.ChallengeEvent

finalizingBlockHeight uint64
finalizingBlockHeight int64

// status info
lastUpdatedOracleL1Height uint64
lastFinalizedDepositL1BlockHeight uint64
lastUpdatedOracleL1Height int64
lastFinalizedDepositL1BlockHeight int64
lastFinalizedDepositL1Sequence uint64
lastOutputTime time.Time
nextOutputTime time.Time
Expand All @@ -56,7 +56,7 @@ func NewChildV1(
}
}

func (ch *Child) Initialize(ctx context.Context, startHeight uint64, startOutputIndex uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo, challenger challenger) error {
func (ch *Child) Initialize(ctx context.Context, startHeight int64, startOutputIndex uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo, challenger challenger) error {
_, err := ch.BaseChild.Initialize(ctx, startHeight, startOutputIndex, bridgeInfo)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions challenger/child/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func (ch *Child) finalizeDepositHandler(_ context.Context, args nodetypes.EventH
return nil
}

func (ch *Child) handleFinalizeDeposit(l2BlockTime time.Time, l1BlockHeight uint64, l1Sequence uint64, from string, to string, amount sdk.Coin, baseDenom string) {
func (ch *Child) handleFinalizeDeposit(l2BlockTime time.Time, l1BlockHeight int64, l1Sequence uint64, from string, to string, amount sdk.Coin, baseDenom string) {
deposit := challengertypes.NewDeposit(l1Sequence, l1BlockHeight, from, to, baseDenom, amount.String(), l2BlockTime)
ch.eventQueue = append(ch.eventQueue, deposit)

ch.Logger().Info("finalize token deposit",
zap.Uint64("l1_blockHeight", l1BlockHeight),
zap.Int64("l1_blockHeight", l1BlockHeight),
zap.Uint64("l1_sequence", l1Sequence),
zap.String("from", from),
zap.String("to", to),
Expand Down
6 changes: 3 additions & 3 deletions challenger/child/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func (ch *Child) beginBlockHandler(ctx context.Context, args nodetypes.BeginBlockArgs) (err error) {
blockHeight := uint64(args.Block.Header.Height)
blockHeight := args.Block.Header.Height
ch.eventQueue = ch.eventQueue[:0]

err = ch.prepareTree(blockHeight)
Expand All @@ -28,7 +28,7 @@ func (ch *Child) beginBlockHandler(ctx context.Context, args nodetypes.BeginBloc
}

func (ch *Child) endBlockHandler(_ context.Context, args nodetypes.EndBlockArgs) error {
blockHeight := uint64(args.Block.Header.Height)
blockHeight := args.Block.Header.Height
batchKVs := make([]types.RawKV, 0)
pendingChallenges := make([]challengertypes.Challenge, 0)

Expand Down Expand Up @@ -109,6 +109,6 @@ func (ch *Child) txHandler(_ context.Context, args nodetypes.TxHandlerArgs) erro
if !ok {
return nil
}
ch.oracleTxHandler(args.BlockTime, msg.Sender, msg.Height, msg.Data)
ch.oracleTxHandler(args.BlockTime, msg.Sender, types.MustUint64ToInt64(msg.Height), msg.Data)
return nil
}
4 changes: 2 additions & 2 deletions challenger/child/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"go.uber.org/zap"
)

func (ch *Child) oracleTxHandler(blockTime time.Time, sender string, l1BlockHeight uint64, oracleDataBytes comettypes.Tx) {
func (ch *Child) oracleTxHandler(blockTime time.Time, sender string, l1BlockHeight int64, oracleDataBytes comettypes.Tx) {
checksum := challengertypes.OracleChecksum(oracleDataBytes)
oracle := challengertypes.NewOracle(l1BlockHeight, checksum, blockTime)
ch.eventQueue = append(ch.eventQueue, oracle)
ch.lastUpdatedOracleL1Height = l1BlockHeight

ch.Logger().Info("update oracle",
zap.Uint64("l1_blockHeight", l1BlockHeight),
zap.Int64("l1_blockHeight", l1BlockHeight),
zap.String("from", sender),
)
}
6 changes: 3 additions & 3 deletions challenger/child/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (

type Status struct {
Node nodetypes.Status `json:"node"`
LastUpdatedOracleL1Height uint64 `json:"last_updated_oracle_height"`
LastFinalizedDepositL1BlockHeight uint64 `json:"last_finalized_deposit_l1_block_height"`
LastUpdatedOracleL1Height int64 `json:"last_updated_oracle_height"`
LastFinalizedDepositL1BlockHeight int64 `json:"last_finalized_deposit_l1_block_height"`
LastFinalizedDepositL1Sequence uint64 `json:"last_finalized_deposit_l1_sequence"`
LastWithdrawalL2Sequence uint64 `json:"last_withdrawal_l2_sequence"`
WorkingTreeIndex uint64 `json:"working_tree_index"`

FinalizingBlockHeight uint64 `json:"finalizing_block_height"`
FinalizingBlockHeight int64 `json:"finalizing_block_height"`
LastOutputSubmissionTime time.Time `json:"last_output_submission_time"`
NextOutputSubmissionTime time.Time `json:"next_output_submission_time"`

Expand Down
14 changes: 7 additions & 7 deletions challenger/child/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ func (ch *Child) handleInitiateWithdrawal(l2Sequence uint64, from string, to str
return nil
}

func (ch *Child) prepareTree(blockHeight uint64) error {
func (ch *Child) prepareTree(blockHeight int64) error {
if ch.InitializeTree(blockHeight) {
return nil
}

err := ch.Merkle().LoadWorkingTree(blockHeight - 1)
err := ch.Merkle().LoadWorkingTree(types.MustInt64ToUint64(blockHeight - 1))
if err == dbtypes.ErrNotFound {
// must not happened
panic(fmt.Errorf("working tree not found at height: %d, current: %d", blockHeight-1, blockHeight))
Expand Down Expand Up @@ -110,12 +110,12 @@ func (ch *Child) prepareOutput(ctx context.Context) error {
return err
} else {
ch.nextOutputTime = output.OutputProposal.L1BlockTime
ch.finalizingBlockHeight = output.OutputProposal.L2BlockNumber
ch.finalizingBlockHeight = types.MustUint64ToInt64(output.OutputProposal.L2BlockNumber)
}
return nil
}

func (ch *Child) handleTree(blockHeight uint64, blockHeader cmtproto.Header) (kvs []types.RawKV, storageRoot []byte, err error) {
func (ch *Child) handleTree(blockHeight int64, blockHeader cmtproto.Header) (kvs []types.RawKV, storageRoot []byte, err error) {
// panic if we passed the finalizing block height
// this must not happened
if ch.finalizingBlockHeight != 0 && ch.finalizingBlockHeight < blockHeight {
Expand All @@ -130,7 +130,7 @@ func (ch *Child) handleTree(blockHeight uint64, blockHeader cmtproto.Header) (kv

ch.Logger().Info("finalize working tree",
zap.Uint64("tree_index", ch.Merkle().GetWorkingTreeIndex()),
zap.Uint64("height", blockHeight),
zap.Int64("height", blockHeight),
zap.Uint64("num_leaves", ch.Merkle().GetWorkingTreeLeafCount()),
zap.String("storage_root", base64.StdEncoding.EncodeToString(storageRoot)),
)
Expand All @@ -139,15 +139,15 @@ func (ch *Child) handleTree(blockHeight uint64, blockHeader cmtproto.Header) (kv
ch.lastOutputTime = blockHeader.Time
}

err = ch.Merkle().SaveWorkingTree(blockHeight)
err = ch.Merkle().SaveWorkingTree(types.MustInt64ToUint64(blockHeight))
if err != nil {
return nil, nil, err
}

return kvs, storageRoot, nil
}

func (ch *Child) handleOutput(blockTime time.Time, blockHeight uint64, version uint8, blockId []byte, outputIndex uint64, storageRoot []byte) error {
func (ch *Child) handleOutput(blockTime time.Time, blockHeight int64, version uint8, blockId []byte, outputIndex uint64, storageRoot []byte) error {
outputRoot := ophosttypes.GenerateOutputRoot(version, storageRoot, blockId)
output := challengertypes.NewOutput(blockHeight, outputIndex, outputRoot[:], blockTime)

Expand Down
2 changes: 1 addition & 1 deletion challenger/host/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (h *Host) initiateDepositHandler(_ context.Context, args nodetypes.EventHan

func (h *Host) handleInitiateDeposit(
l1Sequence uint64,
blockHeight uint64,
blockHeight int64,
blockTime time.Time,
from string,
to string,
Expand Down
2 changes: 1 addition & 1 deletion challenger/host/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (h *Host) beginBlockHandler(_ context.Context, args nodetypes.BeginBlockArg
}

func (h *Host) endBlockHandler(_ context.Context, args nodetypes.EndBlockArgs) error {
blockHeight := uint64(args.Block.Header.Height)
blockHeight := args.Block.Header.Height
batchKVs := []types.RawKV{
h.Node().SyncInfoToRawKV(blockHeight),
}
Expand Down
2 changes: 1 addition & 1 deletion challenger/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewHostV1(
}
}

func (h *Host) Initialize(ctx context.Context, startHeight uint64, child childNode, bridgeInfo opchildtypes.BridgeInfo, challenger challenger) error {
func (h *Host) Initialize(ctx context.Context, startHeight int64, child childNode, bridgeInfo opchildtypes.BridgeInfo, challenger challenger) error {
err := h.BaseHost.Initialize(ctx, startHeight, bridgeInfo)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion challenger/host/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
challengertypes "github.com/initia-labs/opinit-bots/challenger/types"
)

func (h *Host) oracleTxHandler(blockHeight uint64, blockTime time.Time, oracleDataBytes comettypes.Tx) {
func (h *Host) oracleTxHandler(blockHeight int64, blockTime time.Time, oracleDataBytes comettypes.Tx) {
if !h.OracleEnabled() {
return
}
Expand Down
4 changes: 2 additions & 2 deletions challenger/host/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (h *Host) proposeOutputHandler(_ context.Context, args nodetypes.EventHandl
return h.handleProposeOutput(bridgeId, proposer, outputIndex, l2BlockNumber, outputRoot, args.BlockTime)
}

func (h *Host) handleProposeOutput(bridgeId uint64, proposer string, outputIndex uint64, l2BlockNumber uint64, outputRoot []byte, blockTime time.Time) error {
func (h *Host) handleProposeOutput(bridgeId uint64, proposer string, outputIndex uint64, l2BlockNumber int64, outputRoot []byte, blockTime time.Time) error {
output := challengertypes.NewOutput(l2BlockNumber, outputIndex, outputRoot[:], blockTime)

h.lastOutputIndex = outputIndex
Expand All @@ -36,7 +36,7 @@ func (h *Host) handleProposeOutput(bridgeId uint64, proposer string, outputIndex
zap.Uint64("bridge_id", bridgeId),
zap.String("proposer", proposer),
zap.Uint64("output_index", outputIndex),
zap.Uint64("l2_block_number", l2BlockNumber),
zap.Int64("l2_block_number", l2BlockNumber),
zap.String("output_root", base64.StdEncoding.EncodeToString(outputRoot)),
)
return nil
Expand Down
16 changes: 9 additions & 7 deletions challenger/types/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/json"
"fmt"
"time"

"github.com/initia-labs/opinit-bots/types"
)

type Challenge struct {
Expand Down Expand Up @@ -94,7 +96,7 @@ func (e EventType) String() string {
type Deposit struct {
EventType string `json:"event_type"`
Sequence uint64 `json:"sequence"`
L1BlockHeight uint64 `json:"l1_block_height"`
L1BlockHeight int64 `json:"l1_block_height"`
From string `json:"from"`
To string `json:"to"`
L1Denom string `json:"l1_denom"`
Expand All @@ -105,7 +107,7 @@ type Deposit struct {

var _ ChallengeEvent = &Deposit{}

func NewDeposit(sequence, l1BlockHeight uint64, from, to, l1Denom, amount string, time time.Time) *Deposit {
func NewDeposit(sequence uint64, l1BlockHeight int64, from, to, l1Denom, amount string, time time.Time) *Deposit {
d := &Deposit{
Sequence: sequence,
L1BlockHeight: l1BlockHeight,
Expand Down Expand Up @@ -169,7 +171,7 @@ func (d Deposit) IsTimeout() bool {

type Output struct {
EventType string `json:"event_type"`
L2BlockNumber uint64 `json:"l2_block_number"`
L2BlockNumber int64 `json:"l2_block_number"`
OutputIndex uint64 `json:"output_index"`
OutputRoot []byte `json:"output_root"`
Time time.Time `json:"time"`
Expand All @@ -178,7 +180,7 @@ type Output struct {

var _ ChallengeEvent = &Output{}

func NewOutput(l2BlockNumber, outputIndex uint64, outputRoot []byte, time time.Time) *Output {
func NewOutput(l2BlockNumber int64, outputIndex uint64, outputRoot []byte, time time.Time) *Output {
o := &Output{
L2BlockNumber: l2BlockNumber,
OutputIndex: outputIndex,
Expand Down Expand Up @@ -235,13 +237,13 @@ func (o Output) IsTimeout() bool {

type Oracle struct {
EventType string `json:"event_type"`
L1Height uint64 `json:"l1_height"`
L1Height int64 `json:"l1_height"`
Data []byte `json:"data"`
Time time.Time `json:"time"`
Timeout bool `json:"timeout"`
}

func NewOracle(l1Height uint64, data []byte, time time.Time) *Oracle {
func NewOracle(l1Height int64, data []byte, time time.Time) *Oracle {
o := &Oracle{
L1Height: l1Height,
Data: data,
Expand Down Expand Up @@ -283,7 +285,7 @@ func (o Oracle) EventTime() time.Time {
func (o Oracle) Id() ChallengeId {
return ChallengeId{
Type: EventTypeOracle,
Id: o.L1Height,
Id: types.MustInt64ToUint64(o.L1Height),
}
}

Expand Down
3 changes: 2 additions & 1 deletion challenger/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

dbtypes "github.com/initia-labs/opinit-bots/db/types"
"github.com/initia-labs/opinit-bots/types"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -34,7 +35,7 @@ func PrefixedPendingChallenge(id ChallengeId) []byte {
}

func PrefixedTimeEventTypeId(eventTime time.Time, id ChallengeId) []byte {
return append(append(dbtypes.FromUint64Key(uint64(eventTime.UnixNano())), dbtypes.Splitter),
return append(append(dbtypes.FromUint64Key(types.MustInt64ToUint64(eventTime.UnixNano())), dbtypes.Splitter),
PrefixedEventTypeId(id.Type, id.Id)...)
}

Expand Down
12 changes: 1 addition & 11 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,6 @@ func New(remote, wsEndpoint string) (*HTTP, error) {
return NewWithClient(remote, wsEndpoint, httpClient)
}

// Create timeout enabled http client
func NewWithTimeout(remote, wsEndpoint string, timeout uint) (*HTTP, error) {
httpClient, err := jsonrpcclient.DefaultHTTPClient(remote)
if err != nil {
return nil, err
}
httpClient.Timeout = time.Duration(timeout) * time.Second
return NewWithClient(remote, wsEndpoint, httpClient)
}

// NewWithClient allows for setting a custom http client (See New).
// An error is returned on invalid remote. The function panics when remote is nil.
func NewWithClient(remote, wsEndpoint string, client *http.Client) (*HTTP, error) {
Expand Down Expand Up @@ -421,7 +411,7 @@ func (c *baseRPCClient) Block(ctx context.Context, height *int64) (*ctypes.Resul
return result, nil
}

func (c *baseRPCClient) BlockBulk(ctx context.Context, start *uint64, end *uint64) ([][]byte, error) {
func (c *baseRPCClient) BlockBulk(ctx context.Context, start *int64, end *int64) ([][]byte, error) {
result := new(ResultBlockBulk)
params := make(map[string]interface{})
if start != nil {
Expand Down
7 changes: 3 additions & 4 deletions db/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ func FromInt64(v int64) []byte {
return []byte(fmt.Sprintf("%d", v))
}

func ToInt64(v []byte) int64 {
func ToInt64(v []byte) (int64, error) {
data, err := strconv.ParseInt(string(v), 10, 64)
if err != nil {
// must not happen
panic(err)
return 0, fmt.Errorf("failed to parse uint64 from %s: %w", string(v), err)
}
return data
return data, nil
}

func FromUint64(v uint64) []byte {
Expand Down
4 changes: 2 additions & 2 deletions executor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ type QueryWithdrawalResponse struct {
LatestBlockHash []byte `json:"latest_block_hash"`

// extra info
BlockNumber uint64 `json:"block_number"`
BlockNumber int64 `json:"block_number"`
Receiver string `json:"receiver"`
WithdrawalHash []byte `json:"withdrawal_hash"`
}
Expand Down Expand Up @@ -380,7 +380,7 @@ type QueryWithdrawalResponse struct {
LatestBlockHash []byte `json:"latest_block_hash"`

// extra info
BlockNumber uint64 `json:"block_number"`
BlockNumber int64 `json:"block_number"`
Receiver string `json:"receiver"`
WithdrawalHash []byte `json:"withdrawal_hash"`
}
Expand Down
Loading
Loading