Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
d24bd6a
lint enable gosec
JonathanOppenheimer Sep 10, 2025
b465187
make tests deterministic
JonathanOppenheimer Sep 10, 2025
969b51e
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Sep 11, 2025
01395ef
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Sep 12, 2025
3b51256
Update plugin/evm/message/helpers_test.go
JonathanOppenheimer Sep 12, 2025
fb0a1c6
first round stephen reviews
JonathanOppenheimer Sep 12, 2025
6021c88
redo rand implementation
JonathanOppenheimer Sep 12, 2025
e2dd1c0
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Sep 12, 2025
eab8c81
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Sep 17, 2025
e9e136d
austin suggestion
JonathanOppenheimer Sep 17, 2025
66e1c6f
lol what
JonathanOppenheimer Sep 17, 2025
7cbf4c8
lint
JonathanOppenheimer Sep 17, 2025
f92fa5a
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Sep 17, 2025
37f65a0
revert balance
JonathanOppenheimer Sep 18, 2025
57ec43b
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Sep 18, 2025
5fd167c
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Sep 22, 2025
b050e4f
Austin suggestions
JonathanOppenheimer Sep 22, 2025
d960769
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Sep 23, 2025
c121ab6
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Sep 24, 2025
eaf433d
Austin suggestions
JonathanOppenheimer Sep 24, 2025
341e908
clean-up
JonathanOppenheimer Sep 24, 2025
7172e4f
update request strings
JonathanOppenheimer Sep 24, 2025
40ebd2b
this too
JonathanOppenheimer Sep 24, 2025
80c06b8
use correct rand
JonathanOppenheimer Sep 24, 2025
603dd3b
crypto rand
JonathanOppenheimer Sep 24, 2025
57989a3
more aggressive corruption
JonathanOppenheimer Sep 24, 2025
b244df3
fix seeding here too
JonathanOppenheimer Sep 24, 2025
62ec0ca
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Sep 24, 2025
8e657bf
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Sep 25, 2025
e02b224
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Sep 29, 2025
8d0388a
fix imports
JonathanOppenheimer Sep 29, 2025
62612f3
lint
JonathanOppenheimer Sep 29, 2025
1f1bb2a
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Sep 30, 2025
6786d70
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Oct 2, 2025
791ba8d
austin review
JonathanOppenheimer Oct 8, 2025
7da3ca1
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Oct 8, 2025
28e116d
lint
JonathanOppenheimer Oct 8, 2025
ba427bb
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Oct 10, 2025
bc84850
Merge branch 'master' into lint-enable-gosec
JonathanOppenheimer Oct 14, 2025
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: 1 addition & 1 deletion .avalanche-golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ linters:
- goconst
- gocritic
- goprintffuncname
# - gosec
- gosec
- govet
- importas
- ineffassign
Expand Down
2 changes: 1 addition & 1 deletion accounts/abi/bind/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestWaitDeployed(t *testing.T) {
func TestWaitDeployedCornerCases(t *testing.T) {
backend := simulated.NewBackend(
types.GenesisAlloc{
crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(1000000000000000000)},
crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(2000000000000000000)},
},
)
defer backend.Close()
Expand Down
4 changes: 3 additions & 1 deletion cmd/simulator/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"net/http"
"os"
"time"

"github.com/ava-labs/libevm/log"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -66,7 +67,8 @@ func (m *Metrics) Serve(ctx context.Context, metricsPort string, metricsEndpoint
ctx, cancel := context.WithCancel(ctx)
// Create a prometheus server to expose individual tx metrics
server := &http.Server{
Addr: ":" + metricsPort,
Addr: ":" + metricsPort,
ReadHeaderTimeout: 30 * time.Second,
}

// Start up go routine to listen for SIGINT notifications to gracefully shut down server
Expand Down
4 changes: 3 additions & 1 deletion core/blockchain_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1886,7 +1886,9 @@ func ReexecCorruptedStateTest(t *testing.T, create ReexecTestFunc) {
}

// Simulate a crash by updating the acceptor tip
blockchain.writeBlockAcceptedIndices(chain[1])
if err := blockchain.writeBlockAcceptedIndices(chain[1]); err != nil {
t.Fatal(err)
}
blockchain.Stop()

// Restart blockchain with existing state
Expand Down
15 changes: 7 additions & 8 deletions core/extstate/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package extstate

import (
"encoding/binary"
"math/rand"
"path/filepath"
"slices"
"testing"
Expand All @@ -23,6 +22,7 @@ import (

"github.com/ava-labs/coreth/triedb/firewood"
"github.com/ava-labs/coreth/triedb/hashdb"
"github.com/ava-labs/coreth/utils/rand"
)

const (
Expand Down Expand Up @@ -309,9 +309,8 @@ func (fs *fuzzState) deleteStorage(accountIndex int, storageIndexInput uint64) {
}

func FuzzTree(f *testing.F) {
f.Fuzz(func(t *testing.T, randSeed int64, byteSteps []byte) {
f.Fuzz(func(t *testing.T, byteSteps []byte) {
fuzzState := newFuzzState(t)
rand := rand.New(rand.NewSource(randSeed))

for range 10 {
fuzzState.createAccount()
Expand All @@ -333,23 +332,23 @@ func FuzzTree(f *testing.F) {
fuzzState.createAccount()
case updateAccount:
if len(fuzzState.currentAddrs) > 0 {
fuzzState.updateAccount(rand.Intn(len(fuzzState.currentAddrs)))
fuzzState.updateAccount(rand.SecureIntn(len(fuzzState.currentAddrs)))
}
case deleteAccount:
if len(fuzzState.currentAddrs) > 0 {
fuzzState.deleteAccount(rand.Intn(len(fuzzState.currentAddrs)))
fuzzState.deleteAccount(rand.SecureIntn(len(fuzzState.currentAddrs)))
}
case addStorage:
if len(fuzzState.currentAddrs) > 0 {
fuzzState.addStorage(rand.Intn(len(fuzzState.currentAddrs)))
fuzzState.addStorage(rand.SecureIntn(len(fuzzState.currentAddrs)))
}
case updateStorage:
if len(fuzzState.currentAddrs) > 0 {
fuzzState.updateStorage(rand.Intn(len(fuzzState.currentAddrs)), rand.Uint64())
fuzzState.updateStorage(rand.SecureIntn(len(fuzzState.currentAddrs)), rand.SecureUint64())
}
case deleteStorage:
if len(fuzzState.currentAddrs) > 0 {
fuzzState.deleteStorage(rand.Intn(len(fuzzState.currentAddrs)), rand.Uint64())
fuzzState.deleteStorage(rand.SecureIntn(len(fuzzState.currentAddrs)), rand.SecureUint64())
}
default:
t.Fatalf("unknown step: %d", step)
Expand Down
7 changes: 4 additions & 3 deletions network/peer_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package network

import (
"math"
"math/rand"
"time"

"github.com/ava-labs/avalanchego/ids"
Expand All @@ -14,6 +13,8 @@ import (
"github.com/ava-labs/libevm/log"
"github.com/ava-labs/libevm/metrics"

"github.com/ava-labs/coreth/utils/rand"

safemath "github.com/ava-labs/avalanchego/utils/math"
)

Expand Down Expand Up @@ -76,7 +77,7 @@ func (p *peerTracker) shouldTrackNewPeer() bool {
return false
}
newPeerProbability := math.Exp(-float64(numResponsivePeers) * newPeerConnectFactor)
return rand.Float64() < newPeerProbability
return rand.SecureFloat64() < newPeerProbability
}

// getResponsivePeer returns a random [ids.NodeID] of a peer that has responded
Expand Down Expand Up @@ -115,7 +116,7 @@ func (p *peerTracker) GetAnyPeer(minVersion *version.Application) (ids.NodeID, b
random bool
averager safemath.Averager
)
if rand.Float64() < randomPeerProbability {
if rand.SecureFloat64() < randomPeerProbability {
random = true
nodeID, averager, ok = p.getResponsivePeer()
} else {
Expand Down
5 changes: 2 additions & 3 deletions plugin/evm/atomic/atomictest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package atomictest

import (
"math/rand"

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/linearcodec"
"github.com/ava-labs/avalanchego/ids"
Expand All @@ -16,6 +14,7 @@ import (

"github.com/ava-labs/coreth/params/extras"
"github.com/ava-labs/coreth/plugin/evm/atomic"
"github.com/ava-labs/coreth/utils/rand"

avalancheatomic "github.com/ava-labs/avalanchego/chains/atomic"
)
Expand Down Expand Up @@ -150,7 +149,7 @@ func GenerateTestExportTx() *atomic.Tx {
}

func NewTestTx() *atomic.Tx {
txType := rand.Intn(2)
txType := rand.SecureIntn(2)
switch txType {
case 0:
return GenerateTestImportTx()
Expand Down
2 changes: 1 addition & 1 deletion plugin/evm/customtypes/header_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func allFieldsSet[T interface {
if fieldValue.Kind() == reflect.Ptr {
require.Falsef(t, fieldValue.IsNil(), "field %q is nil", field.Name)
}
fieldValue = reflect.NewAt(fieldValue.Type(), unsafe.Pointer(fieldValue.UnsafeAddr())).Elem() //nolint:gosec
fieldValue = reflect.NewAt(fieldValue.Type(), unsafe.Pointer(fieldValue.UnsafeAddr())).Elem()
}

switch f := fieldValue.Interface().(type) {
Expand Down
16 changes: 7 additions & 9 deletions plugin/evm/message/block_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package message

import (
"encoding/base64"
"math/rand"
"fmt"
"testing"

"github.com/ava-labs/libevm/common"
Expand Down Expand Up @@ -38,25 +38,23 @@ func TestMarshalBlockRequest(t *testing.T) {
// TestMarshalBlockResponse asserts that the structure or serialization logic hasn't changed, primarily to
// ensure compatibility with the network.
func TestMarshalBlockResponse(t *testing.T) {
// create some random bytes
// set seed to ensure deterministic random behaviour
rand.Seed(1)
blocksBytes := make([][]byte, 32)
for i := range blocksBytes {
blocksBytes[i] = make([]byte, rand.Intn(32)+32) // min 32 length, max 64
_, err := rand.Read(blocksBytes[i])
assert.NoError(t, err)
size := 32 + (i % 33)
blocksBytes[i] = deterministicBytes(fmt.Sprintf("block-%d", i), size)
}

blockResponse := BlockResponse{
Blocks: blocksBytes,
}

base64BlockResponse := "AAAAAAAgAAAAIU8WP18PmmIdcpVmx00QA3xNe7sEB9HixkmBhVrYaB0NhgAAADnR6ZTSxCKs0gigByk5SH9pmeudGKRHhARdh/PGfPInRumVr1olNnlRuqL/bNRxxIPxX7kLrbN8WCEAAAA6tmgLTnyLdjobHUnUlVyEhiFjJSU/7HON16nii/khEZwWDwcCRIYVu9oIMT9qjrZo0gv1BZh1kh5migAAACtb3yx/xIRo0tbFL1BU4tCDa/hMcXTLdHY2TMPb2Wiw9xcu2FeUuzWLDDtSAAAAO12heG+f69ehnQ97usvgJVqlt9RL7ED4TIkrm//UNimwIjvupfT3Q5H0RdFa/UKUBAN09pJLmMv4cT+NAAAAMpYtJOLK/Mrjph+1hrFDI6a8j5598dkpMz/5k5M76m9bOvbeA3Q2bEcZ5DobBn2JvH8BAAAAOfHxekxyFaO1OeseWEnGB327VyL1cXoomiZvl2R5gZmOvqicC0s3OXARXoLtb0ElyPpzEeTX3vqSLQAAACc2zU8kq/ffhmuqVgODZ61hRd4e6PSosJk+vfiIOgrYvpw5eLBIg+UAAAAkahVqnexqQOmh0AfwM8KCMGG90Oqln45NpkMBBSINCyloi3NLAAAAKI6gENd8luqAp6Zl9gb2pjt/Pf0lZ8GJeeTWDyZobZvy+ybJAf81TN4AAAA8FgfuKbpk+Eq0PKDG5rkcH9O+iZBDQXnTr0SRo2kBLbktGE/DnRc0/1cWQolTu2hl/PkrDDoXyQKL6ZFOAAAAMwl50YMDVvKlTD3qsqS0R11jr76PtWmHx39YGFJvGBS+gjNQ6rE5NfM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GAi1A6wOnDuMAY6j1PIerFL81e60BhjQ2G/Am3jrh+uh5AayFzc3QYudUu/RJ6HAAAAPA4qo0PSD61pmiU/ychcyW/vZZYIF1rk1yP+JPlXBvFppmNnBL5DH8tDZp6YxRYnvfDHfAhdCqQvzwY+sQAAAD3PnXjnTaKHn6sMZvRxTt5laCWK4/doOa2JN9mjCYH44YUnVMs0mXsEqe33ojw5EdKwryrND0ysL0N116nrAAAAPkxMF2yo70lmkPsAJ/Iw/EDoUW3R7cDdOZ1qxoOPtVXXM7x9icZ3q9tS8VkRfUr+CCR2apvzjC9UhiqZT9UFAAAAP92eIg4gH91b38P/sO/ydSN6+Naec+wtcm7QwBkgkJB0Y6CNogSlk8ukn38Iiu4lOYNWql/M9fzjJJIMPKxhHA=="

blockResponseBytes, err := Codec.Marshal(Version, blockResponse)
assert.NoError(t, err)
assert.Equal(t, base64BlockResponse, base64.StdEncoding.EncodeToString(blockResponseBytes))
encoded := base64.StdEncoding.EncodeToString(blockResponseBytes)
t.Log("BlockResponse base64:", encoded)
assert.Equal(t, base64BlockResponse, encoded)

var b BlockResponse
_, err = Codec.Unmarshal(blockResponseBytes, &b)
Expand Down
12 changes: 2 additions & 10 deletions plugin/evm/message/code_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package message

import (
"encoding/base64"
"math/rand"
"testing"

"github.com/ava-labs/libevm/common"
Expand Down Expand Up @@ -34,19 +33,12 @@ func TestMarshalCodeRequest(t *testing.T) {
// TestMarshalCodeResponse asserts that the structure or serialization logic hasn't changed, primarily to
// ensure compatibility with the network.
func TestMarshalCodeResponse(t *testing.T) {
// generate some random code data
// set random seed for deterministic random
rand.Seed(1)
codeData := make([]byte, 50)
_, err := rand.Read(codeData)
assert.NoError(t, err)

codeData := deterministicBytes("code", 50)
codeResponse := CodeResponse{
Data: [][]byte{codeData},
}

base64CodeResponse := "AAAAAAABAAAAMlL9/AchgmVPFj9fD5piHXKVZsdNEAN8TXu7BAfR4sZJgYVa2GgdDYbR6R4AFnk5y2aU"

base64CodeResponse := "AAAAAAABAAAAMqWkzsbJB88MjA9jmA3E46NFQ4OcMsZ29w75hOzrBZKYUb1mOf7XwLSCavbAa/5PTM46"
codeResponseBytes, err := Codec.Marshal(Version, codeResponse)
assert.NoError(t, err)
assert.Equal(t, base64CodeResponse, base64.StdEncoding.EncodeToString(codeResponseBytes))
Expand Down
26 changes: 26 additions & 0 deletions plugin/evm/message/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package message

import (
"crypto/sha256"
"fmt"
)

func deterministicBytes(label string, n int) []byte {
buf := make([]byte, n)
off := 0
var ctr uint64
for off < n {
h := sha256.Sum256([]byte(fmt.Sprintf("%s-%d", label, ctr)))
toCopy := n - off
if toCopy > len(h) {
toCopy = len(h)
}
copy(buf[off:], h[:toCopy])
off += toCopy
ctr++
}
return buf
}
47 changes: 19 additions & 28 deletions plugin/evm/message/leafs_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package message

import (
"encoding/base64"
"math/rand"
"fmt"
"testing"

"github.com/ava-labs/libevm/common"
Expand All @@ -15,18 +15,11 @@ import (
// TestMarshalLeafsRequest asserts that the structure or serialization logic hasn't changed, primarily to
// ensure compatibility with the network.
func TestMarshalLeafsRequest(t *testing.T) {
// generate some random code data
// set random seed for deterministic random
rand.Seed(1)

startBytes := make([]byte, common.HashLength)
endBytes := make([]byte, common.HashLength)

_, err := rand.Read(startBytes)
assert.NoError(t, err)

_, err = rand.Read(endBytes)
assert.NoError(t, err)
copy(startBytes, deterministicBytes("leafs-start", common.HashLength))
copy(endBytes, deterministicBytes("leafs-end", common.HashLength))

leafsRequest := LeafsRequest{
Root: common.BytesToHash([]byte("im ROOTing for ya")),
Expand All @@ -36,11 +29,13 @@ func TestMarshalLeafsRequest(t *testing.T) {
NodeType: StateTrieNode,
}

base64LeafsRequest := "AAAAAAAAAAAAAAAAAAAAAABpbSBST09UaW5nIGZvciB5YQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIFL9/AchgmVPFj9fD5piHXKVZsdNEAN8TXu7BAfR4sZJAAAAIIGFWthoHQ2G0ekeABZ5OctmlNLEIqzSCKAHKTlIf2mZBAAB"
base64LeafsRequest := "AAAAAAAAAAAAAAAAAAAAAABpbSBST09UaW5nIGZvciB5YQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIJKwrrLuvEv+pCsRmxCs6C3pT+zQMou38OjnrYUylszpAAAAILaro5a21oPenq7+x6aZ3RSpIQkoga9KNpz82H8eyrV5BAAB"

leafsRequestBytes, err := Codec.Marshal(Version, leafsRequest)
assert.NoError(t, err)
assert.Equal(t, base64LeafsRequest, base64.StdEncoding.EncodeToString(leafsRequestBytes))
encoded := base64.StdEncoding.EncodeToString(leafsRequestBytes)
t.Log("LeafsRequest base64:", encoded)
assert.Equal(t, base64LeafsRequest, encoded)

var l LeafsRequest
_, err = Codec.Unmarshal(leafsRequestBytes, &l)
Expand All @@ -55,32 +50,26 @@ func TestMarshalLeafsRequest(t *testing.T) {
// TestMarshalLeafsResponse asserts that the structure or serialization logic hasn't changed, primarily to
// ensure compatibility with the network.
func TestMarshalLeafsResponse(t *testing.T) {
// generate some random code data
// set random seed for deterministic random
rand.Seed(1)

keysBytes := make([][]byte, 16)
valsBytes := make([][]byte, 16)
for i := range keysBytes {
keysBytes[i] = make([]byte, common.HashLength)
valsBytes[i] = make([]byte, rand.Intn(8)+8) // min 8 bytes, max 16 bytes
size := 8 + (i % 9)
valsBytes[i] = make([]byte, size)

_, err := rand.Read(keysBytes[i])
assert.NoError(t, err)
_, err = rand.Read(valsBytes[i])
assert.NoError(t, err)
copy(keysBytes[i], deterministicBytes(fmt.Sprintf("leafs-key-%d", i), common.HashLength))
copy(valsBytes[i], deterministicBytes(fmt.Sprintf("leafs-val-%d", i), size))
}

nextKey := make([]byte, common.HashLength)
_, err := rand.Read(nextKey)
assert.NoError(t, err)
copy(nextKey, deterministicBytes("leafs-next-key", common.HashLength))

proofVals := make([][]byte, 4)
for i := range proofVals {
proofVals[i] = make([]byte, rand.Intn(8)+8) // min 8 bytes, max 16 bytes
size := 8 + (i % 9)
proofVals[i] = make([]byte, size)

_, err = rand.Read(proofVals[i])
assert.NoError(t, err)
copy(proofVals[i], deterministicBytes(fmt.Sprintf("leafs-proof-%d", i), size))
}

leafsResponse := LeafsResponse{
Expand All @@ -90,11 +79,13 @@ func TestMarshalLeafsResponse(t *testing.T) {
ProofVals: proofVals,
}

base64LeafsResponse := "AAAAAAAQAAAAIE8WP18PmmIdcpVmx00QA3xNe7sEB9HixkmBhVrYaB0NAAAAIGagByk5SH9pmeudGKRHhARdh/PGfPInRumVr1olNnlRAAAAIK2zfFghtmgLTnyLdjobHUnUlVyEhiFjJSU/7HON16niAAAAIIYVu9oIMfUFmHWSHmaKW98sf8SERZLSVyvNBmjS1sUvAAAAIHHb2Wiw9xcu2FeUuzWLDDtSXaF4b5//CUJ52xlE69ehAAAAIPhMiSs77qX090OR9EXRWv1ClAQDdPaSS5jL+HE/jZYtAAAAIMr8yuOmvI+effHZKTM/+ZOTO+pvWzr23gN0NmxHGeQ6AAAAIBZZpE856x5YScYHfbtXIvVxeiiaJm+XZHmBmY6+qJwLAAAAIHOq53hmZ/fpNs1PJKv334ZrqlYDg2etYUXeHuj0qLCZAAAAIHiN5WOvpGfUnexqQOmh0AfwM8KCMGG90Oqln45NpkMBAAAAIKAQ13yW6oCnpmX2BvamO389/SVnwYl55NYPJmhtm/L7AAAAIAfuKbpk+Eq0PKDG5rkcH9O+iZBDQXnTr0SRo2kBLbktAAAAILsXyQKL6ZFOt2ScbJNHgAl50YMDVvKlTD3qsqS0R11jAAAAIOqxOTXzHYRIRRfpJK73iuFRwAdVklg2twdYhWUMMOwpAAAAIHnqPf5BNqv3UrO4Jx0D6USzyds2a3UEX479ad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AIOMYINGZvfG5FkaMfFlmU9YH6iTkno0XYJRdzz5wmzvkAAAAIGxx030ahNis5MO4BKmrebrSM66owbYrvOh2aC4wXBHsAAAAIPy2H9p6M+09QiQ5LJEQ/t5aUiWu4JYay996TX970dbKAAAAICk/c3GuBZW6aaj1JOGJ0TV7XJlzyY5wdyRMph1Pvu88AAAAEAAAAAheRm4y4L4/egAAAAl4BX68DVmqf5oAAAAK28t5TDoKXe3mTwAAAAvU000vwEklyciOvwAAAAyMpkqLoo9xc7t3/c4AAAANkNAZCfHhlgAL7dUnAwAAAA5TIzp83z0wYkVwNOxi8QAAAA9+g6PBPoROJ4rj5NnchCgAAAAQDh3Z3uqi4VpY/dqO6Q4pRwAAAAjECTPaIxeFOgAAAAnol1UASNalh6AAAAAKxZ3p1yeNLQ875QAAAAsv1HBF3rWoiq5YYwAAAAyOBRehPDv4UGOK1cMAAAANqykJIIyZKGT8Ag2FTwAAAA4CEC5Xar3pHA0bb0cDCQAAAAQAAAAIOX+OwhaAm/YAAAAJ3JVfblHw6v/oAAAACp0GQdu6CmNquyYAAAAL/FNLguGJX8xGQ7Q="

leafsResponseBytes, err := Codec.Marshal(Version, leafsResponse)
assert.NoError(t, err)
assert.Equal(t, base64LeafsResponse, base64.StdEncoding.EncodeToString(leafsResponseBytes))
encoded := base64.StdEncoding.EncodeToString(leafsResponseBytes)
t.Log("LeafsResponse base64:", encoded)
assert.Equal(t, base64LeafsResponse, encoded)

var l LeafsResponse
_, err = Codec.Unmarshal(leafsResponseBytes, &l)
Expand Down
3 changes: 1 addition & 2 deletions sync/handlers/leafs_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package handlers
import (
"bytes"
"context"
"math/rand"
"crypto/rand"
"testing"

"github.com/ava-labs/avalanchego/ids"
Expand All @@ -26,7 +26,6 @@ import (
)

func TestLeafsRequestHandler_OnLeafsRequest(t *testing.T) {
rand.Seed(1)
testHandlerStats := &statstest.TestHandlerStats{}
memdb := rawdb.NewMemoryDatabase()
trieDB := triedb.NewDatabase(memdb, nil)
Expand Down
Loading
Loading