mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-23 23:24:30 +00:00
add randomize(smc) reader
This commit is contained in:
parent
271cab95a4
commit
4b1f635636
1 changed files with 58 additions and 0 deletions
58
contracts/randomizeReader.go
Normal file
58
contracts/randomizeReader.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package contracts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
randomizeContract "github.com/ethereum/go-ethereum/contracts/randomize/contract"
|
||||
"github.com/ethereum/go-ethereum/core/state"
|
||||
)
|
||||
|
||||
var (
|
||||
slotRandomizeMapping = map[string]uint64{
|
||||
"randomSecret": 0,
|
||||
"randomOpening": 1,
|
||||
}
|
||||
ParsedRandomizeABI, _ = abi.JSON(strings.NewReader(randomizeContract.XDCRandomizeABI))
|
||||
)
|
||||
|
||||
func GetSecret(statedb *state.StateDB, parsed abi.ABI, address common.Address) [][]byte {
|
||||
start := time.Now()
|
||||
fmt.Printf("--------GetSecret---------\n")
|
||||
|
||||
slot := slotRandomizeMapping["randomSecret"]
|
||||
locSecret := getLocMappingAtKey(address.Hash(), slot)
|
||||
arrLength := statedb.GetState(common.HexToAddress(common.RandomizeSMC), common.BigToHash(locSecret))
|
||||
fmt.Printf("Secret length: %v\n", arrLength.Hex())
|
||||
keys := []common.Hash{}
|
||||
for i := uint64(0); i < arrLength.Big().Uint64(); i++ {
|
||||
key := getLocDynamicArrAtElement(common.BigToHash(locSecret), i, 1)
|
||||
keys = append(keys, key)
|
||||
}
|
||||
rets := [][]byte{}
|
||||
for _, key := range keys {
|
||||
ret := statedb.GetState(common.HexToAddress(common.RandomizeSMC), key)
|
||||
rets = append(rets, ret.Bytes())
|
||||
fmt.Printf("ret hex: %v - ret byte: %v\n", ret.Hex(), ret.Bytes())
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
|
||||
fmt.Printf("Execution time: %s\n", elapsed)
|
||||
return rets
|
||||
}
|
||||
|
||||
func GetOpening(statedb *state.StateDB, parsed abi.ABI, address common.Address) []byte {
|
||||
start := time.Now()
|
||||
fmt.Printf("--------GetOpening---------\n")
|
||||
|
||||
slot := slotRandomizeMapping["randomOpening"]
|
||||
locOpening := getLocMappingAtKey(address.Hash(), slot)
|
||||
ret := statedb.GetState(common.HexToAddress(common.RandomizeSMC), common.BigToHash(locOpening))
|
||||
fmt.Printf("ret hex: %v - ret byte: %v\n", ret.Hex(), ret.Bytes())
|
||||
elapsed := time.Since(start)
|
||||
fmt.Printf("Execution time: %s\n", elapsed)
|
||||
return ret.Bytes()
|
||||
}
|
||||
Loading…
Reference in a new issue