summaryrefslogtreecommitdiff
path: root/agreement/events_test.go
blob: c558f0067bc87b4e34861fe6d98e293e53243978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package agreement

import (
	"encoding/base64"
	"testing"

	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/stretchr/testify/require"
)

// TestSerializableErrorBackwardCompatible ensures Err field of type serializableError can be
// properly decoded from ConsensusVersionView.
// This test is only needed for agreement state serialization switch from reflection to msgp.
func TestSerializableErrorBackwardCompatibility(t *testing.T) {
	partitiontest.PartitionTest(t)

	encodedEmpty, err := base64.StdEncoding.DecodeString("gqNFcnLAp1ZlcnNpb26jdjEw")
	require.NoError(t, err)

	encoded, err := base64.StdEncoding.DecodeString("gqNFcnKndGVzdGVycqdWZXJzaW9uo3YxMA==")
	require.NoError(t, err)

	// run on master f57a276 to get the encoded data for above
	// cv := ConsensusVersionView{
	// 	Err:     nil,
	// 	Version: protocol.ConsensusV10,
	// }

	// result := protocol.EncodeReflect(&cv)
	// fmt.Println(base64.StdEncoding.EncodeToString(result))

	// se := serializableErrorUnderlying("testerr")
	// cv = ConsensusVersionView{
	// 	Err:     &se,
	// 	Version: protocol.ConsensusV10,
	// }

	// result = protocol.EncodeReflect(&cv)
	// fmt.Println(base64.StdEncoding.EncodeToString(result))

	cvEmpty := ConsensusVersionView{
		Err:     nil,
		Version: protocol.ConsensusV10,
	}

	se := serializableError("testerr")
	cv := ConsensusVersionView{
		Err:     &se,
		Version: protocol.ConsensusV10,
	}

	cv1 := ConsensusVersionView{}
	err = protocol.Decode(encodedEmpty, &cv1)
	require.NoError(t, err)

	cv2 := ConsensusVersionView{}
	err = protocol.DecodeReflect(encodedEmpty, &cv2)
	require.NoError(t, err)

	require.Equal(t, cv1, cv2)
	require.Equal(t, cvEmpty, cv2)

	cv1 = ConsensusVersionView{}
	err = protocol.Decode(encoded, &cv1)
	require.NoError(t, err)

	cv2 = ConsensusVersionView{}
	err = protocol.DecodeReflect(encoded, &cv2)
	require.NoError(t, err)

	require.Equal(t, cv1, cv2)
	require.Equal(t, cv, cv2)
}