Skip to content

Commit

Permalink
Merge pull request #1981 from OffchainLabs/U256Bytes
Browse files Browse the repository at this point in the history
Use U256Bytes from arbmath
  • Loading branch information
PlasmaPower authored Nov 28, 2023
2 parents 328d956 + 64d318e commit 5d4266e
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 29 deletions.
2 changes: 1 addition & 1 deletion arbnode/delayed.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (m *DelayedInboxMessage) AfterInboxAcc() common.Hash {
arbmath.UintToBytes(m.Message.Header.BlockNumber),
arbmath.UintToBytes(m.Message.Header.Timestamp),
m.Message.Header.RequestId.Bytes(),
math.U256Bytes(m.Message.Header.L1BaseFee),
arbmath.U256Bytes(m.Message.Header.L1BaseFee),
crypto.Keccak256(m.Message.L2msg),
)
return crypto.Keccak256Hash(m.BeforeInboxAcc[:], hash)
Expand Down
7 changes: 3 additions & 4 deletions arbnode/inbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/offchainlabs/nitro/util/testhelpers"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -148,10 +147,10 @@ func TestTransactionStreamer(t *testing.T) {
var gas uint64 = 100000
var l2Message []byte
l2Message = append(l2Message, arbos.L2MessageKind_ContractTx)
l2Message = append(l2Message, math.U256Bytes(new(big.Int).SetUint64(gas))...)
l2Message = append(l2Message, math.U256Bytes(big.NewInt(l2pricing.InitialBaseFeeWei))...)
l2Message = append(l2Message, arbmath.Uint64ToU256Bytes(gas)...)
l2Message = append(l2Message, arbmath.Uint64ToU256Bytes(l2pricing.InitialBaseFeeWei)...)
l2Message = append(l2Message, dest.Hash().Bytes()...)
l2Message = append(l2Message, math.U256Bytes(value)...)
l2Message = append(l2Message, arbmath.U256Bytes(value)...)
var requestId common.Hash
binary.BigEndian.PutUint64(requestId.Bytes()[:8], uint64(i))
messages = append(messages, arbostypes.MessageWithMetadata{
Expand Down
5 changes: 2 additions & 3 deletions arbnode/transaction_streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/syndtr/goleveldb/leveldb"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
Expand All @@ -35,6 +34,7 @@ import (
"github.com/offchainlabs/nitro/broadcaster"
"github.com/offchainlabs/nitro/execution"
"github.com/offchainlabs/nitro/staker"
"github.com/offchainlabs/nitro/util/arbmath"
"github.com/offchainlabs/nitro/util/sharedmetrics"
"github.com/offchainlabs/nitro/util/stopwaiter"
)
Expand Down Expand Up @@ -531,8 +531,7 @@ func (s *TransactionStreamer) AddFakeInitMessage() error {
if err != nil {
return fmt.Errorf("failed to serialize chain config: %w", err)
}
// TODO: once we have a safe U256Bytes that does a copy internally, use that instead of doing an explicit copy here
chainIdBytes := math.U256Bytes(new(big.Int).Set(s.chainConfig.ChainID))
chainIdBytes := arbmath.U256Bytes(s.chainConfig.ChainID)
msg := append(append(chainIdBytes, 0), chainConfigJson...)
return s.AddMessages(0, false, []arbostypes.MessageWithMetadata{{
Message: &arbostypes.L1IncomingMessage{
Expand Down
7 changes: 3 additions & 4 deletions arbos/parse_l2.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -40,8 +39,8 @@ func ParseL2Transactions(msg *arbostypes.L1IncomingMessage, chainId *big.Int, ba
return nil, errors.New("cannot issue L2 funded by L1 tx without L1 request id")
}
kind := msg.L2msg[0]
depositRequestId := crypto.Keccak256Hash(msg.Header.RequestId[:], math.U256Bytes(common.Big0))
unsignedRequestId := crypto.Keccak256Hash(msg.Header.RequestId[:], math.U256Bytes(common.Big1))
depositRequestId := crypto.Keccak256Hash(msg.Header.RequestId[:], arbmath.U256Bytes(common.Big0))
unsignedRequestId := crypto.Keccak256Hash(msg.Header.RequestId[:], arbmath.U256Bytes(common.Big1))
tx, err := parseUnsignedTx(bytes.NewReader(msg.L2msg[1:]), msg.Header.Poster, &unsignedRequestId, chainId, kind)
if err != nil {
return nil, err
Expand Down Expand Up @@ -146,7 +145,7 @@ func parseL2Message(rd io.Reader, poster common.Address, timestamp uint64, reque

var nextRequestId *common.Hash
if requestId != nil {
subRequestId := crypto.Keccak256Hash(requestId[:], math.U256Bytes(index))
subRequestId := crypto.Keccak256Hash(requestId[:], arbmath.U256Bytes(index))
nextRequestId = &subRequestId
}
nestedSegments, err := parseL2Message(bytes.NewReader(nextMsg), poster, timestamp, nextRequestId, chainId, depth+1)
Expand Down
2 changes: 1 addition & 1 deletion arbos/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestStorageBackedBigInt(t *testing.T) {
t.Fatal(err)
}
// Verify that our encoding matches geth's signed complement impl
expectedRawVal := common.BigToHash(math.U256(new(big.Int).Set(in)))
expectedRawVal := common.BigToHash(arbmath.U256(in))
if rawVal != expectedRawVal {
t.Fatal("for input", in, "expected raw value", expectedRawVal, "but got", rawVal)
}
Expand Down
7 changes: 3 additions & 4 deletions precompiles/ArbSys.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/offchainlabs/nitro/arbos/util"
"github.com/offchainlabs/nitro/util/arbmath"
Expand Down Expand Up @@ -121,9 +120,9 @@ func (con *ArbSys) SendTxToL1(c ctx, evm mech, value huge, destination addr, cal
sendHash, err := arbosState.KeccakHash(
c.caller.Bytes(),
destination.Bytes(),
math.U256Bytes(evm.Context.BlockNumber),
math.U256Bytes(bigL1BlockNum),
math.U256Bytes(&t),
arbmath.U256Bytes(evm.Context.BlockNumber),
arbmath.U256Bytes(bigL1BlockNum),
arbmath.U256Bytes(&t),
common.BigToHash(value).Bytes(),
calldataForL1,
)
Expand Down
7 changes: 4 additions & 3 deletions staker/assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"

"github.com/offchainlabs/nitro/solgen/go/rollupgen"
"github.com/offchainlabs/nitro/util/arbmath"
"github.com/offchainlabs/nitro/validator"
)

Expand Down Expand Up @@ -39,8 +40,8 @@ func HashChallengeState(
hashesBytes = append(hashesBytes, h[:]...)
}
return crypto.Keccak256Hash(
math.U256Bytes(new(big.Int).SetUint64(segmentStart)),
math.U256Bytes(new(big.Int).SetUint64(segmentLength)),
arbmath.Uint64ToU256Bytes(segmentStart),
arbmath.Uint64ToU256Bytes(segmentLength),
hashesBytes,
)
}
Expand Down
7 changes: 3 additions & 4 deletions system_tests/contract_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -64,10 +63,10 @@ func TestContractTxDeploy(t *testing.T) {
Data: deployCode,
}
l2Msg := []byte{arbos.L2MessageKind_ContractTx}
l2Msg = append(l2Msg, math.U256Bytes(arbmath.UintToBig(contractTx.Gas))...)
l2Msg = append(l2Msg, math.U256Bytes(contractTx.GasFeeCap)...)
l2Msg = append(l2Msg, arbmath.Uint64ToU256Bytes(contractTx.Gas)...)
l2Msg = append(l2Msg, arbmath.U256Bytes(contractTx.GasFeeCap)...)
l2Msg = append(l2Msg, common.Hash{}.Bytes()...) // to is zero, translated into nil
l2Msg = append(l2Msg, math.U256Bytes(contractTx.Value)...)
l2Msg = append(l2Msg, arbmath.U256Bytes(contractTx.Value)...)
l2Msg = append(l2Msg, contractTx.Data...)

err = builder.L2.ConsensusNode.TxStreamer.AddMessages(pos, true, []arbostypes.MessageWithMetadata{
Expand Down
6 changes: 3 additions & 3 deletions system_tests/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ package arbtest
import (
"bytes"
"context"
"math/big"
"strings"
"testing"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/params"

"github.com/offchainlabs/nitro/util/arbmath"
)

func testContractDeployment(t *testing.T, ctx context.Context, client *ethclient.Client, contractCode []byte, accountInfo *AccountInfo, expectedEstimateGasError error) {
Expand All @@ -26,7 +26,7 @@ func testContractDeployment(t *testing.T, ctx context.Context, client *ethclient
0x7F, // PUSH32
}
// len(contractCode)
deployCode = append(deployCode, math.U256Bytes(big.NewInt(int64(len(contractCode))))...)
deployCode = append(deployCode, arbmath.Uint64ToU256Bytes(uint64(len(contractCode)))...)
var codeOffset byte = 42
deployCode = append(deployCode, []byte{
0x80, // DUP
Expand Down
5 changes: 3 additions & 2 deletions system_tests/reorg_resequencing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/params"

"github.com/offchainlabs/nitro/arbos/arbostypes"
"github.com/offchainlabs/nitro/util/arbmath"
)

func TestReorgResequencing(t *testing.T) {
Expand Down Expand Up @@ -69,7 +70,7 @@ func TestReorgResequencing(t *testing.T) {
RequestId: &delayedIndexHash,
L1BaseFee: common.Big0,
},
L2msg: append(builder.L2Info.GetAddress("User4").Bytes(), math.U256Bytes(big.NewInt(params.Ether))...),
L2msg: append(builder.L2Info.GetAddress("User4").Bytes(), arbmath.Uint64ToU256Bytes(params.Ether)...),
}
err = builder.L2.ConsensusNode.TxStreamer.AddMessages(startMsgCount, true, []arbostypes.MessageWithMetadata{{
Message: newMessage,
Expand Down
18 changes: 18 additions & 0 deletions util/arbmath/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/big"
"math/bits"

eth_math "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/params"
)

Expand Down Expand Up @@ -376,3 +377,20 @@ func BalancePerEther(balance *big.Int) float64 {
balancePerEther, _ := new(big.Float).Quo(new(big.Float).SetInt(balance), new(big.Float).SetFloat64(params.Ether)).Float64()
return balancePerEther
}

// U256Bytes converts big Int to 256bit EVM number.
// This operation makes a copy of big Int.
func U256Bytes(n *big.Int) []byte {
return eth_math.U256Bytes(new(big.Int).Set(n))
}

// U256 encodes as a 256 bit two's complement number.
// This operation makes a copy of big Int.
func U256(x *big.Int) *big.Int {
return eth_math.U256(new(big.Int).Set(x))
}

// Uint64ToU256Bytes converts uint64 to 256bit EVM number.
func Uint64ToU256Bytes(n uint64) []byte {
return eth_math.U256Bytes(UintToBig(n))
}

0 comments on commit 5d4266e

Please sign in to comment.