Skip to content

Commit

Permalink
all: revamp for testnet3
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlee42 committed Nov 8, 2024
1 parent 9b4732a commit 4d6ca27
Show file tree
Hide file tree
Showing 30 changed files with 766 additions and 356 deletions.
8 changes: 4 additions & 4 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1066,8 +1066,8 @@ func setBootstrapNodesV5(ctx *cli.Context, cfg *p2p.Config) {
case ctx.IsSet(GoatNetworkFlag.Name):
goatNetwork := ctx.String(GoatNetworkFlag.Name)
switch goatNetwork {
case "testnet":
urls = params.V5GoatTestnetBootnodes
case "testnet3":
urls = params.V5GoatTestnet3Bootnodes
}
}

Expand Down Expand Up @@ -2140,8 +2140,8 @@ func MakeGenesis(ctx *cli.Context) *core.Genesis {
case ctx.IsSet(GoatNetworkFlag.Name):
netwk := ctx.String(GoatNetworkFlag.Name)
switch netwk {
case "testnet":
genesis = core.DefaultGoatTestnetGenesisBlock()
case "testnet3":
genesis = core.DefaultGoatTestnet3GenesisBlock()
default:
Fatalf("unknown goat network: %s", netwk)
}
Expand Down
16 changes: 8 additions & 8 deletions core/genesis_goat.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
//go:embed goat
var goatGenesis embed.FS

// DefaultGoatTestnetGenesisBlock returns the Goat Testnet genesis block.
func DefaultGoatTestnetGenesisBlock() *Genesis {
raw, err := goatGenesis.ReadFile("goat/testnet.json")
// DefaultGoatTestnet3GenesisBlock returns the Goat Testnet3 genesis block.
func DefaultGoatTestnet3GenesisBlock() *Genesis {
raw, err := goatGenesis.ReadFile("goat/testnet3.json")
if err != nil {
panic(err)
}
Expand All @@ -26,13 +26,13 @@ func DefaultGoatTestnetGenesisBlock() *Genesis {
panic(err)
}
return &Genesis{
Config: params.GoatTestnetChainConfig,
Config: params.GoatTestnet3ChainConfig,
Nonce: 0,
Timestamp: 0x6710b732,
Timestamp: 0, // todo(ericlee42)
ExtraData: common.Hex2Bytes("0056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"),
GasLimit: 0x1c9c380,
Difficulty: big.NewInt(1),
GasLimit: params.GoatTxGasLimit,
Difficulty: common.Big0,
Alloc: alloc,
BaseFee: big.NewInt(500000000),
BaseFee: big.NewInt(2028449),
}
}
File renamed without changes.
20 changes: 19 additions & 1 deletion core/state_processor_goat.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func ProcessGoatRequests(height uint64, reward *big.Int, allLogs []*types.Log) (
for _, log := range allLogs {
switch log.Address {
case goattypes.BridgeContract:
if len(log.Topics) < 2 {
if len(log.Topics) < 1 {
continue
}
switch log.Topics[0] {
Expand All @@ -75,6 +75,24 @@ func ProcessGoatRequests(height uint64, reward *big.Int, allLogs []*types.Log) (
return nil, err
}
bridgeRequests.Cancel1s = append(bridgeRequests.Cancel1s, req)
case goattypes.UpdateDepositTaxEventTopic:
req, err := goattypes.UnpackIntoDepositTaxRequest(log.Data)
if err != nil {
return nil, err
}
bridgeRequests.DepositTax = append(bridgeRequests.DepositTax, req)
case goattypes.ConfirmationNumberEventTopic:
req, err := goattypes.UnpackIntoConfirmationNumberRequest(log.Data)
if err != nil {
return nil, err
}
bridgeRequests.Confirmation = append(bridgeRequests.Confirmation, req)
case goattypes.UpdateMinDepositEventTopic:
req, err := goattypes.UnpackIntoMinDepositRequest(log.Data)
if err != nil {
return nil, err
}
bridgeRequests.MinDeposit = append(bridgeRequests.MinDeposit, req)
}
case goattypes.LockingContract:
if len(log.Topics) != 1 {
Expand Down
32 changes: 32 additions & 0 deletions core/state_processor_goat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ func TestProcessGoatRequests(t *testing.T) {
common.HexToHash("0x000000000000000000000000000000000000000000000000000000000000000a"),
},
},

{
Address: goattypes.BridgeContract,
Topics: []common.Hash{
common.HexToHash("0x30b92002139b64ec601b714d1ecccba1212034e735773b3de088e8876f4dfb65"),
},
Data: hexutil.MustDecode("0x0000000000000000000000000000000000000000000000000000000000000006"),
},
{
Address: goattypes.BridgeContract,
Topics: []common.Hash{
common.HexToHash("0x7aa20a242ea0b0f7b0141c56aaad636eb2b2077c9c27d09a8282f6931f486a21"),
},
Data: hexutil.MustDecode("0x00000000000000000000000000000000000000000000000000005af3107a4000"),
},
{
Address: goattypes.BridgeContract,
Topics: []common.Hash{
common.HexToHash("0x1007ff7aec53e9626ce51f25d4e093f290f60da8019c8cf489f0ae2f21ebf76a"),
},
Data: hexutil.MustDecode("0x000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000005af3107a4000"),
},

{
Address: goattypes.RelayerContract,
Topics: []common.Hash{
Expand Down Expand Up @@ -200,6 +223,15 @@ func TestProcessGoatRequests(t *testing.T) {
Cancel1s: []*goattypes.Cancel1Request{
{Id: 10},
},
DepositTax: []*goattypes.DepositTaxRequest{
{Rate: 2, Max: 10000},
},
Confirmation: []*goattypes.ConfirmationNumberRequest{
{Number: 6},
},
MinDeposit: []*goattypes.MinDepositRequest{
{Satoshi: 10000},
},
},
relayer: goattypes.RelayerRequests{
Adds: []*goattypes.AddVoterRequest{
Expand Down
4 changes: 2 additions & 2 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ type Message struct {
// goat
IsGoatTx bool
Deposit *goattypes.Mint // deposit from L1
Claim *goattypes.Mint // gas fee and unlocks from consensus layer
Withdraw *goattypes.Mint // withdraw from consensus layer
}

// TransactionToMessage converts a transaction into a Message.
Expand All @@ -176,7 +176,7 @@ func TransactionToMessage(tx *types.Transaction, s types.Signer, baseFee *big.In
// goat
IsGoatTx: tx.IsGoatTx(),
Deposit: tx.Deposit(),
Claim: tx.Claim(),
Withdraw: tx.Withdraw(),
}
// If baseFee provided, set gasPrice to effectiveGasPrice.
if baseFee != nil {
Expand Down
33 changes: 13 additions & 20 deletions core/state_transition_goat.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,28 @@ func (st *StateTransition) goat(msg *Message, ret []byte, vmerr error) (*Executi
return nil, fmt.Errorf("goat tx reverted (to %s nonce %d data %x err %s ret %x)", msg.To, msg.Nonce, msg.Data, vmerr, ret)
}

// deposit
// deposit from L1
if v := msg.Deposit; v != nil {
amount, overflow := uint256.FromBig(v.Amount)
if overflow {
return nil, fmt.Errorf("goat tx error (amount overflowed to mint: %s)", v.Amount)
return nil, fmt.Errorf("goat tx error (deposit overflowed to mint: %s)", v.Amount)
}

// get the tax from call returns
if len(ret) != 32 {
return nil, fmt.Errorf("goat tx error (deposit should return uint256 but got %x)", ret)
}

// sub the tax and pay it to GF
tax := new(uint256.Int).SetBytes(ret)
if !tax.IsZero() {
if amount.Cmp(tax) < 0 {
return nil, fmt.Errorf("goat tx error (tax is larger: deposit %s tax %s)", v.Amount, tax)
// add the deposit value to the target
log.Debug("NewDeposit", "address", v.Address, "amount", amount, "tax", v.Tax)
st.state.AddBalance(v.Address, amount, tracing.BalanceGoatDepoist)
if v.Tax.Sign() > 0 {
// add the tax to GF
tax, overflow := uint256.FromBig(v.Tax)
if overflow {
return nil, fmt.Errorf("goat tx error (tax overflowed to mint: %s)", v.Tax)
}
amount.Sub(amount, tax)
st.state.AddBalance(goattypes.GoatFoundationContract, tax, tracing.BalanceGoatDepoist)
}

// add the deposit value(withtout tax) to the target
log.Debug("NewDeposit", "address", v.Address, "amount", amount, "tax", tax)
st.state.AddBalance(v.Address, amount, tracing.BalanceGoatDepoist)
}

// distribute reward or unlocking amount
if v := msg.Claim; v != nil && v.Amount.Sign() > 0 {
// withdraw from consensus layer
if v := msg.Withdraw; v != nil && v.Amount.Sign() > 0 {
amount, overflow := uint256.FromBig(v.Amount)
if overflow {
return nil, fmt.Errorf("goat tx error (amount overflowed to distribute: %s)", v.Amount)
Expand All @@ -61,7 +54,7 @@ func (st *StateTransition) goat(msg *Message, ret []byte, vmerr error) (*Executi
// 2. gas fee addding in the runtime

// add the value to the target
log.Debug("NewClaim/Unlock", "address", v.Address, "amount", amount)
log.Debug("NewWithdraw", "address", v.Address, "amount", amount)
if !CanTransfer(st.state, goattypes.LockingContract, amount) {
return nil, fmt.Errorf("goat tx error (amount too large to distribute: %s)", v.Amount)
}
Expand Down
14 changes: 8 additions & 6 deletions core/state_transition_goat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func TestGoatStateTransition(t *testing.T) {

lockingBalance := new(big.Int).Set(alloc[goattypes.LockingContract].Balance)

tax := new(big.Int).Mul(depositValue, depositTaxBp)
tax.Div(tax, mxaBp)
if tax.Cmp(maxDepositTax) > 0 {
tax = tax.Set(maxDepositTax)
}

_, blocks, _ := GenerateChainWithGenesis(gspec, engine, 1, func(i int, b *BlockGen) {
b.SetCoinbase(coinbase)
b.AddTx(types.NewTx(types.NewGoatTx(
Expand All @@ -59,7 +65,8 @@ func TestGoatStateTransition(t *testing.T) {
Txid: common.HexToHash("0x344fb824c793fc370a38577eea12aba8842cb0516cf52099911a36c0c36f11ee"),
TxOut: 0,
Target: depositAddress,
Amount: depositValue,
Amount: new(big.Int).Sub(depositValue, tax),
Tax: tax,
},
)))

Expand Down Expand Up @@ -135,11 +142,6 @@ func TestGoatStateTransition(t *testing.T) {
t.Errorf("balance of coinbase should be 0")
}

tax := new(big.Int).Mul(depositValue, depositTaxBp)
tax.Div(tax, mxaBp)
if tax.Cmp(maxDepositTax) > 0 {
tax = tax.Set(maxDepositTax)
}
if value, got := new(big.Int).Sub(depositValue, tax), state.GetBalance(depositAddress); got.CmpBig(value) != 0 {
t.Errorf("balance of deposited, expected to be %s got %s", value, got)
}
Expand Down
107 changes: 54 additions & 53 deletions core/testdata/goat-genesis.json

Large diffs are not rendered by default.

34 changes: 0 additions & 34 deletions core/types/goattypes/event_tracer.go

This file was deleted.

74 changes: 0 additions & 74 deletions core/types/goattypes/event_tracer_test.go

This file was deleted.

Loading

0 comments on commit 4d6ca27

Please sign in to comment.