From c63cd5f33c418abc5dbcff1b4f23e50c23cdb1f1 Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Sun, 3 Mar 2024 17:38:58 -0500 Subject: [PATCH 01/14] WIP - start to oracle modificaitons. --- go.mod | 2 +- go.sum | 4 ++-- pkg/settler/settler.go | 2 ++ pkg/settler/settler_test.go | 1 + pkg/store/store.go | 4 ++++ pkg/updater/updater.go | 9 ++++++--- 6 files changed, 16 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index f865253..31c01d3 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/ethereum/go-ethereum v1.13.5 github.com/google/go-cmp v0.6.0 github.com/lib/pq v1.10.9 - github.com/primevprotocol/contracts-abi v0.2.0 + github.com/primevprotocol/contracts-abi v0.2.1 github.com/prometheus/client_golang v1.14.0 github.com/testcontainers/testcontainers-go v0.27.0 github.com/urfave/cli/v2 v2.27.1 diff --git a/go.sum b/go.sum index 17483da..2eabf19 100644 --- a/go.sum +++ b/go.sum @@ -312,8 +312,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= -github.com/primevprotocol/contracts-abi v0.2.0 h1:fzADMI4pVpLVOagZ6K1dOeibYDc89TNyuwymSKzU9ls= -github.com/primevprotocol/contracts-abi v0.2.0/go.mod h1:dE2KkvEqC+itvPa3SCrqQfvH5Hfnfn6omNRwWDTdIp8= +github.com/primevprotocol/contracts-abi v0.2.1 h1:NmKopEWPsx7qQBBciKW0w/bn6O/kLjmCQUeMXY14oAk= +github.com/primevprotocol/contracts-abi v0.2.1/go.mod h1:dE2KkvEqC+itvPa3SCrqQfvH5Hfnfn6omNRwWDTdIp8= github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw= github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= diff --git a/pkg/settler/settler.go b/pkg/settler/settler.go index 21cc08c..079522d 100644 --- a/pkg/settler/settler.go +++ b/pkg/settler/settler.go @@ -70,6 +70,7 @@ type Oracle interface { blockNum *big.Int, builder string, isSlash bool, + residualDecay *big.Int, ) (*types.Transaction, error) UnlockFunds(opts *bind.TransactOpts, bidIDs [][32]byte) (*types.Transaction, error) } @@ -256,6 +257,7 @@ RESTART: big.NewInt(settlement.BlockNum), settlement.Builder, settlement.Type == SettlementTypeSlash, + new(big.Int).SetUint64(100), ) if err != nil { return fmt.Errorf("process commitment: %w nonce %d", err, opts.Nonce.Uint64()) diff --git a/pkg/settler/settler_test.go b/pkg/settler/settler_test.go index 78bbf66..ff5f098 100644 --- a/pkg/settler/settler_test.go +++ b/pkg/settler/settler_test.go @@ -105,6 +105,7 @@ func (t *testOracle) ProcessBuilderCommitmentForBlockNumber( blockNum *big.Int, builder string, isSlash bool, + residualDecay *big.Int, ) (*types.Transaction, error) { t.mu.Lock() defer t.mu.Unlock() diff --git a/pkg/store/store.go b/pkg/store/store.go index a5fbe33..5e666df 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -20,6 +20,10 @@ EXCEPTION WHEN duplicate_object THEN null; END $$;` +// TODO(@ckartik): Add table for L2 blocks to logically compute ms timestamps +// Now that I think of this, it's a shortcoming of the settlement layer + +// TODO(@ckartik): Add decay params and the blocknumber for L2 the block was observed in var settlementsTable = ` CREATE TABLE IF NOT EXISTS settlements ( commitment_index BYTEA PRIMARY KEY, diff --git a/pkg/updater/updater.go b/pkg/updater/updater.go index 1a4bcba..738b55e 100644 --- a/pkg/updater/updater.go +++ b/pkg/updater/updater.go @@ -22,6 +22,8 @@ type BlockWinner struct { } type WinnerRegister interface { + // Need to delay subscribe winners somehow? + // This could be a brittle way to do it SubscribeWinners(ctx context.Context) <-chan BlockWinner UpdateComplete(ctx context.Context, blockNum int64) error AddSettlement( @@ -36,7 +38,7 @@ type WinnerRegister interface { ) error } -type L1Client interface { +type EVMClient interface { BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) } @@ -51,7 +53,8 @@ type Preconf interface { type Updater struct { logger *slog.Logger - l1Client L1Client + l1Client EVMClient + l2Client EVMClient winnerRegister WinnerRegister preconfClient Preconf rollupClient Oracle @@ -61,7 +64,7 @@ type Updater struct { func NewUpdater( logger *slog.Logger, - l1Client L1Client, + l1Client EVMClient, winnerRegister WinnerRegister, rollupClient Oracle, preconfClient Preconf, From f256ab982b38813574bc59fe22f3b7f49d280fd6 Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Sun, 3 Mar 2024 20:26:13 -0500 Subject: [PATCH 02/14] Implement decay in the oracle. --- pkg/node/node.go | 9 ++++++- pkg/settler/settler.go | 17 +++++++------ pkg/store/store.go | 9 +++++-- pkg/store/store_test.go | 1 + pkg/updater/updater.go | 25 +++++++++++++++++++ pkg/updater/updater_test.go | 49 ++++++++++++++++++++++++------------- 6 files changed, 82 insertions(+), 28 deletions(-) diff --git a/pkg/node/node.go b/pkg/node/node.go index 08cb042..6b2fc92 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "log" "log/slog" "math/big" "time" @@ -83,6 +84,12 @@ func NewNode(opts *Options) (*Node, error) { return nil, err } + l2Client, err := ethclient.Dial(opts.SettlementRPCUrl) + if err != nil { + log.Fatal().Err(err).Msg("Failed to connect to the L2 Ethereum client") + return nil, err + } + ctx, cancel := context.WithCancel(context.Background()) var listenerL1Client l1Listener.EthClient @@ -144,7 +151,7 @@ func NewNode(opts *Options) (*Node, error) { } oc := &rollupclient.OracleSession{Contract: oracleContract, CallOpts: callOpts} - updtr := updater.NewUpdater(nd.logger.With("component", "updater"), l1Client, st, oc, pc) + updtr := updater.NewUpdater(nd.logger.With("component", "updater"), l1Client, l2Client, st, oc, pc) updtrClosed := updtr.Start(ctx) settlr := settler.NewSettler( diff --git a/pkg/settler/settler.go b/pkg/settler/settler.go index 079522d..f1352cd 100644 --- a/pkg/settler/settler.go +++ b/pkg/settler/settler.go @@ -32,13 +32,14 @@ const ( ) type Settlement struct { - CommitmentIdx []byte - TxHash string - BlockNum int64 - Builder string - Amount uint64 - BidID []byte - Type SettlementType + CommitmentIdx []byte + TxHash string + BlockNum int64 + Builder string + Amount uint64 + BidID []byte + Type SettlementType + DecayPercentage int64 } type Return struct { @@ -257,7 +258,7 @@ RESTART: big.NewInt(settlement.BlockNum), settlement.Builder, settlement.Type == SettlementTypeSlash, - new(big.Int).SetUint64(100), + big.NewInt(settlement.DecayPercentage), ) if err != nil { return fmt.Errorf("process commitment: %w nonce %d", err, opts.Nonce.Uint64()) diff --git a/pkg/store/store.go b/pkg/store/store.go index 5e666df..7e97d1b 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -35,7 +35,8 @@ CREATE TABLE IF NOT EXISTS settlements ( bid_id BYTEA, chainhash BYTEA, nonce BIGINT, - settled BOOLEAN + settled BOOLEAN, + decay_percentage BIGINT );` var winnersTable = ` @@ -163,6 +164,7 @@ func (s *Store) AddSettlement( builder string, bidID []byte, settlementType settler.SettlementType, + decayPercentage int64, ) error { columns := []string{ "commitment_index", @@ -175,6 +177,7 @@ func (s *Store) AddSettlement( "settled", "chainhash", "nonce", + "decay_percentage", } values := []interface{}{ commitmentIdx, @@ -187,6 +190,7 @@ func (s *Store) AddSettlement( false, nil, 0, + decayPercentage, } placeholder := make([]string, len(values)) for i := range columns { @@ -215,7 +219,7 @@ func (s *Store) SubscribeSettlements(ctx context.Context) <-chan settler.Settlem RETRY: for { queryStr := ` - SELECT commitment_index, transaction, block_number, builder_address, amount, bid_id, type + SELECT commitment_index, transaction, block_number, builder_address, amount, bid_id, type, decay_percentage FROM settlements WHERE settled = false AND chainhash IS NULL AND type != 'return' ORDER BY block_number ASC` @@ -236,6 +240,7 @@ func (s *Store) SubscribeSettlements(ctx context.Context) <-chan settler.Settlem &s.Amount, &s.BidID, &s.Type, + &s.DecayPercentage, ) if err != nil { _ = results.Close() diff --git a/pkg/store/store_test.go b/pkg/store/store_test.go index 2bac2ec..ba18b02 100644 --- a/pkg/store/store_test.go +++ b/pkg/store/store_test.go @@ -233,6 +233,7 @@ func TestStore(t *testing.T) { settlement.Builder, settlement.BidID, settlement.Type, + settlement.DecayPercentage, ) if err != nil { t.Fatalf("Failed to add settlement: %s", err) diff --git a/pkg/updater/updater.go b/pkg/updater/updater.go index 738b55e..53bc04d 100644 --- a/pkg/updater/updater.go +++ b/pkg/updater/updater.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "log/slog" + "math" "math/big" "strings" @@ -35,6 +36,7 @@ type WinnerRegister interface { builder string, bidID []byte, settlementType settler.SettlementType, + decayPercentage int64, ) error } @@ -65,6 +67,7 @@ type Updater struct { func NewUpdater( logger *slog.Logger, l1Client EVMClient, + l2Client EVMClient, winnerRegister WinnerRegister, rollupClient Oracle, preconfClient Preconf, @@ -72,6 +75,7 @@ func NewUpdater( return &Updater{ logger: logger, l1Client: l1Client, + l2Client: l2Client, winnerRegister: winnerRegister, preconfClient: preconfClient, rollupClient: rollupClient, @@ -152,6 +156,12 @@ func (u *Updater) Start(ctx context.Context) <-chan struct{} { return fmt.Errorf("failed to get commitment: %w", err) } + l2Block, err := u.l2Client.BlockByNumber(ctx, big.NewInt(int64(commitment.BlockNumber))) + if err != nil { + return fmt.Errorf("failed to get L2 Block: %w", err) + } + decayPercentage := computeDecayPercentage(commitment.DecayStartTimeStamp, commitment.DecayEndTimeStamp, l2Block.Header().Time) + settlementType := settler.SettlementTypeReturn if commitment.Commiter.Cmp(builderAddr) == 0 { @@ -177,6 +187,7 @@ func (u *Updater) Start(ctx context.Context) <-chan struct{} { commitment.Commiter.Hex(), commitment.CommitmentHash[:], settlementType, + decayPercentage, ) if err != nil { return fmt.Errorf("failed to add settlement: %w", err) @@ -224,3 +235,17 @@ func (u *Updater) Start(ctx context.Context) <-chan struct{} { return doneChan } + +// Takes in start time stamp, end timestamp, commit timestamp and computes a linear decay percentage +// The computation does not care what format the timestamps are in, as long as they are consistent +// (e.g they could be unix or unixMili timestamps) +func computeDecayPercentage(startTimestamp, endTimestamp, commitTimestamp uint64) int64 { + // Calculate the total time in seconds + totalTime := endTimestamp - startTimestamp + // Calculate the time passed in seconds + timePassed := commitTimestamp - startTimestamp + // Calculate the decay percentage + decayPercentage := float64(timePassed) / float64(totalTime) + + return int64(math.Round(decayPercentage * 100)) +} diff --git a/pkg/updater/updater_test.go b/pkg/updater/updater_test.go index 9d5657f..37e86c0 100644 --- a/pkg/updater/updater_test.go +++ b/pkg/updater/updater_test.go @@ -117,11 +117,16 @@ func TestUpdater(t *testing.T) { done: make(chan int64, 1), } - testL1Client := &testL1Client{ + l1Client := &testL1Client{ blockNum: 5, block: types.NewBlock(&types.Header{}, txns, nil, nil, NewHasher()), } + l2Client := &testL1Client{ + blockNum: 0, + block: types.NewBlock(&types.Header{}, txns, nil, nil, NewHasher()), + } + testOracle := &testOracle{ builder: "test", builderAddr: builderAddr, @@ -134,7 +139,8 @@ func TestUpdater(t *testing.T) { updtr := updater.NewUpdater( slog.New(slog.NewTextHandler(io.Discard, nil)), - testL1Client, + l1Client, + l2Client, testWinnerRegister, testOracle, testPreconf, @@ -235,11 +241,15 @@ func TestUpdaterBundlesFailure(t *testing.T) { done: make(chan int64, 1), } - testL1Client := &testL1Client{ + l1Client := &testL1Client{ blockNum: 5, block: types.NewBlock(&types.Header{}, txns, nil, nil, NewHasher()), } + l2Client := &testL1Client{ + blockNum: 0, + block: types.NewBlock(&types.Header{Time: uint64(time.Now().UnixMilli())}, txns, nil, nil, NewHasher()), + } testOracle := &testOracle{ builder: "test", builderAddr: builderAddr, @@ -252,7 +262,8 @@ func TestUpdaterBundlesFailure(t *testing.T) { updtr := updater.NewUpdater( slog.New(slog.NewTextHandler(io.Discard, nil)), - testL1Client, + l1Client, + l2Client, testWinnerRegister, testOracle, testPreconf, @@ -268,6 +279,7 @@ func TestUpdaterBundlesFailure(t *testing.T) { count := 0 for { + fmt.Println(count) if count == 9 { break } @@ -296,12 +308,13 @@ func TestUpdaterBundlesFailure(t *testing.T) { } type testSettlement struct { - commitmentIdx []byte - txHash string - blockNum int64 - builder string - amount uint64 - settlementType settler.SettlementType + commitmentIdx []byte + txHash string + blockNum int64 + builder string + amount uint64 + settlementType settler.SettlementType + decayPercentage int64 } type testWinnerRegister struct { @@ -328,14 +341,16 @@ func (t *testWinnerRegister) AddSettlement( builder string, _ []byte, settlementType settler.SettlementType, + decayPercentage int64, ) error { t.settlements <- testSettlement{ - commitmentIdx: commitmentIdx, - txHash: txHash, - blockNum: blockNum, - amount: amount, - builder: builder, - settlementType: settlementType, + commitmentIdx: commitmentIdx, + txHash: txHash, + blockNum: blockNum, + amount: amount, + builder: builder, + settlementType: settlementType, + decayPercentage: decayPercentage, } return nil } @@ -349,7 +364,7 @@ func (t *testL1Client) BlockByNumber(ctx context.Context, blkNum *big.Int) (*typ if blkNum.Int64() == t.blockNum { return t.block, nil } - return nil, errors.New("block not found") + return nil, fmt.Errorf("block %d not found", blkNum.Int64()) } type testOracle struct { From 9401a02c2b79ef91846339d587802d8e34b66bad Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Sun, 3 Mar 2024 20:27:22 -0500 Subject: [PATCH 03/14] removes completd todos. --- pkg/store/store.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/store/store.go b/pkg/store/store.go index 7e97d1b..60d6a8e 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -20,10 +20,6 @@ EXCEPTION WHEN duplicate_object THEN null; END $$;` -// TODO(@ckartik): Add table for L2 blocks to logically compute ms timestamps -// Now that I think of this, it's a shortcoming of the settlement layer - -// TODO(@ckartik): Add decay params and the blocknumber for L2 the block was observed in var settlementsTable = ` CREATE TABLE IF NOT EXISTS settlements ( commitment_index BYTEA PRIMARY KEY, From cb9ad15137432ebc4c8d0089bf601bffb266499f Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Sun, 3 Mar 2024 20:28:23 -0500 Subject: [PATCH 04/14] Fixes spacing issue in schema. --- pkg/store/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/store/store.go b/pkg/store/store.go index 60d6a8e..141f386 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -32,7 +32,7 @@ CREATE TABLE IF NOT EXISTS settlements ( chainhash BYTEA, nonce BIGINT, settled BOOLEAN, - decay_percentage BIGINT + decay_percentage BIGINT );` var winnersTable = ` From 4168506f6796d6cf46f40464fe5507a37cd47928 Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Sun, 3 Mar 2024 20:29:40 -0500 Subject: [PATCH 05/14] remove stale comment. --- pkg/updater/updater.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/updater/updater.go b/pkg/updater/updater.go index 53bc04d..368e5ad 100644 --- a/pkg/updater/updater.go +++ b/pkg/updater/updater.go @@ -23,8 +23,6 @@ type BlockWinner struct { } type WinnerRegister interface { - // Need to delay subscribe winners somehow? - // This could be a brittle way to do it SubscribeWinners(ctx context.Context) <-chan BlockWinner UpdateComplete(ctx context.Context, blockNum int64) error AddSettlement( From 81577770d7dad43e6407e5f1e07b7f3d455cd956 Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Mon, 4 Mar 2024 13:02:31 -0500 Subject: [PATCH 06/14] removes left over debug statement. --- pkg/updater/updater_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/updater/updater_test.go b/pkg/updater/updater_test.go index 37e86c0..a93db73 100644 --- a/pkg/updater/updater_test.go +++ b/pkg/updater/updater_test.go @@ -279,7 +279,6 @@ func TestUpdaterBundlesFailure(t *testing.T) { count := 0 for { - fmt.Println(count) if count == 9 { break } From 0cf3ea686be37253d1f71e54533cd8102768aea1 Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Mon, 4 Mar 2024 13:04:03 -0500 Subject: [PATCH 07/14] Update comment. --- pkg/updater/updater.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/updater/updater.go b/pkg/updater/updater.go index 368e5ad..23cd86e 100644 --- a/pkg/updater/updater.go +++ b/pkg/updater/updater.go @@ -234,7 +234,7 @@ func (u *Updater) Start(ctx context.Context) <-chan struct{} { return doneChan } -// Takes in start time stamp, end timestamp, commit timestamp and computes a linear decay percentage +// computeDecayPercentage takes startTimestamp, endTimestamp, commitTimestamp and computes a linear decay percentage // The computation does not care what format the timestamps are in, as long as they are consistent // (e.g they could be unix or unixMili timestamps) func computeDecayPercentage(startTimestamp, endTimestamp, commitTimestamp uint64) int64 { From c51670fa7d4e4ea9402a85fb9e5cb20dfd2b78b9 Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Fri, 8 Mar 2024 11:28:26 -0500 Subject: [PATCH 08/14] updates dependencies. --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 31c01d3..ba64567 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/ethereum/go-ethereum v1.13.5 github.com/google/go-cmp v0.6.0 github.com/lib/pq v1.10.9 - github.com/primevprotocol/contracts-abi v0.2.1 + github.com/primevprotocol/contracts-abi v0.2.2 github.com/prometheus/client_golang v1.14.0 github.com/testcontainers/testcontainers-go v0.27.0 github.com/urfave/cli/v2 v2.27.1 diff --git a/go.sum b/go.sum index 2eabf19..d05250f 100644 --- a/go.sum +++ b/go.sum @@ -312,8 +312,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= -github.com/primevprotocol/contracts-abi v0.2.1 h1:NmKopEWPsx7qQBBciKW0w/bn6O/kLjmCQUeMXY14oAk= -github.com/primevprotocol/contracts-abi v0.2.1/go.mod h1:dE2KkvEqC+itvPa3SCrqQfvH5Hfnfn6omNRwWDTdIp8= +github.com/primevprotocol/contracts-abi v0.2.2 h1:JXvkUhcvP6ca5Ev2Bu8wjful9bDaGv7nD0eYOjM8Zh4= +github.com/primevprotocol/contracts-abi v0.2.2/go.mod h1:dE2KkvEqC+itvPa3SCrqQfvH5Hfnfn6omNRwWDTdIp8= github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw= github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= From 26766744236a511ac4623619af02980e111ca88c Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Fri, 8 Mar 2024 16:14:30 -0500 Subject: [PATCH 09/14] updates dependencies. --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index ba64567..1493c4c 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/ethereum/go-ethereum v1.13.5 github.com/google/go-cmp v0.6.0 github.com/lib/pq v1.10.9 - github.com/primevprotocol/contracts-abi v0.2.2 + github.com/primevprotocol/contracts-abi v0.2.3 github.com/prometheus/client_golang v1.14.0 github.com/testcontainers/testcontainers-go v0.27.0 github.com/urfave/cli/v2 v2.27.1 diff --git a/go.sum b/go.sum index d05250f..f54c814 100644 --- a/go.sum +++ b/go.sum @@ -312,8 +312,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= -github.com/primevprotocol/contracts-abi v0.2.2 h1:JXvkUhcvP6ca5Ev2Bu8wjful9bDaGv7nD0eYOjM8Zh4= -github.com/primevprotocol/contracts-abi v0.2.2/go.mod h1:dE2KkvEqC+itvPa3SCrqQfvH5Hfnfn6omNRwWDTdIp8= +github.com/primevprotocol/contracts-abi v0.2.3 h1:4jZGl0hrEdP1yensIrs4Sh2DNU6EG+BBNtKvrH5liR0= +github.com/primevprotocol/contracts-abi v0.2.3/go.mod h1:dE2KkvEqC+itvPa3SCrqQfvH5Hfnfn6omNRwWDTdIp8= github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw= github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= From 4c33d9a6953833456627ee23f84d1053e3198694 Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Sat, 9 Mar 2024 21:45:15 -0500 Subject: [PATCH 10/14] Adds a guard to protect against negative decay values. --- pkg/updater/updater.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/updater/updater.go b/pkg/updater/updater.go index 23cd86e..505e27b 100644 --- a/pkg/updater/updater.go +++ b/pkg/updater/updater.go @@ -238,6 +238,10 @@ func (u *Updater) Start(ctx context.Context) <-chan struct{} { // The computation does not care what format the timestamps are in, as long as they are consistent // (e.g they could be unix or unixMili timestamps) func computeDecayPercentage(startTimestamp, endTimestamp, commitTimestamp uint64) int64 { + if startTimestamp >= endTimestamp || startTimestamp > commitTimestamp { + return 0 + } + // Calculate the total time in seconds totalTime := endTimestamp - startTimestamp // Calculate the time passed in seconds From ce26f995e78ce94e7da65f9d57cf3c93aa324bde Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Sun, 10 Mar 2024 13:48:30 -0400 Subject: [PATCH 11/14] set Uint directly. --- pkg/updater/updater.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/updater/updater.go b/pkg/updater/updater.go index 505e27b..e70f495 100644 --- a/pkg/updater/updater.go +++ b/pkg/updater/updater.go @@ -154,7 +154,7 @@ func (u *Updater) Start(ctx context.Context) <-chan struct{} { return fmt.Errorf("failed to get commitment: %w", err) } - l2Block, err := u.l2Client.BlockByNumber(ctx, big.NewInt(int64(commitment.BlockNumber))) + l2Block, err := u.l2Client.BlockByNumber(ctx, new(big.Int).SetUint64(commitment.BlockNumber)) if err != nil { return fmt.Errorf("failed to get L2 Block: %w", err) } From fe628dc0a394bba3155bf74d5aba5f9fd557ea08 Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Tue, 19 Mar 2024 14:42:24 -0400 Subject: [PATCH 12/14] Fix bug and pull correct L2 block. --- pkg/updater/updater.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/updater/updater.go b/pkg/updater/updater.go index e70f495..1b99e82 100644 --- a/pkg/updater/updater.go +++ b/pkg/updater/updater.go @@ -154,7 +154,7 @@ func (u *Updater) Start(ctx context.Context) <-chan struct{} { return fmt.Errorf("failed to get commitment: %w", err) } - l2Block, err := u.l2Client.BlockByNumber(ctx, new(big.Int).SetUint64(commitment.BlockNumber)) + l2Block, err := u.l2Client.BlockByNumber(ctx, commitment.BlockCommitedAt) if err != nil { return fmt.Errorf("failed to get L2 Block: %w", err) } From 2d75b204efe6330ad12411c602c557326f5f047e Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Tue, 19 Mar 2024 15:00:58 -0400 Subject: [PATCH 13/14] Fixes tests. --- pkg/updater/updater_test.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/pkg/updater/updater_test.go b/pkg/updater/updater_test.go index a93db73..45f10e3 100644 --- a/pkg/updater/updater_test.go +++ b/pkg/updater/updater_test.go @@ -82,15 +82,17 @@ func TestUpdater(t *testing.T) { if i%2 == 0 { commitments[string(idxBytes[:])] = preconf.PreConfCommitmentStorePreConfCommitment{ - Commiter: builderAddr, - TxnHash: strings.TrimPrefix(txn.Hash().Hex(), "0x"), - CommitmentHash: common.HexToHash(fmt.Sprintf("0x%02d", i)), + Commiter: builderAddr, + TxnHash: strings.TrimPrefix(txn.Hash().Hex(), "0x"), + CommitmentHash: common.HexToHash(fmt.Sprintf("0x%02d", i)), + BlockCommitedAt: big.NewInt(0), } } else { commitments[string(idxBytes[:])] = preconf.PreConfCommitmentStorePreConfCommitment{ - Commiter: otherBuilderAddr, - TxnHash: strings.TrimPrefix(txn.Hash().Hex(), "0x"), - CommitmentHash: common.HexToHash(fmt.Sprintf("0x%02d", i)), + Commiter: otherBuilderAddr, + TxnHash: strings.TrimPrefix(txn.Hash().Hex(), "0x"), + CommitmentHash: common.HexToHash(fmt.Sprintf("0x%02d", i)), + BlockCommitedAt: big.NewInt(0), } } } @@ -105,9 +107,10 @@ func TestUpdater(t *testing.T) { } commitments[string(idxBytes[:])] = preconf.PreConfCommitmentStorePreConfCommitment{ - Commiter: builderAddr, - TxnHash: bundle, - CommitmentHash: common.HexToHash(fmt.Sprintf("0x%02d", i)), + Commiter: builderAddr, + TxnHash: bundle, + CommitmentHash: common.HexToHash(fmt.Sprintf("0x%02d", i)), + BlockCommitedAt: big.NewInt(0), } } @@ -230,8 +233,9 @@ func TestUpdaterBundlesFailure(t *testing.T) { } commitments[string(idxBytes[:])] = preconf.PreConfCommitmentStorePreConfCommitment{ - Commiter: builderAddr, - TxnHash: bundle, + Commiter: builderAddr, + TxnHash: bundle, + BlockCommitedAt: big.NewInt(0), } } From 59441ba66a21ad4635ab505d8f146c19c37ba3af Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Tue, 19 Mar 2024 15:21:19 -0400 Subject: [PATCH 14/14] Adds a check to ensure decay is being computed correctly. --- pkg/updater/updater_test.go | 40 +++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/pkg/updater/updater_test.go b/pkg/updater/updater_test.go index 45f10e3..96e0af6 100644 --- a/pkg/updater/updater_test.go +++ b/pkg/updater/updater_test.go @@ -56,6 +56,11 @@ func (h *testHasher) Hash() common.Hash { func TestUpdater(t *testing.T) { t.Parallel() + // timestamp of the First block commitment is X + startTimestamp := time.UnixMilli(1615195200000) + midTimestamp := startTimestamp.Add(time.Duration(2.5 * float64(time.Second))) + endTimestamp := startTimestamp.Add(5 * time.Second) + key, err := crypto.GenerateKey() if err != nil { t.Fatal(err) @@ -82,17 +87,21 @@ func TestUpdater(t *testing.T) { if i%2 == 0 { commitments[string(idxBytes[:])] = preconf.PreConfCommitmentStorePreConfCommitment{ - Commiter: builderAddr, - TxnHash: strings.TrimPrefix(txn.Hash().Hex(), "0x"), - CommitmentHash: common.HexToHash(fmt.Sprintf("0x%02d", i)), - BlockCommitedAt: big.NewInt(0), + Commiter: builderAddr, + TxnHash: strings.TrimPrefix(txn.Hash().Hex(), "0x"), + CommitmentHash: common.HexToHash(fmt.Sprintf("0x%02d", i)), + BlockCommitedAt: big.NewInt(0), + DecayStartTimeStamp: uint64(startTimestamp.UnixMilli()), + DecayEndTimeStamp: uint64(endTimestamp.UnixMilli()), } } else { commitments[string(idxBytes[:])] = preconf.PreConfCommitmentStorePreConfCommitment{ - Commiter: otherBuilderAddr, - TxnHash: strings.TrimPrefix(txn.Hash().Hex(), "0x"), - CommitmentHash: common.HexToHash(fmt.Sprintf("0x%02d", i)), - BlockCommitedAt: big.NewInt(0), + Commiter: otherBuilderAddr, + TxnHash: strings.TrimPrefix(txn.Hash().Hex(), "0x"), + CommitmentHash: common.HexToHash(fmt.Sprintf("0x%02d", i)), + BlockCommitedAt: big.NewInt(0), + DecayStartTimeStamp: uint64(startTimestamp.UnixMilli()), + DecayEndTimeStamp: uint64(endTimestamp.UnixMilli()), } } } @@ -107,10 +116,12 @@ func TestUpdater(t *testing.T) { } commitments[string(idxBytes[:])] = preconf.PreConfCommitmentStorePreConfCommitment{ - Commiter: builderAddr, - TxnHash: bundle, - CommitmentHash: common.HexToHash(fmt.Sprintf("0x%02d", i)), - BlockCommitedAt: big.NewInt(0), + Commiter: builderAddr, + TxnHash: bundle, + CommitmentHash: common.HexToHash(fmt.Sprintf("0x%02d", i)), + BlockCommitedAt: big.NewInt(0), + DecayStartTimeStamp: uint64(startTimestamp.UnixMilli()), + DecayEndTimeStamp: uint64(endTimestamp.UnixMilli()), } } @@ -127,7 +138,7 @@ func TestUpdater(t *testing.T) { l2Client := &testL1Client{ blockNum: 0, - block: types.NewBlock(&types.Header{}, txns, nil, nil, NewHasher()), + block: types.NewBlock(&types.Header{Time: uint64(midTimestamp.UnixMilli())}, txns, nil, nil, NewHasher()), } testOracle := &testOracle{ @@ -164,6 +175,9 @@ func TestUpdater(t *testing.T) { break } settlement := <-testWinnerRegister.settlements + if settlement.decayPercentage != 50 { + t.Fatalf("wrong decay percentage, got %d", settlement.decayPercentage) + } if settlement.blockNum != 5 { t.Fatal("wrong block number") }