-
Notifications
You must be signed in to change notification settings - Fork 199
/
common.go
38 lines (31 loc) · 1.13 KB
/
common.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
package smartContract
import (
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/vmcommon"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/process"
)
func findVMByTransaction(container process.VirtualMachinesContainer, tx data.TransactionHandler) (vmcommon.VMExecutionHandler, error) {
scAddress := tx.GetRcvAddr()
return findVMByScAddress(container, scAddress)
}
func findVMByScAddress(container process.VirtualMachinesContainer, scAddress []byte) (vmcommon.VMExecutionHandler, error) {
vmType, err := parseVMTypeFromContractAddress(scAddress)
if err != nil {
return nil, err
}
vm, err := container.Get(vmType)
if err != nil {
return nil, err
}
return vm, nil
}
func parseVMTypeFromContractAddress(contractAddress []byte) ([]byte, error) {
// TODO: Why not check against AddressLength (32)?
if len(contractAddress) < core.NumInitCharactersForScAddress {
return nil, process.ErrInvalidVMType
}
startIndex := core.NumInitCharactersForScAddress - core.VMTypeLen
endIndex := core.NumInitCharactersForScAddress
return contractAddress[startIndex:endIndex], nil
}