-
Notifications
You must be signed in to change notification settings - Fork 35
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
Added comparison methods #1356
base: master
Are you sure you want to change the base?
Added comparison methods #1356
Changes from all commits
f3c1e0b
36be570
9040fa6
419ff4a
53011b7
22b5da5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -325,7 +325,7 @@ func Test_txSnapshotJSON_MarshalJSON_UnmarshalJSON(t *testing.T) { | |
} | ||
|
||
// Test marshalling and unmarshalling txSnapshotJSON. | ||
bs := proto.BlockSnapshot{TxSnapshots: [][]proto.AtomicSnapshot{ | ||
bs := proto.BlockSnapshot{TransactionsSnapshots: []proto.TxSnapshot{ | ||
succeededTxSnap, | ||
failedTxSnap, | ||
elidedTxSnap, | ||
|
@@ -337,25 +337,192 @@ func Test_txSnapshotJSON_MarshalJSON_UnmarshalJSON(t *testing.T) { | |
var unmBs proto.BlockSnapshot | ||
err = json.Unmarshal(data, &unmBs) | ||
require.NoError(t, err) | ||
assert.Len(t, unmBs.TxSnapshots, len(bs.TxSnapshots)) | ||
for i := range bs.TxSnapshots { | ||
assert.ElementsMatch(t, bs.TxSnapshots[i], unmBs.TxSnapshots[i]) | ||
assert.Len(t, unmBs.TransactionsSnapshots, len(bs.TransactionsSnapshots)) | ||
for i := range bs.TransactionsSnapshots { | ||
assert.ElementsMatch(t, bs.TransactionsSnapshots[i], unmBs.TransactionsSnapshots[i]) | ||
} | ||
|
||
// Test empty BlockSnapshot. | ||
data, err = json.Marshal(proto.BlockSnapshot{TxSnapshots: [][]proto.AtomicSnapshot{}}) | ||
data, err = json.Marshal(proto.BlockSnapshot{TransactionsSnapshots: []proto.TxSnapshot{}}) | ||
require.NoError(t, err) | ||
assert.Equal(t, "[]", string(data)) | ||
|
||
// Test BlockSnapshot with nil txSnapshots. | ||
data, err = json.Marshal(proto.BlockSnapshot{TxSnapshots: nil}) | ||
data, err = json.Marshal(proto.BlockSnapshot{TransactionsSnapshots: nil}) | ||
require.NoError(t, err) | ||
assert.Equal(t, "[]", string(data)) | ||
|
||
// Test unmarshalling empty BlockSnapshot. | ||
var unmEmptyBs proto.BlockSnapshot | ||
err = json.Unmarshal(data, &unmEmptyBs) | ||
require.NoError(t, err) | ||
assert.Len(t, unmEmptyBs.TxSnapshots, 0) | ||
assert.Nil(t, unmEmptyBs.TxSnapshots) | ||
assert.Len(t, unmEmptyBs.TransactionsSnapshots, 0) | ||
assert.Nil(t, unmEmptyBs.TransactionsSnapshots) | ||
} | ||
|
||
// TestBlockSnapshotEqual tests the comparison of two BlockSnapshot instances. | ||
func TestBlockSnapshotEqual(t *testing.T) { | ||
addr1, _ := proto.NewAddressFromString("3P9o3uwx3fWZz3b53g53ARUk9sFoPW6z7HA") | ||
addr2, _ := proto.NewAddressFromString("3P9o3uwx3fWZz3b5aaaaaaaaaaFoPW6z7HB") | ||
Comment on lines
+365
to
+366
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle errors |
||
assetID1 := crypto.MustDigestFromBase58("BrjV5AB5S7qN5tLQFbU5tpLj5qeozfVvPxEpDkmmhNP") | ||
assetID2 := crypto.MustDigestFromBase58("5Zv8JLH8TTvq9iCo6HtB2K7CGpTJt6JTj5yvXaDVrxEJ") | ||
publicKey1, _ := crypto.NewPublicKeyFromBase58("5TBjL2VdL1XmXq5dC4SYMeH5sVCGmMTeBNNYqWCuEXMn") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here |
||
leaseID1 := crypto.MustDigestFromBase58("FjnZ7aY8iqVpZc4M4uPFuDzMB6YShYd4cNmRfQP1p4Su") | ||
|
||
// Setup test cases | ||
tests := []struct { | ||
name string | ||
blockSnapshotA proto.BlockSnapshot | ||
blockSnapshotB proto.BlockSnapshot | ||
wantEqual bool | ||
}{ | ||
{ | ||
name: "equal snapshots with single transaction", | ||
blockSnapshotA: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.WavesBalanceSnapshot{Address: addr1, Balance: 100}}, | ||
}, | ||
}, | ||
blockSnapshotB: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.WavesBalanceSnapshot{Address: addr1, Balance: 100}}, | ||
}, | ||
}, | ||
wantEqual: true, | ||
}, | ||
{ | ||
name: "different snapshots with single transaction", | ||
blockSnapshotA: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.WavesBalanceSnapshot{Address: addr1, Balance: 100}}, | ||
}, | ||
}, | ||
blockSnapshotB: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.WavesBalanceSnapshot{Address: addr2, Balance: 100}}, | ||
}, | ||
}, | ||
wantEqual: false, | ||
}, | ||
{ | ||
name: "equal snapshots with multiple transactions", | ||
blockSnapshotA: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.WavesBalanceSnapshot{Address: addr1, Balance: 100}}, | ||
{&proto.AssetBalanceSnapshot{Address: addr2, AssetID: assetID1, Balance: 200}}, | ||
}, | ||
}, | ||
blockSnapshotB: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.WavesBalanceSnapshot{Address: addr1, Balance: 100}}, | ||
{&proto.AssetBalanceSnapshot{Address: addr2, AssetID: assetID1, Balance: 200}}, | ||
}, | ||
}, | ||
wantEqual: true, | ||
}, | ||
{ | ||
name: "snapshots with different asset balances", | ||
blockSnapshotA: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.AssetBalanceSnapshot{Address: addr1, AssetID: assetID1, Balance: 300}}, | ||
}, | ||
}, | ||
blockSnapshotB: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.AssetBalanceSnapshot{Address: addr1, AssetID: assetID2, Balance: 300}}, | ||
}, | ||
}, | ||
wantEqual: false, | ||
}, | ||
{ | ||
name: "snapshots with new lease and cancelled lease snapshots", | ||
blockSnapshotA: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.NewLeaseSnapshot{LeaseID: leaseID1, Amount: 1000, SenderPK: publicKey1, RecipientAddr: addr1}}, | ||
{&proto.CancelledLeaseSnapshot{LeaseID: leaseID1}}, | ||
}, | ||
}, | ||
wantEqual: false, | ||
}, | ||
{ | ||
name: "snapshots with equal AssetVolumeSnapshot", | ||
blockSnapshotA: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.AssetVolumeSnapshot{AssetID: assetID1, TotalQuantity: *big.NewInt(1000), IsReissuable: true}}, | ||
}, | ||
}, | ||
blockSnapshotB: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.AssetVolumeSnapshot{AssetID: assetID1, TotalQuantity: *big.NewInt(1000), IsReissuable: true}}, | ||
}, | ||
}, | ||
wantEqual: true, | ||
}, | ||
{ | ||
name: "snapshots with different AssetVolumeSnapshot reissuability", | ||
blockSnapshotA: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.AssetVolumeSnapshot{AssetID: assetID1, TotalQuantity: *big.NewInt(1000), IsReissuable: true}}, | ||
}, | ||
}, | ||
blockSnapshotB: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.AssetVolumeSnapshot{AssetID: assetID1, TotalQuantity: *big.NewInt(1000), IsReissuable: false}}, | ||
}, | ||
}, | ||
wantEqual: false, | ||
}, | ||
{ | ||
name: "snapshots with equal DataEntriesSnapshot", | ||
blockSnapshotA: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.DataEntriesSnapshot{Address: addr1, DataEntries: proto.DataEntries{ | ||
&proto.IntegerDataEntry{Key: "key1", Value: 100}, | ||
&proto.BooleanDataEntry{Key: "key2", Value: true}, | ||
}}}, | ||
}, | ||
}, | ||
blockSnapshotB: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.DataEntriesSnapshot{Address: addr1, DataEntries: proto.DataEntries{ | ||
&proto.IntegerDataEntry{Key: "key1", Value: 100}, | ||
&proto.BooleanDataEntry{Key: "key2", Value: true}, | ||
}}}, | ||
}, | ||
}, | ||
wantEqual: true, | ||
}, | ||
{ | ||
name: "snapshots with different DataEntriesSnapshot", | ||
blockSnapshotA: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.DataEntriesSnapshot{Address: addr1, DataEntries: proto.DataEntries{ | ||
&proto.IntegerDataEntry{Key: "key1", Value: 100}, | ||
&proto.BooleanDataEntry{Key: "key2", Value: true}, | ||
}}}, | ||
}, | ||
}, | ||
blockSnapshotB: proto.BlockSnapshot{ | ||
TransactionsSnapshots: []proto.TxSnapshot{ | ||
{&proto.DataEntriesSnapshot{Address: addr1, DataEntries: proto.DataEntries{ | ||
&proto.IntegerDataEntry{Key: "key1", Value: 200}, // Different value | ||
&proto.BooleanDataEntry{Key: "key2", Value: true}, | ||
}}}, | ||
}, | ||
}, | ||
wantEqual: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
equal, err := tt.blockSnapshotA.Equal(tt.blockSnapshotB) | ||
if err != nil { | ||
t.Errorf("Error comparing snapshots: %v", err) | ||
} | ||
if equal != tt.wantEqual { | ||
t.Errorf("Expected snapshots to be equal: %v, got: %v", tt.wantEqual, equal) | ||
} | ||
Comment on lines
+520
to
+525
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's necessary to say that this function can mutate the passed argument