Skip to content

Commit

Permalink
feat: added order of the commitments (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikelle authored Dec 3, 2024
1 parent c3cf161 commit 418c1af
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 17 deletions.
62 changes: 47 additions & 15 deletions p2p/pkg/preconfirmation/store/store.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package store

import (
"encoding/binary"
"errors"
"fmt"
"math/big"
"sync"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -22,8 +22,19 @@ const (
)

var (
commitmentKey = func(blockNum int64, index []byte) string {
return fmt.Sprintf("%s%d/%s", commitmentNS, blockNum, string(index))
MaxBidAmount, _ = new(big.Int).SetString("1000000000000000000000000000", 10) // 1e24
)

var (
commitmentKey = func(blockNum int64, bidAmt string, index []byte) string {
bidAmtInt, ok := new(big.Int).SetString(bidAmt, 10)
if !ok {
return ""
}
invertedBidAmount := new(big.Int).Sub(MaxBidAmount, bidAmtInt)
paddedBidAmountHex := fmt.Sprintf("%064x", invertedBidAmount)

return fmt.Sprintf("%s%d/%s/%s", commitmentNS, blockNum, paddedBidAmountHex, string(index))
}
blockCommitmentPrefix = func(blockNum int64) string {
return fmt.Sprintf("%s%d", commitmentNS, blockNum)
Expand Down Expand Up @@ -52,6 +63,11 @@ type BlockWinner struct {
Winner common.Address
}

type CommitmentIndexValue struct {
BlockNumber int64
BidAmount string
}

func New(st storage.Storage) *Store {
return &Store{
st: st,
Expand All @@ -74,12 +90,11 @@ func (s *Store) AddCommitment(commitment *EncryptedPreConfirmationWithDecrypted)
err = batch.Write()
}
}()

} else {
writer = s.st
}

key := commitmentKey(commitment.Bid.BlockNumber, commitment.EncryptedPreConfirmation.Commitment)
key := commitmentKey(commitment.Bid.BlockNumber, commitment.Bid.BidAmount, commitment.EncryptedPreConfirmation.Commitment)

buf, err := msgpack.Marshal(commitment)
if err != nil {
Expand All @@ -91,10 +106,17 @@ func (s *Store) AddCommitment(commitment *EncryptedPreConfirmationWithDecrypted)
}

cIndexKey := cmtIndexKey(commitment.EncryptedPreConfirmation.Commitment)
blkNumBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(blkNumBuf, uint64(commitment.Bid.BlockNumber))
cIndexValue := CommitmentIndexValue{
BlockNumber: commitment.Bid.BlockNumber,
BidAmount: commitment.Bid.BidAmount,
}

return writer.Put(cIndexKey, blkNumBuf)
cIndexValueBuf, err := msgpack.Marshal(cIndexValue)
if err != nil {
return err
}

return writer.Put(cIndexKey, cIndexValueBuf)
}

func (s *Store) GetCommitments(blockNum int64) ([]*EncryptedPreConfirmationWithDecrypted, error) {
Expand Down Expand Up @@ -131,16 +153,16 @@ func (s *Store) ClearBlockNumber(blockNum int64) error {
return s.st.Delete(blockWinnerKey(blockNum))
}

func (s *Store) DeleteCommitmentByDigest(blockNum int64, digest [32]byte) error {
func (s *Store) DeleteCommitmentByDigest(blockNum int64, bidAmt string, digest [32]byte) error {
s.mu.Lock()
defer s.mu.Unlock()

return s.st.Delete(commitmentKey(blockNum, digest[:]))
return s.st.Delete(commitmentKey(blockNum, bidAmt, digest[:]))
}

func (s *Store) SetCommitmentIndexByDigest(cDigest, cIndex [32]byte) error {
s.mu.RLock()
blkNumBuf, err := s.st.Get(cmtIndexKey(cDigest[:]))
cIndexValueBuf, err := s.st.Get(cmtIndexKey(cDigest[:]))
s.mu.RUnlock()
switch {
case errors.Is(err, storage.ErrKeyNotFound):
Expand All @@ -154,8 +176,13 @@ func (s *Store) SetCommitmentIndexByDigest(cDigest, cIndex [32]byte) error {
s.mu.Lock()
defer s.mu.Unlock()

blkNum := binary.LittleEndian.Uint64(blkNumBuf)
commitmentKey := commitmentKey(int64(blkNum), cDigest[:])
var cIndexValue CommitmentIndexValue
err = msgpack.Unmarshal(cIndexValueBuf, &cIndexValue)
if err != nil {
return err
}

commitmentKey := commitmentKey(cIndexValue.BlockNumber, cIndexValue.BidAmount, cDigest[:])
cmtBuf, err := s.st.Get(commitmentKey)
if err != nil {
return err
Expand All @@ -180,8 +207,13 @@ func (s *Store) ClearCommitmentIndexes(uptoBlock int64) error {
keys := make([]string, 0)
s.mu.RLock()
err := s.st.WalkPrefix(cmtIndexNS, func(key string, val []byte) bool {
blkNum := binary.LittleEndian.Uint64([]byte(val))
if blkNum < uint64(uptoBlock) {
var cIndexValue CommitmentIndexValue
err := msgpack.Unmarshal(val, &cIndexValue)
if err != nil {
// If unmarshaling fails, we might have corrupted data; skip this key
return false
}
if cIndexValue.BlockNumber < uptoBlock {
keys = append(keys, key)
}
return false
Expand Down
74 changes: 73 additions & 1 deletion p2p/pkg/preconfirmation/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestStore_AddCommitment(t *testing.T) {
PreConfirmation: &preconfpb.PreConfirmation{
Bid: &preconfpb.Bid{
BlockNumber: 1,
BidAmount: "100",
},
},
}
Expand Down Expand Up @@ -83,6 +84,7 @@ func TestStore_ClearCommitmentIndex(t *testing.T) {
PreConfirmation: &preconfpb.PreConfirmation{
Bid: &preconfpb.Bid{
BlockNumber: 1,
BidAmount: "100",
},
},
}
Expand Down Expand Up @@ -122,6 +124,7 @@ func TestStore_DeleteCommitmentByDigest(t *testing.T) {
PreConfirmation: &preconfpb.PreConfirmation{
Bid: &preconfpb.Bid{
BlockNumber: 1,
BidAmount: "100",
},
},
}
Expand All @@ -130,7 +133,7 @@ func TestStore_DeleteCommitmentByDigest(t *testing.T) {
t.Fatal(err)
}

err = st.DeleteCommitmentByDigest(1, digest)
err = st.DeleteCommitmentByDigest(1, commitment.Bid.BidAmount, digest)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -159,6 +162,7 @@ func TestStore_SetCommitmentIndexByDigest(t *testing.T) {
PreConfirmation: &preconfpb.PreConfirmation{
Bid: &preconfpb.Bid{
BlockNumber: 1,
BidAmount: "100",
},
},
}
Expand Down Expand Up @@ -214,3 +218,71 @@ func TestStore_AddWinner(t *testing.T) {
t.Fatalf("expected winner 0x123, got %s", winners[0].Winner.Hex())
}
}

func TestStore_GetCommitments_Order(t *testing.T) {
st := store.New(inmem.New())

commitment1 := &store.EncryptedPreConfirmationWithDecrypted{
EncryptedPreConfirmation: &preconfpb.EncryptedPreConfirmation{
Commitment: []byte("commitment1"),
},
PreConfirmation: &preconfpb.PreConfirmation{
Bid: &preconfpb.Bid{
BlockNumber: 1,
BidAmount: "300",
},
},
}
commitment2 := &store.EncryptedPreConfirmationWithDecrypted{
EncryptedPreConfirmation: &preconfpb.EncryptedPreConfirmation{
Commitment: []byte("commitment2"),
},
PreConfirmation: &preconfpb.PreConfirmation{
Bid: &preconfpb.Bid{
BlockNumber: 1,
BidAmount: "200",
},
},
}
commitment3 := &store.EncryptedPreConfirmationWithDecrypted{
EncryptedPreConfirmation: &preconfpb.EncryptedPreConfirmation{
Commitment: []byte("commitment3"),
},
PreConfirmation: &preconfpb.PreConfirmation{
Bid: &preconfpb.Bid{
BlockNumber: 1,
BidAmount: "100",
},
},
}

err := st.AddCommitment(commitment1)
if err != nil {
t.Fatal(err)
}
err = st.AddCommitment(commitment3)
if err != nil {
t.Fatal(err)
}
err = st.AddCommitment(commitment2)
if err != nil {
t.Fatal(err)
}

commitments, err := st.GetCommitments(1)
if err != nil {
t.Fatal(err)
}

if len(commitments) != 3 {
t.Fatalf("expected 3 commitments, got %d", len(commitments))
}

expectedOrder := []string{"commitment1", "commitment2", "commitment3"}
for i, commitment := range commitments {
expectedCommitment := expectedOrder[i]
if !bytes.Equal(commitment.EncryptedPreConfirmation.Commitment, []byte(expectedCommitment)) {
t.Fatalf("expected commitment %s at position %d, got %s", expectedCommitment, i, commitment.EncryptedPreConfirmation.Commitment)
}
}
}
3 changes: 2 additions & 1 deletion p2p/pkg/preconfirmation/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type CommitmentStore interface {
ClearBlockNumber(blockNum int64) error
DeleteCommitmentByDigest(
blockNum int64,
bidAmt string,
digest [32]byte,
) error
SetCommitmentIndexByDigest(
Expand Down Expand Up @@ -509,5 +510,5 @@ func (t *Tracker) handleOpenedCommitmentStored(
) error {
// In case of bidders this event keeps track of the commitments already opened
// by the provider.
return t.store.DeleteCommitmentByDigest(int64(cs.BlockNumber), cs.CommitmentDigest)
return t.store.DeleteCommitmentByDigest(int64(cs.BlockNumber), cs.BidAmt.String(), cs.CommitmentDigest)
}

0 comments on commit 418c1af

Please sign in to comment.