-
Notifications
You must be signed in to change notification settings - Fork 8
/
wasm.go
55 lines (48 loc) · 1.09 KB
/
wasm.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
package types
import (
fmt "fmt"
"strings"
"time"
"github.com/ethereum/go-ethereum/crypto"
)
const (
// MaxWasmSize is the maximum size of Wasm bytecode.
MaxWasmSize = 800 * 1024
)
func validateWasmCode(s []byte) error {
if len(s) == 0 {
return fmt.Errorf("empty Wasm code")
}
if len(s) > MaxWasmSize {
return fmt.Errorf("Wasm code cannot be longer than %d bytes", MaxWasmSize)
}
return nil
}
// NewWasm constructs a new Wasm object given bytecode and Wasm type.
// It panics if it fails to compute hash of bytecode.
func NewWasm(bytecode []byte, wasmType WasmType, addedAt time.Time) *Wasm {
hash := crypto.Keccak256(bytecode)
if hash == nil {
panic("failed to compute hash")
}
return &Wasm{
Hash: hash,
Bytecode: bytecode,
WasmType: wasmType,
AddedAt: addedAt,
}
}
func WasmTypeFromString(s string) WasmType {
switch strings.ToUpper(s) {
case "DATA-REQUEST":
return WasmTypeDataRequest
case "TALLY":
return WasmTypeTally
case "DATA-REQUEST-EXECUTOR":
return WasmTypeDataRequestExecutor
case "RELAYER":
return WasmTypeRelayer
default:
return WasmTypeNil
}
}