This repository has been archived by the owner on Oct 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
177 lines (170 loc) · 4.42 KB
/
index.js
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
const { web3BlockToRlp } = require('../eth2near-relay')
const Web3 = require('web3')
const BN = require('bn.js')
const { BorshContract, hexToBuffer, readerToHex } = require('../rainbow/borsh')
const roots = require('./dag_merkle_roots.json')
const borshSchema = {
bool: {
kind: 'function',
// @ts-ignore
ser: (b) => Buffer.from(Web3.utils.hexToBytes(b ? '0x01' : '0x00')),
deser: (z) => readerToHex(1)(z) === '0x01',
},
initInput: {
kind: 'struct',
fields: [
['validate_ethash', 'bool'],
['dags_start_epoch', 'u64'],
['dags_merkle_roots', ['H128']],
['first_header', ['u8']],
['hashes_gc_threshold', 'u64'],
['finalized_gc_threshold', 'u64'],
['num_confirmations', 'u64'],
['trusted_signer', '?AccountId']
],
},
dagMerkleRootInput: {
kind: 'struct',
fields: [['epoch', 'u64']],
},
addBlockHeaderInput: {
kind: 'struct',
fields: [
['block_header', ['u8']],
['dag_nodes', ['DoubleNodeWithMerkleProof']],
],
},
DoubleNodeWithMerkleProof: {
kind: 'struct',
fields: [
['dag_nodes', ['H512']],
['proof', ['H128']],
],
},
H128: {
kind: 'function',
ser: hexToBuffer,
deser: readerToHex(16),
},
H256: {
kind: 'function',
ser: hexToBuffer,
deser: readerToHex(32),
},
H512: {
kind: 'function',
ser: hexToBuffer,
deser: readerToHex(64),
},
'?H256': {
kind: 'option',
type: 'H256',
},
'?AccountId': {
kind: 'option',
type: 'string',
}
}
class EthOnNearClientContract extends BorshContract {
constructor(account, contractId) {
super(borshSchema, account, contractId, {
viewMethods: [
{
methodName: 'initialized',
inputFieldType: null,
outputFieldType: 'bool',
},
{
methodName: 'dag_merkle_root',
inputFieldType: 'dagMerkleRootInput',
outputFieldType: 'H128',
},
{
methodName: 'last_block_number',
inputFieldType: null,
outputFieldType: 'u64',
},
{
methodName: 'block_hash',
inputFieldType: 'u64',
outputFieldType: '?H256',
},
{
methodName: 'known_hashes',
inputFieldType: 'u64',
outputFieldType: ['H256'],
},
{
methodName: 'block_hash_safe',
inputFieldType: 'u64',
outputFieldType: '?H256',
},
],
changeMethods: [
{
methodName: 'init',
inputFieldType: 'initInput',
outputFieldType: null,
},
{
methodName: 'add_block_header',
inputFieldType: 'addBlockHeaderInput',
outputFieldType: null,
},
],
})
}
// Call initialization methods on the contract.
// If validate_ethash is true will do ethash validation otherwise it won't.
async maybeInitialize(validate_ethash, trusted_signer, robustWeb3) {
await this.accessKeyInit()
let initialized = false
try {
// @ts-ignore
initialized = await this.initialized()
} catch (e) { }
if (!initialized) {
console.log('EthOnNearClient is not initialized, initializing...')
const last_block_number = await robustWeb3.getBlockNumber()
const blockRlp = web3BlockToRlp(
await robustWeb3.getBlock(last_block_number)
)
// @ts-ignore
await this.init(
{
validate_ethash: validate_ethash,
dags_start_epoch: 0,
dags_merkle_roots: roots.dag_merkle_roots,
first_header: blockRlp,
hashes_gc_threshold: 40000,
finalized_gc_threshold: 500,
num_confirmations: 10,
trusted_signer,
},
new BN('300000000000000')
)
console.log('EthOnNearClient initialized')
}
console.log('Checking EthOnNearClient initialization.')
// @ts-ignore
const first_root = await this.dag_merkle_root({
epoch: 0,
})
// @ts-ignore
const last_root = await this.dag_merkle_root({
epoch: 511,
})
if (
!(
first_root === '0x55b891e842e58f58956a847cbbf67821' &&
last_root === '0x4aa6ca6ebef942d8766065b2e590fd32'
)
) {
console.log(
`EthOnNearClient initialization error! The first and last roots are ${first_root} and ${last_root}`
)
process.exit(1)
}
}
}
exports.EthOnNearClientContract = EthOnNearClientContract