diff --git a/packages/chain/cons/bp/aggregated_batch_proposals.go b/packages/chain/cons/bp/aggregated_batch_proposals.go index faa01fab8b..3cc242d54c 100644 --- a/packages/chain/cons/bp/aggregated_batch_proposals.go +++ b/packages/chain/cons/bp/aggregated_batch_proposals.go @@ -12,6 +12,7 @@ import ( "github.com/iotaledger/wasp/packages/gpa" "github.com/iotaledger/wasp/packages/hashing" "github.com/iotaledger/wasp/packages/isc" + "github.com/iotaledger/wasp/packages/util/rwutil" ) // Here we store just an aggregated info. @@ -29,7 +30,8 @@ func AggregateBatchProposals(inputs map[gpa.NodeID][]byte, nodeIDs []gpa.NodeID, // // Parse and validate the batch proposals. Skip the invalid ones. for nid := range inputs { - batchProposal, err := batchProposalFromBytes(inputs[nid]) + var batchProposal *BatchProposal + batchProposal, err := rwutil.ReadFromBytes(inputs[nid], new(BatchProposal)) if err != nil { log.Warnf("cannot decode BatchProposal from %v: %v", nid, err) continue diff --git a/packages/chain/cons/bp/batch_proposal.go b/packages/chain/cons/bp/batch_proposal.go index 69e9c6efcb..0982ba5249 100644 --- a/packages/chain/cons/bp/batch_proposal.go +++ b/packages/chain/cons/bp/batch_proposal.go @@ -39,10 +39,6 @@ func NewBatchProposal( } } -func batchProposalFromBytes(data []byte) (*BatchProposal, error) { - return rwutil.ReadFromBytes(data, new(BatchProposal)) -} - func (b *BatchProposal) Bytes() []byte { return rwutil.WriteToBytes(b) } diff --git a/packages/chain/cons/bp/batch_proposal_test.go b/packages/chain/cons/bp/batch_proposal_test.go new file mode 100644 index 0000000000..494baf7860 --- /dev/null +++ b/packages/chain/cons/bp/batch_proposal_test.go @@ -0,0 +1,41 @@ +// Copyright 2020 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +package bp + +import ( + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/iotaledger/wasp/packages/cryptolib" + "github.com/iotaledger/wasp/packages/hashing" + "github.com/iotaledger/wasp/packages/isc" + "github.com/iotaledger/wasp/packages/kv/dict" + "github.com/iotaledger/wasp/packages/util" + "github.com/iotaledger/wasp/packages/util/rwutil" +) + +func TestBatchProposal1Serialization(t *testing.T) { + var reqRefs []*isc.RequestRef + for i := uint64(0); i < 5; i++ { + req := isc.NewOffLedgerRequest(isc.RandomChainID(), 3, 14, dict.New(), i, 200).Sign(cryptolib.NewKeyPair()) + reqRefs = append(reqRefs, &isc.RequestRef{ + ID: req.ID(), + Hash: hashing.PseudoRandomHash(nil), + }) + } + + batchProposal1 := NewBatchProposal(10, isc.RandomAliasOutputWithID(), util.NewFixedSizeBitVector(11), time.Now(), isc.NewRandomAgentID(), reqRefs) + + b := rwutil.WriteToBytes(batchProposal1) + batchProposal2, err := rwutil.ReadFromBytes(b, new(BatchProposal)) + require.NoError(t, err) + require.Equal(t, batchProposal1.nodeIndex, batchProposal2.nodeIndex) + require.Equal(t, batchProposal1.baseAliasOutput, batchProposal2.baseAliasOutput) + require.Equal(t, batchProposal1.dssIndexProposal, batchProposal2.dssIndexProposal) + require.Equal(t, batchProposal1.timeData.UnixNano(), batchProposal2.timeData.UnixNano()) + require.Equal(t, batchProposal1.validatorFeeDestination, batchProposal2.validatorFeeDestination) + require.Equal(t, batchProposal1.requestRefs, batchProposal2.requestRefs) +} diff --git a/packages/cryptolib/private_key_test.go b/packages/cryptolib/private_key_test.go new file mode 100644 index 0000000000..10f35d15cb --- /dev/null +++ b/packages/cryptolib/private_key_test.go @@ -0,0 +1,22 @@ +package cryptolib_test + +import ( + "crypto/rand" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/iotaledger/wasp/packages/cryptolib" + "github.com/iotaledger/wasp/packages/util/rwutil" +) + +func TestPrivateKeySerialization(t *testing.T) { + seedBytes := make([]byte, cryptolib.SeedSize) + rand.Read(seedBytes) + pivkey1 := cryptolib.PrivateKeyFromSeed((cryptolib.SeedFromBytes(seedBytes))) + pivkey2, err := cryptolib.PrivateKeyFromBytes(pivkey1.AsBytes()) + require.NoError(t, err) + require.Equal(t, pivkey1, pivkey2) + + rwutil.ReadWriteTest(t, pivkey1, cryptolib.NewPrivateKey()) +} diff --git a/packages/isc/request_test.go b/packages/isc/request_test.go index c62fc6c188..bb2bcd73cf 100644 --- a/packages/isc/request_test.go +++ b/packages/isc/request_test.go @@ -9,6 +9,7 @@ import ( iotago "github.com/iotaledger/iota.go/v3" "github.com/iotaledger/iota.go/v3/tpkg" "github.com/iotaledger/wasp/packages/cryptolib" + "github.com/iotaledger/wasp/packages/hashing" "github.com/iotaledger/wasp/packages/kv/dict" "github.com/iotaledger/wasp/packages/util/rwutil" ) @@ -53,10 +54,25 @@ func TestRequestDataSerialization(t *testing.T) { }) } -func TestRequestIDToSerialization(t *testing.T) { +func TestRequestIDSerialization(t *testing.T) { req := NewOffLedgerRequest(RandomChainID(), 3, 14, dict.New(), 1337, 200).Sign(cryptolib.NewKeyPair()) requestID := req.ID() rwutil.ReadWriteTest(t, &requestID, new(RequestID)) rwutil.BytesTest(t, requestID, RequestIDFromBytes) rwutil.StringTest(t, requestID, RequestIDFromString) } + +func TestRequestRefSerialization(t *testing.T) { + req := NewOffLedgerRequest(RandomChainID(), 3, 14, dict.New(), 1337, 200).Sign(cryptolib.NewKeyPair()) + reqRef0 := &RequestRef{ + ID: req.ID(), + Hash: hashing.PseudoRandomHash(nil), + } + + b := reqRef0.Bytes() + reqRef1, err := RequestRefFromBytes(b) + require.NoError(t, err) + require.Equal(t, reqRef0, reqRef1) + + rwutil.ReadWriteTest(t, reqRef0, new(RequestRef)) +} diff --git a/packages/peering/peering_id_test.go b/packages/peering/peering_id_test.go new file mode 100644 index 0000000000..e27903c44a --- /dev/null +++ b/packages/peering/peering_id_test.go @@ -0,0 +1,17 @@ +// Copyright 2020 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +package peering_test + +import ( + "testing" + + "github.com/iotaledger/wasp/packages/peering" + "github.com/iotaledger/wasp/packages/util/rwutil" +) + +func TestPeeringIDSerialization(t *testing.T) { + peeringID := peering.RandomPeeringID() + + rwutil.ReadWriteTest(t, &peeringID, new(peering.PeeringID)) +} diff --git a/packages/state/block_test.go b/packages/state/block_test.go new file mode 100644 index 0000000000..337bef5d7d --- /dev/null +++ b/packages/state/block_test.go @@ -0,0 +1,20 @@ +// Copyright 2022 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +package state_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/iotaledger/wasp/packages/state" +) + +func TestBlockSerialization(t *testing.T) { + block1 := state.RandomBlock() + b := block1.Bytes() + block2, err := state.BlockFromBytes(b) + require.NoError(t, err) + require.Equal(t, block1, block2) +} diff --git a/packages/util/ratio_test.go b/packages/util/ratio_test.go new file mode 100644 index 0000000000..a831db2c83 --- /dev/null +++ b/packages/util/ratio_test.go @@ -0,0 +1,28 @@ +package util_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/iotaledger/wasp/packages/util" + "github.com/iotaledger/wasp/packages/util/rwutil" +) + +func TestRatioSerialization(t *testing.T) { + ratio1 := util.Ratio32{ + A: 123, + B: 246, + } + + b := ratio1.Bytes() + ratio2, err := util.Ratio32FromBytes(b) + require.NoError(t, err) + require.Equal(t, ratio1, ratio2) + s := ratio1.String() + ratio3, err := util.Ratio32FromString(s) + require.NoError(t, err) + require.Equal(t, ratio2, ratio3) + + rwutil.ReadWriteTest(t, &ratio1, new(util.Ratio32)) +} diff --git a/packages/vm/gas/limits_test.go b/packages/vm/gas/limits_test.go new file mode 100644 index 0000000000..ef8afbf6ad --- /dev/null +++ b/packages/vm/gas/limits_test.go @@ -0,0 +1,23 @@ +package gas_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/iotaledger/wasp/packages/vm/gas" +) + +func TestGasLimitsSerialization(t *testing.T) { + limits0 := &gas.Limits{ + MaxGasPerBlock: 456, + MinGasPerRequest: 123, + MaxGasPerRequest: 789, + MaxGasExternalViewCall: 12342, + } + + b := limits0.Bytes() + limits1, err := gas.LimitsFromBytes(b) + require.NoError(t, err) + require.Equal(t, limits0, limits1) +}