diff options
author | dm4 <dm4@skymizer.com> | 2019-03-26 18:21:43 +0800 |
---|---|---|
committer | hydai <hydai@skymizer.com> | 2019-04-11 18:16:55 +0800 |
commit | dcba2159eb65d0f08b5643c401b9f8d62eb31e67 (patch) | |
tree | 4af81389b8794c8e795471bc9a769540df37aa10 | |
parent | 8d0abcaefbd5f6ae5c8a3eae7446aaabcade11bf (diff) | |
download | dexon-dcba2159eb65d0f08b5643c401b9f8d62eb31e67.tar.gz dexon-dcba2159eb65d0f08b5643c401b9f8d62eb31e67.tar.zst dexon-dcba2159eb65d0f08b5643c401b9f8d62eb31e67.zip |
core/vm/dvm: add random into eei of dvm
random in EEI is the same as opRand implemented in core/vm/evm/instructions.go
-rw-r--r-- | core/vm/dvm/eei.go | 31 | ||||
-rw-r--r-- | core/vm/dvm/linker.go | 1 |
2 files changed, 32 insertions, 0 deletions
diff --git a/core/vm/dvm/eei.go b/core/vm/dvm/eei.go index df97438b6..35074fa61 100644 --- a/core/vm/dvm/eei.go +++ b/core/vm/dvm/eei.go @@ -20,6 +20,7 @@ package dvm import ( + "encoding/binary" "fmt" "math/big" "reflect" @@ -68,6 +69,7 @@ const ( GasCostLogTopic = 375 GasCostCopy = 3 GasCostBlockHash = 800 + GasCostRandom = 800 ) var eeiTypes = &wasm.SectionTypes{ @@ -351,6 +353,11 @@ func eeiFuncs(in *DVM) []wasm.Function { Host: reflect.ValueOf(func(p *exec.Process) int64 { return getBlockTimestamp(p, in) }), Body: &wasm.FunctionBody{}, }, + { + Sig: &eeiTypes.Entries[12], + Host: reflect.ValueOf(func(p *exec.Process) int64 { return random(p, in) }), + Body: &wasm.FunctionBody{}, + }, } } @@ -1005,3 +1012,27 @@ func getBlockTimestamp(p *exec.Process, in *DVM) int64 { in.gasAccounting(GasCostBase) return in.Time.Int64() } + +func random(p *exec.Process, in *DVM) int64 { + in.gasAccounting(GasCostRandom) + + nonce := in.StateDB().GetNonce(in.Origin) + binaryOriginNonce := make([]byte, binary.MaxVarintLen64) + binary.PutUvarint(binaryOriginNonce, nonce) + + binaryUsedIndex := make([]byte, binary.MaxVarintLen64) + binary.PutUvarint(binaryUsedIndex, in.RandCallIndex) + + in.RandCallIndex += 1 + + hash := crypto.Keccak256( + in.Randomness, + in.Origin.Bytes(), + binaryOriginNonce, + binaryUsedIndex) + + ret := big.NewInt(0) + ret.SetBytes(hash) + + return ret.Int64() +} diff --git a/core/vm/dvm/linker.go b/core/vm/dvm/linker.go index eb57f8b79..e22a982d2 100644 --- a/core/vm/dvm/linker.go +++ b/core/vm/dvm/linker.go @@ -56,6 +56,7 @@ var eeiFunctionList = []string{ "returnDataCopy", "selfDestruct", "getBlockTimestamp", + "random", } var debugFunctionList = []string{ |