-
Notifications
You must be signed in to change notification settings - Fork 203
/
random.go
67 lines (58 loc) · 1.85 KB
/
random.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
package nvm
/*
#include "v8/lib/nvm_error.h"
*/
import "C"
import (
"crypto/md5"
"encoding/binary"
"io"
"strconv"
"unsafe"
"math/rand"
"github.com/nebulasio/go-nebulas/util/logging"
"github.com/sirupsen/logrus"
)
//GetTxRandomFunc return random
//export GetTxRandomFunc
func GetTxRandomFunc(handler unsafe.Pointer, gasCnt *C.size_t, result **C.char, exceptionInfo **C.char) int {
engine, _ := getEngineByStorageHandler(uint64(uintptr(handler)))
if engine == nil || engine.ctx.block == nil {
logging.VLog().Error("random.GetTxRandomFunc Unexpected error: failed to get engine")
return C.NVM_UNEXPECTED_ERR
}
// calculate Gas.
*gasCnt = C.size_t(GetTxRandomGasBase)
if engine.ctx.contextRand == nil {
logging.VLog().WithFields(logrus.Fields{
"height": engine.ctx.block.Height(),
}).Error("ContextRand is nil")
*exceptionInfo = C.CString("random.GetTxRandomFunc(), contextRand is nil")
return C.NVM_EXCEPTION_ERR
}
if engine.ctx.contextRand.rand == nil {
bs := engine.ctx.block.RandomSeed()
if len(bs) == 0 {
logging.VLog().WithFields(logrus.Fields{
"height": engine.ctx.block.Height(),
}).Error("block seed is nil")
*exceptionInfo = C.CString("random.GetTxRandomFunc(), randomSeed len is zero")
return C.NVM_EXCEPTION_ERR
}
txhash := engine.ctx.tx.Hash().String()
if len(txhash) == 0 {
logging.VLog().WithFields(logrus.Fields{
"height": engine.ctx.block.Height(),
}).Error("transaction hash is nil")
*exceptionInfo = C.CString("random.GetTxRandomFunc(), randomSeed len is zero")
return C.NVM_EXCEPTION_ERR
}
m := md5.New()
io.WriteString(m, bs)
io.WriteString(m, txhash)
seed := int64(binary.BigEndian.Uint64(m.Sum(nil)))
engine.ctx.contextRand.rand = rand.New(rand.NewSource(seed))
}
*result = C.CString(strconv.FormatFloat(engine.ctx.contextRand.rand.Float64(), 'f', -1, 64))
return C.NVM_SUCCESS
}