-
Notifications
You must be signed in to change notification settings - Fork 36
/
mgmt_contract_lib.go
185 lines (149 loc) · 5.16 KB
/
mgmt_contract_lib.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package ethereummock
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/ten-protocol/go-ten/go/ethadapter"
"github.com/ten-protocol/go-ten/integration/datagenerator"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ten-protocol/go-ten/go/ethadapter/mgmtcontractlib"
)
var (
depositTxAddr = datagenerator.RandomAddress()
rollupTxAddr = datagenerator.RandomAddress()
storeSecretTxAddr = datagenerator.RandomAddress()
requestSecretTxAddr = datagenerator.RandomAddress()
initializeSecretTxAddr = datagenerator.RandomAddress()
// MgmtContractAddresses make all these addresses available for the host to know what receipts will be forwarded to the enclave
MgmtContractAddresses = []gethcommon.Address{
depositTxAddr,
rollupTxAddr,
storeSecretTxAddr,
requestSecretTxAddr,
initializeSecretTxAddr,
}
)
// mockContractLib is an implementation of the mgmtcontractlib.MgmtContractLib
// it creates ethereum mocked transactions from common.L1Transaction
// and converts ethereum mocked transactions to common.L1Transaction
type mockContractLib struct{}
func NewMgmtContractLibMock() mgmtcontractlib.MgmtContractLib {
return &mockContractLib{}
}
func (m *mockContractLib) IsMock() bool {
return true
}
func (m *mockContractLib) GetContractAddr() *gethcommon.Address {
return &rollupTxAddr
}
func (m *mockContractLib) DecodeTx(tx *types.Transaction) ethadapter.L1Transaction {
// Do not decode erc20 transactions, this is the responsibility
// of the erc20 contract lib.
if tx.To().Hex() == depositTxAddr.Hex() {
return nil
}
if tx.To().Hex() == rollupTxAddr.Hex() {
return ðadapter.L1RollupHashes{
BlobHashes: tx.BlobHashes(),
}
}
return decodeTx(tx)
}
func (m *mockContractLib) CreateBlobRollup(t *ethadapter.L1RollupTx) (types.TxData, error) {
var err error
blobs, err := ethadapter.EncodeBlobs(t.Rollup)
if err != nil {
return nil, fmt.Errorf("failed to convert rollup to blobs: %w", err)
}
var blobHashes []gethcommon.Hash
var sidecar *types.BlobTxSidecar
if sidecar, blobHashes, err = ethadapter.MakeSidecar(blobs); err != nil {
return nil, fmt.Errorf("failed to make sidecar: %w", err)
}
hashesTx := ethadapter.L1RollupHashes{BlobHashes: blobHashes}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.Encode(hashesTx); err != nil {
panic(err)
}
return &types.BlobTx{
To: rollupTxAddr,
Data: buf.Bytes(),
BlobHashes: blobHashes,
Sidecar: sidecar,
}, nil
}
func (m *mockContractLib) CreateRequestSecret(tx *ethadapter.L1RequestSecretTx) types.TxData {
return encodeTx(tx, requestSecretTxAddr)
}
func (m *mockContractLib) CreateRespondSecret(tx *ethadapter.L1RespondSecretTx, _ bool) types.TxData {
return encodeTx(tx, storeSecretTxAddr)
}
func (m *mockContractLib) CreateInitializeSecret(tx *ethadapter.L1InitializeSecretTx) types.TxData {
return encodeTx(tx, initializeSecretTxAddr)
}
func (m *mockContractLib) GetHostAddressesMsg() (ethereum.CallMsg, error) {
return ethereum.CallMsg{}, nil
}
func (m *mockContractLib) DecodeHostAddressesResponse([]byte) ([]string, error) {
return []string{""}, nil
}
func (m *mockContractLib) GetImportantContractKeysMsg() (ethereum.CallMsg, error) {
return ethereum.CallMsg{}, nil
}
func (m *mockContractLib) DecodeImportantContractKeysResponse([]byte) ([]string, error) {
return []string{""}, nil
}
func (m *mockContractLib) SetImportantContractMsg(string, gethcommon.Address) (ethereum.CallMsg, error) {
return ethereum.CallMsg{}, nil
}
func (m *mockContractLib) GetImportantAddressCallMsg(string) (ethereum.CallMsg, error) {
return ethereum.CallMsg{}, nil
}
func (m *mockContractLib) DecodeImportantAddressResponse([]byte) (gethcommon.Address, error) {
return gethcommon.Address{}, nil
}
func decodeTx(tx *types.Transaction) ethadapter.L1Transaction {
if len(tx.Data()) == 0 {
panic("Data cannot be 0 in the mock implementation")
}
// prepare byte buffer
buf := bytes.NewBuffer(tx.Data())
dec := gob.NewDecoder(buf)
// in the mock implementation we use the To address field to specify the L1 operation (rollup/storesecret/requestsecret)
// the mock implementation does not process contracts
// so this is a way that we can differentiate different contract calls
var t ethadapter.L1Transaction
switch tx.To().Hex() {
case storeSecretTxAddr.Hex():
t = ðadapter.L1RespondSecretTx{}
case depositTxAddr.Hex():
t = ðadapter.L1DepositTx{}
case requestSecretTxAddr.Hex():
t = ðadapter.L1RequestSecretTx{}
case initializeSecretTxAddr.Hex():
t = ðadapter.L1InitializeSecretTx{}
default:
panic("unexpected type")
}
// decode to interface implementation
if err := dec.Decode(t); err != nil {
panic(err)
}
return t
}
func encodeTx(tx ethadapter.L1Transaction, opType gethcommon.Address) types.TxData {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.Encode(tx); err != nil {
panic(err)
}
// the mock implementation does not process contract calls
// this uses the To address to distinguish between different contract calls / different l1 transactions
return &types.LegacyTx{
Data: buf.Bytes(),
To: &opType,
}
}