/
output.go
84 lines (74 loc) · 2.4 KB
/
output.go
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
package transaction
import (
"errors"
"io"
"github.com/ethereum/go-ethereum/common"
// "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/bankex/go-plasma/types"
"github.com/ethereum/go-ethereum/rlp"
)
// TransactionInput is one of the inputs into Plasma transaction
type TransactionOutput struct {
OutputNumber [OutputNumberLength]byte
To [AddressLength]byte
Value [ValueLength]byte
}
type rlpTransactionOutput struct {
OutputNumber []byte
To []byte
Value []byte
}
// SetFields creates a new input sturcture
func (output *TransactionOutput) SetFields(outputNumber *types.BigInt, address common.Address, value *types.BigInt) error {
outputNumberBytes, err := outputNumber.GetLeftPaddedBytes(OutputNumberLength)
if err != nil {
return errors.New("Output number is too long")
}
valueBytes, err := value.GetLeftPaddedBytes(ValueLength)
if err != nil {
return errors.New("Value is too long")
}
addressBytes := address.Bytes()
copy(output.OutputNumber[:], outputNumberBytes)
copy(output.To[:], addressBytes)
copy(output.Value[:], valueBytes)
return nil
}
func (output *TransactionOutput) GetValue() *types.BigInt {
bi := types.NewBigInt(0)
bi.SetBytes(output.Value[:])
return bi
}
// EncodeRLP implements rlp.Encoder, and flattens the consensus fields of a receipt
// into an RLP stream. If no post state is present, byzantium fork is assumed.
func (output *TransactionOutput) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, rlpTransactionOutput{output.OutputNumber[:], output.To[:], output.Value[:]})
}
// Get address as Ethereum Address type
func (output *TransactionOutput) GetToAddress() common.Address {
return output.To
}
// DecodeRLP implements rlp.Decoder, and loads the consensus fields of a receipt
// from an RLP stream.
func (output *TransactionOutput) DecodeRLP(s *rlp.Stream) error {
var dec rlpTransactionOutput
if err := s.Decode(&dec); err != nil {
return err
}
if len(dec.OutputNumber) != OutputNumberLength {
return errors.New("Invalid output number length")
}
if len(dec.To) != AddressLength {
return errors.New("Invalid address length")
}
if len(dec.Value) != ValueLength {
return errors.New("Invalid value length")
}
copy(output.OutputNumber[:], dec.OutputNumber)
copy(output.To[:], dec.To)
copy(output.Value[:], dec.Value)
// output.OutputNumber = dec.OutputNumber
// output.To = dec.To
// output.Value = dec.Value
return nil
}