Fix various build errors and bugs

- bug in hex decoding in bls12381 package
- correct interfaces
- missing test variables
This commit is contained in:
Roy Crihfield 2023-06-23 00:30:34 +08:00
parent 5c57181c36
commit 80a4c5f4c5
6 changed files with 97 additions and 25 deletions

View File

@ -1,21 +1,12 @@
package bls12381 package bls12381
import ( import (
"encoding/hex"
"bytes" "bytes"
"crypto/rand" "crypto/rand"
"math/big" "math/big"
"testing" "testing"
) )
func fromHex(s string) []byte {
b, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return b
}
func (g *G1) one() *PointG1 { func (g *G1) one() *PointG1 {
one, _ := g.fromBytesUnchecked( one, _ := g.fromBytesUnchecked(
fromHex("" + fromHex("" +

View File

@ -17,15 +17,34 @@
package bls12381 package bls12381
import ( import (
"strings"
"encoding/hex" "encoding/hex"
"errors" "errors"
"math/big" "math/big"
) )
func bigFromHex(hex string) *big.Int { // fromHex returns the bytes represented by the hexadecimal string s.
b, _ := hex.DecodeString(strings.TrimPrefix(hex, "0x")) // s may be prefixed with "0x".
return new(big.Int).SetBytes(b) func fromHex(s string) []byte {
if has0xPrefix(s) {
s = s[2:]
}
if len(s)%2 == 1 {
s = "0" + s
}
h, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return h
}
// has0xPrefix validates str begins with '0x' or '0X'.
func has0xPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
}
func bigFromHex(s string) *big.Int {
return new(big.Int).SetBytes(fromHex(s))
} }
// decodeFieldElement expects 64 byte input with zero top 16 bytes, // decodeFieldElement expects 64 byte input with zero top 16 bytes,

View File

@ -17,11 +17,11 @@
package hasher package hasher
import ( import (
"fmt"
"bufio" "bufio"
"bytes" "bytes"
"encoding/gob" "encoding/gob"
"errors" "errors"
"fmt"
"io" "io"
"sync" "sync"
@ -57,7 +57,7 @@ func returnToPool(st *StackTrie) {
// in order. Once it determines that a subtree will no longer be inserted // in order. Once it determines that a subtree will no longer be inserted
// into, it will hash it and free up the memory it uses. // into, it will hash it and free up the memory it uses.
type StackTrie struct { type StackTrie struct {
owner core.Hash // the owner of the trie owner core.Hash // the owner of the trie
nodeType uint8 // node type (as in branch, ext, leaf) nodeType uint8 // node type (as in branch, ext, leaf)
val []byte // value contained by this node if it's a leaf val []byte // value contained by this node if it's a leaf
key []byte // key chunk covered by this (leaf|ext) node key []byte // key chunk covered by this (leaf|ext) node
@ -214,7 +214,7 @@ func (st *StackTrie) TryUpdate(key, value []byte) error {
func (st *StackTrie) Update(key, value []byte) { func (st *StackTrie) Update(key, value []byte) {
if err := st.TryUpdate(key, value); err != nil { if err := st.TryUpdate(key, value); err != nil {
fmt.Errorf("Unhandled trie error in StackTrie.Update", "err", err) fmt.Println("Unhandled trie error in StackTrie.Update:", err)
} }
} }

View File

@ -34,13 +34,43 @@ var (
CalaverasGenesisHash = core.HexToHash("0xeb9233d066c275efcdfed8037f4fc082770176aefdbcb7691c71da412a5670f2") CalaverasGenesisHash = core.HexToHash("0xeb9233d066c275efcdfed8037f4fc082770176aefdbcb7691c71da412a5670f2")
) )
var (
// TestChainConfig contains every protocol change (EIPs) introduced
// and accepted by the Ethereum core developers for testing proposes.
TestChainConfig = &ChainConfig{
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
DAOForkSupport: false,
EIP150Block: big.NewInt(0),
EIP155Block: big.NewInt(0),
EIP158Block: big.NewInt(0),
ByzantiumBlock: big.NewInt(0),
ConstantinopleBlock: big.NewInt(0),
PetersburgBlock: big.NewInt(0),
IstanbulBlock: big.NewInt(0),
MuirGlacierBlock: big.NewInt(0),
BerlinBlock: big.NewInt(0),
LondonBlock: big.NewInt(0),
ArrowGlacierBlock: big.NewInt(0),
GrayGlacierBlock: big.NewInt(0),
MergeNetsplitBlock: nil,
ShanghaiTime: nil,
CancunTime: nil,
PragueTime: nil,
TerminalTotalDifficulty: nil,
TerminalTotalDifficultyPassed: false,
Ethash: new(EthashConfig),
Clique: nil,
}
)
// TrustedCheckpoint represents a set of post-processed trie roots (CHT and // TrustedCheckpoint represents a set of post-processed trie roots (CHT and
// BloomTrie) associated with the appropriate section index and head hash. It is // BloomTrie) associated with the appropriate section index and head hash. It is
// used to start light syncing from this checkpoint and avoid downloading the // used to start light syncing from this checkpoint and avoid downloading the
// entire header chain while still being able to securely access old headers/logs. // entire header chain while still being able to securely access old headers/logs.
type TrustedCheckpoint struct { type TrustedCheckpoint struct {
SectionIndex uint64 `json:"sectionIndex"` SectionIndex uint64 `json:"sectionIndex"`
SectionHead core.Hash `json:"sectionHead"` SectionHead core.Hash `json:"sectionHead"`
CHTRoot core.Hash `json:"chtRoot"` CHTRoot core.Hash `json:"chtRoot"`
BloomRoot core.Hash `json:"bloomRoot"` BloomRoot core.Hash `json:"bloomRoot"`
@ -80,7 +110,7 @@ func (c *TrustedCheckpoint) Empty() bool {
type CheckpointOracleConfig struct { type CheckpointOracleConfig struct {
Address core.Address `json:"address"` Address core.Address `json:"address"`
Signers []core.Address `json:"signers"` Signers []core.Address `json:"signers"`
Threshold uint64 `json:"threshold"` Threshold uint64 `json:"threshold"`
} }
// ChainConfig is the core config which determines the blockchain settings. // ChainConfig is the core config which determines the blockchain settings.
@ -97,7 +127,7 @@ type ChainConfig struct {
DAOForkSupport bool `json:"daoForkSupport,omitempty"` // Whether the nodes supports or opposes the DAO hard-fork DAOForkSupport bool `json:"daoForkSupport,omitempty"` // Whether the nodes supports or opposes the DAO hard-fork
// EIP150 implements the Gas price changes (https://github.com/ethereum/EIPs/issues/150) // EIP150 implements the Gas price changes (https://github.com/ethereum/EIPs/issues/150)
EIP150Block *big.Int `json:"eip150Block,omitempty"` // EIP150 HF block (nil = no fork) EIP150Block *big.Int `json:"eip150Block,omitempty"` // EIP150 HF block (nil = no fork)
EIP150Hash core.Hash `json:"eip150Hash,omitempty"` // EIP150 HF hash (needed for header only clients as only gas pricing changed) EIP150Hash core.Hash `json:"eip150Hash,omitempty"` // EIP150 HF hash (needed for header only clients as only gas pricing changed)
EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block
@ -111,9 +141,9 @@ type ChainConfig struct {
BerlinBlock *big.Int `json:"berlinBlock,omitempty"` // Berlin switch block (nil = no fork, 0 = already on berlin) BerlinBlock *big.Int `json:"berlinBlock,omitempty"` // Berlin switch block (nil = no fork, 0 = already on berlin)
LondonBlock *big.Int `json:"londonBlock,omitempty"` // London switch block (nil = no fork, 0 = already on london) LondonBlock *big.Int `json:"londonBlock,omitempty"` // London switch block (nil = no fork, 0 = already on london)
ArrowGlacierBlock *big.Int `json:"arrowGlacierBlock,omitempty"` // Eip-4345 (bomb delay) switch block (nil = no fork, 0 = already activated) ArrowGlacierBlock *big.Int `json:"arrowGlacierBlock,omitempty"` // Eip-4345 (bomb delay) switch block (nil = no fork, 0 = already activated)
GrayGlacierBlock *big.Int `json:"grayGlacierBlock,omitempty"` // Eip-5133 (bomb delay) switch block (nil = no fork, 0 = already activated) GrayGlacierBlock *big.Int `json:"grayGlacierBlock,omitempty"` // Eip-5133 (bomb delay) switch block (nil = no fork, 0 = already activated)
MergeNetsplitBlock *big.Int `json:"mergeNetsplitBlock,omitempty"` // Virtual fork after The Merge to use as a network splitter MergeNetsplitBlock *big.Int `json:"mergeNetsplitBlock,omitempty"` // Virtual fork after The Merge to use as a network splitter
// Fork scheduling was switched from blocks to timestamps here // Fork scheduling was switched from blocks to timestamps here

View File

@ -22,6 +22,37 @@ import (
"testing" "testing"
) )
var (
// AllEthashProtocolChanges contains every protocol change (EIPs) introduced
// and accepted by the Ethereum core developers into the Ethash consensus.
AllEthashProtocolChanges = &ChainConfig{
ChainID: big.NewInt(1337),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
DAOForkSupport: false,
EIP150Block: big.NewInt(0),
EIP155Block: big.NewInt(0),
EIP158Block: big.NewInt(0),
ByzantiumBlock: big.NewInt(0),
ConstantinopleBlock: big.NewInt(0),
PetersburgBlock: big.NewInt(0),
IstanbulBlock: big.NewInt(0),
MuirGlacierBlock: big.NewInt(0),
BerlinBlock: big.NewInt(0),
LondonBlock: big.NewInt(0),
ArrowGlacierBlock: big.NewInt(0),
GrayGlacierBlock: big.NewInt(0),
MergeNetsplitBlock: nil,
ShanghaiTime: nil,
CancunTime: nil,
PragueTime: nil,
TerminalTotalDifficulty: nil,
TerminalTotalDifficultyPassed: false,
Ethash: new(EthashConfig),
Clique: nil,
}
)
func TestCheckCompatible(t *testing.T) { func TestCheckCompatible(t *testing.T) {
type test struct { type test struct {
stored, new *ChainConfig stored, new *ChainConfig

View File

@ -26,10 +26,10 @@ import (
"testing" "testing"
"github.com/openrelayxyz/plugeth-utils/core" "github.com/openrelayxyz/plugeth-utils/core"
"github.com/openrelayxyz/plugeth-utils/restricted/hexutil"
"github.com/openrelayxyz/plugeth-utils/restricted/types"
"github.com/openrelayxyz/plugeth-utils/restricted/crypto" "github.com/openrelayxyz/plugeth-utils/restricted/crypto"
"github.com/openrelayxyz/plugeth-utils/restricted/hexutil"
"github.com/openrelayxyz/plugeth-utils/restricted/rlp" "github.com/openrelayxyz/plugeth-utils/restricted/rlp"
"github.com/openrelayxyz/plugeth-utils/restricted/types"
) )
func fromHex(data string) []byte { func fromHex(data string) []byte {
@ -136,9 +136,10 @@ func (d *hashToHumanReadable) Reset() {
d.data = make([]byte, 0) d.data = make([]byte, 0)
} }
func (d *hashToHumanReadable) Update(i []byte, i2 []byte) { func (d *hashToHumanReadable) Update(i []byte, i2 []byte) error {
l := fmt.Sprintf("%x %x\n", i, i2) l := fmt.Sprintf("%x %x\n", i, i2)
d.data = append(d.data, []byte(l)...) d.data = append(d.data, []byte(l)...)
return nil
} }
func (d *hashToHumanReadable) Hash() core.Hash { func (d *hashToHumanReadable) Hash() core.Hash {