From dcba2159eb65d0f08b5643c401b9f8d62eb31e67 Mon Sep 17 00:00:00 2001 From: dm4 Date: Tue, 26 Mar 2019 18:21:43 +0800 Subject: core/vm/dvm: add random into eei of dvm random in EEI is the same as opRand implemented in core/vm/evm/instructions.go --- core/vm/dvm/eei.go | 31 +++++++++++++++++++++++++++++++ core/vm/dvm/linker.go | 1 + 2 files changed, 32 insertions(+) (limited to 'core') 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{ -- cgit v1.2.3