|
1 | 1 | package types |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "math/big" |
4 | 5 | "reflect" |
5 | 6 | "testing" |
6 | 7 |
|
7 | 8 | ethcommon "github.com/ethereum/go-ethereum/common" |
8 | | - |
9 | 9 | "github.com/ethereum/go-ethereum/crypto" |
| 10 | + "github.com/ethereum/go-ethereum/rlp" |
10 | 11 | "github.com/harmony-one/harmony/staking" |
| 12 | + "github.com/stretchr/testify/require" |
11 | 13 | ) |
12 | 14 |
|
13 | 15 | func TestFindLogsWithTopic(t *testing.T) { |
@@ -112,3 +114,67 @@ func TestFindLogsWithTopic(t *testing.T) { |
112 | 114 | } |
113 | 115 | } |
114 | 116 | } |
| 117 | + |
| 118 | +// Test we can still parse receipt without EffectiveGasPrice for backwards compatibility, even |
| 119 | +// though it is required per the spec. |
| 120 | +func TestEffectiveGasPriceNotRequired(t *testing.T) { |
| 121 | + r := &Receipt{ |
| 122 | + Status: ReceiptStatusFailed, |
| 123 | + CumulativeGasUsed: 1, |
| 124 | + Logs: []*Log{}, |
| 125 | + // derived fields: |
| 126 | + TxHash: ethcommon.BytesToHash([]byte{0x03, 0x14}), |
| 127 | + ContractAddress: ethcommon.HexToAddress("0x5a443704dd4b594b382c22a083e2bd3090a6fef3"), |
| 128 | + GasUsed: 1, |
| 129 | + } |
| 130 | + |
| 131 | + r.EffectiveGasPrice = nil |
| 132 | + b, err := r.MarshalJSON() |
| 133 | + if err != nil { |
| 134 | + t.Fatal("error marshaling receipt to json:", err) |
| 135 | + } |
| 136 | + r2 := Receipt{} |
| 137 | + err = r2.UnmarshalJSON(b) |
| 138 | + if err != nil { |
| 139 | + t.Fatal("error unmarshalling receipt from json:", err) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestReceiptEncDec(t *testing.T) { |
| 144 | + r := ReceiptForStorage(Receipt{ |
| 145 | + Status: ReceiptStatusFailed, |
| 146 | + CumulativeGasUsed: 1, |
| 147 | + Logs: []*Log{}, |
| 148 | + // derived fields: |
| 149 | + TxHash: ethcommon.BytesToHash([]byte{0x03, 0x14}), |
| 150 | + ContractAddress: ethcommon.HexToAddress("0x5a443704dd4b594b382c22a083e2bd3090a6fef3"), |
| 151 | + GasUsed: 1, |
| 152 | + EffectiveGasPrice: big.NewInt(1), |
| 153 | + }) |
| 154 | + |
| 155 | + bytes, err := rlp.EncodeToBytes(&r) |
| 156 | + if err != nil { |
| 157 | + t.Fatal("error encoding receipt to bytes:", err) |
| 158 | + } |
| 159 | + |
| 160 | + r2 := ReceiptForStorage{} |
| 161 | + err = rlp.DecodeBytes(bytes, &r2) |
| 162 | + if err != nil { |
| 163 | + t.Fatal("error decoding receipt from bytes:", err) |
| 164 | + } |
| 165 | + |
| 166 | + require.Equal(t, r, r2) |
| 167 | +} |
| 168 | + |
| 169 | +func TestReceiptDecodeEmptyEffectiveGasPrice(t *testing.T) { |
| 170 | + r := ReceiptForStorage(Receipt{}) |
| 171 | + |
| 172 | + bytes, err := rlp.EncodeToBytes(&r) |
| 173 | + require.NoError(t, err, "error encoding receipt to bytes") |
| 174 | + |
| 175 | + r2 := ReceiptForStorage{} |
| 176 | + err = rlp.DecodeBytes(bytes, &r2) |
| 177 | + require.NoError(t, err, "error decoding receipt from bytes") |
| 178 | + |
| 179 | + require.EqualValues(t, r.EffectiveGasPrice, r2.EffectiveGasPrice) |
| 180 | +} |
0 commit comments