Skip to content

Commit

Permalink
incorporate core/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-shim committed Oct 24, 2024
1 parent 839dffe commit 1b63aee
Show file tree
Hide file tree
Showing 12 changed files with 386 additions and 150 deletions.
102 changes: 63 additions & 39 deletions core/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package core

import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"math/big"
"strconv"

"github.com/Layr-Labs/eigenda/common"
"github.com/Layr-Labs/eigenda/encoding"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/accounts/abi"
"golang.org/x/crypto/sha3"
)

type AccountID = string
Expand Down Expand Up @@ -495,23 +497,69 @@ type PaymentMetadata struct {
}

// Hash returns the Keccak256 hash of the PaymentMetadata
func (pm *PaymentMetadata) Hash() []byte {
// Create a byte slice to hold the serialized data
data := make([]byte, 0, len(pm.AccountID)+4+pm.CumulativePayment.BitLen()/8+1)
func (pm *PaymentMetadata) Hash() ([32]byte, error) {
blobHeaderType, err := abi.NewType("tuple", "", []abi.ArgumentMarshaling{
{
Name: "accountID",
Type: "string",
},
{
Name: "binIndex",
Type: "uint32",
},
{
Name: "cumulativePayment",
Type: "uint256",
},
})
if err != nil {
return [32]byte{}, err
}

// Append AccountID
data = append(data, []byte(pm.AccountID)...)
arguments := abi.Arguments{
{
Type: blobHeaderType,
},
}

// Append BinIndex
binIndexBytes := make([]byte, 4)
binary.BigEndian.PutUint32(binIndexBytes, pm.BinIndex)
data = append(data, binIndexBytes...)
bytes, err := arguments.Pack(pm)
if err != nil {
return [32]byte{}, err
}

// Append CumulativePayment
paymentBytes := pm.CumulativePayment.Bytes()
data = append(data, paymentBytes...)
var hash [32]byte
hasher := sha3.NewLegacyKeccak256()
hasher.Write(bytes)
copy(hash[:], hasher.Sum(nil)[:32])

return crypto.Keccak256(data)
return hash, nil
}

func (pm *PaymentMetadata) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
return &types.AttributeValueMemberM{
Value: map[string]types.AttributeValue{
"AccountID": &types.AttributeValueMemberS{Value: pm.AccountID},
"BinIndex": &types.AttributeValueMemberN{Value: fmt.Sprintf("%d", pm.BinIndex)},
"CumulativePayment": &types.AttributeValueMemberN{
Value: pm.CumulativePayment.String(),
},
},
}, nil
}

func (pm *PaymentMetadata) UnmarshalDynamoDBAttributeValue(av types.AttributeValue) error {
m, ok := av.(*types.AttributeValueMemberM)
if !ok {
return fmt.Errorf("expected *types.AttributeValueMemberM, got %T", av)
}
pm.AccountID = m.Value["AccountID"].(*types.AttributeValueMemberS).Value
binIndex, err := strconv.ParseUint(m.Value["BinIndex"].(*types.AttributeValueMemberN).Value, 10, 32)
if err != nil {
return fmt.Errorf("failed to parse BinIndex: %w", err)
}
pm.BinIndex = uint32(binIndex)
pm.CumulativePayment, _ = new(big.Int).SetString(m.Value["CumulativePayment"].(*types.AttributeValueMemberN).Value, 10)
return nil
}

// OperatorInfo contains information about an operator which is stored on the blockchain state,
Expand All @@ -528,27 +576,3 @@ type ActiveReservation struct {
type OnDemandPayment struct {
CumulativePayment *big.Int // Total amount deposited by the user
}

type BlobVersion uint32

type BlobKey [32]byte

func (b BlobKey) Hex() string {
return hex.EncodeToString(b[:])
}

func HexToBlobKey(h string) (BlobKey, error) {
b, err := hex.DecodeString(h)
if err != nil {
return BlobKey{}, err
}
return BlobKey(b), nil
}

type BlobHeaderV2 struct {
BlobVersion BlobVersion `json:"version"`
QuorumIDs []QuorumID `json:"quorum_ids"`
BlobCommitment encoding.BlobCommitments `json:"commitments"`

PaymentMetadata `json:"payment_metadata"`
}
6 changes: 3 additions & 3 deletions core/v2/assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/Layr-Labs/eigenda/core"
)

func GetAssignments(state *core.OperatorState, blobVersion byte, quorum uint8) (map[core.OperatorID]Assignment, error) {
func GetAssignments(state *core.OperatorState, blobVersion BlobVersion, quorum uint8) (map[core.OperatorID]Assignment, error) {

params, ok := ParametersMap[blobVersion]
if !ok {
Expand Down Expand Up @@ -81,7 +81,7 @@ func GetAssignments(state *core.OperatorState, blobVersion byte, quorum uint8) (

}

func GetAssignment(state *core.OperatorState, blobVersion byte, quorum core.QuorumID, id core.OperatorID) (Assignment, error) {
func GetAssignment(state *core.OperatorState, blobVersion BlobVersion, quorum core.QuorumID, id core.OperatorID) (Assignment, error) {

assignments, err := GetAssignments(state, blobVersion, quorum)
if err != nil {
Expand All @@ -96,7 +96,7 @@ func GetAssignment(state *core.OperatorState, blobVersion byte, quorum core.Quor
return assignment, nil
}

func GetChunkLength(blobVersion byte, blobLength uint32) (uint32, error) {
func GetChunkLength(blobVersion BlobVersion, blobLength uint32) (uint32, error) {

if blobLength == 0 {
return 0, fmt.Errorf("blob length must be greater than 0")
Expand Down
10 changes: 5 additions & 5 deletions core/v2/assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestOperatorAssignmentsV2(t *testing.T) {
state := dat.GetTotalOperatorState(context.Background(), 0)
operatorState := state.OperatorState

blobVersion := byte(0)
blobVersion := corev2.BlobVersion(0)

assignments, err := corev2.GetAssignments(operatorState, blobVersion, 0)
assert.NoError(t, err)
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestAssignmentWithTooManyOperators(t *testing.T) {

assert.Equal(t, len(state.Operators[0]), numOperators)

blobVersion := byte(0)
blobVersion := corev2.BlobVersion(0)

_, err = corev2.GetAssignments(state.OperatorState, blobVersion, 0)
assert.Error(t, err)
Expand Down Expand Up @@ -131,7 +131,7 @@ func FuzzOperatorAssignmentsV2(f *testing.F) {

state := dat.GetTotalOperatorState(context.Background(), 0)

blobVersion := byte(0)
blobVersion := corev2.BlobVersion(0)

assignments, err := corev2.GetAssignments(state.OperatorState, blobVersion, 0)
assert.NoError(t, err)
Expand Down Expand Up @@ -162,7 +162,7 @@ func FuzzOperatorAssignmentsV2(f *testing.F) {

func TestChunkLength(t *testing.T) {

blobVersion := byte(0)
blobVersion := corev2.BlobVersion(0)

pairs := []struct {
blobLength uint32
Expand All @@ -188,7 +188,7 @@ func TestChunkLength(t *testing.T) {

func TestInvalidChunkLength(t *testing.T) {

blobVersion := byte(0)
blobVersion := corev2.BlobVersion(0)

invalidLengths := []uint32{
0,
Expand Down
8 changes: 4 additions & 4 deletions core/v2/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func makeTestComponents() (encoding.Prover, encoding.Verifier, error) {
return p, v, nil
}

func makeTestBlob(t *testing.T, p encoding.Prover, version uint8, refBlockNumber uint64, length int, quorums []core.QuorumID) (corev2.BlobCertificate, []byte) {
func makeTestBlob(t *testing.T, p encoding.Prover, version corev2.BlobVersion, refBlockNumber uint64, length int, quorums []core.QuorumID) (corev2.BlobCertificate, []byte) {

data := make([]byte, length*31)
_, err := rand.Read(data)
Expand All @@ -101,7 +101,7 @@ func makeTestBlob(t *testing.T, p encoding.Prover, version uint8, refBlockNumber

header := corev2.BlobCertificate{
BlobHeader: corev2.BlobHeader{
Version: version,
BlobVersion: version,
QuorumNumbers: quorums,
BlobCommitments: commitments,
},
Expand Down Expand Up @@ -148,7 +148,7 @@ func prepareBlobs(t *testing.T, operatorCount uint, headers []corev2.BlobCertifi

for _, quorum := range header.QuorumNumbers {

assignments, err := corev2.GetAssignments(state, header.Version, quorum)
assignments, err := corev2.GetAssignments(state, header.BlobVersion, quorum)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestValidationSucceeds(t *testing.T) {

bn := uint64(0)

version := uint8(0)
version := corev2.BlobVersion(0)

pool := workerpool.New(1)

Expand Down
Loading

0 comments on commit 1b63aee

Please sign in to comment.