From 295c83b5d9415d9659354fc7c148a1c2500d59b1 Mon Sep 17 00:00:00 2001 From: Siddy J Date: Mon, 20 Nov 2023 14:06:53 -0800 Subject: [PATCH] added logger --- go.mod | 4 ++-- go.sum | 5 ++++ main.go | 73 +++++++++++++++++++++++++++++---------------------------- 3 files changed, 44 insertions(+), 38 deletions(-) diff --git a/go.mod b/go.mod index 2ffbc4e6..7e8e8441 100644 --- a/go.mod +++ b/go.mod @@ -19,12 +19,12 @@ require ( github.com/goccy/go-yaml v1.9.2 // indirect github.com/klauspost/cpuid/v2 v2.2.3 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/mitchellh/mapstructure v1.4.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prysmaticlabs/go-bitfield v0.0.0-20210809151128-385d8c5e3fb7 // indirect golang.org/x/crypto v0.14.0 // indirect - golang.org/x/sys v0.13.0 // indirect + golang.org/x/sys v0.14.0 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 6b18ed6e..7cfcfa3a 100644 --- a/go.sum +++ b/go.sum @@ -39,6 +39,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM= github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8= github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag= @@ -53,6 +55,7 @@ github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A= github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= @@ -73,6 +76,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go index bf2f26fa..ae4cdb9f 100644 --- a/main.go +++ b/main.go @@ -4,18 +4,21 @@ import ( "encoding/hex" "encoding/json" "flag" - "fmt" "os" "github.com/attestantio/go-eth2-client/spec/capella" "github.com/attestantio/go-eth2-client/spec/phase0" ssz "github.com/ferranbt/fastssz" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" ) // this needs to be hand crafted. If you want the root of the header at the slot x, // then look for entry in (x)%SLOTS_PER_HISTORICAL_ROOT in the block_roots. func main() { + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + // Defining flags for all the parameters command := flag.String("command", "", "The command to execute") @@ -38,7 +41,7 @@ func main() { // Check if the required 'command' flag is provided if *command == "" { - fmt.Println("Error: command flag is required") + log.Debug().Msg("Error: command flag is required") return } @@ -54,7 +57,7 @@ func main() { GenerateBalanceUpdateProof(*oracleBlockHeaderFile, *stateFile, *validatorIndex, *chainID, *outputFile) default: - fmt.Println("Unknown command:", *command) + log.Debug().Str("Unknown command:", *command) } } @@ -64,30 +67,31 @@ func GenerateValidatorFieldsProof(oracleBlockHeaderFile string, stateFile string var oracleBeaconBlockHeader phase0.BeaconBlockHeader stateJSON, err := parseStateJSONFile(stateFile) if err != nil { - fmt.Println("error with JSON parsing") + log.Debug().Msg("error with JSON parsing") } ParseCapellaBeaconStateFromJSON(*stateJSON, &state) oracleBeaconBlockHeader, err = ExtractBlockHeader(oracleBlockHeaderFile) if err != nil { - fmt.Println("read error with header file") + log.Debug().AnErr("Error with parsing header file", err) + } beaconStateRoot, err := state.HashTreeRoot() if err != nil { - fmt.Println("Error with HashTreeRoot of state", err) + log.Debug().AnErr("Error with HashTreeRoot of state", err) } epp, err := NewEigenPodProofs(chainID, 1000) if err != nil { - fmt.Println("Error creating EPP object", err) + log.Debug().AnErr("Error creating EPP object", err) } stateRootProof, validatorFieldsProof, err := epp.ProveValidatorFields(&oracleBeaconBlockHeader, &state, uint64(validatorIndex)) if err != nil { - fmt.Println("Error with ProveValidatorFields", err) + log.Debug().AnErr("Error with ProveValidatorFields", err) } proofs := WithdrawalCredentialProofs{ @@ -100,7 +104,7 @@ func GenerateValidatorFieldsProof(oracleBlockHeaderFile string, stateFile string proofData, err := json.Marshal(proofs) if err != nil { - fmt.Println("error") + log.Debug().AnErr("JSON marshal error: ", err) } _ = os.WriteFile(output, proofData, 0644) @@ -113,39 +117,39 @@ func GenerateBalanceUpdateProof(oracleBlockHeaderFile string, stateFile string, var oracleBeaconBlockHeader phase0.BeaconBlockHeader stateJSON, err := parseStateJSONFile(stateFile) if err != nil { - fmt.Println("error with JSON parsing") + log.Debug().AnErr("error with JSON parsing", err) } ParseCapellaBeaconStateFromJSON(*stateJSON, &state) oracleBeaconBlockHeader, err = ExtractBlockHeader(oracleBlockHeaderFile) if err != nil { - fmt.Println("read error with header file") + log.Debug().AnErr("Error with parsing header file", err) } beaconStateRoot, err := state.HashTreeRoot() if err != nil { - fmt.Println("Error with HashTreeRoot of state", err) + log.Debug().AnErr("Error with HashTreeRoot of state", err) } epp, err := NewEigenPodProofs(chainID, 1000) if err != nil { - fmt.Println("Error creating EPP object", err) + log.Debug().AnErr("Error creating EPP object", err) } balanceRootList, err := GetBalanceRoots(state.Balances) if err != nil { - fmt.Println("Error with GetBalanceRoots", err) + log.Debug().AnErr("Error with GetBalanceRoots", err) } balanceRoot := balanceRootList[validatorIndex/4] balanceProof, err := epp.ProveValidatorBalance(&oracleBeaconBlockHeader, &state, uint64(validatorIndex)) if err != nil { - fmt.Println("Error with ProveValidatorBalance", err) + log.Debug().AnErr("Error with ProveValidatorBalance", err) } stateRootProof, validatorFieldsProof, err := epp.ProveValidatorFields(&oracleBeaconBlockHeader, &state, uint64(validatorIndex)) if err != nil { - fmt.Println("Error with ProveValidatorFields", err) + log.Debug().AnErr("Error with ProveValidatorFields", err) } proofs := BalanceUpdateProofs{ ValidatorIndex: uint64(validatorIndex), @@ -159,7 +163,7 @@ func GenerateBalanceUpdateProof(oracleBlockHeaderFile string, stateFile string, proofData, err := json.Marshal(proofs) if err != nil { - fmt.Println("this error") + log.Debug().AnErr("JSON marshal error: ", err) } _ = os.WriteFile(output, proofData, 0644) @@ -190,29 +194,29 @@ func GenerateWithdrawalFieldsProof( oracleBeaconBlockHeader, err := ExtractBlockHeader(oracleBlockHeaderFile) if err != nil { - fmt.Println("read error with header file") + log.Debug().AnErr("Error with parsing header file", err) } stateJSON, err := parseStateJSONFile(stateFile) if err != nil { - fmt.Println("error with JSON parsing state file") + log.Debug().AnErr("error with JSON parsing state file", err) } ParseCapellaBeaconStateFromJSON(*stateJSON, &state) historicalSummaryJSON, err := parseStateJSONFile(historicalSummaryStateFile) if err != nil { - fmt.Println("error with JSON parsing historical summary state file") + log.Debug().AnErr("error with JSON parsing historical summary state file", err) } ParseCapellaBeaconStateFromJSON(*historicalSummaryJSON, &historicalSummaryState) withdrawalBlockHeader, err = ExtractBlockHeader(blockHeaderFile) if err != nil { - fmt.Println("read error with header file") + log.Debug().AnErr("Error with parsing header file", err) } withdrawalBlock, err = ExtractBlock(blockBodyFile) if err != nil { - fmt.Println("read error with body file") + log.Debug().AnErr("Error with parsing body file", err) } hh := ssz.NewHasher() @@ -222,48 +226,45 @@ func GenerateWithdrawalFieldsProof( // validatorIndex := phase0.ValidatorIndex(index) beaconStateRoot, err := state.HashTreeRoot() if err != nil { - fmt.Println("Error with HashTreeRoot of state", err) + log.Debug().AnErr("Error with HashTreeRoot of state", err) } slot := withdrawalBlockHeader.Slot hh.PutUint64(uint64(slot)) slotRoot := ConvertTo32ByteArray(hh.Hash()) - if err != nil { - fmt.Println("Error with HashTreeRoot of latestBlockHeader", err) - } - timestamp := withdrawalBlock.Body.ExecutionPayload.Timestamp hh.PutUint64(uint64(timestamp)) timestampRoot := ConvertTo32ByteArray(hh.Hash()) blockHeaderRoot, err := withdrawalBlockHeader.HashTreeRoot() if err != nil { - fmt.Println("Error with HashTreeRoot of latestBlockHeader", err) + log.Debug().AnErr("Error with HashTreeRoot of blockHeader", err) } executionPayloadRoot, err := withdrawalBlock.Body.ExecutionPayload.HashTreeRoot() if err != nil { - fmt.Println("Error with HashTreeRoot of executionPayload", err) + log.Debug().AnErr("Error with HashTreeRoot of executionPayload", err) } epp, err := NewEigenPodProofs(chainID, 1000) if err != nil { - fmt.Println("Error creating EPP object", err) + log.Debug().AnErr("Error creating EPP object", err) } oracleBeaconStateTopLevelRoots, err := epp.ComputeBeaconStateTopLevelRoots(&state) - //blockHeaderProof, slotProof, withdrawalProof, validatorProof, timestampProof, executionPayloadProof, stateRootAgainstLatestBlockHeaderProof, historicalSummaryProof, err := - // withdrawalProof, stateRootProof, validatorProof, err := epp.ProveWithdrawal(&oracleBeaconBlockHeader, &oracleState, historicalSummaryState.BlockRoots, &withdrawalBlock, validatorIndex) + if err != nil { + log.Debug().AnErr("Error with ComputeBeaconStateTopLevelRoots", err) + } withdrawalProof, err := epp.ProveWithdrawal(&oracleBeaconBlockHeader, &state, oracleBeaconStateTopLevelRoots, historicalSummaryState.BlockRoots, &withdrawalBlock, uint64(validatorIndex), FIRST_CAPELLA_SLOT_GOERLI) if err != nil { - fmt.Println("ProveWithdrawal error", err) + log.Debug().AnErr("Error with ProveWithdrawal", err) } stateRootProof, err := ProveStateRootAgainstBlockHeader(&oracleBeaconBlockHeader) if err != nil { - fmt.Println("ProveStateRootAgainstBlockHeader error", err) + log.Debug().AnErr("Error with ProveStateRootAgainstBlockHeader", err) } validatorProof, err := epp.ProveValidatorAgainstBeaconState(&state, oracleBeaconStateTopLevelRoots, uint64(validatorIndex)) if err != nil { - fmt.Println("ProveValidatorAgainstBeaconState error", err) + log.Debug().AnErr("Error with ProveValidatorAgainstBeaconState", err) } proofs := WithdrawalProofs{ StateRootAgainstLatestBlockHeaderProof: ConvertBytesToStrings(stateRootProof), @@ -287,7 +288,7 @@ func GenerateWithdrawalFieldsProof( proofData, err := json.Marshal(proofs) if err != nil { - fmt.Println("error") + log.Debug().AnErr("JSON marshal error: ", err) } _ = os.WriteFile(outputFile, proofData, 0644)