Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions XDCxlending/lendingstate/lendingitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
LendingStatusCancelled = "CANCELLED"
Market = "MO"
Limit = "LO"
MaxLendingExtraDataSize = 200

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put the math explanation at this comment please.

Based on the above struct, we can see the liquidationData is likely be the one with max length in payload.
I made theaAssumptions that each numeric value (RecallAmount, LiquidationAmount, CollateralPrice) is up to 30 digits long and the Reason field is 20 characters long, the estimated maximum size of the ExtraData JSON string in the ProcessLiquidationData function would be approximately 185 bytes.
Hence the value of 200 has been chosen to safeguard the block/tx in XDC in terms of sizes.

)

var ValidInputLendingStatus = map[string]bool{
Expand Down Expand Up @@ -233,6 +234,9 @@ func (l *LendingItem) VerifyLendingItem(state *state.StateDB) error {
if err := l.VerifyLendingSignature(); err != nil {
return err
}
if err := l.VerifyLendingExtraData(); err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -282,6 +286,13 @@ func (l *LendingItem) VerifyLendingType() error {
return nil
}

func (l *LendingItem) VerifyLendingExtraData() error {
if len(l.ExtraData) > MaxLendingExtraDataSize {
return fmt.Errorf("VerifyLendingExtraData: invalid lending extraData size. Size: %v", len(l.ExtraData))
}
return nil
}

func (l *LendingItem) VerifyLendingStatus() error {
if valid, ok := ValidInputLendingStatus[l.Status]; !ok && !valid {
return fmt.Errorf("VerifyLendingStatus: invalid lending status. Status: %s", l.Status)
Expand Down
32 changes: 27 additions & 5 deletions XDCxlending/lendingstate/lendingitem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package lendingstate

import (
"fmt"
"math/big"
"math/rand"
"os"
"testing"
"time"

"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"github.com/XinFinOrg/XDPoSChain/core/state"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
"github.com/XinFinOrg/XDPoSChain/rpc"
"math/big"
"math/rand"
"os"
"testing"
"time"
)

func TestLendingItem_VerifyLendingSide(t *testing.T) {
Expand Down Expand Up @@ -108,6 +109,27 @@ func TestLendingItem_VerifyLendingType(t *testing.T) {
}
}

func TestLendingItem_VerifyExtraData(t *testing.T) {
tests := []struct {
name string
fields *LendingItem
wantErr bool
}{
{"within the limit", &LendingItem{ExtraData: "123"}, false},
{"within the limit", &LendingItem{ExtraData: "This is a string specifically designed to exceed 201 bytes in length. It contains enough characters, including spaces and punctuation, to ensure that its total size goes beyond the specified limit for demonstration purposes."}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &LendingItem{
ExtraData: tt.fields.ExtraData,
}
if err := l.VerifyLendingExtraData(); (err != nil) != tt.wantErr {
t.Errorf("VerifyLendingExtraData() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestLendingItem_VerifyLendingStatus(t *testing.T) {
tests := []struct {
name string
Expand Down