/
numberedTransaction.go
73 lines (64 loc) · 1.95 KB
/
numberedTransaction.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
package transaction
import (
"bytes"
"encoding/binary"
"errors"
"io"
// "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rlp"
)
// TransactionInput is one of the inputs into Plasma transaction
type NumberedTransaction struct {
TransactionNumber [TransactionNumberLength]byte
SignedTransaction *SignedTransaction
}
type rlpNumberedTransaction struct {
TransactionNumber []byte
SignedTransaction *SignedTransaction
}
func NewNumberedTransaction(signedTX *SignedTransaction, transactionNumber uint32) (*NumberedTransaction, error) {
tx := &NumberedTransaction{}
bs := make([]byte, TransactionNumberLength)
binary.BigEndian.PutUint32(bs, transactionNumber)
copy(tx.TransactionNumber[:], bs)
tx.SignedTransaction = signedTX
return tx, nil
}
// signature is [R || S || V]
func (tx *NumberedTransaction) Validate() error {
err := tx.SignedTransaction.Validate()
if err != nil {
return err
}
return nil
}
// 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 (tx *NumberedTransaction) EncodeRLP(w io.Writer) error {
rlpNumbered := rlpNumberedTransaction{tx.TransactionNumber[:], tx.SignedTransaction}
return rlp.Encode(w, rlpNumbered)
}
// DecodeRLP implements rlp.Decoder, and loads the consensus fields of a receipt
// from an RLP stream.
func (tx *NumberedTransaction) DecodeRLP(s *rlp.Stream) error {
var dec rlpNumberedTransaction
if err := s.Decode(&dec); err != nil {
return err
}
if len(dec.TransactionNumber) != TransactionNumberLength {
return errors.New("Invalid transaction number length")
}
tx.SignedTransaction = dec.SignedTransaction
copy(tx.TransactionNumber[:], dec.TransactionNumber)
return nil
}
func (tx *NumberedTransaction) GetRaw() ([]byte, error) {
var b bytes.Buffer
i := io.Writer(&b)
err := tx.EncodeRLP(i)
if err != nil {
return nil, err
}
raw := b.Bytes()
return raw, nil
}