Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction hashes and failing tests #2399

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"errors"
"io/fs"
"strings"

"github.com/spf13/viper"

Expand Down Expand Up @@ -40,4 +41,5 @@ func InitConfig() {
log.Global.Infof("Loading config from environment variables with prefix: '%s_'", constants.ENV_PREFIX)
viper.SetEnvPrefix(constants.ENV_PREFIX)
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) // Replace hyphens with underscores for env variables
}
15 changes: 9 additions & 6 deletions cmd/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"os"
"strings"
"testing"

"github.com/dominant-strategies/go-quai/common/constants"
Expand All @@ -21,8 +22,8 @@ func testXDGConfigLoading(t *testing.T) {
defer tempFile.Close()
defer os.RemoveAll(mockConfigPath)

// write 'LOG_LEVEL=debug' config to mock config.yaml file
_, err := tempFile.WriteString(LogLevelFlag.Name + " : " + "debug\n")
// write 'log-level = debug' config to mock config.yaml file
_, err := tempFile.WriteString(LogLevelFlag.Name + " = " + "\"debug\"\n")
require.NoError(t, err)

// Set config path to the temporary config directory
Expand All @@ -38,19 +39,21 @@ func testXDGConfigLoading(t *testing.T) {
// the loading of the environment variable and the loading of the cobra flag.
// It verifies the expected order of precedence of config loading.
func TestCobraFlagConfigLoading(t *testing.T) {
t.Skip("Todo: fix failing test")
// Clear viper instance to simulate a fresh start
viper.Reset()
viper.AutomaticEnv()
viper.SetEnvPrefix(constants.ENV_PREFIX)
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) // Replace hyphens with underscores

// Test loading config from XDG config home
testXDGConfigLoading(t)
assert.Equal(t, "debug", viper.GetString(LogLevelFlag.Name))

// Test loading config from environment variable
err := os.Setenv(constants.ENV_PREFIX+"_"+"LOG-LEVEL", "error")
defer os.Unsetenv(constants.ENV_PREFIX + "_" + "LOG-LEVEL")
err := os.Setenv(constants.ENV_PREFIX+"_"+"LOG_LEVEL", "error")
defer os.Unsetenv(constants.ENV_PREFIX + "_" + "LOG_LEVEL")
require.NoError(t, err)
assert.Equal(t, "error", viper.GetString(LogLevelFlag.Name))
assert.Equal(t, "error", viper.GetString("LOG_LEVEL"))

// Test loading config from cobra flag
rootCmd := &cobra.Command{}
Expand Down
3 changes: 2 additions & 1 deletion core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ func EmptyWorkObject(nodeCtx int) *WorkObject {
wo.woBody.SetTransactions([]*Transaction{})
wo.woBody.SetOutboundEtxs([]*Transaction{})
wo.woBody.SetManifest(BlockManifest{})
return NewWorkObjectWithHeader(wo, NewEmptyTx(), nodeCtx, BlockObject)
tx := QuaiTxData()
return NewWorkObjectWithHeader(wo, tx, nodeCtx, BlockObject)
}

func EmptyZoneWorkObject() *WorkObject {
Expand Down
87 changes: 60 additions & 27 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,58 @@ func NewTx(inner TxData) *Transaction {
return tx
}

func NewEmptyTx() *Transaction {
to := common.BytesToAddress([]byte{0x01}, common.Location{0, 0})
func QuaiTxData() *Transaction {
to := common.HexToAddress("0x00bcdef0123456789abcdef0123456789abcdef2", common.Location{0, 0})
address := common.HexToAddress("0x0056789abcdef0123456789abcdef0123456789a", common.Location{0, 0})
parentHash := common.HexToHash("0x456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef3")
mixHash := common.HexToHash("0x56789abcdef0123456789abcdef0123456789abcdef0123456789abcdef4")
workNonce := EncodeNonce(1)
inner := &QuaiTx{
ChainID: new(big.Int).SetUint64(1),
Nonce: 1,
MinerTip: new(big.Int).SetUint64(0),
GasPrice: new(big.Int).SetUint64(0),
Gas: uint64(0),
To: &to,
Value: new(big.Int).SetUint64(0),
Data: []byte{},
AccessList: AccessList{},
V: new(big.Int).SetUint64(0),
R: new(big.Int).SetUint64(0),
S: new(big.Int).SetUint64(0),
ChainID: big.NewInt(1),
Nonce: 1,
MinerTip: big.NewInt(1),
GasPrice: big.NewInt(1),
Gas: 1,
To: &to,
Value: big.NewInt(1),
Data: []byte{0x04},
AccessList: AccessList{AccessTuple{
Address: address,
StorageKeys: []common.Hash{common.HexToHash("0x23456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef1")},
},
},
V: new(big.Int).SetUint64(1),
R: new(big.Int).SetUint64(1),
S: new(big.Int).SetUint64(1),
ParentHash: &parentHash,
MixHash: &mixHash,
WorkNonce: &workNonce,
}
return NewTx(inner)
}

func NewEmptyQiTx() *Transaction {
to := common.BytesToAddress([]byte{0x01}, common.Location{0, 0})
in := TxIn{
PreviousOutPoint: *NewOutPoint(&common.Hash{},
MaxOutputIndex),
PubKey: []byte{0x04, 0x50, 0x49, 0x5c, 0xb2, 0xf9, 0x53, 0x5c, 0x68, 0x4e, 0xbe, 0x46, 0x87, 0xb5, 0x01, 0xc0, 0xd4, 0x1a, 0x62, 0x3d, 0x68, 0xc1, 0x18, 0xb8, 0xdc, 0xec, 0xd3, 0x93, 0x37, 0x0f, 0x1d, 0x90, 0xe6, 0x5c, 0x4c, 0x6c, 0x44, 0xcd, 0x3f, 0xe8, 0x09, 0xb4, 0x1d, 0xfa, 0xc9, 0x06, 0x0a, 0xd8, 0x4c, 0xb5, 0x7e, 0x2d, 0x57, 0x5f, 0xad, 0x24, 0xd2, 0x5a, 0x7e, 0xfa, 0x33, 0x96, 0xe7, 0x3c, 0x10},
}

newOut := TxOut{
Denomination: uint8(1),
Address: to.Bytes(),
}

utxo := &QiTx{
ChainID: big.NewInt(1337),
TxIn: TxIns{in},
TxOut: TxOuts{newOut},
}

return NewTx(utxo)
}

func (tx *Transaction) SetInner(inner TxData) {
tx.setDecoded(inner, 0)
}
Expand Down Expand Up @@ -151,27 +184,27 @@ func (tx *Transaction) ProtoEncode() (*ProtoTransaction, error) {
// Other fields are set conditionally depending on tx type.
switch tx.Type() {
case QuaiTxType:
if tx.To() != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was simply rearranged to match the order of elements in QuaiTx for easier verification.

protoTx.To = tx.To().Bytes()
}
nonce := tx.Nonce()
gas := tx.Gas()
protoTx.Nonce = &nonce
protoTx.Gas = &gas
protoTx.AccessList = tx.AccessList().ProtoEncode()
protoTx.Value = tx.Value().Bytes()
gas := tx.Gas()
protoTx.Gas = &gas
if tx.Data() == nil {
protoTx.Data = []byte{}
} else {
protoTx.Data = tx.Data()
}
if tx.To() != nil {
protoTx.To = tx.To().Bytes()
}
protoTx.ChainId = tx.ChainId().Bytes()
protoTx.MinerTip = tx.MinerTip().Bytes()
protoTx.GasPrice = tx.GasPrice().Bytes()
protoTx.AccessList = tx.AccessList().ProtoEncode()
V, R, S := tx.GetEcdsaSignatureValues()
protoTx.V = V.Bytes()
protoTx.R = R.Bytes()
protoTx.S = S.Bytes()
protoTx.ChainId = tx.ChainId().Bytes()
if tx.ParentHash() != nil {
protoTx.ParentHash = tx.ParentHash().ProtoEncode()
}
Expand Down Expand Up @@ -235,7 +268,7 @@ func (tx *Transaction) ProtoDecode(protoTx *ProtoTransaction, location common.Lo
txType := protoTx.GetType()

switch txType {
case 0:
case QuaiTxType:
if protoTx.Nonce == nil {
return errors.New("missing required field 'Nonce' in ProtoTransaction")
}
Expand Down Expand Up @@ -318,7 +351,7 @@ func (tx *Transaction) ProtoDecode(protoTx *ProtoTransaction, location common.Lo
}
tx.SetInner(&quaiTx)

case 1:
case ExternalTxType:
if protoTx.Gas == nil {
return errors.New("missing required field 'Gas' in ProtoTransaction")
}
Expand Down Expand Up @@ -359,7 +392,7 @@ func (tx *Transaction) ProtoDecode(protoTx *ProtoTransaction, location common.Lo

tx.SetInner(&etx)

case 2:
case QiTxType:
if protoTx.TxIns == nil {
return errors.New("missing required field 'TxIns' in ProtoTransaction")
}
Expand Down Expand Up @@ -425,7 +458,7 @@ func (tx *Transaction) ProtoEncodeTxSigningData() *ProtoTransaction {
return protoTxSigningData
}
switch tx.Type() {
case 0:
case QuaiTxType:
txType := uint64(tx.Type())
protoTxSigningData.Type = &txType
protoTxSigningData.ChainId = tx.ChainId().Bytes()
Expand All @@ -445,9 +478,9 @@ func (tx *Transaction) ProtoEncodeTxSigningData() *ProtoTransaction {
}
protoTxSigningData.MinerTip = tx.MinerTip().Bytes()
protoTxSigningData.GasPrice = tx.GasPrice().Bytes()
case 1:
case ExternalTxType:
return protoTxSigningData
case 2:
case QiTxType:
txType := uint64(tx.Type())
protoTxSigningData.Type = &txType
protoTxSigningData.ChainId = tx.ChainId().Bytes()
Expand Down
Loading
Loading